// parcel.js 08/24/06 R.Dell
// js functions that are used by the ArcIMS parcel (Real Estate Server) web site
// 03/27/07 added function isValid(str) and tests in submit functions

var div1 = "<div style=\"POSITION: relative; border: solid; border-width: thin; text-align: left; margin-left: 20px; margin-right: 20px;  padding-left: 20px; background-color: #DDDCDD;\">";
//var div1 = "<div style=\"POSITION: relative; border: solid; border-width: thin; text-align: center; margin-left: 20px; margin-right: 20px;  padding: 5px; background-color: #DDDCDD;\">";
//var div1 = "<div style=\"POSITION: relative; border: solid; border-width: thin; padding: 5px; background-color: #DDDCDD;\">";
var nbsp = "&nbsp;&nbsp;";
var cmd = "<input type=\"hidden\" name=\"Cmd\" value=\"query\">";

function enterPinc(){
	var frm=document.getElementById("search");
	var content = "";
	content = div1;
	//content += "<form name=\"PincForm\" action=\"parcel_main.asp\" id=\"test1\">";
	content += "<form name=\"PincForm\" action=\"parcel_main.asp\">";
	content += "<b>Enter a Parcel ID:</b>";
	content += nbsp;
	content += "<input type=\"text\" id=\"pinc_id\" name=\"key\" size=\"16\" value=\"\">";
	content += nbsp;
	content += "<input type=\"button\" value=\"Find\" name=\"Submit\" onclick=\"submitPinc()\">"; 
	content += cmd;
	content += "<input type=\"hidden\" name=\"type\" value=\"P\">";
	content += "</form>";
	content += "<p class=\"note\">Example: <b><i>374013030654</i></b>";
	content += "<br>Note: Catawba County Parcel ID's are normally 12 digits. When a building owner is different than the parcel owner, the building Parcel ID has a four character extension.</p>"
	content += "</div>";
	frm.innerHTML = content;
	//document.forms[0]["TextBox1"].focus();
	//frm["pinc"].focus();
	//var xxx = document.getElementById("test1");
	//alert(xxx.nodeName);
	//alert(frm.nodeName);
	document.PincForm.key.focus();
	return;
}

function submitPinc(){
	var pinc = document.PincForm.key.value;
	if ( pinc.length == 12 || pinc.length == 16 ) { 
		document.PincForm.submit();
	}else{
		alert(pinc + ' is not a valid Parcel ID.');
	}
	return;		
}

function enterOwner(){
	var frm=document.getElementById("search");
	var content = "";
	content = div1;
	content += "<form name=\"OwnerForm\" action=\"owner_query.asp\">";
	content += "<b>Enter a last name then a first name, or enter the first portion of a Owner's last name.</b><br><br>";
	content += "<b>Owner (Last First):</b>";
	content += nbsp;
	content += "<input type=\"text\" id=\"owner_id\" name=\"owner\" size=\"30\" value=\"\">";
	content += nbsp;
	content += "<input type=\"button\" value=\"Find\" name=\"Submit\" onclick=\"submitOwner()\">";
	content += "<input type=\"hidden\" name=\"type\" value=\"F\">"; 
	content += "</form>";
	content += "<p class=\"note\">Examples: <i><b>Lee</b></i> or <i><b>smith john</b></i> or <i><b>smith j</b></i>";
	content += "<br>Note: Searches may be in upper or lower case, minimum of 3 characters. Do not use commas.</p>";
	content += "</div>";
	frm.innerHTML = content;
	document.OwnerForm.owner.focus();
	return;
}

function submitOwner(){
	var owner = document.OwnerForm.owner.value;
	var valid = isValid(owner);
	if ( valid && owner.length > 2 ) { 
		document.OwnerForm.submit();
	}else if ( valid) {
		alert('The Owner Name must be a minimum of 3 characters.');
	}
	return;		
}

