var boroughNames = new Array("--- Choose ---", "Manhattan", "Bronx", "Brooklyn", "Queens", "Staten Island");
var paPages = new Array("pageAddress", "pageCommunity", "pagePOI", "pageSubStation");
var bufMiles = new Array(0.1, 0.2, 0.25, 0.3, 0.4, 0.5);
var btnNames = new Array("btnAddress", "btnCommunity", "btnPOI", "btnSubStation");
//var imgDowns = new Array("address.gif", "comm_district.gif", "pointofinterest.gif", "subwaystation.gif");
//var imgUps = new Array("address_over.gif", "comm_district_over.gif", "pointofinterest_over.gif", "subwaystation_over.gif");
var tabtns = new Array("address.gif", "cd.gif", "poi.gif", "subway.gif");
	
var pois = new Array(5);

function showHideSection(secID, shInd) {
	document.getElementById(secID).style.display = shInd ? "block" : "none";
}

function showPage(pgID) {
	var btnNames = new Array("btnAddress", "btnCommunity", "btnPOI", "btnSubStation");
	//var imgUps = new Array("address.gif", "comm_district.gif", "pointofinterest.gif", "subwaystation.gif");
	//var imgDowns = new Array("address_over.gif", "comm_district_over.gif", "pointofinterest_over.gif", "subwaystation_over.gif");
	
	
	for (var i=0; i<paPages.length; i++) {
		if (pgID==paPages[i]) {
			document.getElementById(paPages[i]).style.display= "block";
			document.getElementById("tabBtn").src = "image/" + tabtns[i];
		} else {
		 	document.getElementById(paPages[i]).style.display= "none";
		}	
	}
}


function markPrt() {
	// get tab id
	var tabInx = 0;
	for (var i=0; i<paPages.length; i++) {
		if (document.getElementById(paPages[i]).style.display == "block") {
			tabInx = i;
			break;
		}
	}
	
	var prtStr = "";
	// address page
	if (tabInx == 0) {
		var hnum = document.frmAddress.hseNumber.value;
		var add = document.frmAddress.address.value;
		
		if (hnum == "")
			hnum = " ";
		
		if (add == "")
			add = " ";
		prtStr += "0_$$"+ hnum +"_$$"+ add +"_$$"+document.frmAddress.borough.selectedIndex;
		
		for (var j=0; j<3; j++) {
			if (document.frmAddress.event[j].checked)
				prtStr +="_$$"+j;
		}	
		prtStr +=  "_$$" + document.frmAddress.sltdMBuffer.value;
	}
	
	// CD page
	if (tabInx == 1) {
		var boroInxCD = document.frmCommDist.borough.selectedIndex;
		prtStr =  "1_$$" + boroInxCD + "_$$";
	
		if (boroInxCD == 0)
			prtStr += "-1";
		else 		
			prtStr += document.frmCommDist.cdCommDistNum.selectedIndex;
	}
	
	// POI page
	if (tabInx == 2) {
		var boroInxPOI = document.frmPOI.borough.selectedIndex;
		var poiTypeInx = document.frmPOI.poiTypeSel.selectedIndex;
		prtStr =  "2_$$" + boroInxPOI + "_$$" + poiTypeInx + "_$$";
		
		if (poiTypeInx == -1)
			prtStr += "-1";
		else 		
			prtStr += document.frmPOI.poiNum.selectedIndex;
			
		for (var k=0; k<3; k++) {
			if (document.frmPOI.event[k].checked)
				prtStr +="_$$"+k;
		}	
		prtStr +=  "_$$" + document.frmPOI.sltdMBuffer.value	
	}
	
	// Subway page
	if (tabInx == 3) {
		var subLineInx = document.frmSubStation.sublines.selectedIndex;
		
		prtStr = "3_$$" + subLineInx + "_$$";
				
		if (subLineInx < 0) 
			prtStr += "-1";
		else 
			prtStr += document.frmSubStation.ssID.selectedIndex; 
		
		for (var l=0; l<3; l++) {
			if (document.frmSubStation.event[l].checked)
				prtStr +="_$$"+l;
		}	
		prtStr +=  "_$$" + document.frmSubStation.sltdMBuffer.value	
	}	
	
	document.prtform.pstr.value = prtStr;
	document.prtform.submit();
} 


function wtTbBtns() {
	var tbBtn;
	
	for (var i=0; i<paPages.length; i++) {
	
		tbBtn="<img id='"+btnNames[i]+"' src='image/buttons/";
		
		if (i==0)
			tbBtn+=imgUps[i];
		else 
			tbBtn+=imgDowns[i];
	
			
		tbBtn+= "' onclick='javascript:showPage(paPages["+i+"]);' />";
	
		document.write(tbBtn);
	}
}

