function addRow() {
	dt = document.getElementById("data_table")
	dt.insertRow(0);
}
function addToCart2(strFldName) {
	tmpSubmitFld = "products_qty[" + strFldName + "]";
	
	alert(tmpSubmitFld);
}


//Amount of the current item
var intItemQty = 0;

function setInitQty(strFldName) {
	var tmpFld = window.document.getElementById(strFldName);
	var intQty = tmpFld.value;
	//trim the qty
	intQty = intQty.replace(/^\s+|\s+$/g, '') ;

	if(intQty == "") {
		intQty = 0;	
		// alert(intQty);
	}
	
	if(isNaN(intQty) == true) {
		intQty = 0;	
		// alert(intQty);
	}
	
	intItemQty = parseInt(intQty);
	
	// alert(intItemQty);
}

function removeFromCart(strFldName) {
	
	var tmpFld = window.document.getElementById(strFldName);
	tmpFld.value = 0;
	addToCart(strFldName);
	
}

function addToCart(strFldName) {
	
	
	// alert(strFldName);
	var url = '/online_catalog/index.php/customers/ret_cart_summ';
	
	var tmpFld = window.document.getElementById(strFldName);

	
	var intQty = tmpFld.value;
	// alert(tmpFld.defaultValue);
	tmpSubmitFld = "products_qty[" + strFldName + "]";
	var cartDiv = window.document.getElementById("side-cart");
		
	//trim the qty
	intQty = intQty.replace(/^\s+|\s+$/g, '') ;
	
	
	
	if(intQty == "") {
		intQty = 1;	
		tmpFld.value = intQty;
	}
	
	if(intQty == 0) {
		tmpFld.value = "";	
	}
	
	
	if(isNaN(intQty) == true) {
		alert("The submitted qty is not a number");	
		// tmpFld.focus();
		tmpFld.select();
		return false;
	} else {
	
		// intQty = parseInt(intQty);
		
		//tmpCart Display
		if(intQty != intItemQty) {
			tmpCartText = "<dt>Shopping Cart</dt><dd><p><strong>Your cart is being updated.</strong></p></dd>";
			cartDiv.innerHTML = tmpCartText;
			
			var strParam = tmpSubmitFld + "=" + intQty + "&ajax_update=yes&cart_yes=1";
			new Ajax(url, {update: cartDiv, data: strParam	}).request();
			// var summ_sub = document.getElementById('summ_sub_total').innerHTML;

			//if(blnChkOut == 1) {
//					var randomnumber=Math.floor(Math.random()*1000001);
//					var strUrl = "/online_catalog/index.php/customers/current_order/" + randomnumber;
//					window.navigate(strUrl);
//				
//			}
//			
//			if(document.activeElement != "undefined") {
//				currElement = document.activeElement;
//				if(currElement.id == "process-btn") {
//					var randomnumber=Math.floor(Math.random()*1000001);
//					var strUrl = "/online_catalog/index.php/customers/current_order/" + randomnumber;
//					window.navigate(strUrl);
//					
//				}
//			}

						
		}
	}
}

var arrOptItems = new Array();
var arrOptName = new Array();
var arrOptQty = new Array();
var cntOptItems = 0;

var blnChkOut = 0;

function viewCart() {
	blnChkOut = 1;
//	var blnCheckOut = false;
//	var tmpMsg = "Press OK if you are finished shopping and would like to view your cart details.";
//	blnCheckOut = confirm(tmpMsg);
//	
//	if(blnCheckOut) {
//		var randomnumber=Math.floor(Math.random()*1000001);
//		var strUrl = "/online_catalog/index.php/customers/current_order/" + randomnumber;
//		window.navigate(strUrl);	
//		return true;
//	} else {
//		return false;	
//	}

}

function valCart() {
	blnRslt = true;
	// alert(arrOptItems[0]);
	for (i=0;i<arrOptItems.length;i++) {
			if(arrOptItems[i] == 0) {
				// return true;			
			} else {
				strMsg = 'You have selected to purchase the ' + arrOptName[i] + '.  Please enter the qty of different flavors that you would like delivered until the left to select qty is set to 0.  Then select Update My Cart.';	
				alert(strMsg);
				blnRslt = false;
				
				break;
			}
	}
	return blnRslt;
}

