/*********************************************
		
			CART FUNCTIONS
		
**********************************************/

// Globaalseks teha!
function __getDataXML (res, key) {
	return res.getElementsByTagName(key)[0].firstChild.data
}

function myError(e, fName) {
	txt="There was an error on this page.\n\n";
	txt+="Error function: " + fName + "\n\n";
	txt+="Error name: " + e.name + "\n\n";
	txt+="Error message: " + e.message + "\n\n";
	txt+="Error description: " + e.description + "\n\n";
	txt+="Click OK to continue.\n\n";
	alert(txt);
	
	return false;
}



var Idx = 0;
var timeup = 0;
var Idx = 0;

function chgQuantity (cnt, iNo, Type, wIndex, mQnt) {
	if (cnt == "+1") {
		chgQuantity2 (cnt, iNo, Type, wIndex, mQnt);
	} else if (cnt == "-1") {	
		chgQuantity2 (cnt, iNo, Type, wIndex, mQnt);
	}else if (cnt == ""){
		if (timeup) {
			window.clearTimeout(Idx);
			timeup = 0;
		}
		timeup = window.setTimeout('chgQuantity2 ("0", "'+iNo+'", "'+Type+'", "'+wIndex+'", "'+mQnt+'");',500);
	}else{
		if (timeup) {
			window.clearTimeout(Idx);
			timeup = 0;
		}
		timeup = window.setTimeout('chgQuantity2 ("'+cnt+'", "'+iNo+'", "'+Type+'", "'+wIndex+'", "'+mQnt+'");',500);
	}
	return false;
}



function chgQuantity2 (cnt, iNo, Type, wIndex, mQnt) {
	try {
		var mQnt = mQnt * 1;
		if(cnt < mQnt && cnt!="0" && cnt != "+1" && cnt != "-1"){
			cnt = mQnt;
		}
		
		if (!isNaN(wIndex) && wIndex > 0) {
			var pId = iNo + "_" + Type + "_" + wIndex;
		} else {
			var pId = iNo + "_" + Type;
		}
		
		var cnc;
		
		if (cnt == "+1") {
			var cur = parseInt($(pId).value);
			if (isNaN(cur)) {
				cur = 0;
			}
			if(mQnt > 0){
				cnc = cur + mQnt;
			}else{
				cnc = cur + 1;
			}
		} else if (cnt == "-1") {
			var cur = parseInt($(pId).value);
			if (isNaN(cur)) {
				cur = 0;
			}
			if(mQnt > 0){
				cnc = cur - mQnt;
			}else{
				cnc = cur - 1;
			}
		} else {
			var cur = parseInt(cnt);
			if(mQnt > 0){
				var mitu=Math.floor(cur/mQnt);
				var tapselt =cur%mQnt;
				if(tapselt > 0){
					cur = mQnt * (mitu*1 + 1 );
				}else{
					
				}
			}
			cnc = cur;
		}
//		alert("cnt: " + cnt + " & cur: " + cur + " & cnc: " + cnc);
		
		if (isNaN(cnc) || cnc <= 0) {
			cnc = 0;
			$(pId).value = "";
		} else {
			$(pId).value = cnc;
		}
		
		if (Idx) {
			window.clearTimeout(Idx);
			Idx = 0;
		}
		
		Idx = window.setTimeout('updateCart("' + cnc + '", "' + iNo + '", "' + Type + '", "' + wIndex + '")', 500);
	} catch (e) {
		myError(e, functionName(arguments.callee));
	}
	return false;
}

function incQuantity (iNo, Type, wIndex, mQnt, current) {
	if(((current*1)+1)<(mQnt*1)){
		chgQuantity(mQnt, iNo, Type, wIndex, mQnt);
	}else{
		chgQuantity("+1", iNo, Type, wIndex, mQnt);
	}
	return false;
}

function decQuantity (iNo, Type, wIndex, mQnt, current) {
	if(((current*1)) <= (mQnt*1)){
		chgQuantity(0, iNo, Type, wIndex, mQnt);
	}else{
		chgQuantity("-1", iNo, Type, wIndex, mQnt);
	}
	return false;
}

var updateCartProccess = 0;

