﻿// This function shows / hides state and postal code if USA is not selected
function ShowHideDependencies(selectedCountry)
{
    var mangledPrefix = "ctl00_Body_"; // <-- CASE IS VERY IMPORTANT HERE

    if (selectedCountry != "" && selectedCountry != "United States" && selectedCountry != "Canada" && selectedCountry != "Ireland" && selectedCountry != "United Kingdom")
    {
        // Disable validators
        var tbValState = document.getElementById(mangledPrefix + "RequiredFieldValidator7");
        if(tbValState != null)
        {
            ValidatorEnable(tbValState, false); 
            //alert("?");
        }
        else
        {
            //alert("!");
        }
        var tbValZip = document.getElementById(mangledPrefix + "RequiredFieldValidator8");
        if(tbValZip != null)
        {
            ValidatorEnable(tbValZip, false); 
        }
        
        // hide state and zip divs
        $('#divStateProvLabel').hide();
        $('#divStateProv').hide();
        $('#divZipLabel').hide();
        $('#divZip').hide();
        
    }
    else
    {
        // show state and zip divs
        $('#divStateProvLabel').show();
        $('#divStateProv').show();
        $('#divZipLabel').show();
        $('#divZip').show();

        // Enable state validators for US and Canada
        var tbValState = document.getElementById(mangledPrefix + "RequiredFieldValidator7");
        if(tbValState != null) {
            if (selectedCountry == "United States" || selectedCountry == "Canada") {
                ValidatorEnable(tbValState, true);
            }
            else {
                ValidatorEnable(tbValState, false);
                // hide state div
                $('#divStateProvLabel').hide();
                $('#divStateProv').hide();
            }
        }
        // Enable zip/postcode validators for all
        var tbValZip = document.getElementById(mangledPrefix + "RequiredFieldValidator8");
        if(tbValZip != null)
        {
            ValidatorEnable(tbValZip, true); 
        }
    }
    
}

function OnLoadShowHideDependencies()
{
    
    var mangledPrefix = "ctl00_Body_";

    var ddlCountry = document.getElementById(mangledPrefix + "ddlCountry");
    if(ddlCountry != null)
    {
        ShowHideDependencies(ddlCountry.value);
    }
    else
    {
        alert("OnLoadShowHideDependencies Error");
    }
}

// Get rid of google toolbar yellow fields
if (window.attachEvent)
    window.attachEvent("onload", setListeners);

function setListeners() {
    inputList = document.getElementsByTagName("INPUT");
    for (i = 0; i < inputList.length; i++) {
        inputList[i].attachEvent("onpropertychange", restoreStyles);
        inputList[i].style.backgroundColor = "";
    }
    selectList = document.getElementsByTagName("SELECT");
    for (i = 0; i < selectList.length; i++) {
        selectList[i].attachEvent("onpropertychange", restoreStyles);
        selectList[i].style.backgroundColor = "";
    }
}

function restoreStyles() {
    if (event.srcElement.style.backgroundColor != "")
        event.srcElement.style.backgroundColor = "";
}

/* Multiline Text box max characters */

function CheckCount(text, length) {
    var maxlength = new Number(length); // Change number to your max length.
    if (text.value.length > maxlength) {
        text.value = text.value.substring(0, maxlength);
        alert(" Only " + maxlength + " characters allowed");
    }
}

/* Zip vs Postcode form label */

addOnloadEvent(ZipLabelCountryChangedFormLoad);

// Restore proper zip label on form load - works with form state persistance
function ZipLabelCountryChangedFormLoad() {
    var mangledPrefix = "ctl00_Body_";

    var ddlCountry = document.getElementById(mangledPrefix + "ddlCountry");
    if (ddlCountry != null) {
        ZipLabelCountryChanged(ddlCountry.value);
    }
    else {
        alert("ZipLabelCountryChangedFormLoad Error");
    }
}

// Update zip label
function ZipLabelCountryChanged(selectedCountry) {
    // Retrieve label span
    var labelZip = document.getElementById("lblZip");
    // Check for nulls
    if (labelZip != null) {
        // Display "Postcode" for Ireland and UK
        if (selectedCountry == "Ireland" || selectedCountry == "United Kingdom") {
            labelZip.innerHTML = "Postcode:";
        }
        else {
            labelZip.innerHTML = "ZIP Code:";
        }
    }
    else {
        //
    }
}

// Add onload event without removing existing event handlers
function addOnloadEvent(fnc) {
    if (typeof window.addEventListener != "undefined")
        window.addEventListener("load", fnc, false);
    else if (typeof window.attachEvent != "undefined") {
        window.attachEvent("onload", fnc);
    }
    else {
        if (window.onload != null) {
            var oldOnload = window.onload;
            window.onload = function(e) {
                oldOnload(e);
                window[fnc]();
            };
        }
        else
            window.onload = fnc;
    }
}