function findPos(){
  if(bw.ns4){   //Netscape 4
    x = document.layers.layerMenu.pageX
    y = document.layers.layerMenu.pageY
  }else{ //other browsers
    x=0; y=0; var el,temp
    el = bw.ie4?document.all["divMenu"]:document.getElementById("divMenu");
    if(el.offsetParent){
      temp = el
      while(temp.offsetParent){ //Looping parent elements to get the offset of them as well
        temp=temp.offsetParent; 
        x+=temp.offsetLeft
        y+=temp.offsetTop;
      }
    }
    x+=el.offsetLeft
    y+=el.offsetTop
  }
  //Returning the x and y as an array
  return [x,y]
}

function emptystr(string) {
	var x = 0;
	var chr;
	var empty = true;
	if (string.length) {
		do {
			chr = string.substring(x, x + 1);
			x++;
			empty = (chr == ' ' || chr == '\t' || chr == '\n' || chr == '\r');
		} while (empty && x < string.length);
	}
	return empty;
}

function checkNumber(item, name){
  if (isNaN(document.form_cms.elements[item].value)){
	  alert(name + ' moet een nummer zijn!');
    document.form_cms.elements[item].focus();
    return false;
  }
  else{
    return true;
  }
}

function checkLength(form, item, name, length){
  if(document.forms[form].elements[item].value.length < length){
    alert('Het veld ' + name + ' moet minimaal ' + length + ' karakters bevatten!');
    document.forms[form].elements[item].focus();
    return false;
  }
  else
    return true;
}

function checkEmpty(item, name){
  if (emptystr(document.form_cms.elements[item].value)){
    alert('U bent ' + name + ' vergeten in te voeren!');
    document.form_cms.elements[item].focus();
    return false;
  }
  else
    return true;
}

function checkEmail(item){
  var pattern=/(^[\-_\.a-zA-Z0-9]+)@((([0-9]{1,3}\.){3}([0-9]{1,3})((:[0-9])*))|(([a-zA-Z0-9\-]+)(\.[a-zA-Z]{2,})+(\.[a-zA-Z]{2})?((:[0-9])*)))/;
  
  if (document.form_cms.elements[item].value.search(pattern) == -1){
    alert('Het e-mailadres is onjuist!');
    document.form_cms.elements[item].focus();
    return false;
  }
  else{
    return true;
  }
}

function checkRadio(item){
  for(var i = 0; i < document.form_cms.elements[item].length; i++){
    if(document.form_cms.elements[item][i].checked == true){
      return true;
    }
  }
  alert('U moet een waarde selecteren!');
  return false;
}

function checkPoll(item){
  for(var i = 0; i < document.form_poll.elements[item].length; i++){
    if(document.form_poll.elements[item][i].checked == true){
      return true;
    }
  }
  alert('U moet een waarde selecteren!');
  return false;
}

function checkPassword(item1, item2){
  if (document.form_cms.elements[item1].value != document.form_cms.elements[item2].value){
    alert('Het wachtwoord en de bevestiging van het wachtwoord komen niet overeen!');
    document.form_cms.elements[item1].focus();
    return false;
  }
  else{
    return true;
  }
}

function checkEmptyCheckbox(item, name) {
  var chkd = 0;
  var setGroup = false;

  for (var i = 0; i < document.form_cms.elements.length; i++) {
    if(document.form_cms.elements[i].type == "checkbox"){
      var obj = document.form_cms.elements[i];
      if (obj.name == item && obj.checked) {
        chkd = 1;
        break;
      }
      setGroup = true;
    }
  }

  if (chkd != 1 && setGroup) {
    alert(name);
    return false;
  }
  else {
    return true;
  }
}

// Check function for form module 
function form_checkField(type, name, label) {
  switch (type) {
    case 'textfield':
    case 'password':
    case 'textarea':
      return checkEmpty(name, label);
      break;
    case 'radio':
    case 'checkbox':
      var sel = false;
      var frm = document.forms['form_cms'];
      for (var i = 0; i < frm.elements.length && !sel; i++)
        sel = (frm.elements[i].name == name && frm.elements[i].checked);

      if (sel)
        return true;
    	else {
        alert(label + " is een verplicht veld!");
        return false;
      }
      break;
    case 'select':
      if (document.forms['form_cms'].elements[name].selectedIndex == -1) {
        alert(label + " is een verplicht veld!");
        return false;
      }
      return true;
      break;
  }
  return false;
}

function form_validateForm() {
  var ret = true;

  for (var i = 0; i < form_formChecks.length && ret; i++)
    ret = form_checkField(form_formChecks[i][0], form_formChecks[i][1], form_formChecks[i][2]);
  
  return ret;
}

