
// Clover Shop L3 version 2.0    http://www.cloverwheel.com


// This function shows the shopping cart table in the shopping cart page.

function Cart (shipping_costs,currency_symbol_before_price,currency_symbol_after_price) {
	
	document.writeln ('<TABLE cellpadding=0 cellspacing=0 border=0 width=500>');
	
	total = 0;
	position = 0;
	
	index = document.cookie.indexOf ("Cart");
	cookie_begin = (document.cookie.indexOf ("=", index) + 1);
	cookie_end = document.cookie.indexOf (";", index);
	
	if (cookie_end == -1) cookie_end = document.cookie.length;
	
	if (cookie_end <= 7) document.writeln ('<TR><TD colspan=3>Your Shopping Cart is empty.</TD></TR>');
	
	cookie = document.cookie.substring (cookie_begin, cookie_end);
	
	for (var i = 0; i <= cookie.length; i++) {
		
		if (cookie.substring (i, i+1) == '[') {
			position_begin = i+1;
			} else if (cookie.substring (i, i+1) == ']') {
			price = (eval (cookie.substring (position_begin, i)));
			total = total + (price*quantity);
			position_end = i;
			position = position+1;
			
			document.writeln ('<TR><TD>'+'Item '+product+'<INPUT type="hidden" name="Product_'+position+'" value="'+product+'"></TD><TD align="right" valign="top" width=100>'+currency_symbol_before_price+''+Correct (price*quantity)+''+currency_symbol_after_price+'<INPUT type="hidden" name="Price_'+position+'" value="'+Correct (price*quantity)+'"></TD><TD valign="top" width=100>&nbsp;&nbsp;<A href="javascript:Remove ('+position+')">Remove</A></TD></TR>');
			
			} else if (cookie.substring (i, i+1) == '|') {
			product = cookie.substring (position_begin, i);
			position_begin = i+1;
			} else if (cookie.substring (i, i+1) == '#') {
			quantity = cookie.substring (position_begin, i);
			position_begin = i+1;
		}
		
	}

	if (shipping_costs) document.writeln ('<TR><TD>Shipping costs</TD><TD align="right" valign="top" width=100>'+currency_symbol_before_price+''+Correct (shipping_costs)+' '+currency_symbol_after_price+'<INPUT type="hidden" name="Shipping_costs" value="'+Correct (shipping_costs)+'"></TD><TD width=100>&nbsp;</TD></TR>');
	
	document.writeln ('<TR><TD colspan=3><HR size=1 width=500></TD></TR>');
	
	document.writeln ('<TR><TD width=330>Total</TD><TD align="right" valign="top" width=100>'+currency_symbol_before_price+''+Correct (total+shipping_costs)+' '+currency_symbol_after_price+'<INPUT type="hidden" name="Total" value="'+Correct (total+shipping_costs)+'"></TD><TD width=100>&nbsp;</TD></TR>');
	
	document.writeln ('</TABLE>');
	
}


// This function adds a product to the shopping cart.

function Add (product, quantity, price) {
	
	index = document.cookie.indexOf ("Cart");
	cookie_begin = (document.cookie.indexOf ("=", index) + 1);
	cookie_end = document.cookie.indexOf (";", index);
	
	if (quantity <= 0) quantity = 1;
	
	if (cookie_end == -1) cookie_end = document.cookie.length;
	
	document.cookie = "Cart="+document.cookie.substring (cookie_begin, cookie_end)+"["+product+"|"+quantity+"#"+price+"]";
	document.location = "s_cart.php3";
	
}


// This function removes a product from the shopping cart.

function Remove (remove_position) {
	
	newcart = null;
	position = 0;
	
	for (var i = 0; i <= cookie.length; i++) {
		
		if (cookie.substring (i, i+1) == '[') {
			position_begin = i+1;
			} else if (cookie.substring (i, i+1) == ']') {
			position_end = i;
			position = position + 1;
			if (position != remove_position) newcart = newcart+'['+cookie.substring (position_begin, position_end)+']';
		}
		
	}
	
	document.cookie="Cart="+newcart;
	document.location = "s_cart.php3";
	
}


// This function fixes the price

function Correct (price) {
	
	if (price <= 0.99) {
		left = '0';
		} else {
		left = parseInt (price);
	}
	
	right = parseInt ((price+.0008-left)*100);
	if (eval (right) <= 9) right='0'+right;
	
	price = left + '.' + right;
	
	return (price);
	
}

