// JavaScript Document

var xmlHttp = createXmlHttpRequestObject();

//hold the remote server address

var serverAddress = "./../scripts/validate_other_contact.php";

//when set to true show detailed errors

var showErrors = true;

//initialize the validation requests cache

var cache = new Array();


function createXmlHttpRequestObject()
{
	var xmlHttp;

	try
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{

		var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHTTP");


		for (var i=0; i<xmlHttpVersions.length && !xmlHttp; i++)
		{
			try
			{

			xmlHttp =  new ActiveXObject(xmlHttpVersions[i]);
			}
			catch(e) {}
		}
	}



		if(!xmlHttp)
			alert("Error Creating the XMLHttpRequest object.");
		else
			return xmlHttp;
}


//function that displays the error message

function displayError($message){

	//ignore errors if showErrors is false

	if (showErrors)
	{
		//turn error displaying off
		showErrors = false;
		//display error message

		alert("Error encountered: \n" + $message);
		//retry validation after 10 seconds
		setTimeout("validate();", 10000);
	}

}

//the function handles the validation for any form field

function validate(inputValue, fieldID) {

	if(inputValue != '') {

		//only continue if xmlHttp isn't void

		if (xmlHttp)
		{
				//if we have recieved no nll parameters, we add them to cache in the
				//form of the query string to be sent to the server for validation
				if (fieldID)
				{
					//encode values for the safely adding them to an Http request query strong

					inputValue = encodeURIComponent(inputValue);
					fieldID = encodeURIComponent(fieldID);
					//add the values to the queue
					cache.push("inputValue=" + inputValue + "&fieldID=" + fieldID);
				}
				//try and connecto to the server
				try
				{
					//continue only if the XMLHttpRequest object isn't busy
					//and the cache is not empty

					if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
						 && cache.length > 0)
					{
						//get a new set of parameters from the cache
						var cacheEntry = cache.shift();
						//makes a server request to validate the extracted data
						xmlHttp.open("POST", serverAddress, true);
						xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
						xmlHttp.onreadystatechange = handleRequestStateChange;
						xmlHttp.send(cacheEntry);
					}
				}
					catch (e)
					{
						//dipplay an error when failing to connect to the server
						displayError(e.toString());
					}
		}
	}
}



function handleRequestStateChange()
	{


		if (xmlHttp.readyState == 4)
		{

			if (xmlHttp.status == 200)
			{
				try
				{
				  readResponse();
				}
				catch(e)
				{

					displayError(e.toString());
				}
			}
			else
			{

				displayError(xmlHttp.statusText);
			}
		}

	}


function readResponse()
	{

		var response = xmlHttp.responseText;

		//server error?

		if(response.indexOf("ERRNO") >= 0 || response.indexOf("errror:") >= 0 || response.length == 0)
		throw(response.length == 0 ? "Server error." : response);

		//get the response in xml format

		responseXML = xmlHttp.responseXML;
		//get document element
		xmlDoc = responseXML.documentElement;
		result = xmlDoc.getElementsByTagName("result")[0].firstChild.data;
		fieldID = xmlDoc.getElementsByTagName("fieldid")[0].firstChild.data;
		//find the HTML element that displays the error
		message = document.getElementById(fieldID + "Failed");
		//show the error or hide the error
		message.className = (result == "0") ? "error" : "hidden";
		//call validate () again, in case there are values left in the cahce
		setTimeout("validate();", 500);


}


function setFocus() {

	document.getElementById("txtName").focus();

}


