// Destination
var ajaxdestination="";
var xmlhttp = "";

function getdata(what,where,bereken) { // get data from source (what)
	if (window.XMLHttpRequest) {
  		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
  		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}

 ajaxdestination=where;
 if(bereken == "true") {
 	xmlhttp.onreadystatechange = triggered2; // when request finished, call the function to put result to destination DIV
 } else {
	xmlhttp.onreadystatechange = triggered; // when request finished, call the function to put result to destination DIV
 }
 xmlhttp.open("GET", what);
 xmlhttp.send(null);
 return false;
}

function triggered() { // put data returned by requested URL to selected DIV
  if (xmlhttp.readyState == 4) if (xmlhttp.status == 200) 
    document.getElementById(ajaxdestination).innerHTML = xmlhttp.responseText;
}
function triggered2() { // put data returned by requested URL to selected DIV
  if (xmlhttp.readyState == 4) if (xmlhttp.status == 200) 
    document.getElementById(ajaxdestination).innerHTML = xmlhttp.responseText;
    bereken();
}

function bereken() {
	if (document.forms.formulier) {
		var aantal = document.forms.formulier.elements;
	} else {
		var aantal =0;
	}	
	var y, x, z
	var totaal = 0;
	var fout = false;
	for (i = 0; i < aantal.length; i++) {
		y = aantal[i].id
		if(y) {
			x = document.getElementById("totaal_" + y)     	
			z = document.getElementById("image_" + y)      				
			var aantalstuks = Math.round(aantal[i].value)
			var korting_vanaf = Math.round(product[y][2])
			if(isNaN(aantalstuks)) 
			{
				aantal[i].value = 0;
				o = document.getElementById("fout")
		 		o.innerHTML = "Vul alleen getallen in!"	
				alert("vul alleen getallen in")
			} else if (aantalstuks >= korting_vanaf)
			{    		
				var prijs = ((100 - product[y][1])/100) * (product[y][0] * aantalstuks);
				var a = new ToFmt(prijs);  				
				x.innerHTML = a.fmtF(8,2);
				totaal = totaal + prijs; 			
			} else {				
				var prijs =  product[y][0] * aantalstuks;		
				var a = new ToFmt(prijs);
				x.innerHTML = a.fmtF(8,2); 
				totaal = totaal + prijs; 
			}	
			var j = document.getElementById("totaalbedrag")
			var uit = new ToFmt(totaal);
			j.innerHTML = uit.fmtF(8,2); 			
		}
	}  
}
var Required = new Array("naam","adres","postcode","woonplaats","telefoonnummer","email")
function validate(){
	var Submit = true;
	for (i = 0; i < Required.length; i++) {
	 	var j = document.getElementById(Required[i])
		if(j.value.length < 2) {
	 		 var o = document.getElementById("fout_" + Required[i])
	 	 	 o.innerHTML = "&nbsp;(*)";
	 	 	 var p = document.getElementById("foutmelding")
	 	 	 p.innerHTML = "De velden met een (*) dienen ingevuld te worden."
	 		 Submit = false;
	 	} else  {
	 		 var o = document.getElementById("fout_" + Required[i])
	 	 	 o.innerHTML = ""; 	
	 	}
	 }
	 return Submit;
}

function bestel(id) {
	show('orderform');
	getdata("/bestel.php?a="+id,'orderform',"true");
	show('orderform');
}
function bestelmeer(thisid) {	
	var aantal = document.forms.formulier.elements;
	var y
	for (i = 0; i < aantal.length; i++) {
		y = aantal[i].id
		if(y == "item_"+thisid) {		
			var aantalstuks = Math.round(aantal[i].value)
			if(isNaN(aantalstuks)) 
			{
				aantal[i].value = 0;
				o = document.getElementById("fout")
		 		o.innerHTML = "Vul alleen getallen in!"	
				alert("vul alleen getallen in")
			} else {				
				show('orderform');
				getdata("/bestel.php?b="+thisid+"&c="+aantalstuks,'orderform',"true");
				show('orderform');
			}
		}
	}
}
function verwijder(id) {
	show('orderform');
	getdata("/bestel.php?x="+id,'orderform',"true");
	show('orderform');
}
function show(divID) {
	var swap = document.getElementById(divID)
	swap.style.display = "block"
}

function otherpages() {
	show('orderform');
	bereken();
	//	getdata('/bestel.php','orderform',"false");
}
