/*
	This is the JavaScript file for the How to Create CAPTCHA Protection using PHP and AJAX Tutorial

	You may use this code in your own projects as long as this 
	copyright is left in place.  All code is provided AS-IS.
	This code is distributed in the hope that it will be useful,
 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	
	For the rest of the code visit http://www.WebCheatSheet.com
	
	Copyright 2006 WebCheatSheet.com	

*/
//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
function makeRequest(url, param) {///alert(url + "dev"); exit;
//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);
 // alert(url + "dev");
//  alert(receiveReq.responseText);
 //Set the function that will be called when the XmlHttpRequest objects state changes
   receiveReq.onreadystatechange = updatePage; 
   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
//   alert(receiveReq.readyState);
	 str = receiveReq.responseText;
	// alert(str);
  if(str.match("Success"))
   {
		document.getElementById('spanmessage').innerHTML = receiveReq.responseText;	   
		window.location.href="http://www.lakemurrayboatclub.com/boatingthanks.php";
   }
   else
   {
//	   	alert(receiveReq.responseText);
	  	document.getElementById('spanmessage').innerHTML = receiveReq.responseText;	   
   }
 // document.getElementById('spanmessage').innerHTML = receiveReq.responseText;
   //Get a reference to CAPTCHA image
   img = document.getElementById('imgCaptcha'); 
   //Change the image
   img.src = 'create_image.php?' + Math.random();
   
//   window.location.href="http://www.lakemurrayboatclub.com/boatingthanks.php";
 }
}
//Called every time when form is perfomed
function getParam(theForm) {

if(theForm.name.value == '')
	{
		alert('Please Enter First Name.');
		document.getElementById('name').focus();
		return false;
	}
	if(theForm.last.value == '')
	{
		alert('Please Enter Last Name.');
		document.getElementById('last').focus();
		return false;
	}
	if(theForm.add.value == '')
	{
		alert('Please Enter Address.');
		theForm.add.focus();
		return false;
	}
	if(theForm.city.value == '')
	{
		alert('Please Enter City.');
		theForm.city.focus();
		return false;
	}
	if(theForm.state.value == '')
	{
		alert('Please Enter State.');
		theForm.state.focus();
		return false;
	}
	if(theForm.zip.value == '')
	{
		alert('Please Enter ZipCode.');
		theForm.zip.focus();
		return false;
	}
	if(theForm.phone.value == '')
	{
		alert('Please Enter Phone.');
		theForm.phone.focus();
		return false;
	}
	if(theForm.email.value == '')
	{
		alert('Please Enter Email.');
		document.getElementById('email').focus();
		return false;
	}
	if(theForm.email.value!='')
	{
		var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
		var ans = theForm.email.value.match(emailExp);
		if(ans!=theForm.email.value)
		{
			alert("Please Enter Valid Email.");
			theForm.email.focus();
			return false;				
		}
	}
	
	if(theForm.Marketing.value == '')
	{
		alert('Please Enter How You First Heard About Us.');
		theForm.Marketing.focus();
		return false;
	}
	
		
	if(theForm.txtCaptcha.value == '')
	{
		alert('Please Enter Security Code.');
		theForm.txtCaptcha.focus();
		return false;
	}
	
 //Set the URL
 var url = 'http://www.lakemurrayboatclub.com/CGI-BIN/formmail.php';
 

 //alert(url);
 //Set up the parameters of our AJAX call
var postStr = "txtCaptcha=" + encodeURIComponent( theForm.txtCaptcha.value ) + "&" + theForm.name.name + "=" + encodeURIComponent( theForm.name.value ) + "&" + theForm.last.name + "=" + encodeURIComponent( theForm.last.value )+ "&" + theForm.add.name + "=" + encodeURIComponent( theForm.add.value ) + "&" + theForm.city.name + "=" + encodeURIComponent( theForm.city.value )
+ "&" +  theForm.state.name + "=" + encodeURIComponent( theForm.state.value ) + "&" +  theForm.phone.name + "=" +encodeURIComponent( theForm.phone.value ) + "&" + theForm.email.name + "=" + encodeURIComponent( theForm.email.value ) + "&" +  theForm.zip.name + "=" +encodeURIComponent( theForm.zip.value ) +"&" +  theForm.comments.name + "=" +encodeURIComponent( theForm.comments.value ) +"&" + theForm.recipient.name + "=" + encodeURIComponent(theForm.recipient.value ) + "&" + theForm.subject.name + "=" + encodeURIComponent( theForm.subject.value )+"&" +  theForm.company.name + "=" + encodeURIComponent( theForm.company.value )+"&" +  theForm.Marketing.name + "=" + encodeURIComponent( theForm.Marketing.value );
//alert(postStr);
 //Call the function that initiate the AJAX request
//alert(postStr);
makeRequest(url, postStr);
}

function refreshCaptcha()
{  
	//Get a reference to CAPTCHA image
   img = document.getElementById('imgCaptcha'); 
   //Change the image
   img.src = 'create_image.php?' + Math.random();
	
}