	

	var MaximumNumCharacteristics = 15;
	var DefaultCharacteristicIndex = 20;
	var doSubmit = true;	
		
	// 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 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 indicators and it cannot be added")
					continue outerLoop;
				}				
				addOption(toObject,sel[item].text,sel[item].value);							
			}	
						
			if (j < 0)
			{
			   alert ("Please select one or more indicators that you want to add from the above List");
			}
			else
			{
				fromObject.options.selectedIndex = -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 indicators
	function removeAll(fromObject) 
	{
		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);							
		}							
						
         }
         
         function saveSelectedRiskFactors(sel, toObject)
         {
           // clear any existing variables
            document.forms[0].selchar.value = "";     
                      
           	for (var item in sel)
		{	
			document.forms[0].selchar.value = document.forms[0].selchar.value +  sel[item].value + ";";				
		}											
				
         }
	
		// validates the form input
		
	function  validate() 
	{
		
		if (doSubmit == false)
		   return; 
		    
		var i;
			
		if (validateRowColVariables() == -1) 
		{
			alert ("Please select different row and column variables in Steps One and Two")
			return;
		}
			
			
			
		if (validateYear () == -1)
		{
			alert ("Please select one or more years of interest in Step Three");
			return;
		}			
			
		i = validateRiskFactors ();
			
		if (i == -1)
		{
			alert ("Please select atleast one indicator in Step Five")
			return;
		}
		else if (i == -2)
		{
			alert ("Please select indicator as row variable in Step One to select multiple indicators in Step Five")
				return;
		}
		else if (i == -3)
		{
				alert ("Maximum number of indicators that you can select in Step Five is " + MaximumNumCharacteristics +  " only")
				return;
		}		
				
			
		if (validateOutputPreference () == -1)
		{
			alert ("Please select one or more output preferences in Step Six")
			return;
		}
	
		i = validateOutputPresentation ();
			
		if (i == -1)
		{
			alert ("Please select one or more output presentation formats in Step Seven")
			return;
		}
		else if (i == -2)
		{
			alert ("Please select year as column variable in Step Two for Trendline");
			return;
		}	
		else if (i == -3)
		{
			alert ("Please select more than one year in Step Three for Trendline");
			return;
		}		
		else if (i == -4)
		{
			alert ("Please select consecutive years in Step Three for Trendline");
			return;
		}	
							
							
		document.forms[0].submit ();
	}
		
	// validates row, column variables
	// checks if the row and column variables selected are same
	// returns -1 user if row and column are same 
	// returns 1 on success
		
	function validateRowColVariables() 
	{
		var rowVal;
		var colVal;
		var numRows
		var numCols;	
			
			
		numRows = document.forms[0].row.length;			
		numCols = document.forms[0].col.length;		
			
			
		for (i = 0; i < numRows; i++)
		{
			if (document.forms[0].row[i].checked == true)
			{	
				rowVal = i;
			}
			}
			
			for (i = 0; i < numCols; i++)
			{
				if (document.forms[0].col[i].checked == true)
				{	
					colVal = i;
				}
			}
			
			if (rowVal == colVal)
			{
				return -1;			
			}
			
			return 1;
			
	}
		
	// 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 RiskFactors
	// if user selects one or more indicators then returns 1
	// else returns -1	
		
	function validateRiskFactors ()
	{
		var allItems;
				
		if (document.forms[0].cha.options.length == 0)
			return -1;
				
		// get all items in the selected indicators
		allItems = getAllItems (document.forms[0].cha.options)
		// remove all items
		removeAll (document.forms[0].cha);	
					
	// highlight all items 
		addAllItems (allItems, document.forms[0].cha);				
								
	// check if rf is not row variable and user selected multiple rf's
		if (document.forms[0].row[8].checked == false && 
					document.forms[0].cha.options.length > 1)
		{
			return -2;	
		}
											
	// check if rf is row variable and user selected more than MaximumNumindicators
		if (document.forms[0].row[8].checked == true && document.forms[0].cha.options.length > MaximumNumCharacteristics)
			{
				return -3;	
			}	
				
			// now add selected indicators to hidden text field	
		saveSelectedRiskFactors (allItems, document.forms[0].selchar);					
						
		return 1;
	}
		
	function validateOutputPreference()
	{
		var numOptions;
		numOptions = document.forms[0].geo.length;
			
		for (i = 0; i < numOptions; i++)
		{
			if (document.forms[0].geo[i].checked == true)
			{	
				return 1;
			}
		}
			
		return -1;
	}		
		
		function validateOutputPresentation ()
		{
			var numOptions;
			var j;
			var optionSelected;
			var notified;
				
			notified = 0;
			optionSelected = 0;
			
			numOptions = document.forms[0].tab.length;
			
			for (i = 0; i < numOptions; i++)
			{
				if (document.forms[0].tab[i].checked == true)
				{						
					optionSelected = 1;
					if (i == 1)
					{
						if (CheckForYearAsColVariable() == -1)
						{
							return -2;						
						}
						 
						j = CheckForConsecutiveYearSelection ();
						if (j == -2)
						{
							
							return -3;
						}
						 
						if (j == -1)
						{
							return -4;
						}													
						
					}
					
					// if only TrendLine or BarChart are selected with out selecting Percent
					// prompt user that TrendLine or BarChart
					
					if (i == 1 || i == 2)
					{
						if (document.forms[0].geo[1].checked == false && notified == 0)
						{
							alert ("Trendline and Bar chart display percent values");
							notified = 1;
						}							
					}					
				}			
				
			}
			
			// one of the options has been selected
			if (optionSelected == 1)
			    return 1;		
			
			return -1;
		}
		
		function CheckForYearAsColVariable ()
		{
			if (document.forms[0].col[0].checked == true)
			{	
				return 1;
			}
			
			return -1;
						
		}
		
		function CheckForConsecutiveYearSelection ()
		{
			var numYears;
			var YearsArray;
			var j;
						
			numYears = document.forms[0].yr.length;
			YearsArray = new Array();
			j = 0;
						
			// copy all years selected to YearsArray
			for (i = 0; i < numYears; ++i)
			{
				if (document.forms[0].yr[i].checked == true)
				{	
					YearsArray[j] = i;					
					++j;
				}
			}
			
			if (YearsArray.length < 2)
				return -2;				
						
			for (i = 0; i < YearsArray.length - 1; ++i)
			{
				if (YearsArray[i + 1] - YearsArray[i] == 1)
				{
				}						
				else
					return -1;
			}			
			
			return 1;	
		}
		
		// Adds RiskFactors to the Selected List Box (ca)
				
		function addRiskFactors ()
		{
			addSelected(document.forms[0].AvailChars,document.forms[0].cha)
		}
		
		// Removes RiskFactors from the Selected List Box (ca)		
		function removeRiskFactors ()
		{
			removeSelected(document.forms[0].cha);
		}
		
		// refreshes all options on form
		function refreshAll ()
		{
			var i;
			
			document.forms[0].row[0].checked = true;		
			document.forms[0].col[1].checked = true;
			for (i = 0; i < document.forms[0].yr.length; ++i)
			{
				document.forms[0].yr[i].checked = false;
			}			
			document.forms[0].yr[i - 2].checked = true;	
			
			document.forms[0].rc.selectedIndex = 0;
			document.forms[0].et.selectedIndex = 0;
			document.forms[0].ag.selectedIndex = 0;
			document.forms[0].ma.selectedIndex = 0;
							
			document.forms[0].AvailChars.selectedIndex = DefaultCharacteristicIndex;
			//removeAll 
			removeAll (document.forms[0].cha);
			// add default indicator
			addDefaultRiskFactor ();
			
			for (i = 1; i < document.forms[0].geo.length; ++i)
			{
				document.forms[0].geo[i].checked = false;
			}			
			document.forms[0].geo[0].checked = true;	
			
			//document.forms[0].tab.selectedIndex = 0;
			for (i = 1; i < document.forms[0].tab.length ; ++i)
			{
				document.forms[0].tab[i].checked = false;
			}			
			document.forms[0].tab[0].checked = true;	
			
		}
		
		function addDefaultRiskFactor ()
		{
			if (document.forms[0].AvailChars.options.length > 0)
				document.forms[0].AvailChars.selectedIndex = DefaultCharacteristicIndex;
			
			// if back button was clicked then add previously selected indicators
			// which are stored in hidden element
			if (document.forms[0].selchar.value != "" || document.forms[0].selchar.value.length > 0)
			{
				var SelRiskFactors = new Array ();
				var SelItems;
				var j;
							
				SelItems = document.forms[0].selchar.value;
				SelRiskFactors = SelItems.split (";");
				
				for ( var i = 0; i < SelRiskFactors.length - 1; ++i)
				{
					j = getCharacteristicByValue (SelRiskFactors[i]);
					addOption(document.forms[0].cha, document.forms[0].AvailChars.options[j].text, SelRiskFactors[i]);
				}
				
				document.forms[0].selchar.value = "";
				return;
			}
					
						
			// if the page is loaded for first time then add default indicator			
			if (document.forms[0].cha.options.length == 0 && document.forms[0].AvailChars.options.length > 0)
			{
				addOption(document.forms[0].cha,document.forms[0].AvailChars.options[DefaultCharacteristicIndex].text, document.forms[0].AvailChars.options[DefaultCharacteristicIndex].value);		
			}		
			
		}
		
		function getCharacteristicByValue (val)
		{
			
			var opt = document.forms[0].AvailChars.options;			
			
			for (var i = 0; i < opt.length; ++i)	
			{
			   if (opt[i].value == val)
				   return i;				
			}
			
		}		
		
		function TableUnSelectAttempt()
		{
			alert ("Table is the default output and cannot be unchecked");
		}		
		
			// 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;   
	
	}
	
	function SearchIndicators(event) 
	{
        var IndicatorSearch = document.getElementById('IndicatorSearch');    
        var SelList = document.getElementById('cha');
        var AvList=null;
        AvList = document.getElementById('AvailChars');
        var j = AvList.length;
        var skip=false;

        if ((event.which == 13) || (event.keyCode == 13)) 
        {
            skip=true;
        }

        for (var i=0; i < j; i++) 
        {                
            if (AvList.options[i].text.toLowerCase().indexOf(IndicatorSearch.value.toLowerCase()) > 0) 
            {
                if (skip==true) 
                {
                    if (AvList.options[i].selected) 
                    {
                        skip = false;
                        AvList.options[i].selected=false;
                    }
                } 
                else 
                {
                    AvList.options[i].selected=true;            
                    break;        
                } 
            } 
            else
                AvList.options[i].selected=false;
        }

       doSubmit = false; 
}

function AllowSubmit()
{
    doSubmit = true;
}



