function setHampers() {
			
	truncateHampers();
	print_cookies();

	//first cookie
		setCookie("_ldcookieOpen", "1", 10, '/hampers/');


	function parsePrice(price) {
		var tmpStr = price;
		var tmpSplit = tmpStr.split("$");
		var tmpSplit2 = tmpSplit[1].split(".");
		return tmpSplit2[0];
	}
	
	$(".h-add-to-cart").click(function(i){
		var originalHamperName = $(this).parent().parent().prev().text();
		var hamperName = "_ld__" + originalHamperName;
		var hamperPrice = "_ld__" + $(this).parent().parent().prev().prev().text();
		var parsedHamperPrice = parsePrice(hamperPrice);
		//alert(parsedHamperPrice);
		var cHamperName = hamperName.replace(/ /g, "-")
		cHamperName = cHamperName + "___" + parsedHamperPrice;
		var cHamperAmount = 0;
		cHamperAmount = $(this).prev().attr("value");
		
		// limit at 100 per product
		if(cHamperAmount > 100) {
			//alert("Sorry, basket Limit is 100 for each hamper.\n\n If you need more hampers please call the office.\n\n"); 
			
			 	$.alerts.dialogClass = "error-style"; // set custom style class
				jAlert("Sorry, basket Limit is 100 for each hamper.\nIf you need more hampers please call the office.\n\n" , "Whoops...", function() { $.alerts.dialogClass = null; });
			
			return false; 
		}
		if (isNaN(cHamperAmount)) {
			//alert("Oops!\n\nWe need a real number to add this item to the cart.");
			//jAlert('Oops! We need a real number to add this item to the cart.', 'Please note...');
			
			$.alerts.dialogClass = "error-style"; // set custom style class
			jAlert("We need a real number to add this item to the cart" , "Whoops...", function() { $.alerts.dialogClass = null; });
			
			return false; 
		}
		setCookie(cHamperName, cHamperAmount, 10, '/hampers/');
		
		
 		
		if(cHamperAmount == 1) {
			jAlert(cHamperAmount + ' <strong>' + originalHamperName + '</strong> Hamper has been added to your cart.', 'Notice');
			
		} else {
			jAlert(cHamperAmount + ' <strong>' + originalHamperName + '</strong> Hampers have been added to your cart.', 'Notice');	
		}
		
		// update basket
		print_cookies(true);
		
		//$(this).prev().attr("value") = "1";

		return false;
 	});      
}

function truncateHampers() {
		$(".h-right").each(function(){
		var target = $(this).children().next();
		$(target).truncate({max_length: 300});
	});
}

function removeHamper(which, normal) {
	
	//$.cookie(which, null);
	setCookie(which, "", -1, "/hampers/");
	
	if(!normal) {
		//alert('view == "checkout"');
		orderConfirmation();
	} else {
		print_cookies();
	}
	
	
}
 

function activateBasket() {
	if(!$("dl.hamper-basket div.check").hasClass("shown")) {
			$("dl.hamper-basket div.check").show("slow");
			$("dl.hamper-basket div.check").addClass("shown");
	}	
}
function deactivateBasket() {
	$("dl.hamper-basket div.check").hide("fast");
	$("dl.hamper-basket div.check").removeClass("shown");
	var basketText = "<li>Your cart is empty</li>";
	$(".hamper-basket ul").html(basketText);
}

// put items in the shopping basket from the cookies
function print_cookies() {
	
	//var cookieValueArray = new Array();
	var cartTotal = 0;
	
	var cookies = get_cookies_array();
	var cookieDataString = "";
	for(var name in cookies) {
		var originalName = name;
		var hyphenName = name.replace(/-/g, " ");
		var stripPrefixName = hyphenName.replace("_ld__", "");
		var priceSplit = stripPrefixName.split("___");
		var theName = priceSplit[0];
		var thePrice = priceSplit[1];
		cartTotal += Number(priceSplit[1])*Number(cookies[name]);
		//cookieDataString += '<li><a id="' + originalName + '" onclick="removeHamper(this.id)" title="Remove this item from the cart">&nbsp;<span>[remove]</span></a>' + theName + ' (' + cookies[name] + 'x @' + thePrice + ')</li>';
		cookieDataString += '<li><a id="' + originalName + '" onclick="removeHamper(this.id,true)" title="Remove this item from the cart">&nbsp;<span>[remove]</span></a>' + theName + ' (' + cookies[name] + ')</li>';
	}
	 
	if(cookieDataString == "") {
		deactivateBasket();
	} else {
		activateBasket();
		$(".hamper-basket ul").html(cookieDataString);
		$("#total-price").text(cartTotal+".00");
	}
	
}

