// last updated 12/04/07


function undisplayThis(id) {
    // SOURCE:  http://support.internetconnection.net/CODE_LIBRARY/Javascript_Show_Hide.shtml
	//undisplay data within this (id) tag

	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function displayThis(id) {
	//display data within this (id) tag
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'inline';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'inline';
		}
		else { // IE 4
			document.all.id.style.display = 'inline';
		}
	}
}


function displayThisAlso(id){	
	//execute the displayThis function -- see onclick attributes in body of html
	displayThis(id);
}





function submitNA() {
	//Client-side form validation for National Database form	


	//determine which radio button with name=view is checked, and assign that value to a variable named "NAview"
	//source:  http://www.webdevelopersnotes.com/tips/html/finding_the_value_of_a_radio_button.php3
	for (var i=0; i < document.main.view.length; i++)
	   {
	   if (document.main.view[i].checked)
	      {
	      var NAview = document.main.view[i].value;
	      }
	   }


	//determine which radio button with name=us_st is checked, and assign that value to a variable named "NAus_st"
	for (var i=0; i < document.main.us_st.length; i++)
	   {
	   if (document.main.us_st[i].checked)
	      {
	      var NAus_st = document.main.us_st[i].value;
	      }
	   }


	//determine which radio button with name=UStables is checked, and assign that value to a variable named "NAus_table"
	for (var i=0; i < document.main.UStables.length; i++)
	   {
	   if (document.main.UStables[i].checked)
	      {
	      var NAus_table = document.main.UStables[i].value;
	      }
	   }

	//determine which radio button with name=STtables is checked, and assign that value to a variable named "NAst_table"
	for (var i=0; i < document.main.STtables.length; i++)
	   {
	   if (document.main.STtables[i].checked)
	      {
	      var NAst_table = document.main.STtables[i].value;
	      }
	   }

	//determine which radio button with name=maptype is checked, and assign that value to a variable named "maptype"
	for (var i=0; i < document.main.maptype.length; i++)
	   {
	   if (document.main.maptype[i].checked)
	      {
	      var maptype = document.main.maptype[i].value;
	      }
	   }


    //Get index value of select boxes, convert to numeric "Number(string)", and adjust to actual value  
    //SOURCE:  http://www.quirksmode.org/js/select.html and Flanagan, page 165

	var startYr = Number(document.main.year_in.selectedIndex)+1968;
	var endYr = Number(document.main.year_out.selectedIndex)+1968;



	var ageMin = document.main.minage.selectedIndex;
	   		if (ageMin == 0) {
				minAge = 0;
	      	}
	   		else if (ageMin == 1) {
				minAge = 15;
	      	}
	   		else if (ageMin == 2) {
				minAge = 25;
	      	}
	   		else if (ageMin == 3) {
				minAge = 35;
	      	}
	   		else if (ageMin == 4) {
				minAge = 45;
	      	}
	   		else if (ageMin == 5) {
				minAge = 55;
	      	}
	   		else if (ageMin == 6) {
				minAge = 65;
	      	}
	   		else if (ageMin == 7) {
				minAge = 75;
	      	}
	   		else if (ageMin == 8) {
				minAge = 85;
	      	}


	var ageMax = document.main.maxage.selectedIndex;
	   		if (ageMax == 0) {
				maxAge = 14;
	      	}
	   		else if (ageMax == 1) {
				maxAge = 24;
	      	}
	   		else if (ageMax == 2) {
				maxAge = 34;
	      	}
	   		else if (ageMax == 3) {
				maxAge = 44;
	      	}
	   		else if (ageMax == 4) {
				maxAge = 54;
	      	}
	   		else if (ageMax == 5) {
				maxAge = 64;
	      	}
	   		else if (ageMax == 6) {
				maxAge = 74;
	      	}
	   		else if (ageMax == 7) {
				maxAge = 84;
	      	}
	   		else if (ageMax == 8) {
				maxAge = 130;
	      	}





  //***************** Following are separate IF statements with confirm messages ******************************************  


	//Notify client if hispanic is selected (when selectedIndex equals the second or third value in the list) and when year<1990
	// SOURCE:  http://www.quirksmode.org/js/forms.html  and  http://www.codeave.com/javascript/code.asp?u_log=7035
	if ((document.main.hispanic.selectedIndex == 1 || document.main.hispanic.selectedIndex == 2) &&
	    (startYr < 1990 || endYr < 1990) ) {
				checkHispanic = confirm ("NORMS does not include any data for Hispanic Origin prior to 1990.  Please note that the selected time interval will be changed.  Select 'OK' to continue, or 'Cancel' to change query options.");
				if (checkHispanic == false) {
					return false;
				}
			}


	//Notify client if the lower limit (name="minage") is greater than the upper limit (name="maxage")
	if (minAge > maxAge) {
		confirmAge = confirm ("Please note that the selected age interval is not valid, and will be automatically changed.  Select 'OK' to continue, or 'Cancel' to change query options.");
		if (confirmAge == false) {
			return false;
		}
	}



  //***************** Following are if/thens with alert messages that STOP the SUBMIT **********************************  

	//Notify client if state is not selected (when selectedIndex equals the first value in the list) and when us_st='ST'
	// SOURCE:  http://www.quirksmode.org/js/forms.html
	// for older browsers, may need to use "document.main.state2.options[document.main.state2.selectedIndex].value" instead of "document.main.state2.selectedIndex"
	else if (NAus_st == 'ST' && document.main.state2.selectedIndex == 0) {
				alert ("An individual state has not been selected.  Please select which state you want from the pull-down list.");
				return false;
			}
			
			
	//Notify client if "each sex" is selected (when selectedIndex equals the second value in the list) and when view^='F'
	else if (NAview != 'F' && document.main.sex2.selectedIndex == 1) {
				alert ('Please note that "Each Sex (separately)" is not a valid option for displaying a chart or a map.  Valid selection are "Both Sexes (combined)", "Male", or "Female".');
				return false;
			}
			
	//Notify client if "each race" is selected (when selectedIndex equals the second value in the list) and when view^='F'
	else if (NAview != 'F' && document.main.race2.selectedIndex == 1) {
				alert ('Please note that "Each Race (separately)" is not a valid option for displaying a chart or a map.  Valid selection are "All Races (combined)", "White", "Black", or "Other".');
				return false;
			}
			
	//Notify client if "each Hispanic group" is selected (when selectedIndex equals the second value in the list) and when view^='F'
	else if (NAview != 'F' && document.main.hispanic.selectedIndex == 1) {
				alert ('Please note that "Each Group (separately)" is not a valid option for displaying a chart or a map.  Valid selection are "Both Groups (combined)", "Hispanic", or "Non-Hispanic".');
				return false;
			}
			
	//Notify client if Alaska is selected (when selectedIndex equals the third value in the list) and when view='M'
	else if (NAview == 'M' && NAus_st == 'ST' && document.main.state2.selectedIndex == 2) {
				alert ("NORMS cannot generate a county-level map for Alaska.  Please select a different state.");
				return false;
			}
			
	//Notify client if DC is selected (when selectedIndex equals the 10th value in the list) and when view='M'
	else if (NAview == 'M' && NAus_st == 'ST' && document.main.state2.selectedIndex == 9) {
				alert ("NORMS cannot generate a county-level map for District of Columbia.  Please select a different state.");
				return false;
			}
			
	else {
		return true;
	}
}






