if (!sg)
	var sg = {};

/**
 * suggestion module
**/
sg = {
	URI: "destinations.php?destination=",
	active: false,
	pending: null,
	wait: false,
	response: null,
	lastvalue: null,
	comp: {
		div: null,
		dest_li: null
	},
	field: null,
	start: function(val) {
		if (val.length < 3 || val == sg.lastvalue)
			return;
		
		if (sg.wait)
			sg.pending = val;
		else
			sg.AJAX.send(val);
	},
	display: function() {
		var response_n = sg.response.length;
		if (response_n < 1) {
			sg.wait = false;
			sg.clr();
			return;
		}
		sg.clr();
		if (sg.active) {
			sg.clr();
		} else {
			sg.field = document.getElementById("destination");
			sg.comp.dest_li = document.getElementById("destination_li");
		}
		
		sg.comp.div = document.createElement("div");
		sg.comp.div.id = "multiple_destinations_div";
		sg.comp.div.className = "same_city";
		sg.comp.dest_li.appendChild(sg.comp.div);
		if (response_n > 13 || sg.comp.div.style.height > 200)
			sg.comp.div.style.height = "200px";
		sg.active = true;
		
		var ul, li, txt;
		
		ul = document.createElement("ul");
		ul.id = "destinations";
		sg.comp.div.appendChild(ul);
		
		for (i = 0; i < response_n; i++) { 
			li = document.createElement("li");
			ul.appendChild(li);
			li.onclick = function() { sg.set(this); };
			txt = "<input type=\"radio\" id=\"multiple_destinations_radio_" + i + "\" name=\"multiple_destinations_radio\" value=\"" + sg.response[i] + "\"";
			if (response_n == 1)
				txt += "checked=\"checked\"";
			txt += " />";
			txt += "<label for=\"multiple_destinations_radio_" + i + "\">" + unescape(sg.response[i]) + "</label>";
			li.innerHTML = txt;
		}
		
		sg.awake();
	},
	awake: function() {
		sg.wait = false;
		if (sg.pending && sg.pending != sg.lastvalue)
			sg.AJAX.send(sg.pending);
	},
	set: function(obj) {
		sg.field.value = unescape(obj.childNodes[0].value);
	},
	clr: function() {
		if (sg.active) {
			sg.comp.dest_li.removeChild(sg.comp.div);
			sg.active = false;
		} else {
			var div = document.getElementById("multiple_destinations_div");
			if (div)
				div.parentNode.removeChild(div);
		}
	},
	AJAX: {
		GetXmlHttpObject: function() {
			var objXMLHttp = null;
			if (window.XMLHttpRequest)
				objXMLHttp = new XMLHttpRequest();
			else if (window.ActiveXObject) {
				objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
				if (objXMLHttp == null)
					objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			return objXMLHttp;
		},
		send: function(val) {
			sg.wait = true;
			sg.pending = null;
			sg.lastvalue = val;
			var xmlHttp = sg.AJAX.GetXmlHttpObject();
			
			if (!xmlHttp) {
				alert("Browser does not support HTTP Request.");
				return;
			};
			xmlHttp.open("GET", sg.URI + encodeURIComponent(val), true);
			xmlHttp.onreadystatechange = function() {
				if (xmlHttp.readyState ==4) {
					sg.response = eval(xmlHttp.responseText);
					sg.display();
				}
			}
			xmlHttp.send(null);
		}
	}
}