// --------------------------------------------
// FILENAME  : ajax_search.js
// STARTED   : Wed June 03, 2008
// WEBSITE   : http://www.ArabCoders.Org
// COPYRIGHT : © 2001, 2008 ArabCoders Coding.
// --------------------------------------------

var abobjects = new Array();
var abDOMtype = '';
if (document.getElementById) { abDOMtype = "std"; }
else if (document.all) { abDOMtype = "ie4"; }
else if (document.layers) { abDOMtype = "ns4"; }

var xmlHttp
txt1 = fetch_object("txt1");
livesearch = fetch_object("livesearch");

function fetch_object(idname, forcefetch) {
	if (forcefetch || typeof(abobjects[idname]) == "undefined") {
		switch (abDOMtype) {
			case "std": {
				abobjects[idname] = document.getElementById(idname);
			}
			break;
			case "ie4":	{
				abobjects[idname] = document.all[idname];
			}
			break;
			case "ns4":	{
				abobjects[idname] = document.layers[idname];
			}
			break;
		}
	}
	return abobjects[idname];
}

function xGetXmlHttpObject() {
	var xmlHttp=null;
	try {
	 xmlHttp=new XMLHttpRequest();
 	}
	catch (e) {
 		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  		}
 		catch (e) {
  			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
 	}
	return xmlHttp;
}

function LiveSearch_Result(str) {
	if (str.length <= 3) {
		txt1.innerHTML="";
	 	return
 	}

	xmlHttp = xGetXmlHttpObject();

	if (xmlHttp==null) {
		 alert ("المتصفح لا يدعم الإتصال عن طريق XMLRequest.. نرجو أن تحدث المتصفح الخاص بك!")
		 return
 	}

  	txt1.style.backgroundImage = "url(spinner.gif)";

	var url="ajax_search.php?d="+str;
	url = url+"&sid="+Math.random();

	xmlHttp.onreadystatechange = LiveSearch_StateChanged
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function LiveSearch_StateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		livesearch.innerHTML = xmlHttp.responseText;
   		txt1.style.backgroundImage = "url(searchtxt_bg.gif)";
 	}
}

function LiveSearch_Position() {
	this.elem = fetch_object("txt1");
	this.div = fetch_object("livesearch");
	var el = this.elem;
	var x = 0;
	var y = el.offsetHeight;
	while (el.offsetParent && el.tagName.toUpperCase() != 'BODY') {
		x += el.offsetLeft;
		y += el.offsetTop;
		el = el.offsetParent;
	}

	x += el.offsetLeft;
	y += el.offsetTop;
	y += 5;
	this.div.style.left = x + 'px';
	this.div.style.top = y + 'px';
};

function LiveSearch_HideResults() {
	setTimeout("LiveSearch_HideResults2()",1000)
}
function LiveSearch_HideResults2() {
	livesearch.innerHTML="";
}

function ClearText(text) {
	if (txt1.value == text) {
		txt1.value = "";
		txt1.focus();
	}
}