﻿/*******************************************************
Global Javascript Library
-scott 3/8/09
Last Update: 8/14/09
********************************************************/

/*******************************************************
Element Reference Functions
********************************************************/

// Returns a reference to an object
function gfunGetObject(sObj) {
    var obj = sObj;
    if (typeof sObj == "string") {
        if (document.getElementById) obj = document.getElementById(sObj);
        else if (document.all) obj = document.all[sObj];
        else if (document.layers) obj = document.layers[sObj];
        else {
            try {
                obj = eval("document." + sObj);
            }
            catch (e) {
                obj == null;
            }
        }
    }

    return obj;
}

// Returns a reference to an object's style
function gfunGetObjectStyle(sObj) {
    var obj = gfunGetObject(sObj);
    if (obj != null) {
        if (obj.style != null) return obj.style;
        else if (obj.css != null) return obj.css;
        else return obj;
    }
    return null;
}

// Returns the value of an attribute
function gfunGetAttributeValue(sObj, sAttribute) {
    var obj = gfunGetObject(sObj);

    if (obj != null) {
        if (obj.getAttribute) if (obj.getAttribute(sAttribute) != null) return obj.getAttribute(sAttribute);
        if (obj.getAttributeNode) if (obj.getAttributeNode(sAttribute) != null) return obj.getAttributeNode(sAttribute).value;
        if (obj.attributes) if (obj.attributes.getNamedItem) if (obj.attributes.getNamedItem(sAttribute)) return obj.attributes.getNamedItem(sAttribute).value;
        if (obj[sAttribute] != null) return obj[sAttribute];
    }
    return null;
}

// Sets the value of an attribute
function gfunSetAttributeValue(sObj, sAttribute, sValue) {
    var obj = gfunGetObject(sObj);

    if (obj != null) {
        if (obj.setAttribute) obj.setAttribute(sAttribute, sValue);
        else if (document.createAttribute) {
            var attr = document.createAttribute(sAttribute);
            attr.value = sValue;
            if (obj.setAttributeNode) obj.setAttributeNode(attr);
            else if (obj.attributes != null) obj.attributes.setNamedItem(attr);
        }
        else if (obj[sAttribute] != null) obj[sAttribute] = sValue;
    }
    return null;
}

// Updates the all the existing content of an element to the specified text. (NO HTML)
function gfunUpdateElementText(sObj, sText) {
    var obj = gfunGetObject(sObj);

    if (obj != null) {
        if (obj.innerHTML) obj.innerHTML = sText;
        else if (document.createTextNode && obj.appendChild) {
            while (obj.hasChildNodes()) { obj.removeChild(obj.lastChild); }
            var objTextNode = document.createTextNode(sText);
            obj.appendChild(objTextNode);
        }
    }
}

/*******************************************************
Class Name Functions
********************************************************/

// Gets the Class Name of an object
function gfunGetClass(sObj) {
    var obj = gfunGetObject(sObj);
    var sClass = "";
    if (obj.className != null) sClass = obj.className;
    else if (obj.attributes != null) if (obj.attributes.getNamedItem("class")) sClass = obj.attributes.getNamedItem("class").value;
    return sClass;
}

// Updates the Class Name of an object
function gfunSetClass(sObj, sClass) {
    var obj = gfunGetObject(sObj);
    gfunSetAttributeValue(obj, "class", sClass)
    if (obj.className != null) obj.className = sClass;
    return sClass;
}

// Append a new class onto the existing class for an object
function gfunAppendClass(sObj, sClass) {
    var obj = gfunGetObject(sObj);
    // First get the existing Class
    var sNewClass = gfunGetClass(sObj);
    if (sNewClass == "" || sNewClass == null) sNewClass = sClass;
    else if (sNewClass.indexOf(sClass) == -1) sNewClass = sNewClass + " " + sClass;
    // Finally, set the class of the object
    gfunSetClass(sObj, sNewClass);
}

// Remove class from the existing class for an object
function gfunRemoveClass(sObj, sClass) {
    var obj = gfunGetObject(sObj);
    var sNewClass = gfunGetClass(sObj);
    if (sNewClass != null && sNewClass != "") {
        if (sNewClass.indexOf(sClass) >= 0) {
            var re = new RegExp("(\s*" + sClass + "\s+)|(\s*" + sClass + "$)", "gi");
            sNewClass = sNewClass.replace(re, "");
        }
        // Finally, set the class of the object
        gfunSetClass(sObj, sNewClass);
    }
}

