/*
 * Sysbotz Search - Client Side Search Dialog
 * by Sysbotz. Copyright 2005.
 */
 
/* Base URL to Sysbotz Search scripts */
 var ssearch_url = "http://www.abspkg.com/search/";
 var ssearch_url2 = "http://abspkg.com/search/";

/* Delay from when user stops typing till Sysbotz Search initiates the search */
 var ssearch_delay = 1500;

/* The width of the search results drop down window */
 var ssearch_width = 300;

/* Animation Image */
 var ssearch_anim = true;
 var ssearch_anim_img = "images/search.gif";

/* Used internally */
 var ssearch_div;
 var ssearch_change = 0;
 var ssearch_keywords = "";
 var http;

 /* Initiliaze Dialog
  */
 function ssearch_init() {
 /* Setup the div for search results. */
 	var ie4=document.all;
	var ns6=document.getElementById&&!document.all;

 /* Try to setup XMLHttpRequest */
	http = getHTTPObject();

	if ((ie4||ns6) && http) {
		document.ssearchform.action = "";
		
		/* Create Main Result Window */
		ssearch_div=document.createElement('DIV');
		ssearch_div.id="ssearch_div";
		ssearch_div.className="ssearch_results";
		ssearch_div.style.visibility="hidden";
		ssearch_div.style.width=ssearch_width;
		ssearch_div.style.zIndex="200";
		document.body.appendChild(ssearch_div);
		ssearch_div.style.position="absolute";
		ssearchPos();
		
		document.getElementById("ssearch_submit").style.visibility="hidden";

 /* Setup the event handler */
		ssearch_keywords=document.getElementById("query").value;
		setTimeout("ssearchOnchange()", ssearch_delay);
	}
	
/* Select all search text and put focus on search field */
	document.ssearchform.query.select();
	document.ssearchform.query.focus();
 }
 
 /* On change event.
 */
 function ssearchOnchange() {
 	if(document.getElementById("query").value != ssearch_keywords) {
		if(ssearch_change == 0) {
			ssearch_change = 1;
		// Draw Results div
			ssearch_div.style.position="absolute";
			ssearchPos();
			ssearch_div.style.visibility="visible";
			ssearch_div.innerHTML = new String("<div class=\"ssearch_inner_results\"><div class=\"ssearch_text_results\">Loading...</div></div>");
			
			if(ssearch_anim)
				document.getElementById("ssearch_button_canvas").innerHTML = new String("<img src=\""+ssearch_url+ssearch_anim_img+"\">");
		}
		
		ssearch_keywords=document.getElementById("query").value;
		setTimeout("ssearchOnchange()", ssearch_delay);
	} else {
		if(ssearch_change == 1) {
			ssearchResults();
		} else {
			setTimeout("ssearchOnchange()", ssearch_delay);
		}
	}
 }
 
 /* Do the search
  */
 function ssearchResults() {
 	if(document.getElementById("query").value.length > 2) {
		try {
			http.open("GET", ssearch_url + "results.php?query=" + escape(document.getElementById("query").value), true);
		} catch(e) {
			http.open("GET", ssearch_url2 + "results.php?query=" + escape(document.getElementById("query").value), true);
		}
		
		http.onreadystatechange = handleHttpResponse;
		http.send(null);
	} else {
		ssearch_div.style.visibility="hidden";
		if(ssearch_anim) {
			document.getElementById("ssearch_button_canvas").style.visibility="hidden";
			document.getElementById("ssearch_button_canvas").innerHTML = new String("");
		}

	 /* Setup the event handler */
		ssearch_keywords=document.getElementById("query").value;
		ssearch_change = 0;
		setTimeout("ssearchOnchange()", ssearch_delay);		
	}
 }
 
 /* Get Search Results */
 function handleHttpResponse() {
	if (http.readyState == 4) {
	// Title and Url on each other line.
		results = http.responseText.split("\n");
	
	// Render Results
		html = new String("<div class=\"ssearch_inner_results\"><table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\" class=\"ssearch_text_results\">");
		html += "<tr><td width=\""+(ssearch_width - 20)+"\" bgcolor=\"#F0F0F0\">Search Results ("+results[0]+" matches):</td><td width=\"20\" bgcolor=\"#F0F0F0\"><A HREF=\"javascript:ssearchHide();\" class=\"ssearch_menu_links\">[X]</A></td></tr>";

	/* Output suggested spelling based on keywords indexed. */
		if(results[1].length > 0) {
			html += "<tr><td colspan=\"2\" width=\"100%\">Did you mean <A HREF=\"javascript:ssearchSuggest('"+escape(results[1])+"');\" class=\"ssearch_links\">"+results[1]+"</A>?</td></tr>";
		}
		
		if(results[0] == 0) {
			html += "<tr><td colspan=\"2\" width=\"100%\">"+results[2]+"</td></tr>";
		} else {
			var s=0;
			for(i=2; i < results.length; i++) {
				if(s == 0 && results[i] != "") {
					s=1;
					html += "<tr><td colspan=\"2\" width=\"100%\" class=\"ssearch_links\"><img src=\""+ssearch_url+"images/point.gif\"> <A HREF=\""+results[i]+"\" class=\"ssearch_links\">";
				} else {
					if(results[i] != "") {
						s=0;
						html += results[i]+"</A><br><img height=\"1\" width=\"100%\" src=\""+ssearch_url+"images/seperator.gif\"></td></tr>";
					}
				}
			}
		}

	// Link For Advanced Search
		html += "<tr><td colspan=\"2\" bgcolor=\"#F0F0F0\"><A HREF=\""+ssearch_url+"search.php?query="+escape(document.getElementById("query").value)+"\" class=\"ssearch_menu_links\">[ Advanced Search ]</A></td></tr>";
		html += "</table></div>";
		
		
	// Draw Results div
		ssearchPos();
		ssearch_div.innerHTML=html;
		if(ssearch_anim)
			document.getElementById("ssearch_button_canvas").innerHTML = new String("");

	 /* Setup the event handler */
		ssearch_keywords=document.getElementById("query").value;
		ssearch_change = 0;
		setTimeout("ssearchOnchange()", ssearch_delay);		
	}
 }
 
