  var values_to_remove;
  var vtr_cnt = 0;
	
  function checkRange( elem, elem_min, elem_max )
  {
    if( elem.type == "text" )
    {
      if( elem.value.length > 0 )
      {
        var minval;
        var maxval;
        var value = parseInt( elem.value );

        if( elem_min )
          minval = parseInt( elem_min.value );
        if( elem_max )
          maxval = parseInt( elem_max.value );
          
        if( minval && value < minval )
          return false;
        if( maxval && value > maxval )
          return false;
      }
      return true;
    }
    return false;
  }

  function checkEMail( elem )
  {
    if( elem.type == "text" || elem.type == "password" )
    {
      if( elem.value.length > 0 )
      {
        var format1 = /(\w.+\@\w.+\.(([a-z][a-z])|([a-z][a-z][a-z]))\b)/;
        var format2 = /\b\s\b/;
        return format1.test(elem.value) && !format2.test(elem.value);
      }
      else
        return true;
    }
    return false;
  } 
    
  function checkFormat( elem, format )
  {
    if( elem.type == "text" || elem.type == "textarea" || elem.type == "password" )
    {
      if( elem.value.length > 0 )
        return ( elem.value == elem.value.match( format ) );
      else
        return true;
    }
    return false;
  }  
  
  function checkLength( elem, min )
  {
    if( elem.type == "text" || elem.type == "textarea" || elem.type == "password" )
    {      
      if( elem.value.length > 0 )    
      {
        if( elem.value.length >= min )
          return true;
      }
      else
        return true;
    }
    return false; 
  }
  
  function checkForbiddenChars( elem, list )
  {
    var i;
    if( elem.type == "text" || elem.type == "textarea" || elem.type == "password" )
    {      
      for( i = 0; i < list.length; i++ )
      {
        if( elem.value.indexOf( list.charAt( i ) ) > 0 )
          return false;
      }
      return true;
    }
    return false; 
  }
  
  function checkAllowedChars( elem, chars, elem_add )
  {
    var i;
    if( elem.type == "text" || elem.type == "textarea" || elem.type == "password" )
    {      
      if( elem.value.length > 0 )
      {
        var text = "";
        var tmp;
        
        if( elem_add )
        {
          for( i = 0; i < elem.value.length; i++ )
          {
            tmp = elem.value.charAt( i );
            if( elem_add.value.indexOf( tmp ) < 0 )
              text = text + tmp;
          }        
        }
        else
          text = elem.value;
        return text == text.match( chars );
      }
      else
        return true;
    }
    return false; 
  }
  
  function checkFilled( elem )
  {
    var len = 1;

    // the check whether elem.length exists doesn't work properly with netscape! 
    if( elem[0] )
      len = elem.length;

    if( len == 1 )
    {    
      // this check is possible for text, textarea, checkbox and radio
      // select-one fields: elem.length == elem.options.length
      if( elem.type == "text" || elem.type == "textarea" || elem.type == "password" )
        return elem.value.length > 0;
      if( elem.type == "checkbox" || elem.type == "radio" )
        return elem.checked;      
    }
    else
    {     
      var type = "";
      // this check is for "select-one" fields      
      if( elem.type )
        type = elem.type;
      else
        type = elem[0].type;
    
      if( type == "radio" || type == "checkbox" )
      {
        // find out if one of them is checked
        var i;
        for( i = 0; i < len; i++ )
          if( elem[i].checked )
            return true;
        return false;
      }

      // assumes that first option is used as "no selection"
      if( type == "select-one" )
        if( elem.selectedIndex > 0 )
          return true;
    }
    
    return false;
  }
  
  function regField( idx )
  {
    // add the given index to the array
    // in case of successful check, all elements values will be removed
    // the counter and the array is initialized before every check in checkForm
    values_to_remove[vtr_cnt++] = idx;
  }

  function checkElem( f, idx )
  {   
    // get the name of the field 
    var name = f.elements[idx].name; // complete name of hidden field

    // if it's a param field like _val_<name>_min, no check is neccessary
    // but the value should be removed, because it's for internal use only and has not to be send
    if( name.substr( 0, 1 ) == "_" && name.substr( 4, 1 ) == "_" )
    {
      regField(idx);
      return "";
    }

    // if there's no underscore '_' on position 4 in the string, this field is not used for checks
    if( name.substr( 3, 1 ) != "_" )
      return "";    

    var msg = f.elements[idx].value; // message to be displayed in error case
    var check = name.substr( 0, 4 ); // the name of the associated element
    var elem = f.elements[name.substr(4)]; // the associated element itself
        
    // element is required
    if( check == "req_" ) 
    { 
      regField(idx); 
      if( !checkFilled( elem ) ) 
        return msg + "\n"; 
    }

    // element must be integer
    if( check == "int_" )
    { 
      regField(idx); 
      if( !checkFormat( elem, /[0-9]+/ ) ) 
        return msg + "\n"; 
    }

    // range between min and max (including)
    if( check == "val_" )
    { 
      regField(idx); 
      if( !checkRange( elem, f.elements[ "_" + name + "_min"], f.elements[ "_" + name + "_max"] ) ) 
        return msg + "\n"; 
    }

    // element must be date (dd/mm/yyyy)
    if( check == "dat_" )
    { 
      regField(idx); 
      if( !checkFormat( elem, /[0-3][0-9]\/[0-1][0-9]\/[1-2][0-9][0-9][0-9]/ ) ) 
        return msg + "\n"; 
    }

    // element must be time (hh:mm:ss)
    if( check == "tml_" )
    { 
      regField(idx); 
      if( !checkFormat( elem, /[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]/ ) ) 
      return msg + "\n"; 
    }

    // element must be time (hh:mm)
    if( check == "tms_" )
    { 
      regField(idx); 
      if( !checkFormat( elem, /[0-2][0-9]\:[0-5][0-9]/ ) ) 
      return msg + "\n"; 
    }

    // element must contain at least _len_<name>_min characters
    if( check == "len_" )
    { 
      regField(idx); 
      if( !checkLength( elem, f.elements[ "_" + name + "_min"].value ) ) 
        return msg + "\n"; 
    }

    // element must not contain any character defined in the value of _not_<name>_chr    
    if( check == "not_" )
    { 
      regField(idx); 
      if( !checkForbiddenChars( elem, f.elements[ "_" + name + "_chr"].value ) ) 
        return msg + "\n"; 
    }
    
    // text only: a-z, A-Z
    if( check == "txt_" )
    { 
      regField(idx); 
      if( !checkAllowedChars( elem, /[a-z,A-Z]+/, f.elements[ "_" + name + "_add" ] ) ) 
        return msg + "\n"; 
    }
    
    // digits only (0-9)
    if( check == "dig_" )
    { 
      regField(idx); 
      if( !checkAllowedChars( elem, /[0-9]+/, f.elements[ "_" + name + "_add" ] ) ) 
      return msg + "\n"; 
    }
    
    // alphanumeric input (a-z, A-Z, 0-9)
    if( check == "aln_" )
    { 
      regField(idx); 
      if( !checkAllowedChars( elem, /[a-z,A-Z,0-9]+/, f.elements[ "_" + name + "_add" ] ) ) 
      return msg + "\n"; 
    }
    
    // must contain any valid eMail-Address
    if( check == "eml_" )
    { 
      regField(idx); 
      if( !checkEMail( elem ) ) 
      return msg + "\n"; 
    }

    // extend the list of validitation methods here!
    
    return "";
  }

  function checkForm( name )
  {
    var netscape = document.layers?true:false;
    var explorer = document.all?true:false;

    // only netscape and explorer allowed
    // otherwise no submit, because the validation of the elements is not given
    //if( !netscape && !explorer )
    //  return false;

    // get the required form passed by name
    var f;
		if( explorer ) 
			f = document.all[name];
    else 
			f = document.forms[name];

    // init values to remove array
    values_to_remove = new Array( f.elements.length );
    vtr_cnt = 0;
    
    var result = "";
    // check all elements
    var i;
    for( i = 0; i < f.elements.length; i++ )
      if( f.elements[i].type == "hidden" ) 
        result += checkElem( f, i );

    // if any error message has to be printed
    // if so, do it and avoid submitting the form
    // if not, return true, so the form will be submitted
    if( result != "" )    
    {
      alert( result );
      return false;
    }
    else
    {
      for( i = 0; i < vtr_cnt; i++ )
        f.elements[values_to_remove[i]].value = "";
      return true;
    }
  }
