var alt = false;
var targetBuffer;
var lastSearch = "";
var c = null;
var sorted = false;
var mostRecentSearchId = 0;

var timeID = null
function stage1(text, attribute)
{
    window.clearTimeout(timeID)
    thisSearchId = ++mostRecentSearchId;
    x = function() { stage2(text, attribute, thisSearchId); };
    timeID = window.setTimeout("x()", 300/text.length);
}

var searchCount = 0;

function stage2(text, attribute, id)
{
	if (mostRecentSearchId == id)
	{
		searchCount++;
		findPart(text, attribute);		
		insertAdjacentHTML2("stdout", "Search count = " + searchCount);
	} else {
		//window.status = "search nuked";
		insertAdjacentHTML2("stdout", id);
		// nuked!
	}
}
var stillID;
function findPart(text, attribute)
{
	if (/^\#/.test(text)) {
    text = text.replace(/^\#/, '');
    attribute = 'number';
  }
  targetBuffer = "";
	if( c != null && lastSearch != text )
	{
		if( !sorted )
		{
			c.sort(c[2].compare)
			sorted = true;
		}
		lastSearch = text;
		document.getElementById("target").innerHTML = "";//<table><tr id="trTarget"><!--CoMmEnT--></tr></table>"
		var maxRecords = 30;
		//window.status="";
		//debugger
		var keywordReg = /(?:"+[\w\s)]+"+|\b\w+\b)/g;
		var temp = text.match(keywordReg)
		//var temp = "\"dice mice\" spice \"white rice\"".match(keywordReg)
		//window.status = temp;
		if( text.replace(/\W/g, "").length > 0 )
		{
			//var reg = new RegExp();
			//reg.compile(text, "i");
			var tempReg;
			var i;
			var show;
			var foundRecords = 0;
			for( i = 0; i < temp.length; i++ )
			{
				tempReg = new RegExp();
				tempReg.compile(temp[i].replace(/"/g, ""), "i");
				temp[i] = tempReg;
			}
			for( i = 0; foundRecords < maxRecords && i < c.length; i++ )//100; i++ )
			{				
				//var reg = /d/;
				//reg.
				if( c[i] )
				{
					show = 0;
					for( var j = 0; j < temp.length; j++ )
					{
						if( temp[j].test(c[i][attribute]) )//c[i][attribute].search(reg) > -1 )
						{
							show ++;
						}
					}
					if( show == temp.length )
					{
						//window.status += i + ", ";
						c[i].show();
						foundRecords++;
					}
				}
			}
			with( document.getElementById("target") )
			{
				insertAdjacentHTML2("target", "<table cellspacing='0' cellpadding='1' border='0'>" + targetBuffer + "</table>")
			}
			if( foundRecords == maxRecords )
			{
				insertAdjacentHTML2("target2", " <span style='font-size:16px;font-weight:bold;color:red;background-color:white;'> Partial&nbsp;Results </span> ")
				//window.status = "Partial Results: " + window.status;
			}
			else
			{
				with( document.getElementById("target") )
				{
					insertAdjacentHTML2("target2", " <span style='font-size:16px;font-weight:bold;color:white;'> Search&nbsp;Results&nbsp;" + foundRecords +" </span> ");
				}
			}
			document.getElementById("searchResults").style.display = "inline";
		}
		else
		{
			document.getElementById("searchResults").style.display = "none";
			/*insertAdjacentHTML2("target", "")
			insertAdjacentHTML2("target2", "")
			insertAdjacentHTML2("stdout", "")
			insertAdjacentHTML2("afterBegin", "")*/
		}
	}
	if( c == null )
	{
		stillID = window.setInterval("stillLoading()", 1000)
		stillLoading();
	}
	else
	{
		clearInterval(stillID)
	}
}
function cancelSubmit()
{
	return false;
}
function insertAdjacentHTML2(location, text)
{
	document.getElementById(location).innerHTML = text;
}

function stillLoading()
{
	with( document.getElementById("target") )
	{
		innerHTML = "";
		insertAdjacentHTML2("afterBegin", "<h3 style='color:red;'>Still Loading</h3>");
	}
}
