function validator(){}
validator.prototype.string=function(s){if(s.length == 0 || (/^\s+$/.test(s)))return false; return true;};
validator.prototype.user=function(s){if(s.length < 4 || (/^\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.maandjaar=function(s){return /^([0-9]{2})([\-/])([0-9]{4})$/.test(s);};
validator.prototype.telefoon=function(s){return /^[0-9_\+\(\)\.\-\s]{10,}$/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;  
var parentFormId;

var numberOfForgotten;
var fieldsForgotten = new Array();

function validateForm(parentId, doSubmit){
  okToSubmit1 = true;
  okToSubmit2 = true;
  okToSubmit3 = true;
  parentFormId = parentId;
  fieldsForgotten = new Array();
  numberOfForgotten = 0;
  
  inputObjs  = document.getElementById(parentId).getElementsByTagName("input");
  textareaObjs  = document.getElementById(parentId).getElementsByTagName("textarea");
  selectObjs  = document.getElementById(parentId).getElementsByTagName("select");

  if (inputObjs.length > 0){
     okToSubmit1 = checkObjectArrayValues(inputObjs);
  }
  if (textareaObjs.length > 0){
     okToSubmit2 = checkObjectArrayValues(textareaObjs);
  }
  if (selectObjs.length > 0){
     okToSubmit3 = checkObjectArrayValues(selectObjs);
  }
  
  if(numberOfForgotten > 0) {
      message = "U bent "+numberOfForgotten+ " velden vergeten of onjuist ingevoerd, te weten:\n";   
     
      nrItems = fieldsForgotten.length;
      for(i=0;i<nrItems;i++)
      {
           message+= "- "+fieldsForgotten[i]['name']+"\n";
      }
      alert(message);
  }

  if (okToSubmit1 && okToSubmit2 && okToSubmit3){
    if (doSubmit){
      document.getElementById(parentId).submit();
    }
    return true;
  } else {
    return false;  
  }
}

function addForgotten(fieldname, error) {
    itemNr = fieldsForgotten.length;
    fieldsForgotten[itemNr] = new Array();
    fieldsForgotten[itemNr]['name'] = fieldname;    
    fieldsForgotten[itemNr]['error'] = error;  
    numberOfForgotten++;
}

function checkObjectArrayValues(checkObjs){ 
  validated = true; 
  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].type != 'file'){
      checkObjs[i].value = trim(checkObjs[i].value);
    }

    if (checkObjs[i].id == "verplicht_datum") {
      if (!oVal.datum(checkObjs[i].value)){
        showFieldError(checkObjs[i].name,"Ongeldige datum ingevoerd!");    
        addForgotten(checkObjs[i].title,"Ongeldige datum ingevoerd!");
        validated = false;
      }
    } else if (checkObjs[i].id == "verplicht_string_not_check") {
       if(!document.getElementById(checkObjs[i].name + "_check").checked) 
        {
         if (!oVal.string(checkObjs[i].value)){
            showFieldError(checkObjs[i].name,"Verplicht veld niet ingevuld!");
            addForgotten(checkObjs[i].title,"Verplicht veld niet ingevuld!");
            if (!checkObjs[i].disabled) { };
            validated = false;
        } 
      }
    } else if (checkObjs[i].id == "verplicht_maandjaar_not_check") {
       if(!document.getElementById(checkObjs[i].name + "_check").checked) 
        {
         if (!oVal.maandjaar(checkObjs[i].value)){
            showFieldError(checkObjs[i].name,"Moet uit maand en jaartal bestaan (mm/jjjj)");
            addForgotten(checkObjs[i].title,"Moet uit maand en jaartal bestaan (mm/jjjj)");
            if (!checkObjs[i].disabled) { };
            validated = false;
        } 
      }  
    } else if (checkObjs[i].id == "verplicht_date_not_check") {
       if(!document.getElementById(checkObjs[i].name + "_check").checked) 
        {
         if (!oVal.datum(checkObjs[i].value)){
            showFieldError(checkObjs[i].name,"Ongeldige datum ingevoerd!");
            addForgotten(checkObjs[i].title,"Ongeldige datum ingevoerd!");  
            if (!checkObjs[i].disabled) { };
            validated = false;
        } 
      }
      
    } else if (checkObjs[i].id == "verplicht_pass"){
      if (!oVal.pass(checkObjs[i].value)){
        showFieldError(checkObjs[i].name,"Ongeldig wachtwoord ingevoerd!<br>Moet minimaal bestaan uit 6 karakters");    
        addForgotten(checkObjs[i].title,"Ongeldig wachtwoord ingevoerd!\nMoet minimaal bestaan uit 6 karakters");     
        validated = false;
      }
     } else if (checkObjs[i].id == "verplicht_gebruikersnaam"){
       if (!oVal.user(checkObjs[i].value)){
         showFieldError(checkObjs[i].name,"Geen of te korte gebruikersnaam ingevoerd!<br>Moet minimaal bestaan uit 4 karakters");    
         addForgotten(checkObjs[i].title,"Geen of te korte gebruikersnaam ingevoerd!\nMoet minimaal bestaan uit 4 karakters");              
         
         
        validated = false;
      }
    } else if (checkObjs[i].id == "verplicht_maandjaar"){
      if (!oVal.maandjaar(checkObjs[i].value)){
        showFieldError(checkObjs[i].name,"Moet uit maand en jaartal bestaan (mm/jjjj)");    
        addForgotten(checkObjs[i].title,"Moet uit maand en jaartal bestaan (mm/jjjj)");              
                
        validated = false;
      }
      
    } else if (checkObjs[i].id == "verplicht_postcodecijfers"){
      if (!oVal.postcodecijfers(checkObjs[i].value)){
        showFieldError(checkObjs[i].name,"Postcode moet uit 4 cijfers bestaan!");    
        addForgotten(checkObjs[i].title,"Postcode moet uit 4 cijfers bestaan!");              
        
        
        validated = false;
      }
    } else if (checkObjs[i].id == "optioneel_postcodeletters"){
      if (!checkObjs[i].value.length == 0 && !oVal.postcodeletters(checkObjs[i].value)){
        alert("Als gevuld, waarde moet uit twee letters bestaan!" + checkObjs[i].value);
                
        validated = false;
      } 
    } else if (checkObjs[i].id == "optioneel_postcode"){
      if (!checkObjs[i].value.length == 0 && !oVal.postcode(checkObjs[i].value)){
        alert("Ongeldige postcode ingevoerd!\n.");
        
        
        newClassName = checkObjs[i].className + ' error';
        checkObjs[i].className = newClassName;        
        validated = false;
      }                
    } else if (checkObjs[i].id == "verplicht_string"){
      if (!oVal.string(checkObjs[i].value)){       
        showFieldError(checkObjs[i].name,"Verplicht veld niet ingevuld!");    
        addForgotten(checkObjs[i].title,"Verplicht veld niet ingevuld!");  
        validated = false;
      }      
    } else if (checkObjs[i].id == "verplicht_email"){
      if (!oVal.email(checkObjs[i].value)){
        showFieldError(checkObjs[i].name,"Geen of ongeldig e-mailadres ingevoerd!");    
        addForgotten(checkObjs[i].title,"Geen of ongeldig e-mailadres ingevoerd!");       
        validated = false;
      }   
    } else if (checkObjs[i].id == "optioneel_email"){
      if (!checkObjs[i].value.length == 0 && !oVal.email(checkObjs[i].value)){
        alert("Ongeldig e-mailadres ingevoerd!");
        
        
         validated = 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.");
        
         validated = false;
      }         
    } else if (checkObjs[i].id == "verplicht_valuta"){
      if (!oVal.valuta(checkObjs[i].value)){
        alert("Ongeldig bedrag ingevoerd!");
        
        
         validated = false;
      }         
    } else if (checkObjs[i].id == "verplicht_postcode"){
      if (!oVal.postcode(checkObjs[i].value)){
        showFieldError(checkObjs[i].name,"Ongeldige postcode ingevoerd!");    
        addForgotten(checkObjs[i].title,"Ongeldige postcode ingevoerd!"); 
         validated = false;
      }         
    } else if (checkObjs[i].id == "verplicht_telefoon"){
      if (!oVal.telefoon(checkObjs[i].value)){
        showFieldError(checkObjs[i].name,"Telefoonnummer moet minimaal 10 karakters bevatten");    
        addForgotten(checkObjs[i].title,"Telefoonnummer moet minimaal 10 karakters bevatten");  
        validated = false;
      }  
    } else if (checkObjs[i].id == "optioneel_telefoon"){
      if (!checkObjs[i].value.length == 0 && !oVal.telefoon(checkObjs[i].value)){
        alert("Ongeldig telefoonnummer 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");
        
        
         validated = 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");
        
        
         validated = false;
      }               
    } else if (checkObjs[i].id == "verplicht_aantal"){
      if (!oVal.aantal(checkObjs[i].value)){
        alert("Ongeldige waarde ingevoerd!\nAlleen cijfers zijn toegestaan.");
        
        
        newClassName = checkObjs[i].className + ' error';
        checkObjs[i].className = newClassName;        
         validated = false;
      }         
    } else if (checkObjs[i].id == "optioneel_aantal"){
      if (!checkObjs[i].value.length == 0 && !oVal.aantal(checkObjs[i].value)){
        alert("Ongeldig aantal ingevoerd!");
        
        
        newClassName = checkObjs[i].className + ' error';
        checkObjs[i].className = newClassName;        
         validated = false;
      }         
    } else if (/^equals\[{1}[\w]*\]{1}$/.test(checkObjs[i].id)){
      compareName = checkObjs[i].id.substring(checkObjs[i].id.indexOf('[') + 1,checkObjs[i].id.indexOf(']'));
      if (checkObjs[i].value != document.getElementById(parentFormId).elements[compareName].value){
        newClassName = checkObjs[i].className + ' error';
        checkObjs[i].className = newClassName; 
        alert("De bevestiging wijkt af van het originele veld!");
        
             
         validated = false;
      }         
    } else if (checkObjs[i].id == "verplicht_radio"){
      
      radioOpts = document.getElementById(parentFormId).elements[checkObjs[i].name];
        hasChecked = false;
        for (j = 0; j < radioOpts.length && !hasChecked; j++){
            if (radioOpts[j].checked == true){
                hasChecked = true;
            }
        }
        if (!hasChecked){
            alert("Verplichte optiekeuze niet gemaakt!");
            newClassName = checkObjs[i].className + ' error';
            checkObjs[i].className = newClassName;        
            validated = false;
        }
    }
  } 
  return validated;
}

function trim(value) {
  value = value.replace(/^\s+/,''); 
  value = value.replace(/\s+$/,'');
  return value;
}

function validateField(checkObj)
{
    if (checkObj.type != 'select' && checkObj.type != 'file'){
      checkObj.value = trim(checkObj.value);
    }

    if (checkObj.id == "verplicht_string_not_check") {     
      if(!document.getElementById(checkObj.name + "_check").checked) 
      {
         if (!oVal.string(checkObj.value)){
            showFieldError(checkObj.name,"Verplicht veld niet ingevuld!");
            if (!checkObj.disabled) { };
            return false;
        } 
      }
      
    }
    
    if (checkObj.id == "verplicht_maandjaar_not_check") {     
      if(!document.getElementById(checkObj.name + "_check").checked) 
      {
         if (!oVal.maandjaar(checkObj.value)){
             if(checkObj.value != "") {
                showFieldError(checkObj.name,"Moet uit maand en jaartal bestaan (mm/jjjj)");           
                return false;
             }
        } 
      }
      
    }
    
    if (checkObj.id == "verplicht_datum") {
      if (!oVal.datum(checkObj.value)){
          
        if(checkObj.value != "") {
            showFieldError(checkObj.name,"Ongeldige datum ingevoerd!");              
            return false;
        }
      }
    } else if (checkObj.id == "verplicht_pass"){
      if (!oVal.pass(checkObj.value)){
        showFieldError(checkObj.name,"Ongeldig wachtwoord ingevoerd!\nMoet minimaal bestaan uit 6 karakters");
        
        
        return false;
      }
     } else if (checkObj.id == "verplicht_gebruikersnaam"){
       if (!oVal.user(checkObj.value)){
         showFieldError(checkObj.name,"Geen of te korte gebruikersnaam ingevoerd!\nMoet minimaal bestaan uit 4 karakters");
         
        return false;
      }
    } else if (checkObj.id == "verplicht_postcodecijfers"){
      if (!oVal.postcodecijfers(checkObj.value)){
        showFieldError(checkObj.name,"Postcode moet uit 4 cijfers bestaan!");
        return false;
      }
    } else if (checkObj.id == "optioneel_postcodeletters"){
      if (!checkObj.value.length == 0 && !oVal.postcodeletters(checkObj.value)){
        showFieldError(checkObj.name,"Als gevuld, waarde moet uit twee letters bestaan!" + checkObj.value);
        
        
        return false;
      } 
    } else if (checkObj.id == "optioneel_postcode"){
      if (!checkObj.value.length == 0 && !oVal.postcode(checkObj.value)){
        showFieldError(checkObj.name,"Ongeldige postcode ingevoerd!\n.");
        
              
        return false;
      } 
    } else if (checkObj.id == "verplicht_maandjaar"){
      if (!oVal.maandjaar(checkObj.value)){
          if(checkObj.value != "") {                    
                showFieldError(checkObj.name,"Moet uit maand en jaartal bestaan (mm/jjjj)");    
                addForgotten(checkObj.title,"Moet uit maand en jaartal bestaan (mm/jjjj)");              
                validated = false;
          }
      }               
    } else if (checkObj.id == "verplicht_string"){
      if (!oVal.string(checkObj.value)){
       // showFieldError(checkObj.name,"Verplicht veld niet ingevuld!");
        //if (!checkObj.disabled) { };
       // return false;
      }      
    } else if (checkObj.id == "verplicht_email"){
      if (!oVal.email(checkObj.value)){     
          if(checkObj.value != "") {  
                showFieldError(checkObj.name,"Geen of ongeldig e-mailadres ingevoerd!");                
                return false;
          }
      }   
    } else if (checkObj.id == "optioneel_email"){
      if (!checkObj.value.length == 0 && !oVal.email(checkObj.value)){
        if(checkObj.value != "" ) {
            showFieldError(checkObj.name,"Ongeldig e-mailadres ingevoerd!");             
            return false;
        }        
      }             
    } else if (checkObj.id == "verplicht_checked"){
      if (!oVal.checked(checkObj)){
        showFieldError(checkObj.name,"U dienst akkoord te gaan met de voorwaarden voordat u verder kunt gaan.");        
        return false;
      }         
    } else if (checkObj.id == "verplicht_valuta"){
      if (!oVal.valuta(checkObj.value)){
        showFieldError(checkObj.name,"Ongeldig bedrag ingevoerd!");              
        return false;
      }         
    } else if (checkObj.id == "verplicht_postcode"){
      if (!oVal.postcode(checkObj.value)){
          if(checkObj.value != "") {
            showFieldError(checkObj.name,"Ongeldige postcode ingevoerd!\n.");
            return false;
          }
      }         
    } else if (checkObj.id == "verplicht_telefoon"){
      if (!oVal.telefoon(checkObj.value)){          
          if(checkObj.value != "") {
            showFieldError(checkObj.name,"Telefoonnummer moet minimaal 10 karakters bevatten");        
          }
        return false;
      }                    
    } else if (checkObj.id == "verplicht_aantal"){
      if (!oVal.aantal(checkObj.value)){
        showFieldError(checkObj.name,"Ongeldige waarde ingevoerd!\nAlleen cijfers zijn toegestaan.");                      
        return false;
      }         
    } else if (checkObj.id == "optioneel_aantal"){
      if (!checkObj.value.length == 0 && !oVal.aantal(checkObj.value)){
        showFieldError(checkObj.name,"Ongeldig aantal ingevoerd!");                      
        return false;
      }         
    } else if (/^equals\[{1}[\w]*\]{1}$/.test(checkObj.id)){
      compareName = checkObj.id.substring(checkObj.id.indexOf('[') + 1,checkObj.id.indexOf(']'));
      if (checkObj.value != document.getElementById(parentFormId).elements[compareName].value){
        newClassName = checkObj.className + ' error';
        checkObj.className = newClassName; 
        alert("De bevestiging wijkt af van het originele veld!");                  
        return false;
      }         
    } else if (checkObj.id == "verplicht_radio"){      
      radioOpts = document.getElementById(parentFormId).elements[checkObj.name];
        hasChecked = false;
        for (j = 0; j < radioOpts.length && !hasChecked; j++){
            if (radioOpts[j].checked == true){
                hasChecked = true;
            }
        }
        if (!hasChecked){
            showFieldError(checkObj.name,"Verplichte optiekeuze niet gemaakt!");                             
            return false;
        }
    }
  
    removeChildElement(checkObj.name);
  return true;
}