//id names used for DIV elements in National Database --- Display the results of this query... 
var NAresults=new Array('NAmaps','NAcolorBlind');

function displayNAresults(id){	
	//execute the undisplay function, then display this (id) tag -- see onclick attributes in body of html
	undisplayNAresults();
	displayThis(id);
}

function undisplayNAresults(){
	//undisplay data within all DIVs listed in the NAresults array
	for (var i=0;i<NAresults.length;i++){
		undisplayThis(NAresults[i]);
	}		  
}




//id names used for DIV elements in National Database --- Spatial Geographical Criteria 
var GeogNA=new Array('NAusa','NAgeogSort','NAregion','NAstate','NAstate2');

function displayGeogNA(id){	
	//execute the undisplay function, then display this (id) tag -- see onclick attributes in body of html
	undisplayGeogNA();
	displayThis(id);
}

function undisplayGeogNA(){
	//undisplay data within all DIVs listed in the GeogNA array
	for (var i=0;i<GeogNA.length;i++){
		undisplayThis(GeogNA[i]);
	}		  
}



function OtherGeogNA() {

	//determine which radio button with name=view is checked, and assign that value to a variable named "NAview"
	//source:  http://www.webdevelopersnotes.com/tips/html/finding_the_value_of_a_radio_button.php3
	for (var i=0; i < document.main.view.length; i++)
	   {
	   if (document.main.view[i].checked)
	      {
	      var NAview = document.main.view[i].value;
	      }
	   }

	//determine which radio button with name=us_st is checked, and assign that value to a variable named "NAus_st"
	for (var i=0; i < document.main.us_st.length; i++)
	   {
	   if (document.main.us_st[i].checked)
	      {
	      var NAus_st = document.main.us_st[i].value;
	      }
	   }

	
	//determine which radio button with name=UStables is checked, and assign that value to a variable named "NAus_table"
	for (var i=0; i < document.main.UStables.length; i++)
	   {
	   if (document.main.UStables[i].checked)
	      {
	      var NAus_table = document.main.UStables[i].value;
	      }
	   }

	//determine which radio button with name=STtables is checked, and assign that value to a variable named "NAst_table"
	for (var i=0; i < document.main.STtables.length; i++)
	   {
	   if (document.main.STtables[i].checked)
	      {
	      var NAst_table = document.main.STtables[i].value;
	      }
	   }


	//redisplay or undisplay the following DIVs
	if (NAview == 'F')  {
		undisplayNAresults();
			if (NAus_st == 'US')  {
		   		displayGeogNA('NAusa');
			    	if (NAus_table == 'US')  {
					   	displayThis('yearOut');
			   			undisplayThis('NAgeogSort');
					   	displayThis('NAyearSort');
					}	
			    	else if (NAus_table == 'ST')  {
					   	displayThis('yearOut');
			   			displayThis('NAgeogSort');
			   			undisplayThis('NAyearSort');
					}	
			    	else if (NAus_table == 'CO')  {
					   	undisplayThis('yearOut');
			   			displayThis('NAgeogSort');
			   			undisplayThis('NAyearSort');
					}	
			}
			else if (NAus_st == 'ST')  {
		    	displayGeogNA('NAstate');
		    	displayThisAlso('NAstate2');
			    	if (NAst_table == 'ST')  {
					   	displayThis('yearOut');
			   			undisplayThis('NAgeogSort');
					   	displayThis('NAyearSort');
					}	
			    	else if (NAst_table == 'CO')  {
					   	undisplayThis('yearOut');
			   			displayThis('NAgeogSort');
			   			undisplayThis('NAyearSort');
					}	
			}
			else if (NAus_st == 'RE')  {
		   		displayGeogNA('NAusa');
		   		displayThisAlso('NAregion');
			    	if (NAus_table == 'US')  {
					   	displayThis('yearOut');
			   			undisplayThis('NAgeogSort');
					   	displayThis('NAyearSort');
					}	
			    	else if (NAus_table == 'ST')  {
					   	displayThis('yearOut');
			   			displayThis('NAgeogSort');
			   			undisplayThis('NAyearSort');
					}	
			    	else if (NAus_table == 'CO')  {
					   	undisplayThis('yearOut');
			   			displayThis('NAgeogSort');
			   			undisplayThis('NAyearSort');
					}	
			}
	}
	else if (NAview == 'C')  {
    		displayThis('yearOut');
		undisplayNAresults();
    		undisplayThis('NAyearSort');
   		undisplayGeogNA();
			if (NAus_st == 'ST')  {
		    	displayGeogNA('NAstate2');
			}
			else if (NAus_st == 'RE')  {
		    	displayGeogNA('NAregion');
			}
	}
	else if (NAview == 'M')  {
    		undisplayThis('yearOut');
    		undisplayThis('NAyearSort');
		undisplayGeogNA();
			if (NAus_st == 'US')  {
				displayNAresults('NAmaps');
			}
			else if (NAus_st == 'ST')  {
		    	displayGeogNA('NAstate2');
				undisplayNAresults();
			}
			else if (NAus_st == 'RE')  {
		    	displayGeogNA('NAregion');
				displayNAresults('NAmaps');
			}
		displayThis('NAcolorBlind');
	}
}



