/******************************************************************************
 * ajaxobj.js
 * ----------------------------------------------------------------------------
 * DO NOT MODIFY OR REMOVE THIS COPYRIGHT SECTION
 * ----------------------------------------------------------------------------
 * Author       : Marco Cosentino (technics@alice.it)
 * Copyright    : 2009 (c) Marco Cosentino
 * License      : GPL 2.0
 * Application  : AJAX Perl chat
 * Version      : 0.1
 * Date Started : 2009/02/15
 * Last Modified: 
 *
 * This is a shoubox written in Perl / Javascript
 *
 
  This file is part of AJAX Perl chat.

    AJAX Perl chat is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AJAX Perl chat 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AJAX Perl chat.  If not, see <http://www.gnu.org/licenses/>.

 ******************************************************************************/
 
 // This script includes some AJAX facilities

function AJAXObj() 
{
	this.httpRequest = null;
	this.onReceiveResponse = function(){};
	this.onRequestTimeout = function(){};
	this.onRequestAborted = function(){};
	
	// has this request been aborted
	this.aborted = false;
	
	this.timer = null;

}
 
AJAXObj.prototype.CreateRequest = function()
 {	 
	try 
	{
		// Firefox, Opera 8.0+, Safari
		this.httpRequest = new XMLHttpRequest();
    }
	catch (e)
	{
		// Internet Explorer
		try 
		{
			this.httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			// IE
			var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP' );
			var success = false;
			for (var i=0;i < XMLHTTP_IDS.length && !success; i++) 
			{
				try 
				{
					this.xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
					success = true;
				} catch (e) {}
			}
			if (!success)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	
	return true;
}
 
AJAXObj.prototype.SendRequest = function(script,data,timeout,cache)
{
	var self = this;
	
	// Abort if there's a call in progrss
	this.abort();

	// Set the new call
	this.aborted = false;
	if(!cache)
	{
		var re = /\?/;
		var date = new Date();
		if(re.exec(script))
			script += '&dontcache=' + date.valueOf();
		else
			script += '?dontcache=' + date.valueOf();
	}
	this.httpRequest.open("GET",script,true);

		
	
	// Set onreadystatechange here since it will be reset after a completed call
	this.httpRequest.onreadystatechange = function()
	{
		// State change callback
		switch(self.httpRequest.readyState)
		{
			case 4:

				if(self.timer != null)
					clearTimeout(self.timer);

				if(!self.aborted)
					self.onReceiveResponse(self.httpRequest.status,self.httpRequest.statusText,self.httpRequest.responseText);
				else
				{
					//debugger;
					self.onRequestAborted();
				}
					
				break;
		}
	}
	
	// Here we set a closure to implement callback style timer
	// the callback will be implemented bu the user of "this" object
	if(timeout != null && timeout > 0)
		this.timer = setTimeout(function(){self.onRequestTimeout()},timeout);
		
	this.httpRequest.send(data);
	
}

// Check if there is a call in progress
AJAXObj.prototype.callInProgress = function() 
	{
		switch ( this.httpRequest.readyState ) {
			case 1:
			case 2:
			case 3:
				return true;
			break;
			default:
				return false;
			break;
		}
	},

// Abort an inprogress request
AJAXObj.prototype.abort = function () 
{
	if (this.callInProgress()) 
	{
		this.aborted = true;
		this.httpRequest.abort();
	}
}