var xmlHttpStates;
var xmlHttpCities;
var xmlHttpZipCodes;

function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		xmlHttpStates = new ActiveXObject("Microsoft.XMLHTTP");
		xmlHttpCities = new ActiveXObject("Microsoft.XMLHTTP");
		xmlHttpZipCodes = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		xmlHttpStates = new XMLHttpRequest();
		xmlHttpCities = new XMLHttpRequest();
		xmlHttpZipCodes = new XMLHttpRequest();
	}
}

function switchToInput(fullFieldName) {
	var fieldText       = document.getElementById(fullFieldName + "Text");
	var fieldTextField  = document.getElementById(fullFieldName + "TextField");
	var fieldLookup     = document.getElementById(fullFieldName + "Lookup");
	var fieldLookupList = document.getElementById(fullFieldName + "LookupList");
	
	fieldText.style.visibility = 'visible';
	fieldText.style.display = 'block';
	fieldTextField.name = fullFieldName; 
	
	// Blank out the lookup.
	fieldLookup.style.visibility = 'hidden';
	fieldLookup.style.display = 'none';
	fieldLookupList.name = 'nothing';
	fieldLookupList.options[1] = new Option('--------', '');
	fieldLookupList.options.length = 2;
	fieldLookupList.selectedIndex = 0;
}

function switchToStateInput(fieldName) {
	var stateFieldName  = fieldName + "StateCode";
	
	switchToInput(stateFieldName);
	
	switchToCityInput(fieldName);
}

function switchToCityInput(fieldName) {
	var cityFieldName  = fieldName + "City";
	
	switchToInput(cityFieldName);
	
	switchToZipInput(fieldName);
}

function switchToZipInput(fieldName) {
	var zipFieldName  = fieldName + "ZipCode";
	
	switchToInput(zipFieldName);
}

function syncStateSelectList(fieldName) {
	var countryList     = document.getElementById(fieldName + "CountryCodeLookupList");
	var stateText       = document.getElementById(fieldName + "StateCodeText");
	var stateTextField  = document.getElementById(fieldName + "StateCodeTextField");
	var stateLookup     = document.getElementById(fieldName + "StateCodeLookup");
	var stateLookupList = document.getElementById(fieldName + "StateCodeLookupList");
	
	if ((countryList.selectedIndex <= 0) || (countryList.options[countryList.selectedIndex].value == '')) {
		// They don't have a country selected.
		
		// Turn this field off so the data is not processed when the form is submitted.
		stateText.style.visibility = 'hidden';
		stateText.style.display = 'none';
		stateTextField.name = 'nothing'; 
		
		// Blank out the state lookup.
		stateLookup.style.visibility = 'visible';
		stateLookup.style.display = 'block';
		stateLookupList.name = fieldName + "StateCode";
		stateLookupList.options[1] = new Option('-- Select Country First --', '');
		stateLookupList.options.length = 2;
		stateLookupList.selectedIndex = 0;
		
		syncCitySelectList(fieldName);
	} else {
		// They have a country selected.
		var countryCode = countryList.options[countryList.selectedIndex].value;
		
		// Turn this field off so the data is not processed when the form is submitted.
		stateText.style.visibility = 'hidden';
		stateText.style.display = 'none';
		stateTextField.name = 'nothing'; 
		
		// Blank out the state lookup.
		stateLookup.style.visibility = 'visible';
		stateLookup.style.display = 'block';
		stateLookupList.name = fieldName + "StateCode";
		stateLookupList.options[1] = new Option('Loading State List...', '');
		stateLookupList.options.length = 2;
		stateLookupList.selectedIndex = 1;
		
		createXMLHttpRequest();
		xmlHttpStates.onreadystatechange = handleStates;
		xmlHttpStates.open(
			'GET', 
			'/cms-global/location/GetStates.do?countryCode=' + countryCode + "&fieldName=" + fieldName, 
			true
		);
		xmlHttpStates.send(null);
	}
}

function handleStates() {
	if ((xmlHttpStates.readyState == 4) && (xmlHttpStates.status == 200)) {
		var xmlDoc = xmlHttpStates.responseXML;
		
		var dataElement = xmlDoc.getElementsByTagName('data')[0];
		
		var fieldName       = dataElement.getElementsByTagName('fieldName')[0].childNodes[0].nodeValue;
		var stateFieldName  = fieldName + "StateCode";
		var stateText       = document.getElementById(stateFieldName + "Text");
		var stateTextField  = document.getElementById(stateFieldName + "TextField");
		var stateLookup     = document.getElementById(stateFieldName + "Lookup");
		var stateLookupList = document.getElementById(stateFieldName + "LookupList");
		
		var states = dataElement.getElementsByTagName('state');
		if (states.length > 0) {
			// Turn this field off so the data is not processed when the form is submitted.
			stateText.style.visibility = 'hidden';
			stateText.style.display = 'none';
			stateTextField.name = 'nothing'; 
			
			// Blank out the state lookup.
			stateLookup.style.visibility = 'visible';
			stateLookup.style.display = 'block';
			stateLookupList.name = stateFieldName;
			var selectedItem = 0;
			stateLookupList.options.length = states.length + 1;
			for (var i = 0; i < states.length; ++i) {
				stateLookupList.options[i + 1] = new Option(
					states[i].getElementsByTagName('stateName')[0].childNodes[0].nodeValue,
					states[i].getElementsByTagName('stateCode')[0].childNodes[0].nodeValue
				);
				if (states[i].getElementsByTagName('stateCode')[0].childNodes[0].nodeValue == stateTextField.value) {
					selectedItem = i + 1;
				}
			}
			stateLookupList.selectedIndex = selectedItem;
			
			syncCitySelectList(fieldName);
		} else {
			// Turn this field off so the data is not processed when the form is submitted.
			switchToStateInput(fieldName);
		}
	} else if (xmlHttpStates.readyState == 4) {
		alert("Unable to get states: " + xmlHttpStates.status);
	}
}

