﻿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) {
  try {
    if(obj.detachEvent) {
      obj.detachEvent( 'on'+type, obj[type+fn] );
      obj[type+fn] = null;
    } else {
     obj.removeEventListener(type, fn, false);
    }
  } catch(e) {}
}

function ScrollToElement(theElement){
  var selectedPosX = 0;
  var selectedPosY = 0;              
  while(theElement != null){
    selectedPosX += theElement.offsetLeft;
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }                        		      
  window.scrollTo(selectedPosX,selectedPosY);
}

function incorrect(control) {
  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(" formfield_correct\\b"), "");
    tmpclass = tmpclass.replace(new RegExp(" formfield_error\\b"), "");
    tmp.className = tmpclass + " formfield_error";
  }
}

function correct(control) {
  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 formtest(validationGroup) {
  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(isGetoond(document.getElementById(eval(Page_Validators[i].id).controltovalidate.toString()))){
     // alert(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().replace(' *','') + "</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) {
    foutmeldingen.style.display = 'block';
    ScrollToElement(foutmeldingen);
    foutmeldingen.innerHTML = "&Eacute;&eacute;n of meerdere velden zijn onjuist ingevuld. Vul het formulier aan."
  } else {
    foutmeldingen.style.display = 'none';
    //Page_Validators = new Array();
  }
  return Page_IsValid;
}

function isGetoond(el,retClassname) {  
  if(el.tagName) {
    if(el.tagName.toUpperCase() == 'BODY') {
      return true;
    }
  }
  if(el.style.display != 'none') {
    return isGetoond(el.parentNode,retClassname);
  } else {
    return false;
  }
}