/* ******************************************************************************************
MODULE: ynCheckRequired()
AUTHOR/DATE: L Chalnick 3/16/99
FUNCTION:
   Scrolls through a list of fields in your form to determine whether or not any required fields
   were left blank (or unselected, checked, etc.). It can optionally be used with a form 
   containing rows of the same fields in which the field names were dynamically generated with a
   counter.
PARAMS:
   lstRequired [required] -- Comma-delimited list of required field objects. If you're specifying 
      a number of rows, then this should contain the list of field names, minus the row numbers. 
      In this case, the actual field names will be generated dynamically. If you're NOT 
      specifying a number of rows, then you must include the exact field names in this list.
   lstLabels [optional] -- Comma-delimited list of labels for the fields to be used in error
      messages. Note that the size of the labels list must equal the size of the ayRequiredFields 
      array.
   nRows [optional] -- Number of rows to process. ONLY uses this if you're processing a multi-line
      form. This ONLY for use when processing a multi-line form in which you're dynamically naming 
      fields, like ChargeCode_1, ChargeCode_2, etc. It indicates the number of rows (e.g., 
      MyQuery.RecordCount).
   nBegin [optional] -- This is required if you're using the nRows param. If you're not specifying
    the number of rows, you MUST leave this param off too. It identifies the row number of first 
    row (used to dynamically create field object names).
****************************************************************************************** */
function ynCheckRequired( lstRequired, lstLabels, nRows, nBegin ) {
   var stErrMsg = "";
   var ayRequired = lstRequired.split(",");
   if (lstLabels != '') {
      var ayLabels = lstLabels.split(",");
      if (ayLabels.length != ayRequired.length) {
         alert("ynValidateForm: Developer error--ayLabels must be same length as ayRequired");
         return false;
      } else {
         var ynUseLabels = true;
      }
   } else {
      var ynUseLabels = false;
   }
   if (arguments.length>=4) {
      for (var ii = nBegin; ii <= nRows; ii++) {
         for (var jj=0; jj<ayRequired.length; jj++) {
            if ( ynIsNullValue( eval(ayRequired[jj] + ii ) ) ) {
               stErrMsg = stErrMsg + "\n * " + ((ynUseLabels)?ayLabels[jj]:ayRequired[jj]) + " is required in row " + ii;
            }
         }
      }
   } else {
      for (var jj=0; jj<ayRequired.length; jj++) {
         if ( ynIsNullValue( eval(ayRequired[jj] ) ) ) {
            stErrMsg = stErrMsg + "\n * " + ((ynUseLabels)?ayLabels[jj]:ayRequired[jj]) + " is required";
         }
      }
   }
   if (stErrMsg!="") {
      alert( "Invalid entries: \n" + stErrMsg);
      return false;
   }
   return true;      
}

/* ******************************************************************************************
MODULE: ynIsNullValue()
AUTHOR/DATE: L Chalnick 3/16/99
FUNCTION:
   Determines whether an object has a null value. used by ynCheckRequired(). Determines object
   type and then uses appropriate algorithm to see if a value has been selected or entered.
PARAMS:
   objField [required] -- reference to field object from a form.
****************************************************************************************** */
function ynIsNullValue( objField ) {
   if (objField.type=='text'||objField.type=='hidden'||objField.type=='textarea'||objField.type=='password') {
      return (objField.value=="");
   } else if (objField.type=='select-one'||objField.type=='select-multiple') {
      return (objField.selectedIndex==-1);
   } else if (objField[0].type=='radio') {
      var ynFound=false;
      for (var ii=0; ii<objField.length; ii++) {
         ynFound=objField[ii].checked;
         if (ynFound) break;
      }
      return !ynFound;
   } else if (objField.type=='checkbox') {
      // This case (checkbox) may need work...lmc 3/16.
      return (objField.value=="");
   }
   return true;
}
