
function CustomerProfile_ValidatePage(button, labelID) {
    // Declaratively fire ASPX validators -- if any controls aren't valid the submit button will not trigger a postback
    Page_ClientValidate();
    if (Page_IsValid) {
        button.style.display = "none";
        document.getElementById(g_waitLabel).style.display = "block";
    }
}

function CustomerProfile_HomeCountryChanged(ddlCountry, ddlStateId, txtStateId) {

    switch (ddlCountry.value) {
        case "USA":
            ddlStateId.style.display = "block";
            txtStateId.style.display = "none";
            break
            //////            if(document.getElementById(hidId).value!="")
            //////            {
            //////                document.getElementById(ddlStateId).selectedIndex = document.getElementById(hidId).value;
            //////            }
            //////            else
            //////            {
            //////                document.getElementById(ddlStateId).selectedIndex = 0;
            //////            }
            ////            

            ////            if(document.getElementById(txtStateId).value == "")
            ////                document.getElementById(txtStateId).value = "na";
            ////            break;
        default:
            // store the dropdown's selected value in a hidden field to be reapplied when it is visible
            //document.getElementById(hidId).value == document.getElementById(ddlStateId).selectedIndex;
            ////            // set a dummy value on the dropdown to skirt the validator if it is currently not selected
            ////            if(document.getElementById(ddlStateId).selectedIndex == -1 || document.getElementById(ddlStateId).value == "0")
            ////                document.getElementById(ddlStateId).selectedIndex = 1;
            ////     
            ddlStateId.style.display = "none";
            txtStateId.style.display = "block";
            ////            // Always clear the textbox for the State/Province when the Country is changed from USA to another country
            //document.getElementById(txtStateId).value = "";
            ////            //document.getElementById(txtStateId).innerText = "";
            break;

    }
}
function CustomerProfile_EmployerCountryChanged(ddlCountry, ddlStateId, txtStateId) {

    switch (ddlCountry.value) {
        case "USA":
            ddlStateId.style.display = "block";
            txtStateId.style.display = "none";
            break
        default:
            ddlStateId.style.display = "none";
            txtStateId.style.display = "block";
            break;

    }
}
function CustomerProfile_AlternateCountryChanged(ddlCountry, ddlStateId, txtStateId) {

    switch (ddlCountry.value) {
        case "USA":
            ddlStateId.style.display = "block";
            txtStateId.style.display = "none";
            break
        default:
            ddlStateId.style.display = "none";
            txtStateId.style.display = "block";
            break;

    }
}

function EmployerValidate(source, args) {
    var mailTo = document.getElementById("ctl00_ContentBody_profile_MailTo").value;
    //    if(mailTo.checked == false)
    //    {
    //        mailTo = document.getElementById("ctl00_ContentBody_profile_MailTo_1");
    //        if(mailTo.checked == false)
    //        {
    //            mailTo = document.getElementById("ctl00_ContentBody_profile_MailTo_2");
    //        }
    //    }


    if (mailTo == "H" || mailTo == "A") {
        args.IsValid = true;
    }
    else if (mailTo == "B") {
        var controlToValidate = document.getElementById(source.controltovalidate);
        if (controlToValidate.value == "") {
            args.IsValid = false;
        }
        else {
            args.IsValid = true;
        }
    }
}


function HomeValidate(source, args) {
    var mailTo = document.getElementById("ctl00_ContentBody_profile_MailTo").value;
    //    if(mailTo.checked == false)
    //    {
    //        mailTo = document.getElementById("ctl00_ContentBody_profile_MailTo_1");
    //        if(mailTo.checked == false)
    //        {
    //            mailTo = document.getElementById("ctl00_ContentBody_profile_MailTo_2");
    //        }
    //    }


    if (mailTo == "H") {
        var controlToValidate = document.getElementById(source.controltovalidate);
        if (controlToValidate.value == "") {
            args.IsValid = false;
        }
        else {
            args.IsValid = true;
        }
    }
    else if (mailTo == "B" || mailTo == "A") {
        args.IsValid = true
    }
}

