


function AjaxClient() {

	this.response = '';
	
	this.error = '';

	this.onSuccess = 	function () {
							alert( 'Please override onSuccess callback.' );
						};
	this.onLoading = 	function () {
							alert( 'Please override onLoading callback.' );
						};
	this.onError = 		function ( err) {
							alert( err );
						};
	this.call = 		function( uri, method, serverClass, serverMethod, params ) {
							
							var args = '';
							var selfReference = this;
							
							if( method.toUpperCase() == 'POST'){
								args 	+= '__serverClass=' + serverClass + '&__serverMethod=' + serverMethod;
 
								this.http.open( 'POST',  uri );
								this.http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=iso-8859-2');
								this.http.onreadystatechange = function(){AJAX_stateHandler(selfReference );}
								
								for ( property in params ) {
								    var sValue = '' + params[ property ];
								    sValue = escape( sValue.replace( /%/g, "%25" ) );
								    args += '&' + property + '=' + sValue;
								}									
									
								this.http.send(args);
							}else{
								
								uri 	+= '?__serverClass=' + serverClass + '&__serverMethod=' + serverMethod;
							
								for ( property in params ) {
								    var sValue = '' + params[ property ];
								    sValue = escape( sValue.replace( /%/g, "%25" ) );
									uri += '&' + property + '=' + sValue;
								}
								this.http.open( 'get', uri );
								this.http.setRequestHeader('Content-Type', 'text/html; charset=iso-8859-2');

								this.http.onreadystatechange = function(){AJAX_stateHandler(selfReference );}
								this.http.send( null );
								
							}
							
							
							/*
							uri += '?__serverClass=' + serverClass + '&__serverMethod=' + serverMethod;
							for ( property in params ) {
								uri += '&' + property + '=' + params[ property ];
							}
									var selfReference = this;
							this.http.open( method, uri );
							this.http.onreadystatechange = function(){AJAX_stateHandler(selfReference );}
							this.http.send( null );
							*/

							/* handling for POST method
							this.http.abort;
							this.http.open('post',  'back_end.php');
							this.http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
							this.http.send('arg1=val1&arg2=val2&arg3=val3');
							*/							
						};
	
	this.createRequestObject = 	function () {
									var request_o; //declare the variable to hold the object.
									var browser = navigator.appName; //find the browser name
									if( browser == "Microsoft Internet Explorer" ){
										/* Create the object using MSIE's method */
										request_o = new ActiveXObject( "Microsoft.XMLHTTP" );
									}else{
										/* Create the object using other browser's method */
										request_o = new XMLHttpRequest();
									}
									return request_o; //return the object
								};

	this.http = this.createRequestObject(); 						

}

function AJAX_stateHandler(client) {

	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	//debugToDiv( client.http.responseText, 'divId', 0 );

	if( client.http.readyState == 4 ){ 
		var response = '';
		
		//debugToDiv(client.http.responseText,'print',0);
		if(client.http.responseText!= '')
			eval("response = " + client.http.responseText);
		else 
			return ; 
			
		if( client.http.status != 200 ) {
			client.onError( 'HTTP Error, received status ' + client.http.status + ' instead of expected 200' );
		} else if( response[ '__errorString' ] != null ) {
			client.onError( response[ '__errorString' ] );
		} else {
			client.onSuccess( response );
		}
	} else if( client.http.readyState == 1 ) {
		client.onLoading();
	}
}

function debugToDiv ( oToDebug, sDivId, iIndent ) {
	document.getElementById( sDivId ).innerHTML += '<pre>' ; 
	for ( mProperty in oToDebug ) {
		var oMember = oToDebug[ mProperty ];
		var j = 0;
	
		for( j=0; j<iIndent; j++ ) {
			document.getElementById( sDivId ).innerHTML += '	';
		}
		if( typeof( oMember ) == 'object' ) {
			document.getElementById( sDivId ).innerHTML += mProperty + ' (Object) <br />';
			debugToDiv( oMember, sDivId, iIndent + 1 );
		} else {
			//document.getElementById( sDivId ).innerHTML += oMember ;
			//
			document.getElementById( sDivId ).innerHTML +=( mProperty + ' => ' + oMember + '<br />' );
		}
	}
	document.getElementById( sDivId ).innerHTML += '</pre>' ; 
}


