var filter_service='';
var filter_company='';
var filter_neighborhood='';
var filter_mwbe='';
var pages=0;
var itemCount=0;
var resultsPerPage=20;
var currentPage=0;
var companyIndex=0;
var addressIndex=1;
var cityIndex=2;
var stateIndex=3;
var zipIndex=4;
var phoneIndex=5;
var faxIndex=6;
var emailIndex=7;
var webIndex=8;
var contactIndex=9;
var titleIndex=10;
var serviceIndex=11;
var bidIndex=12;
var certifiedIndex=13;
var mwbeIndex=14;

var mwbeText="M/WBE";
var lbeText="LBE";

var theSpans=new Array();
var aNames=new Array();
for(var i=0;i<vendors.length;i++){
	aNames[i]=vendors[i][companyIndex];
}

var neighborhoods=new Array();
for(var i=0;i<vendors.length;i++){
	if(!neighborhoodsContains(vendors[i][cityIndex])){
		neighborhoods[neighborhoods.length]=vendors[i][cityIndex];
	}
}
neighborhoods=neighborhoods.sort();

function neighborhoodsContains(theEntry){
	var contains=false;
	for(var j=0;j<neighborhoods.length;j++){
		contains=contains||neighborhoods[j].toLowerCase()==theEntry.toLowerCase();
	}
	return(contains);
}

function filterService(){
	filter_service=document.getElementById("services").value;
}
function filterCompany(){
	filter_company=document.getElementById("company").value;
}

function filterNeighborhood(){
	filter_neighborhood=document.getElementById("neighborhood").options[document.getElementById("neighborhood").selectedIndex].value;
}

function filterMWBE(){
	filter_mwbe=document.getElementById("mwbe").options[document.getElementById("mwbe").selectedIndex].value;
}

