// ***************************************************
//   Ajax Script v1.0 By  Ba-Aqeel (c) 2009
// ***************************************************

document.write('<div id="ajax_progress_shadow" style="position:absolute;display:none;background-Color:gray;z-index:601"></div>');
document.write('<div id="ajax_progress_big_shadow" style="position:absolute;display:none;background-Color:gray;z-index:602"></div>');
document.write('<img id="ajax_progress" style="position:absolute;display:none;z-index:603">');

function ajax_start(url, args, container, progress, proc)
{
	/*
		url (string) is URL of source file
			Example\ "./ajaxfunc.php"
		args (string) is arguments of the source file
			Example\ "name=rayaheen&id=12". You should use encodeURI for arabic value or value contains & or =
		container (Array) is Array(id,behavior,focus)
			[id] (string or object) is id of object where output of source file lay
			[behavior] (number) is 1 to 4
					how contaier receive the output
					1 = put the output at the top,
					2 = put the output at the bottom,
					3 = clear the content of container then fill it with the output
					otherwise = overwrite
			[focus] (number) is 0 or 1
					0 = don't set focus,
					1 = set focus
		progress (Array) is Array(source,width,height,position,shadow,container id,offset,big shadow)
			[source] (string) is URL of progress image
			[width] (number) is width of the image
			[height] (number) is height of the image
			[position] (number) is position of the progress image related to container from 1 to 9
					1 = top left, 2 = top center, 3 = top right,
					4 = middle left, 5 = middle center, 6 = middle right,
					7 = bottom left, 8 = bottom center, 9 = bottom right,
					otherwise = it will be 5
			[shadow] (number) is used to hide or show the shadow. It takes 0 or 1
			[container id] (string or object) is id of object where progress image lay
			[offset] (number) is a distance between progress image and the border of container
			[big shadow] (number) is used to hide or show the big shadow that fill whole page. It takes 0 or 1
		proc (function name) is name of function to be invoked after HTTP Request is complete

		Notes:
			you can use brakets [...] instead of Array(...)
	*/

	function initParameters()
	{
		if (typeof(container)=="undefined")
			container = Array();
		if (typeof(progress)=="undefined")
			progress = Array();

		for (var i=0; i<3; i++)
		{
			if (typeof(container[i])=="undefined")
				container[i] = null;
		}
		for (var i=0; i<8; i++)
		{
			if (typeof(progress[i])=="undefined")
				progress[i] = null;
		}
	}

	function initContainer()
	{
		if (container_object)
		{
			if (typeof(container_object)=="string")
				container_object = document.getElementById(container_object);
			else if (typeof(container_object)=="object")
				container_object = container_object;
			var elementTagName = container_object.tagName;
			container_useValueProperty = (elementTagName=="TEXTAREA" || elementTagName=="INPUT");
		}
	}

	function initProgressContainer()
	{
		if (isNaN(progress_offset))
			progress_offset = 0;

		if (!progress_container)
			progress_container = document.body;
		else if (typeof(progress_container)=="string")
			progress_container = document.getElementById(progress_container);
		else if (typeof(progress_container)!="object")
			progress_container = document.body;
		if (progress_container==document.body)
		{
			progress_container_left = document.body.scrollLeft + progress_offset;
			progress_container_top = document.body.scrollTop + progress_offset;
			progress_container_width = document.body.clientWidth - progress_offset * 2;
			progress_container_height = document.body.clientHeight - progress_offset * 2;
		}
		else
		{
			var obj = progress_container, x = 0, y = 0;
			while(obj.offsetParent)
			{
				x += obj.offsetLeft;
				y += obj.offsetTop;
				obj = obj.offsetParent;
			}
			progress_container_left = x + progress_offset;
			progress_container_top = y + progress_offset;
			progress_container_width = progress_container.offsetWidth - progress_offset * 2;
			progress_container_height = progress_container.offsetHeight - progress_offset * 2;
		}
	}

	function initProgressObject()
	{
		if (isNaN(progress_position) || progress_position<1 || progress_position>9) // vertical-align=middle horizontal-align=center is the default
			progress_position = 5;

		progress_object.src = progress_source;
		progress_object.style.display = "none";
		progress_object.style.width = progress_width;
		progress_object.style.height = progress_height;

		var ox = 0, oy = 0;
		if ((progress_position-1)%3==1)
			ox = (progress_container_width / 2) - parseInt(progress_width) / 2;
		if (Math.floor((progress_position-1)/3)==1)
			oy = (progress_container_height / 2) - parseInt(progress_height) / 2 ;
		if (progress_position%3==0)
			ox = progress_container_width - parseInt(progress_width);
		if (Math.floor((progress_position-1)/3)==2)
			oy = progress_container_height - parseInt(progress_height);
		progress_object.style.left = ox + progress_container_left;
		progress_object.style.top = oy + progress_container_top;
		progress_object.style.display = "block";
	}

	function showProgressImage()
	{
		if (progress_source)
		{
			initProgressObject();
			if (progress_shadow_display)
			{
				if (progress_container==document.body)
				{
					progress_shadow_object.style.left = 0;
					progress_shadow_object.style.top = 0;
					progress_shadow_object.style.width = (document.body.scrollWidth>document.body.clientWidth)?document.body.scrollWidth:document.body.clientWidth;
					progress_shadow_object.style.height = (document.body.scrollHeight>document.body.clientHeight)?document.body.scrollHeight:document.body.clientHeight;
				}
				else
				{
					progress_shadow_object.style.left = progress_container_left - progress_offset;
					progress_shadow_object.style.top = progress_container_top - progress_offset;
					progress_shadow_object.style.width = progress_container_width + progress_offset * 2;
					progress_shadow_object.style.height = progress_container_height + progress_offset * 2;
				}
				progress_shadow_object.style.filter = "alpha(opacity=50)";
				progress_shadow_object.style.opacity = 0.50;
				progress_shadow_object.style.display = "block";
			}
			if (progress_big_shadow_display)
			{
				progress_big_shadow_object.style.filter = "alpha(opacity=50)";
				progress_big_shadow_object.style.opacity = 0.5;
			}
			else
			{
				progress_big_shadow_object.style.filter = "alpha(opacity=0)";
				progress_big_shadow_object.style.opacity = 0;
			}
			progress_big_shadow_object.style.left = 0;
			progress_big_shadow_object.style.top = 0;
			progress_big_shadow_object.style.width = (document.body.scrollWidth>document.body.clientWidth)?document.body.scrollWidth:document.body.clientWidth;
			progress_big_shadow_object.style.height = (document.body.scrollHeight>document.body.clientHeight)?document.body.scrollHeight:document.body.clientHeight;
			progress_big_shadow_object.style.display = "block";
			document.body.focus();
		}
	}

	function hideProgressImage()
	{
		if (progress_object)
		{
			progress_object.src = "";
			progress_object.style.width = "1px";
			progress_object.style.height = "1px";
			progress_object.style.display = "none";
		}
		if (progress_shadow_object)
			progress_shadow_object.style.display = "none";

		progress_big_shadow_object.style.display = "none";
	}

	function GetXmlHttpObject()
	{
		if (XMLHttpRequest)
			return new XMLHttpRequest();
		if (ActiveXObject)
			return new ActiveXObject("Microsoft.XMLHTTP");
		return null;
	}

	xmlhttp = GetXmlHttpObject();

	if (xmlhttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}

	xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState==4)
		{
			hideProgressImage();

			var result = xmlhttp.responseText;

			var param = null;
			var isProc = (typeof(proc)=="function");
			if (isProc)
			{
				result = result.split('\1');
				if (result.length==1)
				{
					param = null;
					content = result[0];
				}
				else
				{
					param = result[0];
					content = result[1];
				}
			}
			else
				content = result;

			if (container_object)
			{
				if (content)
				{
					if (container_behavior==1)
					{
						if (container_useValueProperty)
							container_object.value = content.concat(container_object.value);
						else
							container_object.innerHTML = content.concat(container_object.innerHTML);
					}
					else if (container_behavior==2)
					{
						if (container_useValueProperty)
							container_object.value = container_object.value.concat(content);
						else
							container_object.innerHTML = container_object.innerHTML.concat(content);
					}
					else
					{
						if (container_useValueProperty)
							container_object.value = content;
						else
							container_object.innerHTML = content;
					}

					if (container_useValueProperty && container_focus)
						container_object.focus();
				}
			}
			else if (content)
				alert(content);
			if (isProc)
				proc(param);
		}
	}

	initParameters();

	// container object
	var container_object = container[0];
	var container_behavior = parseInt(container[1]);
	var container_focus = parseInt(container[2]);
	var container_useValueProperty = null;
	// progress object
	var progress_object = document.getElementById("ajax_progress");
	var progress_source = progress[0];
	var progress_width = parseInt(progress[1]);
	var progress_height = parseInt(progress[2]);
	var progress_position = progress[3];
	var progress_shadow_display = parseInt(progress[4]);
	var progress_container = progress[5];
	var progress_offset = parseInt(progress[6]);
	var progress_big_shadow_display = parseInt(progress[7]);
	var progress_shadow_object = document.getElementById("ajax_progress_shadow");
	var progress_big_shadow_object = document.getElementById("ajax_progress_big_shadow");
	var progress_container_left, progress_container_top, progress_container_width, progress_container_height;

	var xmlhttp;

	initContainer();
	initProgressContainer();

	if (container_behavior==3 && container_object)
	{
		if (container_useValueProperty)
			container_object.value = "";
		else
			container_object.innerHTML = "";
	}

	xmlhttp.open("POST", url, true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.send(args);
	showProgressImage();
}