function enterOwnerPart(){
	var frm=document.getElementById("search");
	var content = "";
	content = div1;
	content += "<form name=\"OwnerPartForm\" action=\"owner_query.asp\">";
	content += "<b>Enter any portion of a Owners Name.</b><br><br>";
	content += "<b>Owner:</b>";
	content += nbsp;
	content += "<input type=\"text\" id=\"ownerPart_id\" name=\"owner\" size=\"30\" value=\"\">";
	content += nbsp;
	content += "<input type=\"button\" value=\"Find\" name=\"Submit\" onclick=\"submitOwnerPart()\">";
	content += "<input type=\"hidden\" name=\"type\" value=\"P\">";
	content += "</form>";
	content += "<p class=\"note\">Examples: <i><b>LLC</b></i> or <i><b>church</b></i> or <i><b>Lee</b></i>";
	content += "<br>Note: If you are are entering a last name and first name use the format Last First";
	content += "<br>Note: Searches may be in upper or lower case, minimum of 3 characters. Do not use commas.</p>";
	content += "</div>";
	frm.innerHTML = content;
	document.OwnerPartForm.owner.focus();
	return;
}

function submitOwnerPart(){
	var owner = document.OwnerPartForm.owner.value;
	var valid = isValid(owner);
	if ( valid && owner.length > 2 ) { 
		document.OwnerPartForm.submit();
	}else if ( valid) {
		alert('The Owner Name must be a minimum of 3 characters.');
	}
	return;		
}

function enterStreet(){
	var frm=document.getElementById("search");
	var content = "";
	content = div1;
	content += "<form name=\"StreetForm\" action=\"address_query.asp\">";
	content += "<b>Enter any portion of a Street Name, without the number.</b>";
	content += nbsp;
	content += nbsp;
	content += "<a href=\"misc/addressing.html\" style=\"text-decoration:underline;\">County Addressing Conventions</a>";
	content += "<br><br><b>Street: </b>";
	content += nbsp;
	content += "<input type=\"text\" id=\"street_id\" name=\"street\" size=\"40\" value=\"\">";
	content += nbsp;
	content += "<input type=\"button\" value=\"Find\" name=\"Submit\" onclick=\"submitStreet()\">";
	content += "<input type=\"hidden\" name=\"type\" value=\"S\">"; 
	content += "</form>";
	content += "<p class=\"note\">Example: <i><b>Center</b></i> rather than <b>N Center</b> or <b>S Center</b> that is required in the address search.";
	content += "<br>The County Addressing Conventions document contains examples of <b>NC</b> and <b>US</b> route names.";
	content += "<br>Note: Searches may be in upper or lower case, minimum of 2 characters.</p>";
	content += "<a href=\"misc/street_names_alpha.pdf\" style=\"text-decoration:underline\";>Alphabetic List of Street Names</a> (pdf)&nbsp;&nbsp;";
	content += "<a href=\"misc/street_names_by_city.pdf\" style=\"text-decoration:underline\";>Street Names by City</a> (pdf)<br><br>";
	content += "</div>";
	frm.innerHTML = content;
	document.StreetForm.street.focus();
	return;
}

function submitStreet(){
	var street = document.StreetForm.street.value;
	var valid = isValid(street);
	if ( valid && street.length > 1 ) { 
		document.StreetForm.submit();
	}else if ( valid) {
		alert('The Street Name must be a minimum of 2 characters.');
	}
	return;		
}

