var searchTerms = new Array(), searchTermsIndex = 0;
var searchParams = new Array();
var inputTop = 0, inputLeft = 0, inputWidth = 0, inputId="";
var searchesCount = 0, receivedSearchCount=0, searchfor="city";
var lastsearchterm = "", lastsearchparams = new Array();
var lastfollowupid = "";
function locationSuggest(oInput, e, search_for, countryid, regionid, cityid, followupid) {
	if (!e) var e = window.event;
	
	var searchTerm = oInput.value;
	if (!searchTerm) return false;
		
	searchTerms[searchTermsIndex] = searchTerm;
	lastsearchterm = searchTerm;
	
	searchParams[searchTermsIndex] = new Array();
	searchParams[searchTermsIndex]["searchfor"] = search_for ? search_for : "city";
	searchParams[searchTermsIndex]["countyid"] = countryid ? countryid : 0;
	searchParams[searchTermsIndex]["regionid"] = regionid ? regionid : 0;
	searchParams[searchTermsIndex]["cityid"] = cityid ? cityid : 0;
	lastsearchparams = searchParams[searchTermsIndex];
	
	searchTermsIndex = searchTerms.length;

	setTimeout("sendAjaxSearchLocation('"+searchTermsIndex+"')", 500);
	
	inputTop = findPosY(oInput);
	inputLeft = findPosX(oInput);
	inputWidth = oInput.clientWidth;
	inputId = oInput.id;
	
	lastfollowupid = followupid;
	
	return true;
}

function sendAjaxSearchLocation(theNr) {
	if (theNr!=searchTermsIndex && theNr!=-1) return false;
	
	var thesearchterm = (theNr==-1) ? lastsearchterm : searchTerms[searchTermsIndex-1];
	var thesearchparams = new Array();
	thesearchparams = (theNr==-1) ? lastsearchparams : searchParams[searchTermsIndex-1];
	
	var path = theUrl+"?searchfor="+(thesearchparams["searchfor"] ? thesearchparams["searchfor"] : "city")+"&searchterm="+thesearchterm+"&searchcount="+searchesCount;
	if (thesearchparams["countyid"]) path += "&countryid="+thesearchparams["countyid"];
	if (thesearchparams["regionid"]) path += "&regionid="+thesearchparams["regionid"];
	if (thesearchparams["cityid"]) path += "&cityid="+thesearchparams["cityid"];

	searchesCount++;
	new AG_ajax(path,'ajaxResultSearchLocation');

	searchTerms = new Array();
	searchTermsIndex = 0
	searchParams = new Array();
}

function ajaxResultSearchLocation(data) {
	data = unescape(data);
	
	var suggestString = "";
	var dataElements = data.split("|||"), i=1, el;
	
	if (dataElements[0]) {
		if (dataElements[0]<receivedSearchCount) return false;
		else receivedSearchCount = dataElements[0];
	}
	
	var suggestBox = document.getElementById("searchLocationSuggestBox");
	if (!suggestBox) {
		var suggestBox = document.createElement("div");
		suggestBox.id = "searchLocationSuggestBox";
		suggestBox.className = "searchLocationSuggestBox";
		
		document.body.appendChild(suggestBox);
		
		var suggestBox = document.getElementById("searchLocationSuggestBox");
	}

	suggestBox.style.top = (inputTop+20)+"px";
	suggestBox.style.left = inputLeft+"px";
	suggestBox.width = inputWidth+"px";
	suggestBox.style.display = "";
	
	var theCloseSign = document.getElementById("theCloseSign");
	if (!theCloseSign) {
		var theCloseSign = document.createElement("div");
		theCloseSign.id = "theCloseSign";
		theCloseSign.style.position = "absolute";
		theCloseSign.style.top = (inputTop+1)+"px";
		theCloseSign.style.left = (inputLeft+inputWidth-20)+"px";
		theCloseSign.innerHTML = "x";
		theCloseSign.style.width = "20px";
		theCloseSign.style.height = "20px";
		theCloseSign.style.cursor = "pointer";
		theCloseSign.onclick = function () {
			hideSuggestBox();
		}
		document.body.appendChild(theCloseSign);
		
		var theCloseSign = document.getElementById("theCloseSign");
	}
	theCloseSign.style.top = (inputTop+1)+"px";
	theCloseSign.style.left = (inputLeft+inputWidth-20)+"px";
	theCloseSign.style.display = "";
	
	suggestBox.innerHTML = "";
	if (dataElements.length>2) {
		while (el = dataElements[i++]) {
			var el_parts = el.split("|");
			var anchorElement = document.createElement("a");
			anchorElement.setAttribute("id", el_parts[0]);
			anchorElement.setAttribute("searchvalue", el_parts[1]);
			anchorElement.href="javascript:void(0);";
			anchorElement.innerHTML = "<div>"+el_parts[1]+"</div>";
			anchorElement.onclick = function () {
				addSearchedLocationTo(this);
			}
			suggestBox.appendChild(anchorElement);
		}
	} else {
		suggestBox.innerHTML = "<div>no result</div>";
	}
	
}

