/*
	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("Your browser is not compatible with this function!\nIt's time to upgrade..");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();

//Called from keyup on the search textbox.
//Starts the AJAX request.
function searchSuggest(lngLangID) {
	if (searchReq.readyState != 4) {
		var str = document.getElementById('frmSearchField').value;
		if(str.length > 2 )
		{
		var status = document.getElementById('searchWindowHeader')
		status.innerHTML = document.getElementById('searchWindowHeaderRep1').innerHTML;
		
		var sr = document.getElementById('searchWindow')
		var results = document.getElementById('searchResponse')
		sr.style.display = 'block';
		results.style.minHeight = '441';
		var myString = results.innerHTML;
		if (myString.indexOf("loading.gif")==-1)
			{
			results.innerHTML = results.innerHTML + '<div style="width: 900px; position: absolute; z-index: 10; background: #fff;" class="transparent_class"><img src="../layout/loading.gif" width="70" height="64" style="margin: 190px auto 190px 415px; width: 70px;"></div>';
			}
		}
	}
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		//var str = escape(document.getElementById('frmSearchField').value);
		var str = document.getElementById('frmSearchField').value;
		if(str.length > 2 )
		{
		searchReq.open("GET", '../searchResponse2.asp?lngLangID='+ lngLangID +'&frmSearchField=' + str, true);
		searchReq.onreadystatechange = handleSearchSuggest; 
		searchReq.send(null);
		}
	}
	else
	{
		if(document.getElementById('searchResponse').innerHTML.length==0)
		{
		var sr = document.getElementById('searchWindow')
		var results = document.getElementById('searchResponse')
		sr.style.display = 'block';
		results.style.minHeight = '441';
		results.innerHTML = results.innerHTML + '<div style="width: 900px; position: absolute; z-index: 10; background: #fff;" class="transparent_class"><img src="../layout/loading.gif" width="70" height="64" style="margin: 190px auto 190px 415px; width: 70px;"></div>';
		
		}
	}
}

//Called when the AJAX response is returned.
function handleSearchSuggest() {
	
	if (searchReq.readyState == 4) {
		var sr = document.getElementById('searchWindow')
		var results = document.getElementById('searchResponse')
		var status = document.getElementById('searchWindowHeader')
		sr.style.display = 'block';
		results.innerHTML = '';
		var str = searchReq.responseText;
		
			results.innerHTML = str;
		status.innerHTML = document.getElementById('searchWindowHeaderRep2').innerHTML;
	}
	
}
