String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
var buitenland = false;

function selOn(ctrl) {
  ctrl.style.backgroundColor = '#000087';
  ctrl.style.color = '#FFFFFF';
}
function selOff(ctrl) {
  ctrl.style.backgroundColor = '';
  ctrl.style.color = '';
}

function setMenukeuze(id){
  document.getElementById('menuKeuze').value=id;
  document.forms['menu'].submit();
}

function checkAlgemeen(veldnaam, fouttag,melding,verplicht)
{
  fout = false;
  veld = document.getElementById(veldnaam);
  waarde = '' + veld.value;
  waarde = waarde.trim();
  veld.value = waarde;
  fout = (verplicht && (waarde.length ==0));
  elemFout = document.getElementById(fouttag);
  elemFout.innerHTML = (fout?melding:"");
  return !fout;
}
/*
    check of de postcode klop,  leeg veld is toegstaan
*/
function checkPostcode(postcode, fouttag,verplicht){
  fout = false;
  waarde = document.getElementById(postcode);
  if (!buitenland && (waarde.value.length > 0 )) {
    if(!/(^[1-9][0-9]{3} ?[a-zA-Z]{2}$)/.test(waarde.value.toUpperCase())){
      fout = true;
    }
  } else if (verplicht) {
    fout=true;
  }
  elemFout = document.getElementById(fouttag);
  if (elemFout != null) {
    elemFout.innerHTML = (fout?"Verkeerde postcode":"");
  }
  return !fout;
}
function checkNumber(veld, fouttag,melding,verplicht){
  fout = false;
  veldje = document.getElementById(veld);
  //waarde = (''+veldje.value).replace(/[^0-9]/g,'');
  waarde = ''+veldje.value;
  waarde = waarde.trim();
  veldje.value = waarde;
  
  if (verplicht && (waarde.length ==0))   { fout=true; melding="Verplicht invullen";}
  if (!fout && !/(^[1-9]*$)/.test(waarde)){ fout=true; melding="Dit is geen geheel getal";}
  if (!fout && (waarde.length > 2))       { fout=true; melding="Maximaal 99 personen per inschrijving";}

  elemFout = document.getElementById(fouttag);
  if (elemFout != null) {
    elemFout.innerHTML = (fout?melding:"");
  }
  return !fout;
}
function checkTelefoon(veld, fouttag,melding,verplicht){
  fout = false;
  veldje = document.getElementById(veld);
  if (buitenland) {
    strTelNr=''+veldje.value;
    strTelNr = strTelNr.trim();
    if (verplicht && (strTelNr.length == 0 )) { fout = true; melding = "Verplicht invullen";}
  } else {
    strTelNr=(''+veldje.value).replace(/[^0-9]/g,'');
    if (strTelNr.length > 0 ) {
      if(!(/^0/.test(strTelNr))){
        fout = true;
      }
      if (!fout && !(/^0[0-9]+/.test(strTelNr))){
        fout = true;    
      }
    } else if (verplicht) {
      fout = true; melding = "Verplicht invullen";
    }
  }
  if (!fout) veldje.value=strTelNr;
  elemFout = document.getElementById(fouttag);
  if (elemFout != null) {
    elemFout.innerHTML = (fout?melding:"");
  }
  return !fout;
}

/*
    check of de radiobuttons wel gebruikt zijn, zoniet dan een foutmelding
*/
function checkRadio(radio,fouttag){
  fout=true;
  radios = document.getElementsByName(radio);
  if (radios != null) {
    for(i = 0; i < radios.length; i++) {
      if (radios[i].checked) { fout = false; }
    }
  }
  elemFout = document.getElementById(fouttag);
  if (elemFout != null) {
    elemFout.innerHTML = (fout?"Verplicht invullen":"");
  }
  return !fout;
}

