$(document).ready(function () {
	
	$('.ecom_cart_add').live('click', function() {
		var pid = $(this).attr('rel');
		$.ajax({
			url: '/ajax.html',
			data: { context: 'ECom_Site_Ajax', action: 'add_item', plugin: 'ECom', type: 'add', pid: pid },
			type: 'POST',
			dataType: 'json', 
			success: function(data) {
				if (data.status == 'OK') {
					// Display new values
					$('.ecom_cart_total_items').html(data.total_items);
					$('.ecom_cart_total_price').html(data.total_price);
					$('.ecom_cart_add_' + pid + ' > a').html(data.widget_text);
				}
			}
		});
		
		return false;
	});
	
	$('.ecom_cart_delete').live('click', function() {
		var pid = $(this).attr('rel');
		$.ajax({
			url: '/ajax.html',
			data: {context: 'ECom_Site_Ajax', action: 'delete_item', plugin: 'ECom', pid: pid },
			type: 'POST',
			dataType: 'json', 
			success: function(data){
				if (data.status == 'OK') {
					// Update every price fields
					$('.ecom_cart_total_items').html(data.total_items);
					$('.ecom_cart_total_price').html(data.total_price);
					
					// Remove from cart
					$('#ecom_cart_item_line_' + pid).remove();
					
					// Display
					if (data.total_items == 0){
						$('.ecom_cart_total').html(data.total_price);
						$('#ecom_cart_table').remove();
						$('#ecom_cart_empty').show();
					}
				}
			}
		});
												  
		return false;
	});

	$('.ecom_cart_refresh').live('click', function() {
		$('#form_cart').ajaxSubmit({							   
			url: '/ajax.html',
			data: { context: 'ECom_Site_Ajax', action: 'refresh_cart', plugin: 'ECom' },
			type: 'POST',
			dataType: 'json',
			success: function(data) {
				if (data.status == 'OK') {
					if (data.values.qty > 0) {

						// Update subtotals
						for (var x=0; x<data.values.items.length; x++) {
							// Update
							$('#ecom_cart_item_line_' + data.values.items[x].id + ' .ecom_cart_product_total').html(data.values.items[x].price);
						}

						// Remove empty items
						if (data.remove.length > 0) {
							for (var x=0; x<data.remove.length; x++) {
								// Remove
								$('#ecom_cart_item_line_' + data.remove[x]).remove();
							}
						}

						// Update totals
						$('.ecom_cart_total_items').html(data.values.qty);
						$('.ecom_cart_total_price').html(data.values.price);
					
					// Cart is empty, reload page
					} else {
						document.location.reload();
					}
				}
			}
		});
		return false;
	});

	$('.ecom_qty').keypress(function(e) {
		var id = $(this).attr('rel');
	    if (e.keyCode == 13) {
	        $('.ecom_cart_refresh', $(this).closest('tr')).click();
	    }
	});

	$('.ecom_qty').keydown(function(e) {
       // Allow only backspace and delete
        if ( e.keyCode == 46 || e.keyCode == 8 ) {
            // let it happen, don't do anything
        }
        else {
            // Ensure that it is a number and stop the keypress
            if ((e.keyCode < 48 || e.keyCode > 57) && (e.keyCode < 96 || e.keyCode > 105 )) {
                e.preventDefault(); 
            }   
        }
	});
});

function isNumeric(string){

	var numericExpression = /^[0-9]+$/;
	
	if(string.match(numericExpression)) {
	
		return true;
	
	} else {
	
		return false;
	
	}
}

