// buttons.js
//
// December 18, 2008
//
// maps  website
//
//***********************************************	
//***************** FUNCTIONS *******************
//***********************************************


//************ ZOOM SCALE FUNCTIONS *************

//do zoom scale
function doZoomScale(){
	if(isNav4){
		theForm = document.ZOOMSCALE.document.frmZoomScale;
	} else {
		theForm = document.frmZoomScale;
	}
	zs = parseFloat(theForm.txtzoomscale.value);
	if(zs > 0 ){
		document.frmMain.cmd.value = "ZOOMSCALE";
		document.frmMain.zoomscale.value = zs;
		document.frmMain.submit();
		showLayer("loadLayer");
	}else{
		alert("Please enter a valid map scale.");
	}
}




//****************** SEARCH FUNCTIONS ***********

//show parcel
function showParcel(sPID){
	document.frmMain.cmd.value = "ZOOMPARCEL";
	document.frmMain.pid.value = sPID;
	document.frmMain.submit();
	showLayer("loadLayer");
}

//do buffer
function doBuffer(){
	if(isNav4){
		theForm = document.BUFFER.document.frmBuffer;
	} else {
		theForm = document.frmBuffer;
	}
	sDist = theForm.txtdist.value;
	if((document.frmMain.pid.value == "") && (document.frmMain.monument.value == "")){
		alert("Please select a parcel or monument before using the buffer function.");
	} else {
		if(parseInt(sDist) > 0){
			document.frmMain.cmd.value = "BUFFER";
			document.frmMain.dist.value = sDist;
			document.frmMain.submit();
			showLayer("loadLayer");
		} else {
			alert("Please enter a valid buffer distance.");
		}
	}
}

// do search owner
function doSearchOwner(){
	if(isNav4){
		theForm = document.SEARCH.document.frmSearch;
	} else {
		theForm = document.frmSearch;
	}
	sOwnerL = theForm.txtownerl.value;
	sOwnerF = theForm.txtownerf.value;
	if(sOwnerL == ""){
		alert("Please enter a last name.");
	} else {
		showSearchWin("OWNER",sOwnerL, sOwnerF, "", "0");
		showLayer("loadLayer");
	}
}

// do search owner with make download option indicated by the 1
function doSearchOwner_make_download(){
	if(isNav4){
		theForm = document.SEARCH.document.frmSearch;
	} else {
		theForm = document.frmSearch;
	}
	sOwnerL = theForm.txtownerl.value;
	sOwnerF = theForm.txtownerf.value;
	if(sOwnerL == ""){
		alert("Please enter a last name.");
	} else {
		showSearchWin("OWNER",sOwnerL, sOwnerF, "1", "0");
		showLayer("loadLayer");
	}
}

// do search address
//function doSearchAddress(){
//	if(isNav4){
//		theForm = document.SEARCH.document.frmSearch;
//	} else {
//		theForm = document.frmSearch;
//	}
//	sStNum = theForm.txtstnum.value;
//	sStName = theForm.txtstname.value;
//	if(sStName == ""){
//		alert("Please enter a street name.");
//	} else {
//		showSearchWin("ADDRESS",sStNum, sStName, "", "0");
//		showLayer("loadLayer");
//	}
//}

// do search address
//		showSearchWin("ADDRESS",sStNum, sStName, "", "0");
function doSearchAddress(){
	if(isNav4){
		theForm = document.SEARCH.document.frmSearch;
	} else {
		theForm = document.frmSearch;
	}
//alert ("In search address");
	sStNum = theForm.txtstnum.value;
      sStDir = theForm.txtstdir.value;
	sStName = theForm.txtstname.value;
      sStSuffix = theForm.txtstsuffix.value;
      params = sStNum+";"+sStDir+";"+sStName+";"+sStSuffix;
//alert (params);	
	if(sStName == ""){
		alert("Please enter a street name.");
	} else {
            showSearchWin("ADDRESS",params, "", "", "0");
		showLayer("loadLayer");
	}
}


