
var US_COUNTRY_CODE = "US";
var CANADA_COUNTRY_CODE = "CA";
var BLOCK_DISPLAY_STYLE = "";
var NO_DISPLAY = "none";
var NO_DISPLAY_STYLE = "none";
var EMPTY_VALUE = "";
var addressFormObject = null;

function getProvinceField(form) {
    return form.caProvince ? form.caProvince : null;
}


function getStateValue(form) {
    return form.usState ? form.usState.value : null;
}


function getOtherValue(form) {
    return form.otherState ? form.otherState.value : null;
}


function showCounty(countyId) {
    try {
        var elem = document.getElementById(countyId);
        elem.style.display = BLOCK_DISPLAY_STYLE;
    } catch (e) {
        // yes, we know it's undefined.
    }

}


function hideCounty(countyFieldToSet, countyId) {
    try {
        var elem = document.getElementById(countyId);
        elem.style.display = NO_DISPLAY;

        if (countyFieldToSet) {
            setSubentityValue(EMPTY_VALUE, countyFieldToSet);
        }
    } catch (e) {
        // yes, we know it's undefined.
    }
}


function hideProvince(provinceFieldToSet, countyId, provinceId) {
    if (elem = document.getElementById(provinceId)) {
        elem.style.display = NO_DISPLAY;
    }

    try {
        // why isn't this working?
        //setStateValue(EMPTY_VALUE, false, provinceFieldToSet, countyId);
    } catch (e) {
        // yes, we know it's undefined.
    }
}


function hideOther(otherId) {
    if (elem = document.getElementById(otherId)) {
        elem.style.display = NO_DISPLAY;
    }
}


function hideState(stateFieldToSet, stateId, countyId) {
    try {
        var elem = document.getElementById(stateId);
        elem.style.display = NO_DISPLAY;

        setStateValue(EMPTY_VALUE, false, stateFieldToSet, countyId);
    } catch (e) {
        // yes, we know it's undefined.
    }
}


function showState(stateFieldToSet, countyFieldToSet, stateValue, stateId, countyId, selectedEntityPrefix) {
    setStateValue(stateValue, true, stateFieldToSet, countyFieldToSet, countyId, selectedEntityPrefix);

    var elem = document.getElementById(stateId);
    elem.style.display = BLOCK_DISPLAY_STYLE;
}


function showOther(stateFieldToSet, otherValue, countyId, otherId) {
    setStateValue(otherValue, false, stateFieldToSet, countyId);

    var elem = document.getElementById(otherId);
    elem.style.display = BLOCK_DISPLAY_STYLE;
}


function showProvince(stateFieldToSet, provinceFieldToSet, countyId, provinceId) {
    setStateValue(provinceFieldToSet.value, false, stateFieldToSet, countyId);

    var elem = document.getElementById(provinceId);
    elem.style.display = BLOCK_DISPLAY_STYLE;
}


function setPrevSubentityValue(value, selectedEntityPrefix) {
    selectedEntityPrefix = (selectedEntityPrefix && selectedEntityPrefix != 'undefined' && selectedEntityPrefix != '') ? selectedEntityPrefix : '';
    addressFormObject = (addressFormObject && addressFormObject != 'null' && addressFormObject != 'undefined') ? addressFormObject : document.address;

	if(selectedEntityPrefix == 'ship'){
	    addressFormObject.prevSubentityship.value = value;
	} else {
		addressFormObject.prevSubentity.value = value;
	}
}


