//------------------------------------------------------------------------------
// FICHIER : $RCSfile: stream_check.js,v $
// AUTEUR  : $Author: autran $
// VERSION : $Revision: 1.2 $
// DATE    : $Date: 2005/03/01 10:53:28 $
//------------------------------------------------------------------------------

//=~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=
// Manage stream checked field Form.
//=~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=~==~=

//==============================================================================
// Core functions.
//==============================================================================

//------------------------------------------------------------------------------
// streamCheckformList(oForm, arg1, arg2, .. , argN)
//  Check all fields of a form with the list of user-check-function* listed in 
//  args parameters.
//  *user-check-function : is a fonction which take only one argument (an 
//                         '<input>' form object) and return true or false
//                         following the user wants to check.  
// NOTE 1:
//  This Function take ONE or N ARGUMENTS.
//  The first parameter is mandotory must be an '<form>' object.
//  Each argument must have the syntaxe beloxw :
//  - ['check-function-name',['fieldName1','fieldName2', .. , fieldNameX]]
//   where :
//   - check-function-name : string name of the function to check on the
//                             all form's fields listed in fields args array. 
//   - fieldName           : field to check.
//
//  Ex :
//  <form name="mon form" method="POST" 
//
//    onSubmit="
//     return streamCheckformList
//      (this,
//       [ ['chkfuncNull'] , [ ['U_FIELD1']               , ['U_FIELD2']] ],
//       [ ['chkfuncFloat'], [ ['U_FIELD1']               , ['U_FIELD2']] ],
//       [ ['chkMax100']   , [ ['U_FIELD1','U_FIELD2']                  ] ]);
//    ">
//
//  In this example :
//    - 'chkfuncNull'  : a user-check-function who takes one argument and test
//      if this arg is not null.
//    - 'chkfuncFloat' : a user-check-function who takes one argument and test
//      if this arg have a float syntaxe.
//    - 'chkMax100'    : a user-check-function who takes two arguments and test
//      if this arg have a float syntaxe.
//
// NOTE 2:
//   Warning ! The sort order of declaration of function is the sort order 
//   in which the functions will be call.
// 
//------------------------------------------------------------------------------
function streamCheckformList(oForm) {
  var i,j,s,bIsOk;
  var oThis,oItem; 
  
  //-- Inits
  bIsOk = true;
  s     = '';
  oThis = streamCheckformList;
  
  if (!oForm) return false;
  
  //----------------------------------------------------------------------------
  // Store all stream 'object input' form
  //----------------------------------------------------------------------------
  
  //Array to store all stream 'object input' form
  //{TODO : maybe put this array in a global var javascript}
  var aObjItems = Array(1);
  
  //Init aObjItems
  for (j=0; j < oForm.elements.length; j++) {
      oItem = oForm.elements[j];
      
      // Store only "text", "textarea", "checkbox", "radio", "password"
      if (   (oItem.type != "text")
          && (oItem.type != "textarea")
          && (oItem.type != "checkbox")
          && (oItem.type != "radio")
          && (oItem.type != "password")
         ) continue;
      
      // Only stream md5 input are prsed
      // {32chars}[nomchamp][value]    
      if (oItem.name.length <= 33) continue;
      sFieldCode  = oItem.name;
      sPostfixe ="][value]";
      sFieldCode  = sFieldCode.slice (33,0-sPostfixe.length);
      aObjItems[sFieldCode]= oItem;
  }
  
  //--------------------------------------------------------------------------
  // Loop on all functions to apply.
  //--------------------------------------------------------------------------
  
  for (i=1; i<oThis.arguments.length; i++) {
    oArg_i = oThis.arguments[i];
   
    //-- Test if each argument is an array of 2 items.
    if (oArg_i.length!=2) continue;
    
    //-- Test if each argument is an array like : {array}{array} form. 
    if (! (   (typeof(oArg_i[0]) == 'object')
            &&(typeof(oArg_i[1]) == 'object') )) continue;
    
    sJsFuncName = oArg_i[0][0];
     
    aJsFields   = oArg_i[1];
    //-- Eval function !
    
    bIsOk = eval('streamCheckFuncWrapper'+'('+"'"+sJsFuncName+"'"+', aObjItems, aJsFields'+');');
    if (!bIsOk) return false;
    
  }
  
  return true;
}


//------------------------------------------------------------------------------
// Checked function wrapper. 
//------------------------------------------------------------------------------
function streamCheckFuncWrapper(sFuncName, aObjItems, aArgs) {
  var j,sNameDecode;
  var bIsOk = true;
    
    if (aArgs != null) {
      for (j = 0; j < aArgs.length; j++) {
        bIsOk  = eval(sFuncName+'(aObjItems,  aArgs[j]);');
        if (! bIsOk) return false;
      }
    }
    
    return true;
}

