/*************************************************************************
*	@ AJAX
*	@ author B,10/01/2008 
*	@ mash_figaro@hotmail.com
*	@ Version 1.0
**************************************************************************/
var bustcachevar=1;//bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects="";
var rootdomain="http://"+window.location.hostname;
var bustcacheparameter="";

function createXMLHTTPObject() { 
	var xmlhttp = false;
	  try {
		  // Mozilla / Safari / IE7
		  xmlhttp = new XMLHttpRequest();
	  } catch (e) {
		var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP' );
		var success = false;
		for (var i=0;i < XMLHTTP_IDS.length && !success; i++) {
		  try {
			  xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
			  success = true;
			} catch (e) {}
		}
		if (!success) {
		  throw new Error('Unable to create XMLHttpRequest.');
		}
	 }
	 return xmlhttp;
}
// Handle while loading 
function loadpage(page_request, containerid){
	var loadingtext="Loading Information";
		if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
			document.getElementById(containerid).innerHTML=page_request.responseText;
			if(containerid == 'divRateHotelier')
			{
				iPos = document.getElementById(containerid).innerHTML.toLowerCase().indexOf("<img ");
				document.getElementById(containerid).innerHTML = document.getElementById(containerid).innerHTML.substring(0, iPos) + "<br>" + document.getElementById(containerid).innerHTML.substring(iPos);
			}
		}else{
			document.getElementById(containerid).innerHTML="<im" + "g src=\"http://www.fargotravel.com/images/loading.gif\" alt=\""+loadingtext+"\"><br><blink><b>"+loadingtext+"</b></blink>";
		}
}
// call ajax method get
function ajaxpage(url, containerid){
	var page_request = createXMLHTTPObject();
	page_request.onreadystatechange=function(){
		loadpage(page_request, containerid);
	};
	if (bustcachevar){ //if bust caching of external page
		bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();
	}
	page_request.open('GET', url+bustcacheparameter, true);
	page_request.send(null);
}
function getRequestBody(pForm) {
	var nParams = new Array();
	for (var i=0 ; i < pForm.elements.length; i++) {
		var pParam = encodeURIComponent(pForm.elements[i].name);
		pParam += "=";
		pParam += encodeURIComponent(pForm.elements[i].value);
		nParams.push(pParam);
	} 
	return nParams.join("&");        
}
// call ajax method post
// ajaxpost('cal.php','divCal',this.form);
function ajaxpost(url,containerid,pForm){
	var page_request = false;
	page_request = createXMLHTTPObject();	
	page_request.onreadystatechange=function(){
			loadpage(page_request, containerid);
	};
	if (bustcachevar){ //if bust caching of external page
		bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();
	}
	var pBody = getRequestBody(pForm); 
	page_request.open('POST', url+bustcacheparameter, true);
	page_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	page_request.send(pBody);
}


//*****************************************************/
// ajax load option for select box
// Ex : <select id="test" onchange="loadOption('script.php?id='+this.value,'test');">
function handleLoadOption(page_request, containerid,selID){
	var loadingtext="Loading Information";
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
		document.getElementById(containerid).innerHTML='';
		var stringArray=null;
		var stringArray = page_request.responseText.split('##');
		for (var i=0; i < stringArray.length-1; i++){
			var temp=null;
			temp=stringArray[i].split('**');
			if(selID==null){selID=temp[0];}
			document.getElementById(containerid).options[i] = new Option(temp[1],temp[0]);
			
				//document.getElementById(containerid).options[i].selected=true;
			
			
		}
		delete stringArray;
		//alert(selID);
		document.getElementById(containerid).value=selID;
	}else{
		//handle while loading
	}
}
function handleLoadOptionDirection(page_request, containerid,selID){
	var loadingtext="Loading Information";
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
		document.getElementById(containerid).innerHTML='';
		var stringArray=null;
		var stringArray = page_request.responseText.split('##');
		for (var i=0; i < stringArray.length-1; i++){
			var temp=null;
			temp=stringArray[i].split('**');
			if(selID==null){selID=temp[0];}
			
			document.getElementById(containerid).options[i] = new Option(temp[1],temp[0]);
			if(temp[1]=='--Airports nearby--'){
				document.getElementById(containerid).options[i].style.backgroundColor= '#3A6BFF';
				document.getElementById(containerid).options[i].style.color= '#FFFFFF';
				document.getElementById(containerid).options[i].style.fontWeight= 'bold';
			}
			if(temp[1]=='--Attractions--'){
				document.getElementById(containerid).options[i].style.backgroundColor= '#EB5615';
				document.getElementById(containerid).options[i].style.color= '#FFFFFF';
				document.getElementById(containerid).options[i].style.fontWeight= 'bold';
			}
			if(temp[1]=='--Market--'){
				document.getElementById(containerid).options[i].style.backgroundColor= '#24D2EC';
				document.getElementById(containerid).options[i].style.color= '#FFFFFF';
				document.getElementById(containerid).options[i].style.fontWeight= 'bold';
			}
			if(temp[1]=='--Shopping Center--'){
				document.getElementById(containerid).options[i].style.backgroundColor= '#EC24EA';
				document.getElementById(containerid).options[i].style.color= '#FFFFFF';
				document.getElementById(containerid).options[i].style.fontWeight= 'bold';
			}
			if(temp[1]=='--Restaurant--'){
				document.getElementById(containerid).options[i].style.backgroundColor= '#9a24ec';
				document.getElementById(containerid).options[i].style.color= '#FFFFFF';
				document.getElementById(containerid).options[i].style.fontWeight= 'bold';
			}
			if(temp[1]=='--Railway station / Bus station / Boat pier--'){
				document.getElementById(containerid).options[i].style.backgroundColor= '#635da7';
				document.getElementById(containerid).options[i].style.color= '#FFFFFF';
				document.getElementById(containerid).options[i].style.fontWeight= 'bold';
			}
			//alert('temp : '+temp[2]);
			if(selID==temp[2] && containerid=='from'){
				//alert('test');
				document.getElementById(containerid).options[i].selected=true;
			}
			
		}
		delete stringArray;
		//alert(selID);
		//document.getElementById(containerid).value=selID;
	}else{
		//handle while loading
	}
}

function loadOption(url, containerid, selID){
	var page_request = createXMLHTTPObject();
	page_request.onreadystatechange=function(){
		if(containerid=='from' || containerid=='to' || containerid=='from_other' || containerid=='to_other' || containerid=='select_from' || containerid=='select_to' || containerid=='select_from_other' || containerid=='select_to_other'){
				handleLoadOptionDirection(page_request, containerid,selID);
		}else{
			handleLoadOption(page_request, containerid,selID);
		}
	};
	page_request.open('GET', url, true);
	page_request.send(null);
}
//*****************************************************/