function hideSuggestBox() {
	var suggestBox = document.getElementById("searchLocationSuggestBox");
	if (suggestBox) {
		suggestBox.style.display = "none";
	}
	var theCloseSign = document.getElementById("theCloseSign");
	if (theCloseSign) {
		theCloseSign.style.display = "none";
	}
}

function addSearchedLocationTo(oAnchor) {
	var searchInput = document.getElementById(inputId), addString = "", selectedSearch = oAnchor.getAttribute("searchvalue");
	
	searchInput.value = selectedSearch;
	searchInput.setAttribute("valueid", oAnchor.getAttribute("id"));
	
	hideSuggestBox();
	
	if (lastfollowupid) resolveFollowup(lastfollowupid, false);
	
	return true;
}

function resolveFollowup(followupid, trueorfalse) {
	var followupObj = document.getElementById(followupid);
	if (followupObj) {
		if (!trueorfalse) trueorfalse = false;
		followupObj.value = "";
		followupObj.disabled = trueorfalse;
	}
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y) curtop += obj.y;
	return curtop;
}

var showregionselectid = "";
function showRegion(oSelect, selectid) {
	if (!oSelect.value) return false;
	
	showregionselectid = selectid;
	
	var path = theUrl+"?searchfor=getregionlist&countryid="+oSelect.value;
	new AG_ajax(path,'ajaxResultShowRegion');
	
	var otherregions = document.getElementById(selectid+"_checkbox_box");
	if (otherregions) {
		if (oSelect.value=="US") {
			otherregions.style.display = "none";
		} else {
			otherregions.style.display = "block";
		}
	}
}
function ajaxResultShowRegion(data) {
	data = unescape(data);

	if (!showregionselectid) return false;
	
	var regionObj = document.getElementById(showregionselectid);
	
	if (regionObj) {
		var dataElements = data.split("|||"), i=1, el;
		regionObj.innerHTML = "<option value=''>Choose</option>";
		if (dataElements.length>2) {
			var el, i=1;
			while (el = dataElements[i++]) {
				var el_parts = el.split("|");
				var optionElement = document.createElement("option");
				optionElement.value = el_parts[1];
				optionElement.setAttribute("valueid", el_parts[0]);
				optionElement.innerHTML = el_parts[1];
				regionObj.appendChild(optionElement);
			}
		}

		regionObj.disabled = false;
	}
}

var showcityselectid = "";
function showCity(oSelect, selectid, countryselectid) {
	if (!oSelect.value) return false;
	
	showcityselectid = selectid;
	
	if (oSelect.value=="other") {
		var path = theUrl+"?searchfor=getcitylist&countryid="+document.getElementById(countryselectid).value;
	} else {
		var path = theUrl+"?searchfor=getcitylist&region="+oSelect.value+"&regionid="+oSelect.options[oSelect.selectedIndex].getAttribute("valueid");
	}
	new AG_ajax(path,'ajaxResultShowCity');

}
function ajaxResultShowCity(data) {
	data = unescape(data);

	if (!showregionselectid) return false;
	
	var cityObj = document.getElementById(showcityselectid);
	
	if (cityObj) {
		var dataElements = data.split("|||"), i=1, el;
		cityObj.innerHTML = "<option value=''>Choose</option>";
		if (dataElements.length>2) {
			var el, i=1;
			while (el = dataElements[i++]) {
				var el_parts = el.split("|");
				var optionElement = document.createElement("option");
				optionElement.value = el_parts[1];
				optionElement.setAttribute("valueid", el_parts[0]);
				optionElement.innerHTML = el_parts[1];
				cityObj.appendChild(optionElement);
			}
		}
		
		cityObj.disabled = false;
	}
}

function showRegionOther(oCheckbox, countryselectid, regionselectid, cityselectid) {
	if (oCheckbox.checked) {
		var regionObj = document.getElementById(regionselectid);
		if (regionObj) {
			regionObj.innerHTML = "<option value='other' valueid='"+document.getElementById(countryselectid).value+"'>Other</option>";
			regionObj.disabled=true;
		}
		
		showCity(document.getElementById(regionselectid), cityselectid, countryselectid);
	} else {
		showRegion(document.getElementById(countryselectid), regionselectid)
	}
}

