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 addEditProduct(varId, idQtyBox, pageName1, category1) {
	GetXmlHttpObject();

	eleId = "txtQuantity[" + idQtyBox + "]";
	eleQtyBox = document.getElementById(eleId);
	qty = eleQtyBox.value;
	pageName = pageName1;
	category = category1;

	url = "knights_productajax.php";
	url = url + "?doAction=AddEditProduct&varId=" + varId + "&quantity=" + qty;

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

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

			varId = arrResponseText[0];
			totalCost = arrResponseText[1];
			totalQuantity = arrResponseText[2];

			// execute for product listing page only....
			if(pageName != "OL") {
				// for setting display text of 'Add' button to 'Amend'
				eleId = "btnAddChange[" + varId + "]";
				eleBtnAddChange = document.getElementById(eleId);
				eleBtnAddChange.value = "Amend";

				// for setting row class
				eleId = "productVarRow[" + varId + "]";
				eleProductVarRow = document.getElementById(eleId);
				eleProductVarRow.className = "subproduct activeproduct";
			}
			// execute for order listing page only....
			else {
				// calculate differential amount corresponding to change in quantity which we would be using for calculation total and sub total
				diffAmount = totalCost - document.getElementById("spnPrice").innerHTML;
				diffAmount = parseFloat(diffAmount);

				// total
				eleId = "spnTotal[" + varId + "]";
				total = document.getElementById(eleId).innerHTML;
				total = parseFloat(total) + diffAmount;
				document.getElementById(eleId).innerHTML = total.toFixed(2);

				// sub total
				switch(category) {
					case "W":
						eleId = document.getElementById("spnSubTotalWomen");
						break;
					case "M":
						eleId = document.getElementById("spnSubTotalMen");
						break;
					case "S":
						eleId = document.getElementById("spnSubTotalSkin");
						break;
				}

				subTotal = eleId.innerHTML;
				subTotal = parseFloat(subTotal) + diffAmount;
				eleId.innerHTML = subTotal.toFixed(2);

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

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

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