/*************************************************************************************
NOTICE: This software is authored and owned soley by Common Link, LLC 
Any use of this software, outside of Common Link staff, without 
express written autorization of Common Link, LLC, is strictly forbidden.
*************************************************************************************/
function FormHandler()
{
	var localObj = this;

	FormHandler.prototype.getXmlHttpObject = function() {
		var xmlHttp=null;

		try {
			// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		}

		catch (e) {
			// Internet Explorer
			try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}

			catch (e) {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
		}

		return xmlHttp;
	}

	FormHandler.prototype.handleFormPost = function (post_to_file_name, varList, response_type, response_layer) {
		var getdate = new Date();  //Used to prevent caching during ajax call

		localObj.xmlHttp = localObj.getXmlHttpObject();

		// response_type determine how to present the results:
		// Either an innerHTML text response (html) or a javascript response (javascript).
		localObj.responseType = response_type;
		localObj.layerName = response_layer;

		if (localObj.xmlHttp) {
			localObj.xmlHttp.onreadystatechange = localObj.handleServerResponse;      // ***Assign the function pointer to the onreadystatechange property, NOT the return value (with"()")***.
			localObj.xmlHttp.open('POST', post_to_file_name, true);  // Call the script to process the form using the POST method.
			localObj.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			localObj.xmlHttp.setRequestHeader("Content-length", varList.length);
			localObj.xmlHttp.setRequestHeader("Connection", "close");
			localObj.xmlHttp.send(varList); //Posting to the PHP File
		}
	}

	FormHandler.prototype.handleUrlGet = function (url, response_type, response_layer) {
		localObj.responseType = response_type;
		localObj.xmlHttp = localObj.getXmlHttpObject();
		localObj.layerName = response_layer;

		if (localObj.xmlHttp) {
			localObj.xmlHttp.onreadystatechange = localObj.handleServerResponse;      // ***Assign the function pointer to the onreadystatechange property.
			localObj.xmlHttp.open('GET', url, true);
			localObj.xmlHttp.setRequestHeader("Connection", "close");
			localObj.xmlHttp.send(null);
		}
	}

	FormHandler.prototype.handleServerResponse = function() {
		if (localObj.xmlHttp.readyState == 4) {
			if(localObj.xmlHttp.status == 200) {
				if (localObj.responseType == "javascript") {
					var response = localObj.xmlHttp.responseText;
					var headId = document.getElementById(localObj.layerName);
					var oScript = document.createElement( 'script' );
					oScript.type = "text/javascript";
					oScript.text = response;
					headId.appendChild( oScript );
				} else {
					document.getElementById(localObj.layerName).innerHTML = localObj.xmlHttp.responseText;
				}
			} else {
				alert("Unable to retrieve data. Please try again");
				return;
			}
		}
	}
}