// ---------------------------------------------------------------------------
//  Filename: pulse.js
//   Version: 3.0
//      Date: 12/3/2007
//    Author: B. Zink
//   Purpose: Navy specific javascript
// ---------------------------------------------------------------------------
// Copyright© 2004-2010 BRZ, Inc. - All Rights Reserved
// ---------------------------------------------------------------------------
// Note: NO portion of this code may be copied, manipulated, viewed, or used
//       in ANY way without express written permission of the author and
//       Brz, Inc.
// ---------------------------------------------------------------------------
// Revisions:
//
// 1. 12/3/2007 - bz
//    3.0.0 - Initial creation
// ---------------------------------------------------------------------------

var internalChange = 0;

// ---------------------------------------------------------------------------
// HandleSubjectChange
//
//      Purpose: The purpose of this function is to change the selected radio
//               button to the appropriate item when a selection item is 
//               changed AND to make all other <select> items go back to the
//               first item in their select list.
// ---------------------------------------------------------------------------
function HandleSubjectChange( theElement, radioName )
{
   // ------------------------------------------------------------------------
   // allow for default arguments for backward compatibility
   // ------------------------------------------------------------------------
   switch( arguments.length) {
      case 1:
         radioName = 'CATEGORYID';
         break;
   }
   
   // ------------------------------------------------------------------------
   // shortcut to the radio elements
   // ------------------------------------------------------------------------
   radioElement   =  GetObjectFromName(radioName);
   
   // ------------------------------------------------------------------------
   // get the name of the element
   // ------------------------------------------------------------------------
   var elementName = theElement.name;
   
   // ------------------------------------------------------------------------
   // clear all of the select items, except for the one changing
   // ------------------------------------------------------------------------
   ClearSubjectSelectsExcept( elementName );
   
   // ------------------------------------------------------------------------
   // indicate that this is a click initiated internally and NOT by a click
   // ------------------------------------------------------------------------
   internalChange = 1;
   
   // ------------------------------------------------------------------------
   // go through all poe radio buttons (hard coded here, e.g. categoryId) and find
   // the one associated with this select item
   // ------------------------------------------------------------------------
   for (i=0;i<radioElement.length;i++) {
      // ---------------------------------------------------------------------
      // get the value of this radio button
      // ---------------------------------------------------------------------
      var val = radioElement[i].value;
      
      // ---------------------------------------------------------------------
      // if the value is exact same (char match) as select object passed
      // ---------------------------------------------------------------------
      if ( val.substr(0,elementName.length) == elementName ) {
         // ------------------------------------------------------------------
         // check this radio button
         // ------------------------------------------------------------------
         radioElement[i].click();
      }
   }
   
   // ------------------------------------------------------------------------
   // reset internal change indicator
   // ------------------------------------------------------------------------
   internalChange = 0;   
}

// ---------------------------------------------------------------------------
// HideSubjectSelectsExcept
//
//      Purpose: clear all select items, e.g. reset them to no selection,
//               except for the one passed as a parameter
// ---------------------------------------------------------------------------
function HideSubjectSelectsExcept ( elementName, useSub )
{   
   // ------------------------------------------------------------------------
   // allow for default arguments for backward compatibility
   // ------------------------------------------------------------------------
   switch ( arguments.length ) {
      case 0:
         elementName = 'zippo';
      case 1:
         useSub = 1;
         break;
   }
   
   // ------------------------------------------------------------------------
   // shortcut to form
   // ------------------------------------------------------------------------
   form = document.forms[0];
   
   // ------------------------------------------------------------------------
   // go through each element in form and look for select item
   // ------------------------------------------------------------------------
   for (var i=0;i<form.elements.length;i++) {
      var type = form.elements[i].type;
      var name = form.elements[i].name;
      // ---------------------------------------------------------------------
      // fieldsets and legends don't have name properties
      // ---------------------------------------------------------------------
      if ( name == undefined || name == 'undefined' ) {
         continue;
      }
      
      // ---------------------------------------------------------------------
      // if not a select type, then bypass it
      // ---------------------------------------------------------------------
      if (type.substr(0,6) != 'select') {
         continue;
      }
      
      // ---------------------------------------------------------------------
      // if this is a select, and NOT the one passed, then hide it
      // ---------------------------------------------------------------------
      if ( name != elementName ) {
         if ( useSub && name.substr(0,4) != 'sub_') {
            continue;
         }
         form.elements[i].style.display='none';
      }
      else {
         form.elements[i].style.display='inline';
         var theElement = form.elements[i];
	      // ------------------------------------------------------------------
	      // if there is only one selection here, then automatically select it
	      // ------------------------------------------------------------------
         if (theElement.length == 2) {
            theElement.selectedIndex = 1;
         }
      }
   }
}

