﻿
jQuery(document).ready(function () {

    if (document.getElementById("MainContent_hf_PGID") != null && document.getElementById("MainContent_hf_PGID").value != "") {
        lPGID = document.getElementById("MainContent_hf_PGID").value;
        
        /*ABOUT*/
        if (lPGID == 1625 || lPGID == 1626 || lPGID == 1627 || lPGID == 87 || lPGID == 7) {
            jQuery("li.level1.item2").addClass("active");
        }
        /*PROGRAMS*/
        if (lPGID == 920 || lPGID == 922 || lPGID == 1632 || lPGID == 1633 || lPGID == 79 || lPGID == 1635) {
            jQuery("li.level1.item3").addClass("active");
        }

        /*Publications*/
        if (lPGID == 1231 || lPGID == 98 || lPGID == 1638 ) {
            jQuery("li.level1.item4").addClass("active");
        }

        /*Advocacy*/
        if (lPGID == 1639) {
            jQuery("li.level1.item5").addClass("active");
        }

        /*Resources*/
        if (lPGID == 1028 || lPGID == 1641 || lPGID == 7) {
            jQuery("li.level1.item6").addClass("active");
        }
        /*Board*/
        if (lPGID == 100 || lPGID == 1643 || lPGID == 1644 || lPGID == 1645) {
            jQuery("li.level1.item7").addClass("active");
        }
    }
});


function platformsearch(pValue) {

    if (pValue.length > 2) {

        //do search
        lResObject = SiteMaster.Search(pValue);
        //when results come back
        if (lResObject.value != null && lResObject.value != "") {
            jQuery("search-close").show();
            jQuery("#search_resultbox")[0].innerHTML = lResObject.value;
        }
        jQuery("#search_loading").hide();
        jQuery("#btn_search_close").show();
    }

}


function SubmitSearch() {

    platformsearch(jQuery("#searchword")[0].value);
}

function resetsearch() {
    jQuery("#search_loading").hide();
    jQuery("#search_resultbox")[0].innerHTML = "";
    jQuery("#btn_search_close").hide();
}

function Submit_Signup() {
    lEmailAddress = jQuery("#MainContent_main_content_bottom1_edt_NewsLetterSignup")[0].value;
    if (lEmailAddress != null && lEmailAddress != "") {

        SiteMaster.AddToEmailList(lEmailAddress);
        jQuery("#hf_InformationMessage")[0].value = "Thank You For Joining Our Newsletter";
        jQuery("#MainContent_main_content_bottom1_edt_NewsLetterSignup")[0].value = "";
        CheckInformation(true);
    }
}        
    

function ShowServiceDialog(pProduct_ID) {

    lPageContent = SiteMaster.RequestTemplate("popup_dialog.htm").value;
    lDialogWidth = 500;
    if (pProduct_ID != null && pProduct_ID != "") {
        lProductContent = SiteMaster.Request_ProductContent(pProduct_ID).value;
        lPageContent = lPageContent.replace(/{%DIALOG_CONTENT%}/g, lProductContent);
    }

    lPageContent = lPageContent.replace(/{%WDITH%}/g, lDialogWidth);
    lViewPortWidth = jQuery(window).width();
    var divTag = document.createElement("div");
    divTag.id = "popup_dialog";
    divTag.className = "dynamicDiv";
    lViewPortWidth = jQuery(window).width();
    divTag.style.left = ((lViewPortWidth / 2) - (parseInt(lDialogWidth) / 2)) + "px";
    lDefaulttop = (jQuery("#menu").position().top) + 100;
    divTag.style.top = lDefaulttop + "px";
    document.body.appendChild(divTag);
    jQuery("#popup_dialog")[0].innerHTML = lPageContent;
    jQuery('html, body').animate({ scrollTop: 0 }, 0);

}


