function getHTTPObject()
{
   if (window.ActiveXObject) 
       return new ActiveXObject("Microsoft.XMLHTTP");
   else if (window.XMLHttpRequest) 
       return new XMLHttpRequest();
   else 
   {
      alert("Your browser does not support AJAX.");
      reset_interval();
      return null;
   }
}
function setOutput1()
{
	    if(httpObject.readyState == 4)
    {
        var returntext = httpObject.responseText;
       // document.getElementById("scmain1").innerHTML = returntext;
        document.getElementById("scmain2").innerHTML = returntext;
        //document.getElementById("scmain3").className="unhidden";
     	
    }
}
function setOutput2()
{
	    if(httpObject.readyState == 4)
    {
        var returntext = httpObject.responseText;
       // document.getElementById("scmain1").innerHTML = returntext;
        document.getElementById("scmain3").innerHTML = returntext;
       	document.getElementById("scmain3").className="unhidden";
     	document.getElementById("scmain2").className="hidden";
     	
    }
}
function makereservationnow()
{
	var id="makebookingnow";
	if (document.getElementById(id))
	{
		document.getElementById(id).className="hidden";
		document.getElementById("scmain2").className="unhidden";
	}
}
function check_availability()
{
	
	var year="2011";
	var month="9";
	var day="1";
	var nights=0;
	document.getElementById("checkav").innerHTML="Checking Now .....";
	var id = "year";
	if (document.getElementById(id));
	{
		var id1=document.getElementById(id).selectedIndex;
		year=document.getElementById(id).options[id1].value;
	}
	
	id = "month";
	if (document.getElementById(id));
	{
		id1=document.getElementById(id).selectedIndex;
		month=document.getElementById(id).options[id1].value;
	}
	
	id = "day";
	if (document.getElementById(id));
	{
		id1=document.getElementById(id).selectedIndex;
		day=document.getElementById(id).options[id1].value;
	}
	
	id="nights";
	if (document.getElementById(id));
	{
		nights=document.getElementById(id).value;
	}
	var data="&year=" + year + "&month=" + month + "&day=" + day + "&nights=" + nights;
	httpObject = getHTTPObject();
	if (httpObject != null) 
    {
        httpObject.open("GET", "Secret_Garden_Ajax.php?request=check_availability" + data, true);
        httpObject.send(null);  
        httpObject.onreadystatechange = setOutput1;
    }
	
}
function roomstobook()
{
	// this function will look to see if the customer has selected any rooms to book
	// if they have then we can allow them to go to step 2
	// we can also tidy up the text for room(s) by adding or deleting the s
	// this hotel only has two room types
	var room_types=3;
	var type=0;
	var id="";
	var id1="";
	var rooms=0;
	var total_rooms=0;
	var alert_text="";
	for (type=0;type<room_types;type++)
	{
		id="rooms" + type;
		if (document.getElementById(id)) // just checking to see if this select input is displayed
		{
			id1=document.getElementById(id).selectedIndex;
			rooms=document.getElementById(id).options[id1].value;
			
			rooms=parseInt(rooms);
			total_rooms=total_rooms + rooms;
			alert_text=id + " : " + rooms + " : " + total_rooms;
			//alert(alert_text);
			// this is just a tidy thing with the s if they are booking multiple rooms
			id="plurals" + type;
			if (rooms!=1)
			{
				document.getElementById(id).innerHTML="s";
			}
			else
			{
				document.getElementById(id).innerHTML="&nbsp;";
			}
		}
		else
		{
			
		}
	
	}
	total_rooms=parseInt(total_rooms);
	if (total_rooms>0)
	{
		document.getElementById("showstep2").className="unhidden";
		document.getElementById("hidestep2").className="hidden";
	}
	else
	{
		document.getElementById("showstep2").className="hidden";
		document.getElementById("hidestep2").className="unhidden";
	}
}
function gotostep2()
{
	// lets grab all of the inputs
	var year="2011";
	var month="9";
	var day="1";
	var nights=0;
	var room_types=2;
	var data="";
	document.getElementById("showstep2").className="hidden";
	
	document.getElementById("checkingreservation").className="unhidden";
	var id = "year";
	if (document.getElementById(id));
	{
		var id1=document.getElementById(id).selectedIndex;
		year=document.getElementById(id).options[id1].value;
	}
	
	id = "month";
	if (document.getElementById(id));
	{
		id1=document.getElementById(id).selectedIndex;
		month=document.getElementById(id).options[id1].value;
	}
	
	id = "day";
	if (document.getElementById(id));
	{
		id1=document.getElementById(id).selectedIndex;
		day=document.getElementById(id).options[id1].value;
	}
	
	id="nights";
	if (document.getElementById(id));
	{
		nights=document.getElementById(id).value;
	}
	var data="&year=" + year + "&month=" + month + "&day=" + day + "&nights=" + nights;
	// that is the date and nights, now we need the number of rooms selected
	for(var type=0; type<=room_types;type++)
	{
		id="rooms" + type;
		data+="&rooms" + type + "=";
		if (document.getElementById(id)) // just checking to see if this select input is displayed
		{
			id1=document.getElementById(id).selectedIndex;
			rooms=document.getElementById(id).options[id1].value;
			data+=rooms;
		}
		else
		data+="0";
	}
	httpObject = getHTTPObject();
	if (httpObject != null) 
    {
        httpObject.open("GET", "Secret_Garden_Ajax.php?request=gotostep2" + data, true);
        httpObject.send(null);  
        httpObject.onreadystatechange = setOutput2;
    }

}
function setroompriceforthisroom(room,nights,maxpax,rooms)
{
	max_pax=20; //this is way too high but it covers all possibilities
	// lets get the total number of people in this room
	var id="adults" + room;
	var id1="";
	var pax=0;
	var total_pax=0;
	var night=0;
	var price=0;
	var room_price=0;
	var total_price=0;
	var text="";
	var total_text="";
	var adults=0;
	var kids=0;
	if (document.getElementById(id))
	{
		id1=document.getElementById(id).selectedIndex;
		adults=document.getElementById(id).options[id1].value;
		adults=parseInt(adults);
		// now for the kids
		id="children" + room;
		if (document.getElementById(id))
		{
			id1=document.getElementById(id).selectedIndex;
			kids=document.getElementById(id).options[id1].value;
			kids=parseInt(kids);
			total_pax=adults+kids;
			id="roomerror" + room;
			id1="rmb" + room;
			if (total_pax>maxpax)
			{
				document.getElementById(id).className="unhidden";
				
			}
			else
			{
				document.getElementById(id).className="hidden";
			}
			for (night=1;night<=nights;night++)
			{
				
				id1="rmb" + room + "night" + night;
				
				if (total_pax>maxpax)
				{
					
					document.getElementById(id1).className="hidden";
				
				}
				else
				{
					document.getElementById(id1).className="unhidden";
				}
				for (pax=1;pax<=max_pax;pax++)
				{
					id="room" + room + "date" + night + "pax" + pax;
					if (pax==total_pax)
					{
						if (document.getElementById(id))
						{
							document.getElementById(id).className="unhidden";
							price=document.getElementById(id).innerHTML;
							price=parseInt(price);
							room_price+=price;
						}
					}
					else
					{
						if (document.getElementById(id))
						{
							document.getElementById(id).className="hidden";
						}
					}
				}	
			
			}
			
			id="roomtotal" + room;
			if (total_pax>maxpax)
			{
				text="N/A";
				total_text=text;
				
			}
			else
			{
				text=room_price;
			}
			document.getElementById(id).innerHTML=text;
			room_price=0;
			total_text=0;
			// now we need to get the total price for their stay
			var alert_text="";
			for (room=1;room<=rooms;room++)
			{
				id="roomtotal" + room;
				price=document.getElementById(id).innerHTML;
				
				if (price!="N/A")
				{
					price=parseInt(price);
					total_text=parseInt(total_text);
					total_text+=price;
					
				}
				else
				{
					total_text="N/A";
					room=rooms+1;
				}	
			
			}
			
			document.getElementById("total").innerHTML=total_text;
		}
	
	}
	
}
function checkemail(email)
{
	var filter=/^.+@.+\..{2,3}$/;
	if (filter.test(email))
	{
		var testresults=true;
	}
	else
	{
		testresults=false;
	}
	return (testresults);
} 
function process_reservation(rooms,rooms1,rooms2)
{
	
	
	
}
function backselectrooms()
{
	document.getElementById("scmain2").className="unhidden";
	document.getElementById("scmain3").className="hidden";
	
	document.getElementById("checkingreservation").className="hidden";
	document.getElementById("showstep2").className="unhidden";
}
function process_reservation(total_rooms,rooms1,rooms2)
{
	// we have the number of rooms for each type
	// now we need the number of adults and children for each room
	var this_room=0;
	var room_type_number=1; 
	var nights=0;
	var name="";
	var surname="";
	var email="";
	var year=0;
	var month=0;
	var day=0;
	var adults_here=0;
	var kids_here=0;
	var data="";
	var comments=0;
	var tel=0;
	var transfers="";
	
	year=document.getElementById("year").value;
	month=document.getElementById("month").value;
	day=document.getElementById("day").value;
	nights=document.getElementById("nights").value;
	comments=document.getElementById("comments").value;
	var error=false;
	var error_text="";
	name=document.getElementById("name").value;
	surname=document.getElementById("surname").value;
	email=document.getElementById("email").value;
	tel=document.getElementById("contel").value;
	if (document.getElementById("transfersy").checked)
	{
		transfers=document.getElementById("transfersy").value;
	}
	else if (document.getElementById("transfersn").checked)
	{
		transfers=document.getElementById("transfersn").value;
	}
	else if (document.getElementById("transfersm").checked)
	{
		transfers=document.getElementById("transfersm").value;
	}
	else
	{
		transfers="n";
	}
	
	
	// we need to check that these are valid
	
	if (name.length<2)
	{
		error=true;
		error_text+="Your Name\n";
	}
	if (surname.length<2)
	{
		error=true;
		error_text+="Your Surname\n";
	}
	if (!checkemail(email))
	{
		error=true;
		error_text+="A Valid Email Address\n";
	}
	if (error)
	{
		error_text="Please Enter:-\n\n" + error_text;
		alert (error_text);
	}
	else
	{
		document.getElementById("processreservation").innerHTML="Processing Reservation...";
		data="&name=" + name + "&surname=" + surname + "&email=" + email + "&year=" + year + "&month=" + month + "&day=" + day + "&nights=" + nights + "&total_rooms=" + total_rooms + "&book1=" + rooms1  + "&book2=" + rooms2 + "&contel=" +tel + "&transfers=" + transfers;
		for (this_room=1;this_room<=total_rooms;this_room++)
		{
			id="adults" + this_room;
			if (document.getElementById(id))
			{
				id1=document.getElementById(id).selectedIndex;
				adults_here=document.getElementById(id).options[id1].value;
				
				data=data + "&adults" + this_room + "=" + adults_here;
				
			}
			id="children" + this_room;
			if (document.getElementById(id))
			{
				id1=document.getElementById(id).selectedIndex;
				kids_here=document.getElementById(id).options[id1].value;
				
				data=data + "&children" + this_room + "=" + kids_here;
			}
		}
		data+="&comments=" + comments;
		
		httpObject = getHTTPObject();
		if (httpObject != null) 
    	{
        	httpObject.open("GET", "Secret_Garden_Ajax.php?request=makenewbooking" + data, true);
        	httpObject.send(null);  
    	    httpObject.onreadystatechange = setOutput2;
	    }

	}	

}