function viewRefresh() {   
	//This redisplays or undisplays DIVs in both forms, based on client's currently selected options
	//See onLoad attribute in body tag
  
	//
	// List of National Database refresh options 
	//

	//determine which radio button with name=view is checked, and assign that value to a variable named "NAview"
	//source:  http://www.webdevelopersnotes.com/tips/html/finding_the_value_of_a_radio_button.php3
	for (var i=0; i < document.main.view.length; i++)
	   {
	   if (document.main.view[i].checked)
	      {
	      var NAview = document.main.view[i].value;
	      }
	   }

	//determine which radio button with name=us_st is checked, and assign that value to a variable named "NAus_st"
	for (var i=0; i < document.main.us_st.length; i++)
	   {
	   if (document.main.us_st[i].checked)
	      {
	      var NAus_st = document.main.us_st[i].value;
	      }
	   }

	//determine which radio button with name=UStables is checked, and assign that value to a variable named "NAus_table"
	for (var i=0; i < document.main.UStables.length; i++)
	   {
	   if (document.main.UStables[i].checked)
	      {
	      var NAus_table = document.main.UStables[i].value;
	      }
	   }

	//determine which radio button with name=STtables is checked, and assign that value to a variable named "NAst_table"
	for (var i=0; i < document.main.STtables.length; i++)
	   {
	   if (document.main.STtables[i].checked)
	      {
	      var NAst_table = document.main.STtables[i].value;
	      }
	   }


	//redisplay or undisplay the following DIVs
	if (NAview == 'F')  {
		undisplayNAresults();
			if (NAus_st == 'US')  {
		   		displayGeogNA('NAusa');
			    	if (NAus_table == 'US')  {
					   	displayThis('yearOut');
			   			undisplayThis('NAgeogSort');
					   	displayThis('NAyearSort');
					}	
			    	else if (NAus_table == 'ST')  {
					   	displayThis('yearOut');
			   			displayThis('NAgeogSort');
			   			undisplayThis('NAyearSort');
					}	
			    	else if (NAus_table == 'CO')  {
					   	undisplayThis('yearOut');
			   			displayThis('NAgeogSort');
			   			undisplayThis('NAyearSort');
					}	
			}
			else if (NAus_st == 'ST')  {
		    	displayGeogNA('NAstate');
		    	displayThisAlso('NAstate2');
			    	if (NAst_table == 'ST')  {
					   	displayThis('yearOut');
			   			undisplayThis('NAgeogSort');
					   	displayThis('NAyearSort');
					}	
			    	else if (NAst_table == 'CO')  {
					   	undisplayThis('yearOut');
			   			displayThis('NAgeogSort');
			   			undisplayThis('NAyearSort');
					}	
			}
			else if (NAus_st == 'RE')  {
		   		displayGeogNA('NAusa');
		   		displayThisAlso('NAregion');
			    	if (NAus_table == 'US')  {
					   	displayThis('yearOut');
			   			undisplayThis('NAgeogSort');
					   	displayThis('NAyearSort');
					}	
			    	else if (NAus_table == 'ST')  {
					   	displayThis('yearOut');
			   			displayThis('NAgeogSort');
			   			undisplayThis('NAyearSort');
					}	
			    	else if (NAus_table == 'CO')  {
					   	undisplayThis('yearOut');
			   			displayThis('NAgeogSort');
			   			undisplayThis('NAyearSort');
					}	
			}
	}
	else if (NAview == 'C')  {
    		displayThis('yearOut');
		undisplayNAresults();
    		undisplayThis('NAyearSort');
   		undisplayGeogNA();
			if (NAus_st == 'ST')  {
		    	displayGeogNA('NAstate2');
			}
			else if (NAus_st == 'RE')  {
		    	displayGeogNA('NAregion');
			}
	}
	else if (NAview == 'M')  {
    		undisplayThis('yearOut');
    		undisplayThis('NAyearSort');
		undisplayGeogNA();
			if (NAus_st == 'US')  {
				displayNAresults('NAmaps');
			}
			else if (NAus_st == 'ST')  {
		    	displayGeogNA('NAstate2');
				undisplayNAresults();
			}
			else if (NAus_st == 'RE')  {
		    	displayGeogNA('NAregion');
				displayNAresults('NAmaps');
			}
		displayThis('NAcolorBlind');
	}
}








function resetNA() {   
	//This resets DIVs in the National Database form to their default values after the Reset button is clicked. 
	//The selected options are reset by the reset button.
	//See onClick attribute on the reset button, and onReset attribute in the form tag.

	//reset the DIVs associated with radio button with name=view when view='F'
		undisplayNAresults();
   		displayGeogNA('NAusa');
}