<!--
/*******************************************************************************
*** Name: 				httpCommunication
*** Created By:			Eric Keyes
*** Created On:			October 26, 2011
*** Last Modified On:	November 05, 2011
***
*** Description:
***	This javascript object allows behind the sceens two-way communication
***	with a specific web page.
***
*** Parameters:
***	sUrl: The target url to send/request data to.
***
*** Object Variables:
***	url: The target url (See parameters)
***	obj: The control that will receive and return data and where some
***			functions will get there data from.
***	arr: An array of controls where some functions will get their data from.
***	params: Extra Parameters to send with the data/array.
***	empty: Empty the obj or arr after the data has been sent. (Will uncheck
***			obj or arr if it is a checkbox instead of empting it.)
***			Values:
***				True = Empty Data
***				False = Do Not Empty Data
***	onlySendIfChecked: If the control is a checkbox, only send it if it is checked.
***	timerID: If this object will get data on a timer, this is the ID
***
***	xmlhttp: The communication object to the timer object.
***
*******************************************************************************/

function httpCommunication(sUrl)
{
	this.url = sUrl;
	this.obj = null;
	this.arr = [];
	this.params = "";
	this.empty = false;
	this.onlySendIfChecked = false;	
	this.timerID = null;
	
	this.xmlhttp = this.getXMLHttpRequest();
	this.xmlhttp.obj = null;
}

/*******************************************************************************
*** Public Functions
*******************************************************************************/
// Sets the active control to a hidden iFrame
httpCommunication.prototype.sendReturnToFrame = function()
{
	this.obj = this.createCommunicationFrame();
	this.xmlhttp.obj = this.obj;
}

// Sets the active control by it's Element ID
httpCommunication.prototype.setElementById = function(sName, bEmpty)
{
	if (sName.length == 0) return;
	
	this.obj = document.getElementById(sName);
	this.xmlhttp.obj = this.obj;
	this.empty = bEmpty;
}

// Sets the active control by it's Element Name
httpCommunication.prototype.setElementByName = function(sName, bEmpty)
{
	if (sName.length == 0) return;
	
	this.obj = document.getElementsByName(sName);
	this.xmlhttp.obj = this.obj;
	this.empty = bEmpty;
}

// Sets the active control array by it's Element Name
httpCommunication.prototype.setArray = function(sName, bEmpty)
{
	if (sName.length == 0) return;

	this.arr = document.getElementsByName(sName);
	this.empty = bEmpty;		
}

// Gets the data from the supplied URL and puts it in the
// active control.
httpCommunication.prototype.get = function()
{
	if ((this.obj == null) || (this.url.length == 0)) return;

	this.xmlhttp.onreadystatechange = function()
	{
		if ((this.readyState == 4) && (this.status == 200))
		{
			if (this.obj.tagName == "IFRAME")
				this.obj.src = this.responseText;
			else if (this.obj.value != null)
				this.obj.value = this.responseText;
			else if (this.obj.innerHTML != null)
				this.obj.innerHTML = this.responseText;
		}
	}
	this.xmlhttp.open("GET", this.url, true);
	this.xmlhttp.send();
}

httpCommunication.prototype.getDownload = function(sUrl)
{
	if (this.obj.tagName != "IFRAME") return;
	this.obj.src = sUrl;
}

// Sends the current URL without any parameters.
httpCommunication.prototype.sendUrl = function(sUrl)
{
	if (sUrl.length == 0) return;

	xmlhttp.open("GET", sUrl, true);
	xmlhttp.send();
}

// Sends the current control's value as a parameter.  If current
// control is a checkbox and variable onlySendIfChecked = true and the control
// is not check then it is skipped, also it unchecks it if variable empty = true.
httpCommunication.prototype.sendElement = function ()
{
	if (this.obj == null) return;
	if ((this.onlySendIfChecked == true) && (this.obj.checked != true)) return;
	if (this.obj.value.length == 0) return;

	var arr = new Array();
	arr[this.obj.name] = this.obj;
	this.send(arr);
	if (this.empty == true)
	{
		if (this.obj.type == "checkbox") this.obj.checked = false;
		else this.obj.value = "";
	}
}

// Sends arr of controls.
httpCommunication.prototype.sendArray = function()
{	
	this.send(this.arr);
}

// Sends and array of values as a parameter.  If array contains checkbox elements
// and variable onlySendIfChecked = true and the control is not check then it is skipped,
// also it unchecks it if variable empty = true.
httpCommunication.prototype.send = function(aData)
{
	if ((aData.length == 0) || (this.url.length == 0)) return;
	var params = "";
	for (var key in aData)
	{
		if (!(this.onlySendIfChecked == true) && (aData[key].checked != true)) continue;
		
		if (params.length != 0) params += "&";
		params += aData[key].name + "=" + aData[key].value;
		if (this.empty == true)
		{
			if (aData[key].type == "checkbox") aData[key].checked = false;
			else aData[key].value = "";
		}
	}

	if (params.length == 0) return;
	if (this.params.length != 0)
	{
		if(params.length != 0) params += "&";
		params += this.params;
	}

	this.xmlhttp.open("POST", this.url, true);
	this.xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	this.xmlhttp.setRequestHeader("Content-length", params.length);
	this.xmlhttp.setRequestHeader("Connection", "close");
	
	this.xmlhttp.onreadystatechange = function()
	{
		if(this.readyState == 4 && this.status == 200)
		{
			if (this.obj.tagName == "IFRAME")
				this.obj.src = this.responseText;
			else if (this.obj.value != null)
				this.obj.value = this.responseText;
			else if (this.obj.innerHTML != null)
				this.obj.innerHTML = this.responseText;
		}
	}
	this.xmlhttp.send(params);
}

// If a timer has been started, stop it.
httpCommunication.prototype.stopTimer = function()
{
	if (this.timerID == null) return;

	clearTimeout(this.timerID);
	this.timerID = null;
}

// Get the current data and start a timer to get
// the data again is iTime seconds.
httpCommunication.prototype.getDataOnTimer = function (iTime)
{
	if (iTime.length == 0) return;
	
	this.get();
	
	if (this.timerID != null) this.stopTimer();

	var sCmd = "this.getDataOnTimmer("+ iTime +")";
	iTime *= 1000; // Convert seconds to milliseconds
	this.timerID = setTimeout(sCmd, iTime);
}

/*******************************************************************************
*** Private Functions
*******************************************************************************/
// Gets the current browser's way of handling XMLHttp Requests.
httpCommunication.prototype.getXMLHttpRequest = function()
{
	// code for IE7+, Firefox, Chrome, Opera, Safari
	if (window.XMLHttpRequest) return new XMLHttpRequest();
	// code for IE6, IE5
	else return new ActiveXObject("Microsoft.XMLHTTP");
};

httpCommunication.prototype.createCommunicationFrame = function()
{
	var tmp = document.getElementById("commframe")
	if (tmp == null)
	{
		tmp = document.createElement("iframe");
		tmp.id = "commframe";
		tmp.name = "commframe";
		tmp.style.display = "none";
		tmp.style.position = "fixed";
		tmp.style.top = 0;
		tmp.style.left = 0;
		tmp.style.width = 1;
		tmp.style.height = 1;
		document.body.appendChild(tmp);
	}
	return tmp;
}
-->
