﻿function PhAjaxRequest( url, params, post_data, callback )
{
	this.Url		= url;
	this.Params		= params;
	this.Callback	= callback;
	this.PostData	= post_data;	
}

function PhAjaxJson()
{
	this.m_oXmlHttp		= null;
	this.m_fpCallback	= null;
	
	this.m_oRequests	= new Array();
	this.m_bReady		= true;
	
	this.m_strUrl		= '';
	this.m_strParams	= '';
	this.m_strPostData	= '';		
	
	this.InitObject();
}

PhAjaxJson.prototype.InitObject = function()
{
	if( g_oClient.GetBrowser() == PhClient.BROWSER_UNDEFINED )
		return;
		
	var me = this;
		
	try
	{
		if( g_oClient.GetBrowser() == PhClient.BROWSER_IE )
			this.m_oXmlHttp = new ActiveXObject( (g_oClient.GetVersion() == 5)? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP' );
		else
			this.m_oXmlHttp = new XMLHttpRequest();
	}
	catch( e )
	{
		alert( e );
		alert( 'Failed to create the Xml Http request object.  If you are using IE, plese verify that active scripting and ActiveX controls are enabled.' );
	}
}

PhAjaxJson.prototype.BuildPostData = function( data_source )
{
	var post_string = '';
	var i			= 0;
	var name		= '';
	var grouped		= false;
	
	if( typeof(data_source) != 'string' )
	{
	    for( i = 0; i < data_source.elements.length; i++ )
	    {
		    grouped = (data_source.elements[i].type == 'checkbox' || data_source.elements[i].type == 'radio');
			
		    if( data_source.elements[i].name != '' )
		    {					
			    name		 = data_source.elements[i].name;
			    post_string += (post_string == '')? '' : '&'
				
			    if( data_source.elements[i].name == '__VIEWSTATE' || data_source.elements[i].name == '__EVENTVALIDATION' )
				    post_string += name+'='+data_source.elements[i].value;
			    else
				    if( (grouped && data_source.elements[i].checked) || !grouped )
					    post_string += name+'='+escape( data_source.elements[i].value );
					    
				if( typeof(data_source.elements[i].UserValue) != 'undefined' )
					post_string += ','+data_source.elements[i].UserValue;
		    }
	    }
	   
		return post_string;
	}
	else
		return data_source;   
}

PhAjaxJson.prototype.SendRequest = function( url, params, post_data_source, callback )
{
	var post_data = (post_data_source != null)? this.BuildPostData( post_data_source ) : '';

	this.m_oRequests.unshift( new PhAjaxRequest( url, params, post_data, callback ) );
	
	if( this.m_bReady )
		this.MakeAjaxCall( this.m_oRequests.shift() );
}

PhAjaxJson.prototype.MakeAjaxCall = function( request )
{
	var me				= this;
	
	this.m_bReady		= false;
	this.m_strUrl		= request.Url;
	this.m_strParams	= request.Params;
	this.m_strPostData	= request.PostData;
	this.m_fpCallback	= request.Callback;
	
	if( this.m_oXmlHttp != null )
	{

		this.m_oXmlHttp.open( (this.m_strPostData == '')? 'GET' : 'POST', request.Url+'?'+request.Params, true );
		
		if( g_oClient.GetBrowser() != PhClient.BROWSER_IE )
		{			
			this.m_oXmlHttp.onload	= function(){me.HandleResponse();};
			this.m_oXmlHttp.onerror	= function( e ){alert( e );};		
		}
		else
			this.m_oXmlHttp.onreadystatechange = function(){me.HandleResponse();};
						
		if( this.m_strPostData != '' )
			this.m_oXmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
									
		this.m_oXmlHttp.send( this.m_strPostData );
		this.m_strPostData = '';
	}
}

PhAjaxJson.prototype.HandleResponse = function()
{	
	if( this.m_oXmlHttp.readyState == 4 || this.m_oXmlHttp.readyState == 'complete' )
	{
		if( this.m_fpCallback != null )
			this.m_fpCallback( eval( '('+this.m_oXmlHttp.responseText+')' ) );
			
		if( this.m_oRequests.length > 0 )
			this.MakeAjaxCall( this.m_oRequests.pop() );
		else
			this.m_bReady = true;			
	}
}