var SEARCH_NOT_FOUND_MSG = "American FactFinder could not find any information for the location(s) you specified. Please check your spelling or try another location.";
var SEARCH_QUERY_ERROR_MSG = "American FactFinder could not find any information for the location(s) you specified. See search tips for help in refining your search.";

function hasValidSearchData(streetFld, countyFld, cityTownFld, stateFld, zipFld) {

	var	isValidSearchCriteria = false;
	var errMsg = "";
	var hasStreet	= !stringEmpty(streetFld.value);
	var hasCounty	= !stringEmpty(countyFld.value);
	var hasCityTown	= !stringEmpty(cityTownFld.value);
	var hasState	= !stringEmpty(stateFld.value);
	var hasZip		= !stringEmpty(zipFld.value);
	var hCount		= 0;

	// work around ns4 issue; element 0 is "-- select a state --"
	if (ns4 && !ns5) {
		hasState = (stateFld.selectedIndex != 0);
	}
	
	if (hasZip) {
		if (zipFld.value.length != 5) {
			errMsg = "Enter either a 5-digit ZIP code or erase the ZIP code. Click 'Go'.";
		}
		else {
			zipError = false;
			for (var i = 0; i < 3; i++) {
				if ("0123456789".indexOf(zipFld.value.charAt(i)) == -1) {
					zipError = true;
					break;
				}
			}
			if (zipError == false) {
				for (var i = 3; i < 5; i++) {
					if ("H".indexOf(zipFld.value.charAt(i)) != -1) {
						hCount++;
					}
					if ("0123456789H".indexOf(zipFld.value.charAt(i)) == -1) {
						zipError = true;
						break;
					}
				}
				if (hCount == 1)	{
					zipError = true;
				}
			}
			if ( zipError == true ) {
				errMsg = "Enter either a 5-digit ZIP code or erase the ZIP code. Click 'Go'.";
			}
		}
	}
	
	if (errMsg == "" && hasCounty && !hasEnoughData(countyFld, 2)) {
		if (!hasState) {
			errMsg = "Enter either a state or at least two characters in the county field. Click 'Go'.";
		}
	}
	
	if (errMsg == "" && hasCityTown && !hasEnoughData(cityTownFld, 2)) {
		if (!hasState) {
			errMsg = "Enter either a state or at least two characters in the city or town field. Click 'Go'.";
		}
	}
	
	if (errMsg == "") {
		if (hasStreet) {
			isValidSearchCriteria = hasZip || (hasCityTown && hasState);
			errMsg = "Enter a street address, city and state, or a street address and ZIP code. Click 'Go'.";
		}
		else {
			isValidSearchCriteria = hasCounty || hasCityTown || hasState || hasZip;
			errMsg = "Enter information in one or more fields. Click 'Go'.";
		}
	}

	if (!isValidSearchCriteria) {
		alert(errMsg);
	}
	
	return isValidSearchCriteria;
}

 
function hasValidSearchDataAddress(streetFld, cityTownFld, stateFld, zipFld) {

	var	isValidSearchCriteria = false;
	var errMsg = "";
	var hasStreet	= !stringEmpty(streetFld.value);
	var hasCityTown;
	if (cityTownFld) {
		hasCityTown	= !stringEmpty(cityTownFld.value);
	}
	else {
		hasCityTown = false;
	}
	var hasState;
	if (stateFld) {
		hasState	= !stringEmpty(stateFld.value);
	}
	else {
		hasState = false;
	}
	var hasZip		= !stringEmpty(zipFld.value);
	var hCount		= 0;

	// work around ns4 issue; element 0 is "-- select a state --"
	if (ns4 && !ns5) {
		hasState = (stateFld.selectedIndex != 0);
	}
	
	if (hasZip) {
		if (zipFld.value.length != 5) {
			errMsg = "Enter either a 5-digit ZIP code or erase the ZIP code. Click 'Go'.";
		}
		else {
			zipError = false;
			for (var i = 0; i < 3; i++) {
				if ("0123456789".indexOf(zipFld.value.charAt(i)) == -1) {
					zipError = true;
					break;
				}
			}
			if (zipError == false) {
				for (var i = 3; i < 5; i++) {
					if ("H".indexOf(zipFld.value.charAt(i)) != -1) {
						hCount++;
					}
					if ("0123456789H".indexOf(zipFld.value.charAt(i)) == -1) {
						zipError = true;
						break;
					}
				}
				if (hCount == 1)	{
					zipError = true;
				}
			}
			if ( zipError == true ) {
				errMsg = "Enter either a 5-digit ZIP code or erase the ZIP code.";
			}
		}
	}
	
	if (errMsg == "" && hasCityTown && !hasEnoughData(cityTownFld, 2)) {
		if (!hasState) {
			errMsg = "Enter either a state or at least two characters in the city or town field. Click 'Go'.";
		}
	}
			
	if (errMsg == "") {
		if (hasStreet) {
			isValidSearchCriteria = hasZip || (hasCityTown && hasState);
			errMsg = "Enter a street address, city and state, or a street address and ZIP code. Click 'Go'.";
		}
		else {
			isValidSearchCriteria = hasCityTown || hasState || hasZip;
			errMsg = "Enter information in one or more fields. Click 'Go'.";
		}
	}

	if ( !hasStreet ) {
		errMsg = "Enter a street address, city and state, or a street address and ZIP code. Click 'Go'.";
	    isValidSearchCriteria = false;
	}


	if (!isValidSearchCriteria) {
		alert(errMsg);
	}
	
	return isValidSearchCriteria;
}

