/*
*********************************************************************
* Copyright by State of Michigan (Unpublished) - All rights reserved.
*********************************************************************
* Name:  fire_directory_dept.js
*
* Description:  Client script for Selection Criteria.
*
* Special Logic:  None
*
***********************************************************************
* Modification Log:
*
* Programmer      Date      Request No.      Version   Changes
* Daphne Joyce    06/16/04  N/A              1.0       Adapted from Doug Cook's OFIS code.
*
***********************************************************************
*/

// Function: btnAdd_OnClick
// Purpose: Clicked event for the Add button to copy a county from the 
// Available County list to the Selected County list.
function btnAdd_OnClick()
{
	var objList = document.fire_directory.fire_dept;
	var objTarget = document.fire_directory.lstSelectedDepts;	
	var strStop; 
			
	// Loop through list of counties to find the selected item.
	for( var lngCounter = 0; lngCounter < objList.length; lngCounter++ )   
	{
   	  // If the county is selected then add it to the Selected 
	  // County list.
	  if(objList.options[lngCounter].selected)          
	  {
		var strSelectedItem = objList.options[lngCounter].text;
		var strSelectedValue =  objList.options[lngCounter].value;

		// Loop through the Selected County to be sure the county
		// has not already been added.			
		for (var lngCounter2 = 0; lngCounter2 < objTarget.length; lngCounter2++)
		{		    
		  if (strSelectedItem == objTarget.options[lngCounter2].text)
		  {
		    alert("Already Selected");
			strStop = "Stop";
		  }
		} 
		
		if (strStop != "Stop") 
		{
		  // If this is the first county selected, remove the 
		  // No Selected Counties message.
		  if ((objTarget.length == 1) && (objTarget.options[0].value == ""))
		  {
		    objTarget.options[0] = null;
		  }
			
		  objTarget.options[objTarget.length] = new Option( strSelectedItem, strSelectedValue );   //create new items in target select box
		} 
	  }
    }
}

// Function: btnAddAll_OnClick
// Purpose: Clicked event for the Add All button. Adds all insurance companies to 
// the selected insurance company list box. 
function btnAddAll_OnClick()                 
{
	var objList = document.fire_directory.fire_dept;
	var objTarget = document.fire_directory.lstSelectedDepts;	
		
	// Loop backwards through through the selected insurance companies list 
	// clearing every item.   
	for( var lngCounter = objTarget.length - 1; lngCounter >= 0; lngCounter-- )   
    {                                               
	  objTarget.options[lngCounter] = null;        
	} 
		
	// Add all records from the insurance company list to the 
	// selected insurance companies list.
	for( var lngCounter = 0; lngCounter < objList.length; lngCounter++ )   
	{
	  var strSelectedItem = objList.options[lngCounter].text;
	  var strSelectedValue =  objList.options[lngCounter].value;

	  objTarget.options[objTarget.length] = new Option( strSelectedItem, strSelectedValue );   //create new items in target select box
    }                 		
}




// Function: btnRemove_OnClick
// Purpose: Clicked event for the Remove button.  Remove a county
// from the selected county list box.  
function btnRemove_OnClick()
{
	var objTarget = document.fire_directory.lstSelectedDepts;	
	
	// Loop through selected county list to find selected items
	for (var lngCounter = 0; lngCounter < objTarget.length; lngCounter++)   
	{
	  // If an item is selected, remove the item from the selected county
	  // list.
	  if(objTarget.options[lngCounter].selected)          
	  {
        objTarget.options[lngCounter] = null;
	  }
    }    
   
   // If no county records are in the selected counties list,
   // add the No Counties Selected to the list.
   if (objTarget.length == 0)
   {
     objTarget.options[objTarget.length] = new Option( '< No Departments Selected >', '' );
   }
} 


 
// Function: btnRemoveAll_OnClick
// Purpose: Clicked event for the Remove All button. Removes all counties from the Selected 
// County list box. 
function btnRemoveAll_OnClick()                 
{
	var objList = document.fire_directory.lstSelectedDepts;	
		
	// Loop backwards clearing every item from the selected counties list.  
	for (var lngCounter = objList.length - 1; lngCounter >= 0; lngCounter-- )   
	{                                               
	  objList.options[lngCounter] = null;        
	}                   
		
	// Add the No Selected Counties message to the list.
	objList.options[objList.length] = new Option( '< No Depts Selected >', '' );   
}