function displayPOI(poi_type) {	
	var boroSelected = document.getElementById("boroSel").selectedIndex-1;
	var typeSelected = poi_type.selectedIndex;
	
	var poiArray = pois[boroSelected][typeSelected].sort();
	
	var inner_poiHtml = "<TABLE><TR><TD class=\"Text_Black_12_Bold\">&nbsp; &nbsp; &nbsp; &nbsp;3) Select " + category[poi_type.selectedIndex];
	
	inner_poiHtml += "</TD></TR><TR><TD>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<SELECT size='3' name='poiNum' onchange=\"showHideSection('secPoiBtn', true)\">";
	
	for (var i=0; i<poiArray.length; i++)
		inner_poiHtml += "<option value=\""+poiArray[i][1]+"\">"+poiArray[i][0]+"&nbsp; &nbsp; &nbsp; &nbsp;</options>";
	
	inner_poiHtml += "</SELECT></TD></TR></TABLE>";
	
	document.getElementById("secPoiCon").innerHTML = inner_poiHtml; 
	showHideSection("secPoiCon", true);
	showHideSection("secPoiBtn", false);
} 

function wtBoroSelection() {
	for (var i=0; i<boroughNames.length; i++) {
		document.write("<option value=\""+i+"\">"+boroughNames[i]+"</option>");
	}
}

function commBoroChange(boro) {
	var toSorN = (boro.selectedIndex==0) ? false : true; 
	
	showHideSection("secCommDist", toSorN);
	showHideSection("secCommBtn", false);
	
	if (toSorN) { 	
		var commSel_ih = "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<SELECT size=\"5\" name=\"cdCommDistNum\" onchange=\"showHideSection('secCommBtn', true)\">";
		var boroIx = boro.selectedIndex-1;
		
		for (var i=0; i<cmds[boroIx].length; i++)
			 commSel_ih += "<option value=\""+cmds[boroIx][i][1]+"\">"+(i+1)+". "+cmds[boroIx][i][0]+"</options>";
									
		commSel_ih +="</SELECT>";
	
		document.getElementById("secCommInOpts").innerHTML = commSel_ih;
	}
}

function poiBoroChange(boro) {
	var toSorN = (boro.selectedIndex==0) ? false : true; 
	
	showHideSection("secPoiType", toSorN);
	showHideSection("secPoiCon", false);
	showHideSection("secPoiBtn", false);
	document.getElementById("poiTypeSel").selectedIndex=-1;
}


function ssLines() {
	var ssSel_ih = "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<SELECT name=\"sublines\" size=\"5\" onchange=\"displaySS(this)\">";
		
	for (var i=0; i<lines.length; i++)
		 ssSel_ih += "<option>"+lines[i]+"&nbsp; &nbsp; &nbsp; &nbsp;</options>";
									
	ssSel_ih +="</SELECT>";
	document.write(ssSel_ih);
	
}

function displaySS(lines) {	
	//var boro = document.getElementById("ssBoro").selectedIndex-1;
	
	showHideSection("secSSBtn", false);
	
	var line = lines.selectedIndex;
	
	var sorted_sts = stations[line]; 
	
	var ssSel_ih = "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<SELECT size=\"5\" name=\"ssID\" onchange=\"showHideSection('secSSBtn', true)\">";
		
	for (var i=0; i<sorted_sts.length; i++)
		 ssSel_ih += "<option value=\""+sorted_sts[i][2]+"\">"+sorted_sts[i][0]+" ("+sorted_sts[i][1]+")&nbsp; &nbsp; &nbsp; &nbsp</options>";
									
	ssSel_ih +="</SELECT>";
	
	document.getElementById("optSSNames").innerHTML = ssSel_ih;
	
	showHideSection("secSSNames", true);
} 

function wtMlBfZns(mile) {	
	if (mile < 0)
		mile = 0.25;
	
	for (var i=0; i<bufMiles.length; i++) {
		var sled = "";
		if (bufMiles[i] == mile)
			sled = " SELECTED";
			
		
		document.write("<option value=\""+bufMiles[i]+"\""+sled+">"+bufMiles[i]+"</option>");
	}
	/*
	for (var ml=0.10; ml<=0.5; ml+=0.10) {
		document.write("<option value=\""+ml+"\">"+ml+"</option>");
	}
	
	for (var mm=0.75; mm<=10.0; mm+=0.25) {
		document.write("<option value=\""+mm+"\">"+mm+"</option>");
	}*/

}


function goMBuff(ml) {
	//var sn = document.getElementById("sltdMBufferID");

	//document.mapform.sltdMBuffer.value = sn[sn.selectedIndex].value;
	document.mapform.sltdMBuffer.value = ml; 
	document.mapform.event.value = "PROCESS_TRACT";
	document.mapform.mapaction.value = "NONE";
	document.getElementById("loading").style.display = "block";
	document.images["select_buffer"].src = ("image/select_buffer_down.gif");
	document.mapform.submit();
}

