var xmlHttp;
var tot = 0;

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 addEditPackageExtra(pckId, idQtyBox) {
	GetXmlHttpObject();

	eleId = "txtQuantity" + pckId;
	eleQtyBox = document.getElementById(eleId);
	qty = eleQtyBox.value;

	url = "packageajax.php";
	url = url + "?doAction=AddEditPackageExtra&pckId=" + pckId + "&quantity=" + qty;

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

function deletePackageExtra(pckId, idQtyBox) {
	
	GetXmlHttpObject();

	eleId = "txtQuantity" + pckId;
	eleQtyBox = document.getElementById(eleId);
	qty = eleQtyBox.value;

	url = "packageajax.php";
	url = url + "?doAction=DeletePackageExtra&pckId=" + pckId + "&quantity=" + qty;

	xmlHttp.onreadystatechange = stateChanged;

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

function addEditPackage(pckId, idQtyBox) {
	GetXmlHttpObject();

	eleId = "txtQuantity" + pckId;
	eleQtyBox = document.getElementById(eleId);
	qty = eleQtyBox.value;

	url = "packageajax.php";
	url = url + "?doAction=AddEditPackage&pckId=" + pckId + "&quantity=" + qty;

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

function addEditPackageContent(pckId, idQtyBox) {
	GetXmlHttpObject();

	eleId = "txtQuantityCon" + pckId;
	eleQtyBox = document.getElementById(eleId);
	qty = eleQtyBox.value;
	
	url = "packageajax.php";
	url = url + "?doAction=AddEditPackageContent&pckId=" + pckId + "&quantity=" + qty;
	
	xmlHttp.onreadystatechange = stateChangedContent;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function deletePackage(pckId, idQtyBox) {
	
	GetXmlHttpObject();

	eleId = "txtQuantity" + pckId;
	eleQtyBox = document.getElementById(eleId);
	qty = eleQtyBox.value;

	url = "packageajax.php";
	url = url + "?doAction=DeletePackage&pckId=" + pckId + "&quantity=" + qty;

	xmlHttp.onreadystatechange = stateChanged;

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


function deletePackageContent(pckId, idQtyBox) {
	
	GetXmlHttpObject();

	eleId = "txtQuantityCon" + pckId;
	eleQtyBox = document.getElementById(eleId);
	qty = eleQtyBox.value;

	url = "packageajax.php";
	url = url + "?doAction=DeletePackageContent&pckId=" + pckId + "&quantity=" + qty;

	xmlHttp.onreadystatechange = stateChangedContent;

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

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

			pckId = arrResponseText[0];
			totalCost1 = arrResponseText[1];
			totalQuantity = arrResponseText[2];
			totalDiscount = arrResponseText[3];
			extraCost = arrResponseText[7];
			var arrQt = new Array();
			arrQt=arrResponseText[4].split("#");
			var arrCost = new Array();
			arrCost=arrResponseText[5].split("#");
			var arrTxtVal = new Array();
			arrTxtVal=arrResponseText[6].split("#");
			var tot = 0;

				eleId = "chkAddChange" + pckId;
				eleBtnAddChange = document.getElementById(eleId);

				if(eleBtnAddChange.checked == true) {
					// for setting row class
					eleId = "productVarRow" + pckId;
					eleProductVarRow = document.getElementById(eleId);
					if(pckId == 1) {
						eleProductVarRow.className = "active1";

					}
					else if(pckId == 2) {
						eleProductVarRow.className = "active";

					}
					else if(pckId == 3) {
						eleProductVarRow.className = "active2";

					}
					else if(pckId == 4 || pckId == 5) {
						eleProductVarRow.className = "active3";

					}
					else {
						eleProductVarRow.className = "active";
					}
					
				}
				else {
					
					// for setting row class
					eleId = "productVarRow" + pckId;
					eleProductVarRow = document.getElementById(eleId);
					eleProductVarRow.className = "";
					eleQtyBox.value = "";

				}			

				for(var cnt = 1; cnt <= 12; cnt++) {
					eleConId = "txtQuantityCon" + cnt;
					eletxtQuantityCon = document.getElementById(eleConId);

					elechkConId = "chkAddChangeCon" + cnt;
					elechkAddChangeCon = document.getElementById(elechkConId);

					var txtVal = 0;
					if(arrQt[cnt-1]) {
						txtVal = txtVal + parseInt(arrQt[cnt-1]);
					}
					if(arrTxtVal[cnt-1]) {
						txtVal = txtVal + parseInt(arrTxtVal[cnt-1]);
					}

					// hidden variable
					eleOldCon = "oldQtyCon" + cnt;
					eleOldQtyCon = document.getElementById(eleOldCon);
					if(!arrQt[cnt-1]) {
						eleOldQtyCon.value = 0;
					}
					else {
						eleOldQtyCon.value = arrQt[cnt-1];
					}					
						
					if(!arrQt[cnt-1]) {
						if((cnt != '8') && (cnt != '11') && (cnt != '12')) {
							if(txtVal == 0) {
								txtVal = "";
							}
							tot = tot + (txtVal * arrCost[cnt-1]);
							eletxtQuantityCon.value = txtVal;
							if(txtVal == 0) {
								elechkAddChangeCon.checked = false;
							}
							else {
								elechkAddChangeCon.checked = true;
							}
							elechkAddChangeCon.disabled = false;
							if(elechkAddChangeCon.checked == false) {
								eleId = "productVarRow1" + cnt;
								eleProductVarRow1 = document.getElementById(eleId);
								if(txtVal == 0) {
									eleProductVarRow1.className = "";
								}
								else {
									//eleProductVarRow1.className = "active";
									if(cnt == 1 || cnt == 2 || cnt == 3 || cnt == 9 || cnt == 10) {
										eleProductVarRow1.className = "active";
									}
									else {
										eleProductVarRow1.className = "blueactive";
									}
								}
							}
						}
					}
					else if(txtVal != 0) {
						
						if((cnt != '8') && (cnt != '11') && (cnt != '12')) {
							tot = tot + (txtVal * arrCost[cnt-1]);
							eletxtQuantityCon.value = txtVal;
							elechkAddChangeCon.checked = true;
							if(arrQt[cnt-1] != 0) {
								elechkAddChangeCon.disabled = true;
							}
							else {
								elechkAddChangeCon.disabled = false;
							}
							if(elechkAddChangeCon.checked == true) {
								eleId = "productVarRow1" + cnt;
								eleProductVarRow1 = document.getElementById(eleId);
								if(txtVal == 0) {
									eleProductVarRow1.className = "";
								}
								else {
									if(cnt == 1 || cnt == 2 || cnt == 3 || cnt == 9 || cnt == 10) {
										eleProductVarRow1.className = "active";
									}
									else {
										eleProductVarRow1.className = "blueactive";
									}
								}
							}
						}
					}
					else {
						if(txtVal == 0) {
							txtVal = "";
						}
						if((cnt != '8') && (cnt != '11') && (cnt != '12')) {
							tot = tot + (txtVal * arrCost[cnt-1]);
							eletxtQuantityCon.value = txtVal;
							if(txtVal == 0) {
								elechkAddChangeCon.checked = false;
							}
							else {
								elechkAddChangeCon.checked = true;
							}
							elechkAddChangeCon.disabled = false;
							if(elechkAddChangeCon.checked == false) {
								eleId = "productVarRow1" + cnt;
								eleProductVarRow1 = document.getElementById(eleId);
								if(txtVal == 0) {
									eleProductVarRow1.className = "";
								}
								else {
									if(cnt == 1 || cnt == 2 || cnt == 3 || cnt == 9 || cnt == 10) {
										eleProductVarRow1.className = "active";
									}
									else {
										eleProductVarRow1.className = "blueactive";
									}
								}
							}
						}
					}
				}				
			
			// total cost
			tot = tot + parseInt(extraCost);
			grandTotal = parseInt(tot) - parseInt(totalDiscount);
			document.getElementById("spnPrice").innerHTML = tot.toFixed(2);
			document.getElementById("spnPrice2").innerHTML = tot.toFixed(2);
			document.getElementById("spnDis").innerHTML = totalDiscount;
			document.getElementById("spnDis2").innerHTML = totalDiscount;
			document.getElementById("spnTot").innerHTML = grandTotal.toFixed(2);			
	}
}

function stateChangedContent() {
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
			strResponseText = xmlHttp.responseText;
			arrResponseText = strResponseText.split("@");
			pckId = arrResponseText[0];
			
			totalCost1 = arrResponseText[1];
			totalQuantity = arrResponseText[2];
			totalDiscount = arrResponseText[3];
			extraCost = arrResponseText[7];
			var arrQt = new Array();
			arrQt=arrResponseText[4].split("#");
			var arrCost = new Array();
			arrCost=arrResponseText[5].split("#");
			var arrTxtVal = new Array();
			arrTxtVal=arrResponseText[6].split("#");
			var tot = 0;

				eleId1 = "chkAddChangeCon" + pckId;
				eleBtnAddChange1 = document.getElementById(eleId1);

				if(eleBtnAddChange1.checked == true) {
					// for setting row class
					eleId = "productVarRow1" + pckId;
					eleProductVarRow = document.getElementById(eleId);
					if(pckId == 1 || pckId == 2 || pckId == 3 || pckId == 9 || pckId == 10) {
						eleProductVarRow.className = "active";
					}
					else {
						eleProductVarRow.className = "blueactive";
					}

				}
				else {
					
					// for setting row class
					eleId = "productVarRow1" + pckId;
					eleProductVarRow = document.getElementById(eleId);
					eleProductVarRow.className = "";
					eleQtyBox.value = "";

				}

				for(var cnt = 1; cnt <= 12; cnt++) {

					var txtVal = 0;
					if(arrQt[cnt-1]) {
						txtVal = txtVal + parseInt(arrQt[cnt-1]);
					}
					if(arrTxtVal[cnt-1]) {
						txtVal = txtVal + parseInt(arrTxtVal[cnt-1]);
					}				
						
					if(!arrQt[cnt-1]) {						
						tot = tot + (txtVal * arrCost[cnt-1]);
					}
					else if(txtVal != 0) {
						tot = tot + (txtVal * arrCost[cnt-1]);							
					}
					else {
						tot = tot + (txtVal * arrCost[cnt-1]);
					}
				}				
			
			// total cost
			tot = tot + parseInt(extraCost);
			grandTotal = parseInt(tot) - parseInt(totalDiscount);
			document.getElementById("spnPrice").innerHTML = tot.toFixed(2);
			document.getElementById("spnPrice2").innerHTML = tot.toFixed(2);
			document.getElementById("spnDis").innerHTML = totalDiscount;
			document.getElementById("spnDis2").innerHTML = totalDiscount;
			document.getElementById("spnTot").innerHTML = grandTotal.toFixed(2);
	}
}