function FormCheck(fields) // Ctor
{
   this.fields = fields
   this.InputCheck = FormCheck_InputCheck
   // Function below is for internal use.
   this._reqif = FormCheck__reqif
   this._ext = FormCheck__ext
}

function FormCheck_InputCheck(objForm)
{
   var isOk = true
   var index

   for (index = 0; isOk && index < this.fields.length; index++)
   {
      if (this.fields[index].type == "required")
      {
         if (objForm[this.fields[index].fld].value == "")
         {
            isOk = false;
            alert("Field '" + this.fields[index].name 
               + "' is required. Please specify.")
            objForm[this.fields[index].fld].focus();
         }  // if
      }
      else if (this.fields[index].type.substring(0, 5) == "reqif")
      {
         if (this._reqif(objForm, this.fields[index].type.substring(5, 
               this.fields[index].type.length)))
         {
            if (objForm[this.fields[index].fld].value == "")
            {
               isOk = false;
               alert("Field '" + this.fields[index].name 
                  + "' is required. Please specify.")
               objForm[this.fields[index].fld].focus();
            }  // if
         }  // if
      }
      else if (this.fields[index].type.substring(0, 3) == "ext")
      {
         var typeList // List of accepted types (returned by _ext()).

         typeList = this._ext(objForm, this.fields[index].fld,
                       this.fields[index].type.substring(3, 
                       this.fields[index].type.length), typeList)

         if (typeList != '')
         {
            isOk = false;
            alert("Invalid document type for field '" + 
               this.fields[index].name + "'. (Use " + typeList + ")")
            objForm[this.fields[index].fld].focus();
         }
      }
      else if (this.fields[index].type == "dateNl")
      {
         datum = new Datum(objForm[this.fields[index].fld].value, Datum.FORMAT_DDMMYY)
         
         if (!datum.IsOk())
         {           
            isOk = false
            alert(this.fields[index].name + ': ' + datum.FoutmeldingEN())
            objForm[this.fields[index].fld].focus();
         }
      }
      else if (this.fields[index].type.substr(0, 6) == "maxlen")
      {
         maxlen = parseInt(this.fields[index].type.substring(7,
            this.fields[index].type.length -1))

         if (objForm[this.fields[index].fld].value.length > maxlen)
         {
            isOk = false;
            alert("Maximum length for field '" + this.fields[index].name
               + "' is " + maxlen + " characters. Please shorten your input.")
            objForm[this.fields[index].fld].focus();
         }
      }
      else if (this.fields[index].type.substr(0, 7) == "numeric")
      {
         valid = !isNaN(parseInt(objForm[this.fields[index].fld].value))

         if (!valid)
         {
            isOk = false;
            alert("Please specify a numeric value for field '" + 
            	this.fields[index].name + "'.")
            objForm[this.fields[index].fld].focus();
         }
      }
   }  // while
   
   return isOk;
}  // FormCheck_InputCheck

// Checks to see if the condition given in the condition
// parameter is actually met. The condition parameter is
// of the form:  {fieldname, condition}
//
// condition can be: checked    field is a checkbox and must be checked.
//                   unchecked  field is a checkbox and must be unchecked.
//                   empty      field is empty.
//                   filled     field is not empty.
//
function FormCheck__reqif(objForm, condition)
{
   var conditionmet = false
   var comma = condition.indexOf(',')
   var braceopen = condition.indexOf('{')
   var braceclose = condition.indexOf('}')

   if (comma != -1 && braceopen != -1 && braceclose != -1)
   {
      var fldname = condition.substring(1, comma)
      var typecheck = condition.substring(comma + 1, braceclose)
      var objfld = MM_objFind(fldname, objForm)
      
      if (typecheck == 'checked')
         conditionmet = objfld.checked 
      else if (typecheck == 'unchecked')
         conditionmet = !objfld.checked 
      else if (typecheck == 'empty')
         conditionmet = objfld.value.length == 0
      else if (typecheck == 'filled')
         conditionmet = objfld.value.length != 0
   }
   else
      alert('FormCheck: reqif condition spec error')

   return conditionmet
}  // FormCheck__reqif

//
// Controleert of de extensie van het gegeven veld voorkomt in de
// lijst met extensies die zijn toegestaan. Retourneert een lege
// string indien dat zo is, of een lijst met wel toegestande extensies
// indien de gevonden extensie niet wordt goedgekeurd.
//
function FormCheck__ext(objForm, fldname, condition, typeList)
{
   typeList = ''

   var index
   var braceopen = condition.indexOf('{')
   var braceclose = condition.indexOf('}')

   if (braceopen != -1 && braceclose != -1)
   {
      var objfld = MM_objFind(fldname, objForm)

      // Leeg veld wordt goedgekeurd dus alleen de controle
      // uitvoeren indien een waarde is ingevoerd in het te
      // controleren veld.
      if (objfld.value.length != 0)
      {
         var extList  // Lijst met te checken extensies.
         var exts     // Array met extensies uit extList (via String.split()).
         var period   // Positie van punt in antwoordveld (-1 indien geen punt).
         var ok       // true zodra de extensie in array exts is gevonden.
         var ext      // de te onderzoeken extensie (of '' bij geen extensie)
         
         extList = condition.substring(braceopen + 1, braceclose)
         exts = extList.split(",")
         period = objfld.value.lastIndexOf('.')
         if (period == -1) 
            ext = ''
         else
            ext = objfld.value.substring(period + 1,
               objfld.value.length)

         ok = false
         for (index = 0; !ok && index < exts.length; index++)
            ok = (ext.toUpperCase() == exts[index].toUpperCase())

         if (!ok)
         {
            // Lijst aanmaken met toegestane types.
            for (index = 0; index < exts.length; index++)
            {
               if (typeList.length != 0)
               {
                  if (index + 1 == exts.length)
                     typeList = typeList + ' or '
                  else
                     typeList = typeList + ', '
               }
               typeList += '.' + exts[index]
            }  // for
         }  // if
      }  // if
   }
   else
      alert('FormCheck: ext condition spec error')

   return typeList
}

