// *******************************************************
// * Seach Engine Functions
// *******************************************************

// This code does not use the functions search, replace or
// match to improve browser compatibility.

var arrSearchEngines = new Array();
var MAX_ENGINE_COUNT = 20;
var SEARCH_TEXT_POSITION_MARKER = "SearchForText";

function BuildSearchEngineList()
{
	http://www.altavista.com/cgi-bin/query?q=searchtext&kl=XX&pg=q&Translate=on&search.x=36&search.y=15
	AddSearchEngine("AltaVista", "", "http://www.altavista.com/", "http://www.altavista.com/cgi-bin/query?q=SearchForText");
	AddSearchEngine("Anzwers", "", "http://www.anzwers.com.au/", "http://www.anzwers.com.au/cgi-bin/process_search.pl?pageid=search&firstresult=0&query=SearchForText&location=The+World&query_type=all+the+words&numperpage=10&result_type=detailed");
	AddSearchEngine("Deja", "", "http://www.deja.com/", "http://www.dejanews.com/dnquery.xp?QRY=SearchForText&DBS=2&defaultOp=AND&maxhits=20&ST=QS&format=terse");
	AddSearchEngine("Excite", "", "http://www.excite.com/", "http://www.excite.com/search.gw?search=SearchForText&lk=excite_au&c=web&trace=a");
	AddSearchEngine("FastSearch", "",    "http://www.alltheweb.com/", "http://www.alltheweb.com/cgi-bin/search?type=all&query=SearchForText");
	AddSearchEngine("Google", "selected", "http://www.google.com/", "http://www.google.com/search?q=SearchForText");
	AddSearchEngine("HotBot", "", "http://www.hotbot.com/", "http://www.hotbot.com/?MT=SearchForText&submit=SEARCH&SM=MC&DV=0&RG=all&RD=RG&DC=10&DE=2&_v=2&OPs=MDRTP");
	AddSearchEngine("Infoseek", "", "http://infoseek.go.com/", "http://infoseek.go.com/Titles?col=WW&qt=SearchForText&svx=home_searchbox&sv=IS&lk=noframes");
	AddSearchEngine("LookSmart", "", "http://www.looksmart.com.au/", "http://www.looksmart.com.au/r_search?look=&key=SearchForText&search=0");
	AddSearchEngine("Lycos", "", "http://www.lycos.com/", "http://www.lycos.com/cgi-bin/pursuit?matchmode=and&cat=lycos&query=SearchForText");
	AddSearchEngine("MetaCrawler", "", "http://www.metacrawler.com/", "http://www.metacrawler.com/cgi-bin/nph-metaquery.p?general=SearchForText&logic=0®ion=The+World&orgType=Any&maxwait=1&score=0");
	AddSearchEngine("MSN (Microsoft)", "",    "http://www.msn.com/", "http://search.msn.com/results.asp?co=20&RS=CHECKED&FORM=MSNH&ba=0&v=1&q=SearchForText");
	AddSearchEngine("Northern Light", "", "http://www/northernlight.com/", "http://www.northernlight.com/nlquery.fcg?cb=0&qr=SearchForText&orl=");
	AddSearchEngine("SavvySearch", "", "http://www.savvysearch.com/", "http://www.savvysearch.com/search?q=SearchForText&cat=11&op=a");
	AddSearchEngine("WebCrawler", "", "http://www.webcrawler.com", "http://www.webcrawler.com/cgi-bin/WebQuery?searchText=SearchForText");
	AddSearchEngine("Yahoo", "", "http://www.yahoo.com/", "http://search.yahoo.com/bin/search?&y=y&p=SearchForText");
}

