var ecomUpdating = false;

function ecomUpdateTotal( )
{
	var total = parseFloat( $('input#ecomOrigPrice').val( ) );
	$('select.ecomVariation').each( function() {
		var sel = $(this).val( );
		var cls = '';
		$(this).find("option").each( function() {
			if( $(this).attr( 'value' ) == sel )
			{
				cls = $(this).attr( 'class' );
				return;
			}
		} );
		var clsarr = cls.split('|');
		total += parseFloat( clsarr[1] );
	} );
	
	var quantity = parseInt( $('input#ecomQuantity').val( ) );
	$('input#ecomTotal').val( (total * quantity).toFixed(2) );
}

function ecomAddItemToCart( )
{
	var item = $('input#ecomItemId').val( );
	var qty = $('input#ecomQuantity').val( );
	var variations = new Array( );
	$('select.ecomVariation').each( function() {
		var sel = $(this).val( );
		var cls = '';
		$(this).find("option").each( function() {
			if( $(this).attr( 'value' ) == sel )
			{
				cls = $(this).attr( 'class' );
				return;
			}
		} );
		var clsarr = cls.split('|');
		variations.push( $(this).attr( 'name' )+'^'+sel+'^'+cls[1] );
	} );
	variations = variations.join( '||' );
	ecomUpdating = true;
	$('#ecomLoader').show( );
	$.ajax( {
		url: 'ecomCartAjax.php',
		dataType: 'json',
		type: 'POST',
		cache: false,
		success: ecomItemAddedToCart,
		data: {
			'action': 'add',
			'item': item,
			'qty': qty,
			'variations': variations
		},
		error: ecomAjaxError
	} );

	return false;
}

function ecomRemoveItem( i )
{
	ecomUpdating = true;
	$('#ecomLoader').show( );
	$.ajax( {
		url: 'ecomCartAjax.php',
		dataType: 'json',
		type: 'POST',
		cache: false,
		success: ecomItemRemovedFromCart,
		data: {
			'action': 'remove',
			'item': i
		},
		error: ecomAjaxError
	} );
}

function ecomUpdateQuantity( )
{
    var input = $(this);
    var tmparr = input.attr( 'id' ).split( '_' );
	ecomUpdating = true;
	$('#ecomLoader').show( );
    $.ajax( {
        url: 'ecomCartAjax.php',
        dataType: 'json',
        type: 'POST',
        cache: false,
        success: ecomCartQuantityUpdated,
        data: {
            'action': 'updatequantities',
            'item': tmparr[1],
            'qty': parseInt( '0'+input.val( ) )
        },
        error: ecomAjaxError
    } );
}

function ecomItemAddedToCart( response )
{
	$('span#ecomItemCount').text( response.count );
	ecomUpdating = false;
	alert( 'The Product was added to your shopping cart.' );
	$('#ecomLoader').hide( );
}

function ecomItemRemovedFromCart( response )
{
	$('span#ecomItemCount').text( response.count );
	$('tr#ecomcartitem_'+response.index).remove( );
	ecomUpdating = false;
	$('#ecomLoader').hide( );
}

function ecomCartQuantityUpdated( response )
{
	if( response.status == -1 )
		$('tr#ecomcartitem_'+response.index).remove( );
	else
	{
		$('td#total_'+response.index).text( '$'+parseFloat(response.cart.items[response.index].total).toFixed(2) );
	}
	ecomUpdating = false;
	$('#ecomLoader').hide( );
}

function ecomAjaxError( xhr, status, error )
{
	alert( status + ' ' + error );
	ecomUpdating = false;
	$('#ecomLoader').hide( );
}

function ecomCheckOut( )
{
	if( ecomUpdating )
		alert( 'There are changes to your cart that are still being saved. Please wait a few seconds and then try again.' );
	else
		window.location.href = 'ecomCheckOut.php';
}

$(document).ready( function( ) {
	ecomUpdateTotal( );
	$('input#ecomQuantity').bind( 'change', ecomUpdateTotal );
	$('select.ecomVariation').bind( 'change', ecomUpdateTotal );
	$('form#ecomItemForm').bind( 'submit', ecomAddItemToCart );
	$('input.ecomQtyInput').bind( 'change', ecomUpdateQuantity );
} );

