// sample CITY onfocus:
//   onfocus="city_onFocus(this.form.stateProvince, this.form.country);"
// sample CITY onblur:
//   onblur="city_onBlur(this, this.form.stateProvince, this.form.country);"
// sample STATE onchange:
//   onchange="stateProvince_onChange(this,this.form.country);"
// sample COUNTRY onchange:
//   onchange="country_onChange(this.form.stateProvince,this);"

var isSearch = false;
var keyDownFocusObj = null;

function ciDate_onFocus(ciDate) {
	var checkIn = new validDate(ciDate);
	if (!checkIn.valid) ciDate.value = "";
}

function ciDate_onBlur(ciDate, coDate) {
	var checkIn = new validDate(ciDate);
	if (checkIn.valid) {
		checkIn.setField();
		var checkOut = validDate(coDate);
		if (checkOut.valid) before = (checkIn.diffDate(checkOut.d) <= 0);
		if (!checkOut.valid || before) {
			checkOut.setD(checkIn.d);
			checkOut.nextDay();
			checkOut.setField();
		} 
	}
	else ciDate.value = dateFormatString;
}

function coDate_onFocus(coDate) {
	var checkOut = new validDate(coDate);
	if (!checkOut.valid) coDate.value="";
}

function coDate_onBlur(coDate) {
	var checkOut = new validDate(coDate);
	if (checkOut.valid) checkOut.setField();
	else coDate.value = dateFormatString;
}

function city_onFocus(stateProvince, country) {
	zeroOutState(stateProvince);
	zeroOutCountry(country);
	isSearch = true;
	keyDownFocusObj = stateProvince;
}

function city_onBlur(city, stateProvince, country) {
	if (country.options[2].value.length == 2){
		a = isInList(city.value.toLowerCase(),topCities);
	} else {
		a = isInList(city.value.toLowerCase(),topCitiesThreeChar);
	}
	if (a != false) {
		setDropDown(a[1],stateProvince);
		setDropDown(a[2],country);
	}
	isSearch = false;
}

function stateProvince_onChange(stateProvince, country) {
	c = false;
	if (country.options[2].value.length == 2){
		if(isInList(stateProvince.value,us_states)) c = 'US';
		else if (isInList(stateProvince.value,ca_prov)) c = 'CA';
		else if (isInList(stateProvince.value,misc)) c = 'US';
		else if (isInList(stateProvince.value,au_prov)) c = 'AU';
		else if (isInList(stateProvince.value,mx_prov)) c = 'MX';
	} else {
		if(isInList(stateProvince.value,us_states)) c = 'USA';
		else if (isInList(stateProvince.value,ca_prov)) c = 'CAN';
		else if (isInList(stateProvince.value,misc)) c = 'USA';
		else if (isInList(stateProvince.value,au_prov)) c = 'AUS';
		else if (isInList(stateProvince.value,mx_prov)) c = 'MEX';
	}
	if (c != false) setDropDown(c,country);
}

function country_onChange(stateProvince, country) {
	var cntry = country.options[country.selectedIndex].value;
	if (cntry != 'US' && cntry != 'USA' && cntry != 'CA' && cntry != 'CAN' && cntry != 'AU' && cntry != 'AUS' && cntry != 'MX' && cntry != 'MEX') stateProvince.selectedIndex = 0;
	else {
		if (cntry == 'CA' || cntry == 'CAN') {
			if (!isInList(stateProvince.value,ca_prov)) stateProvince.selectedIndex = 0;
		}
		else if (cntry == 'US' || cntry == 'USA') {
			if (!isInList(stateProvince.value,us_states)) stateProvince.selectedIndex = 0;
		}
		else if (cntry == 'AU' || cntry == 'AUS') {
			if (!isInList(stateProvince.value,au_prov)) stateProvince.selectedIndex = 0;
		}
		else if (cntry == 'MX' || cntry == 'MEX') {
			if (!isInList(stateProvince.value,mx_prov)) stateProvince.selectedIndex = 0;
		}
	}
}

function setDropDown(v,dd) {
	for (i = 0; i < dd.options.length - 1; i++) {
		if (dd.options[i].value == v) {
			dd.selectedIndex = i;
			return true;
		}
	}
	return false;
}

function isInList(elt, list) {
	for (var i = 0; i < list.length; i++) {
		var tempArray = list[i].split(":");
		if (tempArray[0] == elt) return tempArray;
	}
	return false;
}

function zeroOutState(stateProvince){
	stateProvince.selectedIndex = 0;
}

function zeroOutCountry(country){
	country.selectedIndex = 0;
}
