// JavaScript Document

var xmlHttp;

function processAjax () {
	//Display results area and call Ajax function
	document.getElementById('displayarea').style.visibility='visible';
	//Add required text/image if necessary
	document.getElementById('displayarea').innerHTML="Please wait ...";

	//callAjaxFunction(URL_To_Process,Parameter1,Parameter2 etc);
	callAjaxFunction('formvalidator/imagevalidation_response.php',document.getElementById('checkCode').value);
}


function callAjaxFunction (url,parameter1) 
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}

	//Define the page to do the processing and echo the resulting info
	url=url + "?checkCode=" + parameter1; 	
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged	//Function to call weh processing complete
	xmlHttp.open("GET",url,true)			//Pass values to page to be picked up with GET
	xmlHttp.send(null)
}


//Supporting Ajax Functions
function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		//Do whatever is needed once the processing has completed
		//Returns 0|Failed wording or 1|OK wording
		arrResult = xmlHttp.responseText.split("|");
		//Show the result on screen
		document.getElementById("displayarea").innerHTML=arrResult[1]; 
		//Complete hidden field to determine if OK or Failed
		document.getElementById("imageCodeValidated").value = arrResult[0];
	}
}


//Standard Function required for processing
function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}


	function checkValidationImage() {
		if(document.getElementById("imageCodeValidated").value==1) {
			return true;
		} else if(document.getElementById("checkCode").value == "") {
			alert("Please enter the validation code you see on screen");
			document.getElementById("checkCode").focus();
			return false;
		} else {
			alert("Sorry, you have entered the code incorrectly. Please re-enter the code, or click 'Change' to get a new code.");
			document.getElementById("checkCode").focus();
			return false;		
		}
	}
	
	function changeImage() {
		var dte = new Date();
		var seed = (dte.getHours() + dte.getMinutes() + dte.getSeconds())
		document.getElementById("validateImage").src = "formvalidator/img.php?regen=y&amp;" + seed;
		document.getElementById("imageCodeValidated").value = "0";	//Show as NOT validated
		document.getElementById("checkCode").value = "";		//Remove existing code
		document.getElementById("displayarea").style.visibility = "hidden";		//Hide Valid/Invalid Message
	}
