var laststate = -1  //index of last selected cat

function initSearchEng() {

	if (typeof(defCountry) == "undefined") defCountry = ""
	if (typeof(defstate) == "undefined") defstate = ""


	sourceCtl = ""
	targetCtl = ""
	arrLoading = new Array("loading...          ")
	arrLoadingId = new Array("0")
	arrData = new Array()
	arrDataid = new Array()
	arrData["cmbstate"] = new Array()
	arrDataid["cmbstate"] = new Array()
	arrData["cmbcity"] = new Array()
	arrDataid["cmbcity"] = new Array()
	defcity = ""
	selectedcity=''
	setText(frm.cmbcountry, defCountry)
	countryChange(frm.cmbcountry)

}

function setText(ctl, newText) {
	for (var i=0; i<ctl.length; i++) {
		if (ctl[i].text == newText) {
			ctl[i].selected = true
			ctl.selectedIndex = i
			return true
		}
	}

	return false
}

function getText(selBox) {
	return selBox[selBox.selectedIndex].value
}

function countryChange(ctl) {
	//repopulate cities
	sourceCtl = ctl
	targetCtl = frm.cmbstate

	ctlChange("GetState")
}

function stateChange(ctl) {
		sourceCtl = ctl
		targetCtl = frm.cmbcity
		lastcat = ctl.selectedIndex
		ctlChange("GetCity")
}

function ctlChange(func) {
	var selArray = arrData[targetCtl.name][getText(sourceCtl)]
	var selArrayid = arrDataid[targetCtl.name][getText(sourceCtl)]

	if (typeof(selArray) == "undefined") {
		//lock form controls
		objRS.busy = true
		setAccess(false, frm.cmbcountry, frm.cmbstate, frm.cmbcity)

		populate(targetCtl, arrLoading, arrLoadingId)
		if (targetCtl == frm.cmbstate) populate(frm.cmbcity, arrLoading, arrLoadingId)

		objRS.getData(func, getText(sourceCtl))
	} else
		rsCallBack(selArray,selArrayid)
}


function rsCallBack(data,dataid) {
	arrData[targetCtl.name][getText(sourceCtl)] = data
	arrDataid[targetCtl.name][getText(sourceCtl)] = dataid

	populate(targetCtl, data, dataid)

	setAccess(true, frm.cmbcountry, frm.cmbstate, frm.cmbcity)
	objRS.busy = false

	if (targetCtl == frm.cmbstate) stateChange(frm.cmbstate)
}

function populate(selBox, data, dataid) {
	var startNum = 0
	selBox.length = 0
	var selBoxStatus = 0

/*	if (selBox.name == "cmbcity" && data[0].substring(0, 10) != "loading...") {
		selBox[0] = new Option("All citys", "0")
		startNum = 1
	}*/

	for (var i=0; i<data.length; i++){
		selBox[i+startNum] = new Option(data[i],dataid[i])
		if(eval('frm.s'+selBox.name).value==dataid[i]){
			selBox.selectedIndex=i;
			selBoxStatus = 1
		}
	}
	if (selBoxStatus==0) {
		selBox.selectedIndex = 0
	}

	if (selBox.name == "cmbstate") {
		if (setText(selBox, defstate)) defstate = ""
	} else {
		if (setText(selBox, defcity)) defcity = ""
	}
}

function setAccess(access, ctls) {
	var ctl

	if (document.layers) {
		for (var i=1; i<arguments.length; i++) {
			ctl = arguments[i]
			if (typeof(ctl) != "object") continue

			if (access)
				ctl.onFocus = ""
			else {
				ctl.onFocus = ctl.blur
				ctl.blur()
			}
		}
	} else {
		for (var i=1; i<arguments.length; i++) {
			ctl = arguments[i]
			if (typeof(ctl) != "object") continue
			ctl.disabled = !access
		}
	}
}