/*******************************************************
Event Functions
********************************************************/
function gfunCancelEvent(evt) {
    evt = (evt) ? evt : ((event) ? event : null);
    if (evt) {
        if (evt.cancelBubble != null) evt.cancelBubble = true;
        if (evt.stopPropogation != null) evt.stopPropogation();
    }
    // Return False to stop events
    return false;
}

// Fires an Event Programmatically
function gfunFireEvent(sObj, sEvent) {
    var obj = gfunGetObject(sObj);

    if (sEvent.substring(0, 2) == "on") sEvent = sEvent.substring(2, sEvent.length);
    
    //On IE
    if (obj.fireEvent) {
        obj.fireEvent("on" + sEvent);
    }
    else if(document.createEvent) {
        //On Gecko based browsers
        var evt = document.createEvent("HTMLEvents");
        if(evt.initEvent) {
            evt.initEvent(sEvent, true, true);
        }
        if (obj.dispatchEvent) {
            obj.dispatchEvent(evt);
        }
    }
}

/*******************************************************
New Window Functions
********************************************************/
// Opens a pop-up window with specific features list
function gfunOpenWin(sURL, sName, nWidth, nHeight, sFeatures) {
    if (sFeatures == null) sFeatures = "location=0,status=0,scrollbars=1,resizable=1";
    sFeatures += ",width=" + nWidth + ",height=" + nHeight;
    return window.open(sURL, sName, sFeatures);
}


/*******************************************************
Show / Hide Functions
********************************************************/

// Show an absolute positioned block element
function gfunShow(sObj) {
    var obj = gfunGetObjectStyle(sObj);
    if (obj != null) obj.visibility = "visible";
}

// Hide an absolute positioned block element
function gfunHide(sObj) {
    var obj = gfunGetObjectStyle(sObj);
    if (obj != null) obj.visibility = "hidden";
}

// Toggle the Visiblity of an element
function gfunToggleVisiblity(sObj) {
    var obj = gfunGetObjectStyle(sObj);
    if (obj != null) {
        if (obj.visibility == "hidden") obj.visibility = "visible";
        else obj.visibility = "hidden";
    }
}

// Display an inline element
function gfunDisplay(sObj) {
    var obj = gfunGetObjectStyle(sObj);
    if (obj != null) obj.display = "block";
}

// Hide an inline element
function gfunCollapse(sObj) {
    var obj = gfunGetObjectStyle(sObj);
    if (obj != null) obj.display = "none";
}

// Toggle the Display of an element
function gfunToggleDisplay(sObj) {
    var obj = gfunGetObjectStyle(sObj);
    if (obj != null) {
        if (obj.display == "none") obj.display = "block";
        else obj.display = "none";
    }
}

/*******************************************************
Position Functions
********************************************************/

// The number of pixels an object is from the top of the window
function gfunGetTop(sObj) {
    var obj = gfunGetObject(sObj);
    var yPos = obj.offsetTop;
    var objParent = obj.offsetParent;
    while (objParent != null) {
        yPos += objParent.offsetTop;
        objParent = objParent.offsetParent;
    }

    return yPos;
}

// The number of pixels an object is from the left of the window
function gfunGetLeft(sObj) {
    var obj = gfunGetObject(sObj);
    var xPos = obj.offsetLeft;
    var objParent = obj.offsetParent;
    while (objParent != null) {
        xPos += objParent.offsetLeft;
        objParent = objParent.offsetParent;
    }

    return xPos;
}

// The X coordinate of a positioned element
function gfunGetPositionLeft(sObj) {
    var obj = gfunGetObject(sObj);
    var result = 0;
    if (obj) {
        if (document.defaultView) {
            var style = document.defaultView;
            var cssDecl = style.getComputedStyle(obj, "");
            result = cssDecl.getPropertyValue("left");
        }
        else if (obj.currentStyle) {
            result = obj.currentStyle.left;
        }
        else if (obj.style) {
            result = obj.style.left;
        }
        else if (obj.left) {
            result = obj.left;
        }
    }
    return parseInt(result);
}

// The Y coordinate of a positioned element
function gfunGetPositionTop(sObj) {
    var obj = gfunGetObject(sObj);
    var result = 0;
    if (obj) {
        if (document.defaultView) {
            var style = document.defaultView;
            var cssDecl = style.getComputedStyle(obj, "");
            result = cssDecl.getPropertyValue("top");
        }
        else if (obj.currentStyle) {
            result = obj.currentStyle.top;
        }
        else if (obj.style) {
            result = obj.style.top;
        }
        else if (obj.top) {
            result = obj.top;
        }
    }
    return parseInt(result);
}

