var dl_Path = "../../downloads/pdf/";
var policy_area=new Array();
var cities=new Array();

var filter_city="";
var filter_policy="";
var filter_keyword="";

function getDocHTML(theDoc) {
	var tmpStr = "";
	var tmpSplit = theDoc.split("|");
	tmpStr = tmpStr + "<a href='" + dl_Path + tmpSplit[0] + "'>" + tmpSplit[1] + "</a>"
	
	return tmpStr;
}
function contains(theArray,theValue){
	for(var ind=0;ind<theArray.length;ind++){
		if(theArray[ind].toLowerCase().indexOf(theValue.toLowerCase())>-1){
			return(true);
		}
	}
	return(false);
}
function writeDropdowns(){
	for(var i=0;i<docs.length;i++){
		if(!contains(policy_area,docs[i].split("|")[3])){
			policy_area[policy_area.length]=docs[i].split("|")[3];
		}
		if(!contains(cities,docs[i].split("|")[2])){
			cities[cities.length]=docs[i].split("|")[2];
		}
	}
	policy_area.sort();
	cities.sort();
	document.write('<table cellspacing="0" cellpadding="0" border="0" width="499">');
	document.write('<tr><td class="resultsDivider"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="1" /></td></tr>');
	document.write('<tr><td><table cellspacing="0" cellpadding="0" border="0" width="484">');
	document.write('<form name="cityPolicy" onSubmit="writeResults();return(false);">');
	document.write('<tr><Td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="452" height="7" /></td></tr>');
	document.write('<tr><td class="searchHeader" colspan="2" id="search_header">Search for:</td></tr>');
	document.write('<tr><Td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="452" height="7" /></td></tr>');
	document.write('<tr><td align="right" valign="top" nowrap class="terms_label" width="146">City:</td>');
	document.write('<td class="terms_field" width="298"><select name="city" id="city" class="textField" style="width:326px;">');
	document.write('<option value="">All</option>');
	for (x=0; x<cities.length; x++) {
		document.write('<option value="' + ((cities[x].split("|"))[0]) + '" >' + (cities[x].split("|"))[0] + '</option>');
	}
	document.write('</select></td></tr>');
	document.write('<tr><Td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="452" height="4" /></td></tr>');
	document.write('<tr><td align="right" nowrap valign="top" class="terms_label" width="146">Policy Area:</td>');
	document.write('<td class="terms_field" width="298"><select name="policy" id="policy" class="textField" style="width:326px;"><option value="">All</option>');
	for (x=0; x<policy_area.length; x++) {
		document.write('<option value="' + (policy_area[x]) + '">' + policy_area[x] + '</option>');
	}
	document.write('</select></td></tr><tr><Td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="452" height="10" /></td></tr>');
	document.write('<tr><td colspan="2" align="right"><input type="button" class="yellow_button" onMouseOver=this.className="yellow_button_over" onMouseOut=this.className="yellow_button" value="Search" style="width:71px;" onClick="writeResults()">&nbsp;<input type="reset" class="yellow_button" onMouseOver=this.className="yellow_button_over" onMouseOut=this.className="yellow_button" value="Reset" style="width:63px;" onClick="clearResults()"></td></tr>');
	document.write('</form></table></td></tr>');
	document.write('<input type="hidden" name="keyword" id="keyword">');

	document.write('<tr id="resultsRow"><td><table cellspacing="0" cellpadding="0" border="0" width="499"><tr><td><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="20" /></td></tr>');
	document.write('<tr><td class="resultsDivider"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="1" /></td></tr>');
	document.write('<tr><td><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="15" /></td></tr>');
	document.write('<tr><td class="searchHeader">Search Results:</td></tr>');
	document.write('<tr><td><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="3" /></td></tr>');
	document.write('<tr><td id="resultsTD"></td></tr>');
	document.write('</table></td></tr>');
	document.write('<tr><td><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="100" /></td></tr>');
	document.write('</table>');
	document.getElementById("resultsRow").style.display="none";
}
function clearResults(){
	document.getElementById("resultsTD").innerHTML="";
	document.getElementById("resultsRow").style.display="none";
	filter_city="";
	filter_policy="";
	filter_keyword="";
	window.scrollTo(0,0);
}

