var xhr = false;
function updateBasketItem(prodID,basketID,basketitemid) {
	document.getElementById("qty" + basketitemid).disabled=true;
	document.getElementById("subtotal"+basketitemid).innerHTML = '<img src="/Content/images/loading.gif">';
	document.getElementById("ordertotal").innerHTML = '<img src="/Content/images/loading.gif">';
	document.getElementById("delv").innerHTML = '<img src="/Content/images/loading.gif">';
	makeRequest("/Content/ASP/updatebasket.asp",1,prodID,basketID,basketitemid);
	return false;
}

function updateAddlCharge(basketID) {
	document.updatebasket.addcharge.disabled=true;
	document.getElementById("ordertotal").innerHTML = '<img src="/Content/images/loading.gif">';
	makeRequest("/Content/ASP/updatebasket.asp",2,0,basketID,0);
	return false;
}

function updateGiftWrap(basketID) {
	document.updatebasket.giftwrap.disabled=true;
	document.getElementById("ordertotal").innerHTML = '<img src="/Content/images/loading.gif">';
	makeRequest("/Content/ASP/updatebasket.asp",3,0,basketID,0);
	return false;
}

function makeRequest(url,formID,prodID,basketID,basketitemid) {
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}
	else {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) { }
		}
	}

	if (xhr) {
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					var outMsg = xhr.responseText;
				}
				else {
					var outMsg = "There was a problem with the request " + xhr.status;

				}
				switch(url) {
					case "/Content/ASP/updatebasket.asp":
						switch(formID) {
							case 1:
						if (outMsg.indexOf(";") != -1){
						var subtotal =  outMsg.split(";");
						document.getElementById("subtotal" + basketitemid).innerHTML = "£" + subtotal[0];
						document.getElementById("ordertotal").innerHTML =  "£" + subtotal[1];
						document.getElementById("delv").innerHTML =  "£" + subtotal[2];
//						document.getElementById("hiddensubtotal").innerHTML = '<input type="hidden" name="MySubtotal" value="' + subtotal[1] + '">';
						}else{
						document.getElementById("subtotal" + basketitemid).innerHTML = outMsg;
						}
						document.getElementById("qty" + basketitemid).disabled = false;
						break;
							default:
						document.getElementById("ordertotal").innerHTML =  "£" + outMsg;
//						document.getElementById("hiddensubtotal").innerHTML = '<input type="hidden" name="MySubtotal" value="' + outMsg + '">';
						document.updatebasket.addcharge.disabled = false;
						document.updatebasket.giftwrap.disabled = false;
						}
				}
			}
		}
		switch(url) {
			case "/Content/ASP/updatebasket.asp":
				switch(formID) {
					case 1:
				data = "basketid=" + basketID + "&basketitemid=" + basketitemid + "&prodid=" + prodID + "&qty=" + document.getElementById("qty" + basketitemid).value;
				break;
					case 2:
				data =  "basketid=" + basketID + "&addcharge=" + document.updatebasket.addcharge.options[document.updatebasket.addcharge.selectedIndex].value;
				break;
					case 3:
				data =  "basketid=" + basketID + "&giftwrap=" + document.updatebasket.giftwrap.options[document.updatebasket.giftwrap.selectedIndex].value;
				break;
					default:
				}
				if (data!="") {
					xhr.open("POST", url,true);
					xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
					xhr.setRequestHeader("Cache-Control","no-cache");
					xhr.setRequestHeader("pragma","no-cache");
					xhr.send(data);
				}
				break;
			default:
				xhr.open("GET", url, true);
				xhr.setRequestHeader("Cache-Control","no-cache");
				xhr.setRequestHeader("pragma","no-cache");
				xhr.send(null);
		}
	}
	else {
		switch(url) {
			case "/Content/ASP/updatebasket.asp":
				switch(formID){
					case 1:
				document.getElementById("subtotal"+basketitemid).innerHTML = "Sorry, connection failed";
				break;
					default:
				document.getElementById("ordertotal").innerHTML = "Sorry, connection failed";
				}
			default:
		}
	}
}

