var request;
var req;
var selectedIndex=-1;
var codes;
var hotelnames = new Array();;
var controlKeys = new Array ( 13 , 38 , 40);
function isControlKey(key)
{
	for (var count=0;count<controlKeys.length ; count++ )
	{
			if (controlKeys[count]==key)
			{
				return true;
			}
	}
	return false;
}


	try
		{
			   request = new window.XMLHttpRequest();
		       if (request.overrideMimeType) 
			   {
					 request.overrideMimeType('text/xml');
			   }
		}
		catch (e) 
		{
			  request = new ActiveXObject("Msxml2.XMLHTTP");
		}

function getValues_Home(e, url , methodName , paramName , paramValue , length)
{
		
		var cityCodeParam = document.getElementById('otherCityCode').value;
		if (cityCodeParam=="on" && document.getElementById('hotelNameString').value != "Hotel Name contains...")
		{
			alert("Please select a city and then enter the Hotel name");
			document.getElementById('hotelNameString').value = "Hotel Name contains...";
			return;
		}
		if (!e)
		{
				e = window.event
		}
		if (!e)
		{
			var e = window.event;
		}	
		if (e.keyCode==13 || e.keyCode==9)
		{
			if (selectedIndex!=-1)
			{
				document.getElementById('hotelNameString').value= hotelnames[selectedIndex];
				document.getElementById('hotelNamesDiv').style.display = "none";
				document.getElementById('hotelNamesDiv').style.display = "none";
				e.cancelBubble = true;
			}
			return;
		}
		if (isControlKey(e.keyCode))
		{
			return;
		}	
		var citySubstring = trim(paramValue);
		if (citySubstring.length<length)
		{
			document.getElementById('hotelNamesDiv').style.display = "none";
			return;
		}
	
		//alert("Param string is :: "+ url+"?method=" + methodName + "&" + paramName +"=" + paramValue +"&cityCode="+cityCodeParam);
		request.open("Get" , url+"?method=" + methodName + "&" + paramName +"=" + paramValue +"&cityCode="+cityCodeParam);
		request.send(null);
		request.onreadystatechange = showRetrievedValues;
		if (request.readyState==4)
		{
			showRetrievedValues()
		}
}