function selectQty(productId) {
	
	var qtyFldName = "products_qty[" + productId + "]";
	var qtySelFldName = "option_qty_" + productId;
	
	
	var qtyFld = window.document.getElementById(productId);
	var qtySelFld = window.document.getElementById(qtySelFldName);
	
	var qtySel = qtySelFld.innerHTML;
	
	itemQty = qtyFld.value;
	
	totQty = itemQty * qtySel;
	arrOptQty[0] = qtySel;
	
	// alert(totQty);
	var strOptCntFld = "option_count" + productId;
	
	var optCntFld = window.document.getElementById(strOptCntFld);
	
	var optCnt = optCntFld.value;
	
	var i = 1;
	var totItems = 0;
	var intOptQty;
	
	while(i <= optCnt) {
		var strFld = "option" + productId + "[" + i + "]";
		totItems = totItems + getTrueQty(strFld, itemQty);

		i++;
	}
	
	
	
	
		strFldName = "option" + productId + "[Driver_Select]";
		var dsQtyFld = window.document.getElementById(strFldName);
		dsQtyFld.value = totQty - totItems;
		arrOptItems = dsQtyFld.value;
		
		
	
	
	if(dsQtyFld.value  != 0) {
		alertNegQty(productId);
	} else {
		rmvAlertNegQty(productId);
	}
	
	/*
	if(dsQtyFld.value == 0) {
		alertCorrectQty(productId);
	}
	*/
}

function alertCorrectQty(productId) {
	var strMsg = '<span class="alert_green">You have selected the correct qty of flavors.</span>';
	var strFld = "alert_option_" + productId + "_0";
	var alrtFld = window.document.getElementById(strFld);
	// $(strFld).set('text',strMsg);
	
	alrtFld.innerHTML = strMsg;
	return true;
} 


function alertNegQty(productId) {
	var strMsg = '<span class="alert_red">This product requires that you select from the flavor options below.  Please select the proper qty until the [left to select] qty = 0.</span>';
	var strFld = "alert_option_" + productId + "_0";
	var alrtFld = window.document.getElementById(strFld);
	// $(strFld).set('text',strMsg);
	
	alrtFld.innerHTML = strMsg;
	return false;
} 

function rmvAlertNegQty(productId) {
	var strMsg = "";
	var strFld = "alert_option_" + productId + "_0";
	var alrtFld = window.document.getElementById(strFld);
	alrtFld.innerHTML = strMsg;
	return true;
	
}

function getTrueQty(strFldName, intItemQty) {
	var tmpFld = window.document.getElementById(strFldName);
	var intQty = tmpFld.value;
	//trim the qty
	intQty = intQty.replace(/^\s+|\s+$/g, '') ;

	if(intQty == "") {
		intQty = 0;	
		// alert(intQty);
	}
	
	if(isNaN(intQty) == true) {
		intQty = 0;	
		// alert(intQty);
	}
	
	if(intQty < 0) {
		intQty = 0;
	}
	
	if(intItemQty == 0) {
		intQty = 0;
	}
	intItemQty = parseInt(intQty);
	tmpFld.value = intItemQty;
	
	return intItemQty;
}

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function submitForPmt() {
		// var strUrl = "https://thatcherfarm.com/online_catalog/processing.html?width=250&height=175";
		// setTimeout(TB_show("Processing",strUrl,true),2000);	
	var pmtFrm = window.document.da_online_payment;
	
	//Get Pmt Amount
	
	var pmtAmt = pmtFrm.pay_amount;
	var numAmt = pmtAmt.value;
	
	
		strMsg = "You are about to charge your credit card " + formatCurrency(numAmt) + "  PRESS OK to Process or Cancel to Not Process";
		
		var blnSubmit = confirm(strMsg);
		
		if(blnSubmit) {
		
			pmtFrm.da_submit_online_payment.value = "Processing Payment";
			pmtFrm.da_submit_online_payment.disabled = true;
			
			pmtFrm.submit();
			return true;
		} else {
			return false;
		}
	
	
}

var cartChngd = 0;

function setUpdCart() {
	
	cartChngd = 1;
}

function chkProcessCart() {
	
	tmpRslt = true;
	if(cartChngd == 1) {
		alert("You have modified the cart. Press OK, check your cart to see if there are any additional instructions or warnings.  If there are, adjust your cart accordingly and then click Update Cart to save your changes.  Then click Process Order.");
		tmpRslt = false;
	} else {
		tmpRslt = valCart();
	}
	return tmpRslt;
}

function submitToCart() {
	var tmpVal;
	tmpVal = setTimeout("countTo1500()",1500);
	
	var cartForm = document.getElementById("frm_prod_list");
	cartForm.submit();
}

function countTo1500() {
	return 1;
}

function emptyCart() {
	var strMsg = "Are you sure you want to empty your cart contents and the order you have submitted for your next delivery?";
	var blnSubmit = confirm(strMsg);	
	
	if(blnSubmit) {
		window.location.href = "/online_catalog/index.php/customers/empty_cart";	
	} else {
		return false;
	}
	
}	
