//function isValid (str) {
	// Are regular expressions supported?
	//var regExSupported = 0;
	//if (window.RegExp) {
		//var tempStr = "a";
		//var tempReg = new RegExp(tempStr);
		//if (tempReg.test(tempStr)) regExSupported = 1;
	//}
	// Compare to regex. Return NOTted result. If regex not supported, just return true
	// allow letters, numbers, period & apostrophe (supports "St. Ann's" or "5th street clinic")
	//if (regExSupported) {
		//var theRegEx = new RegExp("[^\-a-zA-Z\.\' ]"); 
	
		//return ( !theRegEx.test(str));
	//}
		//else return true;
//} 
function isValid(strVal) {
		// should not have these characters:
		//   ?  /   \    =   +   &   <   >
		//    [<>='\"&]
		//  what about  $   #   [  ]  {   }
		var re = /^.*[\[\]\{\}0-9<>&+=\\\/\?\%]+.*$/g;
		return !re.test(strVal);
}
function verifyForm(facilityFormType) {
	var returnValue = true;
	var theForm = document.form1;
	var distance = theForm.distance;
	var zip = theForm.zip;
	var city = theForm.city;
	var state = theForm.state;
	var county = theForm.county;
		//Both regular search-3-Facility.cfm and trpn_facility_form.cfm has the below 3 fields
	var facilityName = theForm.facilityName;
	var factype2 = theForm.factype2;
	var factype3 = theForm.factype3;
	var factype4 = theForm.factype4;
	var factype5 = theForm.factype5;
	var factype6 = theForm.factype6;
	var factype7 = theForm.factype7;
	var displaySSP = theForm.displaySSP.value;
	
	var facilitySubType = theForm.facilitySubType;
	
		//facTypeCls is specific ONLY to search-3-facility.cfm
	var facTypeCls;
	
	var crLf = "\r\n";
	var needZipAndDistanceMsg = "Please input a zip code to execute a search by zip code and distance.";
	var chooseStateWCountyCityMsg = "To search for a city or county, you must also choose a state.  Please choose a state and try your search again.";
	var chooseCityOrCountyWStateMsg = "To search within a state, you must also input either a city or a county.";
	var invalidZipMsg = "The zip code you have entered is not valid. Please enter a 5-digit zip code and try your search again."
	noEntry = "To search for a facility, you must select one of the following criteria ";
	noEntry += "combinations: " + crLf;
	noEntry += "\tZip + Mileage Range" + crLf;
	noEntry += "\tCity + State " + crLf;
	noEntry += "\tState + County " + crLf;
	noEntry += "Please select your location criteria and try your search again.";	
	var strInvStr =  " you entered contains invalid characters. Please use letters only.";
	
	if (facilityFormType != "TRPN") {
		facTypeCls = theForm.facilityTypeClassification;
		factypetwo = theForm.factypetwo;
	}
	
	
	if ((facilityName.value.length > 0) && (!(isValid(facilityName.value)))) {
		if (displaySSP == "true") {
			alert("The other provider name you entered contains invalid characters. Please be sure to use letters only when entering an other provider name.");
		} else {
			alert("The facility name you entered contains invalid characters. Please be sure to use letters only when entering a facility name.");
		}
		facilityName.focus();
		return false;
	} 
	
	//returnValue remains at true;
	//returnValue remains at true; will be set to false by below function calls if need be
	//The not here means if distance.selectedIndex is greater than 0 AND
	//  NEITHER city and state are both input/selected OR count and state are both input/selected.
	//  It means that if both City and State are selected OR county and state are selected
	//  they are OVERRIDING a zip entry.
	if (distance.selectedIndex > 0 && 
		(!((city.value.length > 0 && state.selectedIndex > 0) ||
			  (county.value.length > 0 && state.selectedIndex > 0)))) {
		returnValue = doZipCheck(needZipAndDistanceMsg, invalidZipMsg, theForm, distance, zip, city, state, county);
	} else if((zip.length !== 0)&&(zip.value !== "Zip Code:")) {
		returnValue = doZipCheck(needZipAndDistanceMsg, invalidZipMsg, theForm, distance, zip, city, state, county);		
	} else {
		returnValue = doStateCityCountCheck(strInvStr, chooseStateWCountyCityMsg, chooseCityOrCountyWStateMsg, theForm, city, county, state, zip, distance);
	}
	
	if (facilityFormType == "FAC") {
		if (returnValue) {
			if (facTypeCls[1].checked) facilitySubType.value = factype2[factype2.selectedIndex].text;
			else if (facTypeCls[2].checked) facilitySubType.value = factype3[factype3.selectedIndex].text;
			else if (facTypeCls[3].checked) facilitySubType.value = factype4[factype4.selectedIndex].text;
			else if (facTypeCls[4].checked) facilitySubType.value = factype5[factype5.selectedIndex].text;
			else if (facTypeCls[5].checked) facilitySubType.value = factype6[factype6.selectedIndex].text;
			else if (facTypeCls[6].checked) facilitySubType.value = factype7[factype7.selectedIndex].text;
		}
	}else{
		if (facilityFormType == "TRPN") {
			if (theForm.factype.selectedIndex > 0) {
				facTypeName.value =  theForm.factype[theForm.factype.selectedIndex].text;
			}else{
					theForm.facTypeName.value =  "";
			}
		}
	}
	
	if (returnValue == true) {
		fncShowProgressBar();
	}
	
	return returnValue;
}

