//Gets the browser specific XmlHttpRequest Object 
function getXmlHttpRequestObject() {
 if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); //Mozilla, Safari ...
 } else if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); //IE
 } else {
    //Display our error message
    alert("Your browser doesn't support the XmlHttpRequest object.");
 }
}

//Our XmlHttpRequest object
var receiveReq = getXmlHttpRequestObject();

//Initiate the AJAX request
// Altered by rbaril on May 3, 2007 -- added passthru param (was limited to a single task)
function makeRequest(url, param, passthru) {
//If our readystate is either not started or finished, initiate a new request
 if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
   //Set up the connection to captcha_test.html. True sets the request to asyncronous(default) 
   receiveReq.open("POST", url, true);
   //Set the function that will be called when the XmlHttpRequest objects state changes
   receiveReq.onreadystatechange = function() {eval(passthru)}; 
   receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   receiveReq.setRequestHeader("Content-length", param.length);
   receiveReq.setRequestHeader("Connection", "close");

   //Make the request
   receiveReq.send(param);
 }   
}

//Called every time our XmlHttpRequest objects state changes
function updatePage() {
 //Check if our response is ready
 if (receiveReq.readyState == 4) {
   //Set the content of the DIV element with the response text
   document.getElementById('result').innerHTML = receiveReq.responseText;
   //Get a reference to CAPTCHA image
   img = document.getElementById('imgCaptcha'); 
   //Change the image
   img.src = 'scripts/create_image.php?' + Math.random();
 }
}

//Called every time when form is perfomed
function getParam(theForm) {
 //Set the URL
 var url = 'scripts/captcha.php';
 //Set up the parameters of our AJAX call
 var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value );
 //Call the function that initiate the AJAX request
 makeRequest(url, postStr, 'updatePage()');
}

// Called when a receipt number has been submitted through AJAX (rbaril - May 3, 2007)
function updateReceipt(){
	//Check if our response is ready
	if (receiveReq.readyState == 4) {
		//check the response...
		if (receiveReq.responseText == 'invalid'){
			// bad receipt number, alert them...
			alert('Sorry, that receipt number does not correspond to any in our database.\nPlease try again.');
			// now send them back to that field
			document.contactform.Receipt.focus();
		} else if (receiveReq.responseText == 'used'){
			// this receipt has been used already
			alert('Sorry, that receipt number has been used already.\nPlease try again.');
			// now send them back to that field
			document.contactform.Receipt.focus();
		}
	}
}

// Called when a receipt number has been entered. (rbaril - May 3, 2007)
function check_receipt() {
	//Set the URL
	var url = 'scripts/check_receipt.php';
	//Set up the parameters of our AJAX call
	var postStr = "receipt=" + encodeURIComponent( document.contactform.Receipt.value );
	//Call the function that initiate the AJAX request
	makeRequest(url, postStr, 'updateReceipt()');
}

function validate()
{
	
	var numericExpression = /^[0-9]+$/;


	if (document.contactform.First.value == "")
	{
		// First Name Missing
		alert("Please enter your first name.");
		return false;
	}
	else if (document.contactform.Last.value == "")
	{
		// Last Name Missing
		alert("Please enter your last name.");
		return false;
	}
	else if (document.contactform.EmailFrom.value=="")
	{
		// Email Address Missing
		alert("Please enter your email address.");
		return false;
	}
	else if (document.contactform.EmailFrom.value.indexOf("@") == -1)
	{
		// Invalid Email Address Format
		alert("Invalid email address format. Please enter a valid email address.");
		return false;
	}
	else if ((document.contactform.EmailFrom.value.indexOf("@") +1) >= document.contactform.EmailFrom.value.length)
	{
		// Invalid Email Address Format
		alert("Invalid email address format. Please enter a valid email address.");
		return false;
	}
	else if (document.contactform.FirstVisit.value == "")
	{
		// FirstVisit Missing
		alert("Was this your first visit to Hu's on First?");
		return false;
	}
	else if (document.contactform.HearAbout.value == "")
	{
		// HearAbout Missing
		alert("How did you hear about Hu's on First?");
		return false;
	}
	else if (document.contactform.Server.value == "")
	{
		// Server Name Missing
		alert("Please enter your Server's name.");
		return false;
	}
	else if (document.contactform.Date.value == "")
	{
		// Date Missing
		alert("Please enter the Date of your visit.");
		return false;
	}
		//end of entry
		
	else if (document.contactform.RateServer.value == "")
	{
		// RateServer Missing
		alert("Please rate your server.");
		return false;
	}
	else if (document.contactform.RateFood.value == "")
	{
		// RateFood Missing
		alert("Please rate your food.");
		return false;
	}
	else if (document.contactform.RateAtmosphere.value == "")
	{
		// RateAtmosphere Missing
		alert("Please Rate the Atmosphere/Ambiance of the restaurant.");
		return false;
	}
	else if (document.contactform.RateExperiance.value == "")
	{
		// RateExperiance Missing
		alert("Please rate your experience.");
		return false;
	}
	else if (document.contactform.RateReturn.value == "")
	{
		// RateReturn Missing
		alert("How likely are you to dine again at Hu's?");
		return false;
	}
	else
	{
		// Validation Successful
		getParam(document.commmentform)
		alert("Thank you for taking the time to share your opinion with us. We hope to see you again soon!");
		return true;
	}
}