function writeDropdowns(){
	document.write('<table cellspacing="0" cellpadding="0" border="0" width="499"><tr><td class="terms_table"><table cellspacing="0" cellpadding="0" border="0"><form onsubmit="writeResults();return(false);">');

	document.write('<tr><td align="right" nowrap class="terms_label" width="146">Company:</td>');
    document.write('<td class="terms_field" width="298"><input type="text" class="textbox" name="company" id="company" style="width:298px;" onfocus="this.select()"></td></tr>');
	document.write('<tr><Td colspan="2"><img src="/portal/images/spacers/spacer.gif" width="452" height="7" /></td></tr>');
	
	document.write('<tr><td align="right" nowrap class="terms_label" width="146">Product/Service:</td>');
	document.write('<td class="terms_field" width="298"><input type="text" class="textbox" name="services" id="services" style="width:298px;"></td></tr>');
	document.write('<tr><Td colspan="2"><img src="/portal/images/spacers/spacer.gif" width="452" height="7" /></td></tr>');

	document.write('<tr><td align="right" nowrap class="terms_label" width="146">Neighborhood:</td>');
	document.write('<td class="terms_field" width="298"><select name="neighborhood" id="neighborhood" class="textbox" style="width:298px;">');
	document.write('<option value=""></options>');
	for(var i=0;i<neighborhoods.length;i++){
		document.write('<option value="'+neighborhoods[i]+'">'+neighborhoods[i]+'</option>');
	}
	document.write('</select></td></tr>');
	document.write('<tr><Td colspan="2"><img src="/portal/images/spacers/spacer.gif" width="452" height="7" /></td></tr>');

	document.write('<tr><td align="right" nowrap class="terms_label" width="146">M/WBE or LBE:</td>');
	document.write('<td class="terms_field" width="298"><select name="mwbe" id="mwbe" class="textbox" style="width:298px;">');
	document.write('<option value=""></options>');
	document.write('<option value="'+mwbeText+'">'+mwbeText+'</options>');
	document.write('<option value="'+lbeText+'">'+lbeText+'</options>');
	document.write('</select></td></tr>');
	
	document.write('<tr><Td colspan="2"><img src="/portal/images/spacers/spacer.gif" width="452" height="10" /></td></tr>')

	document.write('<tr><td colspan="2" align="right"><input type="button" class="search_button" value="Find Providers" style="width:100px;" onClick="writeResults()">&nbsp;<input type="reset" class="search_button" value="Reset" style="width:50px;" onclick="clearResults()"></td></tr>');
	document.write('</form></table></td></tr></table>');
	document.write('<div id="results_div" class="results_div"><table cellspacing="0" cellpadding="0" width="499" border="0"><tr><td class="results_header" colspan="2" id="results_header">Search Results:</td></tr><tr><td id="results_info" class="resultsMessage"></td><td id="topLinks" class="resultsMessage" align="right"></td></tr>');
	document.write('<tr><Td colspan="2"><img src="/portal/images/spacers/spacer.gif" width="499" height="12" /></td></tr>')
	document.write('<tr><td colspan="2" id="resultsSpan"></td></tr>');
	document.write('<tr><Td colspan="2"><img src="/portal/images/spacers/spacer.gif" width="452" height="10" /></td></tr>');
	document.write('<tr><td colspan="2" align="right" id="bottomLinks" class="resultsMessage"></td></tr>');
	document.write('<tr><td colspan="2"><img src="/portal/images/spacers/spacer.gif" width="499" height="15" /></td></tr></table></div>');
	document.getElementById("results_div").style.display="none";
}
function clearResults(){
	document.getElementById("results_div").style.display="none";
	document.getElementById("resultsSpan").innerHTML="";
	document.getElementById("results_info").innerHTML="";
	filter_service='';
	filter_company='';
	document.getElementById('topLinks').innerHTML="&nbsp;";
	document.getElementById('bottomLinks').innerHTML="&nbsp;";
}
function clearNonOrg(){
	if(document.getElementById('company').value.length>0){
		document.getElementById('services').selectedIndex=0;
	}
}
function clearFields(){
	document.getElementById('services').value='';
	document.getElementById('company').value='';
	document.getElementById('neighborhood').selectedIndex=0;
	document.getElementById('mwbe').selectedIndex=0;
}
function isValid(theIndex){
	var valid=true;
	if(filter_company.length>0){
		if ((getCompany(theIndex).toLowerCase()).indexOf(filter_company.toLowerCase()) != -1) {
			valid=valid&&true;
		} else {
			valid=valid&&false;	
		}
	}else{
		if(filter_service.length>0){
			valid=valid&&getCategory(theIndex).toLowerCase().indexOf(filter_service.toLowerCase())>-1;
		}
		if(filter_neighborhood.length>0){
			valid=valid&&getNeighborhood(theIndex).toLowerCase().indexOf(filter_neighborhood.toLowerCase())>-1;
		}
		if(filter_mwbe.length>0){
			if(filter_mwbe==mwbeText){
				valid=valid&&getMWBE(theIndex).length>0;
			}else if(filter_mwbe==lbeText){
				valid=valid&&getCertifications(theIndex).toLowerCase().indexOf("City of New York".toLowerCase())>-1;
			}
		}
	}
	return(valid);
}
function writeResults(){
	clearResults();
	document.getElementById("results_div").style.display="";
	document.getElementById("results_header").innerHTML='<span class="red">Searching...</span>';
	setTimeout(writeResultsText,10);
}
function writeResultsText(){
	filterService();
	filterCompany();
	filterNeighborhood();
	filterMWBE();
	clearFields();
	var resultsMessage='';
	if(filter_company.length>0){
		resultsMessage=resultsMessage+filter_company;
	}else if(filter_service.length>0){
		resultsMessage=resultsMessage+filter_service;
		if(filter_neighborhood.length>0){
			resultsMessage=resultsMessage+" + "+filter_neighborhood;
		}
		if(filter_mwbe.length>0){
			resultsMessage=resultsMessage+" + "+filter_mwbe;
		}
	}else if(filter_neighborhood.length>0){
		resultsMessage=resultsMessage+filter_neighborhood;
		if(filter_mwbe.length>0){
			resultsMessage=resultsMessage+" + "+filter_mwbe;
		}
	}else if(filter_mwbe.length>0){
		resultsMessage=resultsMessage+filter_mwbe;
	}else{
		resultsMessage='Displaying all results';
	}
	itemCount=0;
	theSpans=results();
	document.getElementById("results_header").innerHTML='Search Results:';
	document.getElementById("results_info").innerHTML=resultsMessage;
	if(theSpans.length==0){
		resultsMessage=resultsMessage+'<br><br><span class="red">No results found</span>';
		document.getElementById("results_info").innerHTML=resultsMessage;
	}else{
		showPage(0);
	}
}