// order screen
function orderConfirmation() {
	
	//var cookieValueArray = new Array();
	var cartTotal = 0;
	var confirmationDataString = '';
	var confirmationEmailString = '\r\n----------------------- HAMPER ORDER -----------------------\r\n\r\n';
	confirmationDataString += '<h2>Your hamper cart</h2><table class="hamper-cart" border="0" cellspacing="1" cellpadding="0"><tr><th>Hamper</th><th style="text-align:right;">Cost/unit</th><th style="text-align:right;">Total</th><th style="text-align:center;width:14px;"></th></tr>';
	var cookies = get_cookies_array();
	
	for(var name in cookies) {
		var originalName = name;
		var hyphenName = name.replace(/-/g, " ");
		var stripPrefixName = hyphenName.replace("_ld__", "");
		var priceSplit = stripPrefixName.split("___");
		var theName = priceSplit[0];
		var thePrice = priceSplit[1];
		cartTotal += Number(priceSplit[1]) * Number(cookies[name]);
		var rowTotal = Number(thePrice) * Number(cookies[name]);
		//cookieDataString += '<li><a id="' + originalName + '" onclick="removeHamper(this.id)" title="Remove this item from the cart">&nbsp;<span>[remove]</span></a>' + theName + ' (' + cookies[name] + 'x @' + thePrice + ')</li>';
		confirmationDataString += '<tr><td>' + theName + ' (' + cookies[name] + ')</td><td style="text-align:right;">$' + thePrice + '.00</td><td style="text-align:right;">$' + rowTotal + '.00</td><td style="text-align:center;"><a id="' + originalName + '" onclick="removeHamper(this.id,false)" title="Remove this item from the cart">&nbsp;<span>[remove]</span></a></td></tr>';
		confirmationEmailString += theName + ' (' + cookies[name] + ') - $' + rowTotal + '.00\n';

	}
	
	confirmationDataString += '<tr><td colspan="3" style="text-align:right;"><strong>TOTAL inc AU $' + cartTotal + '.00</strong></td><td></td></tr></table>';
	confirmationEmailString += '\r\n------------------------------------------------------------\r\n\r\nTOTAL inc AU $' + cartTotal + '.00\r\n\r\n--------------------- END HAMPER ORDER ---------------------';
	
	if(cartTotal == 0) {
		$("#confirmation-message").hide();
		$("#confirmation-output").html('<p><strong>You have nothing in your cart.</strong></p><p><a href="javascript:history.go(-1);" title="Go back to previous page">&laquo;&nbsp;Go back</a></p>');
		$("div.hamper-form-submit").hide("fast");
	} else {
		//activateBasket();
		$("#confirmation-output").html(confirmationDataString);
		$("#HamperOrder_5_txtOrder").html(confirmationEmailString);
		$("form#hamper-order").show("fast");
	}
	
}

function get_cookies_array() {
	
    var cookies = { };
	
    if (document.cookie && document.cookie != '') {
        var split = document.cookie.split(';');
        for (var i = 0; i < split.length; i++) {

				var name_value = split[i].split("=");
				name_value[0] = name_value[0].replace(/^ /, '');
				checkName = name_value[0].indexOf("_ld__");
				//alert(checkName);
		 	if(checkName == 0 || checkName == "0" || checkName == true) {
				if(name_value[1] !== "" && name_value[1] !== undefined) {
					cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
				}
			 }
        }
    }
    return cookies;
}




function getCookie(w){
	cName = "";
	pCOOKIES = new Array();
	pCOOKIES = document.cookie.split('; ');
	for(bb = 0; bb < pCOOKIES.length; bb++){
		NmeVal  = new Array();
		NmeVal  = pCOOKIES[bb].split('=');
		if(NmeVal[0] == w){
			cName = unescape(NmeVal[1]);
		}
	}
	return cName;
}

function printCookies(w){
	cStr = "";
	pCOOKIES = new Array();
	pCOOKIES = document.cookie.split('; ');
	for(bb = 0; bb < pCOOKIES.length; bb++){
		NmeVal  = new Array();
		NmeVal  = pCOOKIES[bb].split('=');
		if(NmeVal[0]){
			cStr += NmeVal[0] + '=' + unescape(NmeVal[1]) + '; ';
		}
	}
	return cStr;
}

function setCookie(name, value, expires, path, domain, secure){
	//alert(name + "=" + escape(value) + "; ");
	document.cookie = name + "=" + escape(value) + "; ";
	
	if(expires){
		expires = setExpiration(expires);
		document.cookie += "expires=" + expires + "; ";
	}
	if(path){
		document.cookie += "path=" + path + "; ";
	}
	if(domain){
		document.cookie += "domain=" + domain + "; ";
	}
	if(secure){
		document.cookie += "secure; ";
	}
}

function setExpiration(cookieLife){
    var today = new Date();
    var expr = new Date(today.getTime() + cookieLife * 24 * 60 * 60 * 1000);
    return  expr.toGMTString();
}

function removeAllHampers() {
	var cookies = { };
    if (document.cookie && document.cookie != '') {
        var split = document.cookie.split(';');
        for (var i = 0; i < split.length; i++) {
				var name_value = split[i].split("=");
				name_value[0] = name_value[0].replace(/^ /, '');
				checkName = name_value[0].indexOf("_ld__");
				$.cookie(name_value[0], null);
        }
    }
}


function openpopup(url, windowtarget) 
{
    window.open(url, windowtarget, 'width=550,height=600,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0');
}
function openpopupsmall(url, windowtarget) 
{
    window.open(url, windowtarget, 'width=350,height=300,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0');
}


function toggleReceiptDeliveryBox() {
 	var fld = $("#HamperOrder_5_txtRequireReceiptCheck").attr("checked");
 
	if(fld) {
		$(".hamper-receipt").show();
	} else {
		$(".hamper-receipt").hide();
	}
}