function updateCart (cnt, iNo, Type, wIndex, extra) {
	try {
		if (!isNaN(wIndex) && wIndex > 0) {
			var pId = iNo + "_" + Type + "_" + wIndex;
		} else {
			var pId = iNo + "_" + Type;
		}
		
		if (document.getElementById(pId)) {
			if (cnt == "0") {
				$(pId).value = "";
			} else{
				$(pId).value = cnt;
			}
		}
		
		if (extra == undefined) {
			extra = "";
		}
		
		var data = x_eta + "=" + x_vn + x_key + mod + x_del + "iCount" + x_key + cnt + x_del + "iNo" + x_key + iNo + x_del + "iType" + x_key + Type + x_del + "wIndex" + x_key + wIndex + extra;
		
		__X({
			url: www + index,
			post: data,
			func: "updateCartResponse",
			parm: "r.responseXML.documentElement",
			loaded: "cartRequestSent(" + cnt + ")"
		});
		/*
		if (requestData(www + index, data, "updateCartResponse", "XML")) {
			updateCartProccess++;
			
			if (updateCartProccess < 2) {
				if (cnt > 0) {
					$("messageProcess").update("Lisame toodet ostukorvi").show();
				} else {
					$("messageProcess").update("Eemaldame toodet ostukorvist").show();
				}
			}
		}
		*/
	} catch (e) {
		myError(e, functionName(arguments.callee));
	}
}

function cartRequestSent (cnt) {
	updateCartProccess++;
	
	if (updateCartProccess < 2) {
		if (cnt > 0) {
			$("messageProcess").update("Lisame toodet ostukorvi").show();
		} else {
			$("messageProcess").update("Eemaldame toodet ostukorvist").show();
		}
	}
}

