var filter='';
var itemCount=0;
var awardNumberIndex=0;
var nameIndex=1;
var amountIndex=2;
var agencyIndex=3;
var sourceIndex=4;
var awardStatusIndex=5;
var einIndex=6;

document.write('<table cellspacing="0" cellpadding="0" border="0" width="499" id="search_controls">');
document.write('<Tr><td class="label" width="177">Search by name (5 character minimum) or EIN (omit "-"):</td>');
document.write('<td class="field" width="300"><input type="text" name="search_term" id="search_term" style="width:300px;"></td></Tr>');
document.write('<tr><Td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="15" border="0" alt=""></Td></tr>');
document.write('<tr><td colspan="2" class="search_button_td"><input type="button" onClick="showSearching();" style="width:82px;" value="Search" class="search_button"></td></tr></table>');
document.write('<table cellspacing="0" cellpadding="0" border="0" width="499" class="search_bg" id="searchPopup">');
document.write('<tr valign="middle"><td align="center" height="69">Searching</td></tr></table>');
document.write('<table cellspacing="0" cellpadding="0" border="0" width="499"><tr><td><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="15" border="0" alt=""></td></tr>');
document.write('<tr><td id="results_div"><table cellspacing="0" cellpadding="0" border="0" width="499">');
document.write('<tr><Td id="results_header" class="results_heading"></td></tr><tr><Td id="results_span"></td></tr></table></td></tr></table>');
hideSearching();

function clearResults(){
	itemCount=0;
	document.getElementById("results_div").style.display="none";
	document.getElementById("results_header").innerHTML="";
	document.getElementById("results_span").innerHTML="";
}

function isValid(theIndex){
	var valid=false;
	valid=valid||getEin(theIndex)==filter;
	valid=valid||getName(theIndex).toLowerCase().indexOf(filter.toLowerCase())>-1;
	return(valid);
}

function trim(str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}

function writeResults(){
	var searchTerm=trim(document.getElementById('search_term').value);
	filter=searchTerm;
	var resultsMessage='You must enter a search term.';
	var theResults=''
	if(filter.length>4){
		resultsMessage='<span class="resultsMessage">Search Results for &quot;'+filter+'&quot;</span>';
		var theResults='<table cellspacing="0" cellpadding="0" border="0" width="499">';
		theResults=theResults+'<tr><td id="resultsTd">'+results()+'</td></tr></table>';
	}
	if(itemCount==0){
		theResults='<table cellspacing="0" cellpadding="0" border="0" width="499">';
		theResults=theResults+'<tr><td id="resultsTd">No results found</td></tr></table>';
	}
	document.getElementById('results_header').innerHTML=resultsMessage;
	document.getElementById('results_span').innerHTML=theResults;
	document.getElementById("results_div").style.display=""; 
	hideSearching();
}
function hideSearching(){
	document.getElementById("searchPopup").style.display = "none";
	document.getElementById("search_controls").style.display="";
}
function showSearching(){
	clearResults();
	document.getElementById("searchPopup").style.display = "";
	document.getElementById("search_controls").style.display="none";
	setTimeout(writeResults,10);
}

function results(){
	var theString='<table cellspacing="0" cellpadding="0" border="0" width="499">';
	for(var i=0;i<datasets.length;i++){
		if(isValid(i)){
			theString=theString+'<Tr><td class="results_label" colspan="2">'+getName(i)+'</td></tr>';
			theString=theString+'<tr><Td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="3" border="0" alt=""></Td></tr>';
			theString=theString+'<Tr><td class="label" width="177">Award Number:</td><td width="300" class="field_text">'+getAwardNumber(i)+'</td></tr>';
			theString=theString+'<tr><Td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="3" border="0" alt=""></Td></tr>';
			theString=theString+'<Tr><td class="label" width="177">Amount:</td><td width="300" class="field_text">'+getAmount(i)+'</td></tr>';
			theString=theString+'<tr><Td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="3" border="0" alt=""></Td></tr>';
			theString=theString+'<Tr><td class="label" width="177">Agency:</td><td width="300" class="field_text">'+getAgency(i)+'</td></tr>';
			theString=theString+'<tr><Td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="3" border="0" alt=""></Td></tr>';
			theString=theString+'<Tr><td class="label" width="177">Source:</td><td width="300" class="field_text">'+getSource(i)+'</td></tr>';
			theString=theString+'<tr><Td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="3" border="0" alt=""></Td></tr>';
			theString=theString+'<Tr><td class="label" width="177">Award Status:</td><td width="300" class="field_text">'+getAwardStatus(i)+'</td></tr>';
			theString=theString+'<tr><Td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="10" border="0" alt=""></Td></tr>';
			itemCount++;
		}
	}
	theString=theString+"</table>";
	return(theString);
}

function convertCurrency(theNumber){
	var converted="";
	if(theNumber.indexOf(".")>-1){
		converted=theNumber.substring(theNumber.indexOf("."));
		theNumber=theNumber.substring(0,theNumber.indexOf("."));
	}
	var j=0;
	for(var i=theNumber.length;i>0;i--){
		if(j>0&&j%3==0){
			converted=","+converted;
		}
		converted=theNumber.charAt(i-1)+converted;
		j++;
	}
	converted="$"+converted;
	return(converted);
}

function getAwardNumber(theIndex){
	return(trim(datasets[theIndex][awardNumberIndex]));
}

function getName(theIndex){
	return(trim(datasets[theIndex][nameIndex]));
}

function getAmount(theIndex){
	return(convertCurrency(trim(datasets[theIndex][amountIndex])));
}

function getAgency(theIndex){
	return(trim(datasets[theIndex][agencyIndex]));
}

function getSource(theIndex){
	return(trim(datasets[theIndex][sourceIndex]));
}

function getAwardStatus(theIndex){
	return(trim(datasets[theIndex][awardStatusIndex]));
}

function getEin(theIndex){
	return(trim(datasets[theIndex][einIndex]));
}