function checkForm(moduleName, formName, action) {
  switch (moduleName) {
    case 'forum':
      switch (formName) {
        case 'subject':
          if (action == 'add')
            return (checkEmpty('subject', 'het onderwerp') && checkEmpty('user', 'de naam') && checkEmail('email') && checkEmpty('content', 'het bericht')); 
          else
            return false;
          break;
        case 'post':
          if (action == 'add')
            return (checkEmpty('user', 'de naam') && checkEmail('email') && checkEmpty('content', 'het bericht')); 
          else 
            return false;
          break;
      }
      break;
    case 'newsletter':
      switch (formName) {
        case 'member':
          if (action == 'add')
            return (checkEmpty('lastname', 'de achternaam') && checkEmpty('firstname', 'de voornaam') && checkEmail('email') && checkEmptyCheckbox('group[]', 'Selecteer minimaal 1 groep!'));
          else if (action == 'remove')
            return checkEmail('email');
          else
            return false;
      }
      break;
    case 'job':
      switch (formName) {
        case 'reaction':
          if (action == 'send')
            return (checkEmpty('firstname', 'de voornaam') && checkEmpty('lastname', 'de achternaam')  && checkEmpty('address', 'het adres')  && checkEmpty('zipcode', 'de postcode')  && checkEmpty('city', 'de woonplaats') && checkEmail('email')  && checkEmpty('motivation', 'de motivatie'));
          else
            return false;
          break;
        case 'member':
          if (action == 'login')
            return (checkEmpty('username', 'de gebruikersnaam') && checkEmpty('password', 'het wachtwoord'));
          else if (action == 'add')
            return (checkEmpty('username', 'de gebruikersnaam') && checkEmpty('password', 'het wachtwoord') && checkPassword('password', 'password_check') && checkEmpty('name', 'de naam') && checkEmpty('email', 'het e-mailadres') && checkEmail('email') && checkEmptyCheckbox('jobcategories[]', 'Selecteer minimaal 1 rubriek!'));
          else if (action == 'change')
            return (checkPassword('password', 'password_check') && checkEmpty('name', 'de naam') && checkEmpty('email', 'het e-mailadres') && checkEmail('email') && checkEmptyCheckbox('jobcategories[]', 'Selecteer minimaal 1 rubriek!'));
          else if (action == 'remove')
            return (checkEmpty('password', 'het wachtwoord') && confirm('Weet u zeker dat u uw registratie wil verwijderen?'));
          break;
      }
      break;
    case 'search':
      if (action == 'search')
        return (checkLength(formName, 'search', 'zoekopdracht', 4));
      else
        return false;
    break;
    case 'poll':

      if (action == 'vote')
        return (checkPoll('answer'));
      else
        return false;
      break;
    case 'medium':
      if (formName == 'comment' && action == 'add') {
        return (checkEmpty('user', 'de naam') && checkEmpty('email', 'het e-mailadres') && checkEmail('email') && checkEmpty('content', 'de tekst'));
      }
      else
        return false;
      break;
  }
  return false;
}

function windowOpen(name, url, height, width) {
  var l = (screen.width) ? (screen.width - width) / 2 : 0;
  var t = (screen.height) ? (screen.height - height) / 2 : 0;
  var opts = 'width=' + width + ',height=' + height + ',top=' + t + ',left=' + l + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes';

  var win = window.open(url,name, opts);
  win.focus();
}

function showStatus(name){
  window.status=name;
  return true;
}

function swapImage(item,image) {
  document.images[item].src = image;
}

function getElementPos(el) {
  var x = 0, y = 0, temp;
  if (el.offsetParent) {
    temp = el;
    while (temp.offsetParent) { //Looping parent elements to get the offset of them as well
      temp = temp.offsetParent; 
      x += temp.offsetLeft;
      y += temp.offsetTop;
    }
  }
  x += el.offsetLeft;
  y += el.offsetTop;
  //Returning the x and y as an array
  return [x,y];
}

function setBannerDivs() {
  var coll = document.getElementsByTagName('EMBED');
  var pos;

  for (var i = 0; i < coll.length; i++) {
    if (coll[i].className == 'banner') {
      pos = getElementPos(coll[i]);
      coll[i].parentNode.previousSibling.style.left = pos[0] + 'px';
      coll[i].parentNode.previousSibling.style.top = pos[1] + 'px';
    }
  }
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function showMoviePopup() {
  var width = 315, height = 235;
  var l = (screen.width) ? (screen.width - width) / 2 : 0;
  var t = (screen.height) ? (screen.height - height) / 2 : 0;
  var opts = 'width=' + width + ',height=' + height + ',top=' + t + ',left=' + l + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no';
  var url = location.href + '&movie_popup=1';  
  
  var win = window.open(url, name, opts);
  win.focus();
}

function getWindowInnerSize() {
  var bWidth, bHeight;
  
  if (typeof window.innerWidth == 'number') {
    bWidth = window.innerWidth;
    bHeight = window.innerHeight;
  }
  else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    bWidth = document.documentElement.clientWidth;
    bHeight = document.documentElement.clientHeight;
  }
  else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
    bWidth = document.body.clientWidth;
    bHeight = document.body.clientHeight;
  }
  
  return [bWidth, bHeight];
}
