﻿// JScript File

function ShowMenu(pMenuID)
{
    var lMenuItem = "ctl00_"+pMenuID;
    document.getElementById(lMenuItem).className="active";

}

function setactiveproditem(pLink,pActiveDiv)
{
    if (document.getElementById('tabonediv'))
    {
        document.getElementById('tabonediv').style.display = 'none';
    }
    
    if (document.getElementById('tabtwodiv') )
    {
        document.getElementById('tabtwodiv').style.display = 'none';
    }
    
    if (document.getElementById('tabthreediv') )
    {
        document.getElementById('tabthreediv').style.display = 'none';
    }
    
    if (document.getElementById('tabfourdiv') )
    {
        document.getElementById('tabfourdiv').style.display = 'none';
    }
    
    if (document.getElementById('tabfivediv') ) 
    {
        document.getElementById('tabfivediv').style.display = 'none';
    }
    
    if (document.getElementById('tabsixdiv'))
    {
        document.getElementById('tabsixdiv').style.display = 'none';
    }
    //alert(pActiveDiv);
    if (document.getElementById(pActiveDiv))
    {
        document.getElementById(pActiveDiv).style.display ='block';
    }
}

function validateInteger(pEdit) {

    var elem = document.getElementById(pEdit);
    if (elem.value == "0") {
        document.getElementById(pEdit).value = "";
        alert("invalid number");
    }
    else {
        switch (isInteger(elem.value)) {
            case true:
                break;
            case false:
                document.getElementById(pEdit).value = "";
                alert("invalid number");
        }
    }

}

function validateIntegerValue(pControl) {
    if (!isInteger(pControl.value)) {
        pControl.value = "";
        if (document.getElementById("hf_ErrorMessage") != null) {
            document.getElementById("hf_ErrorMessage").value = "Invalid value - please do not enter letters, dollars signs, or commas";
            CheckErrors(true);
            pControl.focus();
        }
        else {
            alert("invalid number");
        }
    }
    else {
        if (document.getElementById("hf_ErrorMessage") != null) {
            dismissboxv2();
            document.getElementById("hf_ErrorMessage").value = "";
        }
    }

}


function validateDecimal(pEdit) {
    var elem = document.getElementById(pEdit);
    var lIsValid = true;

    if (elem != null && elem.value != " " && elem.value != "") {
        if (elem.value.indexOf(".") > -1)//if the value has a decimal
        {
            if ((elem.value.length - (elem.value.indexOf(".") + 1)) > 2)//no more than 2 decimal places
            {
                alert("invalid decimal value");
                lIsValid = false;
            }

        }
        else {
            if (parseInt(elem.value) < 0) //if it doesn't have a decimal and is just a number
            {
                alert("invalid value");
                lIsValid = false;
            }

        }
    }
    return lIsValid;
}



function isInteger(s) {
    var i;

    if (isEmpty(s))
        if (isInteger.arguments.length == 1) return 0;
        else return (isInteger.arguments[1] == true);

    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    return true;
}

function isEmpty(s) {
    return ((s == null) || (s.length == 0))
}

function isDigit(c) {
    return ((c >= "0") && (c <= "9"))
}



function FormatPhoneNumber(pControl) {

    if (NotAnIgnoreKey()) {
        if (pControl.value != null && pControl.value != "") {

            lResult = RemoveAllPhoneCharacters(pControl.value);

            if (isValidPhoneDigits(lResult)) {

                lHasPlus = pControl.value.indexOf("+");

                if (lHasPlus > -1) {

                    pControl.value = lResult;

                } else {
                    //probably US Phone
                    isFormatted = false;
                    if (lResult.length > 1) {
                        if (pControl.value[0] == "1") {
                            lResult = trim(lResult.substring(1, lResult.length));
                        }
                        if (lResult.length == 2) {
                            if (pControl.value[0] == "1") {
                                lResult = " (" + lResult + ")";
                                isFormatted = true;
                            }

                        } else if (lResult.length >= 3 && lResult.length < 7) {
                            lResult = "(" + lResult.substring(0, 3) + ")" + lResult.substring(3, lResult.length);
                            isFormatted = true;
                        } else if (lResult.length >= 7 && lResult.length < 10) {
                            lResult = "(" + lResult.substring(0, 3) + ")" + lResult.substring(3, lResult.length);
                            isFormatted = true;
                        } else if (lResult.length == 10) {
                            lResult = "(" + lResult.substring(0, 3) + ")" + lResult.substring(3, 6) + "-" + lResult.substring(6, lResult.length);
                            isFormatted = true;
                        } else if (lResult.length > 10 && lResult.indexOf("x") > 0) {
                            lResult = "(" + lResult.substring(0, 3) + ")" + lResult.substring(3, 6) + "-" + lResult.substring(6, 10) + " " + trim(lResult.substring(10, lResult.length));
                            isFormatted = true;
                        }

                        if (isFormatted) {
                            if (lResult != "") {
                                if (pControl.value[0] == "1") {
                                    pControl.value = "1 " + lResult;
                                }
                                else {
                                    pControl.value = lResult;
                                }
                            }
                        } else {

                            //if the function didn't format the number don't do anything to it- this is for a paste operation where the
                            //format falls outside of our standard.
                        }
                    }
                }
            }

        } else {
            alert("Invalid Phone Character");
            return "";
        }
    }

}

function isValidPhoneDigits(pValue) {
    if (pValue.length > 1) {

        for (var i = 0; i < pValue.length; i++) {
            lChar = pValue[i];

            if (lChar != "x") {
                return true;
            }
            else {

                return isInteger(lChar);
            }
        }

    }
    else {
        return true;
    }
}

function NotAnIgnoreKey() {
    lresult = true;
    lKeyCode = event.keyCode;
    if (lKeyCode < 48) {
        lresult = false;
    }
    else if (lKeyCode > 90 && lKeyCode < 93) {
        lresult = false;
    }
    else if (lKeyCode > 106) {
        lresult = false;
    }
    if (lKeyCode == 107 || lKeyCode == 187) {
        lresult = true;
    }
    return lresult;
}

function RemoveAllPhoneCharacters(pValue) {
    lResult = "";
    lResult = RemoveCharacter("(", pValue);
    lResult = RemoveCharacter(")", lResult);
    lResult = RemoveCharacter("-", lResult);
    return lResult;
}


function RemoveCharacter(pChar, pValue) {
    if (pValue.length > 1) {
        lResult = "";
        for (var i = 0; i < pValue.length; i++) {
            lChar = pValue[i];
            if (lChar != pChar) {
                lResult += lChar;
            }
        }
        return lResult;
    }
    else {
        return pValue;
    }
}