function showRetrievedValues()
{
		if (request.readyState==4)
		{
			codes = new Array();
			hotelnames = new Array();
			var responseDoc = request.responseXML
			//alert(request.responseText);
			var hotelsList = responseDoc.getElementsByTagName("Hotel")
			var tableString = "<TABLE WIDTH='100%'>";
			if (hotelsList.length==0)
			{
				alert("No Hotel exists with the name you are tying to find");
				document.getElementById('hotelNameString').value="";
				return;

			}
			for (var count=0;count<hotelsList.length; count++ )
			{
				var hotel = hotelsList.item(count);
				var nameElement = hotel.getElementsByTagName("Hotel-Name")[0];
				var name = nameElement.firstChild.nodeValue;
				hotelnames[hotelnames.length] = name;
				var codeElement = hotel.getElementsByTagName("Hotel-Code")[0];
				var cityCode = codeElement.firstChild.nodeValue;
				tableString += "<TR>";
				codes[codes.length] = name;
				var otherHotelString = document.getElementById('hotelNameString').value;
				var boldString = "<B>" + otherHotelString + "</B>";
				var displayHotelName = name.replace(otherHotelString , boldString); 
				tableString += "<TD WIDTH='100%'><A HREF='#' NAME='Link_Hotels' onclick='putValue(event , \""+ count+"\")' style='color:#005893'>" +displayHotelName+"</A></TD>";
				tableString += "</TR>";
			}
			if (hotelsList.length>0)
			{
					var textElement = document.getElementById("hotelNameString");
					document.getElementById("hotelNamesDiv").innerHTML =  tableString;
					repositionDiv();
					if(hotelsList.length<3){
						document.getElementById("hotelNamesDiv").style.height=hotelsList.length*21;
					}
					else{
						document.getElementById("hotelNamesDiv").style.height=63;
					}
					
					document.getElementById("hotelNamesDiv").style.display = "block";
					if(selectedIndex<0 || selectedIndex >= hotelsList.length){
						selectedIndex = 0;
					}
					document.getElementsByName("Link_Hotels")[selectedIndex].parentNode.parentNode.bgColor = "#82CAFF"
			}
			else
			{
				document.getElementById("hotelNamesDiv").style.display = "none"
			}
		}
		
	}

     function  keyPressed_Hotels(e)
	 {
			var ENTER_KEY = 13;
			var UP_KEY = 38;
			var DOWN_KEY = 40;
			var TAB_KEY =  9;
			if (window.event)
			{
					var keyCode = window.event.keyCode;
			}
			else
			{
					var keyCode = e.keyCode
			}

			if (keyCode==ENTER_KEY)
			{
				if (selectedIndex!=-1)
				{
					document.getElementById('hotelNameString').value= hotelnames[selectedIndex];
					document.getElementById('hotelNamesDiv').style.display = "none";
					document.getElementById('hotelNamesDiv').style.display = "none";
					e.cancelBubble = true;
				}
			}

			if (keyCode==TAB_KEY)
			{
				if (selectedIndex!=-1)
				{
					document.getElementById('hotelNameString').value= hotelnames[selectedIndex];
					document.getElementById('hotelNamesDiv').style.display = "none";
					document.getElementById('hotelNamesDiv').style.display = "none";
					e.cancelBubble = true;
				}
			}


	 }
	

	function moveup_Hotels()
	{
			var totalMatches = getTotalMatches_Hotels();
			if(totalMatches > 0)
			{
				totalMatches = document.getElementsByName("Link_Hotels").length;
				document.getElementsByName("Link_Hotels")[selectedIndex].parentNode.parentNode.bgColor = "#FFFFFF"
				if (selectedIndex==0)
				{
					selectedIndex = totalMatches - 1;
				}
				else
				{
					selectedIndex -= 1;
				}
				for (var count=0;count<document.getElementsByName("Link_Hotels").length ;count++ )
				{
					document.getElementsByName("Link_Hotels")[selectedIndex].parentNode.parentNode.bgColor = "#82CAFF";
				}
				document.getElementsByName("Link_Hotels")[selectedIndex].parentNode.parentNode.bgColor = "#FFFFFF";
				document.getElementsByName("Link_Hotels")[selectedIndex].parentNode.parentNode.bgColor = "#82CAFF"
				try{
				document.getElementsByName("Link_Hotels")[selectedIndex].focus();
				document.getElementById("hotelNameString").focus();
				}catch(e){
				}
			}

	}



	function movedown_Hotels()
	{
			var totalMatches = getTotalMatches_Hotels();
			if (totalMatches > 0)
			{
				document.getElementsByName("Link_Hotels")[selectedIndex].parentNode.parentNode.bgColor = "#FFFFFF"
				if (selectedIndex==(totalMatches-1))
				{
					selectedIndex = 0;
				}
				else
				{
					selectedIndex += 1;
				}
				for (var count=0;count<document.getElementsByName("Link_Hotels").length ;count++ )
				{
					document.getElementsByName("Link_Hotels")[selectedIndex].parentNode.parentNode.bgColor = "#82CAFF";
				}
				document.getElementsByName("Link_Hotels")[selectedIndex].parentNode.parentNode.bgColor = "#FFFFFF";
				document.getElementsByName("Link_Hotels")[selectedIndex].parentNode.parentNode.bgColor = "#82CAFF"
				try{
				document.getElementsByName("Link_Hotels")[selectedIndex].focus();
				document.getElementById("hotelNameString").focus();
				}catch(e){
				}
			}
	}
		

	function getTotalMatches_Hotels()
	{
			var totalMatches;
			if (document.getElementsByName("Link_Hotels")==null || document.getElementsByName("Link_Hotels").length==0)
			{
				totalMatches = 0;
			}
			else
			{
				totalMatches = document.getElementsByName("Link_Hotels").length;
			}
			return totalMatches;
	}

	function setColors_Hotels()
	{
		var totalMatches = getTotalMatches_Hotels();
		for (var count=0;count<totalMatches ;count++ )
		{
			var row = 	document.getElementsByName("Link_Hotels")[count].parentNode.parentNode;
			if (count==selectedIndex)
			{
				row.bgcolor = "#82CAFF";
			}
			else
			{
				row.bgcolor = "#FFFFFF";
			}
		}
	}



	function putValue(e , count)
	{
		document.getElementById('hotelNamesDiv').style.display = "none";
		document.getElementById('hotelNamesDiv').style.display = "none";
		document.getElementById('hotelNameString').value= hotelnames[count];
		e.cancelBubble = true;
	}