function ShowStaffInfo(pStaff_ID) {
    lPageContent = SiteMaster.RequestTemplate("popup_dialog.htm").value;
    lDialogWidth = 500;
    if (pStaff_ID != null && pStaff_ID != "") {
        lStaffContent = SiteMaster.Request_StaffContent(pStaff_ID).value;
        lPageContent = lPageContent.replace(/{%DIALOG_CONTENT%}/g, lStaffContent);
    }

    lPageContent = lPageContent.replace(/{%WDITH%}/g, lDialogWidth);
    lViewPortWidth = jQuery(window).width();
    var divTag = document.createElement("div");
    divTag.id = "popup_dialog";
    divTag.className = "dynamicDiv";
    lViewPortWidth = jQuery(window).width();
    divTag.style.left = ((lViewPortWidth / 2) - (parseInt(lDialogWidth) / 2)) + "px";
    lDefaulttop = (jQuery("#menu").position().top) + 100;
    divTag.style.top = lDefaulttop + "px";
    document.body.appendChild(divTag);
    jQuery("#popup_dialog")[0].innerHTML = lPageContent;
    jQuery('html, body').animate({ scrollTop: 0 }, 0);
}

function readmore_dlg(pInDashPage_ID) {

    //showPopWin("PopupDialogs/popup_information.aspx?pg_id=" + pInDashPage_ID, 500, 250);
    show_dialog(pInDashPage_ID,500);
}

function show_dialog(pInDashPage, pWidth) {

    lPageContent = SiteMaster.RequestTemplate("popup_dialog.htm").value;
    if (pInDashPage != null && pInDashPage != "") {
        lInDashContent = SiteMaster.RequestInDashContent(pInDashPage,"T").value;
        lPageContent = lPageContent.replace(/{%DIALOG_CONTENT%}/g, lInDashContent);
    }
    if (pWidth != null && pWidth != "")
    {
        lPageContent = lPageContent.replace(/{%WDITH%}/g, pWidth);
    }
    lViewPortWidth = jQuery(window).width();
    var divTag = document.createElement("div");
    divTag.id = "popup_dialog";
    divTag.className = "dynamicDiv";
    lViewPortWidth = jQuery(window).width();
    divTag.style.left = ((lViewPortWidth /2) - (parseInt(pWidth)/2)) + "px";
    lDefaulttop = (jQuery("#menu").position().top) + 100;
    divTag.style.top = lDefaulttop + "px";
    document.body.appendChild(divTag);
    jQuery("#popup_dialog")[0].innerHTML = lPageContent;
    jQuery('html, body').animate({ scrollTop: 0 }, 0);
}


function close_dialog() {
    divTag = document.getElementById("popup_dialog");
    if (divTag) {
        document.body.removeChild(divTag);
     }
}



function CheckErrors(pDisplayNow) {

    if (jQuery("#hf_ErrorMessage")[0] != null) {
        if (jQuery("#hf_ErrorMessage")[0].value != "") {
            if (jQuery("#dropinboxv2")[0] == null) {
                lTemplate = SiteMaster.RequestTemplate("Error_Message.htm").value;
                lTemplate = lTemplate.replace("{%MESSAGE_ICON%}", "<div class=\"error_message_icon\"></div>");
                lTemplate = lTemplate.replace("{%MESSAGE%}", jQuery("#hf_ErrorMessage")[0].value);
                lLeft = 0;
                lTop = 0;
                var divTag = document.createElement("div");
                divTag.id = "dropinboxv2";
                divTag.style.left = lLeft + "px";
                divTag.style.top = lTop  + "px";
                document.body.appendChild(divTag);
            }
            jQuery("#dropinboxv2")[0].innerHTML = lTemplate;
            if (jQuery("#dropinboxv2")[0] == null) {

                jQuery('#dropinboxv2').slideDown('slow', 3000);
            }
        }
    }
    else {
        alert("hf_ErrorMessage not found");
    }
}

function CheckInformation(pDisplayNow) {

    if (jQuery("#hf_InformationMessage")[0] != null) {
        if (jQuery("#hf_InformationMessage")[0].value != "") {
            if (jQuery("#dropinboxv2")[0] == null) {
                lTemplate = SiteMaster.RequestTemplate("Error_Message.htm").value;
                lTemplate = lTemplate.replace("{%MESSAGE_ICON%}", "<div class=\"information_message_icon\"></div>");
                lTemplate = lTemplate.replace("{%MESSAGE%}", jQuery("#hf_InformationMessage")[0].value);
                lLeft = 0;
                lTop = 0;
                var divTag = document.createElement("div");
                divTag.id = "dropinboxv2";
                divTag.style.left = lLeft + "px";
                divTag.style.top = lTop + "px";
                document.body.appendChild(divTag);
            }
            jQuery("#dropinboxv2")[0].innerHTML = lTemplate;
            if (jQuery("#dropinboxv2")[0] == null) {

                jQuery('#dropinboxv2').slideDown('slow', 3000);
            }
        }
    }
    else {
        alert("hf_InformationMessage not found");
    }

}

