// Suggest-o-matic, by Mark Willis of Roxboxonline.com
// December 30th, 2008.

var docRoot = "http://www.forza3cars.com/inc/mLib/mSuggest/";
var boxactive = 0;

function mHtmlspecialchars(p_string)
{
	p_string = p_string.replace(/&/g, '&amp;');
	p_string = p_string.replace(/</g, '&lt;');
	p_string = p_string.replace(/>/g, '&gt;');
	p_string = p_string.replace(/"/g, '&quot;');
	p_string = p_string.replace(/'/g, '&#039;');
	return p_string;
}

function mShowSugBox()
{
	if(document.mSuggestForm.mSearchSuggestion.value == "Search..."){document.mSuggestForm.mSearchSuggestion.value = '';}
	
	document.getElementById('mSuggestBox').style.display = "block";
	var theWidth = (document.getElementById('mSearchSuggestion').offsetWidth - 2) + 'px';
	document.getElementById('mSuggestBox').style.width = theWidth;
	return false;
}
	
function mHideSugBox()
{
	if(boxactive == 0){
	document.getElementById('mSuggestBox').style.display = "none";}
	return false;
}
	
function mSuggestData()
{
	var userInput = mHtmlspecialchars(document.getElementById('mSearchSuggestion').value);
	mSuggestGo(userInput);
}

function mSuggestGo(userInput)
{
	var d = new Date();
	var tsec = d.getSeconds();

	var ajaxRequest;
	try{ajaxRequest = new XMLHttpRequest(); } catch (e){try{ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {try{ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");} catch (e){return true;}}}
	ajaxRequest.onreadystatechange = function()
	{
	if(ajaxRequest.readyState == 4){
	var resulten = ajaxRequest.responseText;
	if(resulten == "")
	{
		document.getElementById('mSuggestBox').innerHTML = '<div class="mSuggestContent">No matches found.</div>';
	}else{
		document.getElementById('mSuggestBox').innerHTML = '<div class="mSuggestContent">' + resulten + '</div>';
	}
	}
	}
	var queryString = docRoot + 'mSuggest.php?data=' + userInput + '&time=' + tsec;
	ajaxRequest.open("GET", queryString, true);
	ajaxRequest.send(null);
}

function mActivateBox()
{
	boxactive = 1;
}
function mDeactivateBox()
{
	boxactive = 0;
}

/* Actions trigger */
function mSearchAction(aid)
{
	switch(aid)
	{
		case 1: mShowSugBox(); break;
		case 2: mHideSugBox(); break;
		case 3: mSuggestData(); break;
		case 4: mActivateBox(); break;
		case 5: mDeactivateBox(); break;
	}
}


