//This utility file contains some usefull methos that can be used to implement ajax

//this method creates XMLHttp Object that support various browser
function createXMLHttp()
{
	if (typeof XMLHttpRequest != "undefined")
	{
		return new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		var aVersions = [ "MSXML2.XMLHttp.5.0",
						"MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
						"MSXML2.XMLHttp","Microsoft.XMLHttp"
						];
		for (var i = 0; i < aVersions.length; i++)
		{
			try
			{
				var oXmlHttp = new ActiveXObject(aVersions[i]);
				return oXmlHttp;
			}
			catch (oError)
 			{
				//Do nothing
			}
		}
	}
	throw new Error("XMLHttp object could be created.");
}

// Following code is written for adding support for firefox.

	var IE = document.all?true:false;
	 
	if(window.Event && document.captureEvents)
	{
		document.captureEvents(Event.CLICK);
		document.onclick = getMousePos;
	}

	// Finding the position of mouse on mouse click in firefox
	function getMousePos(e)
	{
		// grab the x-y pos.s if browser is NS 
		posx = e.pageX; 
		posy = e.pageY; 
	}
	
	// This method is used for positioning div
	
	function positioningDivRelativeToMouse(e,id,relPosX,relPosY) 
	{
		if (IE)
		{
			if (!e) var e = window.event;
			if (e.pageX || e.pageY) 	{
				posx = e.pageX;
				posy = e.pageY;
			}
			else if (e.clientX || e.clientY) 	{
				posx = e.clientX + document.body.scrollLeft
					+ document.documentElement.scrollLeft;
				posy = e.clientY + document.body.scrollTop
					+ document.documentElement.scrollTop;
			}
		}	
		// positioning div on the desired position.
		document.getElementById(id).style.top = posy + relPosY + 'px';
		document.getElementById(id).style.left = posx  + relPosX  + 'px';
	}
	
	function getResponseText(url)
	{
	
		var xmlHttp = createXMLHttp();
		var stringContainer = "";
		xmlHttp.open("POST", url, false);
		
		xmlHttp.onreadystatechange = function () {
			if (xmlHttp.readyState == 4) {
				if(xmlHttp.status == 200){
					stringContainer = xmlHttp.responseText;
					resetTimeOut();
				}else{
					alert("An error has occurred: " + xmlHttp.status);
				}
			}
		}
		xmlHttp.send(null);
		return stringContainer;		
	}
	
	function resetTimeOut()
	{
		if(document.getElementById('sessionTimeOutId'))
		{
			var timeOutId = document.getElementById('sessionTimeOutId').value;
			clearTimeout(timeOutId);
			if(document.getElementById('timeOutDifference').value == "")
			{
				timeOutId = setTimeout("warnSessionExpiration()", 29*60*1000);
			}
			else
			{
				timeOutId = setTimeout("warnSessionExpiration()", document.getElementById('timeOutDifference').value);
			}
			document.getElementById('sessionTimeOutId').value = timeOutId;
		}
	}