function dismiss_message() {
    if (jQuery("#hf_ErrorMessage")[0] != null) {
        jQuery("#hf_ErrorMessage")[0].value = "";
    }
    if (document.getElementById("hf_InformationMessage") != null) {
        document.getElementById("hf_InformationMessage").value = "";
    }
    if (jQuery("#dropinboxv2")[0] != null) {

        jQuery('#dropinboxv2').slideUp('slow', function () {
            divTag = document.getElementById("dropinboxv2");
            document.body.removeChild(divTag);
        });
    }

}



function addLoginAsFavorite(pURL) {
    if (document.all) {
        window.external.AddFavorite(pURL, '3cPlatform');
    } else if (window.sidebar) {
        window.sidebar.addPanel('3cPlatform', pURL, "");
    }
}



function GetInnerText(pElement) {
    if (document.all) {
        return trim(pElement.innerText);
    }
    else {
        return trim(pElement.textContent);
    }
}

function SetInnerText(pElement, pValue) {
    if (document.all) {
        pElement.innerText = trim(pValue);
    }
    else {
        pElement.textContent = trim(pValue);
    }
}


function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}

function ltrim(stringToTrim) {
    return stringToTrim.replace(/^\s+/, "");
}

function rtrim(stringToTrim) {
    return stringToTrim.replace(/\s+$/, "");
}

function SingleQuoteReplace(stringTocheck) {
    var lsRegExp = /'/g;
    return String(stringTocheck).replace(lsRegExp, "&#146;");
}


function IsValidated(pValidationField, pField, pFunc) {
    if (pField.value != "") {
        pFunc(pField.value);

    }
}

function formatDecimal(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
    num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + num + '.' + cents);
}

function addCommas_To_Number(num) {
    num += '';
    x = num.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;

}

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 CheckALLCheckBoxes(spanChk) {

    elm = document.forms[0].elements;

    for (i = 0; i < elm.length; i++)
        if (elm[i].type == "checkbox") {
            if (elm[i].id.indexOf("chk_Selected") >= 0) {
                elm[i].click();
            }
        }

}

function SelectAllCheckboxes(spanChk) {

    // Added as ASPX uses SPAN for checkbox
    var oItem = spanChk.children;
    var theBox = (spanChk.type == "checkbox") ?
        spanChk : spanChk.children.item[0];
    xState = theBox.checked;
    elm = theBox.form.elements;

    for (i = 0; i < elm.length; i++)
        if (elm[i].type == "checkbox" &&
              elm[i].id != theBox.id) {
            //elm[i].click();
            if (elm[i].checked != xState)
                elm[i].click();
            //elm[i].checked=xState;
        }
    lSelectAll = document.getElementById("ctl00$MainContent$hf_SelectAll");
    if (lSelectAll != null) {
        lSelectAll.value = "ALL";
    }

}



function ClearDrownDown(pDropDownList) {
    if (pDropDownList != null) {
        for (i = pDropDownList.options.length; i >= 0; i--) {
            pDropDownList.options[i] = null; //clear out the list;
        }
    }
    else {
        alert("No Drop Down");
    }
}

function AddItemsToDropDownList(pDropDownList, pValuesAsString) {
    pDropDownList.options[0] = new Option("", "");
    lValues = pValuesAsString.split("|");
    //alert(lValues);
    for (var i = 0; i < lValues.length - 1; i++) {
        lRec = lValues[i];
        lRecItems = lRec.split('~');
        pDropDownList.options[pDropDownList.options.length] = new Option(lRecItems[1], lRecItems[0]);
    }
}



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;
    }
}


