/** File Description:
	- This file creates a ajax request and process the result text or response as per the request
	- For the after processing it uses the xml.js file for xmlDom parsing
*/ 
//Ajax Initialiser
function KoolAjax()
{
	this.RequestURL	= '';
	this.OnReady	= '';
	this.Container	= '';
	this.Result		= '';
	this.ParseContent=false;
}
KoolAjax.prototype.getXMLHTTP = function ()
{
	//return new ActiveXObject("Msxml2.XMLHTTP");
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
		try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } 

catch (E) { xmlhttp=false; } }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!="undefined") xmlhttp=new XMLHttpRequest();
	return xmlhttp;
}
KoolAjax.prototype.UpdateContainer = function ()
{
	// remove all children from element
	this.Container.innerHTML = this.Result;
}

KoolAjax.prototype.SetContainer = function ( eleName){
	if( typeof eleName == "object" ){
		this.Container = eleName; return true;}		
	
	var tmpTargetEle = document.getElementById( eleName );
	if( tmpTargetEle == null || tmpTargetEle == undefined ){
		alert( 'Set Container called. Invalid Target Object Defination.' + eleName );return false;}
	this.Container = tmpTargetEle;
	tmpTargetEle.innerHTML = '<img src="images/loading.gif" />Loading please wait...'; 
	return true;
}
KoolAjax.prototype.Init = function ()
{
	//Expected Parameters:
	//A. muRUL: Name of Page with the Get Variables attached after ?
	//B. OnReadyAction: Function Name which should be called for handling the Result. For displaying or changing the Content of container use UpdateContainer(ContainerName,Result)
	var that=this;
	var xmlObj = new this.getXMLHTTP();
	if(this.RequestURL== '')
		return false;

	xmlObj.open("GET",this.RequestURL);
    xmlObj.onreadystatechange=function() 
	{
		if (xmlObj.readyState==4&&xmlObj.status==200) 
		{
			if(xmlObj.responseText != "")
			{
				that.Result=xmlObj.responseText;
				if(that.Container !== ''){
					that.UpdateContainer.call(that);
				}else{
					that.OnReady.call(that);
				}
			}
		}
	}
	xmlObj.send(null);
}

//Local Function For Handling the Request
function RequestKoolAjax(URL,Container,Handler,P_Content)
{
	var myObj=new KoolAjax();
	
	if(Container != "")
		myObj.SetContainer(Container);
	
	myObj.RequestURL=URL;
	myObj.OnReady=Handler;
	myObj.ParseContent=P_Content;
	myObj.Init();
}