function checkEmail(emailString){
  //Advanced Email Check credit-
  //By JavaScript Kit (http://www.javascriptkit.com)
  //Over 200+ free scripts here!

  elem = document.getElementById("emailFout");
  fout = false;
  if (emailString) {
    emailString = emailString.trim();
    filter=/^([\w-]+(\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i ;
    if (!filter.test(emailString)) {
    elem.innerHTML = "Ongeldig e-mail adres";
      fout= true;
    } else {
      elem.innerHTML ="";
      
    }
  } else {
    elem.innerHTML ="Verplicht invullen";
    fout= true;
  }
  return !fout;
}
/*
Eeerst ingevoerde waardes controleren daarna opslaan gegevens
*/
function wsrSubmit()
{
  // een gevulde array betekent data
  if (document.getElementsByName('land').length>0) {
    landen = document.getElementsByName('land');
    buitenland = landen[0].value != 'Nederland';
  } else { 
    document.getElementById('send').disabled=true;
    return false; 
  }
  emails = document.getElementsByName('email');
  goOn= checkAlgemeen('voornaam','voornaamFout',"Dit veld is verplicht",true);
  if (goOn) { goOn = checkAlgemeen('achternaam','achternaamFout',"Dit veld is verplicht",true);}
  if (goOn ) {goOn = checkEmail(emails[0].value);}
  if (goOn) { goOn = checkPostcode('postcode','postcodeFout',false);}
  if (goOn) { goOn = checkTelefoon('telefoon','telefoonFout',"Maximaal 15 karakters",false); }
  if (goOn) { goOn = checkNumber('personen','personenFout',"Dit is geen cijfer",true);}
  if (goOn) { goOn = checkRadio('trailer','trailerFout');}
  if (goOn) {
    document.getElementById('action').value = "save";
    document.forms['inschrijven'].submit(); 
  } else {
    return false;
  }
}
/*
Zorgt ervoor dat de sessie gevens worden weggehaald 
*/
function resetFormulier(){
  document.getElementById('action').value = "reset";
  document.getElementById('send').disabled=false;  
  document.forms['inschrijven'].submit();
}




/**
 * Create a fader that will progressively change an element's background color from
 * a given color back to its original color.
 *
 *param elt the element to fade
 *param color the starting color (default yellow)
 *param duration the fade duration in msecs (default 1000)
 *param fps the animation frames per seconds (default 25)
 */
function Fader(elt, color, duration, fps) {
   // Set default values
   if (!color) color = "#FFFF80"; // yellow
   if (!duration) duration = 1000; // 1 sec
   if (!fps) fps = 25; // 25 frames/sec
   
   this.element = elt;
   this.fromColor = Fader.colorToRgb(color);
   this.toColor = Fader.colorToRgb(Fader.getBgColor(this.element));
   
   this.maxFrames = Math.round(fps * duration / 1000.0);
   this.delay = duration / this.maxFrames;
}
/** Converts a "#RRGGBB" color as an array of 3 ints */
Fader.colorToRgb = function(hex) {
  return [
      parseInt(hex.substr(1,2),16),
      parseInt(hex.substr(3,2),16),
      parseInt(hex.substr(5,2),16) ];
}

/** Converts rgb values to a "#RRGGBB" color */
Fader.rgbToColor = function(r, g, b) {
  r = r.toString(16); if (r.length == 1) r = '0' + r;
  g = g.toString(16); if (g.length == 1) g = '0' + g;
  b = b.toString(16); if (b.length == 1) b = '0' + b;
  return "#" + r + g + b;
}



/** Get the background color of an element */
Fader.getBgColor = function(elt) {
  while(elt) {
      var c;
      if (window.getComputedStyle) c = window.getComputedStyle(elt,null).getPropertyValue("background-color");
      if (elt.currentStyle) c = elt.currentStyle.backgroundColor;
      if ((c != "" && c != "transparent") || elt.tagName == "BODY") { break; }
      elt = elt.parentNode;
  }
  if (c == undefined || c == "" || c == "transparent" || c == "white") c = "#FFFFFF";

  var rgb = c.match(/rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/);
  if (rgb) return this.rgbToColor(parseInt(rgb[1]),parseInt(rgb[2]),parseInt(rgb[3]));
  return c;
}


Fader.prototype.changingColor = function() {
  if (this.frame < this.maxFrames) {
      // Schedule the next iteration right now to keep a more accurate timing
      var fader = this;
      setTimeout( function() {fader.changingColor();}, this.delay);
  }
  var newColor = new Array(3);
  if (this.flip) {
    for (var channel = 0; channel < 3; channel++) {
        newColor[channel] = Math.floor(
            this.fromColor[channel] * ((this.maxFrames - this.frame) / this.maxFrames) +
            this.toColor[channel] * (this.frame/this.maxFrames)
        );
    }
  } else {
    for (var channel = 0; channel < 3; channel++) {
        newColor[channel] = Math.floor(
            this.toColor[channel] * ((this.maxFrames - this.frame) / this.maxFrames) +
            this.fromColor[channel] * (this.frame/this.maxFrames) 
        );
    }
  }
  this.frame++;
  if (this.frame == this.maxFrames) {
    this.fading++;
    if (this.fading <= this.maxFading) {
      this.frame=0;
      this.flip = !this.flip;
    } else { 
      flip=true; 
    }
  }
  var color = Fader.rgbToColor(newColor[0], newColor[1], newColor[2]);
  this.element.style.backgroundColor = color;
  return newColor;
}

Fader.prototype.start = function() {
 this.frame = 0;
 this.flip = true;
 this.maxFading = 4;
 this.fading = 0;
 this.changingColor();
}

/**
 * Creates a default fader for a given element. This function can be used to set BrowserUpdate.highlight
 */
Fader.fade = function(elt) {
 new Fader(elt).start();
}

