/*******************************************************************************************
	Ajax_Request Class
	Author: Joshua Bolduc
	Date: 5/24/2007
	Usage: Lets you send and recieve data from a server script
	I've tested this script on IE7, Opera and Firefox. It works on all of them
	
	How to use
	1. start a new request
		request = new ajax_request; 
	2. Set your parameters
		request.process("script.php") 							GET					
		request.process("script.php", "POST", "var1=blah&var2=blah"); 	POST
	3. Run your action
		request.onreadystatechange(your_function); 				Your function will run once the request is ready

********************************************************************************************/
/*******************************************************************************************
	Ajax_Request 
********************************************************************************************/
function ajax_request()
{
	this.loader_enabled = false; 
	this.loader_screen_top_margin = 200;
	this.serverScript = "test.php"; 
	this.is_ie = false; 
}
/*******************************************************************************************
	enable_loader_screen
********************************************************************************************/
ajax_request.prototype.enable_loader_screen = function()
{
	this.loader_enabled = true; 
}
/*******************************************************************************************
	set_loader_screen_top_margin
********************************************************************************************/
ajax_request.prototype.set_loader_screen_top_margin = function(integer)
{
	this.loader_screen_top_margin = integer; 
}
/*******************************************************************************************
	run_loader_screen
********************************************************************************************/
ajax_request.prototype.run_loader_screen = function()
{
	 
	if(this.loader_enabled == true)
	{
		container = document.createElement("div"); 
		container.id = "ajax_request_loader_screen";
		container.style.position = "absolute"; 
		container.style.top      = "1px"; 
		container.style.zIndex = "30"; 
		container.style.width = "99%"; 
		container.style.height= "99%"; 
		
		loader_content = "<img src = \"skins/default/images/loading.gif\" /> loading . . ."; 
		
		div = document.createElement("div"); 
		div.style.width 	  = "300px"; 
		div.style.border 	  = "solid 3px #adadad";
        div.style.padding 	  = "5px"; 
		div.style.height 	  = "20px"; 
		div.style.marginLeft  = "auto"; 
		div.style.marginRight = "auto";
		div.style.marginTop   = this.loader_screen_top_margin+"px"; 
		div.style.background  = "#fff"; 
		div.innerHTML = loader_content; 
		container.appendChild(div); 
		document.body.appendChild(container); 
	}
}
/*******************************************************************************************
	stop_loader_screen
********************************************************************************************/
ajax_request.prototype.stop_loader_screen = function()
{
	loader_screen = document.getElementById("ajax_request_loader_screen");
	
	if(loader_screen)
	{	
		document.body.removeChild(loader_screen);
		
	}
}
/*******************************************************************************************
	init
********************************************************************************************/
ajax_request.prototype.init = function()
{
 
	try
    {
		// Firefox, Opera 8.0+, Safari
		if(navigator.appName == "Microsoft Internet Explorer") this.is_ie = true; 
		this.xmlObj = new XMLHttpRequest();
		 
    }
	catch (e)
    {
		// Internet Explorer
		try
		{
			this.xmlObj=new ActiveXObject("Msxml2.XMLHTTP");
			this.is_ie = true; 
		}
		catch (e)
		{
		try
        {
			this.xmlObj=new ActiveXObject("Microsoft.XMLHTTP");
			this.is_ie = true; 
        }
		catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
	this.xmlObj.serverScript = this.serverScript; 
 
	//----------------------------------------------------------
	// Response Parser
	//----------------------------------------------------------
 
	this.xmlObj.parse_response = function()
	{
		
		var rtnObj   = new Object; 
		var response = this.responseText; 
		 
		while(response.indexOf("[[/")!=-1)
		{
			var start = response.indexOf("[[");
			var name = response.substr(start+2, response.indexOf("]]")-(start+2));
			
			var start_name = "[["+name+"]]"
			var end_name   = "[[/"+name+"]]"; 
			
			var start_pos = response.indexOf(start_name)+start_name.length; 
			var end_pos   = response.indexOf(end_name);
			var objName   = name.toLowerCase()
			    rtnObj[objName] = response.substr(start_pos, end_pos-start_pos); 
			  
			response = response.substr(end_pos+end_name.length);
			 
		}
		return rtnObj; 
	 
		
	}
 
 
}

/*******************************************************************************************
	Process
********************************************************************************************/
ajax_request.prototype.process = function(url, type, post)
{
	
	if(!this.xmlObj) this.init(); 	
	
	if(!type) type = "GET"; 
	if(!url)  url = this.serverScript;
	this.xmlObj.open(type, url, true); 
	
	if(type == "GET")
	{
		this.xmlObj.send(null); 
	}
	
	if(type == "POST")
	{
		
		this.xmlObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		this.xmlObj.send(post); 
		this.xmlObj.type = type;  
	}
	
	
	
	
}
/*******************************************************************************************
	onreadystatechange
********************************************************************************************/
ajax_request.prototype.onreadystatechange = function(action)
{
	my_self = this; 
	my_self.run_loader_screen(); 
	
	if (this.is_ie)
	{
	
		this.xmlObj.action = action;
		
		if(this.xmlObj.type == "POST")
		{	
			var xmlObj= this.xmlObj; 
			xmlObj.onreadystatechange = function()
			{
				
				if(xmlObj.readyState == 4)
				{
						
					my_self.stop_loader_screen(); 
					xmlObj.response = xmlObj.parse_response(); 
					xmlObj.action(); 

				}			
			}
		}else{
			
			var xmlObj= this.xmlObj; 
			xmlObj.onreadystatechange = function()
			{
				if(xmlObj.readyState == 4 && xmlObj.status == 200)
				{
					 
					my_self.stop_loader_screen();
					xmlObj.response = xmlObj.parse_response(); 
					xmlObj.action(); 
				}
			}
		
		}
		
		
	}else{
		
		var xmlObj = this.xmlObj
		xmlObj.action = action; 
		xmlObj.onreadystatechange = function()
		{
			
			if(xmlObj.readyState == 4)
			{
				//stops the loader screen
				my_self.stop_loader_screen();
				xmlObj.response = xmlObj.parse_response(); 
				xmlObj.action(); 
			
			}

		}	
	}
	
}
/*******************************************************************************************
		This function parses out to response and stores it in an object
********************************************************************************************/