function doZipCheck(needZipAndDistanceMsg, invalidZipMsg, theForm, distance, zip, city, state, county) {
	var retVal = true;
	//Any distance chosen and the presumption is searchCriteriaType = 1
	if (zip.length == 0) {
		alert(needZipAndDistance);
		retVal = false;
		zip.focus();
	}else{
		if (!(isValidZipCode(zip.value))) {
			alert(invalidZipMsg);
			retVal = false;
			zip.focus();
		}else{
			city.value = "";
			state.selectedIndex = 0;
			county.value = "";
			theForm.searchCriteriaType.value = 1;
		//fncSetSearchCriteriaType(theForm);
		}
	}
	return retVal;
}
function doStateCityCountCheck(strInvStr, chooseStateWCountyCityMsg, 
		chooseCityOrCountyWStateMsg, theForm,
		city, county, state, zip, distance) {
   var retVal = true;
	if (state.selectedIndex > 0) {
		if (city.value.length > 0 && city.value != "City:") {
			if (isValid(city.value)) {
				//This would be searchCriteriaType of 2
				theForm.searchCriteriaType.value = 2;
				zip.value = "";
				distance.selectedIndex = 0;
				county.value = "";
			}else{
				alert("The city you entered contains invalid characters. Please use letters only.");
				retVal = false;
				city.focus();
			}
		}else{
			if (county.value.length > 0 && county.value != "County:") {
				if (isValid(county.value)) {
					theForm.searchCriteriaType.value = 3;
					zip.value = "";
					distance.selectedIndex = 0;
					city.value = "";
				}else{
					alert("The county you entered contains invalid characters. Please use letters only.");
					retVal = false;
					county.focus();
				}
			}else{

				alert(chooseCityOrCountyWStateMsg);						
				retVal = false; 
				city.focus();
			}
		}
	}else{
		if ((city.value.length > 0 && city.value != "City:") || 
				(county.value.length > 0 && county.value != "County:" )) {
				//This means prompt that state is not selected.
				alert(chooseStateWCountyCityMsg);
				state.focus();
		}else{
			//This means that nothing was selected	
			alert(noEntry);
		}
			
		retVal = false;
	}
	return retVal;
}

function isValidZipCode(strVal) {
   		var re = /^\d{5}([\-]\d{4})?$/;
   		return (re.test(strVal));
}
