var boroughs=["Bronx","Brooklyn","Manhattan","Queens","Staten Island"];
var classifications=new Array();
var filter_borough="";
var filter_classification="";
var schoolIndex=0;
var urlIndex=1;
var addressIndex=2;
var classificationIndex=3;
var descriptionIndex=4;
var boroughIndex=5;
var searchClassificationIndex=6;
var resultsPerPage=20;
var theSpans=new Array();
var itemCount=0;
var sortColumn=-1;
var sortAscending=true;
var theId="";

function classificationsContains(newEntry){
	for(var j=0;j<classifications.length;j++){
		if(classifications[j].toLowerCase()==newEntry.toLowerCase()){
			return(true);
		}
	}
	return(false);
}
function writeClassificationDropdown(){
	for(var i=0;i<edu_list.length;i++){
		if(edu_list[i][searchClassificationIndex].length>0&&!classificationsContains(edu_list[i][searchClassificationIndex])){
			classifications[classifications.length]=edu_list[i][searchClassificationIndex];
		}
	}
	classifications=classifications.sort();
	document.write('<select id="selectClassification" name="selectClassification" style="width:150px;"><option value="">School Classification</option>');
	for(var i=0;i<classifications.length;i++){
		document.write('<option value="'+classifications[i]+'">'+classifications[i]+'</option>');
	}
	document.write('</select>');
}
function writeBoroughDropdown(){
	document.write('<select id="selectBorough" name="selectBorough" style="width:131px;"><option value="">Borough</option>');
	for(var i=0;i<boroughs.length;i++){
		document.write('<option value="'+boroughs[i]+'">'+boroughs[i]+'</option>');
	}
	document.write('</select>');
}
function writeDropdowns(){
	for(var i=0;i<edu_list.length;i++){
		if(edu_list[i][searchClassificationIndex].length>0&&!classificationsContains(edu_list[i][searchClassificationIndex])){
			classifications[classifications.length]=edu_list[i][searchClassificationIndex];
		}
	}
	classifications=classifications.sort();
	document.write('<table cellpadding="0" cellspacing="0" border="0" width="499"><tr><td class="search_table"><table cellspacing="0" cellpadding="0" border="0" width="449">');
	document.write('<tr><td class="search_label" width="103">Borough:</td><td class="search_field" width="327"><select id="selectBorough" name="selectBorough" style="width:327px;"><option value="">- All -</option>');
	for(var i=0;i<boroughs.length;i++){
		document.write('<option value="'+boroughs[i]+'">'+boroughs[i]+'</option>');
	}
	document.write('</select></td></tr><tr><td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="449" height="3" border="0" alt=""></td></tr>');
	document.write('<tr><td class="search_label" width="103">Classification:</td>');
	document.write('<td class="search_field" width="327"><select id="selectClassification" name="selectClassification" style="width:327px;"><option value="">- All -</option>');
	for(var i=0;i<classifications.length;i++){
		document.write('<option value="'+classifications[i]+'">'+classifications[i]+'</option>');
	}
	document.write('</select></td></tr><tr><td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="449" height="7" border="0" alt=""></td></tr>');
	document.write('<tr><td colspan="2" align="right" class="search_buttons"><input type="button" class="yellow_button" onMouseOver=this.className="yellow_button_over" onMouseOut=this.className="yellow_button" value="Go" style="width:36px;" onClick="writeResults(schoolIndex);">&nbsp;');
	document.write('<input type="reset" class="yellow_button" onMouseOver=this.className="yellow_button_over" onMouseOut=this.className="yellow_button" value="Reset" style="width:59px;" onclick="clearResults()"></td></tr></table></td></tr></table>');
	document.write('<div id="resultsDiv" class="results_div"><table cellspacing="0" cellpadding="0" width="499" border="0">');
	document.write('<tr><Td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="41" /></td></tr>');
	document.write('<tr><td class="results_header" id="results_header">Results Header</td><td align="right" id="pagination" class="pagination">&nbsp;</td></tr>');
	document.write('<tr><Td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="12" /></td></tr>');
	document.write('<tr><td id="resultsTD" colspan="2"></td></tr>');
	document.write('<tr><Td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="12" /></td></tr>');
	document.write('<tr><td colspan="2" align="right" id="pagination2" class="pagination">&nbsp;</td></tr>');
	document.write('<tr><Td colspan="2"><img src="../../includes/site_images/spacers/spacer.gif" width="499" height="90" /></td></tr></table></div>');
	document.getElementById("resultsDiv").style.display="none"; 
	document.write('<div id="detailsDiv" class="details_div" style="width:460px;z-index:20;position:absolute; top:0px;left:226px;"><table cellspacing="0" cellpadding="0" width="460" border="0">');
	document.write('<tr><td class="details_header" id="detailsHeader">&nbsp;</td><td class="details_link"><a href="javascript:closeDescription();">Close</a></td></tr>');
	document.write('<tr><Td class="details_text" id="detailsText">&nbsp;</td></tr></table></div>');
	document.getElementById("detailsDiv").style.display="none"; 
	document.write('<iframe scrolling="no" frameborder="0" class="shim" name="shim" id="shim" style="width:460px;z-index:10;position:absolute; top:0px;left:226px;"></iframe>');
	document.getElementById("shim").style.display="none";
	var params=(location.search).substr(1, (location.search).length);
	if(params.length>0){
		var pairs=params.split('&');
		for(var i=0;i<pairs.length;i++){
			var thePair=pairs[i].split('=');
			if(thePair[0]=="borough"){
				filter_borough=unescape(thePair[1]);
				setBorough();
			}else if(thePair[0]=="classification"){
				filter_classification=unescape(thePair[1]);
				setClassification();
			}
		}
	}
}
function clearResults(){
	document.getElementById('resultsDiv').style.display="none";
	document.getElementById('detailsDiv').style.display="none";
	document.getElementById('resultsTD').innerHTML="";
	document.getElementById('selectBorough').selectedIndex=0;
	document.getElementById('selectClassification').selectedIndex=0;
	document.getElementById('pagination').innerHTML="&nbsp;";
	document.getElementById('pagination2').innerHTML="&nbsp;";
	filter_borough="";
	filter_classification="";
	sortColumn=-1;
	sortAscending=true;
	writeResults(schoolIndex);
}
function showPage(pageNumber){
	closeDescription();
	theId="";
	setPagination(pageNumber,theSpans.length);
	document.getElementById('resultsTD').innerHTML=theSpans[pageNumber];
}
function setPagination(pageNumber,totalPages){
	var thePagination='<a href="javascript:showPage(';
	if(pageNumber==0){
		thePagination=thePagination+(totalPages-1);
	}else{
		thePagination=thePagination+(pageNumber-1);
	}
	thePagination=thePagination+')">Previous</a> | ';
	for(var ind=0;ind<totalPages;ind++){
		if(ind==pageNumber){
			thePagination=thePagination+'<span class="black">'+(ind+1)+'</span> ';
		}else{
			thePagination=thePagination+'<a href="javascript:showPage('+ind+')">'+(ind+1)+'</a> ';
		}
	}
	thePagination=thePagination+'| <a href="javascript:showPage(';
	if(pageNumber==(totalPages-1)){
		thePagination=thePagination+0;
	}else{
		thePagination=thePagination+(pageNumber+1);
	}
	thePagination=thePagination+')">Next</a>';
	document.getElementById('pagination').innerHTML=thePagination;
	document.getElementById('pagination2').innerHTML=thePagination;
}
function writeResults(column){
	document.getElementById('resultsTD').innerHTML='<span class="red_bold">Searching...</span>';
	document.getElementById('resultsDiv').style.display="";
	document.getElementById('detailsDiv').style.display="none";
	document.getElementById('pagination').innerHTML="&nbsp;";
	document.getElementById('pagination2').innerHTML="&nbsp;";
	itemCount=0;
	var theField=document.getElementById('selectBorough');
	filter_borough=theField.options[theField.selectedIndex].value;
	theField=document.getElementById('selectClassification');
	filter_classification=theField.options[theField.selectedIndex].value;
	if(sortColumn==column){
		sortAscending=!sortAscending;
	}else{
		sortAscending=true;
	}
	sortColumn=column;
	setTimeout(writeResultsText,10);
}
function writeResultsText(){
	var resultsMessage="";
	if(filter_borough.length>0){
		resultsMessage=filter_borough;
		if(filter_classification.length>0){
			resultsMessage=resultsMessage+" and "+filter_classification;
		}
	}else if(filter_classification.length>0){
		resultsMessage=filter_classification;
	}else{
		resultsMessage="Displaying all schools";
	}
	itemCount=0;
	theSpans=getSpans();
	document.getElementById("results_header").innerHTML=resultsMessage;
	if(theSpans.length==0){
		theSpans=['<span class="red">No results found</span>'];
	}
	showPage(0);
}
function isValid(theIndex){
	valid=true;
	if(filter_borough.length>0){
		valid=valid&&filter_borough.toLowerCase()==edu_list[theIndex][boroughIndex].toLowerCase();
	}
	if(filter_classification.length>0){
		valid=valid&&filter_classification.toLowerCase()==edu_list[theIndex][searchClassificationIndex].toLowerCase();
	}
	return(valid);
}
function showDescription(theIndex){
	closeDescription();
	document.getElementById('detailsHeader').innerHTML=getSchoolName(theIndex);
	document.getElementById('detailsText').innerHTML=getDescription(theIndex);
	theId="description"+theIndex;
	var theLinkTd=document.getElementById(theId);
	var posy = theLinkTd.offsetTop;
    tempEl = theLinkTd.offsetParent;
    while (tempEl != null) {
          posy += tempEl.offsetTop;
          tempEl = tempEl.offsetParent;
      }
	var yDim=document.getElementById("detailsDiv").offsetHeight;
	var myHeight=0;
	var scrOfY=0;
	var totalHeight=0;
	var overallY=posy+yDim;
	if( document.documentElement && document.documentElement.clientHeight) {
	    //IE 6+ in 'standards compliant mode'
	    myHeight = document.documentElement.clientHeight;
	} else if( document.body && document.body.clientHeight) {
	    //IE 4 compatible
	    myHeight = document.body.clientHeight;
	}
	if( document.body && document.body.scrollTop) {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	} else if( document.documentElement && document.documentElement.scrollTop) {
	    //IE6 standards compliant mode
	    scrOfY = document.documentElement.scrollTop;
	}
	totalHeight=myHeight+scrOfY;
	if(overallY>totalHeight){
		posy=totalHeight-yDim;
	}
	document.getElementById(theId).innerHTML="Description";
	document.getElementById("detailsDiv").style.top = posy+theLinkTd.offsetHeight+2;
	document.getElementById("detailsDiv").style.display = "";
	document.getElementById("shim").style.top = posy+theLinkTd.offsetHeight+2;
	document.getElementById("shim").style.visibility = "";
}
function closeDescription(){
	if(theId.length>0){
		try {
			document.getElementById(theId).innerHTML='<a href="javascript:showDescription('+theId.substr(theId.length-1)+')">Description</a>';
		}	catch(err) { }
	}
	document.getElementById("detailsDiv").style.display = "none";
	document.getElementById("shim").style.display = "none";
}
function getSpans(){
	var spans=new Array();
	edu_list=edu_list.sort(compareSchools);
	var theString='<table cellspacing="0" cellpadding="0" border="0" width="499">';
	theString=theString+'<tr><td class="sort_header1" width="112"><a href="javascript:writeResults(schoolIndex);" class="filterHead">School</a></Th><td class="sort_header2" width="64"><a href="javascript:writeResults(boroughIndex);" class="filterHead">Borough</a></Th>';
	theString=theString+'<td class="sort_header2" width="96"><a href="javascript:writeResults(classificationIndex);" class="filterHead">Classification</a></Th><td class="sort_header2" width="122">Address</Th>';
	theString=theString+'<td class="sort_header2" width="67">&nbsp;</Th></tr>';
	for(var i=0;i<edu_list.length;i++){
		if(isValid(i)){
			var theClass="normal";
			if(itemCount%2==0){
				theClass="normal";
			}else{
				theClass="alternate"
			}
			theString=theString+'<tr valign="top"><td class="'+theClass+'1" width="112">'+getSchoolLink(i)+'</td>';
			theString=theString+'<td class="'+theClass+'2" width="64">'+getBorough(i)+'</td>';
			theString=theString+'<td class="'+theClass+'2" width="96">'+getClassification(i)+'</td>';
			theString=theString+'<td class="'+theClass+'2" width="122">'+getAddress(i)+'</td>';
			theString=theString+'<td class="'+theClass+'2" width="67" id="description'+i+'"><a href="javascript:showDescription('+i+')">Description</a></td></tr>';
			itemCount++;
			if(itemCount%resultsPerPage==0){
				theString=theString+'</table>';
				spans[spans.length]=theString;
				theString='<table cellspacing="0" cellpadding="0" border="0" width="499">';
				theString=theString+'<tr><td class="sort_header1" width="112"><a href="javascript:writeResults(schoolIndex);" class="filterHead">School</a></Th><td class="sort_header2" width="64"><a href="javascript:writeResults(boroughIndex);" class="filterHead">Borough</a></Th>';
				theString=theString+'<td class="sort_header2" width="96"><a href="javascript:writeResults(classificationIndex);" class="filterHead">Classification</a></Th><td class="sort_header2" width="122">Address</Th>';
				theString=theString+'<td class="sort_header2" width="67">&nbsp;</Th></tr>';
			}
		}
	}
	if(theString.length>63&&theString.substring(theString.length-8)!="</table>"){
		theString=theString+'</table>';
		spans[spans.length]=theString;
	}
	return(spans);
}
function getSchoolLink(theIndex){
	if(getURL(theIndex).length>0){
		return('<a href="'+getURL(theIndex)+'">'+getSchoolName(theIndex)+'</a>');
	}else{
		return(getSchoolName(theIndex));
	}
}
function getSchoolName(theIndex){
	return(edu_list[theIndex][schoolIndex]);
}
function getURL(theIndex){
	var theUrl=edu_list[theIndex][urlIndex];
	if(theUrl.length>0&&theUrl.toLowerCase().indexOf("nyc.gov")==-1){
		theUrl='http://www.nyc.gov/cgi-bin/exit.pl?url='+theUrl;
	}
	return(theUrl);
}
function getAddress(theIndex){
	return(edu_list[theIndex][addressIndex]);
}
function getClassification(theIndex){
	return(edu_list[theIndex][classificationIndex]);
}
function getDescription(theIndex){
	return(edu_list[theIndex][descriptionIndex]);
}
function getBorough(theIndex){
	return(edu_list[theIndex][boroughIndex]);
}
function compareSchools(a,b){
	var elemA=a[sortColumn].toLowerCase();
	var elemB=b[sortColumn].toLowerCase();
	if(elemA==elemB){
		return(0);
	}
	var aGreater=elemA>elemB;
	if(aGreater){
		if(sortAscending){
			return(1);
		}else{
			return(-1);
		}
	}else{
		if(sortAscending){
			return(-1);
		}else{
			return(1);
		}
	}
}
function schoolSearch(){
	var boroughDropdown=document.getElementById('selectBorough');
	var classificationDropdown=document.getElementById('selectClassification');
	var boroughEncoded=escape(boroughDropdown.options[boroughDropdown.selectedIndex].value);
	var classificationEncoded=escape(classificationDropdown.options[classificationDropdown.selectedIndex].value);
	var url="../../html/education/colleges.shtml";
	if(boroughEncoded.length>0){
		url=url+"?borough="+boroughEncoded;
		if(classificationEncoded.length>0){
			url=url+"&classification="+classificationEncoded;
		}
	}else if(classificationEncoded.length>0){
		url=url+"?classification="+classificationEncoded;
	}
	window.location.href=url;
}
function setBorough(){
	var selectBorough=document.getElementById("selectBorough").options;
	for(var i=0;i<selectBorough.length;i++){
		if(selectBorough[i].value==filter_borough){
			selectBorough[i].selected=true;
		}else{
			selectBorough[i].selected=false;
		}
	}
}
function setClassification(){
	var selectClassification=document.getElementById("selectClassification").options;
	for(var i=0;i<selectClassification.length;i++){
		if(selectClassification[i].value==filter_classification){
			selectClassification[i].selected=true;
		}else{
			selectClassification[i].selected=false;
		}
	}
}

