function checkBrowser(formObj) {
        //alert(navigator.appName + " ---- " + navigator.appVersion);
        if (navigator.appName.indexOf("Netscape") !== -1) {
            if (parseFloat(navigator.appVersion) < 4.5) {
                  alert("Please upgrade your browser to Netscape 4.5 or later!");
                  return false;
            } else {
 				return runVerifyForm(formObj);
			}
        } else if (navigator.appName.indexOf("Microsoft") !== -1) {
            if ((parseFloat(navigator.appVersion.substring((navigator.appVersion.indexOf("MSIE")+4)))) < 4.5) {
                  alert("Please upgrade your browser to IE 4.5 or later!");
                  return false;
            } else {
 				return runVerifyForm(formObj);
			} 
        } else {
 			return runVerifyForm(formObj);
        }
}

function runVerifyForm(formObj) {
		if (formObj.name == "hucForm") {
			return validateHuc(formObj);
		} else if (formObj.name == "epaForm") {
			return validateEPA(formObj);
		} else if (formObj.name == "latLongForm") {
			return validateLatLong(formObj);
		} else {
            return validateZipCityCnty(formObj);
		}
}

function validateZipCityCnty(form) {
    myZipCode = escape(form.pzipcode.value);
	myCity = escape(form.pcity.value);
	myCounty = escape(form.pcounty.value);
	myState = escape(form.pstate.options[form.pstate.selectedIndex].value);
	
    if (myZipCode.length == 0 && myCity.length == 0 && myCounty.length == 0 && myState.length == 0) {
        alert("Enter a ZIP code or City or County and State.");
        return false;
    }
    if (myZipCode.length > 0) {
        if (validateUSZip(myZipCode) == false) {
            alert("ZIP code: numeric characters only and 5 digits!");
			form.pzipcode.value = "";
            return false;
        } else
            return true;
    }       
    else if (myCity.length !== 0 && myState.length == 0) {
      	alert("Enter both City and State.");
        return false;
    }
	else  if (myCounty.length !== 0 && myState.length == 0) {
      	alert("Enter both County and State.");
        return false;
	}
	//else  if ((myCounty.length == 0 && myCity.length == 0) && myState.length !== 0) {
    //  	alert("Enter City or County for selected state.");
    //    return false;
	//} 
	else
        return true;  
}

function validateAddr(form) {
	myAddress2 = escape(form.paddress2.value);
   	myCity2 = escape(form.pcity2.value);
   	myState2 = escape(form.pstate2.options[form.pstate2.selectedIndex].value);
   	myZip2 = form.pzipcode2.value;

    if (myZip2.length > 0) {
        if (validateUSZip(myZip2) == false) {
            alert("ZIP code: numeric characters only and 5 digits!");
       		addrform.pzipcode2.value = "";
            return false;
        }
    }
    if (myAddress2.length == 0 || myCity2.length == 0 || myState2 == "") {
        if (myAddress2.length == 0 ){
            alert("Please fill in Address!");
            return false;}
        if (myCity2.length == 0 ) {
            alert("Please fill in City!");
            return false;}
        if (myState2 == "" ) {
            alert("Please Choose a State!");
            return false;}
        if (myZip2 == "" ) {
            alert("Please enter a Zipcode!");
            return false;}
    }
}

function validateFac(facForm) {
	myFacId = facform.pfacility_id.value;
	myFacName = facform.pfacility_name.value;
	if ((myFacId.substr(0, 15) == "Enter a partial" || myFacId == "") && (myFacName.substr(0, 15) == "Enter a partial" || myFacName == "")) { 
		alert("Please fill in a partial or complete facility ID or name!");
		return false;
	} else {
		if (myFacId.substr(0, 15) == "Enter a partial")
			facform.pfacility_id.value = "";
		if (myFacName.substr(0, 15) == "Enter a partial")
			facform.pfacility_name.value = "";
		return true;
	}
}

function setFacId(form) {
	form.pfacility_id.value = "";
	form.pfacility_name.value = "Enter a partial or complete facility name"
}

function setFacName(form) {
	form.pfacility_name.value = "";
	form.pfacility_id.value = "Enter a partial or complete facility ID"
}

function validateUSZip(strValue) {
   /************************************************
         DESCRIPTION: Validates that a string a United
           States zip code in 5 digit format or zip+4
           format. 99999 or 99999-9999
         PARAMETERS:
           strValue - String to be tested for validity
         RETURNS:
            True if valid, otherwise false.
    *************************************************/
    var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
   //check for valid US Zipcode
   return objRegExp.test(strValue);
}