function hasValidSearchDataAddressForCWS(streetFld, cityTownFld, stateFld, zipFld) {

	var	isValidSearchCriteria = false;
	var errMsg = "";
	var hasStreet	= !stringEmpty(streetFld.value);
	var hasZip		= !stringEmpty(zipFld.value);
	var hCount		= 0;

	if (hasZip) {
		if (zipFld.value.length != 5) {
			errMsg = "Enter a 5-digit ZIP code.";
		}
		else {
			zipError = false;
			for (var i = 0; i < 3; i++) {
				if ("0123456789".indexOf(zipFld.value.charAt(i)) == -1) {
					zipError = true;
					break;
				}
			}
			if (zipError == false) {
				for (var i = 3; i < 5; i++) {
					if ("H".indexOf(zipFld.value.charAt(i)) != -1) {
						hCount++;
					}
					if ("0123456789H".indexOf(zipFld.value.charAt(i)) == -1) {
						zipError = true;
						break;
					}
				}
				if (hCount == 1)	{
					zipError = true;
				}
			}
			if ( zipError == true ) {
				errMsg = "Enter a 5-digit ZIP code.";
			}
		}
	}
	else {
		errMsg = "Enter a 5-digits ZIP code or a street address and ZIP code. Click 'Go'.";
	}
	
	if (errMsg != "") {
		alert(errMsg);
		return false;
	}

	if (hasStreet) {
		isValidSearchCriteria = hasZip;
		errMsg = "Enter a street address and ZIP code. Click 'Go'.";
	}
	else {
		isValidSearchCriteria = true;
	}

	if (!isValidSearchCriteria) {
		alert(errMsg);
		return false;
	}

	return true;
}


function hasValidSearchDataNameSearch(countyFld,stateFld) {
		
	var	isValidSearchCriteria = true;
	var errMsg = "";
	
	var hasCounty	= !stringEmpty(countyFld.value);
	var hasState	= !stringEmpty(stateFld.value);	
	var hCount		= 0;

	// work around ns4 issue; element 0 is "-- select a state --"
	if (ns4 && !ns5) {
		hasState = (stateFld.selectedIndex != 0);
	}
	
	if (errMsg == "" && hasCounty && !hasEnoughData(countyFld, 2)) {
		if (!hasState) {
			errMsg = "Enter either a state or at least two characters in the city/town, county, or zip field. Click 'Go'.";
			isValidSearchCriteria = false; 
		}
	}
	if (errMsg == "" && !hasCounty && !hasState) {
		errMsg = "Enter either a state or at least two characters in the city/town, county, or zip field. Click 'Go'.";
		isValidSearchCriteria = false; 
	}

	if (!isValidSearchCriteria) {
		alert(errMsg);
	}	
	return isValidSearchCriteria;
}

function hasEnoughData(Fld, minLength) {
	return (Fld.value.length > minLength) ? true : (Fld.value.indexOf("*") == -1);
}