/*
	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 Sucks!\nIt's about time to upgrade don't you think?");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();

//Called from keyup on the search textbox.
//Starts the AJAX request.
function searchSuggest() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById('txtSearch').value);
		if (document.getElementById('search_type').value == 'home') {
			var stype = document.getElementById('selecttype').value;  //City, Hotel or Attraction
			searchReq.open("GET", '/admin/searchsuggest.php?stype=' + stype + '&page=home&sid=0&search=' + str, true);
		} else if (document.getElementById('search_type').value == 'state_page') {
			//*******************************************************
			//* Handle State Page
			//*******************************************************
			var stype = document.getElementById('selecttype').value;  //City, Hotel or Attraction
			var stateid = document.getElementById('sid').value;
			searchReq.open("GET", '/admin/searchsuggest.php?stype=' + stype + '&page=state_page&sid=' + stateid + '&search=' + str, true);
		} else if (document.getElementById('search_type').value == 'country_page') {
			//*******************************************************
			//* Handle Country Page
			//*******************************************************
			var stype = document.getElementById('selecttype').value;  //City, Hotel or Attraction
			var countryid = document.getElementById('sid').value;
			searchReq.open("GET", '/admin/searchsuggest.php?stype=' + stype + '&page=country_page&sid=' + countryid + '&search=' + str, true);
		} else if (document.getElementById('search_type').value == 'city_page') {
			//*******************************************************
			//* Handle City Page
			//*******************************************************
			var stype = document.getElementById('selecttype').value;  //City, Hotel or Attraction
			var cityid = document.getElementById('sid').value;
			searchReq.open("GET", '/admin/searchsuggest.php?stype=' + stype + '&page=city_page&sid=' + cityid + '&search=' + str, true);
		} else if (document.getElementById('search_type').value == 'hotel_page') {
			//*******************************************************
			//* Handle Hotel Page
			//*******************************************************
			var stype = document.getElementById('selecttype').value;  //City, Hotel or Attraction
			var hotelid = document.getElementById('sid').value;
			searchReq.open("GET", '/admin/searchsuggest.php?stype=' + stype + '&page=hotel_page&sid=' + hotelid + '&search=' + str, true);
		} else if (document.getElementById('search_type').value == 'attraction_page') {
			//*******************************************************
			//* Handle Attraction Page
			//*******************************************************
			var stype = document.getElementById('selecttype').value;  //City, Hotel or Attraction
			var attractionid = document.getElementById('sid').value;
			searchReq.open("GET", '/admin/searchsuggest.php?stype=' + stype + '&page=attraction_page&sid=' + attractionid + '&search=' + str, true);
		} else { 
			// bad parameter
			return;
		}

		searchReq.onreadystatechange = handleSearchSuggest; 
		searchReq.send(null);
	}		
}

//Called when the AJAX response is returned.
function handleSearchSuggest() {
	if (searchReq.readyState == 4) {
		
		if (document.getElementById('search_suggest')) {
			var ss = document.getElementById('search_suggest');
		} else {
			var ss = document.getElementById('search_suggest_home');
		}
		ss.innerHTML = '';
		var str = searchReq.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.
			if (document.getElementById('search_type').value == 'city_search_home') {
				var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
				suggest += 'onmouseout="javascript:suggestOut(this);" ';
				suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
				suggest += 'class="suggest_link">' + str[i] + '</div>';
				ss.innerHTML += suggest;
			} else {
				var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
				suggest += 'onmouseout="javascript:suggestOut(this);" ';
				suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
				suggest += 'class="suggest_link_sub">' + str[i] + '</div>';
				ss.innerHTML += suggest;
			}
		}
	}
}

//Search Type Set
function searchTypeSet(stype) {
	// first set the Prompting Text in Search Box
	// stype is either City, Hotel, Attraction
	document.getElementById('txtSearch').value = 'Enter ' + stype;
	document.getElementById('txtSearch').style.color = '#999999';
	document.getElementById('selecttype').value = stype;
}

//Mouse over function
function suggestOver(div_value) {
	div_value.className = 'suggest_link_over';
}
//Mouse out function
function suggestOut(div_value) {
	div_value.className = 'suggest_link';
}
//On Click
function actionClick() {
	// Click to remove the default "Enter City, Hotel or Attraction"

	var strval = document.getElementById('txtSearch').value;
	if ((strval == 'Enter City') || (strval == 'Enter Hotel') || (strval == 'Enter Attraction')) {
		document.getElementById('txtSearch').value = '';
		document.getElementById('txtSearch').style.color = '#000000';
	} else {
		document.getElementById('txtSearch').style.color = '#000000';
	}
}

//On Off Focus
function actionBlur() {
	// Click to remove the default "Enter City"
	type = document.getElementById('selecttype').value;
    
	var strval = document.getElementById('txtSearch').value;
	if (strval.length == 0) {
		document.getElementById('txtSearch').value = 'Enter ' + type;
		document.getElementById('txtSearch').style.color = '#999999';
	} 
}

//Action Keypress
function actionKeypress(evt) {
	// if Escape
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode == 27) document.getElementById('txtSearch').value = '';  // clear box
}

//Click function
function setSearch(value) {
	document.getElementById('txtSearch').value = value.replace(/<.*?>/g, ''); // removes html
	if (document.getElementById('search_suggest')) {
		document.getElementById('search_suggest').innerHTML = '';
	} else {
		document.getElementById('search_suggest_home').innerHTML = '';
	}
}