function enterAddress(){
	var frm=document.getElementById("search");
	var content = "";
	content = div1;
	content += "<form name=\"AddressForm\" action=\"address_query.asp\">";
	content += "<b>Enter a parcel address.</b>";
	content += nbsp;
	content += nbsp;
	content += "Addresses must use the abbreviations found in the <a href=\"misc/addressing.html\" style=\"text-decoration:underline;\">County Addressing Conventions</a>";
	content += "<br><br><b>House No: </b> <input type=\"text\" id=\"addr_id\" name=\"bldg_no\" size=\"6\" value=\"\">";
	content += " <b>Street Name:</b> <input type=\"text\" name=\"street\" size=\"40\" value=\"\">";
	content += nbsp;
	content += "<input type=\"submit\" value=\"Find\" name=\"Submit\">";
	content += "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"help.html#address\" style=\"text-decoration:underline;\">Address Help</a><br>";
	content += "<input type=\"hidden\" name=\"type\" value=\"A\">"; 
	content += "</form>";
	content += "<p class=\"note\">Examples: Enter <i><b>3407</b></i> in the House No box and <i><b>Plateau</b></i> in the Street Name box, or you can search just on the street name; <i><b>Plateau</b></i> or <i><b>N Center</b></i> or  <i><b>20th st cir</b></i> or <i><b>1st av</b></i>"; 
	content += "<br>The County Addressing Conventions document contains examples of <b>NC</b> and <b>US</b> route names. ";
	content += "<br>Note: The House No, or Street Name is optional, and you may use just the first part of a Street Name.";
	content += "<br>Searches may be in upper or lower case. Do not put a House No, City or State in the <b>Street Name</b> box</p>";
	content += "<a href=\"misc/street_names_alpha.pdf\" style=\"text-decoration:underline\";>Alphabetic List of Street Names</a> (pdf)&nbsp;&nbsp;";
	content += "<a href=\"misc/street_names_by_city.pdf\" style=\"text-decoration:underline\";>Street Names by City</a> (pdf)<br><br>";
	content += "</div>";
	frm.innerHTML = content;
	document.AddressForm.bldg_no.focus();
	return;
}

//function submitAddress(){
// because I use a submit button the action on the form is executed on submit
// I use the submit button because it is executed when the enter key is hit
//	var street = document.AddressForm.street.value;
//	if ( street.length > 2 ) { 
//		document.AddressForm.submit();
//	}else{
//		alert('The Street Name must be a minimum of 3 characters.');
//	}
//	return;		
//}

function enterLandmark(){
	var frm=document.getElementById("search");
	var content = "";
	content = div1;
	content += "<form name=\"LandmarkForm\" action=\"landmark_query.asp\">";
	content += "<b>Enter any portion of a Landmark or Business Name.</b><br><br>";
	content += "<b>Landmark/Business Name:</b>";
	content += nbsp;
	content += "<input type=\"text\" id=\"landmark_id\" name=\"landmark\" size=\"30\" value=\"\">";
	content += nbsp;
	content += "<input type=\"button\" value=\"Find\" name=\"Submit\" onclick=\"submitLandmark()\">";
	content += "</form>";
	content += "<p class=\"note\">Examples: <i><b>burger</b></i> or <i><b>park</b></i>";
	content += "<br>Note: Searches may be in upper or lower case, minimum of 3 characters.</p>";
	content += "</div>";
	frm.innerHTML = content;
	document.LandmarkForm.landmark.focus();
	return;
}

function submitLandmark(){
	var landmark = document.LandmarkForm.landmark.value;
	var valid = isValid(landmark);
	if ( valid && landmark.length > 2 ) { 
		document.LandmarkForm.submit();
	}else if ( valid) {
		alert('The Business/Landmark Name must be a minimum of 3 characters.');
	}
	return;		
}

function enterLrk(){
	var frm=document.getElementById("search");
	var content = "";
	content = div1;
	content += "<form name=\"LrkForm\" action=\"parcel_main.asp\">";
	content += "<b>Enter a LRK:</b>";
	content += nbsp;
	content += "<input type=\"text\" id=\"lrktb_id\" name=\"key\" size=\"6\" value=\"\">";
	content += nbsp;
	content += "<input type=\"button\" value=\"Find\" name=\"Submit\" onclick=\"submitLrk()\">"; 
	content += cmd;
	content += "<input type=\"hidden\" name=\"type\" value=\"L\">";
	content += "</form>";
	content += "<p class=\"note\">Examples: <i><b>9422</b></i> or <i><b>1260</b></i>";
	content += "</div>";
	frm.innerHTML = content;
	document.LrkForm.key.focus();
	return;
}

