﻿
// Instantiate an XHR DataSource and define schema as an array:
//     ["ResultNodeName",
//     "QueryKeyAttributeOrNodeName",
//     "AdditionalParamAttributeOrNodeName1",
//     ...
//     "AdditionalParamAttributeOrNodeNameN"]
oACDS = new YAHOO.widget.DS_XHR("/sitecore modules/Web/CitySearchService.ashx",["\n", "\t"]);
oACDS.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
oACDS.maxCacheEntries = 100;
oACDS.scriptQueryAppend = "";

// Instantiate AutoComplete
oAutoComp = new YAHOO.widget.AutoComplete('city','cityAjaxContainer', oACDS);
// Animation -- Optional
// Container will expand and collapse vertically
oAutoComp.animVert = true;

// Container will expand and collapse horizontally
oAutoComp.animHoriz = false;

// Container animation will take 0.3 seconds to complete
oAutoComp.animSpeed = 0.3;

// Other
oAutoComp.queryDelay = 0;
oAutoComp.minQueryLength = 0;
oAutoComp.useShadow = true;
oAutoComp.typeAhead = false;
oAutoComp.autoHighlight = false;
// Enable an iFrame shim under the container element
oAutoComp.useIFrame = true;
oAutoComp.formatResult = function(oResultItem, sQuery) {
	// This was defined by the schema array of the data source
	var city = oResultItem[0];
	var state = oResultItem[1];
	var stateid = oResultItem[2];

	if (state != '') {
		var sMarkup = city + ', ' + state;
	} else {
		var sMarkup = city;
	}
	return (sMarkup);
};

if(oAutoComp.itemSelectEvent != null){
    oAutoComp.itemSelectEvent.subscribe(updateState);
    updateAjaxInputs();
 }
function updateAjaxInputs(country) {
	//var myDataSource3 = new YAHOO.widget.DS_XHR("/common/include/ajaxsearch1.php", ["\n", "\t"]);
	oACDS.scriptQueryAppend = 'province='+document.inputForm.province.value+'&state='+document.inputForm.state.value+'&country='+document.inputForm.country.value;
	oACDS.flushCache();
}
function updateState(a, b, c) {
	var query = String(b[2]);
	var queryArray = query.split(",");
	var stateName = queryArray[2];
	// Search through states
	//document.inputForm.state.value
	if (document.inputForm.country.value=="United-States") {
		var opts = document.inputForm.state;
		for (var i=0; i<opts.length; i++) {
			if (opts[i].value.replace(/-/g," ") == stateName) {
				opts[i].selected = true;
				break;
			}
		}
	}
	if (document.inputForm.country.value=="Canada")	{
		var opts = document.inputForm.province;
		for (var i=0; i<opts.length; i++) {
			if (opts[i].value.replace(/-/g," ") == stateName) {
				opts[i].selected = true;
				break;
			}
		}
	}
	updateAjaxInputs();
}

function checkCountry() {
	var frm = document.inputForm;
	var country = frm.country.options[frm.country.selectedIndex].value;
	var displayState = "", displayProvince = "", rightPadding = "";
	if (country == "United-States") {
		displayState = "block";
		displayProvince = "none";
		rightPadding = "12px";
	}
	else if (country == "Canada") { 
		displayState = "none";
		displayProvince = "block";
		rightPadding = "12px";
	}
	else {
		displayState = "none";
		displayProvince = "none";
		rightPadding = "0px";
	}
	if (document.all) { // IE, >=5
		divState.style.display = displayState;
		divProvince.style.display = displayProvince;
		home_state_search.style.paddingRight = rightPadding;
	}
	else if (document.getElementById) { // Netscape, >=6
		document.getElementById("divState").style.display = displayState;
		document.getElementById("divProvince").style.display = displayProvince;
		document.getElementById("home_state_search").style.paddingRight = rightPadding;
	}
}