// do search address with the download option indicated by the 1
//function doSearchAddress_make_download(){
//	if(isNav4){
//		theForm = document.SEARCH.document.frmSearch;
//	} else {
//		theForm = document.frmSearch;
//	}
//	sStNum = theForm.txtstnum.value;
//	sStName = theForm.txtstname.value;
//	if(sStName == ""){
//		alert("Please enter a street name.");
//	} else {
//		showSearchWin("ADDRESS",sStNum, sStName, "1", "0");
//		showLayer("loadLayer");
//	}
//}

// do search address with the download option indicated by the 1
// showSearchWin("ADDRESS",sStNum, sStName, "1", "0");
function doSearchAddress_make_download(){
	if(isNav4){
		theForm = document.SEARCH.document.frmSearch;
	} else {
		theForm = document.frmSearch;
	}
	sStNum = theForm.txtstnum.value;
      sStDir = theForm.txtstdir.value;
	sStName = theForm.txtstname.value;
      sStSuffix = theForm.txtstsuffix.value;
      params = sStNum+";"+sStDir+";"+sStName+";"+sStSuffix;
//alert (params);	
	if(sStName == ""){
		alert("Please enter a street name.");
	} else {
		showSearchWin("ADDRESS",params, "", "1", "0");
		showLayer("loadLayer");
	}
}

// do search PID
function doSearchPID(){
	if(isNav4){
		theForm = document.SEARCH.document.frmSearch;
	} else {
		theForm = document.frmSearch;
	}
	sTwn = theForm.txttwn.value;
	sMap = theForm.txtmap.value;
	sLot = theForm.txtlot.value;
	
	if((sTwn == "") || (sMap == "")){
		alert("Please enter a Parcel ID number.");
	} else {
		showSearchWin("PID",sTwn, sMap, sLot, "0");
		showLayer("loadLayer");
	}
}


// do search city
function doSearchCity(sCity){
	//alert(sCity);
	//if(isNav4){
	//	theForm = document.SEARCH.document.frmSearch;
	//} else {
	//	theForm = document.frmSearch;
	//}
	//sCity = theForm.selcity.options[theForm.selcity.options.selectedIndex].value;
	if(sCity == ""){
		alert("Please select a City name.");
	} else {
		document.frmMain.cmd.value = "ZOOMCITY";
		document.frmMain.city.value = sCity;
		document.frmMain.submit();
		showLayer("loadLayer");
	}
}


// do search road
function doSearchRoad(sRoad){
	if(sRoad == ""){
		alert("Please select a Road name.");
	} else {
		document.frmMain.cmd.value = "ZOOMROAD";
		document.frmMain.road.value = sRoad;
		document.frmMain.submit();
		showLayer("loadLayer");
	}
}


//do search monument
function doSearchMonument(){
	if(isNav4){
		theForm = document.SEARCH.document.frmSearch;
	} else {
		theForm = document.frmSearch;
	}
	sMonument = theForm.selmonument.options[theForm.selmonument.options.selectedIndex].value;
	if(sMonument == ""){
		alert("Please select a Geodetic Monument name.");
	} else {
		document.frmMain.cmd.value = "ZOOMMONUMENT";
		document.frmMain.monument.value = sMonument;
		document.frmMain.submit();
		showLayer("loadLayer");
	}
}


