var xmlHttp
var startclass, endclass;
var t1, t2;
var startid = 0, countid = 0;  

function RefreshNewCalendar(obj)
{
//	$quote = obj.value;
  $quote = document.getElementById("diaryquote").value;
  document.getElementById("HLBcal_msg").innerHTML="<img src='../images/ajaxloading.gif'>Loading refreshed calendar - please wait...";
	$accomid = document.getElementById("accomid").value;
	$page = document.getElementById("pagenumber").value;
	ShowNewCalendar($accomid,$quote,$page);
}

function ShowNewCalendar(accomid,quote,page) {
if (accomid.length==0)  { 
  document.getElementById("HLBcalendarcontainer").innerHTML="Error accommodation ID not specified.";
  return;
}
  
ajax=GetXmlHttpObject();
if (ajax==null)  {
  alert ("Sorry your browser does not support AJAX!");
  return;
} 

ajax.open("POST", "/livebooking/ajaxnewcalendar.php", true);
ajax.setRequestHeader('Content-type','application/x-www-form-urlencoded;charset=UTF-8;');
ajax.onreadystatechange= function () {
	if ((ajax.readyState==4) && (ajax.status == 200))
		document.getElementById("HLBcalendarcontainer").innerHTML=ajax.responseText;
}

postvars="accomid="+accomid;
postvars+="&diaryquote="+quote;
postvars+="&diarypage="+page;
ajax.send(postvars);
} 

function StateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById("HLBcalendarcontainer").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;
}


 
function HighlightCell($id, $count, $price, $msg)
{
/* 	if (($id == startid) && ($count == countid)) 
	{
		clearTimeout(t1);
	}
	else
	{
		startid = $id;
		countid = $count;
	}
*/	
	clearTimeout(t2);
	
	if ($price != "")
	{
	 document.getElementById("diaryprice").innerHTML = $price;	 
	}
	if ($msg != "")
	{
	 document.getElementById("HLBcal_msg").innerHTML = $msg;	 
	}	

	// Set bookend
	startclass = document.getElementById("c" + $id).className;
	if (startclass.indexOf("diaryfirstnight") == -1)
		document.getElementById("c" + $id).className += " bookendstart";

	endclass = document.getElementById("c" + eval($id + $count - 1)).className;
	if (endclass.indexOf("diarybookedlastday") == -1)
		document.getElementById("c" + eval($id + $count -1 )).className += " bookendend";

 
	for ($i = $id ; $i < $id + $count ; $i++)
	{
		var $cell = document.getElementById("c" + $i);
		if ($cell) $cell.style.backgroundColor ='CYAN';
	}
}


function deHighlightCell($id,$count)
{
	
	// Remove bookend 
//	t1 = setTimeout(function() {
		document.getElementById("c" + $id).className = startclass;
		document.getElementById("c" + eval($id + $count - 1)).className = endclass;

		for ($i = $id ; $i < $id + $count ; $i++)
		{
			var $cell = document.getElementById("c" + $i);
			if ($cell) $cell.style.backgroundColor ="";
		}
//	} , 1 );
	
	// Timeout function to remove it if we're done.
	t2 = setTimeout(function() {
		document.getElementById("diaryprice").innerHTML = "";	 
		document.getElementById("HLBcal_msg").innerHTML = document.getElementById("defaultmsg").innerHTML;	 	
	} , 400 );
}
 
function ScrollPagesMore()
{
	pn = document.getElementById("pagenumber")
	pn.value = eval(parseInt(pn.value) + 6);
	RefreshNewCalendar(this);
} 

function ScrollPagesLess()
{
	pn = document.getElementById("pagenumber")
	pn.value = eval(parseInt(pn.value) - 6);
	RefreshNewCalendar(this);
} 