function AlternateValidate(source, args) {
    var mailTo = document.getElementById("ctl00_ContentBody_profile_MailTo").value;
    //    if(mailTo.checked == false)
    //    {
    //        mailTo = document.getElementById("ctl00_ContentBody_profile_MailTo_1");
    //        if(mailTo.checked == false)
    //        {
    //            mailTo = document.getElementById("ctl00_ContentBody_profile_MailTo_2");
    //        }
    //    }

    if (mailTo == "A") {
        var controlToValidate = document.getElementById(source.controltovalidate);
        if (controlToValidate.value == "") {
            args.IsValid = false;
        }
        else {
            args.IsValid = true;
        }
    }
    else if (mailTo == "B" || mailTo == "H") {
        args.IsValid = true
    }
}

//function CustomerProfile_CollapseExpandAddress(ObjImage,ObjDiv)
//{
//    var objImageSource = document.images[ObjImage.id].src;
//    var pos = objImageSource.indexOf("Plus");       
//    if(pos > -1)
//    {
//        document.images[ObjImage.id].src = g_rootpath +'/DM/CustomerProfile/images/Minus.gif';
//        ObjDiv.style.display ='';
//    }
//    else
//    {
//        document.images[ObjImage.id].src = g_rootpath +'/DM/CustomerProfile/images/Plus.gif';
//        ObjDiv.style.display ='none';
//    }

//}

function CustomerProfile_CollapseExpandAddress(ObjImage, ObjDiv) {
    //var objImageSource = document.images[ObjImage.id].src;

    var objImageSource = document.images[ObjImage].src;
    var pos = objImageSource.indexOf("Plus");
    if (pos > -1) {
        document.images[ObjImage].src = g_rootpath + '/DM/CustomerProfile/images/Minus.gif';
        document.getElementById(ObjDiv).style.display = '';
    }
    else {
        document.images[ObjImage].src = g_rootpath + '/DM/CustomerProfile/images/Plus.gif';
        document.getElementById(ObjDiv).style.display = 'none';
    }

}

function ValidateUSAPhone(phoneNumber) {
    var regPhone = /^[01]?[- .]?\(?[2-9]\d{2}\)?[- .]?\d{3}[- .]?\d{4}$/;

    if (phoneNumber.length > 0) {
        if (phoneNumber.match(regPhone)) {
            return true;
        }
        else {
            return false;
        }

    }
    return true;
}

function ValidateNoneUSAPhone(phoneNumber) {
    var regPhone = /^[0-9]{7}$/;

    if (phoneNumber.length > 0) {
        if (phoneNumber.match(regPhone)) {
            return true;
        }
        else {
            return false;
        }

    }
    return true;
}

function ValidateUSZip(zipCode) {
    var regZip = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
    if (zipCode.length > 0) {
        if (zipCode.match(regZip)) {
            return true;
        }
        else {
            return false;
        }

    }
    return true;
}

function ValidateCanadaZip(zipCode) {

    var regZip = /^[A-Za-z][0-9][A-Za-z][-\s]?[0-9][A-Za-z][0-9]$/;
    if (zipCode.length > 0) {
        if (zipCode.match(regZip)) {
            return true;
        }
        else {
            return false;
        }

    }
    return true;

}

function ValidateZipCode() {
    var zipCode = document.getElementById("ctl00_ContentBody_profile_HomeZip").value;
    var country = document.getElementById("ctl00_ContentBody_profile_ddlHomeCountry").value;
    if (country == "USA") {
        if (!ValidateUSZip(zipCode)) {
            alert("Please format home zipcode as 99999 or 99999-9999");
            return false;
        }
    }
    else if (country == "CAN") {
        if (!ValidateCanadaZip(zipCode)) {
            alert("Please format home zipcode as A9A-9A9 or A9A 9A9 or A9A9A9");
            return false;
        }
    }


    var zipCode = document.getElementById("ctl00_ContentBody_profile_EmployerZip").value;
    var country = document.getElementById("ctl00_ContentBody_profile_ddlEmployerCountry").value;
    if (country == "USA") {
        if (!ValidateUSZip(zipCode)) {
            alert("Please format business zipcode as 99999 or 99999-9999");
            return false;
        }
    }
    else if (country == "CAN") {
        if (!ValidateCanadaZip(zipCode)) {
            alert("Please format business zipcode as A9A-9A9 or A9A 9A9 or A9A9A9");
            return false;
        }
    }

    var zipCode = document.getElementById("ctl00_ContentBody_profile_AlternateZip").value;
    var country = document.getElementById("ctl00_ContentBody_profile_ddlAlternateCountry").value;
    if (country == "USA") {
        if (!ValidateUSZip(zipCode)) {
            alert("Please format alternate zipcode as 99999 or 99999-9999");
            return false;
        }
    }
    else if (country == "CAN") {
        if (!ValidateCanadaZip(zipCode)) {
            alert("Please format alternate zipcode as A9A-9A9 or A9A 9A9 or A9A9A9");
            return false;
        }
    }
    return true;

}


