
var g_sBillingShippingDelimiter="^";

//*****************************
// GetCookie
//*****************************
function GetCookie(name) {
	var start = document.cookie.indexOf(name + "=");
	var length = start+name.length + 1;
	if ((!start) && (name != document.cookie.substring(0,name.length))) { return ""; }
	if (start == -1) { return ""; }
	var end = document.cookie.indexOf(";",length);
	if (end == -1) { end = document.cookie.length; }
	return unescape(document.cookie.substring(length,end));
}

//*****************************
// SetCookie
//*****************************
function SetCookie(name,value,expires,path,domain,secure) {
	path = "/";
	document.cookie = name + "=" +
	escape(value) +
	((expires) ? ";expires=" + expires.toGMTString() : "") +
	((path) ? ";path=" + path : "") + 
	((domain) ? ";domain=" + domain : "") +
	((secure) ? ";secure" : "");
}

//*****************************
// DeleteCookie
//*****************************
function DeleteCookie(name,path,domain) {
	path = "/";
	if (GetCookie(name)) {
		document.cookie = name + "=" +
		((path) ? ";path=" + path : "") +
		((domain) ? ";domain=" + domain : "") +
		";expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

//*****************************
// AddToCart
//*****************************
function AddToCart(sIsTaxable,fOunces,itemNumber,itemName,price,measurement,form,jar,optionalMinQty,optionalMaxQty) {
	
	// note: measurement is the unit of weight (i.e. Oz)
	sIsTaxable = sIsTaxable.toLowerCase();
	switch(sIsTaxable)
	{
		case "yes": sIsTaxable="y"; break;
		case "no": sIsTaxable="n"; break;
		case "y": break;
		case "n": break;
		default: sIsTaxable="n"; break;
	}
	
	// Load the 'basket' cookie
	var cookieValue = GetCookie('basket');

	// check for existing item in basket. if the item already exists in the basket, we will not add it.
	var addItem = true;
	
	// examine each row/field in the 'basker' cookie
	var items = cookieValue.split(",");
	
	// BEGIN UNIQUE ITEM CHECK
	// Uncomment out this block if you wish to only allow the user to add an item 1 time to the cart
	// for each row/field in the 'basket' cookie:
	/*for (var fieldPosition = 0; fieldPosition < items.length; fieldPosition++) {
		// fetch the 'basket' row and split it into an array of fields
		var row = items[fieldPosition].split(":");
		// is this row for the selected item?
		if (row[0] == itemNumber) { 
			// yes. so don't re-add it to the basket. The "View basket" instead will allow the user to add qty to the basket
			addItem = false;
			break;
		}
	}*/
	// END UNIQUE ITEM CHECK
		
	// default Minimum and Max qty
	var sQty = "1";
	var sMaxQty = "80";
	
	// if the optional min qty was passed, use that value.
	if (AddToCart.arguments.length > 8)
	{
		// if passed as a string, use string value
		if (typeof AddToCart.arguments[8] == 'string')
		{
			sQty = AddToCart.arguments[8];
		}
		// if passed as a number, convert to string
		if (typeof AddToCart.arguments[8] == 'number')
		{
			sQty = AddToCart.arguments[8].toString();
		}
	}
	// if the optional max qty was passed, use that value.
	if (AddToCart.arguments.length > 9)
	{
		// if passed as a string, use string value
		if (typeof AddToCart.arguments[9] == 'string')
		{
			sMaxQty = AddToCart.arguments[9];
		}
		// if passed as a number, convert to string
		if (typeof AddToCart.arguments[9] == 'number')
		{
			sMaxQty = AddToCart.arguments[9].toString();
		}
	}
	
	// it's okay to add new item to basket
	if (addItem == true) {
		// create a new row for the 'basket' cookie made up of the passed fields.
		// Note, we add the selected qty and the minimum required qty. The min required qty is the new field at the end of the row
		var newEntry = itemNumber + ":" + itemName + ":" + price + ":" + sQty + ":" + measurement + ":" + form + "-:" + jar + "-0" + ":" + sQty + ":" + sMaxQty + ":" + sIsTaxable + ":" + fOunces + ",";
		// append this new row to the cookie
		cookieValue += newEntry;
		// rewrite the 'basket' cookie 
		SetCookie("basket",cookieValue,"","","","");
	}
	
	// redirect to the view basket page.
	window.location = "/basket/basket.shtml";
}

//*****************************
// ClearCart
//*****************************
function ClearCart() {
	DeleteCookie("basket","","");
	window.location.reload();
}

//*****************************
// Recalculate
//*****************************
function Recalculate(page) {

	var bCheckingOut=false;
	
	// is the user checking out?
	if (Recalculate.arguments.length > 1)
	{
		bCheckingOut = Recalculate.arguments[1] 
	}
	
// get the 'backet' cookie containing all the items
	var cookieValue = GetCookie('basket');
	var newCookieValue = "";
	
	// get an array of the items in the cart
	var items = cookieValue.split(",");
	
	// for each row in the cart
	for (var fieldPosition = 0; fieldPosition < items.length; fieldPosition++) {
		
		// build an array of the fields in the row
		var row = items[fieldPosition].split(":");
		if (row[0]) {
			// only process item if remove checkbox is unchecked
			if (document.getElementById("remove_" + row[0] + "_" + fieldPosition).checked == false) {
				
				// mfindlay: always remember the minimum qty allowed
				var sMinAmount = row[7];
				// mfindlay: always remember the max qty allowed
				var sMaxAmount = row[8];
				
				// new/current amount entered into basket
				var newAmount = document.getElementById("amount_" + row[0] + "_" + fieldPosition).value;
				
				// mfindlay: safety check
				if (newAmount.length < 1)
				{
					newAmount = "1";
				}
				
				// mfindlay: never let the amount get lower than the minimum allowed
				if (parseInt(newAmount) < parseInt(sMinAmount))
				{
					alert("The minimum amount allowed for item " + row[0] + " [" + row[1] + "] is " + sMinAmount + ". Your cart will be updated with this amount.");
					newAmount = sMinAmount;
				}
				// mfindlay: never let the amount get higher than the max allowed
				if (parseInt(newAmount) > parseInt(sMaxAmount))
				{
					alert("The maximum amount allowed for item " + row[0] + " [" + row[1] + "] is " + sMaxAmount + ". Your cart will be updated with this amount.");
					newAmount = sMaxAmount;
				}
				
				// "add jar" selected so add $2.00
				var jarBoolean = "n";
				var jarFee = 0;
				if (document.getElementById("jar_" + row[0] + "_" + fieldPosition)) {	
					jarBoolean = "y";
					if (document.getElementById("jar_" + row[0] + "_" + fieldPosition).checked == true) {
						jarFee = 2;
					}
				}
				var jarField = jarBoolean + "-" + jarFee;
				
				// carry form field through
				var formBoolean = "n";
				var formType = "";
				if (document.getElementById("form_" + row[0] + "_" + fieldPosition)) {	
					formBoolean = "y";
					formType = document.getElementById("form_" + row[0] + "_" + fieldPosition).value;
					
					// make sure the user makes a selection if checking out
					if (bCheckingOut)
					{
						if (formType.length < 1)
						{
							document.getElementById("form_" + row[0] + "_" + fieldPosition).focus();
							alert("Please select an option for item " + row[0] + " [" + row[1] + "]");	
							return;
						}
					}
				}
				var formField = formBoolean + "-" + formType;
				var sIsTaxable="n";
				if (row[9])
				{
					sIsTaxable=row[9];
				}	
				var fOunces=0;
				if (row[10])
				{
					fOunces=row[10];
				}
							
				// create the new row, always remembering the minumum allowed for this item
				var newEntry = row[0] + ":" + row[1] + ":" + row[2] + ":" + newAmount + ":" + row[4] + ":" + formField + ":" + jarField + ":" + sMinAmount + ":" + sMaxAmount + ":" +  sIsTaxable + ":" + fOunces + ",";
				newCookieValue += newEntry;
			}
		}
	}
	SetCookie("basket",newCookieValue,"","","","");
	document.location.href = page;
}


//*****************************
// FormatCurrency
//*****************************
function FormatCurrency(number) {
	number = number.toString().replace(/\$|\,/g,'');
	if (isNaN(number))
	number = "0";
	sign = (number == (number = Math.abs(number)));
	number = Math.floor(number * 100 + 0.50000000001);
	cents = number%100;
	number = Math.floor(number / 100).toString();
	if (cents < 10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((number.length-(1+i))/3); i++)
	number = number.substring(0,number.length - (4 * i + 3)) + ',' + number.substring(number.length - (4 * i + 3));
	return (((sign)?'':'-') + number + '.' + cents);
}
//****************************************************
// JSTrim
// Removes LEADING and TRAILING spaces from a string
//****************************************************
function JSTrim (returnString, removeChar) 
{
	if (removeChar.length)
	{
	  while(''+returnString.charAt(0)==removeChar)
		{
		  returnString=returnString.substring(1,returnString.length);
		}
		while(''+returnString.charAt(returnString.length-1)==removeChar)
	  {
	    returnString=returnString.substring(0,returnString.length-1);
	  }
	}
	
	return returnString;
}
//****************************************************
// JSTrimSpace
// Removes leading and trailing spaces from a string
//****************************************************
function JSTrimSpace(inputString)
{
	return JSTrim(inputString,' ');
}