function showPage(thePage){
	currentPage=parseInt(thePage);
	document.getElementById('resultsSpan').innerHTML=theSpans[thePage];
	hideAll();
	var prevLink="";
	if(thePage>0){
		prevLink="<a class='page_link' href='javascript:showPage("+(thePage-1)+")'>Previous</a> | ";
	}
	var pageLinks=getPageLinks(thePage);
	var nextLink="";
	if(thePage<theSpans.length-1){
		nextLink=" | <a class='page_link' href='javascript:showPage("+(thePage+1)+")'>Next</a>";
	}
	document.getElementById('topLinks').innerHTML=prevLink+pageLinks+nextLink;
	document.getElementById('bottomLinks').innerHTML=prevLink+pageLinks+nextLink;
}
function getPageLinks(thePage){
	var modulo=parseInt(thePage/5);
	var thePageLinks='';
	var maxPages=false;
	if(itemCount>resultsPerPage){
		for(var i=0;i<5;i++){
			if(modulo*5+i<theSpans.length){
				if(modulo*5+i==thePage){
					(i>0)?(thePageLinks=thePageLinks+" <a href='javascript:showPage("+(modulo*5+i)+")' class='active_link'>"+(modulo*5+i+1)+"</a>"):(thePageLinks=thePageLinks+"<a href='javascript:showPage("+(modulo*5+i)+")' class='active_link'>"+(modulo*5+i+1)+"</a>");
				}else{
					(i>0)?(thePageLinks=thePageLinks+" <a href='javascript:showPage("+(modulo*5+i)+")' class='page_link'>"+(modulo*5+i+1)+"</a>"):(thePageLinks=thePageLinks+"<a href='javascript:showPage("+(modulo*5+i)+")' class='page_link'>"+(modulo*5+i+1)+"</a>");
				}
			}else{
				maxPages=true;
			}
			if(i==theSpans.length){
				maxPages=true;
			}
		}
		if(!maxPages){
			thePageLinks=thePageLinks+" <a href='javascript:showPage("+(modulo*5+5)+")' class='page_link'>More</a>"
		}
	}
	return(thePageLinks);
}
function getResults(pageNumber){
	var firstItem=(pageNumber)*resultsPerPage+1;
	var lastItem=firstItem+resultsPerPage-1;
	if(lastItem>itemCount){
		lastItem=itemCount;
	}
	return(firstItem+"-"+lastItem);
}
function getCategory(theIndex){
	return(vendors[theIndex][serviceIndex]);
}
function getAddress(theIndex){
	var theAddress="";
	if(vendors[theIndex][addressIndex].length>0&&vendors[theIndex][addressIndex]!="N/A"){
		theAddress=theAddress+vendors[theIndex][addressIndex]+"<br>";
	}
	if(vendors[theIndex][cityIndex].length>0&&vendors[theIndex][stateIndex].length>0){
		var theCity=convertCase(vendors[theIndex][cityIndex]);
		theAddress=theAddress+" "+theCity+", "+vendors[theIndex][stateIndex];
	}
	if(vendors[theIndex][zipIndex].length>0){
		theAddress=theAddress+" "+vendors[theIndex][zipIndex];
	}
	return(theAddress);
}

function getNeighborhood(theIndex){
	return(vendors[theIndex][cityIndex]);
}

function getMWBE(theIndex){
	return(vendors[theIndex][mwbeIndex]);
}

