/************************************************
**
**	CXmlHttpContainer class definition.
**	Developed by FunnyFox Group -FFG Co Ltd-.
**
************************************************/





/* start PROPERTIES declaration */

CXmlHttpContainer.nonIE;
	// = true if the browser is not IE, false if yes

// -- Flags for 'm_oSelfXmlHttp.readyState' property
// The object has been created but the open() method hasn't been called.
CXmlHttpContainer.READY_STATE_UNINITIALIZED = 0;

// The open() method has been called but the request hasn't been sent
CXmlHttpContainer.READY_STATE_LOADING = 1;

// The request has been sent.
CXmlHttpContainer.READY_STATE_LOADED = 2;

// A partial response has been received.
CXmlHttpContainer.READY_STATE_INTERACTIVE = 3;

// All data has been received and the connection has been closed.
CXmlHttpContainer.READY_STATE_COMPLETE = 4;
// --|

CXmlHttpContainer.prototype.m_oSelfXmlHttp;

CXmlHttpContainer.prototype.m_bAsync = true;
	// = true: enable asynchronous communication with server
	// = false: enable synchronous communication with server

CXmlHttpContainer.prototype.m_iReadyState;

CXmlHttpContainer.prototype.m_iStatus;
	// = 200: response loaded successfully

// This prop doesnt work on Opera
CXmlHttpContainer.prototype.m_sStatusText;

// Response content type
CXmlHttpContainer.prototype.m_sResponseType;

// Text returned, inside serverScript: header("Content-Type: text/plain");
CXmlHttpContainer.prototype.m_sResponseText;

// XML content returned, inside serverScript: header("Content-Type: text/xml");
CXmlHttpContainer.prototype.m_sResponseXml;

/* end PROPERTIES declaration */





/* start METHODS declaration */

function CXmlHttpContainer( ) {
	if ( typeof XMLHttpRequest != "undefined" ) {
		
		CXmlHttpContainer.nonIE = true;
        this.m_oSelfXmlHttp = new XMLHttpRequest( );
		
    } else if ( window.ActiveXObject ) {
		
		CXmlHttpContainer.nonIE = false;
		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 {
				this.m_oSelfXmlHttp = new ActiveXObject( aVersions[i] );
				break;
			} catch ( oError ) {
				//Do nothing
			}
		}
		
		//throw new Error( "XMLHttp object could be created." );	
    }
    	
}

CXmlHttpContainer.prototype.registerCallback = function( p_sEvent, p_sCallback ) {
	var aEvents = new Array( "onreadystatechange" );
	
	for ( var i = 0; i < aEvents.length; i++ ) {
		if( p_sEvent == aEvents[i] ) {
			eval( "this." + p_sEvent + "Callback( \"" + p_sCallback + "\" );" );
		}
	}
}

CXmlHttpContainer.prototype.onreadystatechangeCallback =  function( p_sCallback ) {
	var oSelf = this;
	var oSelfXmlHttp = this.m_oSelfXmlHttp;
	
	oSelfXmlHttp.onreadystatechange = function( ) {
		
		oSelf.setReadyState( oSelfXmlHttp.readyState );

		if ( oSelfXmlHttp.readyState == CXmlHttpContainer.READY_STATE_COMPLETE ) {
			oSelf.setStatus( oSelfXmlHttp.status );
			oSelf.setStatusText( oSelfXmlHttp.statusText );
			
			if ( oSelfXmlHttp.status == 200 ) {
				var sContentType = oSelfXmlHttp.getResponseHeader( "Content-Type" );
				
				if ( sContentType.indexOf( "text/xml" ) != -1 || sContentType.indexOf( "application/xml" ) != -1 ) {
					oSelf.setResponseType( "xml" );
					oSelf.setResponse( "xml", oSelfXmlHttp.responseXML );
				} 
				else if ( sContentType.indexOf( "text/plain" ) != -1 || sContentType.indexOf( "text/html" ) != -1 || sContentType.indexOf( "application/html" ) != -1 ) {
					oSelf.setResponseType( "text" );
					oSelf.setResponse( "text", oSelfXmlHttp.responseText );
					
					if( oSelfXmlHttp.responseText === 'Login expired.' ) {
						document.location.href = 'index.php';
					}
						
				}
				
			}
			
		}
		
		eval( p_sCallback + ".call( );" );
		
	};
}

