function getMyChoice() {
    var myChoice = "";
	//var myCounter = "";
	//var myList = 0;
	//var myChoiceRadioButton = document.layerForm.byOptions;
//alert(myChoiceRadioButton.length);
//if (myChoiceRadioButton.length>0) 
//{
//	for (var counter = 0; counter < myChoiceRadioButton.length; counter++) {
      
//    	if ( myChoiceRadioButton[counter].checked ) {
//       		myChoice = myChoiceRadioButton[counter].value;
//			myCounter = counter;
//	}
//     }
// }
//else
//{
myChoice = 1;
//}
    //alert(myCounter + " -- "+myChoice);
	return myChoice;
}


//function getSelectedLayer(myChoice) {
//	//alert(document.layerForm.efLayer1.value);
//	//var myOption = document.layerForm.elements[myChoice];
//	var myOption = document.layerForm.efLayer1;
//	//alert (myOption);
//	var mySelectedLayer = "";
//	var myFinalSelect = "";
//	var layerCount = 0;
//	var parentLyr = "everyone";
//	var parentLyrName = "All";
//	//alert(myOption.length);
//	for (var i = 0; i < myOption.options.length; i++) {
//	//for (var i = 0; i < 1; i++){
//		//if (myOption[i].selected) {
//		if (myOption[i].selected) {
//		//if (true){
//			layerCount = layerCount + 1;
//			mySelectedLayer = myOption[i].value;
//			//alert(mySelectedLayer);
//			//alert("myFinalSelect: "+myFinalSelect+" -- mySelectedLayer: "+mySelectedLayer);
//			if (myFinalSelect == "" || layerCount == 1) {
//				//alert("parentlayer: "+parentLyr+" --  " + mySelectedLayer + "___" + mySelectedLayer.indexOf(parentLyr));
//				myFinalSelect = mySelectedLayer;
//			}
//			else if ((myFinalSelect == "map_all#" && mySelectedLayer == "all*") || (myFinalSelect == "all*" && mySelectedLayer == "map_all#")) {
//					alert("Cannot select both Map All and List All.");
//					myFinalSelect = myFinalSelect;
//					myOption[i].selected = false;
//			}
//			else {		// checking for if ALL selected, stop other individual selection
//				//alert(myFinalSelect + "--" + mySelectedLayer + "--" + myOption[i].name);
//				if (layerCount > 1 && mySelectedLayer.indexOf(parentLyr) !== -1 || parentLyr == "all") { 
//					alert("Cannot select any sub-category for \"" + parentLyrName + "\".");
//					myFinalSelect = myFinalSelect;
//					myOption[i].selected = false;
//				}
//				else {
//					myFinalSelect = myFinalSelect + "," + mySelectedLayer;
//				}
//			}
//			// find out prefix/parent category info
//			if (mySelectedLayer.indexOf("all") !== -1) {
//				parentLyrName = myOption[i].name;
//				
//				if (mySelectedLayer == "all*" || mySelectedLayer == "map_all#")
//					parentLyr = "all";
//				else if (parentLyr !== "all") 
//					parentLyr = mySelectedLayer.substring(0,mySelectedLayer.indexOf("all"));
//			}
//		}
//	}
//	//alert(myFinalSelect);
//
//	return myFinalSelect;
//}

//function checkList() {
//
//	var myChoice = getMyChoice();
//	if ( myChoice == "") {
//		alert("Please check a toggle button!"); 
//		return (false);
//    }
//	var mySelectedLayer = getSelectedLayer(myChoice);
//	//alert(mySelectedLayer + " -- " + mySelectedLayer.indexOf("_"));
//	// Check if list only here
//	if (mySelectedLayer.indexOf("*") !== -1) {
//		alert("Only list report is available.  Please click List It button to get list report.");
//		return false;
//	}
//}

