/*
Author/Owner: 	Daniel Peel.
Contact: 		mintpants@tiscali.co.uk.
Details:		Ajax Object.
Requires:       Base.js
Created: 		25.10.07
Updated: 		1.11.07
				20.01.08 - 	Moved Augment function to the core jscript script
						   	Removed the parse tabs Augmentation(tabberAutomatic())
				21.02.08 - 	Converted to a class giving more flexibility and simplicity to different uses of ajax
						   	added callback functionailty to deal with the request result
				25.11.08 - 	Removed the loading messge functionality, moved to its own functions (see macros at bottom of class)
							as changing the messages style within the ajax object was not atomic, and problems with "this" reference.
							Added a preload event to be defined when calling the object.
							Added an example use at the bottom of the file.
*/

//Requires innerWindow, scrolled, divScreenCenter, Augment, and base
function Ajax()	{
	
	//********************  create ajaxObject  *********************//
	this.jaxObj = false;
	
	//firefox, netscape or safari
	if(window.XMLHttpRequest)	
	{
		this.jaxObj = new XMLHttpRequest();
	}	
	//interent explorer
	else if(window.ActiveXObject)	
	{
		this.jaxObj = new ActiveXObject("Microsoft.XMLHTTP"); 
	}
	//*******************  end ajaxObject  *************************//
	
}

Ajax.prototype.callBack = function(responseText)	{
	
	//Cleanup if we havent been overwriiten
	responseText = null;
	this.jaxObj =  null;
}

Ajax.prototype.preLoad = function()	{

}
	
//**************************************************************//
//					  GET data ajax request                     //

//requires the response div object not just a reference
Ajax.prototype.getData = function(source, extraArgs)	{
		
	this.args = Augment({
     	 noneForNow: false
    }, extraArgs );
	
	if(this.jaxObj)
	{
		this.jaxObj.open("GET", source);
		
		var self = this;		
		
		this.jaxObj.onreadystatechange = function()	{
				
			//has the data been retrieved
			if(self.jaxObj.readyState == 4 && self.jaxObj.status == 200)
			{				
				self.callBack(self.jaxObj.responseText);										
			}
			else
			{
				self.preLoad();
			}
		}
	
		//send the request
		this.jaxObj.send(null);
	}
}//end get data source

//***************************************************************//
//					   POST data ajax request                    //

//requires the response div object not just a reference
Ajax.prototype.postData = function(url, data, extraArgs)	{
	
	this.args = Augment({
     	 noneforNow: false
	}, extraArgs );
	
	if(this.jaxObj)
	{
		this.jaxObj.open("POST", url);
		this.jaxObj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		this.jaxObj.setRequestHeader("Content-length", data.length);
    	this.jaxObj.setRequestHeader("Connection", "close");
		
		var self = this;
				 
		this.jaxObj.onreadystatechange = function()	{
				
			//has the data been retrieved
			if(self.jaxObj.readyState == 4 && self.jaxObj.status == 200)
			{				
				self.callBack(self.jaxObj.responseText);										
			}
			else
			{
				self.preLoad();
			}	
		}
		
		//send the request
		this.jaxObj.send(data);
	
	}//End if JaxObject
}//end get data source

//************************************ MACRO **************************************//
//*********************************************************************************//

function AjaxLoadMessage(loadingDivId, parentNodeId, loadImgRelUrl, messageText)		{	
	
	var loadingDiv, loadingTxt, loadingImg; 
	
	//Only load the loader once
	if(ById(loadingDivId) != null)
	{
		return;
	}
	//Create the wrapper
	loadingDiv = document.createElement("div");
	loadingDiv.setAttribute("id" , loadingDivId);
	loadingDiv.style.position = 'absolute';
	loadingDiv.style.visibility = 'hidden';
	loadingDiv.style.top = '0px';
	loadingDiv.style.left = '0px';
	loadingDiv.style.textAlign = 'center';
	loadingDiv.style.fontSize = "12px";
	loadingDiv.style.color = '#999999';
		
	//Create the load image
	loadingImg = document.createElement("img");
	loadingImg.setAttribute("src", loadImgRelUrl);
	
	//Append to the wrapper
	loadingDiv.appendChild(loadingImg);
	
	//Create the load message
	if(messageText != false)
	{	
		spacer = document.createElement("div");
		spacer.setAttribute("id" , loadingDivId + "_prometheus_spacer");
		//spacer.style.marginBottom = '3px;' //throws an exception in ie
		loadingDiv.appendChild(spacer);
		
		loadingTxt = document.createTextNode(messageText);
		//Append to wrapper
		loadingDiv.appendChild(loadingTxt);
	}	
	
	
	//Append the wrapper to the DOM so we can access its offset properties
	document.body.appendChild(loadingDiv);	
	
	if(parentNodeId == 'body')
	{
		//load the inner window size
		innerWindowSize();
		
		loadingDiv.style.padding = "20px";
		
		loadingDiv.style.top = divScreenCenter(loadingDiv.offsetWidth, loadingDiv.offsetHeight).verCent + "px";		
		loadingDiv.style.left = divScreenCenter(loadingDiv.offsetWidth, loadingDiv.offsetHeight).horCent + "px";
		
		loadingDiv.style.backgroundColor = "#ffffff";
		loadingDiv.style.border = "1px solid #000000";
		
	}
	else
	{	
		var pNode = ById(parentNodeId);
		pNode.appendChild(loadingDiv);
		
		loadingDiv.style.position = null;
		loadingDiv.style.top = null;
		loadingDiv.style.left = null;
		loadingDiv.style.width = 'inherit';
		//Center the loadingDiv withint the parent wrapper
		loadingDiv.style.marginTop = (pNode.offsetHeight/2) - (loadingDiv.offsetHeight/2) + "px";		
	}	
	
	//Show the loading div
	loadingDiv.style.visibility = 'visible';
	
}

function AjaxRemoveLoadMessage(loadingDivId, parentNodeId )	{
	
	if(parentNodeId == 'body')
	{		
		document.body.removeChild(ById(loadingDivId));
	}
	else
	{
		ById(parentNodeId).removeChild(ById(loadingDivId));
	}
	
}


//*********************************************************************************//
//******************************* EXAMPLE USE *************************************//
/*
function AjaxEample(statusText, statusBttn, id)
{	
	var statusObj = ById(statusText);
	var bttnObj = ById(statusBttn);
	
	var loadingDivId 	= "processingStatusReq";
	var parentNodeId 	= "body";
	var loadImgRelUrl 	= "../img/ajax-loader.gif";
	var messageText 	= "loading";
	
	jax = new Ajax();	
	
	jax.preLoad = function()	{		
		AjaxLoadMessage(loadingDivId, parentNodeId, loadImgRelUrl, messageText);	
	}
		
	jax.callBack = function(responseText)	{
	
		AjaxRemoveLoadMessage(loadingDivId, parentNodeId);
		
		if(responseText.indexOf('success', 0) > -1)
		{
			if(statusObj.innerHTML == 'Yes')
			{
				statusObj.innerHTML = 'No';
				bttnObj.innerHTML = '<img src="../images/activate_bttn.jpg" />';
			}
			else
			{
				statusObj.innerHTML = 'Yes';
				bttnObj.innerHTML = '<img src="../images/deactivate_bttn.jpg" />';
			}
		}
		else
		{
			alert("Sorry, a system error occured, if the problem persists please contact the website administrator.");
		}
		
	}
			
	var postStr = "submit=true&id="+encodeURI(id);
	jax.postData("ajax_updateDlStatus.php", postStr);		
	
}
*/
	
