



function verifyCheckBoxes() {
	var formObj = document.UserForm;
	//var Geo =      formObj.StateBox.value;
    var list = formObj.StateBox
	var index = list.selectedIndex;
	var Geo = list.options[index].value;
	
	//var County =     formObj.CountyBox.value;
    var list = formObj.CountyBox
	var index = list.selectedIndex;
	var County;
	if (index == -1) {
		County = "000";
	}
	else {
		County = list.options[index].value;
	}
	
	//var Compare = formObj.CompareBox.value;
    var list = formObj.CompareBox
	var index = list.selectedIndex;
	var Compare = list.options[index].value;
	
	var okToSubmit = 0;
	var listForError = "";
	
	if (Geo == "**") {
		listForError = listForError + "State\n";
	}
	if (County == "***") {
		listForError = listForError + "County\n";
	}
	if (Compare == "**") {
		listForError = listForError + "Comparison\n";
	}
	
	//return okToSubmit;
	return listForError;
}


function submitQS() {	
	//alert("in submit()");
	var formObj = document.UserForm;
   
	var list = formObj.StateBox
	var index = list.selectedIndex;
	var Geo = list.options[index].value;
	
	var list = formObj.CountyBox
	var index = list.selectedIndex;
	var County;
	if (index == -1) {
		County = "000";
	}
	else {
		County = list.options[index].value;
	}
	
	//if (formObj.CountyBox.disabled == true) {
	//	County = "000";
	////	alert ("County Box is disabled, County = "+County);
	//}
	
	var list = formObj.CompareBox;
	var index = list.selectedIndex;
	var Compare = list.options[index].value;
	var listForError = verifyCheckBoxes() ;
		
	if (listForError != "") {
		alert("Please make a selection for each category before continuing.");
	}
	else {
		var queryString = Geo+"&"+County+"&"+Compare+"&1&0&1"; //sort var, sort order, report type
		formObj.target=null;
		formObj.target='_self';
		formObj.action = "/cgi-bin/ratetrendbyarea/rtarea.pl?"+queryString;
		formObj.submit();
	}
}

function submitQS2() {	
	//alert("in submit()");
	var formObj = document.UserForm;
   
	var list = formObj.StateBox
	var index = list.selectedIndex;
	var Geo = list.options[index].value;
	
	var list = formObj.CountyBox
	var index = list.selectedIndex;
	var County;
	if (index == -1) {
		County = "000";
	}
	else {
		County = list.options[index].value;
	}
	
	//if (formObj.CountyBox.disabled == true) {
	//	County = "000";
	////	alert ("County Box is disabled, County = "+County);
	//}
	
	var list = formObj.CompareBox;
	var index = list.selectedIndex;
	var Compare = list.options[index].value;
	var listForError = verifyCheckBoxes() ;
	
	
	var paramArray = GetParams();
	var Sort = paramArray[3];
	var SortOrder = paramArray[4];
	var Report = 1; // regular report
		
	if (listForError != "") {
		alert("Please make a selection for each category before continuing.");
	}
	else {
		var queryString = Geo+"&"+County+"&"+Compare+"&"+Sort+"&"+SortOrder+"&"+Report; //sort var, sort order, report type
		formObj.target=null;
		formObj.target='_self';
		formObj.action = "/cgi-bin/ratetrendbyarea/rtarea.pl?"+queryString;
		formObj.submit();
	}
}