function syncCitySelectList(fieldName) {
	var countryList    = document.getElementById(fieldName + "CountryCodeLookupList");
	var stateTextField = document.getElementById(fieldName + "StateCodeTextField");
	var stateList      = document.getElementById(fieldName + "StateCodeLookupList");
	var cityText       = document.getElementById(fieldName + "CityText");
	var cityTextField  = document.getElementById(fieldName + "CityTextField");
	var cityLookup     = document.getElementById(fieldName + "CityLookup");
	var cityLookupList = document.getElementById(fieldName + "CityLookupList");
	
	stateTextField.value = stateList.options[stateList.selectedIndex].value;
	
	if ((stateList.selectedIndex <= 0) || (stateList.options[stateList.selectedIndex].value == '')) {
		// They don't have a state selected.
		
		// Turn this field off so the data is not processed when the form is submitted.
		cityText.style.visibility = 'hidden';
		cityText.style.display = 'none';
		cityTextField.name = 'nothing'; 
		
		// Blank out the city lookup.
		cityLookup.style.visibility = 'visible';
		cityLookup.style.display = 'block';
		cityLookupList.name = fieldName + "City";
		cityLookupList.options[1] = new Option('-- Select State First --', '');
		cityLookupList.options.length = 2;
		cityLookupList.selectedIndex = 0;
		
		syncZipSelectList(fieldName);
	} else {
		// They have a state selected.
switchToCityInput(fieldName);
return;
		var countryCode = countryList.options[countryList.selectedIndex].value;
		var stateCode = stateList.options[stateList.selectedIndex].value;
		
		// Turn this field off so the data is not processed when the form is submitted.
		cityText.style.visibility = 'hidden';
		cityText.style.display = 'none';
		cityTextField.name = 'nothing'; 
		
		// Blank out the city lookup.
		cityLookup.style.visibility = 'visible';
		cityLookup.style.display = 'block';
		cityLookupList.name = fieldName + "City";
		cityLookupList.options[1] = new Option('Loading City List...', '');
		cityLookupList.options.length = 2;
		cityLookupList.selectedIndex = 1;
		
		createXMLHttpRequest();
		xmlHttpCities.onreadystatechange = handleCities;
		xmlHttpCities.open(
			'GET', 
			'/cms-global/location/GetCities.do?countryCode=' + countryCode + "&stateCode=" + stateCode + "&fieldName=" + fieldName, 
			true
		);
		xmlHttpCities.send(null);
	}
}

function handleCities() {
	if ((xmlHttpCities.readyState == 4) && (xmlHttpCities.status == 200)) {
		var xmlDoc = xmlHttpCities.responseXML;
		
		var dataElement = xmlDoc.getElementsByTagName('data')[0];
		
		var fieldName      = dataElement.getElementsByTagName('fieldName')[0].childNodes[0].nodeValue;
		var cityFieldName  = fieldName + "City";
		var cityText       = document.getElementById(cityFieldName + "Text");
		var cityTextField  = document.getElementById(cityFieldName + "TextField");
		var cityLookup     = document.getElementById(cityFieldName + "Lookup");
		var cityLookupList = document.getElementById(cityFieldName + "LookupList");
		
		var cities = dataElement.getElementsByTagName('city');
		if (cities.length > 0) {
			// Turn this field off so the data is not processed when the form is submitted.
			cityText.style.visibility = 'hidden';
			cityText.style.display = 'none';
			cityTextField.name = 'nothing'; 
			
			// Blank out the city lookup.
			cityLookup.style.visibility = 'visible';
			cityLookup.style.display = 'block';
			cityLookupList.name = cityFieldName;
			var selectedItem = 0;
			cityLookupList.options.length = cities.length + 1;
			for (var i = 0; i < cities.length; ++i) {
				cityLookupList.options[i + 1] = new Option(
					cities[i].getElementsByTagName('cityName')[0].childNodes[0].nodeValue,
					cities[i].getElementsByTagName('cityName')[0].childNodes[0].nodeValue
				);
				if (cities[i].getElementsByTagName('cityName')[0].childNodes[0].nodeValue == cityTextField.value) {
					selectedItem = i + 1;
				}
			}
			cityLookupList.selectedIndex = selectedItem;
			
			syncZipSelectList(fieldName);
		} else {
			// Turn this field off so the data is not processed when the form is submitted.
			switchToCityInput(fieldName);
		}
	} else if (xmlHttpCities.readyState == 4) {
		alert("Unable to get cities: " + xmlHttpCities.status);
	}
}