/* Fill text box with suggestion */
 function ssearchSuggest(w) {
 	document.getElementById("query").value=unescape(w);	
 }
 
/* Hide results div */
 function ssearchHide() {
	ssearch_div.style.visibility="hidden";
	document.getElementById("query").value="Search";
	ssearch_keywords=document.getElementById("query").value;
 }

/* Helper functions for displaying results */
 function ssearchPos() {
 	var y = getposOffset(document.getElementById("query"),"top");
	var x = getposOffset(document.getElementById("query"),"left");
	var w = getBrowserWidth();
	
	if((ssearch_width + x + 6) > w)
		x = w - (ssearch_width + 6);
	
	ssearch_div.style.top=y + 20;
	ssearch_div.style.left=x - 4;
 }

 function getposOffset(what, offsettype){
	var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
	var parentEl=what.offsetParent;
	while (parentEl!=null){
		totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	return totaloffset;
 }

 function getBrowserHeight() {
 	if(window.innerHeight)
		return window.innerHeight;
	
	if(document.body.clientHeight)
		return document.body.clientHeight;
 }
 
 function getBrowserWidth() {
 	if(window.innerWidth)
		return window.innerWidth;
	
	if(document.body.clientWidth)
		return document.body.clientWidth;
 }
 
 function getHTTPObject() {
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	
	return req;
 }

 /* Used to display enter */ 
 function noEnter(event) {
 	if(http) {
	 	if(window.event) {
			var keyCode = window.event.keyCode;
		} else {
			var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		}
	
		if (keyCode == 13)
			return false;
	}
 }
