// form validation function //

	var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  var telRegex =  /^((\+|00)\d{2}(\s*|-)?0?|0)(\d\s*-?(\s*\d\s*){8}|\d{2}\s*-?(\s*\d\s*){7}|\d{3}\s*-?(\s*\d\s*){6}|\d{4}\s*-?(\s*\d\s*){5}|\d{5}\s*-?(\s*\d\s*){4})$/;
  var digits  = /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/;
	var  postRegex = /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/;


function validateContact() {
var ct = document.forms.form4;

  var voornaam			= ct.voornaam.value;
 	var naam 					= ct.naam.value;
  var adres 				= ct.adres.value;
  var postcode 			= ct.postcode.value;
	var plaats 				= ct.plaats.value;
	var telefoon		 	= ct.telefoon.value;
  var email 				= ct.email.value;
	
	var geslacht =document.form4.geslacht[1];

   if(voornaam == "") {
    inlineMsg('voornaam','Please fill out this field',2);
    return false;
  }
  
	if(naam == "") {
    inlineMsg('naam','Please fill out this field',2);
    return false;
  }
  
  
  if ( ( document.form4.geslacht[0].checked == false )
    && ( document.form4.geslacht[1].checked == false ) )
    {
       inlineMsg('geslacht','Please fill out this field.',2);
       return false;
    }

  
	 if(adres == "") {
    inlineMsg('adres','Please fill out this field',2);
    return false;
  }
  
	if(postcode == "") {
    inlineMsg('postcode','Please fill out this field',2);
    return false;
  }

  
  if(plaats == "") {
    inlineMsg('plaats','Please fill out this field',2);
    return false;
  }
   

  ct.submit();
}
   

function validate(form) {

  var voornaam			= form.voornaam.value;
 	var naam 					= form.naam.value;
  var adres 				= form.adres.value;
  var postcode 			= form.postcode.value;
	var plaats 				= form.plaats.value;
	var telefoonnummer 	= form.telefoonnummer.value;
  var email 				= form.email.value;
  var bestand 			= form.bestand.value;
	var geslacht = document.formulier.geslacht[1];


   if(voornaam == "") {
    inlineMsg('voornaam','Please fill out this field',2);
    return false;
  }
  
	if(naam == "") {
    inlineMsg('naam','Please fill out this field',2);
    return false;
  }
  
  if(email == "") {
    inlineMsg('email','Please fill out this field',2);
    return false;
  }
  
  if(!email.match(emailRegex)) {
    inlineMsg('email','This is not a valid e-mail adress',2);
    return false;
  }

  if(bestand == "") {
    inlineMsg('bestand','Please upload a file',2);
    return false;
  }


  return true;
}


function validateReview(form) {

  var voornaam			= form.voornaam.value;
 	var naam 					= form.naam.value;
  var email 				= form.email.value;
  var geslacht = document.formulier.geslacht[1];


   if(voornaam == "") {
    inlineMsg('voornaam','Please fill out this field',2);
    return false;
  }
  
	if(naam == "") {
    inlineMsg('naam','Please fill out this field',2);
    return false;
  }
  
  if(email == "") {
    inlineMsg('email','Please fill out this field',2);
    return false;
  }
  
  if(!email.match(emailRegex)) {
    inlineMsg('email','This is not a valid e-mail adress',2);
    return false;
  }


  return true;
}


function validateFrend(form) {
var d = document.forms.formulier;
  var uwnaam			= d.uwnaam.value;
 	var uwemail 		= d.uwemail.value;
  var naam 				= d.naam.value;
  var ontemail 		= d.ontemail.value;
  
   if(uwnaam == "") {
    inlineMsg('uwnaam','Please fill out this field',2);
    return false;
  }

  
    if(uwemail == "") {
    inlineMsg('uwemail','Please fill out this field',2);
    return false;
  }
  
  if(!uwemail.match(emailRegex)) {
    inlineMsg('uwemail','This is not a valid e-mail adress',2);
    return false;
  }
  
    
	if(naam == "") {
    inlineMsg('naam','Please fill out this field',2);
    return false;
  }
  
	 if(ontemail == "") {
    inlineMsg('ontemail','Please fill out this field',2);
    return false;
  }
  
  if(!ontemail.match(emailRegex)) {
    inlineMsg('ontemail','This is not a valid e-mail adress',2);
    return false;
  }
 
postTellafriend();
}

// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}