function validator(){}
validator.prototype.string=function(s){if(s.length == 0 || (/^\s+$/.test(s)))return false; return true;};
validator.prototype.email=function(s){return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(s);};
validator.prototype.checked=function(s){if(s.checked)return true;return false;};
validator.prototype.datum=function(s){return /^((((0?[1-9]|[12]\d|3[01])[\-](0?[13578]|1[02])[\-]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\-](0?[13456789]|1[012])[\-]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\-]0?2[\-]((1[6-9]|[2-9]\d)?\d{2}))|(29[\-]0?2[\-]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/i.test(s);};
validator.prototype.valuta=function(s){return /^([0-9])+(,[0-9]{2})?$/.test(s);};
validator.prototype.postcode=function(s){return /^([0-9]{4})(\s)?([a-zA-Z]{2})$/.test(s);};
validator.prototype.telefoon=function(s){return /^([\+][0-9]{2}[\(][0-9][\)][0-9]{1,3}[-][0-9]{6,10})$|^[0-9]{3}-[0-9]{7}$|^[0-9]{4}-[0-9]{6,8}$|^[0-9]{4}-[0-9]{4}$|^06-[0-9]{8}$/i.test(s)};
validator.prototype.pass=function(s){return /^[a-zA-Z0-9~\!@#\$%\^&\*\(\)-\+;\:\.,]{6,20}$/i.test(s)}; 
validator.prototype.postcodecijfers=function(s){return /^\d{4}$/i.test(s)}; 
validator.prototype.aantal=function(s){return /^([0-9])+$/i.test(s)};
validator.prototype.postcodeletters=function(s){return /[a-zA-Z]{2}$/i.test(s)}; 


var oVal = new validator;  

function validateForm(parentId, doSubmit){
  okToSubmit = true;
  inputObjs  = document.getElementById(parentId).getElementsByTagName("input");
  textareaObjs  = document.getElementById(parentId).getElementsByTagName("textarea");
  selectObjs  = document.getElementById(parentId).getElementsByTagName("select");

  if (inputObjs.length > 0){
    okToSubmit = checkObjectArrayValues(inputObjs);
  }
  if (textareaObjs.length > 0 && okToSubmit){
     okToSubmit = checkObjectArrayValues(textareaObjs);
  }
  if (selectObjs.length > 0 && okToSubmit){
     okToSubmit = checkObjectArrayValues(selectObjs);
  }
  
  if (okToSubmit){
    if (doSubmit){
      document.getElementById(parentId).submit();
    }
    return true;
  } else {
    return false;  
  }
}

function checkObjectArrayValues(checkObjs){   
  for (i = 0; i < checkObjs.length; i++){
    if (checkObjs[i].className.indexOf("error") != -1){
      checkObjs[i].className = checkObjs[i].className.substring(0,checkObjs[i].className.indexOf("error"));
    }
    if (checkObjs[i].type != 'select'){
      checkObjs[i].value = trim(checkObjs[i].value);
    }

    if (checkObjs[i].id == "verplicht_datum") {
      if (!oVal.datum(checkObjs[i].value)){
        alert("Ongeldige datum ingevoerd!");
        checkObjs[i].focus();
        checkObjs[i].select();
        return false;
      }
    } else if (checkObjs[i].id == "verplicht_pass"){
      if (!oVal.pass(checkObjs[i].value)){
        alert("Ongeldig wachtwoord ingevoerd!");
        checkObjs[i].focus();
        checkObjs[i].select();
        return false;
      }
    } else if (checkObjs[i].id == "verplicht_postcodecijfers"){
      if (!oVal.postcodecijfers(checkObjs[i].value)){
        alert("Postcode moet uit 4 cijfers bestaan!");
        checkObjs[i].focus();
        checkObjs[i].select();
        return false;
      }
    } else if (checkObjs[i].id == "optioneel_postcodeletters"){
      if (!checkObjs[i].value.length == 0 && !oVal.postcodeletters(checkObjs[i].value)){
        alert("Als gevuld, moet waarde moet uit twee letters bestaan!" + checkObjs[i].value);
        checkObjs[i].focus();
        checkObjs[i].select();
        return false;
      } 
    } else if (checkObjs[i].id == "optioneel_postcode"){
      if (!checkObjs[i].value.length == 0 && !oVal.postcode(checkObjs[i].value)){
        alert("Ongeldige postcode ingevoerd!\nAls u een postcode invoert moet deze bestaan uit 4 cijfers en 2 letters.\n");
        checkObjs[i].focus();
        checkObjs[i].select();
        newClassName = checkObjs[i].className + ' error';
        checkObjs[i].className = newClassName;        
        return false;
      }                
    } else if (checkObjs[i].id == "verplicht_string"){
      if (!oVal.string(checkObjs[i].value)){
        alert("Verplicht veld niet ingevuld!");
        if (!checkObjs[i].disabled) { checkObjs[i].focus();};
        newClassName = checkObjs[i].className + ' error';
        checkObjs[i].className = newClassName;
        return false;
      }      
    } else if (checkObjs[i].id == "verplicht_email"){
      if (!oVal.email(checkObjs[i].value)){
        alert("Ongeldig mailadres ingevoerd!");
        checkObjs[i].focus();
        checkObjs[i].select();
        newClassName = checkObjs[i].className + ' error';
        checkObjs[i].className = newClassName;        
        return false;
      }   
    } else if (checkObjs[i].id == "optioneel_email"){
      if (!checkObjs[i].value.length == 0 && !oVal.email(checkObjs[i].value)){
        alert("Ongeldig mailadres ingevoerd!");
        checkObjs[i].focus();
        checkObjs[i].select();
        return false;
      }             
    } else if (checkObjs[i].id == "verplicht_checked"){
      if (!oVal.checked(checkObjs[i])){
        alert("U dienst akkoord te gaan met de voorwaarden voordat u verder kunt gaan.");
        checkObjs[i].focus();
        return false;
      }         
    } else if (checkObjs[i].id == "verplicht_valuta"){
      if (!oVal.valuta(checkObjs[i].value)){
        alert("Ongeldig bedrag ingevoerd!");
        checkObjs[i].focus();
        checkObjs[i].select();
        return false;
      }         
    } else if (checkObjs[i].id == "verplicht_postcode"){
      if (!oVal.postcode(checkObjs[i].value)){
        alert("Ongeldige postcode ingevoerd!\nPostcode moet bestaan uit 4 cijfers en 2 letters.\n");
        checkObjs[i].focus();
        checkObjs[i].select();
        newClassName = checkObjs[i].className + ' error';
        checkObjs[i].className = newClassName;
        return false;
      }         
    } else if (checkObjs[i].id == "verplicht_telefoon"){
      if (!oVal.telefoon(checkObjs[i].value)){
        alert("Ongeldig telefoonnummer ingevoerd!\nHet nummer dient alsvolgt te worden ingevoerd:\n- Vast nr NL: 020-1234567\n- Mobiel nr NL: 06-12345678\n- 0800 en 090x nr: 0800 of 090x - gevolgd door 4, 6 , 7 of 8 cijfers\n- Buitenland:  +31(0)20-1234567\n");
        checkObjs[i].focus();
        checkObjs[i].select();
        return false;
      }  
    } else if (checkObjs[i].id == "optioneel_telefoon"){
      if (!checkObjs[i].value.length == 0 && !oVal.telefoon(checkObjs[i].value)){
        alert("Ongeldig telefoonnummer ingevoerd!\nAls u een telefoonnummer invoert moet het nummer alsvolgt worden ingevoerd:\n- Vast nr NL: 020-1234567\n- Mobiel nr NL: 06-12345678\n- 0800 en 090x nr: 0800 of 090x - gevolgd door 4, 6, 7 of 8 cijfers\n- Buitenland:  +31(0)20-1234567\n");
        checkObjs[i].focus();
        checkObjs[i].select();
        return false;
      }         
    } else if (checkObjs[i].id == "optioneel_fax"){
      if (!checkObjs[i].value.length == 0 && !oVal.telefoon(checkObjs[i].value)){
        alert("Ongeldig faxnummer ingevoerd!\nHet nummer dient alsvolgt te worden ingevoerd:\n- Vast nr NL: 020-1234567\n- 0800 & 0900 nr 0900-222222\n- Buitenland:  +31(0)20-1234567\n");
        checkObjs[i].focus();
        checkObjs[i].select();
        return false;
      }               
    } else if (checkObjs[i].id == "verplicht_aantal"){
      if (!oVal.aantal(checkObjs[i].value)){
        alert("Ongeldig aantal ingevoerd!");
        checkObjs[i].focus();
        checkObjs[i].select();
        newClassName = checkObjs[i].className + ' error';
        checkObjs[i].className = newClassName;        
        return false;
      }         
    } else if (checkObjs[i].id == "optioneel_aantal"){
      if (!checkObjs[i].value.length == 0 && !oVal.aantal(checkObjs[i].value)){
        alert("Ongeldig aantal ingevoerd!");
        checkObjs[i].focus();
        checkObjs[i].select();
        newClassName = checkObjs[i].className + ' error';
        checkObjs[i].className = newClassName;        
        return false;
      }         
    }    
  } 
  return true;
}

function trim(value) {
  value = value.replace(/^\s+/,''); 
  value = value.replace(/\s+$/,'');
  return value;
}