//do advanced search
function doAdvancedSearch(){
	if(isNav4){
		theForm = document.ADVANCEDSEARCH.document.frmAdvancedSearch;
	} else {
		theForm = document.frmAdvancedSearch;
	}
	var check = true;
	acreage = theForm.selacreage.options[theForm.selacreage.options.selectedIndex].value;
	totalvalue = theForm.selvalue.options[theForm.selvalue.options.selectedIndex].value;
      subdivision = theForm.selsub.options[theForm.selsub.options.selectedIndex].value;
      improved = theForm.selimproved.options[theForm.selimproved.options.selectedIndex].value;
	mm1 = theForm.selMM1.options[theForm.selMM1.options.selectedIndex].value;
	yyyy1 = theForm.selYYYY1.options[theForm.selYYYY1.options.selectedIndex].value;
	date1 = yyyy1 + mm1;
	mm2 = theForm.selMM2.options[theForm.selMM2.options.selectedIndex].value;
	yyyy2 = theForm.selYYYY2.options[theForm.selYYYY2.options.selectedIndex].value;
	date2 = yyyy2 + mm2;
	luse = theForm.selluse.options[theForm.selluse.options.selectedIndex].value;
      zeros = "0000";
      deedbook = theForm.txtdeedbook.value;
      deedpage = theForm.txtdeedpage.value;
      if ( (deedbook.length) + (deedpage.length) > 0 ) {
        deedl = (deedbook.length);
        if (deedl < 4) {
          numzeros = 4 - deedl;
          fillzeros = zeros.substring(0,numzeros);
          deedbook = fillzeros + deedbook;
        }
        deedpage = theForm.txtdeedpage.value;
        deedl = (deedpage.length);
        if (deedl < 4) {
          numzeros = 4 - deedl;
          fillzeros = zeros.substring(0,numzeros);
          deedpage = fillzeros + deedpage;
        }
	  deed = deedbook + " " + deedpage;
      }  
      else {
        deed = "";  
      }
      platcabinet = theForm.txtsurvey_platcabinet.value;
      slide = theForm.txtsurvey_slide.value;
      subslide = theForm.txtsurvey_subslide.value;
      if ( (platcabinet.length) + (slide.length) + (subslide.length) > 0 ) {
        survey = platcabinet + "-" + slide + "-" + subslide;
      }
      else {
        survey = "";
      }      
      if ((date1.length > 0) && (date1.length != 6)){
		check = false;
		alert("Please select a month and year for the beginning sale date.");
	}
	if ((date2.length > 0) && (date2.length != 6)){
		check = false;
		alert("Please select a month and year for the ending sale date.");
	}
	if (parseFloat(date1) > parseFloat(date2)){
		check = false;
		alert("Please specify a valid range for the sale date.");
	}
      township = theForm.seltownship.value;
      if ( (township.length) = 0 ) {
        township = "";
      }
      city = theForm.selcity2.value;
      if ( (city.length) = 0 ) {
        city = "";
      }
	test = acreage+totalvalue+date1+date2+luse+deed+survey+township+city+improved+subdivision;
	//params = acreage;totvalue;date1;date2;luse;deed;survey
      //params = acreage+";"+totalvalue+";"+date1+";"+date2+";"+luse+";"+deed+";"+survey;
      makedownload = "make-download-no"
	params = acreage+";"+totalvalue+";"+date1+";"+date2+";"+luse+";"+deed+";"+survey+";"+makedownload+";"+township+";"+city+";"+improved+";"+subdivision;
	
	if(test.length == 0){
		check = false;
		alert("Please enter at least one search criteria.");
	}
	if(check){
		showSearchWin("ADVANCED",params, "", "", "0");
		showLayer("loadLayer");
	}
}