//==============================================================================
// COMMENTS : 
// some exmaples of check user funcion .
// theses function can be put directly in application because
// of particular locale alert message.
//==============================================================================

//------------------------------------------------------------------------------
// isCheckError : Manage and display check error in load of user check function.
//------------------------------------------------------------------------------
/*
function streamCheckIsError (aObjItems,aArgs,indx) {
  var sName, oItem;
  
  if (!(sName = aArgs[indx])) {
     window.alert ("Un champ à tester est introuvable !");
     return false;
  } else {
    if (!(oItem = aObjItems[sName])) {
      window.alert (aArgs[indx]+": champ à tester introuvable !");
      return false;
    }
    return true;
  }
  
}
*/

//------------------------------------------------------------------------------
// chkfuncNull : (user-check-function) Check if the input value is "".
//------------------------------------------------------------------------------
/*
function chkfuncNull(aObjItems, aArgs) {
  var sName, oItem;
  
  //-- Manage & display error.
  if (!streamCheckIsError (aObjItems, aArgs, 0)) return false;
  
  //-- Check test
  if ( (sName = aArgs[0]) && (oItem = aObjItems[aArgs[0]]) ) {
    if (oItem.value == "") {
      window.alert ("Ce champ ne doit pas être vide !");
      oItem.select ();
      return false;
    }
    return true;
  }
  return true;
}
*/

//------------------------------------------------------------------------------
// chkfuncFloat : (user-check-function) Check if the input value is a float.
//------------------------------------------------------------------------------
/*
function chkfuncFloat(aObjItems, aArgs) {
  var sName, oItem;
  
  //-- Manage & display error.
  if (!streamCheckIsError (aObjItems, aArgs, 0)) return false;
  
  //-- Check test
  if ( (sName = aArgs[0]) && (oItem = aObjItems[aArgs[0]]) ) {
    
    val = ""+oItem.value;
    
    if (!val.match(/^[0-9]+\.?[0-9]*$/)) {
      window.alert ("Mauvais format ! ce champ doit etre un float !");
      oItem.select ();
      return false;
    }
    return true;
  }
  return true;
}
*/

//------------------------------------------------------------------------------
// chkfuncFloat : (user-check-function) Check if the input value is a float.
//------------------------------------------------------------------------------
/*
function chkMax100(aObjItems, aArgs) {
  var sName1, oItem1, sName2, oItem2;
 
  //-- Manage & display error.
  if (!streamCheckIsError (aObjItems, aArgs, 0)) return false;
  if (!streamCheckIsError (aObjItems, aArgs, 1)) return false;
  
  //-- Check test
  if (  (sName1 = aArgs[0]) && (oItem1 = aObjItems[aArgs[0]])
      &&(sName2 = aArgs[1]) && (oItem2 = aObjItems[aArgs[1]]) 
      ) {
    
    if ( parseFloat(oItem1.value)+parseFloat(oItem2.value) > 100) {
      window.alert ("Attention la somme des champs ne doit pas > 100 !");
      //We select the first field for example...
      oItem1.select ();
      return false;
    }
    return true;
  }
  return true;
}
*/
//------------------------------------------------------------------------------
// streamChkOperator (user-check-function)
// Note : args2, args 3 are parameters function and not items!
// Proble is to genere a good languge alert...
// Maybe to do : call streamChkOperator with an other (not mandatory) args with
// the label msg.
//
//------------------------------------------------------------------------------
/*
function streamChkOperator(aObjItems, aArgs) {
  var sName1, oItem1;
  var iMax;
  var sOperator = '';
  
  //-- Manage & display error.
  if (!streamCheckIsError (aObjItems, aArgs, 0)) return false;
  
  //-- Check test
  if (   (sName1 = aArgs[0]) && (oItem1 = aObjItems[aArgs[0]])
       &&(sOperator=aArgs[1])
       &&(iMax=aArgs[2])
      ) {
    
    if ( eval('parseFloat(oItem1.value)'+sOperator+'parseFloat(iMax)')) {
      window.alert ("Attention la somme des champs ne doit pas "+sOperator+" "+iMax+" !");
      //We select the first field for example...
      oItem1.select ();
      return false;
    }
    return true;
  }
  return true;
}
*/

//------------------------------------------------------------------------------
// $Log: stream_check.js,v $
// Revision 1.2  2005/03/01 10:53:28  autran
// Allow "password" input form to be stored by streamCheckformList().
//
// Revision 1.1  2005/02/22 09:04:21  autran
// Initial Version.
//
// Revision 1.3  2005/02/03 11:00:53  autran
// Add streamChkOperator function.
//
// Revision 1.2  2005/01/28 15:19:02  autran
// Add  "textarea", "checkbox", "radio" in the element form to store in aObjItems.
//
// Revision 1.1  2005/01/28 14:25:43  autran
// Initial version.
//
// 
//-- End of source  ------------------------------------------------------------