function submitLrk(){
	var lrk = document.LrkForm.key.value;
	var notnumeric = /\D/.test(lrk);
	if ( notnumeric || lrk.length == 0 ) {
		alert(lrk + ' is not a good value.');
	}else{
//		alert(lrk);
		document.LrkForm.submit();
	}
	return;		
}

function enterSubdiv(){
	var frm=document.getElementById("search");
	var content = "";
	content = div1;
	content += "<form name=\"SubdivForm\" action=\"subdiv_query.asp\">";
	content += "<b>Enter a Subdivision Name or the first portion of a Subdivision name.</b><br><br>";
	content += "<b>Subdivision:</b>";
	content += nbsp;
	content += "<input type=\"text\" id=\"subdiv_id\" name=\"subdiv\" size=\"30\" value=\"\">";
	content += nbsp;
	content += "<input type=\"button\" value=\"Find\" name=\"Submit\" onclick=\"submitSubdiv()\">";
	content += "</form>";
	content += "<p class=\"note\">Example: <i><b>rock barn</b></i>";
	content += "<br>Note: Searches may be in upper or lower case. Do not use punctuation.</p>";
	content += "</div>";
	frm.innerHTML = content;
	document.SubdivForm.subdiv.focus();
	return;
}

function submitSubdiv(){
	var subdiv = document.SubdivForm.subdiv.value;
	var valid = isValid(subdiv);
	if ( valid && subdiv.length > 1 ) { 
		document.SubdivForm.submit();
	}else if ( valid) {
		alert('What you entered is Not a good value.');
	}
	return;		
}

function enterPlat(){
	var frm=document.getElementById("search");
	var content = "";
	content = div1;
	content += "<form name=\"PlatForm\" action=\"parcel_main.asp\">";
	content += "<b>Enter a Book and Page.</b><br><br>";
	content += "<b>Plat Book: </b><input type=\"text\" id=\"plat_id\" name=\"book\" size=\"4\" value=\"\">";
	content += nbsp;
	content += "<b>Plat Page: </b><input type=\"text\" name=\"page\" size=\"4\" value=\"\">";
	content += nbsp;
	content += "<input type=\"button\" value=\"Find\" name=\"Submit\" onclick=\"submitPlat()\">"; 
	content += cmd;
	content += "<input type=\"hidden\" name=\"key\" value=\"\">";
	content += "<input type=\"hidden\" name=\"type\" value=\"PB\">";
	content += "</form>";
	content += "<p class=\"note\">Example: <b><i>19 40</i></b>&nbsp;&nbsp;&nbsp; Note: Both fields are required.</p></b>";
	content += "</div>";
	frm.innerHTML = content;
	document.PlatForm.book.focus();
	return;
}

function submitPlat(){
	// the user has to click the find button to get here
	var book = document.PlatForm.book.value;
	var page = document.PlatForm.page.value;
	var plat = book + ' ' + page;
	var notnumeric = /\D/.test(book);
	var notnumeric2 = /\D/.test(page);
	if ( notnumeric || notnumeric2 || book.length == 0 || page.length == 0 ) {
		alert(plat +  ' is not a valid Plat Book and Page.');
	}else{
//		alert(plat);
		document.PlatForm.key.value = plat;
		document.PlatForm.submit();
	}
	return;		
}

function enterTile(){
	var frm=document.getElementById("search");
	var content = "";
	content = div1;
	content += "<form name=\"TileForm\" action=\"parcel_main.asp\">";
	content += "<b>Enter a Tile Number: </b><input type=\"text\" id=\"tile_id\" name=\"key\" size=\"6\" value=\"\">";
	content += nbsp;
	content += "<input type=\"button\" value=\"Find\" name=\"Submit\" onclick=\"submitTile()\">"; 
	content += cmd;
	content += "<input type=\"hidden\" name=\"type\" value=\"T\">";
	content += "</form>";
	content += "<p class=\"note\">Examples: <i><b>376704</b></i> or <i><b>277001</b></i>";
	content += "<br>Note: Tile numbers are 6 digits. Every parcel number, within a tile, begins with that tile number.</p>";
	content += "</div>";
	frm.innerHTML = content;
	document.TileForm.key.focus();
	return;
}