function cleanLatLong(form) {
	form.platD.value = "";   
    form.platM.value = "";
    form.platS.value = "";
	form.plonD.value = "";   
    form.plonM.value = "";
    form.plonS.value = "";
	form.plat.value = "";
	form.plon.value = "";
}

function validateEPA(form) {
	if ( form.pepaRegionList.selectedIndex == 0 ) {
		alert("Select an EPA region!")
		return false;
	}
	else
		return true;
}

function validateLatLong(form) {
	var myChoiceRadioButton = form.platLong;
	for (var counter = 0; counter < myChoiceRadioButton.length; counter++) {
    	if ( myChoiceRadioButton[counter].checked ) {
    		myChoice = myChoiceRadioButton[counter].value;
     	}
    }
    if (myChoice == "LatLonDCM") {
   		theLat = form.plat.value;
   		theLong = form.plon.value;
		if (isNaN(theLong) || isNaN(theLat) || (theLong.length == 0 & theLat.length == 0)) {
			alert("Please input valid number for latitude and longitude.");
			return false;
    	}
		else if ((theLat < 15) || (theLat > 73)) {
       		alert("Latitude must be between 15 and 73");
       		return false;
    	}
    	//Longitude must be between -190 and -63
    	else if ((theLong < -190) || (theLong > -63)) { 
	       alert("Longitude must be between -190 and -63");
    	   return false;
    	}
		else
			return true;
	}
	else if (myChoice == "LatLonDGR") {
		dDegree=form.platD.value;   
        dMinute=form.platM.value;
        dSecond=form.platS.value;
        theLat =(dDegree * 1.0) + ((dMinute * 1.0) + (dSecond/60))/60;
        dDegree=form.plonD.value;       
        dMinute=form.plonM.value;
        dSecond=form.plonS.value;
        theLong =(dDegree * 1.0) - ((dMinute * 1.0) + (dSecond/60))/60;
		//alert(theLat + ", " + theLong);
		if (isNaN(theLong) || isNaN(theLat) || (theLong == 0 & theLat == 0)) {
			alert("Please input valid number for latitude and longitude.");
			return false;
    	}		
		else if ((theLat < 15) || (theLat > 73)) {
           alert("Latitude must be between 15 and 73");
           return false;
        }
        else if ((theLong < -190) || (theLong > -63)) {
           alert("Longitude must be between -190 and -63");
           return false;
        }	
		else
			return true;
	}
}
	
function cleanLLDGR(form) {
	form.platLong[0].checked = true;
	form.platD.value = "";   
    form.platM.value = "";
    form.platS.value = "";
	form.plonD.value = "";   
    form.plonM.value = "";
    form.plonS.value = "";
}

function cleanLLDCM(form) {
	form.platLong[1].checked = true;
	form.plat.value = "";
	form.plon.value = "";
}

function validateHuc(form) {
	hucCode = form.pcode.value;
	catName = form.pcat_name.value;
    subReg = form.psub_reg.value;
	//state = form.pstate.options[form.pstate.selectedIndex].value;

	if ((hucCode.length == 0 && catName.length == 0 && subReg.length == 0)) {
        alert("Please fill in Huc Code or Catunit Name or Subregion Name!");
        return false;
	}
	//else if ((hucCode.length == 0 && catName.length == 0 && subReg.length == 0) && state !== "0") {
    //    alert("Please fill in Huc Code or Catunit Name or Subregion Name!");
    //    return false;
	//}
	//else if ((hucCode.length == 0 || catName.length !== 0) && state == "-") {
    //    alert("Please select a State!");
    //    return false;
	//}
    else if (hucCode.length !== 0) {
        if (validateUSHuc8(hucCode) == false) {
             alert("Huc Code: numeric characters only and 4 or 8 digits!");
             return false;
        }
        else {
             //if (state == "") {
			 //	alert("Please select a State!");
			//	return false;
			//}
			//else {
				return true;
			//}
			 //if (catName.length == 0)
             //     form.pstate.selectedIndex = -1;
        }
	}
}

function validateUSHuc8(strValue) {
       var objRegExp  = /(^\d{4}$)/;
       //check for valid 8 digit HUC
       if (objRegExp.test(strValue) == false){
            var objRegExp  = /(^\d{8}$)/;
            return objRegExp.test(strValue);
       }
       return true;
}