function ValidateForm() {
    if (ValidateContactMethod()) {
        if (ValidatePhoneFields()) {
            if (!ValidateZipCode()) {
                return false;
            }
        }
        else {
            return false;
        }
    }
    else {
        return false;
    }
}

function ValidateContactMethod() {


    var tempobj = document.getElementById("ctl00_ContentBody_profile_rblPrimaryMethod").value;
    if (tempobj == "EMAILl" && document.getElementById("ctl00_ContentBody_profile_Email1").value == "") {
        alert("Please enter primary email.");
        return false;
    }
    else if (tempobj == "EMAIL2" && document.getElementById("ctl00_ContentBody_profile_Email2").value == "") //|| (document.getElementById("ctl00_ContentBody_profile_Email2").value==null)))
    {
        alert("Please enter secondary email.");
        return false;
    }
    else if (tempobj == "HP" && document.getElementById("ctl00_ContentBody_profile_HomePhone").value == "")//|| document.getElementById("ctl00_ContentBody_profile_HomePhone").value==null))
    {
        alert("Please enter home phone.");
        return false
    }
    else if (tempobj == "BP" && document.getElementById("ctl00_ContentBody_profile_BusinessPhone").value == "")// || document.getElementById("ctl00_ContentBody_profile_BusinessPhone").value==null))
    {
        alert("Please enter business phone.");
        return false
    }
    else if (tempobj == "AP" && document.getElementById("ctl00_ContentBody_profile_AlternatePhone").value == "")// || document.getElementById("ctl00_ContentBody_profile_AlternatePhone").value==null))
    {
        alert("Please enter alternate phone.");
        return false
    }


    return true;
}

function ValidatePhoneFields() {
    var phoneNumber = document.getElementById("ctl00_ContentBody_profile_HomePhone").value;
    var country = document.getElementById("ctl00_ContentBody_profile_ddlHomeCountry").value;
    if (country == "USA" || country == "CAN") {
        if (!ValidateUSAPhone(phoneNumber)) {
            alert("Please format home phone number as 555-555-5555");
            return false;
        }
    }
    else {
        if (!ValidateNoneUSAPhone(phoneNumber)) {
            alert("Please format home phone number as 5555555");
            return false;
        }
    }


    var phoneNumber = document.getElementById("ctl00_ContentBody_profile_BusinessPhone").value;
    var country = document.getElementById("ctl00_ContentBody_profile_ddlEmployerCountry").value;


    if (country == "USA" || country == "CAN") {
        if (!ValidateUSAPhone(phoneNumber)) {
            alert("Please format business phone number as 555-555-5555");
            return false;
        }
    }
    else {
        if (!ValidateNoneUSAPhone(phoneNumber)) {
            alert("Please format business phone number as 5555555");
            return false;
        }
    }

    var phoneNumber = document.getElementById("ctl00_ContentBody_profile_AlternatePhone").value;
    var country = document.getElementById("ctl00_ContentBody_profile_ddlAlternateCountry").value;


    if (country == "USA" || country == "CAN") {
        if (!ValidateUSAPhone(phoneNumber)) {
            alert("Please format altenate phone number as 555-555-5555");
            return false;
        }
    }
    else {
        if (!ValidateNoneUSAPhone(phoneNumber)) {
            alert("Please format alternate phone number as 5555555");
            return false;
        }
    }
    return true;
}



