var objNumber = 0; // Current AJAX request number
var maxObjNumber = 10; // Maximum number of AJAX request

function ajax( image_path ) // AJAX Class
{
	this.method = "GET"; // Send Method can be "GET" or "POST"
	this.url = ""; // URL Address, from where we can get content
	this.objOut = ""; // ID name where output will display
	this.objWait = ""; // ID name where Wait Image will display
	this.para = ""; // Parameter list for "GET" or "POST" method. Format is "variable1=value1&variable2=value2&........"
	this.extra_function = ""; // Function name where we want to pass control after geting contents.
	this.type = ""; // Type can be empty(text) or XML.
	this.send = sendRequest; // After providing required information this method call for process and geting contents
	this.img = document.createElement('img'); // Image object for wait image
	this.imgPath = image_path; // Image path for wait image. You can change it on runtime
	this.formType = "application/x-www-form-urlencoded"
}

function sendRequest() // This function call from AJAX class
{
	if(this.url=="")
	{
		alert("No any URL Address Enter");
		return false;
	}
	//this.url=this.url+"&"+new Date().getTime();
	ns4 = (document.layers)? true:false;
	ie4 = (document.all)? true:false;
	centerX = 0;
	centerY = 0;

	if(ie4)
	{
		centerX = document.body.offsetWidth / 2;
		centerY = document.body.offsetHeight / 2;
	}
	else
	{
		if(ns4)
		{
			centerX = window.innerWidth / 2;
			centerY = window.innerHeight / 2;
		}
	}

	varName = "ajaxObj" + objNumber++
	if(objNumber>=maxObjNumber)
	{
		objNumber = 0;		
	}

	this.img.setAttribute('src', this.imgPath);
	this.img.setAttribute('id', "wimg" + objNumber);
	this.img.style.position = "absolute";
	this.img.style.visibility = "hidden";
	if(this.objWait=="")
	{
		this.img.style.left = centerX
		this.img.style.top = centerY
	}
	else
	{
		var wimg = document.getElementById(this.objWait)
		this.img.style.left = getXOffset(wimg);
		this.img.style.top = getYOffset(wimg);
	}
	
	document.getElementById('tmp').appendChild(this.img);
	
	eval(varName + " = new createXMLHttp()");
	//alert( varName + ".onreadystatechange = function() {processRequest(" + varName + ",'" + this.objOut + "', '" + this.img.id + "','" + this.extra_function +"','" + this.type +"');}" );
	//return false;
	eval(varName + ".onreadystatechange = function() {processRequest(" + varName + ",'" + this.objOut + "', '" + this.img.id + "','" + this.extra_function +"','" + this.type +"');}");
	eval(varName + ".open(\""+ this.method +"\", \"" + this.url + "\", true)");
	eval(varName + ".setRequestHeader('Content-Type', '" + this.formType + "')");
	eval(varName + ".send('" + this.para + "')");
	
}

function createXMLHttp() // Initializing AJAX object
{
	var req;
	if(window.XMLHttpRequest){
		req = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		var avers = ["Microsoft.XmlHttp", "MSXML2.XmlHttp", "MSXML2.XmlHttp.3.0", "MSXML2.XmlHttp.4.0", "MSXML2.XmlHttp.5.0"];
	   for (var i = avers.length -1; i >= 0; i--) {
		  try {
			 req = new ActiveXObject(avers[i]);
			 } catch(e) {}
	   }
	} else {
		alert('There was a problem creating the XMLHttpRequest object');
	}
	return req;
}

function processRequest (obj,output, objwimg, extra_function, type) // function for checking response and placing data on required place
{
	if (obj.readyState == 1) {
		document.getElementById(objwimg).style.visibility = "visible";
		if(trim(extra_function)!="")
		{
			//alert( "EXTRA FUNCTION : \n" + extra_function );
			eval(extra_function);
		}
   }
   if (obj.readyState == 2) {
   }
   if (obj.readyState == 3) {
   }
   if (obj.readyState == 4) {
	if (obj.status == 200) {
			if(type=="xml")
			{
				getString = obj.responseXML;
			}
			else
			{
				getString = obj.responseText;
			}
			//alert( getString );
			if(!output=="")
				
				document.getElementById(output).innerHTML = getString;
			obj = null;
			
		}
		else
		{
			alert("Can not process your request. Please try again.\n\nStatus : " + obj.status )
		}
		document.getElementById(objwimg).style.visibility = "hidden";
	}
}

function getXOffset(element) // Geting center location varticaly
{
     var tmpEl = element;
     var offset = 0;
     while ( tmpEl != null) {
          offset += tmpEl.offsetLeft;
          tmpEl = tmpEl.offsetParent;
     }
     return offset;
}

function getYOffset(element) // Geting middle location horizantaly
{
     var tmpEl = element;
     var offset = 0;
     while ( tmpEl != null) {
          offset += tmpEl.offsetTop;
          tmpEl = tmpEl.offsetParent;
     }
     return offset;
}

function ltrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

function rtrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}


function trim( value ) {
	return ltrim(rtrim(value));
}
/*****************************************************************
Main Wrapper Function To Call to make Ajax Calls
**********************************************************/

function loadPage( page_url , args, displayArea, image_path, extra_function  )
{
	var obj = new ajax( image_path );
	
	obj.url = page_url+"?"+args+"&"+new Date().getTime();
	//alert( obj.url );
	obj.objOut = displayArea;
	obj.type = "";
	obj.extra_function = extra_function;
	obj.send();
	obj = null;
}

function accExecuteScript(str)
{
	/*try
	{
		//alert(str.substr(str.indexOf("<!--")+4,str.indexOf("-->")-(str.indexOf("<!--")+4)));
		eval(str.substr(str.indexOf("<!--")+4,str.indexOf("-->")-(str.indexOf("<!--")+4)));
		createCookie("patient_list_select_old","table_row2");
	}catch(Exp){alert(Exp.message)}*/
}