//do advanced search make download option
function doAdvancedSearch_make_download(){
	if(isNav4){
		theForm = document.ADVANCEDSEARCH.document.frmAdvancedSearch;
	} else {
		theForm = document.frmAdvancedSearch;
	}
	var check = true;
	acreage = theForm.selacreage.options[theForm.selacreage.options.selectedIndex].value;
	totalvalue = theForm.selvalue.options[theForm.selvalue.options.selectedIndex].value;
      subdivision = theForm.selsub.options[theForm.selsub.options.selectedIndex].value;
      improved = theForm.selimproved.options[theForm.selimproved.options.selectedIndex].value;
	mm1 = theForm.selMM1.options[theForm.selMM1.options.selectedIndex].value;
	yyyy1 = theForm.selYYYY1.options[theForm.selYYYY1.options.selectedIndex].value;
	date1 = yyyy1 + mm1;
	mm2 = theForm.selMM2.options[theForm.selMM2.options.selectedIndex].value;
	yyyy2 = theForm.selYYYY2.options[theForm.selYYYY2.options.selectedIndex].value;
	date2 = yyyy2 + mm2;
	luse = theForm.selluse.options[theForm.selluse.options.selectedIndex].value;
      zeros = "0000";
      deedbook = theForm.txtdeedbook.value;
      deedpage = theForm.txtdeedpage.value;
      if ( (deedbook.length) + (deedpage.length) > 0 ) {
        deedl = (deedbook.length);
        if (deedl < 4) {
          numzeros = 4 - deedl;
          fillzeros = zeros.substring(0,numzeros);
          deedbook = fillzeros + deedbook;
        }
        deedpage = theForm.txtdeedpage.value;
        deedl = (deedpage.length);
        if (deedl < 4) {
          numzeros = 4 - deedl;
          fillzeros = zeros.substring(0,numzeros);
          deedpage = fillzeros + deedpage;
        }
	  deed = deedbook + " " + deedpage;
      }  
      else {
        deed = "";  
      }
      platcabinet = theForm.txtsurvey_platcabinet.value;
      slide = theForm.txtsurvey_slide.value;
      subslide = theForm.txtsurvey_subslide.value;
      if ( (platcabinet.length) + (slide.length) + (subslide.length) > 0 ) {
        survey = platcabinet + "-" + slide + "-" + subslide;
      }
      else {
        survey = "";
      }
     	if ((date1.length > 0) && (date1.length != 6)){
		check = false;
		alert("Please select a month and year for the beginning sale date.");
	}
	if ((date2.length > 0) && (date2.length != 6)){
		check = false;
		alert("Please select a month and year for the ending sale date.");
	}
	if (parseFloat(date1) > parseFloat(date2)){
		check = false;
		alert("Please specify a valid range for the sale date.");
	}
      township = theForm.seltownship.value;
      if ( (township.length) = 0 ) {
        township = "";
      }
      city = theForm.selcity2.value;
      if ( (city.length) = 0 ) {
        city = "";
      }     	
	test = acreage+totalvalue+date1+date2+luse+deed+survey+township+city+improved+subdivision;
	//params = acreage;totvalue;date1;date2;luse;deed;survey
      makedownload = "make-download-yes"
	params = acreage+";"+totalvalue+";"+date1+";"+date2+";"+luse+";"+deed+";"+survey+";"+makedownload+";"+township+";"+city+";"+improved+";"+subdivision;
	if(test.length == 0){
		check = false;
		alert("Please enter at least one search criteria.");
	}
	if(check){
		showSearchWin("ADVANCED",params, "", "", "0");
		showLayer("loadLayer");
	}
}

// show parcel
function showParcel(sPID){
	document.frmMain.cmd.value = "ZOOMPARCEL";
	document.frmMain.pid.value = sPID;
	document.frmMain.submit();
	showLayer("loadLayer");	
}


// show panel
function showPanel(sPanel) {
	hidePanel();
	showLayer(sPanel);
	document.frmMain.panel.value = sPanel;
}


// hide panel
function hidePanel() {
	sPanel = document.frmMain.panel.value;
	if(sPanel != ""){
		hideLayer(sPanel);
	}
	document.frmMain.panel.value = "";
}


// show search win
function showSearchWin(cmd, param1, param2, param3, startnum){
	theURL = "map_search.asp?cmd="+cmd+"&param1="+param1+"&param2="+param2+"&param3="+param3+"&startnum="+startnum;
	searchWin = window.open(theURL, "Search", "width=450,height=400,toolbar=0,menubar=1,scrollbars=1,resizable=0,left=200,top=0");
	if(searchWin.opener == null){
		searchWin.opener = self;
	}
}

// show identify win
function showIdentifyWin(sX, sY){
	minX = document.frmMain.minX.value;
	minY = document.frmMain.minY.value;
	maxX = document.frmMain.maxX.value;
	maxY = document.frmMain.maxY.value;
	activelayer = document.frmMain.activelayer.value;
	theURL = "map_identify.asp?selX="+sX+"&selY="+sY+"&activelayer="+activelayer+"&minX="+minX+"&minY="+minY+"&maxX="+maxX+"&maxY="+maxY;
	searchWin = window.open(theURL, "Search", "width=450,height=400,toolbar=0,menubar=1,scrollbars=1,resizable=0,left=200,top=0");
	if(searchWin.opener == null){
		searchWin.opener = self;
	}
}