//**************************************************************************************
// This function is used for the Meeting Registration page - MeetingRegistration.aspx //
//**************************************************************************************
function NTI_ntiSessionDetail(mid, year, webid, typ) {

    //var url = g_NTIPath + "SessionDetail.aspx?year=" + year + "&typ=" + typ;
    var url = "/DM/NTI/NTISessionDetail.aspx?year=" + year + "&typ=" + typ;
    var labelField = "Title";
    //Peak_PopupPage(gridid, page, id, mid, title, height, width, top, left, showscroll, showmenu, resizeable)
    Peak_PopupPage(null, url, webid, mid, "SessionDetail", 400, 600, 10, 10, true);
}



//12.21.08 REGISTRATIONNEW.ASPX VALIDATION SCRIPTS

function ValidatePhone(source, args) {
    
    var phoneAreaCodeId = trim(document.getElementById(source.phoneAreaCodeId).value);
    var phoneExchangeId = trim(document.getElementById(source.phoneExchangeId).value);
    var phoneSuffixId = trim(document.getElementById(source.phoneSuffixId).value);
    /*if ((phoneAreaCodeId == "") || (phoneExchangeId == "") || (phoneSuffixId == "")) {
        args.IsValid = false;
        Page_IsValid = false;
    }
    else 
    */
    if (isNaN(phoneAreaCodeId) || isNaN(phoneExchangeId) || isNaN(phoneSuffixId)) {
        args.IsValid = false;
        Page_IsValid = false;
    }
    else if ((phoneAreaCodeId.length < 3) || (phoneExchangeId.length < 3) || (phoneSuffixId.length < 4)) {
        args.IsValid = false;
        Page_IsValid = false;
    }
    else {
        args.IsValid = true;
        Page_IsValid = true;
    }
    return false;
}

function ValidateNonUsPhone(source, args) {

    var p = trim(document.getElementById(source.phoneNonUsId).value);

    while (p.indexOf("-") > 0) {
        p = p.replace("-", "");
    }

    while (p.indexOf(" ") > 0) {
        p = p.replace(" ", "");
    }
    
    if (p.length == 0) {
        args.IsValid = false;
        Page_IsValid = false;
        return false;
    }
    var nextChar;
    
    for (var x = 0;x < p.length; x++) {
        nextChar = p.substr(x, 1);    
        if (isNaN(nextChar)) {            
                args.IsValid = false;
                Page_IsValid = false;
                return false;            
        }
    }
    args.IsValid = true;
    Page_IsValid = true;
    return false;
}

function ToggleNonUSValidators(onSwtich) {
    for (k = 0; k < nonUsValidators.length; k++) {        
        nonUsValidators[k].enabled = onSwtich;
    }
}

function ToggleUSValidators(onSwtich) {
    for (k = 0; k < usValidators.length; k++) {
        usValidators[k].enabled = onSwtich;
    }
}

function ToggleCANValidators(onSwtich) {
    for (k = 0; k < CANValidators.length; k++) {
        CANValidators[k].enabled = onSwtich;
    }
}

function ToggleBusinessNameValidators(onSwtich, busNameDiv) {
        
    if (onSwtich) {
        document.getElementById("divCompany").style.display = "block";
        document.getElementById("addrLabel1").innerHTML = "<em class='required'>*</em>Busines Address 1:";
        document.getElementById("addrLabel2").innerHTML = "Busines Address 2:";
        document.getElementById("addrLabel3").innerHTML = "Busines Address 3:";
    }
    else {
        document.getElementById("divCompany").style.display = "none";
        document.getElementById("addrLabel1").innerHTML = "<em class='required'>*</em>Address 1:";
        document.getElementById("addrLabel2").innerHTML = "Address 2:";
        document.getElementById("addrLabel3").innerHTML = "Address 3:";
        
    }
    for (k = 0; k < businessNameValidators.length; k++) {
        businessNameValidators[k].enabled = onSwtich;
    }    
}

