//Xml Response kj.c
var ROOT = "root";
var ELEMENT = "element";
var AJAX_CORE_HANDLER_PATH = null;
//var AJAX_CORE_HANDLER_PATH = "Core/Framework/handler/AjaxTest.aspx";
//var coreUrl = g_baseUrl + AJAX_CORE_HANDLER_PATH;
var coreUrl;

///////////////////////////////////////////////////////////////
 ////  Ajax Classes Using Function Names       ////////////////
 //////////////////////////////////////////////////////////////
function getCoreUrl()
{
    return  g_baseHandler;
}
function AjaxXmlGet( serverFunction , newCoreUrl)
{
    coreUrl = getCoreUrl();
    
    this.ajax = new AjaxCore( "XML" );
    this.url = newCoreUrl ? newCoreUrl : coreUrl;
    this.url += "?" + "sID" +  "=" + new Date().getTime();    
    this.addArgument( "functionName", serverFunction)
}

AjaxXmlGet.prototype.addArgument = function( name, value )
{
    //    kj.debug.alert( "Adding argument: Name= " + name + ", Value=" +value);
    if (value)
    {
	value +="";
	value = value.replace(/&/gi,kj.c.AJAX_FOUND_AMP_REPLACE );
    }
    this.url += "&" + name + "=" + value;    
}

AjaxXmlGet.prototype.addParam = AjaxXmlGet.prototype.addArgument;

AjaxXmlGet.prototype.addParams = function( params )
{
    for ( var id in params )
    {
	if (typeof params[id] != 'function') //sanity check
	    this.addParam(id,params[id]);
    }
}


AjaxXmlGet.prototype.send = function( callbackFunction )
{
    if (!callbackFunction )
	callbackFunction = function() {};

    var encodedUri = encodeURI(this.url);
    
    var verifyLogIn = function( responseXML )
    {
      callbackFunction ( responseXML );
    }
    
    this.ajax.startGetRequest( encodedUri, verifyLogIn );          
}



function AjaxXmlPost( serverFunction , newCoreUrl)
{
    coreUrl = getCoreUrl();
    
    this.ajax = new AjaxCore( "XML" );
    this.url = newCoreUrl ? newCoreUrl : coreUrl;
    //    alert(coreUrl );
    this.url += "?" + "sID" +  "=" + new Date().getTime();    

    this.paramString = "functionName" + "=" + serverFunction;
}

AjaxXmlPost.prototype.addArgument = function( name, value )
{
    //    kj.debug.alert( "Adding argument: Name= " + name + ", Value=" +value);
    //    kj.debug.alert( typeof value );
    
    if (value)
    {
	value += "";
	value = value.replace(/&/gi,kj.c.AJAX_FOUND_AMP_REPLACE );	
    }
    this.paramString += "&" + name + "=" + value;    
}

AjaxXmlPost.prototype.addParam = AjaxXmlPost.prototype.addArgument;

AjaxXmlPost.prototype.addParams = function( params )
{
    for ( var id in params )
    {
	if (typeof params[id] != 'function') //sanity check
	    this.addParam(id,params[id]);
    }
}


AjaxXmlPost.prototype.send = function( callbackFunction )
{
    var self = this;
    if (!callbackFunction )
	callbackFunction = function( responseXML ) {};


    var enhancedCallback = function( responseXML )
    {
	callbackFunction( responseXML );
	this.visualResponse( responseXML, succMsg, errorMsg );
    }
    
    var verifyLogIn = function( responseXML )
    {
      callbackFunction ( responseXML );
    }
    
    this.ajax.startPostRequest( this.url, this.paramString, verifyLogIn );          
}

AjaxXmlPost.prototype.sendWithVisual = function( callbackFunction, succMsg, errorMsg )
{
    if (!callbackFunction )
	callbackFunction = function( responseXML ) {};

    var self = this;
    var enhancedCallback = function( responseXML )
    {
	callbackFunction( responseXML );
	self.visualResponse( responseXML, succMsg, errorMsg );
    }
    
    this.send( enhancedCallback );
}


AjaxXmlPost.prototype.visualResponse = function( responseXml, succMsg, errorMsg )
{
    if ( Tools.isFailureResponse( responseXml ))
    {
	if (errorMsg)
	{
	    TopMessenger.sendError( errorMsg );
	    //            kj.debug.alert( "FailMessage Sent to messenger is:" + failMessage );
	}
    }
    else
    {
	if ( succMsg )
	{
	    TopMessenger.sendComment( succMsg );
	    //            kj.debug.alert( "SuccMessage Sent to messenger is:" + succMessage );	
	}
    }        
}


///////////////////////////////////////////////////////////////
/////  Normal Ajax Class   ////////////////////////////////////
///////////////////////////////////////////////////////////////