// ---------------------------------------------------------------------------
// HandleCategoryChange
//
//      Purpose: clear all select statements when a radio button is clicked
// ---------------------------------------------------------------------------
function HandleCategoryChange ( elementObj )
{
   // ------------------------------------------------------------------------
   // clear ALL of the select items, since a new radio button was clicked
   // ------------------------------------------------------------------------
   var value = elementObj.value;

   var endIndex = value.indexOf(DATA_SEPARATOR);
   var name = value.substr(0,endIndex);
      
   if ( !internalChange )
      HideSubjectSelectsExcept(name);
}

// ---------------------------------------------------------------------------
// ValidatePulseForm
//
//      Purpose: Insure fields are entered properly
// ---------------------------------------------------------------------------
function ValidatePulseForm (form, useConfirm)
{
   var checkedButton = "" ;
       
   // ------------------------------------------------------------------------
   // go through all radio buttons (named 'categoryId') and insure one is checked
   // ------------------------------------------------------------------------
   for (var i=0; i < form.CATEGORYID.length;i++) {        
      if (form.CATEGORYID[i].checked==true) {           
         checkedButton = form.CATEGORYID[i].value;
      }
   }
   
   // ------------------------------------------------------------------------
   // no radio button is checked, so inform user and return
   // ------------------------------------------------------------------------
   if (checkedButton == "") {
      ErrorMsg ('You must select a specific category for this report!\n' +
                'Click on a radio button next to your desired\n' +
                'Category or simply select from any dropdown list');       
      return false;    
   }
   else {
      // ---------------------------------------------------------------------
      // get the name of the radio button 
      // ---------------------------------------------------------------------
      var endIndex = checkedButton.indexOf(DATA_SEPARATOR);
      var name = checkedButton.substr(0,endIndex);
      
      // ---------------------------------------------------------------------
      // build the name of the dropdown box associated with this radio button
      // ---------------------------------------------------------------------
      var element = 'form.' + name + '.selectedIndex';
      var selectedIndex = eval(element);
      
      // ---------------------------------------------------------------------
      // if no item selected for the selected poe, inform user and return
      // ---------------------------------------------------------------------
      if ( selectedIndex == 0 ) {
         // ------------------------------------------------------------------
         // name should start with 'sub_', thus start four(4) past beginning
         // ------------------------------------------------------------------
         var tempName = name.substring(4,name.length);
         var friendly = BuildFriendlyName(tempName);
         ErrorMsg('You must select a specific item from ' +
                  'the dropdown list of your choice\n\n' +
                  'e.g. click on drop down of \'' + friendly + '\'');
         return false;
      }               
   }
   
   // ------------------------------------------------------------------------
   // do 'standard' form validation of fields - NO confirm here
   // ------------------------------------------------------------------------
   var retValue    = ValidateForm(form,useConfirm);
   
   // ------------------------------------------------------------------------
   // if error, then return false
   // ------------------------------------------------------------------------
   if ( !retValue ) {
      return false;
   }
   
   // ------------------------------------------------------------------------
   // go through all radio buttons (named 'reportType') and insure one is checked
   // ------------------------------------------------------------------------
   var checkedType = "";
   
   for (var i=0; i < form.REPORTTYPE.length;i++) {        
      if (form.REPORTTYPE[i].checked==true) {           
         checkedType = form.REPORTTYPE[i].value;
      }
   }
   
   // ------------------------------------------------------------------------
   // if nothing is checked, notify user
   // ------------------------------------------------------------------------
   if (checkedType == "") {
      ErrorMsg ('You must select a specific type of report!\n' +
                'Click on a radio button next to the appropriate\n' +
                'report type that you are submitting.');       
      return false;    
   }
   
   if (useConfirm) {
	   // ------------------------------------------------------------------------
	   // Build confirmation message
	   // ------------------------------------------------------------------------
	   var confirmMsg = "Are You Sure All Information Is Exactly As You Want It ?";
	   
	   // ------------------------------------------------------------------------
	   // Display confirm message to user - asking to continue
	   // ------------------------------------------------------------------------
	   if ( ConfirmMsg(confirmMsg) == false )
	      return false;
	}
		      
   // ------------------------------------------------------------------------
   // all is well in mudville
   // ------------------------------------------------------------------------
   return true;
}

