function ajaxRequest()
{
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Please update your browser to the latest version in order to use this website.");
				return false;
			}
		}
	}
	return ajaxRequest;
}

function inschrijving()
{
	var formDiv = document.getElementById('formulier').style;
	var resultDiv = document.getElementById('result');
	
	
	var xmlhttp = ajaxRequest();
	xmlhttp.open("POST", "inschrijving.php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	
	xmlhttp.onreadystatechange = function() 
	{	
		if(xmlhttp.readyState == 4)
		{
			if (xmlhttp.status == 200) 
			{
				//var uitkomst = xmlhttp.responseText;
				var xmlDoc=xmlhttp.responseXML;
				var resultaat = xmlDoc.getElementsByTagName("resultaat")[0].childNodes[0].nodeValue;
				var uitkomst = xmlDoc.getElementsByTagName("fouttekst")[0].childNodes[0].nodeValue;
				//resultDiv.innerHTML = uitkomst;
				
				if(resultaat == "goed")
				{
					resultDiv.innerHTML = uitkomst;
					formDiv.display = "none";
				}
				if(resultaat == "fout")
				{
					document.getElementById('result').style.background = "#95be60";
					resultDiv.innerHTML = uitkomst;

				}
			} else {
				alert('Unable to perform the action.');
			}
		}
	}
	
	var nieuwsbrief;
	if(document.getElementById('nieuwsbrief').checked == true)
	{
		nieuwsbrief = 1;
	} else {
		nieuwsbrief = 0;
	}
	
	
	var parameters = "actie=opslaan" +
	"&voornaam=" + encodeURI(document.getElementById('voornaam').value) +
	"&achternaam=" + encodeURI(document.getElementById('achternaam').value) +
	"&straatnaam=" + encodeURI(document.getElementById('straatnaam').value) +
	"&huisnummer=" + encodeURI(document.getElementById('huisnummer').value) +
	"&postcode=" + encodeURI(document.getElementById('postcode').value) +
	"&woonplaats=" + encodeURI(document.getElementById('woonplaats').value) +
	"&land=" + encodeURI(document.getElementById('land').value) +
	"&telefoonnummer=" + encodeURI(document.getElementById('telefoonnummer').value) +
	"&emailadres=" + encodeURI(document.getElementById('emailadres').value) +
	"&wachtwoord1=" + encodeURI(document.getElementById('wachtwoord1').value) +
	"&wachtwoord2=" + encodeURI(document.getElementById('wachtwoord2').value) +
	"&sessie=" + encodeURI(document.getElementById('sessie').value) +
	"&nieuwsbrief=" + nieuwsbrief;
	
	xmlhttp.send(parameters);
}

function artikeltoevoegen(artikelid)
{
	var aantal = document.getElementById('aantal'+artikelid).value;
	var directadd = document.getElementById('directadd').value;

	if(directadd == 1)
	{
		var addDiv = document.getElementById('addimage'+artikelid);
		var updateDiv = document.getElementById('updateimage'+artikelid);
		var failDiv = document.getElementById('failimage'+artikelid);	
	} else {
		var addDiv = document.getElementById('addimage');
		var updateDiv = document.getElementById('updateimage');	
		var failDiv = document.getElementById('failimage');	
	}
	addDiv.style.display = "none";
	updateDiv.style.display = "none";
	failDiv.style.display = "none";	
	
	var a = ajaxRequest();
	a.open("POST", "winkelwagen.php", true);
	a.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	
	a.onreadystatechange = function() 
	{	
		if(a.readyState == 4)
		{
			if (a.status == 200) 
			{
				var uitkomst = a.responseText;
			
				if(uitkomst == "toegevoegd")
				{
					addDiv.style.display = "inline";
				} else if (uitkomst == "aangepast") {
					updateDiv.style.display = "inline";
				} else {
				 	failDiv.style.display = "inline";
				}
			} else {
				alert('Unable to perform the action.');
			}
		}
	}
	var parameters = "action=artikeltoevoegen" +
	"&artikelid=" + artikelid +
	"&aantal=" + aantal;
	
	a.send(parameters);
}
function veranderaantal(artikelid)
{
	var nieuwaantal = document.getElementById('aantal'+artikelid).value;
	
	var a = ajaxRequest();
	a.open("POST", "winkelwagen.php", true);
	a.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	
	a.onreadystatechange = function() 
	{	
		if(a.readyState == 4)
		{
			if (a.status == 200) 
			{
				var uitkomst = a.responseText;
				//alert(uitkomst);
				//document.getElementById('changeDiv').innerHTML = uitkomst;
				redirectPage('winkelwagen.php');
			} else {
				alert('Unable to perform the action.');
			}
		}
	}
	var parameters = "action=aantalwijzigen" +
	"&artikelID=" + artikelid +
	"&aantal=" + nieuwaantal;
	
	a.send(parameters);
}
function redirectPage(pagina)
{
   //Time out om de foutmelding te kunnen tonen.
	setTimeout("procesRedirect('" + pagina + "')",0000);
}
function procesRedirect(pagina)
{
	window.location= pagina;
}