// show cama win
function showCAMAWin(sPID){
	theURL = "map_cama.asp?pid="+sPID;
	searchWin = window.open(theURL, "Search", "width=450,height=400,toolbar=0,menubar=1,scrollbars=1,resizable=0,left=200,top=0");
	if(searchWin.opener == null){
		searchWin.opener = self;
	}
}

// show buffer win
function showBufferWin(){
	theURL = "map_buffer.asp";
	searchWin = window.open(theURL, "Search", "width=450,height=400,toolbar=0,menubar=1,scrollbars=1,resizable=0,left=200,top=0");
	if(searchWin.opener == null){
		searchWin.opener = self;
	}
}



// ************** LAYER LIST FUNCTIONS **********

// set layer list
function setLayerList(iLayer){
	var theLayers = document.frmMain.layerlist.value;
	theImg = eval('document.L'+iLayer);
	theSrc = theImg.src;
	if(theSrc.indexOf("on")!= -1){
		theImg.src = "images/c_box_off.gif";
		theNewLayers = theLayers.substring(0,iLayer-1) + "0" + theLayers.substring(iLayer, theLayers.length);
	} else {
		theImg.src = "images/c_box_on.gif";
		theNewLayers = theLayers.substring(0,iLayer-1) + "1" + theLayers.substring(iLayer, theLayers.length);
	}
	//document.frmMain.cmd.value = "REFRESH";
	document.frmMain.layerlist.value = theNewLayers;
	//document.frmMain.submit();
	//showLayer("loadLayer");
}  

// set active layer
//function setActiveLayer(iLayer){
//	document.frmMain.activelayer.value = iLayer;
//}  

// refresh layer list
function refreshLayerList(){
	document.frmMain.cmd.value = "REFRESH";
	document.frmMain.submit();
	showLayer("loadLayer");
}


//****************** LAYER FUNCTIONS ************

//toggle layer group
function toggleLayerGroup(sID){
	theImg = eval("document.LYRGRP_IMG_"+sID);
	theDiv = document.getElementById("LYRGRP_"+sID);
	visGroups = document.frmMain.visgrps.value;
	//closed rec
	if(theImg.src.indexOf("i_plus.gif") > 0){
		theImg.src = "images/i_minus.gif";
		theDiv.className = "LayerGroupOpen";
		visGroups = visGroups + "G"+sID+ ",";
	//open rec
	}else{
		theImg.src = "images/i_plus.gif";
		theDiv.className = "LayerGroupClosed";
		visGroups = visGroups.replace("G"+sID+",", "");
	}
	document.frmMain.visgrps.value = visGroups;

}

//set active layer
function setActiveLayer(sID){
	theOldRow = document.getElementById("LYR_"+document.frmMain.activelayer.value);
	theOldRow.className = "Row";
	theRow = document.getElementById("LYR_"+sID);
	theRow.className = "RowActive";
	document.frmMain.activelayer.value = sID;
}

//toggle layer 
function toggleLayer(i){
	sVisList = document.frmMain.vislyrs.value;
	theChkBox = eval("document.frmLayers.chkLyr_"+i);
	if(theChkBox != null){
		if(theChkBox.checked){
			sVisList = sVisList + "L" + i + ",";
		} else {
			sVisList = sVisList.replace("L"+i+",", "");
		}
		document.frmMain.vislyrs.value = sVisList;
	}
}

//check visible layers
function checkVisibleLayers(){
	sVal = document.frmMain.vislyrs.value;
	sLyrArray = sVal.split(",");
	for (i=0; i<sLyrArray.length; i++){
		if(sLyrArray[i] != ""){
			sID = sLyrArray[i].replace("L", "");
			theChkBox = eval("document.frmLayers.chkLyr_"+sID);
			if(theChkBox != null){
				theChkBox.checked = true;
			} 
		}
	}
}

