//------------------------------------------------------------------------
// ValidateExtract.js
//
// Functions to make sure that bit per pixel and channel selection
// are correct, with respect to each other.  If bits/pixel=10, then
// all channels must be selected.
//
// Called from user_preferences and sub_delivery stylesheets.  If the
// names or positions of the form fields are changed, these functions
// probably will no longer work.
//
// $Id: ValidateExtract.js,v 1.5 2004/10/14 22:24:08 kmoore Exp $
// $Log: ValidateExtract.js,v $
// Revision 1.5  2004/10/14 22:24:08  kmoore
// Enter PR: 1486
// Different datatype channels are handled in a more generic fashion.
// Javascript for updating hidden channels_DATATYPE variable.
//
// Revision 1.4  2003/11/19 17:16:09  kmoore
// Enter PR: 307
// Added datatype name as a parameter to javascription functions
// SetAllChannels and ChannelCheck so that elements in the form
// may be found by name instead of position.
//
// Revision 1.3  2003/11/18 18:39:04  kmoore
// Enter PR: 307
// Must use "<" and ">" instead of "&lt;" and "&gt;" since the javascript is
// no longer inside a stylesheet.
//
// Revision 1.2  2003/11/18 18:26:18  kmoore
// Enter PR: 307
// Add GVAR extraction javascript function so that this javascript file can
// be called from both user_preferences.xsl and sub_delivery.xsl.
//
// Revision 1.1  2003/07/09 17:47:36  kmoore
// Enter PR: 307
// Javascript to validate AVHRR extraction fields (bits and channels).
//
//------------------------------------------------------------------------

//------------------------------------------------------------------------
// SetAllChannels
//     channels - the Select widget that was changed
//     dt       - datatype family
// This function is called when the "10 bits" radio button is pressed.
// This function will check all the channels associated with the datatype
// for which 10 bits was selected.
//------------------------------------------------------------------------
function SetAllChannels(channels,dt) {
   var name = "chan_" + dt;
   var form = channels.form;
   var n = form.elements.length
   var i,j;

   // Find Select named "chan_<dt>" and select all options.
   for (i=0; i < n; i++) {
      if (form.elements[i].name == name) {
         for (j=0; j<form.elements[i].length; j++) {
            form.elements[i].options[j].selected = 'Y';
         }
         UpdateChannelString(form.elements[i],dt);
         break;
      }
   }
}

//--------------------------------------------------------------------
// ChannelCheck
//     channel - the select widget that was clicked on
//     dt      - datatype that the widget represents
// If datatype family=AVHRR and bits selection is 10, don't
// allow deselection of channels.
// For all datatypes, set hidden variable "channels_"dt to
// reflect the selection object settings.
//--------------------------------------------------------------------
function ChannelCheck(channels, dt) {
   var name = "bits_" + dt;
   var n = channels.form.elements.length;
   var i;

   //If datatype is AVHRR:
   //Find which element position is the 10-bits radio button,
   //then if 10-bits, don't allow unselecting the channels.

   if (dt.length > 4 && dt.substring(0,5) == "AVHRR") {
   for (i=0; i < n; i++) {
      if (name == channels.form.elements[i].name) {
         //The 10 bits selection control that goes with the channel selection
         //will be the next consecutive control.
         if (channels.form.elements[i+1].checked) {
           SetAllChannels(channels,dt);
         }
         else {
           UpdateChannelString(channels,dt);
         }
         break;
      }
   }
   }
   else
           UpdateChannelString(channels,dt);
}


//-----------------------------------------------------------------------------
// UpdateChannelString
//    channels - Select object which represents the channels
//    dt       - datatype family
//
// Look for text object named "channels_"dt (example channels_AVHRR_GAC)
// and set the bits the same as what is represented by the channels Select object
//-----------------------------------------------------------------------------
function UpdateChannelString(channels,dt) {
   var i;
   var name = "channels_" + dt;
   var form = channels.form;

   //alert("UpdateChannelString");
   for (i=0; i<form.elements.length; i++) {
      if (name == form.elements[i].name) {
         var newValue="";
         var oldValue = form.elements[i].value;
         //alert("UpdateChannelString: " + oldValue);
         for (j=0; j<channels.length; j++) {
            if (channels.options[j].selected)
               newValue=newValue + "1";
            else
               newValue=newValue + "0";
         }
         form.elements[i].value = newValue + oldValue.substring(j,oldValue.length);
         //alert("new value: " + newValue);
      }
   }
}


//--------------------------------------------------------------------
// When the user selects from dropdown list, this function automatically 
// combines VIS and IR resolution value.
//--------------------------------------------------------------------
function getResolution(resolution, num)
{
   
   var n = resolution.form.elements.length;
   var hidden_num = -1;
   var resolution_num = -1;
   var i;

   
   for (i=0; i < n; i++) {
      if (resolution.name == resolution.form.elements[i].name) {
         
         resolution_num = i;

         if (num==1)  hidden_num = i+2;
         if (num==2)  hidden_num = i+1;
         break;
      }
   }

  if (hidden_num > 0 ) {

   var s1= resolution.form.visresolution.options[resolution.form.visresolution.selectedIndex].value;
   var s2= resolution.form.irresolution.options[resolution.form.irresolution.selectedIndex].value;

   //alert (s1);
   //alert (s2);

  resolution.form.elements[hidden_num].value= s1+','+s2;
  //alert (resolution.form.elements[hidden_num].value); 
  }
}
