// JavaScript Document

// Holds instance of xhtmlhttprequest
var xmlHttp = createXmlHttpRequestObject();

// Holds the remote server address
var serverAddress = "validate.php";

// whe set to true, disply detailed error messages
var showErrors = true;

// Initialise the validation request cache
var cache = new Array();

// Creates an XMLHttpRequest Instance
function CreateXmlHttpRequestObject() {
	
		var xmlHttp;
		
		try {
			// All new browsers
			xmlHttp = new XMLHttpRequest();
		}
		
		catch(e) {
			
			// assume IE6 or older
			var XmlHttpVersions = new Array("MSXML2.XMLHTTTP.6.0",
											"MSXML2.XMLHTTTP.5.0",
											"MSXML2.XMLHTTTP.4.0",
											"MSXML2.XMLHTTTP.3.0",
											"MSXML2.XMLHTTTP",
											"Microsoft.XMLHTTP");

			for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
				try {
					xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
				}
				
				catch (e) {}
			}
		}
		
		// Return created object oe display an error message
		if (!xmlHttp)
			displayError("Error Creating the XMLHTTPREQUEST Object");
		else
			return xmlHttp;
}

// Function to disply error message
function displayError($message) {
	
	if(showErrors) {
		
		// turn off error disply
		showError = false;
		
		// display error message
		alert("Error Encountered: \n" + $message);
		setTimeout("validate();", 10000);
	}
}

// Function to handle the field validation
function validate(inputValue, fieldID) {
	
	if(xmlHttp) {
		
		if(fieldID) {
			
			inputValue = encodeURIComponent(inputValue);
			fieldID = encodeURIComponent(fieldID);
			
			cache.push("inputValue=" + inputValue + "&fieldID=" + fieldID);
		}
		
		try {
			if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) && cache.length > 0) {
				
				var cacheEntry = cache.shift();
				
				xmlHttp.open("POST", serverAddress, true);
				xmlHttp.setRequestHeader("Content-Type",
										 "Application/x-www-form-url-urlencoded");
				xmlHttp.onreadystatechange = handleRequestStateChange;
				xmlHttp.send(cacheEntry);
			}
		}
		
		catch (e) {

			displayError(e.toString());
		}
	}
}

// function to handl HTTP response
function handleRequestStateChange() {
	
	if (xmlHttp.readyState == 4) {
		
		if (xmlHttp.status == 200) {
			
			try {
				
				readResponse();
			}
			catch(e) {
				
				displayError(e.toString());
				
			}
		} else {
				displayError(xmlHttp.statusText);
		}
	}
}

// function to read server response
function readResponce() {
	
	var response = xmlHttp.responseText;
	
	// Server Error?
	
	if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 || response.length ==0)
		throw(response.length == 0 ? "Server Error." : response);
		
		responseXml = xmlHttp.responseXML;
		
		xmlDoc = response.documentElement;
		result = xmlDoc.getElementsByTagName("result")[0].firstchild.data;
		fieldIF = xmlDoc.getElementsByTagName("fieldid")[0].firstchilddata;
		
		message = document.getElementById(fieldID + "Failed");
		
		message.className = (result == "0") ? "error" : "hidden";
		
		setTimeout("validate();", 500);
}

//sets focus on first field of the form
function setFocus() {
	
	// we need to place fist field in here!!!!!
	document.getElementById("xxx").focus();

}		