// Function: btnReset_OnClick()
// Purpose: Reset event for the form.  Reset the default values for the form.
function btnReset_OnClick()
{
    document.fire_directory.hdnSelectedDept.value = '';
    document.fire_directory.fire_dept.selectedIndex = -1;	
			
    // Remove all entries from the selected counties list.
    btnRemoveAll_OnClick();
}   



// Function:  restoreChosen
// Purpose: When the page is re-loaded, set the values of the selected county list based on the data in the hidden fields.
function restoreChosen() 
{
	var objList = document.fire_directory.fire_dept;	
	var objTarget = document.fire_directory.lstSelectedDepts;
	var strSelectedDept;
	var strText;
	var strValue;

	strSelectedDept = document.fire_directory.hdnSelectedDept.value;
	
    // Delete No Selected Counties message.
    if (objTarget.options[0].value == '')
	{
      objTarget.options[0] = null;
	}

    // Loop through the selected counties array to find a match with the list of 
	// counties.  When one is found, copy the information to the selected 
	// county list box.
	for (var lngCounter = 0; lngCounter < arrSelectedDept.length; lngCounter++)
	{
	  // Loop through the county list to obtain the text and the
	  // value.  
	  for (var lngCounter2 = 0; lngCounter2 < objList.length; lngCounter2++)
	  {
        if (arrSelectedDept[lngCounter] == objList[lngCounter2].value) 
		{ 
   	      strText = objList.options[lngCounter2].text;
		  strValue = objList.options[lngCounter2].value;
		  objTarget.options[objTarget.length] = new Option( strText, strValue );
		}
	  }
	}  	   
}

// Function: saveChosen
// Purpose: Save the selected counties to the hdnSelectedCounty field.
function saveChosen() 
{
	var strChosen = '';
	var strFields = '';
	
	var objList = document.fire_directory.lstSelectedDepts;
	
	// Loop through all selected insurance companies to add them to the 
	// hdnSelectedCounties hidden fields.
	for( var lngCounter = 0; lngCounter < objList.length; lngCounter++ )   
    {
	  if (lngCounter == 0) {
	    strFields = objList.options[lngCounter].value;
	  }
	  else
	  {	
	    strFields = strFields + ',' + objList.options[lngCounter].value;
	  }	
	}

    document.fire_directory.hdnSelectedDept.value = strFields; 		
}

// Function: validateForm
// Purpose: Submit event for the form.  Verify the user complete the form
// correctly.
function validateForm()
{
	if (verifySelection()) {
	  return true; 
	}
	else
	{
	  return false;
	}   
	document.fire_directory.fire_dept.value = ''; 
}
  
   

// Function: verifySelection
// Purpose: Verify the user selected at least one county.
function verifySelection()
{
	// Save the selected counties into a hidden field.
	saveChosen();
	
    // If no counties were selected, display an error message.  Also, return false
	// from this function so the form is not submitted.
	if (document.fire_directory.hdnSelectedDept.value == "") {
      alert("No departments have been selected.  Please select at least one department and try again.");
	  return false;
	}    
	return true;	
}

// Function: window_OnLoad
// Purpose: OnLoad event for the window.  If the browser is Internet Explorer and data 
// is saved in the hdnChosen field then restore the selected insurance company list.
function window_OnLoad()
{
	// Do something only if the user has chosen insurance companies.
	if (document.fire_directory.hdnSelectedDept.value != "0")
	{
	  restoreChosen();
	}
}
	



