﻿function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}
 
function GETRequest(query) {
    createXMLHttpRequest();
          
    var queryString = "inc/aanmelden.php?" + query;
    
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.open("GET", queryString, true);
    xmlHttp.send(null);
}
function POSTRequest(query, url) {


  
    createXMLHttpRequest();
    //var url = "inc/aanmelden.php"; 
    xmlHttp.open("POST", url, true);
    xmlHttp.onreadystatechange = handleStateChange;

	
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
	
    xmlHttp.send(query);
}
    
function handleStateChange() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
            parseResults();
        }
    }
}

function parseResults() {
	 $('gegevens').innerHTML = xmlHttp.responseText;
}
function emailCheck(adress){
  
  if(adress.indexOf('@') > 0 && adress.indexOf('.') > 0){
    return true;
  }
  else{
    return false;
  }
}

function modelAanmelden(){


var post = true;
  if($('inputvNaam').value.length == 0){
    $('titlevNaam').style.color = '#ff0000';
	post = false;
  }
  else{
    $('titlevNaam').style.color = '#000000';
	if(post){post = true};
  }
  if($('inputaNaam').value.length == 0){
    $('titleaNaam').style.color = '#ff0000';
	post = false;
  }
  else{
    $('titleaNaam').style.color = '#000000';
	if(post){post = true};
  }
  
  if($('inputEmail').value.length == 0 || !emailCheck($('inputEmail').value)){
    $('titleEmail').style.color = '#ff0000';
	post = false;
  }
  else{
    $('titleEmail').style.color = '#000000';
	if(post){post = true};
  }
  if($('inputAdres').value.length == 0){
    $('titleAdres').style.color = '#ff0000';
	post = false;
  }
  else{
    $('titleAdres').style.color = '#000000';
	if(post){post = true};
  }
  if($('inputPostcode').value.length == 0 || $('inputWoonplaats').value.length == 0){
    $('titlePostcode').style.color = '#ff0000';
	post = false;
  }
  else{
    $('titlePostcode').style.color = '#999999';
	if(post){post = true};
  }

  if($('inputWachtwoord').value.length == 0 || $('inputWachtwoord2').value.length == 0){
    $('titleWachtwoord').style.color = '#ff0000';
	$('titleWachtwoord2').style.color = '#ff0000';
	post = false;
  }
  else{
    $('titleWachtwoord').style.color = '#000000';
	$('titleWachtwoord2').style.color = '#000000';
	
    if($('inputWachtwoord').value != $('inputWachtwoord2').value){
      $('wwerr').innerHTML = "De wachtwoorden komen niet overeen.";
	  post = false;
    }
    else{
      $('wwerr').innerHTML = "";
	  if(post){post = true};
    } 
  }
  if($('hiddenFoto').value == 0){
    $('fotoerr').innerHTML = "Vergeet niet je foto te uploaden";
	post = false;
  }
  else{
    $('fotoerr').innerHTML = "";
	if(post){post = true};
  }
  
  if(post){
    POSTRequest($('modelAanmelden').serialize(), "inc/aanmelden.php");
    $('gegevens').innerHTML = "<img src='spinner.gif'> Bezig met aanmelden";
  }
}
function berichtVersturen(){
  var post = true;
  if($('inputOnderwerp').value.length == 0){
    $('titleOnderwerp').style.color = '#ff0000';
    post = false;
  }
  else{
    $('titleOnderwerp').style.color = '#000000';
    if(post){post=true};
  } 
  if($('inputBericht').value.length < 10){
    $('titleBericht').style.color = '#ff0000';
    post = false;
  }
  else{
    $('titleBericht').style.color = '#000000';
    if(post){post=true};
  }
  
  if(post){
    $('bericht').submit();
  }
}
function bedrijfAanmelden(){
  var post = true;
  if($('inputBedrijf').value.length == 0){
    $('titleBedrijf').style.color = '#ff0000';
	post = false;
  }
  else{
    $('titleBedrijf').style.color = '#000000';
	if(post){post = true};
  }
  if($('inputKVK').value.length == 0){
    $('titleKVK').style.color = '#ff0000';
	post = false;
  }
  else{
    $('titleKVK').style.color = '#000000';
	if(post){post = true};
  }
  if($('inputContactpersoon').value.length == 0){
    $('titleContactpersoon').style.color = '#ff0000';
	post = false;
  }
  else{
    $('titleContactpersoon').style.color = '#000000';
	if(post){post = true};
  }
  if($('inputAdres').value.length == 0 || $('inputPostcode').value.length == 0 || $('inputWoonplaats').value.length == 0){
    $('titleAdres').style.color = '#ff0000';
	post = false;
  }
  else{
    $('titleAdres').style.color = '#000000';
	if(post){post = true};
  }
  if($('inputTelefoon').value.length == 0){
    $('titleTelefoon').style.color = '#ff0000';
	post = false;
  }
  else{
    $('titleTelefoon').style.color = '#000000';
	if(post){post = true};
  }
    if($('inputEmail').value.length == 0 || !emailCheck($('inputEmail').value)){
    $('titleEmail').style.color = '#ff0000';
	post = false;
  }
  else{
    $('titleEmail').style.color = '#000000';
	if(post){post = true};
  }
    if($('inputWachtwoord').value.length == 0 || $('inputWachtwoord2').value.length == 0){
    $('titleWachtwoord').style.color = '#ff0000';
	$('titleWachtwoord2').style.color = '#ff0000';
	post = false;
  }
  else{
    $('titleWachtwoord').style.color = '#000000';
	$('titleWachtwoord2').style.color = '#000000';
	
    if($('inputWachtwoord').value != $('inputWachtwoord2').value){
      $('wwerr').innerHTML = "De wachtwoorden komen niet overeen.";
	  post = false;
    }
    else{
      $('wwerr').innerHTML = "";
	  if(post){post = true};
    } 
  }
  
  if(post){
    POSTRequest($('bedrijfAanmelden').serialize(), "inc/aanmelden.php");
    $('gegevens').innerHTML = "<img src='spinner.gif'> Bezig met aanmelden";
  }
}
function uploadProgress(elm, status, bericht){
  if(status == 'enable'){
    $(elm).innerHTML = bericht;
	$('foto').disabled = false;
  }
  else if(status == 'disable'){
     $(elm).innerHTML = "";
  }
}
function loadImage(elm,img){
  $(elm).src = img;
}

function toonFoto(file){
  document.getElementById('grotefotomodel').style.backgroundImage = "url('./img/foto/"+file+".jpg')";
}
