/* 
 * Display or remove state options from form.
 */
 function ShowHideStates()
 {
	// Set local variable names
	var refStatesDef = document.getElementById('statesDef');
	var refStatesOpen = document.getElementById('statesOpen');
	
	// Verify state of DIV and switch
	if (refStatesDef.style.display == 'block')
	{
		refStatesDef.style.display = 'none';
		refStatesOpen.style.display = 'block';
	}
	else
	{
		refStatesOpen.style.display = 'none';
		refStatesDef.style.display = 'block';
	}
 }

/* 
 * Dynamically select all state checkboxes in form.
 */
 function SelectAllStates()
 {
	// Set local reference to state inputs
	var arrStateInputs = document.forms['UpdateEmailPreferences'].elements['states[]'];
	
	// Loop through state inputs array
	for (var i=0; i<arrStateInputs.length; i++)
	{
		// Check if "all states" checkbox is checked - yes
		if (document.forms['UpdateEmailPreferences'].elements['statesAll'].checked == true)
		{
			// Set each state input to checked
			arrStateInputs[i].checked = true;
		}
		// "All states" checkbox is not checked
		else
		{
			// Set each state input to unchecked
			arrStateInputs[i].checked = false;
		}
	}
 }

/* 
 * Confirm deletion of preference on form submission.
 */
 function CheckDeleteStatusOnUpdate()
 {
	// Check if delete radio button is checked - yes
	if (document.forms['UpdateEmailPreferences'].elements['deletePreference'][1].checked == true)
	{
		// Popup JS-confirmation window
		var intConfirmed = confirm('Deleting this preference will disable automatic notifications of the criteria provided. Click "OK" if you wish to continue or "Cancel" to stop this request.');
		
		// Check if user has confirmed delete action - yes
		if (intConfirmed)
		{
			// Continue processing form/actions
			return true;
		}
		// User has not confirmed delete action
		else
		{
			// Stop processing form/actions
			return false;
		}
	}
 }
 
/* 
 * Check or uncheck a specific state in both DIVs
 */
 function CheckUncheckThisState(objRef,strValueRef)
 {
 	// Set local reference to state inputs
	var arrStateInputs = document.forms['UpdateEmailPreferences'].elements['states[]'];
	
	// Loop through state inputs array
	for (var i=0; i<arrStateInputs.length; i++)
	{
		// Check if checkbox was just checked "on" and checkbox in loop is the same - yes
		if (objRef.checked == true && arrStateInputs[i].value == strValueRef)
		{
			// Set other checkbox in other DIV to "on"
			arrStateInputs[i].checked = true;
		}
		// Check if checkbox was just checked "off" and checkbox in loop is the same - yes
		else if (objRef.checked == false && arrStateInputs[i].value == strValueRef)
		{
			// Set other checkbox in other DIV to "off"
			arrStateInputs[i].checked = false;
		}
	}
 }
