// 03/27/07 added function isValid(str) and tests in submit functions
// 05/01/07 added valid test for pinc, lrk, and plat
// 05/30/08 added jquery functions
// 06/10/08 went into production with jquery and web services
// r.dell

//var wsbase = "http://gis7352/rest/";		// base url for web services
var wsbase = "http://www.gis.catawba.nc.us/rest/";

/* 	document ready functions	*/
$(document).ready(function() {
	$("form").each(function() {		// reset all form values
  	this.reset();
  });
	
	$("p.help").hide();
	
	$('div.joe')													// toggle the help associated w the link
			$("a.link").click(function(){
				var bro = $(this).parent().next();
				bro.toggle();
				return false;						// without this the focus goes to the top of the page
			});
	
	$(function(){
//		setAutoComplete("streetname", "results", "http://gis7352/rest/v1/ws_ims_street.php?format=json&jsonp=?&callback=ws_autosuggest&streetname=");
		setAutoComplete("streetname", "results", "http://www.gis.catawba.nc.us/rest/v1/ws_ims_street.php?format=json&jsonp=?&callback=ws_autosuggest&streetname=");
	});
	
});

function submitPinc(){
	var pinc = document.PincForm.key.value;
	var valid = isValid(pinc);
	if ( valid && (pinc.length == 12 || pinc.length == 16) ) { 
		document.PincForm.submit();
	}else{
		alert(pinc + ' is not a valid Parcel ID.');
	}
	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 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 submitAddress(){
	document.AddressForm.submit();
	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 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 submitLrk(){
	var lrk = document.LrkForm.key.value;
	var valid = isValid(lrk);
	var notnumeric = /\D/.test(lrk);
	if ( ! valid || notnumeric || lrk.length == 0 ) {
		alert(lrk + ' is not a good value.');
	}else{
//		alert(lrk);
		document.LrkForm.submit();
	}
	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 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 valid = isValid(plat);
	var notnumeric = /\D/.test(book);
	var notnumeric2 = /\D/.test(page);
	if ( ! valid || notnumeric || notnumeric2 || book.length == 0 || page.length == 0 ) {
		alert(plat +  ' is not a valid Plat Book and Page.');
	}else{
//		alert(plat);
		document.PlatForm.submit();
	}
	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;
}