// JavaScript Document
// JavaScript Document
var xhr; // XMLHttpRequest
var spanID = 0;

function createXHR() {
	var xhr;
	try {
		xhr = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xhr = false;
		}
	}
	if (!xhr && typeof XMLHttpRequest != 'undefined') {
		  xhr = new XMLHttpRequest();
	}
	return xhr;
}

function loadCart() {
	document.getElementById("cart").style.display = "none";
	xhrcart = createXHR();
	xhrcart.onreadystatechange=processloadCart;
	xhrcart.open("GET", "../templates/loadCart.php");
	xhrcart.send(null);
}

function processloadCart() {
	if (xhrcart.readyState == 4) {
		if (xhrcart.status == 200) {
			var res = xhrcart.responseText;
			if ( res.length > 5 ) { 
				document.getElementById("cart").style.display = "block"; 
			}else{
				document.getElementById("cart").style.display = "none"; 
			}
			document.getElementById("cart").innerHTML = res;
			
		} else {
			//document.getElementById("result").innerHTML = "Fout bij het ophalen van de data...";
		}
		//document.getElementById("loader").style.display = "none";
		
	} else if (xhrcart.readyState > 1) {
		//document.getElementById("loader").style.display = "inline";
	}
}

// product toevoegen aan bestelling
function orderProduct(product_ID) {
	xhrprod = createXHRProd();
	xhrprod.onreadystatechange=processOrderProduct;
	xhrprod.open("GET", "../templates/orderProduct.php?p="+product_ID);
	xhrprod.send(null);
}

function createXHRProd() {
	var xhrprod;
	try {
		xhrprod = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xhrprod = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xhrprod = false;
		}
	}
	if (!xhrprod && typeof XMLHttpRequest != 'undefined') {
		  xhrprod = new XMLHttpRequest();
	}
	return xhrprod;
}

function processOrderProduct() {
	if (xhrprod.readyState == 4) {
		if (xhrprod.status == 200) {
			var res = xhrprod.responseText;
			//alert("res= " + res);
			var restext = '<img src="../images/carticon2.gif" border="0" />';
			var spanid  = 'cart' + res;
			//alert("spanid =  " + spanid);
			document.getElementById(spanid).innerHTML = restext;
			loadCart();			
		} else {}
	} else if (xhrprod.readyState > 1) {}
}

// product verwijderen uit winkelmandje
function cancelProduct(product_ID) {
	xhrcanc = createXHRCancel();
	xhrcanc.onreadystatechange=processCancelProduct;
	xhrcanc.open("GET", "../templates/cancelProduct.php?p="+product_ID);
	xhrcanc.send(null);
}

function createXHRCancel() {
	var xhrcanc;
	try {
		xhrcanc = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xhrcanc = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xhrcanc = false;
		}
	}

	if (!xhrcanc && typeof XMLHttpRequest != 'undefined') {
		  xhrcanc = new XMLHttpRequest();
	}
	return xhrcanc;
}

function processCancelProduct() {
	if (xhrcanc.readyState == 4) {
		if (xhrcanc.status == 200) {
			var res = xhrcanc.responseText;
			var restext = '<a href="javascript:orderProduct('+res+')" class="aimg"><img src="../images/iconcart.gif" width="20" height="20" border="0" /></a>';
			var spanid  = "span"+res;
			loadCart();
			document.getElementById(spanid).innerHTML = restext;
		} else {}
	} else if (xhrcanc.readyState > 1) {}
}

// product aantal wijzigen in winkelmand
function changeAantal(product_ID,aantal) {
	xhraant = createXHRAantal();
	xhraant.onreadystatechange=processOrderAantal;
	xhraant.open("GET", "../templates/changeAantal.php?p="+product_ID+"&a="+aantal);
	xhraant.send(null);
}

function createXHRAantal() {
	var xhraant;
	try {
		xhraant = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xhraant = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xhraant = false;
		}
	}
	if (!xhraant && typeof XMLHttpRequest != 'undefined') {
		  xhraant = new XMLHttpRequest();
	}
	return xhraant;
}

