/**
	* @author Kevin Ross
	* @fileoverview Ajax request class
	*	Browsers supported
	*	Apple Safari 1.2 > (Note:Cannot return an empty string to safari, include at least space char in response)
	*	Konqueror
	*	IE5 > (Windows only)
	*	Moz/Firefox 1 >
	*	Netscape 7.1 >
	*	Opera 7.6 >
*/

/**
 * Constructs a Kajax class
 * @class A class which contains functions specific to ajax request handling
 * @constructor
*/
function Kajax()//start class
{
	var self = this;
	
	this.xmlhttp = false;
	this.method = "GET";
	this.url = "";
	this.vars = "";
	this.responseType = "";
	this.response = "";
	this.responseXML = "";
	this.error = "";
	
	this.onLoading = function(){};
	this.onLoaded = function(){};
	this.onInteractive = function(){};
	this.onCompletion = function(){};
	this.onError = function(){};
	
	//public methods

	this.sendRequest = function()
	{
		init();
		create();
		send();
	}
		
	//private methods
	function init()
	{
		self.response = "";
		self.responseXML = "";
		self.error = "";
	}
		
	function create()
	{
  	if(window.XMLHttpRequest) 
		{
    	try
			{
				self.xmlhttp = new XMLHttpRequest();
      }
			catch(e) 
			{
				self.xmlhttp = false;
      }
			
			//if moz override mime type
			try	
			{
				self.xmlhttp.overrideMimeType("text/xml");//stops moz from locking up when passing back NON text/xml
			}
			catch(e)
			{}
			
    }
		else if(window.ActiveXObject) 
		{
    	try
			{
      	self.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } 
			catch(e)
			{
      	try
				{
        	self.xmlhttp = new ActiveXObject("Msxml.XMLHTTP");
        } 
				catch(e)
				{
          self.xmlhttp = false;
        }
			}
		}
		else
		{
			return false;
		}
  }
		
	function send(p_strVars)
	{
		if (self.xmlhttp) 
		{
		
			self.xmlhttp.onreadystatechange = processStateChange;
			
			if (self.method == "GET")
			{
				try
				{
					self.xmlhttp.open("GET", self.url, true);
					//defeat IE cache issue
					self.xmlhttp.setRequestHeader('If-Modified-Since','Fri, 15 Nov 1977 09:42:28 GMT');
					self.xmlhttp.send(null);
					
				}
				catch(e)
				{
					self.error = "OPEN URL FAILED";
					self.onError();
					clearRequest();
				}
			}
			else
			{
				
				try
				{
					self.xmlhttp.open("POST", self.url, true);
					self.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					self.xmlhttp.send(self.vars);
				}
				catch(e)
				{
					self.error = "OPEN URL FAILED";
					self.onError();
					clearRequest();
				}
			}			
		}
		else
		{
			self.error = "XMLHTTP CREATE FAILED";
			self.onError();
			clearRequest();
		}
	}
		
	function processStateChange()
	{
		switch(self.xmlhttp.readyState)
		{
			case 1 :
				self.onLoading();
				break;
			case 2 :
				self.onLoaded();
				break;
			case 3 :
				self.onInteractive();
				break;
			case 4 :
				switch(self.xmlhttp.status)
				{
					//note:safari sets any status code other than 200 to undefined
					case 200:
						self.response = self.xmlhttp.responseText;
						self.responseXML = self.xmlhttp.responseXML;
						if (self.responseType == "text/xml" && self.responseXML.xml == "")
						{
							self.error = "NO XML DATA";
						}
						break;	
					case 500:
						self.error = "500 INTERNAL SERVER ERROR";
						break;
					case 404:
						self.error = "404 NOT FOUND";
						break;
					default:
						self.error = "ERROR STATUS CODE : " + self.xmlhttp.status;	
				}
				
				if (self.error == "")
				{
					self.onCompletion();
				}
				else
				{
					self.onError();
				}
				
				clearRequest();
				break;
		}
	}	
	
	function clearRequest()
	{
		self.xmlhttp = false;
		self.method = "GET";
		self.url = "";
		self.vars = "";
		self.onLoading = function(){};
		self.onLoaded = function(){};
		self.onInteractive = function(){};
		self.onCompletion = function(){};
		self.onError = function(){};
	}
	
}//end class

//util funcs
function hasAjaxSupport()
{	
  if(window.XMLHttpRequest) 
	{
    try
		{
			new XMLHttpRequest();
			return true;
    }
		catch(e) 
		{
			return false;
    }
	}
	else if(window.ActiveXObject) 
	{
    try
		{
      new ActiveXObject("Msxml2.XMLHTTP");
			return true;
    } 
		catch(e)
		{
    	try
			{
      	new ActiveXObject("Msxml.XMLHTTP");
				return true;
      } 
			catch(e)
			{
      	return false;
     	}
		}
	}
	else
	{
		return false;
	}
}
