var ArcMapWindow;	
	var ZipLevel = 1; 		
			
	var DefaultIndicatorIndex;        
	//var FertilityRateIndex = 1;   
	//var LiveBirthsIndex = 0;     
	var IsCountyHealthDistrictSelected = false;         
      
	DefaultIndicatorIndex = 1;	//Mortality
		    
		 // funtions for modifing select box options
	function deleteOption(object,index) 
	{
		object.options[index] = null;
	}

	function addOption(object,text,value) 
	{
		var defaultSelected = true;
		var selected = true;
		var optionName = new Option(text, value, defaultSelected, selected)
		object.options[object.length] = optionName;
	}
	
	function addOption2(object,text,value) 
	{
		var defaultSelected = false;
		var selected = false;
		var optionName = new Option(text, value, defaultSelected, selected)
		object.options[object.length] = optionName;
	}	
	
	function addSelected(fromObject,toObject) 
	{    
		var sel = getSelected(fromObject.options);
		var j = -1;
			
		outerLoop:
		for (var item in sel)
		{												
			for (j = 0, l = toObject.options.length; j < l; j++)
			{									
				if (sel[item].text == toObject.options[j].text)
				{
					continue outerLoop;
				}					
			}	
			
			if (sel[item].value == "grpheading")
			{
					alert (sel[item].text + " is a group name of available Infant Mortality Variables and it cannot be added")
					continue outerLoop;
			}	
			
			addOption(toObject,sel[item].text,sel[item].value);							
		}	
						
			if (j < 0)
			{
			   //alert ("Please select one or more zipcodes/counties/healthdistricts that you want to add from the above List");
			   return -1;
			}
			else
			{
				fromObject.options.selectedIndex = -1;	
			}
			
		return 1;
				
	}

   
	function removeSelected(fromObject) 
	{
		var sel = getSelected(fromObject.options);
		var i = 0;
			
		   for (var item in sel)
		{
			deleteOption(fromObject,sel[item].index - i);
			++i;								
		}			
	}
		
	// Removes all items from selected zipcodes/counties/healthdists
	function removeAll(fromObject) 
	{
		if (fromObject.options.length == 0)
		{
			return;
		}
		
		for (var i = fromObject.options.length - 1; i > -1; i--)
		{
			deleteOption(fromObject,i);										
		}					
	}
		
	// Returns the multiple items selected in Select List Box
	// as an array
		
	function getSelected(opt) 
	{
		var selected = new Array();
		var index = 0;
           
		for (var intLoop = 0; intLoop < opt.length; intLoop++) 
		{
			if ((opt[intLoop].selected)) 
			{
				index = selected.length;
				selected[index] = new Object;
				selected[index].value = opt[intLoop].value;
				selected[index].index = intLoop;                  
				selected[index].text = opt[intLoop].text;
			}
         }
		return selected;
     }
         
    // Returns all items in Select List Box
	//  as an array
		
	function getAllItems(opt) 
	{
        var allItems = new Array();
        var index = 0;
           
        for (var intLoop = 0; intLoop < opt.length; intLoop++) 
        {
			index = allItems.length;
            allItems[index] = new Object;
            allItems[index].value = opt[intLoop].value;
            allItems[index].index = intLoop;                  
            allItems[index].text = opt[intLoop].text;               
        }
        return allItems;
   }
   
   function addAllItems (sel, toObject)
	{
		var j;
									
		outerLoop:
		for (var item in sel)
		{												
			for (j = 0, l = toObject.options.length; j < l; j++)
			{									
				if (sel[item].text == toObject.options[j].text)
				{
						continue outerLoop;
				}				
			}					
				
			addOption(toObject,sel[item].text,sel[item].value);							
		}							
						
	}	
		  	
	//
	//  Fills the select box with available counties  
	//	
	function setCounties()
	{
		if (OutputLevel == 2 && document.forms[0].levelcty.checked == true)
              return;
        
        OutputLevel = 2;
       // EnableStep6Options ();
       // DisableConfidentialityRules ();
           
        window.status = "Please wait while the zipcodes/counties/health districts listbox is filled"        
        removeAll (document.forms[0].CtyZipHDist);
        removeAll (document.forms[0].SelCtyZipHDist);
         
        CountyList = document.forms[0].TxtCountyList.value;
		Counties = CountyList.split (";");
				
		for (var i = 0; i < Counties.length - 1; i = i + 2)
		{
			addOption2(document.forms[0].CtyZipHDist, Counties[i], Counties[i+1]);
		}
		document.forms[0].CtyZipHDist[0].selected = true;	
		
		setDefaultLocation ();	
		
		window.status = "Done"
				
	}
	   			
	
	
	// Disable rates, conf intervals
	function EnableConfidentialityRules ()
	{
		if (document.forms[0].georates1.checked == true || document.forms[0].conf1.checked == true ||
				document.forms[0].georates.checked == true)
		{
			alert ("Percents, Rates and 95% Confidential Intervals in Step Nine are not available when Zip Codes is selected in Step Five");		
		}	
		//document.forms[0].georates.checked = false;		
		//document.forms[0].georates.disabled = true;
		//document.forms[0].georates1.checked = false;		
		//document.forms[0].georates1.disabled = true;
		//document.forms[0].conf1.checked = false;
		//document.forms[0].conf1.disabled = true;
	}
	
	function DisableConfidentialityRules ()
	{
		//document.forms[0].georates.disabled = false;
		//document.forms[0].georates1.disabled = false;		
		//DisableConfidenceIntervals ();		
		
	}		
	
	// Adds selected locations to the Selected List Box (ca)				
	function addCtyZipHDist ()
    {
		var i;
		
		i = addSelected(document.forms[0].CtyZipHDist, document.forms[0].SelCtyZipHDist);		
		if (i == -1)
			alert ("Please select one or more Counties/Health Districts that you want to add from the above List");
		
	}
        
    // Removes selected locations from the Selected List Box (ca)      
	function removeCtyZipHDist ()
	{
		removeSelected(document.forms[0].SelCtyZipHDist);		
	}
	
		
	
	
	// Adds selected locations to the Selected List Box (ca)				
	function addIMIndicators ()
    {
		var SelectedIndicator;
		
		SelectedIndicator = document.forms[0].IMIndicators.options[document.forms[0].IMIndicators.selectedIndex].text;
		
		if (document.forms[0].IMIndicators.options[document.forms[0].IMIndicators.selectedIndex].value == "grpheading")
		{
			alert (SelectedIndicator + " is a group name of available Infant Mortality Variables and it cannot be added")
			return;
		}
		
		// remove any existing selections
		if (document.forms[0].SelIMIndicators.options.length > 0)
		{
			alert ("Maps are not available with Multiple Infant Mortality Indicators");
			removeAll(document.forms[0].SelIMIndicators);			
		}	
		addSelected(document.forms[0].IMIndicators, document.forms[0].SelIMIndicators);	
	}
	        
    // Removes selected indicators from the Selected List Box (ca)      
	function removeIMIndicators ()
	{
		removeSelected(document.forms[0].SelIMIndicators);
	}
	
	
	// Checks if the user has selected a valid age
	function CheckForValidBirthWeight()
	{
		if (document.forms[0].bw[document.forms[0].bw.selectedIndex].value == "Empty")
		{
			alert ("Please select a valid Birthweight")
			document.forms[0].bw.selectedIndex = 0;
		}			
	}    
	
	// sets default health indicator
	function setDefaultIMIndicator ()
	{
		if (document.forms[0].SelIMIndicators.options.length != 0)
			removeAll (document.forms[0].SelIMIndicators);
		addOption(document.forms[0].SelIMIndicators, document.forms[0].IMIndicators.options[DefaultIndicatorIndex].text, document.forms[0].IMIndicators.options[DefaultIndicatorIndex].value);
	}
	
	// select or un select all years
	function SelectAllYears ()
	{
		var numYears;
        numYears = document.forms[0].yr.length;
                    
        if (document.forms[0].allyr.checked == true)
        {
			for (i = 0; i < numYears; i++)
			{
				document.forms[0].yr[i].checked = true;				
			}
        }
        else
        {
			for (i = 0; i < numYears; i++)
			{
				document.forms[0].yr[i].checked = false;				
			}	
        }
                  
       return -1;   
	
	}
	
	
	// Set the old selections on the input form
	// retains the selected indicators when the browser's back button is clicked
    // using the delimited selected indexes in hidden text fields   
	function SetOldSelections()
	{
		var YearsList;
		var Years;
		var i, j;
		var RaceAgeMaritalEthnicities;
		var RaceAgeMaritalEthnicityList;
		var SelectedOutcomes;
		var SelectedOutcomesList;
		var OutputPref;
		var BreakIntervals;
		var Color;
		var selOptions;
		var SelectedOptions;
		
		// set the previous years selections
		YearsList = document.forms[0].TxtSelYears.value;
		
		if (YearsList.length > 0)
		{
			Years = YearsList.split (",");
			
			// clear the current selections - page defaults
			for (j = 0; j < document.forms[0].yr.length; j++)
			{
				document.forms[0].yr[j].checked = false;
			}   
					
			for (i = 0; i < Years.length; ++i)
			{
				
				for (j = 0; j < document.forms[0].yr.length; j++)
				{
					if (document.forms[0].yr[j].value == Years[i])
					{   
						document.forms[0].yr[j].checked = true;
					}
					
				}     
				
			}
		}
		
		// Set Previous Race, Age, Birth Weight, Ethnicity, Gestational Age
		RaceAgeBirthweightEthnicityGestAgeList = document.forms[0].TxtSelRcAgBwEtGa.value;
		
		if (RaceAgeBirthweightEthnicityGestAgeList.length > 0)
		{
			RaceAgeBirthweightEthnicityGestAges = RaceAgeBirthweightEthnicityGestAgeList.split (",");
			
			for (i = 0; i < document.forms[0].rc.options.length; ++i)
				if (document.forms[0].rc.options[i].value == RaceAgeBirthweightEthnicityGestAges[0])
					document.forms[0].rc.selectedIndex = i;
			
			for (i = 0; i < document.forms[0].ag.options.length; ++i)
				if (document.forms[0].ag.options[i].value == RaceAgeBirthweightEthnicityGestAges[1])
					document.forms[0].ag.selectedIndex = i;
					
			for (i = 0; i < document.forms[0].bw.options.length; ++i)
				if (document.forms[0].bw.options[i].value == RaceAgeBirthweightEthnicityGestAges[2])
					document.forms[0].bw.selectedIndex = i;
					 		
			for (i = 0; i < document.forms[0].et.options.length; ++i)
				if (document.forms[0].et.options[i].value == RaceAgeBirthweightEthnicityGestAges[3])
					document.forms[0].et.selectedIndex = i;
					
			for (i = 0; i < document.forms[0].ga.options.length; ++i)
				if (document.forms[0].ga.options[i].value == RaceAgeBirthweightEthnicityGestAges[4])
					document.forms[0].ga.selectedIndex = i;				
				
		 }
		 
		 document.forms[0].TxtSelRcAgBwEtGa.value = "";
		 
		 for (i = 0; i < document.forms[0].geo.length; ++i)
				if (document.forms[0].geo[i].value == document.forms[0].TxtSelLocation.value)
				{
					document.forms[0].geo[i].checked = true;						
				}
		
			
		
		// set the previous outcome selections
		selOptions	= document.forms[0].TxtSelIndicators.value;
		
		//alert (selOptions.length)
		// fill the selected indicators
		if (selOptions.length > 0)
		{
			window.status = "Please wait while the Infant Mortality Indicators listbox is filled"        
	      
			Options = selOptions.split (",");
					
			for (var i = 0; i < Options.length; ++i)
			{
				j = getIndicatorIndexByValue (Options[i]);				
				addOption(document.forms[0].SelIMIndicators, document.forms[0].IMIndicators.options[j].text, Options[i]);
										
			}		
			
			document.forms[0].TxtSelIndicators.value = "";
					
			window.status = "Done"
		}	
		
		// if the page is loaded for first time then add default Health and Life Syle indicator			
		if (document.forms[0].SelIMIndicators.options.length == 0 && document.forms[0].IMIndicators.options.length > 0)
		{
			setDefaultIMIndicator ();
		}				
		
		// select previous output preference		
		OutputPref = document.forms[0].TxtSelOutputPref.value;
		
		if (OutputPref.length > 0)
		{
			for (i = 0; i < document.forms[0].geo.length; ++i)
				if (document.forms[0].geo[i].value == OutputPref)
					document.forms[0].geo[i].checked = true;				
		}				
	
		// set previous break intervals
		BreakIntervals = document.forms[0].TxtSelCountInt.value;
		if (BreakIntervals.length > 0)
		{
			for (i = 0; i < document.forms[0].br.length; ++i)
				if (document.forms[0].br[i].value == BreakIntervals)
				{
					document.forms[0].br[i].checked = true;					
				}				
		
		}
		
		// set previous color selection		
		Color = document.forms[0].TxtSelColor.value;
		if (Color.length > 0)
		{
			for (i = 0; i < document.forms[0].color.length; ++i)
				if (document.forms[0].color[i].value == Color)
					document.forms[0].color[i].checked = true;				
		}
	}
	
	function ShowMapInNewWindow ()
	{
		var Url;
		var Width;
		var Height;
			
		Url = document.forms[0].TxtRedirectUrl.value;
		
		document.forms[0].TxtRedirectUrl.value = "";
		
		Width = window.screen.width;		
		Height = window.screen.height;				
		
		if (Url.length > 0)
		{
			//alert (Url)
			if (!ArcMapWindow || ArcMapWindow.closed)
			{
				ArcMapWindow = window.open(Url, "ArcMapWindow", "location=no,menubar=yes,toolbar=yes,resizable=yes,scrollbars=yes,left=0,top=0," + "width=" + Width + ",height=" + Height);											
			} 
			else 
			{
				ArcMapWindow.navigate (Url);								
			} 
		}
		else
		{
			//alert ("An error occured while processing your request.")
		}
			
	}	
	
	
	function setDefaultIMIndicator ()
	{
		if (document.forms[0].SelIMIndicators.options.length != 0)
			removeAll (document.forms[0].SelIMIndicators);
		addOption(document.forms[0].SelIMIndicators, document.forms[0].IMIndicators.options[DefaultIndicatorIndex].text, document.forms[0].IMIndicators.options[DefaultIndicatorIndex].value);
	}
	
	function validateIMIndicators()
	{
		var allItems;		
		
		if (document.forms[0].SelIMIndicators.options.length == 0)
			return -1;
				
		// get all items in the selected Health and Life Style Indicators
		allItems = getAllItems (document.forms[0].SelIMIndicators)
		// remove all items
		removeAll (document.forms[0].SelIMIndicators);	
					
		// highlight all items 
		addAllItems (allItems, document.forms[0].SelIMIndicators);
					
		return 1;	
	}
		
		
	function getIndicatorIndexByValue (val)
	{
			
		var opt = document.forms[0].IMIndicators.options;			
			
		for (var i = 0; i < opt.length; ++i)	
		{
			  if (opt[i].value == val)
				  return i;				
		}
			
	}	
		
	// validates the year of interest selected by user
    // if user does not select any year returns -1
    // else returns 1
                
	function validateYear()
	{
		var numYears;
		numYears = document.forms[0].yr.length;
                    
		for (i = 0; i < numYears; i++)
		{
			if (document.forms[0].yr[i].checked == true)
			{   
				return 1;
			}
		}                    
		return -1;
                        
     }    
     
     // validates outputpreference
	function validateOutputPreference()
	{
		var i;
		var j;
		
		//j = IsLiveBirthsAlreadySelected ();
						
		for (i = 0; i < document.forms[0].geo.length; ++i)
		{
			if (document.forms[0].geo[i].checked == true)
			{
				return 1;
			}		
			
		}	
		
		return -1;
	
	}
     
   // validates all the inputs	
	
	function validate()
	{                       
		var i;              
		   
		if (validateYear () == -1)
		{
			alert ("Please select one or more years of interest in Step One");
			return;
		}    
		
		i = validateIMIndicators ();
		
		if (i == -1)
		{
			alert ("Please select atleast one Infant Mortality Variable in Step Four")
			return;
		}	
					
		
		document.forms[0].submit ();
	}   
