//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;
	//}else{
		//alert("regex not suported???")
	//}
	// 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) {
		//alert("regex IS supported.")
			//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(providerFormType) {
	//**************************************************************************************
	// NOTE: at present this function services both search-3-provider.cfm and 
	//    trpn_provider_form.cfm.
	// The following fields need to exist on BOTH forms and be named the same:
	//     distance  (select)
	//     zip       (text input)
	//     city      (text input)
	//     state     (select)
	//     county    (text input)
	//     lname     (text input)
	//     theForm   (page's form)
	//     searchCriteriaType (hidden)
	
	//  The field fname is checked for (exists on search-3-provider.cfm, and does NOT
	//     exist on trpn_provider_form.cfm), and utilized on if providerFormType != "TRPN"
	//**************************************************************************************
	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;
	var lname = theForm.lname;
	var fname;
	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 doctor, 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 (providerFormType != "TRPN") {
		fname = theForm.fname;
	}
	
	//On Search-3-Provider.cfm, there are fields lname and fname
	//On trpn_provider_form, there is only lname
	if (doNamesCheck(providerFormType, strInvStr, theForm, lname, fname) == false) {
		return false;
	}
	
	//returnValue remains at true; will be set to false by below function calls if need be
	if (distance.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);
	}
	
	//Clear these out if so far OK to submit
	if (returnValue == true) {
		if (lname.value == "Last:") lname.value = "";
		
		if (providerFormType == "PRAC") {
			if (fname.value == "First:") fname.value = "";
		
			//Set the names of the specs so that I don't have to requery on results page.
			if (theForm.speccode1.value != '') theForm.speccode1_name.value = theForm.speccode1[theForm.speccode1.selectedIndex].text;
			if (theForm.speccode2.value != '') theForm.speccode2_name.value = theForm.speccode2[theForm.speccode2.selectedIndex].text;
			if (theForm.speccode3.value != '') theForm.speccode3_name.value = theForm.speccode3[theForm.speccode3.selectedIndex].text;
			if (theForm.speccode4.value != '') theForm.speccode4_name.value = theForm.speccode4[theForm.speccode4.selectedIndex].text;
			if (theForm.langcode.value != '') theForm.langcode_name.value = theForm.langcode[theForm.langcode.selectedIndex].text;
		}
		
		fncShowProgressBar();
	}
	
	
	
	return returnValue;
}

function doNamesCheck(providerFormType, strInvStr, theForm, lname, fname) {
	var retVal = true;
	if ((lname.value.length > 0) && (lname.value != "Last:") && (!(isValid(lname.value)))) {
		alert("The last name" + strInvStr);
		lname.focus();
		retVal = false;
    }
	if (providerFormType != "TRPN") {
		if ((fname.value.length > 0) && (fname.value != "First:") && (!(isValid(fname.value)))) {
			alert("The first name" + strInvStr);
			fname.focus();
			retVal = false;
		}
	}
	return retVal;
}

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));
}
