﻿function SearchInput(searchString)
{
	document.write("<br><div align=\"center\"><table border=\"0\" width=\"380\" align=\"center\">");
	document.write("<tr><td width=\"100%\"><b><font face=\"Verdana\" size=\"2\">Search this site for</font></b></td>");
	document.write("</tr><tr><td width=\"100%\">");
	document.write("<form name=\"entryform\" method=\"get\"><input type=\"text\" size=20 name=\"q\" value=\"" + (searchString ? htmlEncode(searchString) : "") + "\">");
	document.write("<input type=\"submit\" value=\"Search\">&nbsp;");
	document.write("</form><hr size=\"2\" noshade>");
	document.write("</td></tr></table>");
}

if (document.location.search)
{
	var arrResults = new Array();
	var searchString;
	var arrSearchTerms;

	var arrMatch = document.location.search.match(/q=([^&]+)/i
);

	if (arrMatch && arrMatch.length == 2)
	{
		searchString = decodeURIComponent(arrMatch[1]).replace(/\+/g, " ");
	}
	else
	{
		arrMatch = document.location.search.replace(/^\?/, "").split("&");
		if (arrMatch)
		{
			searchString = decodeURIComponent(arrMatch.shift()).replace(/\+/g, " ");
		}
		else
		{
			searchString = decodeURIComponent(document.location.search.replace(/^\?/, "")).replace(/\+/g, " ");
		}
	}

	if (searchString && searchString.length > 0)
	{
		arrSearchTerms = searchString.split(/\s+/);

		for(var searchTerm in arrSearchTerms)
		{
			var term = new Object();
			term.term = arrSearchTerms[searchTerm];
			
			if (/^-/.test(term.term))
			{
				term.exclude = true;
				term.re = new RegExp(term.term.replace(/^-/,""), "ig");
			}
			else
			{
				term.exclude = false;
				term.re = new RegExp(term.term, "ig");
			}

			arrSearchTerms[searchTerm] = term;
		}

		var dbFrm = document.forms["database"];
		if (dbFrm)
		{
			var listInput = dbFrm.elements["list"];
			if (listInput)
			{
				var keywordList = listInput.value;
				if (keywordList)
				{
					var arrKeywordList = keywordList.split("*");					

					for(var keywordEntry in arrKeywordList)
					{
						var arrKeyMatch = arrKeywordList[keywordEntry].match(/^([^~]*)~([^\|]*)\|([^\^]*)\^(.*)$/);
						if (arrKeyMatch)
						{
							var termMatch = true;

							for(var searchTerm in arrSearchTerms)
							{
								var term = arrSearchTerms[searchTerm];


								var reSearch = term.re;

								if (arrKeyMatch[2].search(reSearch) >= 0 || arrKeyMatch[3].search(reSearch) >= 0 || arrKeyMatch[4].search(reSearch) >= 0)
								{
									if (term.exclude)
										termMatch = false;
									else
										continue;
								}
								else
								{
									if (term.exclude)
										continue;
									else
										termMatch = false;
								}

								break;
							}

							if (termMatch)
							{
								var result = new Object();
								result.url = arrKeyMatch[1];
								result.title = arrKeyMatch[2];
								result.description = arrKeyMatch[4];
								arrResults.push(result);
							}
						}
					}
				}
			}
		}			
	}

	SearchInput(searchString);

	if (arrResults.length == 0)
	{
		document.write("<font face=\"Verdana\" size=\"2\">Search for \"" + htmlEncode(searchString) + "\" produced no results</font>");
	}
	else
	{
		var visited = new Object();

		document.write("<table width=\"100%\"><tr><td><font face=\"Verdana\" size=\"2\">&nbsp;Results - " + arrResults.length + " matches found</font><br><hr size=\"1\" noshade>");

		for(var r in arrResults)
		{
			if (!visited[arrResults[r].url])
			{
				document.write("<font face=\"Arial\" size=\"2\"><b><a href=\"" + arrResults[r].url + "\">" + arrResults[r].title + "</a></b><br>" + hilite(arrResults[r].description, searchString) + "</font><br><br>");

				visited[arrResults[r].url] = true;
			}
		}

		document.write("</td></tr></table>");
	}
}
else
{
	SearchInput();
}

function hilite(content, terms)
{
	var arrTerms = terms.split(/\s+/);

	for(var term in arrTerms)
	{
		if (!/^-/.test(arrTerms[term]))
		{
			var re = new RegExp(regExEncode(arrTerms[term]), "ig");

			content = content.replace(re, function() { return "<font color=\"red\">" + arguments[0] + "</font>"; });
		}
	}

	return content;
}

function htmlEncode(str)
{
    return new String(str).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;"); 
}

function regExEncode(str)
{
	return new String(str).replace(/\$|\(|\)|\*|\+|\.|\[|\]|\?|\\|\/|\^|\{|\}|\|/g, function() { return "\\" + arguments[0]; });
}