function submitTile(){
	var tile = document.TileForm.key.value;
	var notnumeric = /\D/.test(tile);
	if ( notnumeric || tile.length != 6 ) {
		alert(tile + ' is not a good value.');
	}else{
		document.TileForm.submit();
	}
	return;		
}

function enterScale(){
	var frm=document.getElementById("search");
	var content = "";
	content = div1;
	content += "<form name=\"ScaleForm\" action=\"parcel_main.asp\">";
	content += "<b>The current printed map scale is " + document.Variables.current_scale.value + " feet/per inch</b><br><br>";
	content += "<b>Set the printed map Scale 1 inch =  </b><input type=\"text\" id=\"scale_id\" name=\"scale\" size=\"4\" value=\"\"> <b>feet</b>";
	content += nbsp;
	content += "<input type=\"button\" value=\"GO\" name=\"Submit\" onclick=\"submitScale()\">"; 
	content += "<input type=\"hidden\" name=\"Cmd\" value=\"setscale\">";
	content += "</form>";
	content += "<br>Note: The scale must be between 20 and 2000 feet.";
	content += "</div>";
	frm.innerHTML = content;
	document.ScaleForm.scale.focus();
	return;
}

function submitScale(){
	var scale = document.ScaleForm.scale.value;
	var notnumeric = /\D/.test(scale);
	if ( notnumeric || scale < 20 || scale > 2000 ) {
		alert(scale + ' is not a good value.');
	}else{
			document.ScaleForm.submit();
	}
	return;
}

function refreshLayers() {
// when I put this function in function.js the generated code is unbable to find it ??
// this function builds the the items in frmlayers, all the form items are hidden
// the items indicate if a layer is tured on or off
// when the function is executed the visible command is submitted to map_logic.asp
// I pulled may hair out to be able to do this, after doing it it is not that diffcult, such is life.

	var layervisible;
	var xx;
	var hidden_item="";
	var frm;
	var layercount = document.Variables.layercount.value
	
	frm=document.getElementById("frmlayers_id");	//use id to get a reference to the form
	hidden_item+="<input type=\"hidden\" name=\"Cmd\" value=\"visible\">";	// I had to set the cmd here for it to work
	for ( xx = 1; xx <= layercount; ++xx ) {			// for each layer in the layer toc
		layervisible = eval("document.frmlayer_toc.lyrVis" + xx + ".checked");
		hidden_item+="<input type=\"hidden\" name=\"lyr" + xx;	// this works, getting the quotes correct was diffcult
		hidden_item+="\" value=\"" + layervisible;
		hidden_item+="\" >";
	}
	frm.innerHTML = hidden_item;
	document.frmlayers.submit();
//	alert('frmlayers form length = ' + document.forms.frmlayers.length);		//ok
	return;
}

function VoterLocator() {
// call the Voter Locator ArcIMS application
	var street = document.frmVoter.street.value;
	var city = document.frmVoter.city.value;
	if ( street > '' && city > '' ) {
		var dimensions;
		dimensions = GetScreenDimensions();
		document.frmVoter.width.value = dimensions[0];
		document.frmVoter.height.value = dimensions[1];
		document.frmVoter.submit();
	}else{
		alert('There is not enough information to link to the Voter Locator web site. A building number, street, or city is unkown for this parcel.  ');
	}
	return;		
}

function SchoolLocator(school) {
// call the School Locator ArcIMS application
	var street = document.frmSchool.street.value;
	var city = document.frmSchool.city.value;
	if ( street > '' && city > '' ) {
		var dimensions;
		dimensions = GetScreenDimensions();
		document.frmSchool.width.value = dimensions[0];
		document.frmSchool.height.value = dimensions[1];
		document.frmSchool.school.value = school;
		document.frmSchool.submit();
	}else{
		alert('There is not enough information to link to the School Locator web site. A building number, street, or city is unkown for this parcel.  ');
	}
	return;
}

