	/******************************************************************************************************
	 *					 			HTTPService class and prototypes 		
	 *										  
	 * Created on May 12, 2007
	 *
	 * @author	Rayner Chu
	 * @company	WebMeridian Technologies
	 * @contact	www.webmeridian.com
	 * @copyright	2007 All Rights Reserved. Company confidential.
	 *
	 * Notes:
	 *
	 ******************************************************************************************************/

	 
	/**
	  * Obfuscation: Do NOT obfuscate these terms. JS will execute with erros!
	  *
	  *			HTTPService (public class)
	  *			serverAction (public method)
	  *
	  */
	

	HTTPService = function() { this.initAjax.call(this); }
	
	// maximum number of times a same request is sent (when request fails)
	HTTPService.MAX_TRIAL = 3;
	HTTPService.DEFAULT_TIMEOUT = 3; // in seconds
	
	HTTPService.prototype.initAjax = function()
	{
		try
		{
			// Firefox, Opera 8.0+, Safari
			this.xmlHttp = new XMLHttpRequest();
		}
		catch (e)
		{
			// Internet Explorer
			try
		    {
		      this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		    }
		    catch (e)
		    {
			    try
			    {
					this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			    }
			    catch (e)
			    {
					alert("Your browser does not support AJAX!\nPlease download the latest version.");
			        this.xmlHttp = null;
			    }
			}
		}
	}


	HTTPService.prototype.serverAction = function( method, url, asynch, xml, callBackFunc, opt )
	{
		// reset trial for resending HTTP request if fails
		this.numTrial = 1;
		
		this.method = (method && method.length > 0) ? method : "POST";
		this.asynch = (asynch && asynch.length > 0) ? asynch : true;
		this.xml = (xml && xml.length > 0) ? xml : "";
		this.callBackFunc = callBackFunc;
		this.opt = opt;
		this.url = url;
		
		if (this.url && this.xml.length > 0) {
			this.ajaxRequest( this.method, this.url, this.asynch, this.xml, this.callBackFunc, this.opt );
			return true;
		} else {
			alert("Oops! httpService.js::serverAction() error. No AJAX controller url or xml is provided.");
			return false;
		}
	}
	
	
	HTTPService.prototype.ajaxRequest = function( method, url, asynch, xml, callBackFunc, opt )
	{
		var that = this;
		
		// do not place the 'open' function call after 'onreadystatechange'. This is to reuse the same XMLHttpRequest while
		// the first request fails and it will cancel the first request
		this.xmlHttp.open( method, url, asynch );
		
		this.xmlHttp.onreadystatechange=function()
	 	{
			that.onreadystatechange.call( that, callBackFunc, opt );
		} 
		
		// nocache gets a randomized 6-digit number to trick
		// browser not to retrieve content from cache everytime.
		xml = "nocache=" + Math.floor(Math.random() * 10000001) + "&" + xml;

		this.xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		this.xmlHttp.send( xml );
	}
	
	
	HTTPService.prototype.onreadystatechange=function( callBackFunc, opt )
	{
		var isSuccess = true;	// whether the request is successful
		
		if( this.xmlHttp.readyState == 4 )
		{
			if (this.xmlHttp.status == 200 || this.xmlHttp.status == 0)
			{
				var responseXML = this.xmlHttp.responseText + "";		// returns text string
				
				// to test whether responseXML is valid
				if ( !responseXML ){
					isSuccess = false;
				}
				
				if(isSuccess){
					if ( callBackFunc )
					{
						this.xmlHttp = null;
						callBackFunc( responseXML, opt );
					}
					else
					{
						this.xmlHttp = null;
						return responseXML;
					}
				}
				
			} else {
				isSuccess = false;
			}
			
		} 
		
		if(!isSuccess){
			this.numTrial++;
			if( this.numTrial <= HTTPService.MAX_TRIAL ){
				// resent the request
				this.timeout = this.setTimeOut(HTTPService.DEFAULT_TIMEOUT);
			}
			else{
				// maximum number of trials reached
				this.onMaxRequest();
			}
		}
	}
	
	
	/**
	  *=-----------------------------------------------------------=
	  * setTimeOut
	  *=-----------------------------------------------------------=
	  * 
	  * Set a timeout for request
	  *  
	  * Parameters:
	  *	sec	- number of seconds for time out
	  *
	  * Returns:
	  * 	timeout variable
	  *
	  */
	HTTPService.prototype.setTimeOut = function(sec){
	
		if( !sec ) sec = HTTPService.DEFAULT_TIMEOUT;
		
		// change seconds to miliseconds
		sec = sec * 1000;
		
		var self = this;
		return setTimeout(function(){self.onTimeOut();}, sec);

	}

	
	/**
	  *=-----------------------------------------------------------=
	  * onTimeOut
	  *=-----------------------------------------------------------=
	  * 
	  * Request time out handling function
	  *  
	  * Parameters:
	  *	
	  *
	  * Returns:
	  * 	none
	  *
	  */
	HTTPService.prototype.onTimeOut = function(){
		
		
			/*resent the request*/
		
			
			// get a new xmlHttp object
			//this.xmlHttp = null;
			//this.initAjax();
		this.ajaxRequest( this.method, this.url, this.asynch, this.xml, this.callBackFunc, this.opt );
			
		
	}
	
	
	/**
	  *=-----------------------------------------------------------=
	  * onMaxRequest
	  *=-----------------------------------------------------------=
	  * 
	  * Handling function when request fails after maximum number of trials
	  *  
	  * Parameters:
	  *	
	  *
	  * Returns:
	  * 	none
	  *
	  */
	HTTPService.prototype.onMaxRequest = function(){
		alert('AJAX communication with server failed. Please try again or report the error.' + this.xmlHttp.status + '->' + this.xmlHttp.responseXML );
	}
	
