var currentFormElement=null;

function showDate(e) {
	currentFormElement=e;
	// display the book button
	// or hide it if it's visible
	if(document.getElementById("calPopup").style.visibility=="visible") {
		document.getElementById("calPopup").style.visibility="hidden";
	 	return;
	}
		
	// get the absolute position of the element
	if(e.value!="")
	{
		var vs=e.value.split("/");
		current_month=vs[1];
		current_year=vs[2];
	}
	else // put the top year / month in
	{
		current_month=upper_month;
		current_year=upper_year;
	}
	loadAndShowPopup(e);
}

function closePopup() {
	document.getElementById("calPopup").style.visibility="hidden";
}

function goNextMonth() {
	current_month++;
	if(current_month > 12) {
		current_month=1;
		current_year++;
	}
	
	getPopup();
	
	return false;
}

function goPrevMonth() {
	if(current_month == upper_month && current_year == upper_year) {
		alert("You can't choose a date before today!");
		return false;
	}
	
	current_month--;
	if(current_month < 1) {
		current_month = 12;
		current_year--;
	}
	
	getPopup();
	
	return false;
}

function getPopup() {
	xmlhttp=getHTTPSocket();
	
	rndm=Math.random() * 999999;
		
	xmlhttp.open("GET", "getCalPopup.php?m="+current_month+"&y="+current_year+"&forcerefresh="+rndm,true);
 	xmlhttp.onreadystatechange=function() {
  		 if (xmlhttp.readyState==4) {
   			document.getElementById("theCal").innerHTML=xmlhttp.responseText;
 		 }
 	}
 	xmlhttp.send(null);

}

function loadAndShowPopup(e){
	xmlhttp=getHTTPSocket();
	
	rndm=Math.random() * 999999;
		
	xmlhttp.open("GET", "getCalPopup.php?m="+current_month+"&y="+current_year+"&forcerefresh="+rndm,true);
 	xmlhttp.onreadystatechange=function() {
  		 if (xmlhttp.readyState==4) {
   			document.getElementById("theCal").innerHTML=xmlhttp.responseText;
   			var p=findPos(e);
			p[0]=parseInt(p[0]);
			p[1]=parseInt(p[1]);
			p[0]+=130;
			p[1]-=5;
			document.getElementById("calPopup").style.left=p[0]+"px";
			document.getElementById("calPopup").style.top=p[1]+"px";
			document.getElementById("calPopup").style.visibility="visible";

 		 }
 	}
 	xmlhttp.send(null);

}

function calendarClick(d,m,y) {
	if(currentFormElement.name == "end_date") // MAKE SURE WE DON'T GET AN EARLIER END DATE
	{
		if(document.getElementById("start_date").value!="")
		{
			e=document.getElementById("start_date").value.split("/");
			if( (y < e[2]) ||
				(y == e[2] && m < e[1]) ||
				(y == e[2] && m == e[1] && d <= e[0])) {
					alert("Please choose an end date that's later than your start date!");
					return;
			}
		}
	}
	else
	{
		if(document.getElementById("end_date").value!="")
		{
			e=document.getElementById("end_date").value.split("/");
			if( (y > e[2]) ||
				(y == e[2] && m > e[1]) ||
				(y == e[2] && m == e[1] && d >= e[0])) {
					alert("Please choose a start date that's earlier than your end date!");
					return;
			}
		}
	}
	
	currentFormElement.value=d+"/"+m+"/"+y;
	closePopup();
}

function checkForm() {
	var msg="";
	
	if(document.theForm.start_date.value=="")
		msg+="\nYou need to enter a start date";
		
	if(document.theForm.end_date.value=="")
		msg+="\nYou need to enter an end date";
		
	if(document.theForm.first_name.value=="")
		msg+="\nYou need to enter your first name";
		
	if(document.theForm.last_name.value=="")
		msg+="\nYou need to enter your surname";
		
	if(document.theForm.email.value=="")
		msg+="\nYou need to enter an email address";
	else if(document.theForm.email.value.indexOf("@") < 0)
		msg+="\nYou need to enter a valid email address";
		
	if(document.theForm.daytime_phone.value=="")
		msg+="\nYou need to enter a daytime phone number";
	else if(!/^[\d ()+-]+$/.test(document.theForm.daytime_phone.value))
		msg+="\nYou need to enter a valid phone number";
		
	if(document.theForm.address.value=="")
		msg+="\nYou need to enter an address";
	
	if(document.theForm.no_of_adults.selectedIndex==0)
		msg+="\nYou need to choose the number of adults";
		
	if(document.theForm.no_of_children.selectedIndex==0)
		msg+="\nYou need to choose the number of children";
	
	if(msg=="")
		return true;
	
	alert("Please correct the following errors:"+msg);
	return false;
}

function findPos(obj) {
	curleft=curtop=0;
	if(obj.offsetParent) {
		curleft=obj.offsetLeft;
		curtop=obj.offsetTop;
		while(obj=obj.offsetParent){
			curleft+=obj.offsetLeft;
			curtop+=obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

