var ajax = null;
var ajaxIntervalId = 0;
var cpt = 0;
var ajaxProcessing = null;
var divDisable = null;

function initAjax(stack)
{
	ajax = new ajaxClass();
}

function startGetPageStackTimer(stack)
{	
	cpt = stack.length - 1;
	changeContent = setTimeout('AjaxInExecution()', 30);
	if (document.getElementById('disableScreen') == null)
	{
		
		divDisable = document.createElement('div');
		divDisable.id = 'disableScreen';
		divDisable.className = 'disableScreen';	
		document.body.appendChild (divDisable);	
		
	}else
		divDisable = document.getElementById('disableScreen');
	
	divDisable.style.height = "100%";
	divDisable.style.display = "block";
}
function AjaxInExecution()
{
	try
	{	
		ajaxIntervalId = setInterval('startGetPageStack()',30);
	} catch(e) { handleError(e); }
}
function startGetPageStack()
{
	try
	{
		if(document.getElementById('ajaxProcessing') == null)
		{
			ajaxProcessing = document.createElement('div');
			ajaxProcessing.id = 'ajaxProcessing';
			document.body.appendChild (ajaxProcessing);
			ajax.getPage(stack[cpt]["div"], stack[cpt]["page"]);
			cpt--;
		}
		if(cpt < 0 )
		{
		    clearInterval(ajaxIntervalId);
			document.body.removeChild (divDisable);
		}
	} catch(e) { handleError(e); }
}

function ajaxClass()
{
	this.getPage = function (divResult,requestPage)
	{
		try
		{
			load();
			
			var oXmlHttp = zXmlHttp.createRequest();
			oXmlHttp.open("get", requestPage  , true);
			oXmlHttp.onreadystatechange = function () 
			{
			    if (oXmlHttp.readyState == 4) {
			        if (oXmlHttp.status == 200) {
			            ajax.result(oXmlHttp.responseText,divResult);
			        } else {
			        	ajax.result("An error occurred: " + oXmlHttp.statusText,div);
			        }
			    }            
			};
			oXmlHttp.send(null);
		}catch(e) { handleError(e); }
	};

	this.result = function(result,divResult)
	{
		try{
			load();
			var div = document.getElementById(divResult);
			div.innerHTML =  result;
			if(document.getElementById('ajaxProcessing') != null)
				document.body.removeChild (document.getElementById('ajaxProcessing'));
			
			
		} catch(e) { handleError(e); }
	};
	this.postPage = function(f,div,action) 
	{
		try
		{
			load();
			
		   var oForm = document.getElementById(f);
		   var sBody = this.getRequestBody(oForm);
		   sBody += "&ajax=true";
		   var oXmlHttp = zXmlHttp.createRequest();
		   oXmlHttp.open("post", action, true);
		   oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		   oXmlHttp.onreadystatechange = function () {
		       if (oXmlHttp.readyState == 4) {
		           if (oXmlHttp.status == 200) {
		        	   ajax.result(oXmlHttp.responseText,div);
		           } else {
		        	   ajax.result("An error occurred: " + oXmlHttp.statusText,div);
		           }
		       }            
		   };
		   oXmlHttp.send(sBody);
	} catch(e) { handleError(e); }
	};
	this.getRequestBody=function(oForm) 
	{
		try
		{
			var aParams = new Array();
			for (var i=0 ; i < oForm.elements.length; i++) 
			{
			    var sParam = encodeURIComponent(oForm.elements[i].name);
			    if(sParam != "undefined"){
			    	sParam += "=";
			    	sParam += encodeURIComponent(oForm.elements[i].value);
			    	aParams.push(sParam);
			    }
			} 
			return aParams.join("&");     
		} catch(e) { handleError(e); }
	};
}