//open visible groups
function openVisibleGroups(){
	sVal = document.frmMain.visgrps.value;
	sGrpArray = sVal.split(",");
	for (i=0; i<sGrpArray.length; i++){
		if(sGrpArray[i] != ""){
			sID = sGrpArray[i].replace("G", "");
			theImg = eval("document.LYRGRP_IMG_"+sID);
			theDiv = document.getElementById("LYRGRP_"+sID);
			theImg.src = "images/i_minus.gif";
			theDiv.className = "LayerGroupOpen";
		}
	}
}

//highlight active layer
function highlightActiveLayer(){
	id = document.frmMain.activelayer.value;
	theRow = document.getElementById("LYR_"+id);
	theRow.className = "RowActive";
	document.frmLayers.rbActive.value = id;
	for (i=0; i<document.frmLayers.rbActive.length; i++){
		if(document.frmLayers.rbActive[i].value == id){
			document.frmLayers.rbActive[i].checked = true;
		}
	}
}


//setup layer list
function setupLayerList(){
	checkVisibleLayers();
	openVisibleGroups();
	highlightActiveLayer();
}

// *************** MAP SIZE FUNCTIONS ***********

// set map size
function setMapSize(s){
	document.frmMain.cmd.value = "REFRESH";
	document.frmMain.msize.value = s;
	document.frmMain.submit();
	showLayer("loadLayer");
}  


//****************** HELP FUNCTIONS ************

// show help
function showHelp(){
	if (typeof helpWin == 'undefined'){
		helpWin = window.open("help.htm", "Help", "width=500,height=400,toolbar=0,menubar=0,scrollbars=1,resizable=1,left=340,top=0");
		helpWin.opener = self;
	} else {
		if (helpWin.closed){
			helpWin = window.open("help.htm", "Help", "width=500,height=400,toolbar=0,menubar=0,scrollbars=1,resizable=1,left=340,top=0");
			helpWin.opener = self;
		} else {			
			helpWin.focus();
		}
	}
}


//****************** PRINT FUNCTIONS ***********

// do layout
function doLayout(){
	if(isNav4){
		theForm = document.LAYOUT.document.frmLayout;
	} else {
		theForm = document.frmLayout;
	}
	title = escape(theForm.title.value);
//	if(theForm.orient[0].checked){
//		orient = "LANDSCAPE";
//	}else {
//		orient = "PORTRAIT";
//	}
	paper = theForm.paper.options[theForm.paper.options.selectedIndex].value;
	mscale = theForm.scale.value;
	labelfield = theForm.labelfield.options[theForm.labelfield.options.selectedIndex].value;
	pid = document.frmMain.pid.value;
	minX = document.frmMain.minX.value;
	minY = document.frmMain.minY.value;
	maxX = document.frmMain.maxX.value;
	maxY = document.frmMain.maxY.value;
	layerlist = document.frmMain.vislyrs.value;
	if(theForm.legend[0].checked){
		legend = "YES";
	}else {
		legend = "NO";
	}
//	theURL = "map_print.asp?pid="+pid+"&minX="+minX+"&minY="+minY+"&maxX="+maxX+"&maxY="+maxY+"&title="+title+"&orient="+orient+"&paper="+paper+"&scale="+mscale+"&layerlist="+layerlist+"&labelfield="+labelfield;
// with orient	theURL = "map_print.asp?pid="+pid+"&minX="+minX+"&minY="+minY+"&maxX="+maxX+"&maxY="+maxY+"&title="+title+"&orient="+orient+"&paper="+paper+"&scale="+mscale+"&layerlist="+layerlist+"&labelfield="+labelfield+"&legend="+legend;
      theURL = "map_print.asp?pid="+pid+"&minX="+minX+"&minY="+minY+"&maxX="+maxX+"&maxY="+maxY+"&title="+title+"&paper="+paper+"&scale="+mscale+"&layerlist="+layerlist+"&labelfield="+labelfield+"&legend="+legend;


	printWin = window.open(theURL, "Print", "width=600,height=400,toolbar=0,menubar=1,scrollbars=1,resizable=1,left=20,top=0");
}