function convertCase(theText){
	var returnText="";
	var theSplit=theText.split(" ");
	for(var i=0;i<theSplit.length;i++){
		returnText=returnText+theSplit[i].charAt(0).toUpperCase()+theSplit[i].substr(1).toLowerCase();
		if(i<theSplit.length-1){
			returnText=returnText+" ";
		}
	}
	return(returnText);
}
function getCompany(theIndex){
	return(vendors[theIndex][companyIndex]);
}
function getWebsite(theIndex){
	var theSite="";
	if(vendors[theIndex][webIndex].length>0){
		theSite="http://www.nyc.gov/cgi-bin/exit.pl?url=";
		if(vendors[theIndex][webIndex].indexOf("http")>0){
			theSite=theSite+vendors[theIndex][webIndex];
		}else{
			theSite=theSite+"http://"+vendors[theIndex][webIndex];
		}
		theSite='<a href="'+theSite+'">'+vendors[theIndex][webIndex]+'</a>';
	}
	return(theSite);
}
function getContact(theIndex){
	return(vendors[theIndex][contactIndex]);
}
function getFax(theIndex){
	return(vendors[theIndex][faxIndex]);
}
function getPhone(theIndex){
	return(vendors[theIndex][phoneIndex]);
}
function getService(theIndex){
	return(vendors[theIndex][serviceIndex]);
}
function getEmail(theIndex){
	return(vendors[theIndex][emailIndex]);
}
function getBID(theIndex){
	return(vendors[theIndex][bidIndex]);
}
function getCertifications(theIndex){
	return(vendors[theIndex][certifiedIndex]);
}
function results(){
	var spans=new Array();
	var theString='<table cellspacing="0" cellpadding="0" border="0" width="499">';
	theString=theString+'<tr><td class="results_table_header" width="155">Company Name</td><td class="results_table_header" width="155">Address</td><td class="results_table_header_rt" width="153">Product/Service</td></tr>';
	for(var i=0;i<vendors.length;i++){
		if(isValid(i)){
			theString=theString+'<tr id="inactive_top'+itemCount+'" valign="top"><td class="inactive_entry_top" width="157"><a href="javascript:showEntry('+itemCount+')" class="page_link">'+getCompany(i)+'</a></td>';
			theString=theString+'<td class="inactive_entry_top" width="157">'+getAddress(i)+'</td>';
			theString=theString+'<td class="inactive_entry_top" width="155">'+getService(i)+'</td></tr>';
			theString=theString+'<tr id="inactive_bottom'+itemCount+'"><td class="inactive_entry_bottom" colspan="3"><img src="/portal/images/spacers/spacer.gif" width="499" height="1" /></td></tr>';
			theString=theString+'<tr id="active_top'+itemCount+'" valign="top"><td class="active_entry_top" width="157"><a href="javascript:hideEntry('+itemCount+')" class="active_link">'+getCompany(i)+'</a></td>';
			theString=theString+'<td class="active_entry_top" width="157">'+getAddress(i)+'</td>';
			theString=theString+'<td class="active_entry_top" width="155">'+getService(i)+'</td></tr>';
			theString=theString+'<tr id="active_bottom'+itemCount+'"><td class="active_entry_table" colspan="3">';
			theString=theString+'<table cellspacing="0" cellpadding="0" border="0">';
			var detailsText="";
			if(getContact(i).length>0){
				detailsText=detailsText+'<tr><td class="active_label" width="144">Contact Person:</td><td class="active_text" width="311">'+getContact(i)+'</td></tr>';
			}
			if(getPhone(i).length>0){
				if(detailsText.length>0){
					detailsText=detailsText+'<tr><td colspan="2"><img src="/portal/images/spacers/spacer.gif" width="100" height="3" border="0" alt=""></td></tr>';
				}
				detailsText=detailsText+'<tr><td class="active_label" width="144">Phone:</td><td class="active_text" width="311">'+getPhone(i)+'</td></tr>';
			}
			if(getFax(i).length>0){
				if(detailsText.length>0){
					detailsText=detailsText+'<tr><td colspan="2"><img src="/portal/images/spacers/spacer.gif" width="100" height="3" border="0" alt=""></td></tr>';
				}
				detailsText=detailsText+'<tr><td class="active_label" width="144">Fax:</td><td class="active_text" width="311">'+getFax(i)+'</td></tr>';
			}
			if(getEmail(i).length>0){
				if(detailsText.length>0){
					detailsText=detailsText+'<tr><td colspan="2"><img src="/portal/images/spacers/spacer.gif" width="100" height="3" border="0" alt=""></td></tr>';
				}
				detailsText=detailsText+'<tr><td class="active_label" width="144">Email:</td><td class="active_text" width="311">'+getEmail(i)+'</td></tr>';
			}
			if(getWebsite(i).length>0){
				if(detailsText.length>0){
					detailsText=detailsText+'<tr><td colspan="2"><img src="/portal/images/spacers/spacer.gif" width="100" height="3" border="0" alt=""></td></tr>';
				}
				detailsText=detailsText+'<tr><td class="active_label" width="144">Web site:</td><td class="active_text" width="311">'+getWebsite(i)+'</td></tr>';
			}
			if(getBID(i).length>0){
				if(detailsText.length>0){
					detailsText=detailsText+'<tr><td colspan="2"><img src="/portal/images/spacers/spacer.gif" width="100" height="3" border="0" alt=""></td></tr>';
				}
				detailsText=detailsText+'<tr><td class="active_label" width="144">BID Capacity:</td><td class="active_text" width="311">'+getBID(i)+'</td></tr>';
			}
			if(getCertifications(i).length>0){
				if(detailsText.length>0){
					detailsText=detailsText+'<tr><td colspan="2"><img src="/portal/images/spacers/spacer.gif" width="100" height="3" border="0" alt=""></td></tr>';
				}
				detailsText=detailsText+'<tr><td class="active_label" width="144">Certifications:</td><td class="active_text" width="311">'+getCertifications(i)+'</td></tr>';
			}
			theString=theString+detailsText+'</table></td></tr>';
			itemCount++;
			if(itemCount%resultsPerPage==0&&i<vendors.length-1){
				theString=theString+'</table>';
				spans[spans.length]=theString;
				theString='<table cellspacing="0" cellpadding="0" border="0" width="499">';
				theString=theString+'<tr><td class="results_table_header" width="155">Company Name</td><td class="results_table_header" width="155">Address</td><td class="results_table_header_rt" width="153">Product/Service</td></tr>';
			}
		}
	}
	if(theString.length>63&&theString.substring(theString.length-8)!="</table>"){
		theString=theString+'</table>';
		spans[spans.length]=theString;
	}
	return(spans);
}
function hideAll(){
	var start=currentPage*resultsPerPage;
	for(var i=0;i<resultsPerPage;i++){
		if(start+i<itemCount){
			hideEntry(start+i);
		}
	}
}
function showEntry(theEntry){
	hideAll();
	var theId="inactive_top"+theEntry;
	document.getElementById(theId).style.display="none";
	theId="inactive_bottom"+theEntry;
	document.getElementById(theId).style.display="none";
	if(theEntry%20!=0){
		theId="inactive_bottom"+(theEntry-1);
		document.getElementById(theId).style.display="none";
	}
	theId="active_top"+theEntry;
	document.getElementById(theId).style.display="";
	theId="active_bottom"+theEntry
	document.getElementById(theId).style.display="";
}
function hideEntry(theEntry){
	var theId="inactive_top"+theEntry;
	document.getElementById(theId).style.display="";
	theId="inactive_bottom"+theEntry;
	document.getElementById(theId).style.display="";
	if(theEntry%resultsPerPage==resultsPerPage-1||theEntry==itemCount-1){
		document.getElementById(theId).style.display="none";
	}
	if(theEntry%20!=0){
		theId="inactive_bottom"+(theEntry-1);
		document.getElementById(theId).style.display="";
	}
	theId="active_top"+theEntry;
	document.getElementById(theId).style.display="none";
	theId="active_bottom"+theEntry
	document.getElementById(theId).style.display="none";
}