
	// Copyright (C) 2006  By MR Treebhoph Thoomsan
	// 
	// This library is free software; you can redistribute it and/or
	// modify it under the terms of the GNU Lesser General Public
	// License as published by the Free Software Foundation; either
	// version 2.1 of the License, or (at your option) any later version.
	// 
	// This library is distributed in the hope that it will be useful,
	// but WITHOUT ANY WARRANTY; without even the implied warranty of
	// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	// Lesser General Public License for more details.
	// 
	// You should have received a copy of the GNU Lesser General Public
	// License along with this library; if not, write to the Free Software
	// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

	function RemoteMethodInvoker( )
	{
		var xmlHTTP 	= null;
		var callBack	= "";
		
		this.Location		= "";
		this.RemoteMothod		= "";
		this.Parameters		= new ArrayList( );
		this.UseHttpPost		= false;
		this.ClientCallBackMethod	= "";
		this.paramPost = "";
		this.IsBrowserSupported = function( )
		{
			if( xmlHTTP != null )
				return true;
			else
				return initialXmlHTTP( );
			return false;
		}

		function initialXmlHTTP( ) 
		{ 
			if ( window.ActiveXObject ) { 	 
				try  {
  					xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");
 				} 
				catch (e)  {
					try {
   						xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP"); 
  					} 
					catch ( e )  {
   						xmlHTTP = null;
  					}
 				}	
   			} 
			else if (window.XMLHttpRequest)	
      				xmlHTTP = new XMLHttpRequest(); 
   			
			if( xmlHTTP == null )
				return false;
			return true;
		} 

		this.Invoke = function(  )
		{	
			callBack = this.ClientCallBackMethod;

			if( this.IsBrowserSupported( ) ) {
				try {	
					
					xmlHTTP.onreadystatechange = onXmlHttpStateChanged;
					
					if( ! this.UseHttpPost  ) {
						xmlHTTP.open( "POST", this.Location, true ); 	

						xmlHTTP.send(  createHttpRequestMessage( this.RemoteMethod, this.Parameters.Join( "," ), this.paramPost )  );
						
					}
					else {
						var queryString = "";

						if( this.RemoteMethod != "" )
							queryString = "?__RemoteMethod__=" + this.RemoteMethod + "&__ParameterList__="+ this.Parameters.Join( "," );
				
						xmlHTTP.open( "GET", this.Location+ queryString, true ); 	
						xmlHTTP.send(  null  );
					}
				}
				catch( e ) {
					alert( e );
					xmlHTTP.abort( );
				}

				this.Parameters.Clear();
			}
		}


		function onXmlHttpStateChanged(  )
		{
			if (xmlHTTP.readyState == 1) {
			   var resultarea= document.getElementById('result');
			  // resultarea.innerHTML = "";
			}else if (xmlHTTP.readyState == 2) {
			   var resultarea= document.getElementById('result');
			  // resultarea.innerHTML = "";
			}else if (xmlHTTP.readyState == 3) {
			   var resultarea= document.getElementById('result');
			  // resultarea.innerHTML = "";
			}else if( xmlHTTP.readyState == 4 ) {		
				var resp  = new ResponseMessage( );
				
				if( xmlHTTP.status == 200 ) {
					resp.ReturnValue 		= xmlHTTP.responseText;
					resp.HttpStatusCode		= xmlHTTP.status;
					resp.HttpResponseHeader	= xmlHTTP.getAllResponseHeaders();
					invokeClientFunction( callBack, resp );
				}
				else {
					resp.HttpStatusCode		= xmlHTTP.status;
					resp.HttpResponseHeader	= xmlHTTP.getAllResponseHeaders();
					invokeClientFunction(  callBack, resp );
				}
			} 
		}

		function createHttpRequestMessage( remoteMethod, parameterList, paramP)
		{
			var httpBody = "__RemoteMethod__=" + escape( remoteMethod )+"&__ParameterList__="+escape( parameterList )+"&"+paramP;

			xmlHTTP.setRequestHeader( "connection", "close" );
			xmlHTTP.setRequestHeader( "content-length",  httpBody.length );
			xmlHTTP.setRequestHeader( "content-type", "application/x-www-form-urlencoded" );

			return httpBody;
		}

		function invokeClientFunction( callBackFunction, resp )
		{	
			xmlHTTP.abort( );

			if( callBackFunction != "" )
				eval( (callBackFunction + "(  resp  );").toString() );
		}

		function ResponseMessage( )
		{
			this.ReturnValue		= null;
			this.HttpStatusCode		= null;
			this.HttpResponseHeader	= null;
		}

		function ArrayList( )
		{
			var arrParams = new Array( );
			this.Count 	= 0;
	
			this.Add = function( value ) 
			{
				arrParams.push( value );
				this.Count = arrParams.length;
				return this.Count - 1;
			}

			this.RemoveAt = function( index  )
			{
				arrParams.splice( index, 1 );
				this.Count = arrParams.length;
				return this.Count - 1;
			}

			this.Clear = function( )
			{
				while( arrParams.length > 0 )
					arrParams.pop( );
				this.Count = 0;
			}
	
			this.Join = function( delimiter )
			{
				return arrParams.join( delimiter );
			}
		}
	}