// 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 ('<div align=left><TABLE cellpadding=0 cellspacing=0 border=0 width=450 id=cart>');
    
    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 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>'+quantity+'&nbsp;x&nbsp;'+product+'<INPUT type="hidden" name="Product_'+position+'" value="'+quantity+' x '+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+')">Cancel</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 ('');
    
    document.writeln ('<TR><TD colspan=3 align=left><HR size=1 width=500 color=red></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></div>');
    
}


// 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 = "miocart.php";
    
}


// 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 = "miocart.php";
    
}


// 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);
    
}