CXmlHttpContainer.prototype.iOnreadystatechangeCallback =  function( p_vCallback ) {
	var oSelf = this;
	var oSelfXmlHttp = this.m_oSelfXmlHttp;
	
	oSelfXmlHttp.onreadystatechange = function( ) {
		
		oSelf.setReadyState( oSelfXmlHttp.readyState );

		if ( oSelfXmlHttp.readyState == CXmlHttpContainer.READY_STATE_COMPLETE ) {
			oSelf.setStatus( oSelfXmlHttp.status );
			oSelf.setStatusText( oSelfXmlHttp.statusText );
			
			if ( oSelfXmlHttp.status == 200 ) {
				var sContentType = oSelfXmlHttp.getResponseHeader( "Content-Type" );
				
				if ( sContentType.indexOf( "text/xml" ) != -1 || sContentType.indexOf( "application/xml" ) != -1 ) {
					oSelf.setResponseType( "xml" );
					oSelf.setResponse( "xml", oSelfXmlHttp.responseXML );
					
					p_vCallback( oSelfXmlHttp.responseXML );
				} 
				else if ( sContentType.indexOf( "text/plain" ) != -1 || sContentType.indexOf( "text/html" ) != -1 || sContentType.indexOf( "application/html" ) != -1 ) {
					oSelf.setResponseType( "text" );
					oSelf.setResponse( "text", oSelfXmlHttp.responseText );
					
					if( oSelfXmlHttp.responseText === 'Login expired.' ) {
						document.location.href = 'index.php';
					}
					else p_vCallback( oSelfXmlHttp.responseText );
				}
				
			}
			
		}
		
		//eval( p_sCallback + ".call( );" );
		
	};
}

CXmlHttpContainer.prototype.sendRequest = function( p_sMethod, p_sUrl, p_sQueryString ) {
	if( p_sMethod == "GET" ) {
		this.m_oSelfXmlHttp.open( "GET", p_sUrl + '?' + p_sQueryString, this.m_bAsync );
		this.m_oSelfXmlHttp.send( null );
	}
	else if( p_sMethod == "POST" ) {
		this.m_oSelfXmlHttp.open( "POST", p_sUrl, this.m_bAsync );
		this.m_oSelfXmlHttp.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		this.m_oSelfXmlHttp.send( p_sQueryString );
	}
}

CXmlHttpContainer.prototype.setAsync = function( p_bAsync ) {
	this.m_bAsync = p_bAsync;	
}

CXmlHttpContainer.prototype.getAsync = function( ) {
	return this.m_bAsync;
}

CXmlHttpContainer.prototype.setReadyState = function( p_iReadyState ) {
	this.m_iReadyState = p_iReadyState;	
}

CXmlHttpContainer.prototype.getReadyState = function( ) {
	return this.m_iReadyState;
}

CXmlHttpContainer.prototype.setStatus = function( p_iStatus ) {
	this.m_iStatus = p_iStatus;	
}

CXmlHttpContainer.prototype.getStatus = function( ) {
	return this.m_iStatus;
}

CXmlHttpContainer.prototype.setStatusText = function( p_sStatusText ) {
	this.m_sStatusText = p_sStatusText;	
}

CXmlHttpContainer.prototype.getStatusText = function( ) {
	return this.m_sStatusText;
}

CXmlHttpContainer.prototype.setResponseType = function( p_sResponseType ) {
	this.m_sResponseType = p_sResponseType;	
}

CXmlHttpContainer.prototype.getResponseType = function( ) {
	return this.m_sResponseType;
}

CXmlHttpContainer.prototype.setResponse = function( p_sType, p_sData ) {
	if( p_sType == "text" )	
		this.m_sResponseText = p_sData;	
	else if( p_sType == "xml" )
		this.m_sResponseXml = p_sData;	
}

CXmlHttpContainer.prototype.getResponse = function( p_sType ) {
	if( p_sType == "text" )	
		return this.m_sResponseText;
	else if( p_sType == "xml" )
		return this.m_sResponseXml;
}

CXmlHttpContainer.prototype.getXmlHttpObject = function( ) {
	return this.m_oSelfXmlHttp;	
}

// Do not use this method
CXmlHttpContainer.prototype.clear = function( ) {
	delete this.m_oSelfXmlHttp;
}

/* end METHODS declaration */