//function for toggling all/none of the RAD layers
//this function is dependent on a boolean variable, bAllChecked,
//  which should be defined in the page including this .js file
var mRegionSubbasin = new Array(21); //
var layerNamesOption="";
var layerNamesOptionTitle="<br \><h3><a href='http://www.epa.gov/waters/data/prog.html'>Water Program Features</a> (Choose one) -- <a href='http://oaspub.epa.gov/waters10/eventstatus'>Display Availability</a></h3>"
var layerNamesCheckbox="";
var layerNamesCheckboxTitle="<br \><h3><a href='http://www.epa.gov/waters/data/prog.html'>Water Program Features</a> (Choose one, multiple, or <input type='button' value=' All ' onClick='selectAllLayers();' style='LEFT: 10px; WIDTH: 36px; TOP: 14px; HEIGHT: 22px'> ) -- <a href='http://oaspub.epa.gov/waters10/eventstatus'>Display Availability</a></h3>"
var dlevel=-1;
var isIE4=false;
var bAllChecked=true;

  if (navigator.appName.indexOf("Microsoft") != -1)
   {
	if (document.getElementById) isIE4 = false;
	else isIE4 = true;
   }


function displayLegendNames(btn) {

   var strHTML="";
   var strTitle="";

   if ( btn==0 ) {
      strHTML=layerNamesOption;
      strTitle=layerNamesOptionTitle;
   } else {
      strHTML=layerNamesCheckbox;
      strTitle=layerNamesCheckboxTitle;
   }

	   if (isIE4)
	   {
			document.all.msgLayer.style.visibility = "visible";
			document.all.msgTitle.style.visibility = "visible";
			document.all.msgTitle.innerHTML = strTitle;
			document.all.msgLayer.innerHTML = strHTML;

       }

       else
       {
			var msgLayer = getObjectByID("msgLayer");
			var msgTitle= getObjectByID("layerTitle");
			if (msgTitle)
			{
				msgTitle.style.visibility = "visible";
				msgTitle.innerHTML = strTitle ;

			}

			if (msgLayer)
			{
				msgLayer.style.visibility = "visible";
				msgLayer.innerHTML = strHTML;
			}
       }

}
function doToggle(){
	//get the form
	theForm = document.frmExtract;
	cnt=theForm.elements.length;
	if (bAllChecked) {
		for (i=0;i<cnt;i++) {
			if (theForm.elements[i].name.substr(0,3)=="chk") {
				theForm.elements[i].checked=true;
			}
		}

		//theForm.elements["btnToggle"].value="Clear All";
	} else {

		for (i=0;i<cnt;i++) {
			if (theForm.elements[i].name.substr(0,3)=="chk") {
				theForm.elements[i].checked=false;
			}
		}

		//theForm.elements["btnToggle"].value="Select All";

	}
	//toggle the flag
	bAllChecked=!bAllChecked;
}

//set the active radio button to the select box that was manipulated
function setOption (selName) {
	//get the form
	theForm = document.frmExtract;
	cnt=theForm.elements.length;
	optVal = "";

	//figure out which selector we're using based on the selector name
	switch (selName) {
		case "selState" :
			optVal=0;
			break;
		case "selCU" :
			optVal=1;
			break;
	}

	//find the correct option, select it, and then break
	for (i=0;i<cnt;i++) {
		if (theForm.elements[i].name=="optLevel" && theForm.elements[i].value==optVal) {
			theForm.elements[i].checked=true;
			break;
		}
	}
}

//jburka 11/5/03
//reload the form with no parameters
//Updated by RGomez on June 22, 2004.
//There is no need to reload the form anymore
function clearForm() {
   var theform = document.frmExtract;
   theform.reset();
   setControls('0');
   selectClearSelection('selCU');
}

function SubBasin () {
   this.huc  = "";
   this.name = "";
}

function selectClearSelection(id) {
   //clear a select content
   var x = document.getElementById(id);
   var i=x.length-1;
   for(; i!=-1; i-- ) {
      x.remove(i);
   }
}

function changeReg(id, idCU) {
   var selReg = document.getElementById(id);
   var subBasins = document.getElementById(idCU);
   var idx = selReg.selectedIndex-1; //array position
   var localSubbasins=mRegionSubbasin[idx];
   var oOption = document.createElement("OPTION");

   if (selReg.selectedIndex !=0 ) {
      selectClearSelection(idCU);
      for (i=0;i<localSubbasins.length;i++) {
         oOption = document.createElement("OPTION");
         oOption.text=localSubbasins[i].name;
         oOption.value=localSubbasins[i].huc;
         try {
            subBasins.add(oOption, null);
         } catch(ex) {
            subBasins.add(oOption);
         }
      }
   } else {
      selectClearSelection(idCU);
   }
}

//enabled and disabled select boxes when
function setControls(btn) {

   document.getElementById("dzform").selRegion.disabled = true;
   document.getElementById("dzform").selCU.disabled     = true;
   document.getElementById("dzform").selState.disabled  = true;

   if ( btn == 0 ) {
      document.getElementById("dzform").selState.disabled  = false;
   } else if ( btn == 1 ) {
      document.getElementById("dzform").selRegion.disabled = false;
      document.getElementById("dzform").selCU.disabled     = false;
   }

   displayLegendNames(btn);

}

/*initializes all the proper values for the page
  and is called onLoad event for the html body  */
function init() {
   initLayers(); //this function is dynamically written into the document as a javascript function

   if (document.getElementById("dzform").optLevelState.checked == true) {
      setControls('0');
   } else {
      setControls('1');
   }

   initSubBasins();  //this function is dynamically written into the document as a javascript function

   changeReg('selRegion','selCU');

}

function getObjectByID(divname) {

  if(document.layers)         return document.layers[divname];
  if(document.all)            return document.all(divname);
  if(document.getElementById) return document.getElementById(divname);

  return null;
}

//Sets all the layer checkboxes on
function selectAllLayers() {
   bAllChecked = true;
   doToggle();
}