//function checkMap() {
//	var myChoice = getMyChoice();
//	if ( myChoice == "") {
//		alert("Please check a toggle button!"); 
//		return (false);
//    }
//	var mySelectedLayer = getSelectedLayer(myChoice);
//	//alert(mySelectedLayer + " -- " + mySelectedLayer.indexOf("_"));
//	// Check if list only here
//	if (mySelectedLayer.indexOf("#") !== -1) {
//		alert("Only map is available.  Please click Map It button to get map.");
//		return false;
//	}
//}

function epaFormSubmit(region) {
	document.epaForm.epaReg.value = region;
	//alert(document.epaForm.cmd.value);
	//var myChoiceRadioButton = document.epaForm.pOutType;
//	for (var counter = 0; counter < myChoiceRadioButton.length; counter++) {
//    	if ( myChoiceRadioButton[counter].checked ) {
//       		mySelectedRadio = myChoiceRadioButton[counter].value;
//		}
//	}
//	var myChoiceLayer = getMyChoice();
//	//alert(mySelectedRadio + "--" + document.epaForm.epaReg.value + "--" + document.epaForm.efLayer.value);
//	if (mySelectedRadio == "Map It") {
//		if (checkBrowser(document.epaForm) !== false)
			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) {

	var myChoice = getMyChoice();

	//var mySelectedLayer = getSelectedLayer(myChoice);

	// turn off for testing here
	//return false;
	//alert(mySelectedLayer);
	//mySelectedLayer = removeAsterisk(mySelectedLayer);
	//alert(formObj.name);
	//formObj.efLayer.value = mySelectedLayer;
	//alert(formObj.name);
	
	//	formObj.efLayer.value = parent.document.layerForm.elements[myChoice].options[parent.document.layerForm.elements[myChoice].selectedIndex].value;
		if (formObj.name == "hucForm") {
			return validateHuc(formObj);
		} else if (formObj.name == "epaForm") {
			return true;
		} else if (formObj.name == "latLongForm") {
			//return validateLatLong(formObj);
			if (validateLatLong(formObj) == true) {
			    myLat = formObj.latitude.value;
			    myLong = formObj.longitude.value;
			    //document.location.href = "http://ese-pa/emef/ef_master.ZoomInByGeography?cmd=ZoomInByLatLong&latitude="+myLat+"&longitude="+myLong;
			    document.location.href = "http://iaspub.epa.gov/emef/ef_master.ZoomInByGeography?cmd=ZoomInByLatLong&latitude="+myLat+"&longitude="+myLong;
			} 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);
		//} 		
		// Added by NZ oct. 2004
		//else if (formObj.name == "addrForm") {
		//	return validateZipCityCntyAddress(formObj);
			
		} else {
            	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;
	}
}

// ValidateZipCityCntyAddress added by NZ oct. 2004
//function validateZipCityCntyAddress(form) {
//	myZipCode = escape(form.zipcode.value);
//	myCity = escape(form.city.value);
//	myCounty = escape(form.county.value);
//	myState = escape(form.state.options[form.state.selectedIndex].value);
//	myAddress = escape(form.address.value);
//	if (myAddress.length !== 0){
////		if (myState.length == 0 || myCity.length == 0) {
////	      	alert("Enter both City and State.");
////    	    return false;
////		} else {
//			form.cmd.value = "ZoomInByAddr";
//			return true;
////		}
//   	 }
//	else if (myZipCode.length > 0) {
//        if (validateUSZip(myZipCode) == false) {
//            alert("ZIP code: numeric characters only and 5 digits!");
//			form.zipcode.value = "";
//            return false;
// //       } else {
////			form.cmd.value = "ZoomInByZip";
////            return true;
//		}
//    }   
//}
    
function validateZipCityCnty(form) {
    myZipCode = escape(form.zipcode.value);
	myCity = escape(form.city.value);
	myCounty = escape(form.county.value);
	myState = escape(form.state.options[form.state.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.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.");
        form.cmd.value = "ZoomInByState";
		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;

    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;
	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 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;
	}
	else
		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;
	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;
	//state = form.state.options[form.state.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!");
             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;
}