
function countryDropdownOnChange() {
	var stateList = document.accountform.ajaxlocation_state;
	var cityList = document.accountform.ajaxlocation_city;
	stateList.options.length = 0;
	cityList.options.length = 0;
	if(document.accountform.ajaxlocation_country.value == "US" 
			|| document.accountform.ajaxlocation_country.value == "CA") {
		document.getElementById("AJAXLocationForm_USCAZipcodeBox").style.display = "block";
		document.getElementById("AJAXLocationForm_USCAStateCityBox").style.display = "none";
		document.getElementById("AJAXLocationForm_StateCityBox").style.display = "none";
	} else {
		document.getElementById("AJAXLocationForm_USCAZipcodeBox").style.display = "none";
		document.getElementById("AJAXLocationForm_USCAStateCityBox").style.display = "none";
		document.getElementById("AJAXLocationForm_StateCityBox").style.display = "block";
		fillStateList(document.accountform.ajaxlocation_country.value);
	}
}

function stateDropdownOnChange() {
	var cityList = document.accountform.ajaxlocation_city;
	cityList.options.length = 0;

	fillCityList(document.accountform.ajaxlocation_state.value);
}

function cityDropdownOnChange() {
	document.accountform.country.value = document.accountform.ajaxlocation_country.value;
	document.accountform.state.value = document.accountform.ajaxlocation_state.value;
	document.accountform.city.value = document.accountform.ajaxlocation_city.value;
}

function locationDropdownOnChange() {
	var loc = document.accountform.ajaxlocation_location.value;
	document.accountform.country.value = document.accountform.ajaxlocation_country.value;
	document.accountform.zip.value = document.accountform.ajaxlocation_zipcode.value;
	document.accountform.city.value = loc.substring(0, loc.indexOf("||"));
	document.accountform.state.value = loc.substring(loc.indexOf("||")+2);
}

function clearZipcode() {
	document.accountform.ajaxlocation_zipcode.value = '';
	
	document.getElementById("AJAXLocationForm_USCAZipcodeBox").style.display = "block";
	document.getElementById("AJAXLocationForm_USCAStateCityBox").style.display = "none";
	document.getElementById("AJAXLocationForm_StateCityBox").style.display = "none";
}

function fillStateList(countryCode) {
	var stateList = document.accountform.ajaxlocation_state;
	stateList.options[0] = new Option('Loading states...', '');

	http.open("get", "/location.php?cmd=getStateList&countryCode=" 
			+ document.accountform.ajaxlocation_country.value);
	http.onreadystatechange = updateStateList;
	http.send(null);
}

function fillCityList(stateCode) {
	var cityList = document.accountform.ajaxlocation_city;
	if(document.accountform.ajaxlocation_state.value == 'ALL') {
		cityList.options[0] = new Option('No City', '');
		return;
	}
	cityList.options[0] = new Option('Loading cities...', '');

	http.open("get", "/location.php?cmd=getCityList&stateCode=" 
			+ document.accountform.ajaxlocation_state.value);
	http.onreadystatechange = updateCityList;
	http.send(null);
}

function zipcodeLookup() {
	document.getElementById("AJAXLocationForm_USCAZipcodeBox").style.display = "none";
	document.getElementById("AJAXLocationForm_USCAStateCityBox").style.display = "block";
	
	/*
	alert("/location.php?cmd=getLocationsByZipcode&zipCode="
			+ document.accountform.ajaxlocation_zipcode.value
			+ "&countryCode="
			+ document.accountform.ajaxlocation_country.value);
			*/
	http.open("get", "/location.php?cmd=getLocationsByZipcode&zipCode="
			+ document.accountform.ajaxlocation_zipcode.value
			+ "&countryCode="
			+ document.accountform.ajaxlocation_country.value);
	http.onreadystatechange = updateLocationList;
	http.send(null);
}

function updateStateList() {
	if(http.readyState == 4) {
		var stateList = document.accountform.ajaxlocation_state;

		var response = http.responseXML.documentElement;
		var country = response.getElementsByTagName('country')[0];
		var name = country.getElementsByTagName('name')[0].firstChild.data;
		var rd = country.getElementsByTagName('regionDesignator')[0].firstChild.data;
		var states = country.getElementsByTagName('region');
		
		stateList.options.length = 0;
		stateList.options[0] = new Option("Please Select a "+rd, "");
		stateList.selectedIndex = 0;

		for(var i=0; i<states.length; i++) {
			stateList.options[stateList.options.length] = 
				new Option(states[i].firstChild.data, states[i].getAttribute('code'));
		}
	}
}

function updateCityList() {
	if(http.readyState == 4) {
		var cityList = document.accountform.ajaxlocation_city;

		var response = http.responseXML.documentElement;
		var state = response.getElementsByTagName('state')[0];
		var name = state.getElementsByTagName('name')[0].firstChild.data;
		var cities = state.getElementsByTagName('city');
		
		cityList.options.length = 0;
		cityList.options[0] = new Option("Please Select a city");
		cityList.selectedIndex = 0;

		for(var i=0; i<cities.length; i++) {
			cityList.options[cityList.options.length] = 
				new Option(cities[i].firstChild.data, cities[i].firstChild.data);
		}
	}
}

function updateLocationList() {
	if(http.readyState == 4) {
		var locationList = document.accountform.ajaxlocation_location;

		var response = http.responseXML.documentElement;
		var zip = response.getElementsByTagName('zip')[0];
		var cities = zip.getElementsByTagName('location');
		
		locationList.options.length = 0;
		locationList.options[0] = new Option("Please Select a location");
		locationList.selectedIndex = 0;

		for(var i=0; i<cities.length; i++) {
			var data = cities[i].getAttribute("city")
				+ '||' + cities[i].getAttribute("stateCode");
			locationList.options[locationList.options.length] = 
				new Option(cities[i].firstChild.data, data);
		}

	}
}

function submitSignup() {
	
	if(!(
			document.accountform.password.value != "" &&
			document.accountform.password.value == document.accountform.confirmpassword.value 
		 )) {
		alert("Passwords must match");
		return false;
	}

	if(!(
			document.accountform.vc_firstname.value != "" 
			/*
			&&
			document.accountform.vc_lastname.value != "" 
			*/
		 )) {
		alert("You must provide a name");
		return false;
	}

	if(!(
			document.accountform.email.value != "" &&
			document.accountform.email.value == document.accountform.email_confirm.value 
		 )) {
		alert("Email addresses must match");
		return false;
	}

	if(!(
		document.accountform.country.value != "" &&
		document.accountform.city.value != "" &&
		document.accountform.state.value != "" 
	 )) {
	alert("You must select your location");
	return false;
	}


	if(!(document.accountform.agreeterms.checked)) {
		alert("You must agree to the Terms Of Use");
		return false;
	}
}

function _signup_init() {
	document.accountform.onsubmit = submitSignup;
}

window.onload = _signup_init;