function updateCartResponse (res) {
	try {
		updateCartProccess--;
		
		if (updateCartProccess == 0) {
			$("messageProcess").hide();
//			$("messageDone").show();
//			window.setTimeout('$("messageDone").hide()', 1500);
		}
		
		if (isValidXMLObject(res)) {
			if (parseInt(__getDataXML(res, "status"))) {
				$("messageDone").update(__getDataXML(res, "message")).show();
				window.setTimeout('$("messageDone").hide()', 2000);
				
				var iNo = __getDataXML(res, "iNo");
				var iType = __getDataXML(res, "iType");
				
				var iCount = parseInt(__getDataXML(res, "iCount"));
				var iPreCount = parseInt(__getDataXML(res, "iPreCount"));
				
				var iTotal = __getDataXML(res, "iTotal");
				var wIndex = parseInt(__getDataXML(res, "wIndex"));
				
				// Detected pre order query
				
				var isPre = (!isNaN(wIndex) && wIndex > 0)
				
				if (!isPre) {
					
					$("rowT_" + iNo + "_" + iType).update(iTotal);
				}
				
				if (document.getElementById("cartSumTotal")) {
					$("cartSumTotal").update(__getDataXML(res, "cSum"));
					$("cartVatTotal5").update(__getDataXML(res, "cVat5"));
					$("cartVatTotal20").update(__getDataXML(res, "cVat20"));
					$("cartGrandTotal").update(__getDataXML(res, "cTotal"));
				}
				
				if (document.getElementById("upperCartCount")) {
					$("upperCartCount").update(__getDataXML(res, "cCount"));
				}
				
				if (document.getElementById("upperPreCartCount")) {
					$("upperPreCartCount").update(__getDataXML(res, "cPreCount"));
				}
				
				// Remove or color/decolor (main) product
				var itemListRow = "rowR_" + iNo + "_" + iType;
				if (document.getElementById(itemListRow)) {
					if (isPre) {
						if (!iPreCount) {
							if (__getDataXML(res, "Page") == "cart") {
								$(itemListRow).parentNode.deleteRow($(itemListRow).rowIndex);
							} else {
								$(itemListRow).removeClassName("active-preorder");
							}
						} else {
							if (__getDataXML(res, "Page") != "cart") {
								$(itemListRow).addClassName("active-preorder");
							}
						}
					} else {
						if (iCount) {
							if (__getDataXML(res, "Page") != "cart") {
								$(itemListRow).addClassName("active");
							}
						} else {
							if (__getDataXML(res, "Page") == "cart") {
								$(itemListRow).parentNode.deleteRow($(itemListRow).rowIndex);
							} else {
								$(itemListRow).removeClassName("active");
							}
						}
					}
				}
				
				// PreOrderLine create, update & delete
				if (!isNaN(wIndex) && wIndex > 0) {
					if (!iCount) {
						var preOrderListRow = "rowW_" + iNo + "_" + iType + "_" + wIndex;
						if (document.getElementById(preOrderListRow)) {
							$(preOrderListRow).parentNode.deleteRow($(preOrderListRow).rowIndex);
						}
					} else {
						
						var Content = __getDataXML(res, "Content");
						var preView = parseInt(__getDataXML(res, "preView"));
						if (!Content.empty()) {
							var tBody = $(itemListRow).parentNode;
							
							var curIndex = (1 + $(itemListRow).rowIndex);
							
							var row = tBody.insertRow(curIndex);
							var rowId = "rowW_" + iNo + "_" + iType + "_" + wIndex;
							
							row.setAttribute("id", rowId);
							
//							if (preView) {
								row.setAttribute("class", "new-week");
//							}
							
							/*
							var cell = row.insertCell(0);
							cell.colSpan = 11;
							cell.style.padding = "1px 0 0 0";
							
							var div = document.createElement("div");
							div.setAttribute("class", "week-options");
							
							cell.appendChild(div);
							
							div.update(Content);
							*/
							
							$(rowId).update(Content);
							/*
							var Contenttd = __getDataXML(res, "td_1");
							var cell = row.insertCell(0);
							cell.colSpan = 6;
							cell.style.padding="1px 0pt 0pt";
							cell.innerHTML=Contenttd;
							
							Contenttd = __getDataXML(res, "td_2");
							cell = row.insertCell(1);
							cell.setAttribute("class", "week-options");
							cell.id="rowFORIE_" + iNo + "_" + iType + "_" + wIndex;
							$("rowFORIE_" + iNo + "_" + iType + "_" + wIndex).update(Contenttd);

							
							Contenttd = __getDataXML(res, "td_3");
							cell = row.insertCell(2);
							cell.setAttribute("class", "week-options");
							cell.innerHTML=Contenttd;
							

							Contenttd = __getDataXML(res, "td_4");
							cell = row.insertCell(3);
							cell.setAttribute("class", "week-options");
							cell.innerHTML=Contenttd;

							Contenttd = __getDataXML(res, "td_5");
							cell = row.insertCell(4);
							cell.setAttribute("class", "week-options");
							cell.style.fontWeight="bold";
							cell.id="rowT_" + iNo + "_" + iType + "_" + wIndex;
							$("rowT_" + iNo + "_" + iType + "_" + wIndex).addClassName("align-right");
							cell.innerHTML=Contenttd;

							Contenttd = __getDataXML(res, "td_6");
							cell = row.insertCell(5);
							cell.setAttribute("class", "week-options");
							cell.innerHTML=Contenttd;
							//eval(Content);

							//row.update(Content.length);
							*/
						}
						
						if (!preView) {
							$("rowT_" + iNo + "_" + iType + "_" + wIndex).update(iTotal);
						}
					}
				}
				
				var itemListRem = "rowD_" + iNo + "_" + iType;
				if (document.getElementById(itemListRem)) {
					if (parseInt(__getDataXML(res, "iCount"))) {
						$(itemListRem).show();
					} else {
						$(itemListRem).hide();
					}
				}
				
				
			} else {
				$("messageError").update(__getDataXML(res, "message")).show();
				
				window.setTimeout('$("messageError").hide()', 2000);
			}
		} else {
			$("messageError").update("Tundmatu vastus serverilt, palun proovige uuesti #1").show();
			window.setTimeout('$("messageError").hide()', 2000);
		}
	} catch (e) {
		myError(e, functionName(arguments.callee));
	}
}

// If there isn't an element with an onclick event in your row, then this function can't be used.
function deleteCurrentRow(obj)
{
	if (hasLoaded) {
		var delRow = obj.parentNode.parentNode;
		var tbl = delRow.parentNode.parentNode;
		var rIndex = delRow.sectionRowIndex;
		var rowArray = new Array(delRow);
		deleteRows(rowArray);
		reorderRows(tbl, rIndex);
	}
}

function deleteRows(rowObjArray)
{
	if (hasLoaded) {
		for (var i=0; i<rowObjArray.length; i++) {
			var rIndex = rowObjArray[i].sectionRowIndex;
			rowObjArray[i].parentNode.deleteRow(rIndex);
		}
	}
}

// Add new preOrder line (show/hide)
function showHideAddWeek (id, show) {
	if (show) {
		$(id).show();
	} else {
		$(id).hide();
	}
}

function preOrderCart (iNo, Type, extra) {
	updateCart (1, iNo, Type, 0, extra);
}

function removePreOrder (iNo, Type, weekDate) {
	updateCart(0, iNo, Type, weekDate, "");
}