function checkStates(country, stateFieldToSet, countyFieldToSet, provinceFieldToSet, stateValue, stateId, provinceId, countyId, otherId, otherValue, selectedEntityPrefix, billCounty, formObject) {
	addressFormObject = (formObject && formObject != 'null' && formObject != 'undefined') ? formObject : document.address;
    switch(country.value) {
        case US_COUNTRY_CODE:
            hideProvince(provinceFieldToSet, countyId, provinceId);
            hideOther(otherId);
            showState(stateFieldToSet, countyFieldToSet, stateValue, stateId, countyId, selectedEntityPrefix);
            showCounty(countyId);
            displaySubentity(countyFieldToSet, stateValue, countyId, selectedEntityPrefix);
            selectSubentity(stateValue, billCounty);
            break;
        case CANADA_COUNTRY_CODE:
            hideState(stateFieldToSet, stateId, countyId);
            hideOther(otherId);
            hideCounty(countyFieldToSet, countyId)
            //alert('stateFieldToSet:' + stateFieldToSet.name + ' provinceFieldToSet: ' + provinceFieldToSet.value);
            showProvince(stateFieldToSet, provinceFieldToSet, countyId, provinceId);
            break;
        default:
            hideState(stateFieldToSet, stateId, countyId);
            hideProvince(stateFieldToSet, countyId, provinceId);
            hideCounty(countyFieldToSet, countyId);
            showOther(stateFieldToSet, otherValue, countyId, otherId);
            //setStateValue(EMPTY_VALUE, false, stateFieldToSet, countyId);
    }
}


/*
 * Displays or hides the states county if it exists.
 */
function displaySubentity(countyFieldToSet, stateValue, countyId, selectedEntityPrefix) {
    selectedEntityPrefix = (selectedEntityPrefix && selectedEntityPrefix != 'undefined' && selectedEntityPrefix != '') ? selectedEntityPrefix : '';

    addressFormObject = (addressFormObject && addressFormObject != 'null' && addressFormObject != 'undefined') ? addressFormObject : document.address;
    
	if(selectedEntityPrefix == 'ship'){
	    var prevSubentity = addressFormObject.prevSubentityship.value;
	} else {
		var prevSubentity = addressFormObject.prevSubentity.value;
	}

    var selectedEntity = selectedEntityPrefix + stateValue;

    setPrevSubentityValue(selectedEntity, selectedEntityPrefix);

	//don't check for an empty value because we now have a "Please Select..." option with a value of "" so if that's selected and you switch to a legit state with counties,
	//the county drop down did not show up. NM 16179
    //if (EMPTY_VALUE != prevSubentity) {
        try {
            showCounty(countyId);
            var elem = document.getElementById(prevSubentity);
            //elem.style.display = NO_DISPLAY;
        } catch (e) {
            // yes, we know it's undefined.
        }
    //}

    try {
        var elem = document.getElementById(selectedEntity);
        elem.style.display = BLOCK_DISPLAY_STYLE;

        if (countyFieldToSet) {
            eval('var tempValue = document.' + addressFormObject.name + '.thisSubentity' + selectedEntity + '.value');
            setSubentityValue(tempValue, countyFieldToSet);
        }
    } catch(e) {
        hideCounty(countyFieldToSet, countyId);
    }
}

/*
 * Selects a value in the county drop down
 */
function selectSubentity(stateValue, billCounty){
	addressFormObject = (addressFormObject && addressFormObject != 'null' && addressFormObject != 'undefined') ? addressFormObject : document.address;
	
	if(billCounty && billCounty != '' && billCounty != 'undefined'){
		countyFieldToSet = 'thisSubentityship' + stateValue;
	
		try {
			var elements = addressFormObject.elements;
			var length = elements.length;
			
			for(i=0;i<length;i++){
				var current = elements[i];

				if(current.name == countyFieldToSet){
					var options = current.options;
					var optionLength = options.length;
					
					for(j=0;j<optionLength;j++){
						var currentOption = options[j];

						if(currentOption.value == billCounty){
							current.selectedIndex = j;
						}
					}
				}
			}
	
		} catch (e) {
			//Yes, we know it's undefined.
		}
	}
}

function setStateValue(stateValue, doSubentity, stateFieldToSet, countyFieldToSet, countyId, selectedEntityPrefix) {
    //alert('state='+stateValue+'stateFieldToSet='+stateFieldToSet.name);
    stateFieldToSet.value = stateValue;

    // pretty much a hack.
    if (doSubentity && countyFieldToSet) {
        displaySubentity(countyFieldToSet, stateValue, countyId, selectedEntityPrefix);
    } else {
        try {
            var elem = document.getElementById(countyId);
            elem.style.display = NO_DISPLAY;
        } catch (e) {
            // yes, we know it's undefined.
        }
    }
}


function setSubentityValue(subentityValue, countyFieldToSubmit) {
    countyFieldToSubmit.value = subentityValue;
}


