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 updateProduct(cartId, quantity) {
	GetXmlHttpObject();

	url = "productajax.php";
	url = url + "?doAction=UpdateProduct&cartId=" + cartId + "&quantity=" + quantity;

	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function updateWishList(wlcId, prodPrice, quantity) {
	GetXmlHttpObject();

	url = "productajax.php";
	url = url + "?doAction=UpdateWishList&wlcId=" + wlcId + "&prodPrice=" + prodPrice + "&quantity=" + quantity;

	xmlHttp.onreadystatechange = stateWishListChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged() {
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
			strResponseText = xmlHttp.responseText;
			arrResponseText = strResponseText.split("@");

			totalCost = arrResponseText[0];
			totalQuantity = arrResponseText[1];
			cartId = arrResponseText[2];
			qty = arrResponseText[3];
			price = arrResponseText[4];
			delCharge = arrResponseText[5];

			// change quantity and price for selected cart item
			document.getElementById("spnQty" + cartId).innerHTML = arrResponseText[3];
			document.getElementById("spnPrice" + cartId).innerHTML = arrResponseText[4];

			// Final Price
			var finalPrice = parseFloat(arrResponseText[3]) * parseFloat(arrResponseText[4]);
			document.getElementById("spnFinalPrice" + cartId).innerHTML = finalPrice.toFixed(2);
			
			// delivery charge
			if(document.getElementById("spnDelCharge")) {
				document.getElementById("spnDelCharge").innerHTML = delCharge;
			}
			if(delCharge != 0) {
				document.getElementById("spnDispDelcharge").style.display = '';
			}
			else {
				document.getElementById("spnDispDelcharge").style.display = 'none';
			}

			// grand total
			document.getElementById("spnGrandTotal").innerHTML = totalCost;

			// total cost
			document.getElementById("spnTotalPrice").innerHTML = totalCost;

			// total quantity
			document.getElementById("spnTotalQty").innerHTML = totalQuantity;
	}
}

function stateWishListChanged() {
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
			strResponseText = xmlHttp.responseText;
			arrResponseText = strResponseText.split("@");

			wlcId = arrResponseText[0];
			qty = arrResponseText[1];
			price = arrResponseText[2];
			totalQuantity = arrResponseText[3];

			// change price for selected wishlist item
			document.getElementById("spnQty" + wlcId).innerHTML = qty;
			document.getElementById("spnFinalPrice" + wlcId).innerHTML = price;

			// total quantity
			document.getElementById("spnWishListTotQty").innerHTML = totalQuantity;
	}
}