function writeResults(){
	clearResults();
	document.getElementById("keyword").value="";
	var citySelect=document.getElementById('city');
	var policySelect=document.getElementById('policy');
	filter_city=citySelect.options[citySelect.selectedIndex].value;
	filter_policy=policySelect.options[policySelect.selectedIndex].value;
	var message='<span class="red">Searching...</span>';
	document.getElementById("resultsTD").innerHTML=message;
	document.getElementById("resultsRow").style.display="";
	if(filter_city.length==0&&filter_policy.length==0){
		document.getElementById("resultsTD").innerHTML="You must select a city or policy area.";
	}else{
		setTimeout(writeResultsText,10);
	}
}

function writeResultsText(){
	var finalStr = "";
	if(filter_city.length>0){
		if(filter_policy.length>0){
			for (var x=0; x<docs.length; x++) {
				if (filter_city == docs[x].split("|")[2]) {
					if (docs[x].split("|")[3].indexOf(filter_policy) != -1) {
						finalStr = finalStr + getDocHTML(docs[x]);
						finalStr = finalStr + "<br>";
					}
				}
			}
			if(finalStr.length>0){
				finalStr='<tr><td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="4" border="0" alt=""></td></tr><tr valign="top"><td class="data_label" width="136">'+filter_policy+'</b><td class="doc_field" width="335">'+finalStr+'</td></tr>';
			}
		}else{
			var theCityDocs=new Array();
			for(var x=0;x<docs.length;x++){
				if(docs[x].split("|")[2].indexOf(filter_city)>-1){
					theCityDocs[theCityDocs.length]=docs[x];
				}
			}
			theCityDocs.sort(sortByPolicy);
			var policyHolder="none";
			for(var i=0;i<theCityDocs.length;i++){
				if(theCityDocs[i].split("|")[3].indexOf(policyHolder)>-1){
					finalStr=finalStr+"<br>"+getDocHTML(theCityDocs[i]);
				}else{
					if(policyHolder.indexOf("none")==-1){
						finalStr=finalStr+"</td></tr>";
					}
					policyHolder=theCityDocs[i].split("|")[3];
					finalStr=finalStr+'<tr><td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="4" border="0" alt=""></td></tr>'
					finalStr=finalStr+'<tr valign="top"><td class="data_label" width="136">'+policyHolder+'</b><td class="doc_field" width="335">'+getDocHTML(theCityDocs[i]);
					if(i==theCityDocs.length-1){
						finalStr=finalStr+"</td></tr>";
					}
				}
			}
		}
	}else{
		var thePolicyDocs=new Array();
		for(var x=0;x<docs.length;x++){
			if(docs[x].split("|")[3].indexOf(filter_policy)>-1){
				thePolicyDocs[thePolicyDocs.length]=docs[x];
			}
		}
		thePolicyDocs.sort(sortByCity);
		var cityHolder="none";
		for(var i=0;i<thePolicyDocs.length;i++){
			if(thePolicyDocs[i].split("|")[2].indexOf(cityHolder)>-1){
				finalStr=finalStr+"<br>"+getDocHTML(thePolicyDocs[i]);
			}else{
				if(cityHolder.indexOf("none")==-1){
					finalStr=finalStr+"</td></tr>";
				}
				cityHolder=thePolicyDocs[i].split("|")[2];
				finalStr=finalStr+'<tr><td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="4" border="0" alt=""></td></tr>'
				finalStr=finalStr+'<tr valign="top"><td class="data_label" width="136">'+cityHolder+'</b><td class="doc_field" width="335">'+getDocHTML(thePolicyDocs[i]);
				if(i==thePolicyDocs.length-1){
					finalStr=finalStr+"</td></tr>";
				}
			}
		}
	}
	if(finalStr.length==0) {
		finalStr = "No best practices were found based on the provided criteria.";
	}else{
		finalStr='<table cellspacing="0" cellpadding="0" width="499" border="0">'+finalStr+'</table>';
	}
	document.getElementById("resultsTD").innerHTML=finalStr;
	setTimeout(scrollToResults,10);
}

function scrollToResults(){
	var elem=document.getElementById('search_header');
	var theTop=elem.offsetTop;
	var tempEl = elem.offsetParent;
	while (tempEl != null) {
		theTop += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
  	}
	window.scrollTo(0,theTop);
}

function sortNumber(a,b)
{
	return a - b;
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
 }
 
function sortByPolicy(a,b){
	var x = a.split("|")[3].toLowerCase();
	var y = b.split("|")[3].toLowerCase();
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function sortByCity(a,b){
	var x = a.split("|")[2].toLowerCase();
	var y = b.split("|")[2].toLowerCase();
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}