function paInit() {
	document.frmAddress.hseNumber.focus();
	document.frmCommDist.borough.selectedIndex = 0;
	document.frmPOI.borough.selectedIndex = 0;
	document.frmSubStation.sublines.selectedIndex = -1;
}

	var imageBase = "image/";
	function Clicked(nameoftool){
		resetTools();
		document.images[nameoftool].src = ("image/" + nameoftool + '_down.gif');
	}
	function resetTools(){
		toggleDiv('bufferDiv', 0);
		document.images["zoom_in"].src = "image/zoom_in_up.gif";
		document.images["zoom_out"].src = "image/zoom_out_up.gif";	
		
		document.images["zoom_default"].src = "image/zoom_default_up.gif";	
		document.images["select_new"].src = "image/select_new_up.gif";	
	
		if (document.images["select_add"])			
			document.images["select_add"].src = "image/select_add_up.gif";	
		
		if (document.images["select_remove"])
			document.images["select_remove"].src = "image/select_remove_up.gif";
			
		if (document.images["select_buffer"])
			document.images["select_buffer"].src = "image/select_buffer_up.gif";
				
	}
	function toggleDiv(szDivID, iState){ // 1 visible, 0 hidden

	    if(document.layers){	   //NN4+
	       document.layers[szDivID].visibility = iState ? "show" : "hide";
    	}
	    else if(document.getElementById){	  //gecko(NN6) + IE 5+
            var obj = document.getElementById(szDivID);
    	    obj.style.visibility = iState ? "visible" : "hidden";
    	}
	    else if(document.all){	// IE 4
            document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
    	}
	}

	

function validate(form) {
	if (form.name=="frmAddress") {
		if (form.address.value == "") {
	    	alert("Please input an address to proceed");
	    	document.frmAddress.address.focus();
	    	return;
    	}else if (form.borough.selectedIndex == 0) {
			alert('Please specifiy a borough to proceed');
			document.frmAddress.borough.focus();
			return;
		}
	}
	
    
    if (form.event[1].checked)
    	form.smind.value = "MULTI";
    else
   	 	form.smind.value = "SINGLE";
   	 	
     form.submit();
}

function aTivRd(frm) {
	frm.event[1].checked=true;
}


function showTool(e) {
 	//alert("showt tool called");
 
 
    if (!IE) event=e;
    
	var dmav = document.mapform.mapaction.value;
	if ( (dmav == "ZOOM_IN") || (dmav == "ZOOM_OUT") || (dmav == "SINGLE") || (dmav == "ADD") || (dmav == "REMOVE") ) {
		var laNme;
		
		if (dmav == "ZOOM_IN")
			laNme = "divzIn";
		if (dmav == "ZOOM_OUT")
			laNme = "divzOut";
		if (dmav == "SINGLE")
			laNme = "divsNew";
		if (dmav == "ADD")
			laNme = "divsAdd";
		if (dmav == "REMOVE")
			laNme = "divsRemove";		
			
	//alert("before: " + laNme);			
		var oDiv = document.getElementById(laNme);
		
  //alert("after: " + oDiv);
        oDiv.style.left=event.clientX+11+document.body.scrollLeft;
	    oDiv.style.top=event.clientY+11+document.body.scrollTop;
	    
	    oDiv.style.display= "block";

     	showHideDiv(laNme,1);
    }
}

function showHideDiv(szDivID, iState) {
    if(document.layers) {
      document.layers[szDivID].visibility = iState ? "show" : "hide";
    }
    else if(document.getElementById) {
      var obj = document.getElementById(szDivID);
      obj.style.visibility = iState ? "visible" : "hidden";
    }
    else if(document.all) {
      document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
    }
}


function hideTool() {
	var dmav = document.mapform.mapaction.value;
	if ( (dmav == "ZOOM_IN") || (dmav == "ZOOM_OUT") || (dmav == "SINGLE") || (dmav == "ADD") || (dmav == "REMOVE") ) {
	   var laNme;
		
		if (dmav == "ZOOM_IN")
			laNme = "divzIn";
		if (dmav == "ZOOM_OUT")
			laNme = "divzOut";
		if (dmav == "SINGLE")
			laNme = "divsNew";
		if (dmav == "ADD")
			laNme = "divsAdd";
		if (dmav == "REMOVE")
			laNme = "divsRemove";
	
	  showHideDiv(laNme,0);
    }
}

function loading() {
	if (document.mapform.mapaction.value == "ADD" && ctcount >= 40) {
		alert("A maximum of 40 census tracts can be selected for a profile.\n\nPlease choose other operation tools: like Select a new tract, Remove a tract, etc.");
		return false;
	} else  {
  		document.getElementById("loading").style.display = "block";
  		return true;
  	}
  	
}

function swapCE(ttl) {
	var tools = ["ZOOM_IN", "ZOOM_OUT", "ADD", "REMOVE", "ZOOM_SELECT", "SINGLE" ];
	var gifs = ["zoom_in", "zoom_out", "select_add", "select_remove", "zoom_default", "select_new" ];
	
	for (var i=0; i<tools.length; i++) {
		if (ttl == tools[i])
			return gifs[i];
	}
}
