/**
 * @author karremanb
 */

/*
 *** Validatie Settings ***
 */

/*** Verplichte Velden ***/
var verplicht = new Array();
verplicht[0] = 'voornaam';
verplicht[1] = 'achternaam';
verplicht[2] = 'email';

/*** Error melding  ***/
var errorMsg = 'U heeft niet alle verplichte velden ingevuld';



var xmlHttp;

function createXMLHttpRequest() {
	if(window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //IE6 
	}
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest(); // FF Safari IE7 enz. 
	}
}


function sendRequest(url) 
{
	createXMLHttpRequest();
	if(url == "" || url == null) {
		alert("No URL given, please check your sendRequest() statement");
	}
	var oInputs = '';
	var errorFields = '';
	
	$.each($(":input"), function() {
		for(i=0; i < verplicht.length; i++){
			if(verplicht[i] == this.name && this.value == ''){
				$(this).attr('class', 'input_error');				
				errorFields += this.name + ' ';
			}
		}

		if (this.type == 'checkbox') {
			if (this.checked) {
				oInputs += this.name + '=DoeMee&';
			}
		}else{
			if (this.value.length > 0) {
				oInputs += this.name + '=' + this.value + '&';
			}
		}
		
	});

	if (errorFields.length == 0) {
		// create the variablechain for use in our php-script
		xmlHttp.open("POST", url, true);
		xmlHttp.onreadystatechange = callBack;
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.send(oInputs);
	}else{
		$('#error_msg').html(errorMsg);
		window.scrollTo(0,0);
	}
	
}


	function callBack() {
		if(xmlHttp.readyState == 4) {
			if(xmlHttp.status == 200) {
				
				var response = xmlHttp.responseText;

				
				if (!response.match('MISSING-FIELD')) {
					document.location.href = 'dank.htm';
				}else{
					$('#error_msg').html(errorMsg);
					window.scrollTo(0,0);
				}
			}
			// if file was not found, return a 404 erro
			else if (xmlHttp.status == 404) {
				alert("Bestand niet gevonden");
			}
			else {
				alert("Er is iets fout gegaan, Server: ".xmlHttp.status);
			}
		}
	}