function InitValidation(pfield, pNullable, pValidFcts, pOnErrorFct)
{
 if(pfield.tagName=='undefined')
 {
  pfield = pfield[0] ;
 }

 pfield.nullable = pNullable ;
 pfield.valid_fcts = pValidFcts ;
 pfield.valid = true ;
 pfield.onErrorFct = pOnErrorFct ;
}

function ValidateField(pfield)
{
 pfield.valid=true ;

 // If not excluded
 if(!parseInt(pfield.getAttribute('exclude')) && (pfield.type!='hidden'))
 {
  // If there is a value
  var no_spaces_val = pfield.value.replace(/\s+/g,'');
  
  if(no_spaces_val.length!=0)
  {
   if(pfield.valid_fcts)
   {
    fct_str = pfield.valid_fcts.replace(/this/gi, "pfield");

    var fcts = fct_str.split(';') ;

    for(var i=0; i < fcts.length; i++)
    {
     if(fcts[i])
     {
      pfield.valid &= eval(fcts[i]) ;
     }
    }
   }
  }
  else
  {
   // If there is not a value > verify that the field can be null
   if(!pfield.nullable)
   {
    pfield.valid=false ;
   }
  }

  if(pfield.onErrorFct)
  {
   pfield.onErrorFct() ;
  }
 }
}

function ValidateForm(pForm)
{
 var ret=true ;

 for(var i = 0; i < pForm.elements.length; i++)
 {
  var elem = pForm.elements[i] ;

  if(typeof(elem.valid) != 'undefined')
  {
   ValidateField(elem) ;
   ret &= elem.valid ;
  }
 }

 return ret ;
}

function InitAllFields(pcbs)
{
 with(document.param_f)
 {
  for(i in elements)
  {
   if(elements[i])
   {
    if((elements[i].tagName=='INPUT') || (elements[i].tagName=='SELECT') || (elements[i].tagName=='TEXTAREA'))
    {
     if(!elements[i].getAttribute('exclude') && (typeof(elements[i].valid) == 'undefined'))
     {
      pcb = '' ;

      if(pcbs)
      {
       if(pcbs[elements[i].name])
       {
        pcb = pcbs[elements[i].name] ;
       }
      }

      InitValidation(elements[i], false, pcb, OnFormErrorCB) ;
     }
    }
   }
  }
 }
}

// Validation functions

function IsANumber(pfield, pclass)
{
 ret=true ;
 if(isNaN(pfield.value*1))
 {
  ret=false ;
 }

 return ret;
}


function ArithmeticTest(pfield, poperator, pval, pclass)
{
 ret=true ;
 if(isNaN(pfield.value))
 {
  ret=false ;
 }
 else
 {
  if(!eval(pfield.value+poperator+pval))
  {
   ret=false ;
  }
 }

 return ret;
}

function IsADate(pelem)
{
 var objRegExp = /^\d{1,2}\/\d{1,2}\/\d{2,4}$/

 strValue = pelem.value ;

 //check to see if in correct format
 if(!objRegExp.test(strValue))
 {
  return false; //doesn't match pattern, bad date
 }
 else
 {
  var arrayDate = strValue.split('/'); //split date into month, day, year

  //create a lookup for months not equal to Feb.
  var arrayLookup = { 1 : 31, 3 : 31, 4 : 30, 5 : 31, 6 : 30, 7 : 31,
                      8 : 31, 9 : 30, 10 : 31, 11 : 30, 12 : 31}

  var intDay = parseInt(arrayDate[0], 10);

  //check if month value and day value agree
  var vmonth = parseInt(arrayDate[1], 10) ;

  if(arrayLookup[vmonth] != null)
  {
   if((intDay <= arrayLookup[vmonth]) && (intDay != 0))
   {
    return true; //found in lookup table, good date
   }
  }

  //check for February (bugfix 20050322)
  var intMonth = parseInt(arrayDate[1], 10);
  if (intMonth == 2)
  {
   var intYear = parseInt(arrayDate[2], 10);
   if((((intYear % 4 == 0) && intDay <= 29) || ((intYear % 4 != 0) && intDay <=28)) && (intDay !=0))
   {
    return true; //Feb. had valid number of days
   }
  }
 }
 return false; //any other values, bad date
}

function isATime(pelem)
{
 var objRegExp = /^\d{1,2}:\d{1,2}$/

 strValue = pelem.value ;

 //check to see if in correct format
 if(!objRegExp.test(strValue))
 {
  return false; //doesn't match pattern, bad time
 }
 else
 {
  var arrayTime = strValue.split(':'); //split date into hour / time

  ph = parseInt(arrayTime[0],10) ;
  if((ph < 0)||(ph>23))
  {
   return false ;
  }

  ph = parseInt(arrayTime[1],10) ;
  if((ph < 0)||(ph>59))
  {
   return false ;
  }
 }

 return true ;
}