function startInstructions(){
	var frm=document.getElementById("init");
	var content = "<p>To begin, click on one of the search options from the selection on your left and enter information in the box that will appear below the map.</p>";
	frm.innerHTML = content;
	return;
}

function isValid(str) {
	//test for quotes in the seach string, they will break the sql query
	var notallowed = "'";
  for (var i=0; i< str.length; i++) {
  	if (str.charAt(i) == notallowed) {
  		alert('remove the quotes from your input');	
    	return false;
    }	
  }
return true;
}

function parcel_initLatLong(){
	//setup and display the lat/long table and form
	// added 02/12/08
	var frm=document.getElementById("search");
	var content = "";
	content = div1;
	content += "<form name=\"latlongfrm\" id=\"llfrm\" action=\"LatLongReport.asp\">";
	content += "<input type=\"submit\" value=\"Print Lat/Long Report\" name=\"Submit\" id = \"llbtn\">" + nbsp; 
	content += "<b>Click on the map to get the lat/long of a point, scroll down to see the results.</b><br>";
	content += "</form>";
	content += "<table class=\"words12\" cols =\"4\" width =\"400\" id=\"lltbl\">";
	content += "<tr><td colspan=2><b>Decimal Degrees</b></td><td colspan=2><b>Degrees Minutes Seconds</b></td></tr>";	
	content += "</table>";
	
	//content += "<b>Click on the map to get the lat/long of a point</b><br>";
	//content += "<table class=\"words10\" cols =\"4\" width =\"400\" id=\"lltbl\">";
	//content += "<tr><td colspan=2><b>Decimal Degrees</b></td><td colspan=2><b>Degrees Minutes Seconds</b></td></tr>";	
	//content += "</table>";
	//content += "<form name=\"latlongfrm\" id=\"llfrm\" action=\"LatLongReport.asp\">";
	//content += "<input type=\"submit\" value=\"Print Lat/Long Report\" name=\"Submit\" id = \"llbtn\">" + nbsp; 
	//content += "</form>";
	
	content += "</div>";
	frm.innerHTML = content;
	document.latlongfrm.Submit.focus();
	return;
}

function parcel_showLatLon(latdd,londd,latdms,londms) {
  //alert(latitude + "," + longitude);
  var div2=document.getElementById("llfrm");
  var button1=document.getElementById("llbtn");
  var newitem = "<input type=hidden name=dd value=" + latdd + "|" + londd + ">";
  newitem += "<input type=hidden name=dms value=" + latdms + "|" + londms + ">";
  var newnode=document.createElement("div");
  newnode.innerHTML=newitem;
  //alert(div2.nodeName);		// nodenames are FORM, BUTTON, IMG etc
  //the insertBefore was failing in IE and working in FF, in FF the nodename was FORM, in IE it was IMG
  //in the initLatLong function I had given the form the id of latlong. that conflicted the image name LATLONG set in parcel_main.asp
  //giving the form id a unique name solved the problem. that took most of one day to figure out
  div2.insertBefore(newnode,button1);
  
  // creates the display of the lat/long points
	var tblBody = document.getElementById("lltbl");
	var newRow = tblBody.insertRow(-1);
  var newCell0 = newRow.insertCell(0);
  newCell0.innerHTML = latdd
  var newCell1 = newRow.insertCell(1);
  newCell1.innerHTML = londd;
  var newCell2 = newRow.insertCell(2);
  var latdms2 = latdms.split("!");
  newCell2.innerHTML = latdms2[0] + " " + latdms2[1] + "' " + latdms2[2] + "''";
  var newCell3 = newRow.insertCell(3);
  var londms2 = londms.split("!");
  newCell3.innerHTML = londms2[0] + " " + londms2[1] + "' " + londms2[2] + "''";
	return;
}	