﻿function getXmlhttpRequestObject() {
    try {
        var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e) {
            alert('Error');
        }
    }
    return xmlhttp;
}


function DisplayEvent(event) {

if  (document.getElementById(event).className == 'invisible')
    document.getElementById(event).className = 'visible';
else if (document.getElementById(event).className == 'visible')
 document.getElementById(event).className = 'invisible'; 
    
}

function jsPopupAtXY(popupNode, x, y) {
    var xPopupNode = document.getElementById(popupNode);
    xPopupNode.className = 'popup';
    xPopupNode.style.left = x;
    xPopupNode.style.top = y;
}

function jsPopup(targetNode, popupNode) {
    var xPopupNode = document.getElementById(popupNode);
    xPopupNode.className = 'popup';
    if (targetNode.length > 0) {
        var xNode = document.getElementById(targetNode);
        var y = xNode.offsetTop;
        while (xNode.tagName !== 'BODY') {
            xNode = xNode.offsetParent;
            y = y + xNode.offsetTop;
        }

        xPopupNode.style.left = (screen.width / 2) - (document.getElementById(popupNode).offsetWidth / 2);
        xPopupNode.style.top = y - document.getElementById(popupNode).offsetHeight;
    }
    else {
        xPopupNode.style.left = (screen.width / 2) - (document.getElementById(popupNode).offsetWidth / 2);
        xPopupNode.style.top = 100; //(screen.height / 2) - (document.getElementById(popupNode).offsetHeight /2);
    }
}

function jsPopup2(targetNode, popupNode, className, vDirection) {
    var xPopupNode = document.getElementById(popupNode);
    xPopupNode.className = className;
    if (targetNode.length > 0) {
        var xNode = document.getElementById(targetNode);
        var y = xNode.offsetTop;
        while (xNode !== null) {
            y = y + xNode.offsetTop;
            xNode = xNode.offsetParent;
        }

        xPopupNode.style.left = (screen.width / 2) - (document.getElementById(popupNode).offsetWidth / 2);
        if (vDirection == 'down') {
            xPopupNode.style.top = y;
        }
        else {
            xPopupNode.style.top = y - document.getElementById(popupNode).offsetHeight;
        }
    }
    else {
        xPopupNode.style.left = (screen.width / 2) - (document.getElementById(popupNode).offsetWidth / 2);
        xPopupNode.style.top = 100; //(screen.height / 2) - (document.getElementById(popupNode).offsetHeight /2);
    }
}

function hidePopup(popupNode) {
    document.getElementById(popupNode).className = 'hidden';
}

function validatePhone(source, args) {
    var result = checkPhone(args.Value);
    if (result == false) {
        args.IsValid = false;
    } else {
        args.IsValid = true;
        document.all[source.controltovalidate].value = result;
    }
}

//weakly validates a phone number, allowing any 3 or more digit number with only + or - or blank spaces added
function checkPhone(toCheck) {

    // check for a length of less than 3 - if so, return false
    if (toCheck.length < 3) { return false; }

    //first character can be a + or 0-9
    if ((toCheck.charAt(0) >= '0' && toCheck.charAt(x) <= '9') || toCheck.charAt(0) == '+') { /* do nothing */ }
    else { return false; }

    // loop through the rest of the characters of the string
    for (var x = 1; x < toCheck.length; x++) {
        // if the character is < 0 or > 9, return false (not a number)
        if ((toCheck.charAt(x) >= '0' && toCheck.charAt(x) <= '9') || (toCheck.charAt(x) == '-') || (toCheck.charAt(x) == ' ')) { /* do nothing */ }
        else { return false; }
    }

    return toCheck;
}

function isNumeric(toCheck) {

    // check for a length of 0 - if so, return false
    if (toCheck.length == 0) { return false; }
    else if (toCheck.length == 1 && (toCheck.charAt(0) == '.' || toCheck.charAt(0) == ',' || (toCheck.charAt(0) == '-'))) { return false; }

    // loop through each character of the string
    for (var x = 0; x < toCheck.length; x++) {
        // if the character is < 0 or > 9, return false (not a number)
        if ((toCheck.charAt(x) >= '0' && toCheck.charAt(x) <= '9') || toCheck.charAt(x) == '.' || toCheck.charAt(x) == ',' || (toCheck.charAt(x) == '-' && x == 0)) { /* do nothing */ }
        else { return false; }
    }

    // made it through the loop - we have a number
    return true;
}


function checkTextAreaMaxLength(textBox, e, length) {

    var mLen = textBox["MaxLength"];
    if (null == mLen)
        mLen = length;

    var maxLength = parseInt(mLen);
    if (!checkSpecialKeys(e)) {
        if (textBox.value.length > maxLength - 1) {
            if (window.event)//IE
                e.returnValue = false;
            else//Firefox
                e.preventDefault();
        }
    }
}
function checkSpecialKeys(e) {
    if (e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40)
        return false;
    else
        return true;
}  
    