var xmlHttp;

function GetXmlHttpObject() {
	try	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch(e) {
		// Internet Explorer
		try	{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
}

function addToCart(productId, quantity, price, vatApply, deliveryApply, attCnt) {
	GetXmlHttpObject();
	url = "productajax.php";
	url = url + "?doAction=addItem&productId=" + productId + "&quantity=" + quantity + "&price=" + price + "&vatApply=" + vatApply + "&deliveryApply=" + deliveryApply;
	if(attCnt > 0) {
		for(var i=1; i<=attCnt; i++) {
			var strAttr = 'attrId' + i;
			var attrId = document.getElementById(strAttr).value;
			var strLst = 'lst' + attrId;
			var objLst = document.getElementById(strLst);
			url = url + '&attrId' + i + "=" + attrId + "&" + strLst + "=" + objLst.value;
		}
		url = url + "&attCnt=" + attCnt;
	}//alert(url);return false;
	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged() {
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {

		strResponseText = xmlHttp.responseText;//alert(strResponseText);

		arrResponse = strResponseText.split("#@#");
		
		var objTopCartQty = document.getElementById("topCartQty");
		var objTopCartPrice = document.getElementById("topCartPrice");

		strQtyInfo = arrResponse[0];
		arrResponseText = strQtyInfo.split("@");

		flMsg = arrResponseText[0];	
		if(flMsg == 'Y' || flMsg == 'U') {
			totalQuantity = arrResponseText[1];
			totalPrice = parseFloat(arrResponseText[2]);

			// change quantity and price for selected cart item
			objTopCartQty.innerHTML = totalQuantity;
			objTopCartPrice.innerHTML = totalPrice.toFixed(2);
			if(flMsg == 'Y') {
				alert('Item has been added to your basket.');
			}
			else {
				alert('Item has been updated in your basket.');
			}
		}
	}
}