function ToggleUS(ddl) {
    if (ddl.value == "CAN") {
        ToggleCANValidators(true);
    }
    else {
        ToggleCANValidators(false);
    }
    if (ddl.value == "USA") {
        ToggleUSValidators(true);
        ToggleNonUSValidators(false);        
        document.getElementById("divStateNonUs").style.display = "none";
        document.getElementById("divStateUS").style.display = "block";
        document.getElementById("divPhoneNonUs").style.display = "none";
        document.getElementById("divPhoneUs").style.display = "block";
    }    
    else {
        ToggleUSValidators(false);
        ToggleNonUSValidators(true);
        document.getElementById("divStateNonUs").style.display = "block";
        document.getElementById("divStateUS").style.display = "none";
        document.getElementById("divPhoneNonUs").style.display = "block";
        document.getElementById("divPhoneUs").style.display = "none";        
    }
}

function ValidateZipUS(source, args) {
    var zip = trim(args.Value);
    if (!ValidateUSZip(zip)) {
        args.IsValid = false;
        Page_IsValid = false;
        return false;
    }    
    args.IsValid = true;
    Page_IsValid = true;
    return false;
}

function ValidateZipCAN(source, args) {

    var zip = trim(args.Value);
    if (!ValidateCanadaZip(zip)) {
        args.IsValid = false;
        Page_IsValid = false;
        return false;
    }   
    args.IsValid = true;
    Page_IsValid = true;
    return false;
}



// MODIFIED 1.2.09 to present more user friendly field name.  Integrate to Peak later.
// Function: Peak_ValidateName(id, mid)
// Description: Used for validating names and addresses
// Parameters:  (object) frmObj - Form object (TextBox) to be validated
function Peak_ValidateNameMod(frmObj, fldName) {
    var testString = frmObj.value;
    var tmpString;
    var strMsg;
    var errorFlag = false;

    // Regular expression patterns
    var BadChars = /(\w*)([^a-zA-Z_\s]+)(\w*)/gi; //Non-word characters before words
    var RepeatSpace = /\s{2,}/gi; //Two or more blanks
    var LeadSpace = /^\s+/; //Leading blank spaces
    var PeriodCheck = /\w([\.]{1})$/gi; //Check for period after words
    var DashCheck = /\w[-]{1}\w/i; //Check for a hyphen between words
    var AposCheck = /\w[']{1}\w/i; //Check for an apostrophe between words
    var CommaCheck = /\w([,]{1})$/gi; //Check for comma after words

    //Get rid of leading blanks if they exist
    tmpString = testString.replace(LeadSpace, "");
    //Get rid of extra blanks
    tmpString = tmpString.replace(RepeatSpace, " ");
    frmObj.value = tmpString;
    //Split the name into an array to check each word individually.
    var tmpStringArray = tmpString.split(" ");
    //Loop through the array and match patterns for bad characters
    for (var i = 0; i < tmpStringArray.length; i++) {
        var tmpCheck = tmpStringArray[i];
        //Check for non-word characters
        var tmpArray = tmpCheck.match(BadChars)
        if (tmpArray != null) {
            //If there are multiple occurences or bad characters within a word, don't let them continue
            if (tmpArray.length > 1)
                errorFlag = true;
            //Check for hyphenated word
            else if (tmpCheck.match(DashCheck) != null) 
            				errorFlag = false;
            //Check for apostrophe between words
            else if (tmpCheck.match(AposCheck) != null)
                errorFlag = false;
            //Check for period at the end of a word
            else if (tmpCheck.match(PeriodCheck) != null)
                errorFlag = false;
            //Check for comma at the end of a word
            else if (tmpCheck.match(CommaCheck) != null)
                errorFlag = false;
            //Bad characters
            else
                errorFlag = true;
        }
        if (errorFlag)
            break;
    }
    //Display message if there is an error and focus back on the form field
    if (errorFlag == true) {
        var splitObj = frmObj.name.split("$");
        //var fldName = splitObj[splitObj.length - 1];
        var strMsg = "The text you have entered for '" + fldName + "' contains invalid characters";
        alert(strMsg);
        frmObj.focus();
        return false;
    }
}