function initializeDisplay(thisState, thisProvince, stateFieldToSet, customerCounty, countyFieldToSet, provinceFieldToSet, stateValue, countyId, thisOther, otherValue, selectedEntityPrefix) {
    if (BLOCK_DISPLAY_STYLE == thisState) {
        setStateValue(stateValue, false, stateFieldToSet, countyFieldToSet, countyId);
        setPrevSubentityValue(stateValue, selectedEntityPrefix);

        try {
			var stateElementName = '';

			if(selectedEntityPrefix && selectedEntityPrefix != '' && selectedEntityPrefix != 'undefined'){
				stateElementName = selectedEntityPrefix + stateValue;
			} else {
				stateElementName = stateValue;
			}

			var elem = document.getElementById(stateElementName);
            elem.style.display = BLOCK_DISPLAY_STYLE;

            showCounty(countyId);
            try {
                if (0 < customerCounty.length) {
                    countyFieldToSet.value = customerCounty;

                    displaySubentity(countyFieldToSet, stateValue, countyId, selectedEntityPrefix);
                }
            } catch (e) {
                // yes, we know it's undefined.
            }
        } catch(e) { }
    } else if (BLOCK_DISPLAY_STYLE == thisProvince) {
        if (provinceFieldToSet) {
            setStateValue(provinceFieldToSet.value, false, stateFieldToSet, countyFieldToSet, countyId);
        }

    } else if (BLOCK_DISPLAY_STYLE == thisOther) {
        setStateValue(otherValue, false, stateFieldToSet, countyFieldToSet, countyId);

    } else {
        //setStateValue(EMPTY_VALUE, false, stateFieldToSet, countyFieldToSet, countyId);

    }
}

function registerCountryInputElement(countryElement, usStateElement, canadianProvinceElement, otherStateElement, usCountyElements, actualStateElement, actualCountyElement
	, countrySystems, usStateDisplayElement, canadianProvinceDisplayElement, otherStateDisplayElement, usCountyDisplayElement){
	var countrySystemCounter = countrySystems.length;
	var countrySystem = new Object();

	//these elements may not be present on the page depending on which countries/states you have setup in the admin so accomodate for that NM 17393
	if(usStateElement == 'undefined'){
		usStateElement = null;
	}
	if(canadianProvinceElement == 'undefined'){
		canadianProvinceElement = null;
	}
	if(usStateDisplayElement == 'undefined'){
		usStateDisplayElement = null;
	}
	if(canadianProvinceDisplayElement == 'undefined'){
		canadianProvinceDisplayElement = null;
	}

	if(usStateDisplayElement != null){
		countrySystem.usStateDisplayElement = usStateDisplayElement;
	}
	if(canadianProvinceDisplayElement != null){
		countrySystem.canadianProvinceDisplayElement = canadianProvinceDisplayElement;
	}
	countrySystem.otherStateDisplayElement = otherStateDisplayElement;
	countrySystem.usCountyDisplayElement = usCountyDisplayElement;

	countryElement.onchange = function(){ processCountryElementOnchange(countrySystemCounter); };
	countrySystem.country = countryElement;

	if(usStateElement != null){
		usStateElement.onchange = function(){ processStateElementOnchange(countrySystemCounter, this); };
		countrySystem.usState = usStateElement;
	}

	if(canadianProvinceElement != null){
		canadianProvinceElement.onchange = function(){ processStateElementOnchange(countrySystemCounter, this); };
		countrySystem.canadianProvince = canadianProvinceElement;
	}

	otherStateElement.onblur = function(){ processStateElementOnchange(countrySystemCounter, this); };
	countrySystem.otherState = otherStateElement;

	for (var stateKey in usCountyElements){
		usCountyElements[stateKey].onchange = function(){ processCountyElementOnchange(countrySystemCounter, this); };
	}
	countrySystem.counties = usCountyElements;

	countrySystem.stateSaveElement = actualStateElement;
	countrySystem.countySaveElement = actualCountyElement;

	//display the us state dropdown/ca province dropdown/other country state input based on our country element's selection
	displayAppropriateStateInput(countrySystem);

	countrySystems.push(countrySystem);
}

function processCountryElementOnchange(countrySystemCounter){
	var countrySystem = countrySystemsOnPage[countrySystemCounter];
	//display the 'us state dropdown' / 'ca province dropdown' / 'other country state input' based on our country element's selection
	displayAppropriateStateInput(countrySystem);
}

