var itens; 
var delivery_tax;

itens = new Array();
delivery_tax = 0;

function formatCurrency( strValue ) 
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	
	dblValue = parseFloat(strValue);

	blnSign = ( dblValue == ( dblValue = Math.abs( dblValue ) ) );
	
	dblValue = Math.floor( dblValue * 100 + 0.50000000001 );
	
	intCents = dblValue % 100;
	
	strCents = intCents.toString();
	
	dblValue = Math.floor( dblValue / 100 ).toString();
	
	if( intCents < 10 ) strCents = "0" + strCents;
	
	for ( var i = 0; i < Math.floor( ( dblValue.length - ( 1 + i ) ) / 3 ); i++ )
	{
		dblValue = dblValue.substring( 0, dblValue.length - ( 4 * i + 3 ) ) + '.' + dblValue.substring( dblValue.length - ( 4 * i + 3 ) );
	}
	
	return ( ( ( blnSign ) ? '' : '-' ) + '' + dblValue + ',' + strCents );
}

function add_item (id, quantity, value, weight) {
	if( isNaN( quantity ) ) {
		return;
	}
	
	itens[id] = { 'quantity' : quantity, 'value' : value, 'weight' : weight };
	
	if(quantity == 0) {
		itens[id] = null;
	
		Effect.DropOut( 'item_' + id );
	}
	
	refresh_cart_values();
}

function rem_item( id )
{
	add_item( id, 0 );
}

function refresh_cart_values() 
{
	var total = 0;
	var weight = 0;
	var item;
	
	for( var i in itens )
	{
		if(isNaN(i) || ! itens[i]) continue;
		
		item 	= itens[i].quantity * itens[i].value;
		weight += itens[i].quantity * itens[i].weight;
		total  += item;
		
		if( $('quantity_' + i) ) 
		{
			$('quantity_' + i + '_value').innerHTML = 'R$' + formatCurrency(item);
			$('quantity_' + i).value = itens[i].quantity;
		}
	}
	
	if( $( 'subtotal_value' ) ) 
	{
		$( 'subtotal_value' ).innerHTML = formatCurrency( total );
		
		total += delivery_tax;

		$( 'total_value' ).innerHTML = 'R$' + formatCurrency( total );
		
		if( $('delivery_value') ) 
		{
			$('delivery_value').innerHTML = formatCurrency( weight );
		}
	}
	
	
	
	if( total == 0 ) 
	{
		Effect.DropOut( 'order' );
		Effect.DropOut( 'subtotal' );
		Effect.DropOut( 'delivery' );
		Effect.DropOut( 'line' );
		Effect.DropOut( 'pay_types' );
		Effect.DropOut( 'total' );
		
		show( 'no_items' );
	}
}  
