var xmlHttp


function suggestSurname()
{
	
	xmlHttp=GetXmlHttpObject();
	var url="suggestSurname.php"
	url=url+"?sid="+Math.random()
	xmlHttp.onreadystatechange=surnameChanged
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)

}


function surnameChanged()
{
	var div_id="surnameSource";
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		//alert(xmlHttp.responseText);
		document.getElementById(div_id).value=xmlHttp.responseText;
		document.getElementById('txtHint').innerHTML="<small><i><font color=green>Loading complete.</font> If you now type a family name, a list of existing users will appear.";
		var oTextbox = new AutoSuggestControl(document.getElementById("txtfamily"), new surnameSuggestions());
	}
}




function calculateBalance(row)
{
	//function for calculating initial balance

	if(row==-1)
	{
		var paymentCombo= document.tblConfirm.paymentMethod
		var amountDue= document.tblConfirm.amountDue.value
		var vinitialAmountPaid=document.tblConfirm.initialAmountPaid.value		
		if (!isFloat(vinitialAmountPaid))
		{
			alert("Please enter a number in [Initial Amount Paid] box")
			document.tblConfirm.initialAmountPaid.focus()
			return false;
		}		
		var initialBalance =amountDue-vinitialAmountPaid
		if (paymentCombo.value !=3)
			document.tblConfirm.initialBalance.value=initialBalance
		else
		{
			initialBalance =parseFloat(initialBalance) +parseFloat(vinitialAmountPaid)
			document.tblConfirm.initialBalance.value=initialBalance
		}		
	}
	else
	{
		var paymentCombo= eval("document.tblConfirm.paymentMethod"+row)
		var amountDueBox= eval("document.tblConfirm.amountDue"+row)
		var initialAmountPaidBox= eval("document.tblConfirm.initialAmountPaid"+row)
		var amountDue= amountDueBox.value
		var initialAmountPaid= initialAmountPaidBox.value
		var initialBalance  =amountDue-initialAmountPaid

		var initialBalanceBox=eval("document.tblConfirm.initialBalance"+row)
		if (paymentCombo.value !=3)
			initialBalanceBox.value=initialBalance
		else
		{
			initialBalance =parseFloat(initialBalance) +parseFloat(initialAmountPaid)
			initialBalanceBox.value=initialBalance
		}
		document.tblConfirm.initialAmountPaid.value=parseFloat(document.tblConfirm.initialAmountPaid.value)+parseFloat(initialAmountPaid)
		document.tblConfirm.paymentMethod.value=paymentCombo.value
	}
	if (paymentCombo.value==5)
		document.tblConfirm.freeText.focus();
}


function changeFoodReqd()
{
	var state=true;
	var foodReqd=document.formBooking.foodReqd.value

	if(foodReqd==1)
		state=false;
	document.formBooking.menuType.disabled=state;
	document.formBooking.buffetPrice.disabled=state;
	document.formBooking.menuDetails.disabled=state;
	document.formBooking.foodTime.disabled=state;
}




function getProduct()
{		
	var vRoom=document.formBooking.roomID.value;
	var div_id="productSpan";
	document.getElementById(div_id).innerHTML="";
	
	xmlHttp=GetXmlHttpObject();
	
	var url="getProduct.php"
	url=url+"?q1="+vRoom
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=productChanged
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function productChanged() 
{ 	
	var div_id="productSpan";
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 		document.getElementById(div_id).innerHTML="<select name=productID>"+xmlHttp.responseText+"</select>";
}


function getReason()
{	
	var freason1=document.formBooking.reason1.value;
	var freason2=document.formBooking.reason2.value;
	
	var div_id="reason2span";
	document.getElementById(div_id).innerHTML="";
	document.getElementById('btnNext').value="Please wait";
	document.getElementById('btnNext').disabled="disabled";
	document.getElementById(div_id).innerHTML="<i>Loading.. </i><img src=../img/loading.gif>"; 
	
	xmlHttp=GetXmlHttpObject();
	
	var url="getReason.php"
	url=url+"?q1="+freason1+"&q2="+freason2
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=reasonChanged
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


function reasonChanged() 
{ 	
	var div_id="reason2span";
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
 		document.getElementById(div_id).innerHTML=xmlHttp.responseText; 
 		document.getElementById('btnNext').value="Next step";
 		document.getElementById('btnNext').disabled="";
 	}
}

function searchBooking(from, to)
{
	document.getElementById("searchResult").innerHTML="";	
	if (from.length==0)
	{
		alert("Please fill in start date");
		document.forms[0].fromDate.focus();
		return;
	}
	if (to.length==0)
	{
		alert("Please fill in end date");
		document.forms[0].toDate.focus();
		return;
	}	
	var start = Date.parse(from);
	var end = Date.parse(to);	
	if (start > end )
	{
		alert("Start date must be earlier than end date");
		return;
	}	
	var today=Date.parse(Date());	
	if (start < today )
	{
		alert("Pleae select a date in future ");
		document.forms[0].fromDate.focus();
		return false;
	}
	xmlHttp=GetXmlHttpObject();
	var url="searchBooking.php"
	url=url+"?from="+from+"&to="+to
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=stateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


function stateChanged() 
{ 
	var div_id="searchResult"
	document.getElementById(div_id).innerHTML="<p><i>Searching...</i>" 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 		document.getElementById(div_id).innerHTML=xmlHttp.responseText;
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		 // Firefox, Opera 8.0+, Safari
		 xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		 //Internet Explorer
		 try
	  	{
	  		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  	}
	 	catch (e)
	  	{
	  		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  	}
	}
	return xmlHttp;
}