
var oudid = 0;

window.onload=function()
{
    var p=document.getElementsByTagName('table')[0].getElementsByTagName('tr');
    for(var i=0; i < p.length; i++)
    {
    	if(p[i].getAttribute('class')=='info'){
        p[i-1].setAttribute('id',i);
        p[i].setAttribute('id',i+1);

        p[i-1].onclick=function()
        {
        		if(oudid != 0){
        			document.getElementById(oudid).style.display = 'none';
				document.getElementById(oudid-1).bgColor = '#FFFFFF';
        		}
			document.getElementById(this.id).bgColor = '#F0F0F0';
			oud = document.getElementById(parseInt(this.id)+1).style.display;
            document.getElementById(parseInt(this.id)+1).style.display = 'table-row';
            oudid= parseInt(this.id)+1;

        }

        }
    }
}

var xmlhttp = null;
     
if(window.XMLHttpRequest){
	xmlhttp = new XMLHttpRequest();
}else{
	try{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}catch(e){
		window.alert("Uw browser ondersteunt het XMLHttpRequest object niet!");
	}
}
     
function bestellen(artikelid){
	xmlhttp.open("GET", "inhoud/mandje.php?id=" + artikelid, true);
	xmlhttp.onreadystatechange = vraagBestellen;
	xmlhttp.send('');
}

function gaVerwijderen(artikelid){
	xmlhttp.open("GET", "inhoud/mandje.php?action=delete&id=" + artikelid, true);
	xmlhttp.onreadystatechange = geefreactie;
	xmlhttp.send('');
}

function vraagBestellen(){
	if(xmlhttp.readyState == 4){
		if(confirm(xmlhttp.responseText)){
			document.location = 'index.php?inhoud=bestellen';
		}else{
			return false;
			
		}
	}
}

function geefreactie(){
	if(xmlhttp.readyState == 4){

	}
}

function verwijderen(artikelId){
	gaVerwijderen(artikelId);
	verwijderrij = document.getElementById(artikelId);
	document.getElementById('bestellen').removeChild(verwijderrij);
	berekenPrijs();
}
function berekenPrijs(){
	var rijen = document.getElementById('bestellingen').getElementsByTagName('tr');
	var totaalprijs = 0;
	var totaalaantal = 0;
	
	for(i=1;i<rijen.length-2;i++){
		cellen = rijen[i].getElementsByTagName('td');
		aantal = 0;
		if(cellen.length > 3){
			aantal = parseFloat(cellen[2].getElementsByTagName('select')[0].value);
			prijs = parseFloat(cellen[3].innerHTML);
			
			if(aantal == 0){
				rijen[i].style.display = 'none';
			}
			
			totaalaantal += parseFloat(aantal);
			totaalprijs += (parseFloat(aantal)*parseFloat(prijs));

		}
	}
	
	if(totaalaantal > 0){
		document.getElementById('totaalprijs').innerHTML = totaalprijs.toFixed(2);
	}else{
		document.getElementById('bestellingen').style.display = 'none';
		document.getElementById('infobericht').innerHTML = 'Er zijn nog geen artikelen aan uw bestelling toegevoegd. Klik <a href="index.php?inhoud=winkel">hier</a> om naar de winkel te gaan.';
	}
	
	
}

function setQuantity(artikelid,quantity){
	xmlhttp.open("GET", "inhoud/mandje.php?quantity=" + quantity + "&id=" + artikelid, true);
	xmlhttp.onreadystatechange = geefreactie;
	xmlhttp.send('');
}