// Move a positionable element
function gfunMoveTo(sObj, x, y) {
    var obj = gfunGetObjectStyle(sObj);
    if (obj != null) {
        if (document.getElementById) {
            // equalize incorrect numeric value type
            var units = (typeof obj.left == "string") ? "px" : 0;
            if (x != null) obj.left = x + units;
            if (y != null) obj.top = y + units;
        }
        else if (obj.moveTo) {
            if (x == null) x = gfunGetLeft(obj);
            if (y == null) y = gfunGetTop(obj);
            obj.moveTo(x, y);
        }
    }
}

/*******************************************************
Dimension Functions
********************************************************/

// Get the Width of an object
function gfunGetWidth(sObj) {
    var obj = gfunGetObject(sObj);
    var result = 0;

    if (obj != null) {
        if (obj.offsetWidth) {
            result = obj.offsetWidth;
        }
        else if (obj.clip && obj.clip.width) {
            result = obj.clip.width;
        }
        else if (obj.style && obj.style.pixelWidth) {
            result = obj.style.pixelWidth;
        }
    }
    return parseInt(result);
}

// Get the Height of an object
function gfunGetHeight(sObj) {
    var obj = gfunGetObject(sObj);
    var result = 0;

    if (obj != null) {
        if (obj.offsetHeight) {
            result = obj.offsetHeight;
        }
        else if (obj.clip && obj.clip.height) {
            result = obj.clip.width;
        }
        else if (obj.style && obj.style.pixelHeight) {
            result = obj.style.pixelHeight;
        }
    }
    return parseInt(result);
}

/*******************************************************
String Helper Functions
********************************************************/

// Remove the HTML Tags from a String
function gfunRemoveHTML(sString) {
    var sTemp = sString;
    if (sTemp != null && sTemp != "") {
        sTemp = sTemp.replace(/<\/?[^>]+(>|$)/g, "");
    }
    return sTemp;
}

// Format a String to be URL Friendly
function gfunURLFormat(sString) {
    var sTemp = gfunRemoveHTML(sString).toLowerCase();
    if (sTemp != null && sTemp != "") {
        sTemp = sTemp.replace(/\s/g, "-");
        sTemp = sTemp.replace(/[^\w\-]/g, "");
    }
    return sTemp;
}


/*******************************************************
CheckBox Validation Functions
********************************************************/
function gfunCheckBoxValidatorDisableButton(chkId, mustBeChecked, btnId) {
    var button = document.getElementById(btnId);
    var chkbox = document.getElementById(chkId);

    if (button && chkbox) {
        button.disabled = (chkbox.checked != mustBeChecked);
    }
}

function gfunCheckBoxValidatorEvaluateIsValid(val) {
    var control = document.getElementById(val.controltovalidate);
    var mustBeChecked = Boolean(val.mustBeChecked == 'true');

    return control.checked == mustBeChecked;
}

function gfunCheckBoxListValidatorEvaluateIsValid(val) {
    var control = document.getElementById(val.controltovalidate);
    var minimumNumberOfSelectedCheckBoxes = parseInt(val.minimumNumberOfSelectedCheckBoxes);

    var selectedItemCount = 0;
    var liIndex = 0;
    var currentListItem = document.getElementById(control.id + '_' + liIndex.toString());
    while (currentListItem != null) {
        if (currentListItem.checked) selectedItemCount++;
        liIndex++;
        currentListItem = document.getElementById(control.id + '_' + liIndex.toString());
    }

    return selectedItemCount >= minimumNumberOfSelectedCheckBoxes;
}

/*******************************************************
    Credit Card Validation Functions
********************************************************/
function gfunUpdateCCRegExPattern(sCardType, sRegExClientID) {
    var sRegExp = "";
    switch (sCardType.toLowerCase())
    {
        case "visa":
            sRegExp = "^[4]([0-9]{15}$|[0-9]{12}$)";
            break;

        case "mastercard":
            sRegExp = "^[5][1-5][0-9]{14}$";
            break;

        case "american express":
            sRegExp = "^[34|37][0-9]{14}$";
            break;

        case "discover":
            sRegExp = "^6011[0-9]{12}$";
            break;

        case "diner's club":
            sRegExp = "(^30[0-5][0-9]{11}$)|(^(36|38)[0-9]{12}$)";
            break;

        case "enroute":
            sRegExp = "^(2014|2149)[0-9]{11}$";
            break;
    }
    
    try {
        eval(sRegExClientID + ".validationexpression = \"" + sRegExp + "\"");
    }
    catch(e) {
        // Do Nothing
        return true;
    }
}