/*
	This is the JavaScript file for the AJAX Suggest Tutorial

	You may use this code in your own projects as long as this 
	copyright is left	in place.  All code is provided AS-IS.
	This code is distributed in the hope that it will be useful,
 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	
	For the rest of the code visit http://www.DynamicAJAX.com
	
	Copyright 2006 Ryan Smith / 345 Technical / 345 Group.	

*/
//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Browser incompatible.");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq2 = getXmlHttpRequestObject();

//Called from keyup on the search textbox.
//Starts the AJAX request.
function searchSuggest2() {
	if (searchReq2.readyState == 4 || searchReq2.readyState == 0) {
		var str2 = document.getElementById('regionaj').value;
		var str = escape(document.getElementById('txtSearch2').value);
		searchReq2.open("GET", 'scriptcode/searchSuggest2.php?search=' + str + '&CATEGORIES_ID=' + str2, true);
		searchReq2.onreadystatechange = handleSearchSuggest2; 
		searchReq2.send(null);
			if (str.length > 0)
			{
				document.getElementById('search_suggest2').className = 'visible';
			} else {
				document.getElementById('search_suggest2').className = 'invisible';
			}
	}		
}

//Called when the AJAX response is returned.
function handleSearchSuggest2() {
	if (searchReq2.readyState == 4) {
		var ss = document.getElementById('search_suggest2')
		ss.innerHTML = '';
		var str = searchReq2.responseText.split("\n");
		for(i=0; i < str.length - 1; i++) {
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var suggest = '<div onmouseover="javascript:suggestOver2(this);" ';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setSearch2(this.innerHTML);" ';
			suggest += 'class="suggest_link">' + str[i] + '</div>';
			ss.innerHTML += suggest;
		}
	}
}


var stayAlive = 15; //seconds to stay open
timeout = 0;

function myClose2()
{
	timeout = setTimeout('suggestOff2()',stayAlive * 1000);
}

function resettime2()
{
	clearTimeout(timeout);
	myClose2();
}

//Mouse over function
function suggestOver2(div_value) {
	div_value.className = 'suggest_link_over';
	document.getElementById('search_suggest2').className = 'visible';
}
//Mouse out function
function suggestOut(div_value) {
	div_value.className = 'suggest_link';
}

function suggestOff2() 
{
	document.getElementById('search_suggest2').innerHTML = '';
	document.getElementById('search_suggest2').className = 'invisible';
}

//Click function
function setSearch2(value) {
	value2 = value.replace("&amp;","&");
	document.getElementById('txtSearch2').value = value2;
	document.getElementById('search_suggest2').innerHTML = '';
	document.getElementById('search_suggest2').className = 'invisible';
}