﻿var formtesttype = "normal";
var foutmeldingen = null;
function addEvent(obj, type, fn) {
  if(obj.attachEvent) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn](window.event);
  }
  obj.attachEvent('on'+type, obj[type+fn]);
  } else {
    obj.addEventListener(type, fn, false);
  }
}

function removeEvent(obj, type, fn) {
  if(obj.detachEvent) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else {
   obj.removeEventListener(type, fn, false);
  }
}

function incorrect(control) {
  if(!document.getElementById(control))
  {
    return;
  }
  if(formtesttype == "div") {
    var tmp = document.getElementById(control+"div")
    tmp.style.backgroundColor = "#ffffff";
    tmp.style.className = "checkfout";
  } else {
    var tmp = document.getElementById(control).parentNode
    var tmpclass = tmp.className
    tmpclass = tmpclass.replace(new RegExp(" ingevuld\\b"), "");
    tmpclass = tmpclass.replace(new RegExp(" fingevuld\\b"), "");
    tmp.className = tmpclass + " fingevuld";
  }
}

function correct(control) {
  if(!document.getElementById(control))
  {
    return;
  }
  if(formtesttype == "div") {
    var tmp = document.getElementById(control + "div")
    tmp.style.backgroundColor = '#ffffff';
    tmp.className = "check";
  } else {
    var tmp = document.getElementById(control).parentNode
    var tmpclass = tmp.className
    tmpclass = tmpclass.replace(new RegExp(" ingevuld\\b"), "");
    tmpclass = tmpclass.replace(new RegExp(" fingevuld\\b"), "");
    tmp.className = tmpclass + " ingevuld";
  }
}

function isGetoond(el)
{
  //document.getElementById("debug").innerHTML = document.getElementById("debug").innerHTML + '<br />' + el.id;
  var classn = '';
  if(!el)
  {
    return true;
  }
  if(el.className)
  {
    classn = el.className;
  }
  if(classn.indexOf("nietzichtbaar") > -1)
  {
    return false;
  }else if(classn.indexOf("zichtbaar") > -1) 
  {
    return true;
  }else if(el.id == 'formulier')
  {
    return true;
  }else
  {
    return isGetoond(el.parentNode);
  }
}

function formtest(validationGroup,niettonen) {
  if (typeof(Page_Validators) == "undefined") {
    return true;
  }
  if(validationGroup == "") {
    validationGroup = "formulier";
  }
  var i;
  var prevField = "";
  var prevValid = true;

  var blnCorrect = true;
  foutmeldingen = document.getElementById("foutmeldingen");
  foutmeldingen.innerHTML = "";
    
  for (i = 0; i < Page_Validators.length; i++) {
    //if(isDisplay(document.getElementById(eval(Page_Validators[i].id).controltovalidate.toString()))){
    if(isGetoond(document.getElementById(eval(Page_Validators[i].id).controltovalidate.toString()))){
        ValidatorValidate(Page_Validators[i], validationGroup, null);
        if(!eval(Page_Validators[i].id).isvalid) {
          incorrect(eval(Page_Validators[i].id).controltovalidate.toString());
          if (prevField != eval(Page_Validators[i].id).controltovalidate.toString() || prevValid) {
            foutmeldingen.innerHTML += "<li>" + eval(Page_Validators[i].id).errormessage.toString() + "</li>";
            blnCorrect=false;
          }
        }else if (eval(Page_Validators[i].id).controltovalidate.toString() != prevField) {
          correct(eval(Page_Validators[i].id).controltovalidate.toString());
        }
        
        prevField = eval(Page_Validators[i].id).controltovalidate.toString();
        prevValid = eval(Page_Validators[i].id).isvalid;
     } 
  }
  
  ValidatorUpdateIsValid();
  ValidationSummaryOnSubmit(validationGroup);
  Page_IsValid=blnCorrect;
  Page_BlockSubmit = !Page_IsValid;

  if (!Page_IsValid && niettonen != true) {
    foutmeldingen.style.display = 'block';
    document.location.hash = 'foutmeldingen';
  } else {
    foutmeldingen.style.display = 'none';
  }
  if($("#ctl00_ContentPlaceHolder1_wucInhoud_leeftijdtoestel_1:checked").length > 0 && (($("#ctl00_ContentPlaceHolder1_wucInhoud_installatiedatum").val() == "") ||       $("#ctl00_ContentPlaceHolder1_wucInhoud_installatiedatum").val().search("^(((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}))|(29\\-02\\-((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$") < 0          ))
  {
    foutmeldingen.innerHTML += "<li>Installatiedatum is niet correct ingevuld (dd-MM-jjjj)</li>";
    foutmeldingen.style.display = 'block';
    document.location = "#foutmeldingen";
    Page_IsValid = false;
  }
  return Page_IsValid;
}