function processOrderAantal() {
	if (xhraant.readyState == 4) {
		if (xhraant.status == 200) {
			var res = xhraant.responseText;
			loadCart();
			totalCart();
		} else {}
	} else if (xhraant.readyState > 1) {}
}

// totaal cart wijzigen na aantal wijzigen in winkelmand
function totalCart() {
	xhrtotcart = createXHRAantal();
	xhrtotcart.onreadystatechange=processTotalCart;
	xhrtotcart.open("GET", "../templates/totalCart.php");
	xhrtotcart.send(null);
}

function createXHRtotcart() {
	var xhrtotcart;
	try {
		xhrtotcart = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xhrtotcart = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xhrtotcart = false;
		}
	}
	if (!xhrtotcart && typeof XMLHttpRequest != 'undefined') {
		  xhrtotcart = new XMLHttpRequest();
	}
	return xhrtotcart;
}

function processTotalCart() {
	if (xhrtotcart.readyState == 4) {
		if (xhrtotcart.status == 200) {
			var res = xhrtotcart.responseText;
			document.getElementById("totalcart").innerHTML = res;
			//loadCart();
		} else {}
	} else if (xhrtotcart.readyState > 1) {}
}

// gegevens klant wijzigen in winkelmand
function changeInput(veld,inhoud) {
	xhrinput = createXHRinput();
	xhrinput.onreadystatechange=processChangeInput;
	xhrinput.open("GET", "../templates/changeInput.php?v="+veld+"&i="+inhoud);
	xhrinput.send(null);
}

function createXHRinput() {
	var xhrinput;
	try {
		xhrinput = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xhrinput = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xhrinput = false;
		}
	}
	if (!xhrinput && typeof XMLHttpRequest != 'undefined') {
		  xhrinput = new XMLHttpRequest();
	}
	return xhrinput;
}

function processChangeInput() {
	if (xhrinput.readyState == 4) {
		if (xhrinput.status == 200) {
			var res = xhrinput.responseText;
		} else {}
	} else if (xhrinput.readyState > 1) {}
}

// verhinder enter-key
function stopRKey(evt) {
	var evt;
	var node;
	evt = (evt) ? evt : ((event) ? event : null);
	node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
	if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
document.onkeypress = stopRKey;

// checkbox cadeau wijzigen in winkelmand
function changeCadeau(detail_ID) {
	xhrcb = createXHRcb();
	xhrcb.onreadystatechange=processCB;
	xhrcb.open("GET", "../templates/changeCadeau.php?p="+detail_ID);
	xhrcb.send(null);
}

function createXHRcb() {
	var xhrcb;
	try {
		xhrcb = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xhrcb = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xhrcb = false;
		}
	}
	if (!xhrcb && typeof XMLHttpRequest != 'undefined') {
		  xhrcb = new XMLHttpRequest();
	}
	return xhrcb;
}

function processCB() {
	if (xhrcb.readyState == 4) {
		if (xhrcb.status == 200) {
			var res = xhrcb.responseText;
			//loadCart();
			//totalCart();
		} else {}
	} else if (xhrcb.readyState > 1) {}
}

// checkbox levering wijzigen in winkelmand
function changeLevering() {
	xhrcl = createXHRcl();
	xhrcl.onreadystatechange=processCL;
	xhrcl.open("GET", "../templates/changeLevering.php");
	xhrcl.send(null);
}

function createXHRcl() {
	var xhrcl;
	try {
		xhrcl = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xhrcl = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xhrcl = false;
		}
	}
	if (!xhrcl && typeof XMLHttpRequest != 'undefined') {
		  xhrcl = new XMLHttpRequest();
	}
	return xhrcl;
}

function processCL() {
	if (xhrcl.readyState == 4) {
		if (xhrcl.status == 200) {
			var res = xhrcl.responseText;
			//loadCart();
			//totalCart();
		} else {}
	} else if (xhrcl.readyState > 1) {}
}


