/*
 * constant values used to configure the script
 */
var urlSearchesResults = "SearchesServlet";//  http://192.168.1.177/site_published/
//var urlSearchesResults = "http://www.newmont.co.uk/SearchesServlet";
//document.location + "/SearchesServlet";
var ajaxDelay = 60*60*30;//to change to a more proper value - was 60*60*30

/*************************************************************************************
 * The function used to position the div element for the search results display 
 * in the correct position of the page.
 * Used when the page is loaded or resized.
 */
function setSearchesDiv()
{
	var tableWidth = 1020; 
	var delta = 10;

	if (!dom&&!ie&&!ns4)
	{		return
	}
	x = (document.all)? document.body.clientWidth : (dom)?window.innerWidth  : window.innerWidth;	
	x = (x - tableWidth) / 2;
	if ( x >= 0)
	{	
		x += delta;
	}
	else
	{
		x = delta;
	}
	elemSt = (dom)?document.getElementById("all_container").style : ie? document.all.searchesDiv : document.searchesDiv
	elemSt.visibility = (dom||ie)? "visible" : "show";
	elemSt.left = x;
}
/****************************************************************************
 * for the ajax implementation
 * used to query and get the last 10 searched made in the booking site
 */
 
var httpRequest;

 /* 
 * processes the response from the SearchesServlet
 */
function parseSearchesResponse()
{
    // only if httpRequest shows "loaded"
	if ( httpRequest.readyState == 4 )
	{
		if ( httpRequest.status == 200 ) 
         {
			var xmldoc = httpRequest.responseXML;			
			var results = xmldoc.getElementsByTagName('results').item(0);
			var str = "<nobr><font color=#000000><b>Live searches click here...  </b></font>";
			for ( var i = 0; i < results.childNodes.length; i++ ) 
			{
	if(i==0||i==3||i==6||i==9){var color="<font color=#FF6600>";} 
	if(i==1||i==4||i==7){var color="<font color=#006600>";} 
	if(i==2||i==5||i==8){var color="<font color=#0066FF>";} 
               var searchREsult = results.childNodes.item(i);
               var depart = results.getElementsByTagName("departure")[i].childNodes[0].nodeValue;
               //searchResult.childNodes.item(0);
               var dest = results.getElementsByTagName("destination")[i].childNodes[0].nodeValue;
               var departDate = results.getElementsByTagName("departure-date")[i].childNodes[0].nodeValue;
			   var retDate = results.getElementsByTagName("return-date")[i].childNodes[0].nodeValue;
   			   var airlineCode = results.getElementsByTagName("airline-code")[i].childNodes[0].nodeValue;
  			   var price = results.getElementsByTagName("price")[i].childNodes[0].nodeValue;
           //var num=(i+1);
               str +=color+" <b>"+(i+1) + ". " +depart + " to " + dest + ", depart " + departDate + ", return " + retDate +
					", " + airlineCode + ", &pound;<b>" + price +
					"</b></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			}
			str += "</nobr>"
			var divName="searchResult";
			document.getElementById(divName).innerHTML = str;
		} 
        /*else 
        {
            alert("Latest Searches not loaded.");
        }*/
	}
}

function loadSearches()
{	httpRequest=null
	if (window.XMLHttpRequest)
	{		httpRequest=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		httpRequest=new ActiveXObject("Microsoft.XMLHTTP")
	}
	if (httpRequest!=null)
	{
		httpRequest.onreadystatechange = parseSearchesResponse
		httpRequest.open("GET",urlSearchesResults,true)
		
		if(!document.all)
			httpRequest.overrideMimeType('text/xml')
		else
			httpRequest.setRequestHeader("Content-Type", "text/xml; charset=UTF-8");
		httpRequest.send(null)
	}
	else
	{
		alert("Your browser does not support XMLHTTP.")
	}
//	alert("a facut laod");
}

loadInterval = setInterval("loadSearches()", ajaxDelay);
