// Variables used by all the layer based search functions
var targetLayer = "";
var targetField = "";
var targetValue;
var searchByID; 
var sSQL = "";

// Variables used by the geocoding search functions
var StNo = "";
var Suite = "";
var Prefix = "";
var StName = "";
var StType = "";

// Variables used by the intersection geocoding search
var StName1 = "";
var StName2 = "";

// Variable used by the APN validation function. Contains the county abbreviation if any
var cprefix = "";

/******************************************************
 * The following functions validate and prepare the   *
 * needed variables to be submitted to the server     *
 * These functions are common to POIPage and POIPage2 *
 ******************************************************/
function commonAPNSearch() {
	if (commonChecks())
	{
		with (document.forms[0]) 
		{ 
			POIContent1_TextboxAPN.value = cleanUp(POIContent1_TextboxAPN.value.replace("-", ""));
			if (!apnValidate()) {
					alert("Please enter a valid PIN number.");
					POIContent1_TextboxAPN.focus();
			}
			else
			{
				targetLayer = "Parcels";
				targetField = "APN";
				targetValue = "'" 
				if (cprefix.length == 0)
					targetValue += POIContent1_dropCounty.value 
				targetValue += cleanUp(POIContent1_TextboxAPN.value.replace("-", "")) + "'";
				searchByID = true; 
				sSQL = "";
				return true;
			}
		}
	}
	return false;
}

function commonKIVASearch() {
	if (commonChecks())
	{
		with (document.forms[0]) 
		{ 
			POIContent1_TextBoxKiva.value = cleanUp(POIContent1_TextBoxKiva.value)
			// check to see if kivapin value is not numeric and is not 6 digits long
			if (isNaN(POIContent1_TextBoxKiva.value) || (POIContent1_TextBoxKiva.value.length == 0)) 
			//ML: Removed kivapin length check || !(TextBoxKiva.value.length == 6)
			{
				alert("The KIVAPIN should be numeric.");
				POIContent1_TextBoxKiva.focus();
			}
			else
			{
				targetLayer = "Parcels";
				targetValue = "'" + cleanUp(POIContent1_TextBoxKiva.value) + "'"; // it's stored as a string value not a number
				targetField = "KIVAPIN";
				searchByID = true; 
				sSQL = "";
				return true;
			} 
		}
	}
	return false;
}

function commonAddressSearch() {
	if (commonChecks())
	{
		with (document.forms[0]) 
		{ 
			StName = cleanUp(POIContent1_TextBoxStName.value);
			POIContent1_TextBoxStName.value = StName;
			if (StName == '') 
			{
				alert('The Street Name is a required field!');
				POIContent1_TextBoxStName.focus();
			} 
			else 
			{
				StNo = cleanUp(POIContent1_TextBoxStNo.value);
				Suite = cleanUp(POIContent1_TextBoxSuite.value);
				Prefix = trimAll(POIContent1_DropDownListPrefix.value);
				StType = trimAll(POIContent1_DropDownListStType.value);
				return true;
			}
		}
	}
	return false;
}

function commonIntersectionSearch() {
	if (commonChecks())
	{
		with (document.forms[0]) { 
			StName1 = cleanUp(POIContent1_TextBoxStName1.value);
			POIContent1_TextBoxStName1.value = StName1;
			StName2 = cleanUp(POIContent1_TextBoxStName2.value);
			POIContent1_TextBoxStName2.value = StName2;
			if (StName1 == '') 
			{
				alert('The 1st Street Name is a required field!');
				POIContent1_TextBoxStName1.focus();
			} 
			else if (StName2 == '') 
			{
				alert('The 2nd Street Name is a required field!');
				POIContent1_TextBoxStName2.focus();
			} 
			else 
			{
				searchByID = true; 
				sSQL = "";
				return true;
			}
		} 
	}
	return false;
}

function commonPOISearch() {
	if (commonChecks())
	{
		with (document.forms[0]) 
		{ 
			var strCategory = POIContent1_POIDropCategory.value;
			targetLayer = "Points of Interest - " + strCategory.replace("&","and");
			//alert(targetLayer);
			targetField = "OBJECTID";
			targetValue = POIContent1_dropInterest.value;
			searchByID = true; 
			sSQL = "";
			return true
		} 
	}
	return false;
}


/******************************************************
 * Validates an APN to be numeric or prefixed with a  *
 * valid county abbreviation                          *
 ******************************************************/
function apnValidate() {
	cprefix = "";
	var apn = document.forms[0].POIContent1_TextboxAPN.value;
	if (apn.length > 0)
	{
		if (isNaN(apn))
		{
			// if it's not a number then we check if it contains a valid county prefix
			cprefix = apn.substr(0,2).toUpperCase();
			switch (cprefix)
			{
				case "JA":
					return true;
					break;
				case "CA":
					return true;
					break;
				case "CL":
					return true;
					break;
				case "PL":
					return true;
					break;
				default:
					break;
			}
		}
		else
			return true;
	}
	return false;
}

/******************************************************
 * Utility function to remove any leading or trailing *
 * blank space                                        *
 ******************************************************/
function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
		sString = sString.substring(1, sString.length);

	while (sString.substring(sString.length-1, sString.length) == ' ')
		sString = sString.substring(0,sString.length-1);

	return sString;
}

/******************************************************
 * Utility function to remove any unwanted character  *
 ******************************************************/
function cleanUp(sString) 
{
	// Basically replace all non alphanumeric characters with nothing then trim all blanks
	return trimAll(sString.replace(/[^\w\s]*/gi, ''));
}