/*
* new way
* John Stephens re-wrote it to make it work in Netscape, then re-wrote it again to support IE 5.0
*/
function WriteCountySelectBox(){
	//alert("write county select box");
//Function : 	WriteCountySelectBox
//Purpose :	To assign the available options in the CountySelectBox based upon the Selected  State in the State List Box
	
   var formObj = document.UserForm;
	//Assign State List Box Object
	var stateForm = formObj.StateBox;
	//Assign County List Box Object
	var countyForm = formObj.CountyBox;
	//Obtain the value of the Selected Index in the State List Box 
	var selectedStateFips = stateForm.options[stateForm.selectedIndex].value;
	//Assign the Starting Index for the for loop that will be used to access the variables in the CountyName array
	//The County Name Array contains All of the US Counties indexed by their State/County Fips
	var forLoopStart = selectedStateFips + "000";
	//Assign the Ending Index for the for loop that will be used to access the variables in the CountyName array
	var forLoopEnd = selectedStateFips + "999";
	//alert("entering if loops");
	
	var countyNames = new Array();
	var counties = new Array();
	var CcountyNames = new Array();
	countyForm.disabled = false;
	
	//Read counties in and sort them BEFORE adding to options
	var j=0; //[1] is already used for Entire State	
	for (var i=forLoopStart; i<=forLoopEnd; i++) {
		//If the Array spot has an entry
		if (seerStatCountyName[i]) {
			//countyNames.push(seerStatCountyName[i]+"###"+i);
			countyNames[j] = (seerStatCountyName[i]+"###"+i);
			j++;
		}
	}
	
	countyNames.sort();
	
	//Checks specific cases and inserts appropriate selections at beginning of countyNames array
	if (selectedStateFips == "**") {
		var selections = new Array(CHOOSE_BEGINNING + "choose county"  + CHOOSE_END + "###***", "Entire State###000");
		var CcountyNames = selections.concat(countyNames);
		countyForm.selectedIndex = 0;	
		countyForm.disabled = true;
	}
	else if (selectedStateFips == "00") { // Entire US
		var selections = new Array(CHOOSE_BEGINNING + "choose county"  + CHOOSE_END + "###***", "U.S. (Total)###000");
		var CcountyNames = selections.concat(countyNames);
		countyForm.selectedIndex = 1;
		countyForm.disabled = true;
	}			
	else if (selectedStateFips == "11") { // DC - don't add entire state
		var selections = new Array(CHOOSE_BEGINNING + "choose county"  + CHOOSE_END + "###***");
		var CcountyNames = selections.concat(countyNames);
		//countyNames.unshift(stateForm.options[stateForm.selectedIndex].text+"###"+stateForm.options[stateForm.selectedIndex].value); 
		// this is really awesome.  It takes what's in the box and uses that to fill in this next box - so if we change District of Columbia then it automatically changes here!
		countyForm.selectedIndex = 1;
		countyForm.disabled = true;
	}
	else if ( ( selectedStateFips == "15" ) ) { //||  (selectedStateFips == "02") ) { // Hawaii - disable
		var selections = new Array(CHOOSE_BEGINNING + "choose county"  + CHOOSE_END + "###***", "Entire State###000");
		var CcountyNames = selections.concat(countyNames);
		countyForm.selectedIndex = 1;
		countyForm.disabled = true;
	}
	else { 
		var selections = new Array(CHOOSE_BEGINNING + "choose county"  + CHOOSE_END + "###***", "Entire State###000");
		var CcountyNames = selections.concat(countyNames);
		countyForm.selectedIndex = 0;
		countyForm.disabled = false;
	}
		
	countyForm.options.length = CcountyNames.length; 
	
	for (var i=0; i<CcountyNames.length; i++){
	     //name and fips finder	
		var name = CcountyNames[i].substring(0, CcountyNames[i].indexOf('#'));	
		var longFips = CcountyNames[i].substring(CcountyNames[i].lastIndexOf('#')+1, CcountyNames[i].length);
		var longFipsLength = longFips.length;
		var fips = longFips.substring(longFipsLength-3, longFipsLength);
		
				
	     //new county constructor call -- seems to reduce "flashing" in Netscape
		counties[i] = new county(name, fips);
		
		//alert (counties[i].value);		
	}
		
	for (var j=0; j<counties.length; j++){
	     //Populates countyForm with appropriate counties		
		countyForm.options[j].text = counties[j].name;
		countyForm.options[j].value = counties[j].value;	
	}

	// This is only in this writecountyselectbox, not the age dist one or peer counties
	updateCompareBox(); //need to clear out last state
}