function processStateElementOnchange(countrySystemCounter, changedElement){
	var countrySystem = countrySystemsOnPage[countrySystemCounter];
	countrySystem.stateSaveElement.value = changedElement.value;
	displayAppropriateCountyInput(countrySystem);
}

function processCountyElementOnchange(countrySystemCounter, changedElement){
	var countrySystem = countrySystemsOnPage[countrySystemCounter];
	countrySystem.countySaveElement.value = changedElement.value;
}

function displayAppropriateStateInput(countrySystem){
	countryElement = countrySystem.country;
	usStateDisplayElement = countrySystem.usStateDisplayElement;
	canadianProvinceDisplayElement = countrySystem.canadianProvinceDisplayElement;
	otherStateDisplayElement = countrySystem.otherStateDisplayElement;

	var countryValue = countryElement.value;
	var stateSaveValue = '';
	if(countryValue == US_COUNTRY_CODE){
		if(usStateDisplayElement != null){
			usStateDisplayElement.style.display = BLOCK_DISPLAY_STYLE;
		}
		if(canadianProvinceDisplayElement != null){
			canadianProvinceDisplayElement.style.display = NO_DISPLAY_STYLE;
		}
		otherStateDisplayElement.style.display = NO_DISPLAY_STYLE;
		stateSaveValue = countrySystem.usState.value;//usStateDisplayElement[usStateDisplayElement.selectedIndex].value;
	} else if(countryValue == CANADA_COUNTRY_CODE){
		if(usStateDisplayElement != null){
			usStateDisplayElement.style.display = NO_DISPLAY_STYLE;
		}
		if(canadianProvinceDisplayElement != null){
			canadianProvinceDisplayElement.style.display = BLOCK_DISPLAY_STYLE;
		}
		otherStateDisplayElement.style.display = NO_DISPLAY_STYLE;
		stateSaveValue = countrySystem.canadianProvince.value;//DisplayElement[canadianProvinceDisplayElement.selectedIndex].value;
	} else {
		if(usStateDisplayElement != null){
			usStateDisplayElement.style.display = NO_DISPLAY_STYLE;
		}
		if(canadianProvinceDisplayElement != null){
			canadianProvinceDisplayElement.style.display = NO_DISPLAY_STYLE;
		}
		otherStateDisplayElement.style.display = BLOCK_DISPLAY_STYLE;
		stateSaveValue = countrySystem.otherState.value;//DisplayElement.value;
	}
	//alert('State: ' + stateSaveValue);
	countrySystem.stateSaveElement.value = stateSaveValue;
	displayAppropriateCountyInput(countrySystem);
}

function displayAppropriateCountyInput(countrySystem){
	countryElement = countrySystem.country;
	var countryValue = countryElement.value;
	//a list of county drop down HTML elements that can be displayed/hidden based on state selection
	var counties = countrySystem.counties;
	//the element that holds all of the county dropdowns that would be hidden/displayed based on country selected (i.e. the div that holds the "county" title and all of the dd's)
	var countyDisplayElement = countrySystem.usCountyDisplayElement;

	if(countryValue == US_COUNTRY_CODE){
		var usStateValue = countrySystem.usState.value;
		//hide the entire county element.  We'll show it again if the selected state has counties configured
		countyDisplayElement.style.display = NO_DISPLAY_STYLE;
		var countySaveValue = '';
		for (var stateKey in counties){
			if(stateKey == usStateValue){
				countyDisplayElement.style.display = BLOCK_DISPLAY_STYLE;
				counties[stateKey].style.display = BLOCK_DISPLAY_STYLE;
				countySaveValue = counties[stateKey][counties[stateKey].selectedIndex].value;
			} else {
				counties[stateKey].style.display = NO_DISPLAY_STYLE;
			}
		}
		//alert('County: ' + countySaveValue);
		countrySystem.countySaveElement.value = countySaveValue;
	} else {
		countyDisplayElement.style.display = NO_DISPLAY_STYLE;
		for (var stateKey in counties){
			counties[stateKey].style.display = NO_DISPLAY_STYLE;
		}
	}
}
