function appName()
{
  var count = "";
  var theName = "";
	var myAppRadioButton = document.newform.subject;
	for (var i = 0; i < myAppRadioButton.length; i++) {
	    if ( myAppRadioButton[i].checked ) {
		   theName = myAppRadioButton[i].value;
		   count = i;
		}
	}
	//alert(count + " -- "+theName);
	return theName;
}

function noAction(newform) {
//      alert("no action!");
        return;
}


function epaFormSubmit(region) {
	document.epaForm.epaReg.value = region;
	document.epaForm.appl.value = appName();
	//alert(document.epaForm.appl.value);
	if (document.epaForm.appl.value == "emtri")
	   alert("EPA region is not available for the TRI application.");
	else if (document.epaForm.appl.value == "wme")
		 alert("EPA region is not available for the WME application.");
	else if (document.epaForm.appl.value == "pesticide") 
		 alert("The Pesticide application can only Zoom in by city, county or state.");  
  else
	   document.epaForm.submit();

}

function checkBrowser(formObj) {
        //alert(navigator.appName + " ---- " + navigator.appVersion);
	
	//alert(formObj.name);
        if (navigator.appName.indexOf("Netscape") !== -1) {
            if (parseFloat(navigator.appVersion) < 5.0) {
                  alert("Please upgrade your browser to Netscape 6.0 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) {
//alert(formObj.name);

		if (formObj.name == "hucForm") {
			return validateHuc(formObj);
		} else if (formObj.name == "epaForm") {
//			return validateEPA(formObj);
			return true;
		} else if (formObj.name == "latLongForm") {
			formObj.appl.value = appName();
			//alert(formObj.appl.value);
			//return validateLatLong(formObj);
			if (validateLatLong(formObj) == true) {
			    myLat = formObj.latitude.value;
			    myLong = formObj.longitude.value;
			    appl = formObj.appl.value;
			    //alert("http://ese-pa/emfront/front_master.ZoomInByGeography?cmd=ZoomInByLatLong&latitude="+myLat+"&longitude="+myLong+"&appl="+appl);
			    //document.location.href = "http://ese-pa/emfront/front_master.ZoomInByGeography?cmd=ZoomInByLatLong&latitude="+myLat+"&longitude="+myLong+"&appl="+appl;
			    document.location.href = "http://iaspub.epa.gov/enviro/front_master.ZoomInByGeography?cmd=ZoomInByLatLong&latitude="+myLat+"&longitude="+myLong+"&appl="+appl;
			    //formObj.cmd.value="ZoomInByLatLong";
			    //return true;
			} else {
			    return false;
			}
		} else if (formObj.name == "cdForm") {
	  	return validateCD(formObj);
		} else if (formObj.name == "addrForm") {
			return validateAddr(formObj);
		} else if (formObj.name == "usaStateForm") {
			return validateUsaStates(formObj);
		} else if (formObj.name == "facForm") {
			return validateFac(formObj);			
		} 
		
		  else {
			//alert(formObj.name);
      return validateZipCityCnty(formObj);		
		}
}

function noListForUS(form) {
	if (form.byUsaState[2].checked == false) {
		alert("There is no report for US.");
		return false;
	} else
		return true;
}

//function validateUsaStates(form) {
//	if (form.byUsaState[2].checked == true) {
//		var myOption = form.state;
//		var mySelectedState = "";
//		//alert(myLayerOption.options.length);
//		for (var i = 0; i < myOption.options.length; i++) {
//			if (myOption[i].selected) {
//				if (mySelectedState == "")
//					mySelectedState = myOption[i].value;
//				else 
//	   				mySelectedState = mySelectedState + "," + myOption[i].value;
//			}
//		}
//		//alert(mySelectedState);
//		form.stateList.value = mySelectedState;
//		if (mySelectedState.length == 0) {
//			alert("Select one or more states.");
//			return false;
//		} else {
//			return true;
//		}
//	} else {
//		return true;
//		
//	}
//}

    
function validateZipCityCnty(form) {
  myZipCode = escape(form.zipcode.value);
  //  alert(myZipCode);
	myCity = escape(form.city.value);
	myCounty = escape(form.county.value);
	myState = escape(form.state.options[form.state.selectedIndex].value);
	form.appl.value = appName();
  //alert(form.appl.value);
  
  if (form.appl.value == "pesticide"){
  	if (myZipCode.length > 0){
  	   alert("The Pesticide application can only Zoom in by city, county or state"); 
  	   return false;}
  	if (myState.length !== 0){  
    	if (myState !== "CA" && myState !== "OR" && myState !== "WA"){
    		alert("Only California, Oregon and Washington are available for the Pesticide application");
    		return false;}
    	else{
    	 if (myCity.length !== 0)
    	   form.cmd.value = "ZoomInByCity";
       else if (myCounty.length !== 0)
    	   form.cmd.value = "ZoomInByCounty";	
       else 
    	   form.cmd.value = "ZoomInByState";
  		 return true;}
    }
  	} 
 	
    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.zipcode.value = "";
			form.zipcode.focus();
            return false;
        } else {
			form.cmd.value = "ZoomInByZip";
            return true;
		}
    }   
     
    else if (myCity.length !== 0) {
		if (myState.length == 0) {
	      	alert("Enter both City and State.");
    	    return false;
		} else {
			form.cmd.value = "ZoomInByCity";
			return true;
		}
    }
	else  if (myCounty.length !== 0) {
		if (myState.length == 0) {
      		alert("Enter both County and State.");
        	return false;
		} else {
			form.cmd.value = "ZoomInByCounty";
			return true;
		}
	}
	else  if ((myCounty.length == 0 && myCity.length == 0) && myState.length !== 0) {
      	//alert("Enter City or County for selected state.");
      	if (form.appl.value == "wme"){
      		alert("Enter City or County for selected state.");
      		return false;
      	}
      else
        form.cmd.value = "ZoomInByState";
        //alert(form.cmd.value);
		return true;
	} 
	else
        return true;  
}

function validateAddr(form) {
	myAddress = escape(form.address.value);
   	myCity = escape(form.city.value);
   	myState = escape(form.state.options[form.state.selectedIndex].value);
   	myZip = form.zipcode.value;
   	form.appl.value = appName();			
		//alert(form.appl.value);
		
    if (form.appl.value == "pesticide"){
    	alert("The Pesticide application can only Zoom in by city, county or state");
    return false;} 
    		
    if (myZip.length > 0) {
        if (validateUSZip(myZip) == false) {
            alert("ZIP code: numeric characters only and 5 digits!");
       		form.zipcode.value = "";
       		form.zipcode.focus();
            return false;
        }
    }
    if (myAddress.length == 0 || myCity.length == 0 || myState == "" || myZip == "") {
        if (myAddress.length == 0 ){
            alert("Please fill in Address!");
            return false;}
        if (myCity.length == 0 ) {
            alert("Please fill in City!");
            return false;}
        if (myState == "" ) {
            alert("Please Choose a State!");
            return false;}            
        if (myZip == "" ) {
            alert("Please enter a Zipcode!");
            return false;}       
    }
}

function validateFac(form) {
	myFacId = form.facility_id.value;
	myFacName = form.facility_name.value;
	form.appl.value = appName();
	//alert(form.appl.value);
  if (form.appl.value == "pesticide"){
   	alert("The Pesticide application can only Zoom in by city, county or state");
  return false;} 	
	
	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 ( form.pgm_sys.options[form.pgm_sys.selectedIndex].value == "" ) {
	//    alert("Please select a database.");
	//    return false;
	} else {
		if (myFacId.substr(0, 15) == "Enter a partial")
			form.facility_id.value = "";
		if (myFacName.substr(0, 15) == "Enter a partial")
			form.facility_name.value = "";
		return true;
	}
}

function setFacId(form) {
	form.facility_id.value = "";
	form.facility_name.value = "Enter a partial or complete facility name"
}

function setFacName(form) {
	form.facility_name.value = "";
	form.facility_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 cleanCityCntyState(form) {
	form.city.value = "";
	form.county.value = "";
	form.state.value = "";
}

function cleanZipCnty(form) {
	form.zipcode.value = "";
	form.county.value = "";
} 

function cleanZipCity(form) {
	form.zipcode.value = "";
	form.city.value = "";
}

function cleanHucNames(form) {
	form.cat_name.value = "";
	form.sub_name.value = "";
}

function cleanHucCode(form) {
	form.code.value = "";
}

function cleanLatLong(form) {
	form.latD.value = "";   
    form.latM.value = "";
    form.latS.value = "";
	form.lonD.value = "";   
    form.lonM.value = "";
    form.lonS.value = "";
	form.lat.value = "";
	form.lon.value = "";
}

function validateEPA(form) {
//	if ( form.epaReg.value == "" ) {
//		alert("Select an EPA region!")
//		return false;
//	}
		return true;
}

function validateCD(form) {
	myCD = form.cd.value;
	if (myCD.length == 0) {
		alert("Enter a Congressional District number!");
		return false;
	} else if ( form.state.options[form.state.selectedIndex].value == "" ) {
		alert("Select a State!")
		return false;
	}
	else
		return true;
}

function validateLatLong(form) {
	var myChoiceRadioButton = form.latLong;
	form.appl.value = appName();
	
  if (form.appl.value == "pesticide"){
    alert("The Pesticide application can only Zoom in by city, county or state");
  return false;} 	
	
	for (var counter = 0; counter < myChoiceRadioButton.length; counter++) {
    	if ( myChoiceRadioButton[counter].checked ) {
    		myChoice = myChoiceRadioButton[counter].value;
     	}
    }
    if (myChoice == "LatLonDCM") {
   		theLat = form.lat.value;
   		theLong = form.lon.value;
		//alert(theLat + ", " + theLong);
		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 {
			form.latitude.value = theLat;
			form.longitude.value = theLong;
			return true;
		}
	}
	else if (myChoice == "LatLonDGR") {
		dDegree=form.latD.value;   
        dMinute=form.latM.value;
        dSecond=form.latS.value;
        theLat =(dDegree * 1.0) + ((dMinute * 1.0) + (dSecond/60))/60;
        dDegree=form.lonD.value;       
        dMinute=form.lonM.value;
        dSecond=form.lonS.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 {
			form.latitude.value = theLat;
			form.longitude.value = theLong;
			return true;
		}
	}
}
	
function cleanLLDGR(form) {
	form.latLong[0].checked = true;
	form.latD.value = "";   
    form.latM.value = "";
    form.latS.value = "";
	form.lonD.value = "";   
    form.lonM.value = "";
    form.lonS.value = "";
}

function cleanLLDCM(form) {
	form.latLong[1].checked = true;
	form.lat.value = "";
	form.lon.value = "";
}

function validateHuc(form) {
	hucCode = form.code.value;
	catName = form.cat_name.value;
    subReg = form.sub_name.value;
    form.appl.value = appName();
    //alert(form.appl.value);
	//state = form.state.options[form.state.selectedIndex].value;
	
  if (form.appl.value == "pesticide"){
    alert("The Pesticide application can only Zoom in by city, county or state");
  return false;} 	
  
  if (form.appl.value == "wme" || form.appl.value == "emtri" ){
    alert("The map will be displayed around the centroid point");
    } 	

	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!");
             form.code.value = "";
             form.code.focus();
             return false;
        }
        else {
             //if (state == "") {
			 //	alert("Please select a State!");
			//	return false;
			//}
			//else {
			//alert(hucCode);
			if (hucCode.length == 8)
				form.cmd.value = "ZoomInByCat";
			else if (hucCode.length == 4)
				form.cmd.value = "ZoomInBySubreg";
			return true;
			//}
			 //if (catName.length == 0)
             //     form.state.selectedIndex = -1;
        }
	} else {
		if (catName.length !== 0) {
			form.cmd.value = "ZoomInByCat";
		}
		else if (subReg.length !== 0) {
			form.cmd.value = "ZoomInBySubreg";
		}
	}
}

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;
}

function removeAsterisk(inString) {
	var outString = "";
	var myString = "";
	for (var i = 0; i < inString.length; i++) {
		myString = inString.substring(i, i+1);
		if (myString == "*" || myString == "#")
			outString = outString;
		else
			outString = outString + myString;
	}
	return outString;
}