var here = this;
var cart = getCart();
var noItemsMessage = "There are currently no items in your cart.";
var expandedItems = {};
var usdExch = .8969;
var euExch = .57144;
var curr = getCookie("curr") ? getCookie("curr") : "AUD";
var shipZone = getCookie("shipZone") ? getCookie("shipZone") : "L";
var shipCountry = getCookie("shipCountry") ? getCookie("shipCountry") : "Australia";
var handlingRate = .015;
var countries = new Array();
with(countries){
	push(["Australia", "L"]);
	push(["Austria", "D"]);
	push(["Belgium", "D"]);
	push(["Canada", "C"]);
	push(["China", "B"]);
	push(["Demark", "D"]);
	push(["Fiji", "B"]);
	push(["Finland", "D"]);
	push(["France", "D"]);
	push(["Germany", "D"]);
	push(["Greece", "D"]);
	push(["Hong Kong, China", "B"]);
	push(["India", "B"]);
	push(["Indonesia", "B"]);
	push(["Ireland", "D"]);
	push(["Israel", "C"]);
	push(["Italy", "D"]);
	push(["Japan", "B"]);
	push(["Korea, South", "B"]);
	push(["Malaysia", "B"]);
	push(["Malta", "D"]);
	push(["Netherlands", "D"]);
	push(["New Caledonia", "B"]);
	push(["New Zealand", "A"]);
	push(["Norway", "D"]);
	push(["Papua New Guinea", "B"]);
	push(["Philippines", "B"]);
	push(["Poland", "D"]);
	push(["Singapore", "B"]);
	push(["Solomon Islands", "B"]);
	push(["South Africa", "D"]);
	push(["Spain", "D"]);
	push(["Sri Lanka", "B"]);
	push(["Sweden", "D"]);
	push(["Switzerland", "D"]);
	push(["Taiwan", "B"]);
	push(["Thailand", "B"]);
	push(["United Kingdom", "D"]);
	push(["USA", "C"]);
	push(["Vietnam", "B"]);
}
var alert_USA = "The United States FDA has specific requirements regarding food importation. More information about <a href='http://www.cfsan.fda.gov/~furls/ovffreg.html' target='_blank'>Facility Registration</a> and <a href='http://www.cfsan.fda.gov/~pn/pnoview.html' target='_blank'>Prior Notice of Imports</a> may be found at their <a href='http://www.fda.gov/' target='_blank'>website</a>.";
var shipObj = {};
shipObj.L = [5, 7.25, 13.5, 18, 24.5, 23.85, 27.3, 30.75, 3.65];
shipObj.A = [6.6, 10.05, 13.5, 16.95, 20.4, 23.85, 27.3, 30.75, 3.65];
shipObj.B = [7.75, 12.25, 16.75, 21.25, 25.75, 30.25, 34.75, 39.25, 4.70];
shipObj.C = [8.8, 14.35, 19.9, 25.45, 31, 36.55, 42.1, 47.65, 6.8];
shipObj.D = [10.3, 17.45, 24.6, 31.75, 38.9, 46.05, 53.2, 60.35, 9];
//
//
function addToSideColumn(val){
	if(!id('side_column')){
		id('content').innerHTML += "<div id='side_column'></div>";
	}
	id("side_column").innerHTML += val;
}
function writeItems(){
	t = "";
	for(i=0; i<items.length; i++){
		if(items[i][5] == ss || ss == "All Products" || (ss == "New Items" && items[i][6])){
			t += writeItem(items[i]);
		}
	}
	id('shop_content').innerHTML = t;
}
function writeItem(src){
	ret = "<div class='item'>";
	ret += "<div class='item_info'>";
	ret += "<h3>"+src[1]+"</h3>";
	ret += "<h4>"+src[8]+"</h4>";
	ret += "<a href='javascript:toggleInfo(\""+src[0]+"\");' class='info_toggle' id='link"+src[0]+"'>"+(expandedItems[src[0]] ? "&laquo; Less Information" : "&raquo; More Information")+"</a><div class='"+(expandedItems[src[0]] ? "info_visible" : "info_hidden")+"' id='"+src[0]+"'>";
	ret += src[2];
	ret += "</div>";
	ret += "<div class='buying'>";
	if(src[3]){
		ret += "<span style='float:right;'>Quantity in cart:</span><div class='quantity'><a href=\"javascript:removeOne('"+src[0]+"');\" class='item_less'></a><div class='item_count'>"+getQuantityInCart(src[0])+"</div><a href=\"javascript:addOne('"+src[0]+"');\" class='item_more'></a></div><span class='item_price'>"+getSymbol()+getPrice(src[3])+getPriceSuffix()+"</span>";//(ss == "Cart" ? "<div class='item_total_price'>"+getSymbol()+getPrice(src[3]*getQuantityInCart(src[0]))+getPriceSuffix()+"</div>" : "")
	} else {
		ret += "This item is available only by special order. Please <a href='?s=contact&r=sales&subject=Vanilla Plants Sales'>enquire here</a> for purchasing information.";
	}
	ret += "</div>";
	ret += "</div>";
	ret += "<div class='item_image'><a href='_pictures/_shop/_full/"+(src[4].split("_thumbs/")[1])+"' rel='lightbox' title='"+src[1]+"'><img src='"+src[4]+"' class='picture' /></a></div>";
	ret += "<div class='clear'></div>";
	ret += "</div>";
	return ret;
}
function writeCart(){
	if(cart.length){
		t = "<div class='cart_items'>";
		form = '<form action="https://www.paypal.com/cgi-bin/webscr" method="post" onsubmit="checkout()" target="_blank">';
		weight = 0;
		for(i=0; i<cart.length; i++){
			details = getItemDetails(cart[i][0]);
			t += writeItem(details);
			weight += details[7]*cart[i][1];
			form += '<input type="hidden" name="item_number_'+(i+1)+'" value="'+details[0]+'"><input type="hidden" name="item_name_'+(i+1)+'" value="'+details[1]+'"><input type="hidden" name="quantity_'+(i+1)+'" value="'+cart[i][1]+'"><input type="hidden" name="amount_'+(i+1)+'" value="'+details[3]+'">';
		}
		t += "<div id='alert_box'></div>";
		t += "</div>";
		form += '<input type="hidden" name="cmd" value="_cart">';
		form += '<input type="hidden" name="upload" value="1">';
		form += '<input type="hidden" name="business" value="admin@daintreevanilla.com">';
        form += '<input type="hidden" name="cpp_header_image" value="http://daintreevanilla.com/_img/logo_for_paypal.gif">';
        form += '<input type="hidden" name="return" value="http://daintreevanilla.com/?s=shop">';
        form += '<input type="hidden" name="cancel_return" value="http://daintreevanilla.com/?s=shop">';
		form += '<input type="hidden" name="currency_code" value="'+curr+'">';
        form += '<input type="hidden" name="shipping_1" value="'+getShipping()+'">';
        form += '<input type="hidden" name="handling_1" value="'+getHandling()+'">';
		t += "<div class='cart_total'>";
			t += "<div class='cart_total_black' style='float:left;'>"+getTotalItems()+"x Items<br />Shipping<br />Handling<br /><strong>Total</strong></div>";
			t += "<div class='cart_total_green'>"+getSymbol()+getTotalPrice()+getPriceSuffix()+"<br />"+getSymbol()+getShipping()+getPriceSuffix()+" <span class='cart_total_black'>to "+writeShipping()+"</span><br />"+getSymbol()+getHandling()+getPriceSuffix()+"<br />"+getSymbol()+getTotal()+getPriceSuffix()+"</div>";
			t += "</div>";
			t += "<div class='big_dotted_line'></div>";
		if(getShipping() != "0.00"){
			t += form+"<input type='image' src='_img/shim.gif' class='side_cart_check_out' /></form>";
		} else {
			t += "<div class='alert'>Your items' combined weight is too heavy to automatically calculate the cost of shipping. You will have to <a href='?s=contact&r=sales&subject=Bulk Order Required'>contact us directly</a> to inquire about this order.</div>";
			
		}
	} else {
		t = "<div class='alert'>There are currently no items in your cart.<br />To remedy this, click on one of the sections above and choose what you like! Might we recommend one of our <a href='?s=shop&ss=New%20Items'>new items</a>?</div>";
	}
	id('shop_content').innerHTML = t;
	id('side_cart').style.margin = "0px";
	id('side_cart').style.visibility = "hidden";
	writeShippingAlert(shipCountry);
}
function writeSideCart(){
	t = "<img src='_img/side_cart_header.gif' />";
	if(cart.length){
		t += "<div id='side_cart_contents'>";
		for(i=0; i<cart.length; i++){
			cItem = getItemDetails(cart[i][0]);
			t += "<div class='item'"+(i/2 == Math.round(i/2) ? "" : " id='dark'")+"><span class='quantity'>"+cart[i][1]+"x</span><span class='item_name'>"+cItem[1]+"</span><span class='price'>"+getSymbol()+getPrice(cItem[3]*cart[i][1])+getPriceSuffix()+"</span></div>";
		}
		t += "</div>";
	} else {
		t += "<div class='side_cart_no_items'>"+noItemsMessage+"</div>";
	}
	t += "<div id='side_cart_total'><span class='total_quantity'>"+getTotalItems()+"</span><span class='total_price'>"+getSymbol()+getTotalPrice()+getPriceSuffix()+"</span><div class='clear'></div></div><a href='?s=shop&ss=Cart' class='side_cart_open_cart'></a><div class='side_cart_bottom'></div>";//<a href='' class='side_cart_check_out'></a>
	if(!id('side_column')){
		id('content').innerHTML += "<div id='side_column'></div>";
	}
	id('side_cart').innerHTML = t;
}
function getTotal(){
	return((parseFloat(getTotalPrice())+parseFloat(getHandling())+parseFloat(getShipping())).toFixed(2));
}
function getHandling(){
	return((parseFloat(getTotalPrice())*handlingRate+.3).toFixed(2));
}
function getShipping(){
	if(shipZone == "L"){
		if(weight < 250){
			cla = 0;
		} else if(weight < 501){
			cla = 1;
		} else if(weight < 1001){
			cla = 2;
		} else if(weight < 1501){
			cla = 3;
		} else if(weight < 2101){
			cla = 4;
		} else {
			cla = -1;
		}
		if(cla > -1){
			return getPrice(shipObj[shipZone][cla]);
		} else {
			return getPrice(0);
		}
	} else {
		if(weight < 250){
			cla = 0;
		} else if(weight < 500){
			cla = 1;
		} else if(weight < 750){
			cla = 2;
		} else if(weight < 1000){
			cla = 3;
		} else if(weight < 1250){
			cla = 4;
		} else if(weight < 1500){
			cla = 5;
		} else if(weight < 1750){
			cla = 6;
		} else if(weight < 2000){
			cla = 7;
		} else if(weight < 20000){
			cla = 8;
		} else {
			cla = -1;
		}
		if(cla == 8){
			baseShipping = shipObj[shipZone][7]+Math.ceil((weight-2000)/500)*shipObj[shipZone][8];
		} else if(cla > -1){
			baseShipping = shipObj[shipZone][cla];
		} else {
			return getPrice(0);
		}
		totalPriceAUS = getTotalPriceAUS();
		if(totalPriceAUS > 100){
			baseShipping += 8.1;
			totalPriceAUS -= 100;
			baseShipping += Math.floor(totalPriceAUS/100)*2.15;
		}
		return getPrice(baseShipping);
	}
}
function writeShipping(){
	t = "<select onchange='changeShipping(this.value);'>";
	for(i=0; i<countries.length; i++){
		t += "<option value='"+countries[i][0]+"' "+(shipCountry == countries[i][0] ? "selected" : "")+">"+countries[i][0]+"</option>";
	}
	t += "</select>";
	return t;
}
function changeShipping(val){
	for(j=0; j<countries.length; j++){
		if(countries[j][0] == val){
			shipCountry = countries[j][0];
			shipZone = countries[j][1];
			setCookie("shipCountry", shipCountry);
			setCookie("shipZone", shipZone);
			updateCart();
			break;
		}
	}
}
function writeShippingAlert(val){
	if(here["alert_"+val]){
		id('alert_box').innerHTML = here["alert_"+val];
		id('alert_box').className = 'alert';
	}
}
function writeCurrency(){
	t = "Currency:<br />";
	t += "<select name='currency' onchange='changeCurrency(this.value)'>";
	t += "<option value='AUD'"+(curr == 'AUD' ? 'selected' : '')+">Australian Dollars (AUD)</option>";
	t += "<option value='USD'"+(curr == 'USD' ? 'selected' : '')+">US Dollars (USD)</option>";
	t += "<option value='EUR'"+(curr == 'EUR' ? 'selected' : '')+">Euros (EUR)</option>";
	t += "</select>";
	id('currency').innerHTML = t;
}
function changeCurrency(val){
	if(curr != val){
		curr = val;
		setCookie("curr", val);
		updateCart();
	}
}
function getPrice(aud){
	if(curr == "USD"){
		return((aud*usdExch).toFixed(2));
	} else if(curr == "EUR"){
		return((aud*euExch).toFixed(2));
	} else {
		return aud.toFixed(2);
	}
}
function getSymbol(){
	if(curr == "USD"){
		return "$";
	} else if(curr == "EUR"){
		return "&euro;";
	} else {
		return "$";
	}
}
function getPriceSuffix(){
	if(curr == "USD"){
		return " <span class='px9'>USD</span>";
	} else if(curr == "EUR"){
		return "";
	} else {
		return " <span class='px9'>AUD</span>";
	}
}
function toggleInfo(ref){
	id("link"+ref).blur();
	if(id(ref).className == "info_hidden"){
		id(ref).className = "info_visible";
		id("link"+ref).innerHTML = "&laquo; Less Information";
		expandedItems[ref] = true;
	} else {
		id(ref).className = "info_hidden";
		id("link"+ref).innerHTML = "&raquo; More Information";
		expandedItems[ref] = false;
	}
}
function getTotalItems(){
	tot = 0;
	for(k=0; k<cart.length; k++){
		tot += cart[k][1];
	}
	return tot;
}
function getTotalPrice(){
	tot = 0;
	for(k=0; k<cart.length; k++){
		tot += parseFloat(getPrice(getItemDetails(cart[k][0])[3]))*cart[k][1];
	}
	return tot.toFixed(2);
}
function getTotalPriceAUS(){
	tot = 0;
	for(k=0; k<cart.length; k++){
		tot += getItemDetails(cart[k][0])[3]*cart[k][1];
	}
	return tot;
}
function getQuantityInCart(ref){
	for(k=0; k<cart.length; k++){
		if(cart[k][0] == ref){
			return cart[k][1];
		}
	}
	return 0;
}
function addOne(ref){
	found = false;
	for(k=0; k<cart.length; k++){
		if(cart[k][0] == ref){
			found = k;
		}
	}
	if(typeof(found) == "number"){
		if(cart[found][1] < 99){
			cart[found][1]++;
		}
	} else {
		cart.push([ref, 1]);
	}
	updateCart();
}
function removeOne(ref){
	for(k=0; k<cart.length; k++){
		if(cart[k][0] == ref){
			if(cart[k][1] > 1){
				cart[k][1]--;
			} else {
				c = confirm("Are you sure you wish to remove this item from your cart?");
				if(c){
					cart.splice(k, 1);
				}
			}
		}
	}
	updateCart();
}
function updateCart(){
	newCart = "";
	for(i=0; i<cart.length; i++){
		newCart += cart[i];
		if(i < cart.length-1) newCart += ",,";
	}
	setCookie("cart", newCart);
	if(ss == "Cart"){
		writeCart();
	} else {
		writeSideCart();
		writeItems();
	}
	writeCurrency();
}
function getCart(){
	full = getCookie("cart");
	if(full){
		existingCart = full.split(",,");
		rCart = new Array();
		for(i=0; i<existingCart.length; i++){
			arrs = existingCart[i].split(",");
			arrs[1] = parseInt(arrs[1]);
			rCart.push(arrs);
		}
		return rCart;
	} else {
		return new Array();
	}
}
function getItemDetails(ref){
	for(l=0; l<items.length; l++){
		if(items[l][0] == ref){
			return items[l];
		}
	}
	return false;
}