

var qsearch = null;

function getQSearchInstance() {
	if(qsearch == null)
		qsearch = new QSearch();
	return qsearch;
}

function QSearch() {
	
	var request = null;
	var divLvl2 = document.getElementById("lvl2");
	var divLvl3 = document.getElementById("lvl3");
	var ctrys = document.getElementById("sctrys");
	var citys = document.getElementById("scitys");
	var hotels = document.getElementById("shotels");
	
	var getXMLHttpRequest = function(){
		if(request != null)
			return request;
		
		if(typeof(XMLHttpRequest)!='undefined') {
			request = new XMLHttpRequest();
		}
		else {
			var axO = ['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.4.0',
				'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'], i;
			for(var i=0; i < axO.length; i++)
				try {
					request = new ActiveXObject(axO[i]);
					break;
				} catch(e){}
		}	
		request.abort();
		return request;
	}
	
	
	var removeAll = function(options) {
		while(options.length > 0)
			options.remove(options[0]);
	}

	var getObjId = function(value) {
		var pos = value.indexOf(":");
		if(pos > -1)
			return value.substring(0,pos);
		return value;
	}
	var getObjHrefname = function(value) {
		var pos = value.indexOf(":");
		if(pos > -1)
			return value.substring(pos + 1);
		return value;
	}
	
	this.choseCountry = function() {
		var req = getXMLHttpRequest();
		
		var url = "/servlets/sastabase2.DispatcherServlet?impl=menuajax&ctryid=" + getObjId(ctrys.value);
		req.open("GET", url , true);
	
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.status == 200)
					processCountryChoose(req.responseXML);
		    }
		};

		req.send(null);
	}
	
	
	var processCountryChoose = function(rslt) {
		var resCitys = rslt.getElementsByTagName("city");
		
		removeAll(hotels.options);
		if(!hotels.disabled)
			hotels.disabled = true;
		divLvl2.style.display = "";
		removeAll(citys.options);
		citys.options.add(document.createElement("option"), 0);
		for(var i=0; i < resCitys.length; i++) {
			var option = document.createElement("option");
			option.text = resCitys[i].getAttribute("name");
			option.hrefname = resCitys[i].getAttribute("hrefname");
			option.value = resCitys[i].getAttribute("id");
			citys.options.add(option, i + 1);
		}

		citys.disabled = citys.options.length <= 1;
		if(citys.options.length <= 1)
			divLvl2.style.display = "none";
		else
			divLvl2.style.display = "";		
	}
	
	this.choseCity = function() {
		var req = getXMLHttpRequest();
		
		var url = "/servlets/sastabase2.DispatcherServlet?impl=menuajax&cityid=" + citys.value;

		req.open("GET", url , true);
	
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.status == 200)
					processCityChoose(req.responseXML);
		    }
		};

		req.send(null);
	}
	
	var processCityChoose = function(rslt) {
		var resHotels = rslt.getElementsByTagName("hotel");

		removeAll(hotels.options);
		divLvl3.style.display = "";	
		hotels.options.add(document.createElement("option"), 0);
		for(var i=0; i < resHotels.length; i++) {
			var option = document.createElement("option");
			option.text = resHotels[i].getAttribute("name");
			option.hrefname = resHotels[i].getAttribute("hrefname");
			option.value = resHotels[i].getAttribute("id");
			hotels.options.add(option, i + 1);
		}
		hotels.disabled = hotels.options.length <= 1;
		if(hotels.options.length <= 1)
			divLvl3.style.display = "none";
		else
			divLvl3.style.display = "";
	}
	
	var getHref = function() {
		var ctryHrefname = getObjHrefname(ctrys.value).toLowerCase();
		
		var cityHrefname = "";
		var hotelHrefname = "";

		for(var i=1; i < citys.options.length; i++)
			if(citys.options[i].selected) {
				cityHrefname = citys.options[i].hrefname.toLowerCase();
				break;
			}
		for(var i=1; i < hotels.options.length; i++) {
			if(hotels.options[i].selected) {
				hotelHrefname = hotels.options[i].hrefname.toLowerCase();
				break;
			}
		}
		var href = null;
		if(typeof(ctryHrefname) != "undefined" && ctryHrefname != "") { 
			href = "/"+ctryHrefname + "/";
			if(typeof(cityHrefname) != "undefined" && cityHrefname != "") {
				href += cityHrefname + "/";
				if(typeof(hotelHrefname) != "undefined" && hotelHrefname != "")
				href += hotelHrefname + "/";				
			}
		}
		return href;
	}
	
	this.perfom = function() {
		var href = getHref();
		if(href == null) {
			alert("Вы ничего не выбрали");
			return;
		}
		window.location.href = href;
	}

}
