/*
  Script for client-side form checking
  Versie 20090514

  Example HTML:
  
  <script src="site/scripts/intronet_formcheck.js"></script>
  <script><!-- 
    // List of fields to check
    // Caution! The last line does not end in a comma!
    var fieldlist = [
      ['fieldNameA' , 'Error message if not correctly filled'],
      ['fieldNameB' , 'Error message if not correctly filled', 'CheckType'],
      ['fieldNameC' , 'Error message if not correctly filled', 'CheckType', 'Condition'],
      ['fieldNameD' , 'Error message if not correctly filled'] // no comma on last line
    ];  
  --></script>

  <form onsubmit="return intronetCheckForm(fieldlist)">

  
  'CheckType' can be:
  - FILLED (default, checks if field is filled)
  - EMAIL (checks if the field contains a valid email address [text fields only])
  - LENGTH:999 (checks if the field contains at least 999 characters [text fields only])
  
  'Condition' can contain a condition that makes this field required. If the condition isn't
  met, this field is not required. 
  'Condition' can contain:
  'FILLED:name' (check only if field 'name' is filled/checked)
  'VALUE:name:value' (check only if field 'name' contains the value 'value')
*/

function intronetCheckForm(lst) {
  var i;   
  var error = '';
  for(i=0; i<lst.length; i++) {
    var item = lst[i];

    /* check for conditions */
    var skip = false;
    if(item.length > 3) {
      var cond = item[3].split(':');
      cond[0].toUpperCase;
      switch(cond[0]) {
        case 'FILLED':
          var check = [ cond[1] ];
          if(!intronetCheckItem(check)) skip = true;
          break;
        case 'VALUE':
          if(!intronetCheckFormValue(cond[1], cond[2])) skip = true;
          break;
        default:
          alert('Unknown condition type '+cond[0]);
      }
    }
    
    /* check field */ 
    if(!skip && !intronetCheckItem(item))
      error+= item[1]+'\n';
  }                     

  if(error == '') {      
    return(true); 
  } else {     
    alert(error);
    return(false);
  }  
}

/* Checks a single item line */
function intronetCheckItem(item) {
  var elementlist = document.getElementsByName(item[0]);
  if(elementlist.length > 0) {
    elm = elementlist[0];               

    /* check if element is properly set */
    switch(elm.type) {
      case 'checkbox':
        if(!elm.checked) return(false);
        break;
      case 'radio':
        var check = false;
        for(var radionr=0; radionr < elementlist.length; radionr++) 
          if(elementlist[radionr].checked) 
            check = true;                  
        if(!check) return(false);
        break;
      case 'select-one':
        if(elm.value == '' || elm.value == '0') return(false);
        break;
      case 'select-multiple':
        if(elm.value == '') return(false);
        break;
      case 'text':    
        if(item.length > 2) {
          var checkType = item[2].split(':');
        } else {
          checkType = [ 'FILLED' ];
        }                          
        checkType[0].toUpperCase;
        switch(checkType[0]) {
          case 'FILLED':
            if(elm.value == '') return(false); 
            break;
          case 'EMAIL':
            if(!elm.value.match(/[a-z0-9._+-]+@[a-z0-9.-]+\.[a-z][a-z]+/i)) return(false); 
            break;
          case 'LENGTH':
            if(elm.value.length < checkType[1]) return(false); 
            break;
          default:
            alert('Unknown check type '+checkType[0]);
        }
        break;
    }
  } else {
    alert('Form element '+item[0]+' not found');
    return(false);
  }    
  
  return(true);
}

/* Checks if the element elementName has the value elementValue. Supports multi-selectboxes and radio buttons */
function intronetCheckFormValue(elementName, elementValue) {
  var elementlist = document.getElementsByName(elementName);
  if(elementlist.length > 0) {
    switch(elementlist[0].type) {
      case 'checkbox':
      case 'radio':
        for(var i=0; i < elementlist.length; i++) 
          if(elementlist[i].checked && elementlist[i].value == elementValue) 
            return(true);  
        break;
      case 'select-one':
      case 'select-multiple':
      case 'text':
        for(var i=0; i < elementlist.length; i++) 
          if(elementlist[i].value == elementValue) 
            return(true);
        break;
    }
  } else {
    alert('Form element '+item[0]+' not found');
    return(false);
  }    
  
  return(false);
}
