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 addArtist(artistId, prodPrice) {

	GetXmlHttpObject();
	url = "productajax.php";
	url = url + "?doAction=addArtist&artistId=" + artistId + "&prodPrice=" + prodPrice;

	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function updateArtist(cartId, quantity) {
	GetXmlHttpObject();

	url = "productajax.php";
	url = url + "?doAction=UpdateArtist&cartId=" + cartId + "&quantity=" + quantity;

	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function updateArddress(addrId){
	GetXmlHttpObject();

	url = "productajax.php";
	url = url + "?doAction=UpdateAddress&addrId=" + addrId;

	xmlHttp.onreadystatechange = stateChanged1;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged1(){
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		strResponseText = xmlHttp.responseText;
		arrResponse = strResponseText.split("~@~");

		addr1 = arrResponse[0];		
		addr2 = arrResponse[1];
		addr3 = arrResponse[2];
		email1 = arrResponse[3];
		email2 = arrResponse[4];
		postCode = arrResponse[5];
		country = arrResponse[6];
		

		document.getElementById("txtAddressM1").value = addr1;
		document.getElementById("txtAddressM2").value = addr2;
		document.getElementById("txtAddressM3").value = addr3;
		document.getElementById("txtEmailM1").value = email1;
		document.getElementById("txtPostCodeM").value = postCode;
		//document.getElementById("txtCountryM").value = country;

		for (i = 0; i < document.getElementById('lstCountryMId').length;i++)
		{

			if (country == document.getElementById('lstCountryMId').options[i].value)
			{
			document.getElementById('lstCountryMId').options[i].selected = true;
			return;
			}
		}
	}
}


function stateChanged() {
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {

		strResponseText = xmlHttp.responseText;
		arrResponse = strResponseText.split("#@#");

		strQtyInfo = arrResponse[0];
		strProdInfo = arrResponse[1];
		
		arrResponseText = strQtyInfo.split("@");
		arrProductInfo = strProdInfo.split("@~@");

		flMsg = arrResponseText[0];		
		totalQuantity = arrResponseText[1];
		totalPrice = arrResponseText[2];
		flagCart = arrResponseText[3];

		if (flagCart == "Y"){
			cartId = arrResponseText[4];
			qty = arrResponseText[5];
			price = arrResponseText[6];

			// change quantity and price for selected cart item
			document.getElementById("spnQty" + cartId).innerHTML = qty;
			document.getElementById("spnPrice" + cartId).innerHTML = price;

			// Final Price
			var finalPrice = parseFloat(qty) * parseFloat(price);
			document.getElementById("spnFinalPrice" + cartId).innerHTML = finalPrice.toFixed(2);
			
			totalPrice = parseFloat(totalPrice)
			// total cost
			document.getElementById("spnGrandTotal").innerHTML = totalPrice.toFixed(2);
		}
		
		if (flMsg != "S"){
			// FOR CART
			document.getElementById("shopCartTable").style.display = "";
			document.getElementById("checkOut").style.display = "";
			
			// FOR NOITEM	
			document.getElementById("shopCartBox").style.display = "";

			objTr = document.getElementById('cartTBody');
			currTable = objTr.parentNode;
			currTable.removeChild(objTr);
			
			cartTable = document.getElementById("shopCartTable")
			var cartBody = document.createElement("tbody");
			cartBody.setAttribute("id", "cartTBody");
			

			////////////////////////////////////
			for (i = 0 ; i < arrProductInfo.length; i++)
			{	
				arrProdDetails = arrProductInfo[i].split("~@~");
				if (arrProdDetails[0] == "") continue;

				tempRow = document.createElement("tr");
				tempCell = document.createElement("td");
				tempCell.className = "product";

				tempDl = document.createElement("dl");
				tempDl.className = "attributes";
				tempCell.appendChild(tempDl);
				
				tempDt = document.createElement("dt");
				tmpAnchor = document.createElement("a");
				tmpAnchor.setAttribute("href", "artistdetail.php?artistListId=" + arrProdDetails[0]);

				tmpAnchor.appendChild(document.createTextNode(arrProdDetails[1]));
				tempDt.appendChild(tmpAnchor);
				tempDl.appendChild(tempDt);

				tempDd = document.createElement("dd");
				tempDl.appendChild(tempDd);

				tempStrong = document.createElement("strong");
				tempStrong.appendChild(document.createTextNode("Code :"));
				tempDd.appendChild(tempStrong);

				tempDd.appendChild(document.createTextNode(arrProdDetails[2]));
				tempRow.appendChild(tempCell);

				tempCell = document.createElement("td");
				tempCell.className = "qty";
				tempCell.appendChild(document.createTextNode(arrProdDetails[3]));
				tempRow.appendChild(tempCell);
				cartBody.appendChild(tempRow);
				cartTable.appendChild(cartBody);
			}
			////////////////////////////////////

			document.getElementById("totPrice").innerHTML = totalPrice;
		}

		if(flMsg == "Y") {
			// display message
			alert("The item has been added to your basket");
		}
		else if (flMsg == "N"){
			// display message
			alert("The item already added to the basket");
		}
		else if (flMsg == "S"){
			// display message
			alert("No songs available for the selected artist");
		}
	}
}