function repositionDiv()
{

	var starRatingTitlePaneDivHeight = document.getElementById("guestsTable").offsetHeight;
	var childPaneTableHeight = document.getElementById("childrenAgesTable").offsetHeight;
	var initialHeightOccupied;
	if (navigator.appName.indexOf("Explorer")==-1){
		initialHeightOccupied = 197;
	}else{
		initialHeightOccupied = 190;
	}
	if(childPaneTableHeight>0){
		if (navigator.appName.indexOf("Explorer")==-1){
			initialHeightOccupied = 217;
		}else{
			initialHeightOccupied = 207;
		}
	}
	var topOffset = starRatingTitlePaneDivHeight +childPaneTableHeight+initialHeightOccupied;
	document.getElementById("hotelNamesDiv").style.position = "absolute";
			if (navigator.appName.indexOf("Explorer")==-1)
			{
				if(window.screen.width == 1152 && window.screen.height == 864){
					XValue = 13;
					YValue = 155;
				}

				if(window.screen.width == 1280 && window.screen.height == 720){
					XValue = 13;
					YValue = 155;
				}

				if(window.screen.width == 1280 && window.screen.height == 1024){
					XValue = 12;
					YValue = 155;
				}

				if(window.screen.width == 1024 && window.screen.height == 768){
					XValue = 15;
					YValue = 155;
				}

				if(window.screen.width == 1280 && window.screen.height == 768){
					XValue = 13;
					YValue = 155;
				}
				//alert(XValue+" IE "+YValue);
				document.getElementById("hotelNamesDiv").style.left = XValue//elementLeft + 2;
				if (navigator.appName.indexOf("Explorer")!=-1)
				{
						document.getElementById("hotelNamesDiv").style.top = YValue + topOffset - 25;
				}
				else
				{
					document.getElementById("hotelNamesDiv").style.top = YValue + topOffset-30;					
				}
			}
			else
			{
				if(window.screen.width == 1152 && window.screen.height == 864){
					XValue = 13;
					YValue = 180;
				}

				if(window.screen.width == 1280 && window.screen.height == 720){
					XValue = 13;
					YValue = 180;
				}

				if(window.screen.width == 1280 && window.screen.height == 1024){
					XValue = 12;
					YValue = 180;
				}

				if(window.screen.width == 1024 && window.screen.height == 768){
					XValue = 14;
					YValue = 180;
				}

				if(window.screen.width == 1280 && window.screen.height == 768){
					XValue = 13;
					YValue = 180;
				}
				//alert(XValue+" MOZZILLA "+YValue);
				document.getElementById("hotelNamesDiv").style.left = XValue//elementLeft + 2;
				if (navigator.appName.indexOf("Explorer")!=-1)
				{
					document.getElementById("hotelNamesDiv").style.top = YValue + topOffset - 25
				}
				else
				{
					document.getElementById("hotelNamesDiv").style.top = YValue + topOffset-30;					
				}
			}		
				
}


/*
	Here, the 2 time outs set in the search results page have to be cleared
*/