function Ajax( handlerUrl, responseType )
{
    if(!handlerUrl)
    { handlerUrl = getCoreUrl(); }
    
    if (!responseType)
    { responseType = "text"; }
	
    this.ajax = new AjaxCore( responseType );
    this.url = handlerUrl;
    this.url += "?" + "sID" +  "=" + new Date().getTime();    
    this.paramString = "";
}


Ajax.prototype.addParam = function( name, value )
{
    this.paramString += "&" + name + "=" + value;    
}

Ajax.prototype.addParams = function( params )
{
    for ( var id in params )
    {
	if (typeof params[id] != 'function') //sanity check
	    this.addParam(id,params[id]);
    }
}

Ajax.prototype.get = function( callbackFunction )
{
    if ( !callbackFunction )
	callbackFunction = function( responseText ) {};

    this.url += this.paramString;
    var encodedUri = encodeURI(this.url);
    this.ajax.startGetRequest( encodedUri, callbackFunction );          
}

Ajax.prototype.post = function( callbackFunction )
{
    if (!callbackFunction )
	callbackFunction = function( responseText ) {};

    this.paramString = this.paramString.substring(1); //remove extra & in beginning
    this.ajax.startPostRequest( this.url, this.paramString, callbackFunction );          
}

Ajax.prototype.postWithVisual = function( callbackFunction, succMsg, errorMsg )
{    
    if (!callbackFunction )
	callbackFunction = function( responseText ) {};

    var self = this;
    var enhancedCallback = function( responseText )
    {
	callbackFunction( responseText );
	self.visualResponse( responseText, succMsg, errorMsg );
    }
    this.post( enhancedCallback );
}


Ajax.prototype.visualResponse = function( responseText, succMsg, errorMsg )
{
    if ( Tools.isFailureTextResponse( responseText ))
    {
	if (errorMsg)
	{
	    TopMessenger.sendError( errorMsg );
	    //            kj.debug.alert( "FailMessage Sent to messenger is:" + failMessage );
	}
    }
    else
    {
	if ( succMsg )
	{
	    TopMessenger.sendComment( succMsg );
	    //            kj.debug.alert( "SuccMessage Sent to messenger is:" + succMessage );	
	}
    }        
}















///////////////////////////////////////////////////////////////
 //// Thee Ajax Core module javascript section ////////////////
 //////////////////////////////////////////////////////////////


function AjaxCore( responseType )
{
    this.responseType = responseType;
    this.createXMLHttpRequest();
}

AjaxCore.prototype.createXMLHttpRequest = function () 
{
    if (window.ActiveXObject) 
    {
	this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) 
    {
	this.xmlHttp = new XMLHttpRequest();
    }
}


AjaxCore.prototype.startGetRequest = function ( url, callbackFunction ) 
{
    //    myNote("Reached Starting of Request with url = " + url);
    var self = this;
    if (!callbackFunction )
	callbackFunction = function() {};
    this.callbackFunction = callbackFunction;
    this.xmlHttp.onreadystatechange = function() { self.handleStateChange( self );} ;
    this.xmlHttp.open("GET", url , true);
    this.xmlHttp.send(null);
}


AjaxCore.prototype.startPostRequest = function ( url, paramString, callbackFunction ) 
{
    //    myNote("Reached Starting of Request with url = " + url);
    var self = this;
    if (!callbackFunction )
	callbackFunction = function() {};
    this.callbackFunction = callbackFunction;
    this.xmlHttp.onreadystatechange = function() { self.handleStateChange( self );} ;
    this.xmlHttp.open("POST", url , true);
    this.xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
    this.xmlHttp.send(paramString);
}

AjaxCore.prototype.handleStateChange = function( self ) 
{
    //    kj.debug.alert("Reached Starting handleState");
    //    kj.debug.alert( self.xmlHttp.readyState );
    if(self.xmlHttp.readyState == 4)
    { 
	if(self.xmlHttp.status == 200) 
	{
	    //	    myNote( "Status OK" );
	    
	    if (self.responseType == "XML")
	    {
		//		myNote("Recieved Ajax Response in XML mode, Status OK.");
		self.callbackFunction( self.xmlHttp.responseXML );

		return;
	    }
	    else
	    {
		//		kj.debug.alert( self.xmlHttp.responseText);		
		//		myNote("Recieved Ajax Response in Text mode");
		self.callbackFunction( self.xmlHttp.responseText);
	    }
	}
	else
	{ 
	    if ( (self.xmlHttp.status == 404))
		kj.debug.error( "Ajax Request Destination Not found!" );
		//;
	    //		
	    else
		kj.debug.error( "Ajax Request Failure with following status: " + self.xmlHttp.statusText );	  	    
		//;
	    self.callbackFunction( false );
	}
    }	    
}