function county(name, fips){
   //county constructor -- two dimensional array	
	this.name = name;
	this.value = fips	
}



function GetParams()
{
    var sLocation = new String(window.location);     //property contains domain, directory, 
                                                     //filename of the document plus the params (and the "?" before the params).
    var sParamsList = new String(sLocation.substr(sLocation.lastIndexOf("?")+1));
    return sParamsList.split('&');
}

function UpdateSelected()
{
	//alert ("in update selected");
        var VisibleForm = document.UserForm;
	//var HiddenFormVar = document.hiddenForm;
	
	var CurrentState = VisibleForm.StateBox;
	var CurrentCounty = VisibleForm.CountyBox;
	var CurrentCompare = VisibleForm.CompareBox;
	//alert(CurrentRace);
	
	paramArray = GetParams();
	var CurrentStateHidden = paramArray[0]; //named area - NOT geo
	var CurrentCountyHidden = paramArray[1];
	var lastparam = paramArray[2];
	var lastparamarray = lastparam.split('#');
	var CurrentCompareHidden = lastparamarray[0];
//	alert("current state, current county, current compare: "+CurrentStateHidden+" "+CurrentCountyHidden+" "+CurrentCompareHidden);
	
	
	for (var i=0; i<CurrentState.length; i++)
	{
	//	alert ("CurrentState ["+i+"] - "+CurrentState.options[i].value);
	  if (CurrentStateHidden == CurrentState.options[i].value) 
	  {
	    CurrentState.selectedIndex=i;
	    break;
	  }
	}
	WriteCountySelectBox();
	for (var i=0; i<CurrentCounty.length; i++)
	{
	  if (CurrentCountyHidden == CurrentCounty.options[i].value) 
	  {
	    CurrentCounty.selectedIndex=i;
	    break;
	  }
	}
	
	updateCompareBox();
	for (var i=0; i<CurrentCompare.length; i++)
	{
	  if (CurrentCompareHidden == CurrentCompare.options[i].value) 
	  {
	    CurrentCompare.selectedIndex=i;
	    break;
	  }
	}
  		
}

function updateCompareBox() {
//	alert ("in updateCompareBox()");
	var County = document.UserForm.CountyBox;
	var Compare = document.UserForm.CompareBox;
	var currentSelected = Compare.selectedIndex;
	if (currentSelected == -1) {
		currentSelected = 0;
	}
	var currentName = Compare.options[currentSelected].text;
	
	
	var State = document.UserForm.StateBox;
	var index = State.selectedIndex;
	var StateFIPS_Alpha = State.options[index].value;
	var StateFIPS = StateFIPS_Alpha * 1;
	
	var StateFIPSNum = 0;
	
	var forLoopStart = StateFIPS;
	var forLoopEnd = StateFIPS;
	
	if (County.selectedIndex > 1) { //0 is the "choose"
		for (var i=forLoopStart; i<=forLoopEnd; i++) {
			var StateFIPSNum = i;
		}
	//	alert ("sfnum = "+StateFIPSNum);
		Compare.options[2] = new Option(StateNameAndFIPS[StateFIPSNum]);
		Compare.options[2].value = StateFIPS;
	}
	else {
		Compare.length = 0;
		Compare.options[0] = new Option(CHOOSE_BEGINNING + "choose comparison" + CHOOSE_END);
		Compare.options[0].value = "**";
		
		Compare.options[1] = new Option("United States");
		Compare.options[1].value = "00";
	}
	
	Compare.selectedIndex = 0;
	if (StateFIPS == "02" || StateFIPS == "11" || StateFIPS == "15") {
		Compare.selectedIndex = 1;
	}
	else if (currentSelected == "1") {
		Compare.selectedIndex = 1;
	}
	else if (currentSelected == "2") {
	 	var lenCompare = Compare.length;
	 	if (lenCompare > 2) {
	//	if (currentName == Compare.options[2].text) {
			Compare.selectedIndex = 2;
		}
	}
}