function AddSearchEngine(strName, strOptions, strHomeURL, strURLWithQueryString)
{
	var numEngineCount = arrSearchEngines.length;

	if (numEngineCount <= MAX_ENGINE_COUNT)
	{
		arrSearchEngines[numEngineCount] = new Engine(strName, strOptions, strHomeURL)

		var numSearchForTextPosition = LocateSubString(SEARCH_TEXT_POSITION_MARKER, strURLWithQueryString);

		arrSearchEngines[numEngineCount].numPreQueryStringPosition = strURLWithQueryString.substring(0, numSearchForTextPosition);
		arrSearchEngines[numEngineCount].numPostQueryStringPosition = strURLWithQueryString.substring(numSearchForTextPosition+SEARCH_TEXT_POSITION_MARKER.length, strURLWithQueryString.length);
	}
	else
	{
		alert("Too many Search arrSearchEngines were added. Need to increase MAX_ENGINE_COUNT: " + arrSearchEngines.length + ">" + MAX_ENGINE_COUNT)
	}
}

function DoSearchEngineQuery(frmForm)
{
	var i=0, strSearchForText=frmForm.SearchForText.value, strQueryText='';

	//alert("SearchForText entered:" + escape(strSearchForText));

	// Trim any leading or trailing spaces
	strSearchForText = TrimSpaces(strSearchForText);

	// Replace any carriage return line feed charaters pairs
	strSearchForText = ReplaceString(strSearchForText, '\r\n', ' ');

	// Replace any 2 spaces with 1 space
	strSearchForText = ReplaceString(strSearchForText, '  ', ' ');

	// Replace spaces with +
	strQueryText = ReplaceString(strSearchForText, ' ', '+');

	var objSearchEngineSelected = arrSearchEngines[frmForm.SearchEngine.selectedIndex];

	//alert("SearchForText to be submitted:" + escape(strQueryText));

	OpenWindow(objSearchEngineSelected.numPreQueryStringPosition + strQueryText + objSearchEngineSelected.numPostQueryStringPosition, 600, 400)
}

function ReplaceString(strStringToSearch, strFindString, strReplaceWithString)
{
	var strSrchInString = strStringToSearch

	for (var i=0; i <= strSrchInString.length; i++)
	{
		if (strSrchInString.substring(i, i + strFindString.length) == strFindString)
			strSrchInString = strSrchInString.substring(0, i) + strReplaceWithString + strSrchInString.substr(i + strFindString.length);
	}
	return strSrchInString;
}

function Engine(strName, strOptions, strHomeURL)
{
	this.name = strName;
	this.options = strOptions;
	this.home = strHomeURL;
	this.numPreQueryStringPosition=0;
	this.numPostQueryStringPosition=0;
}

function OpenWindow(strHref, numWidth, numHeigth)
{
	var strWindowAttributes;
	var resultWin;

	if (is_ie4up)
	{
		resultWin = window.open(strHref,"Search_Results");
		//resultWin=window.open(strHref, "Search_Results", "width=600, height=400, resizable=yes, menubar=yes, scrollbars=yes, toolbar=yes, location=yes, directories=yes, status=yes");
	}
	else if (is_nav4up)
	{
		strWindowAttributes = "width=" + numWidth + ",height=" + numHeigth + ",toobar=yes,menubar=yes,scrollbars=yes,resizable=yes,status=yes,location=yes,directories=yes,copyhistory=yes";
		resultWin = window.open(strHref,"Search_Results",strWindowAttributes);
	}
	else
		window.location.href = strHref;
}

function LocateSubString(strSearchForText, strTextToSearch)
{
	for (var i=0; i<=strTextToSearch.length; i++)
	{
		if (strSearchForText == strTextToSearch.substring(i, i + strSearchForText.length))
			return i;
	}
	return false;
}

function TrimSpaces(strText)
{
	var i=0;

	// trim leading spaces
	if (strText.length > 0)
	{
		while (strText.charAt(i) == ' ')
			i++;

		strText = strText.substr(i);
	}

	// trim trailing spaces
	if (strText.length > 0)
	{
		i=strText.length - 1;

		while (strText.charAt(i) == ' ')
			i--;

		strText = strText.substr(0, i + 1);
	}
	return strText;
}