function syncZipSelectList(fieldName) {
	var countryList   = document.getElementById(fieldName + "CountryCodeLookupList");
	var stateList     = document.getElementById(fieldName + "StateCodeLookupList");
	var cityTextField = document.getElementById(fieldName + "CityTextField");
	var cityList      = document.getElementById(fieldName + "CityLookupList");
	var zipText       = document.getElementById(fieldName + "ZipCodeText");
	var zipTextField  = document.getElementById(fieldName + "ZipCodeTextField");
	var zipLookup     = document.getElementById(fieldName + "ZipCodeLookup");
	var zipLookupList = document.getElementById(fieldName + "ZipCodeLookupList");
	
	cityTextField.value = cityList.options[cityList.selectedIndex].value;
	
	if ((cityList.selectedIndex <= 0) || (cityList.options[cityList.selectedIndex].value == '')) {
		// They don't have a city selected.
		
		// Turn this field off so the data is not processed when the form is submitted.
		zipText.style.visibility = 'hidden';
		zipText.style.display = 'none';
		zipTextField.name = 'nothing'; 
		
		// Blank out the zip lookup.
		zipLookup.style.visibility = 'visible';
		zipLookup.style.display = 'block';
		zipLookupList.name = fieldName + "ZipCode";
		zipLookupList.options[1] = new Option('-- Select City First --', '');
		zipLookupList.options.length = 2;
		zipLookupList.selectedIndex = 0;
	} else {
		// They have a state selected.
		var countryCode = countryList.options[countryList.selectedIndex].value;
		var stateCode = stateList.options[stateList.selectedIndex].value;
		var cityName = cityList.options[cityList.selectedIndex].value;
		
		// Turn this field off so the data is not processed when the form is submitted.
		zipText.style.visibility = 'hidden';
		zipText.style.display = 'none';
		zipTextField.name = 'nothing'; 
		
		// Blank out the zip lookup.
		zipLookup.style.visibility = 'visible';
		zipLookup.style.display = 'block';
		zipLookupList.name = fieldName + "ZipCode";
		zipLookupList.options[1] = new Option('Loading Zip Code List...', '');
		zipLookupList.options.length = 2;
		zipLookupList.selectedIndex = 1;
		
		createXMLHttpRequest();
		xmlHttpZipCodes.onreadystatechange = handleZipCodes;
		xmlHttpZipCodes.open(
			'GET', 
			'/cms-global/location/GetZipCodes.do?countryCode=' + countryCode + "&stateCode=" + stateCode + "&cityName=" + cityName + "&fieldName=" + fieldName, 
			true
		);
		xmlHttpZipCodes.send(null);
	}
}

function handleZipCodes() {
	if ((xmlHttpZipCodes.readyState == 4) && (xmlHttpZipCodes.status == 200)) {
		var xmlDoc = xmlHttpZipCodes.responseXML;
		
		var dataElement = xmlDoc.getElementsByTagName('data')[0];
		
		var fieldName     = dataElement.getElementsByTagName('fieldName')[0].childNodes[0].nodeValue;
		var zipFieldName  = fieldName + "ZipCode";
		var zipText       = document.getElementById(zipFieldName + "Text");
		var zipTextField  = document.getElementById(zipFieldName + "TextField");
		var zipLookup     = document.getElementById(zipFieldName + "Lookup");
		var zipLookupList = document.getElementById(zipFieldName + "LookupList");
		
		var zipCodes = dataElement.getElementsByTagName('zip');
		if (zipCodes.length > 0) {
			// Turn this field off so the data is not processed when the form is submitted.
			zipText.style.visibility = 'hidden';
			zipText.style.display = 'none';
			zipTextField.name = 'nothing'; 
			
			// Blank out the zip lookup.
			zipLookup.style.visibility = 'visible';
			zipLookup.style.display = 'block';
			zipLookupList.name = zipFieldName;
			var selectedItem = 0;
			zipLookupList.options.length = zipCodes.length + 1;
			for (var i = 0; i < zipCodes.length; ++i) {
				zipLookupList.options[i + 1] = new Option(
					zipCodes[i].getElementsByTagName('zipCode')[0].childNodes[0].nodeValue,
					zipCodes[i].getElementsByTagName('zipCode')[0].childNodes[0].nodeValue
				);
				if (zipCodes[i].getElementsByTagName('zipCode')[0].childNodes[0].nodeValue == zipTextField.value) {
					selectedItem = i + 1;
				}
			}
			zipLookupList.selectedIndex = selectedItem;
		} else {
			// Turn this field off so the data is not processed when the form is submitted.
			switchToZipInput(fieldName);
		}
	} else if (xmlHttpZipCodes.readyState == 4) {
		alert("Unable to get zip codes: " + xmlHttpZipCodes.status);
	}
}

