
function increaseItemCount(cartItemId) {
	$.getJSON(
		"/store/cart/increase-item-count?ajax",
		{cart_item_id: cartItemId},
		function(result) {
			fillCartTable(result);
			fillSelect(
				'#TotalShippingList', 
				result['data']['shipping_method_list']['store_shipping_method_list'],
				result['data']['shipping_method_id']
			);
		}		
	);	
}

function decreaseItemCount(cartItemId) {	
	$.getJSON(
		"/store/cart/decrease-item-count?ajax",
		{cart_item_id: cartItemId},
		function(result) {
			fillCartTable(result);
			/*fillSelect(
				'#TotalShippingList', 
				result['data']['shipping_method_list']['store_shipping_method_list'],
				result['data']['shipping_method_id']
			);*/
		}
	);
}

function setItemCount(cartItemId) {
	var item_count = $('#itemQuantity' + cartItemId).val();
	
	if(intval(item_count)) {
		$('body').css('cursor', 'wait');
		$.getJSON(
			"/store/cart/set-item-count?ajax", {
				cart_item_id: cartItemId,
				cart_item_count: item_count
			},
			function(result) {
				fillCartTable(result);
				fillSelect(
					'#TotalShippingList', 
					result['data']['shipping_method_list']['store_shipping_method_list'],
					result['data']['shipping_method_id']
				);
				$('#itemQuantity' + cartItemId).focus();
				$('body').css('cursor', 'auto');
			}
		);
	}
}

function removeItem(cartItemId) {
	$.getJSON(
		"/store/cart/remove-item?ajax",
		{cart_item_id: cartItemId},
		function(result) {
			fillCartTable(result);
			/*fillSelect(
				'#TotalShippingList', 
				result['data']['shipping_method_list']['store_shipping_method_list'],
				result['data']['shipping_method_id']
			);*/
		}
	);
}

function changeShippingMethod(methodId) {
	$('body').css('cursor', 'wait');
	$.getJSON(
		"/store/cart/change-shipping-method?ajax",
		{method_id: methodId},
		function(result) {
			var shipping_list = $('#TotalShippingList');
			$('option', shipping_list)
				.removeAttr('selected')
				.end()
				.find('[value="' + result.data.method_id + '"]', shipping_list)
				.attr('selected', 'selected');	
			fillSubTotal(result['data']['sub_total_price']);
			$('body').css('cursor', 'auto');
		}
	);
}

function changePackingMethod(methodId) {
	$('body').css('cursor', 'wait');
	$.getJSON(
		"/store/cart/change-packing-method?ajax",
		{method_id: methodId},
		function(result) {
			var packing_list = $('#TotalPackingList');
			$('option', packing_list)
				.removeAttr('selected')
				.end()
				.find('[value="' + result.data.method_id + '"]', packing_list)
				.attr('selected', 'selected');	
			fillSubTotal(result['data']['sub_total_price']);
			$('body').css('cursor', 'auto');
		}
	);
}

/*function hideCheckoutLink() {
	$("#CartCheckoutItemLink").css("display","none");
}

function submitCartForm() {
	$("#PayPalCart").submit();
}*/

function fillCartTable(cart_data) {
	
	if(cart_data['success']) {
		$("#Cart tbody").empty();
		
		if(cart_data['data']) {
			//cart_item_list = cart_product_item_list['data'];
			//$('.CartLink #ItemCount').html(cart_data['data']['total_items_quantity']);
			var cart_row_html = '';
			$(cart_data['data']['item_list']).each(function (i) {
				var cart_product_item = cart_data['data']['item_list'][i];
				cart_row_html = cart_row_html + '<tr class="CartItem">\
					<td><a class="Check" onClick="removeItem(' + cart_product_item.store_cart_item.id + '); return false;"></a></td>\
					<td><img src="/template/store_cart/images/product.jpg" height="53" width="51" alt=""></td>\
					<td width="290">\
						<p>“' + cart_product_item.store_product.short_name + '”</p>\
						<p style="height:30px;overflow-y: hidden;">' + cart_product_item.store_product.description + '</p>\
					</td>\
					<td>$' + number_format(cart_product_item.store_cart_item.price, 0, '.', '') + '</td>\
					<td><input id="itemQuantity' + cart_product_item.store_cart_item.id + '" type="text" value="' + cart_product_item.store_cart_item.quantity + '" maxlength="4" size="3" onkeyup="setItemCount(' + cart_product_item.store_cart_item.id + '); return false;"></td>\
					<td>$' + number_format(cart_product_item.store_cart_item.total_price, 0, '.', '') + '</td>\
					</tr>';
			});
			$('#Cart tbody').append(cart_row_html);
			//preLoad(document.getElementById("CartTableText"), ($("#CartTableContainer").height() - $("#CartTableText").height()));
			
			$('#Total')
				.html('$' + number_format(cart_data['data']['total_price'], 2, '.', '') );
			fillSubTotal(cart_data['data']['sub_total_price']);
		}
	}
}

function fillSubTotal(sub_total_price) {
	$('#SubTotal').html('$' + number_format(sub_total_price, 2, '.', '') );
}

function fillSelect(selector, items_list, selected_id) {
	var select = $(selector);
	select.empty();
	var items_html = '';
	$.each(items_list, function(i) {
		var item = items_list[i];
		if(item['id'] == selected_id) {
			var selected = 'selected="selected"';
		} else {
			var selected = '';
		}

		if(item['price'] <= 0) {
			items_html = items_html + '<option value="' + item['id'] + '" price="' + item['price'] + '" ' + selected + '>' + item['caption'] + '</option>';
		} else {
			items_html = items_html + '<option value="' + item['id'] + '" price="' + item['price'] + '" ' + selected + '>' + item['caption'] + ' - $' + number_format(item['price'], 2, '.', '') + '</option>';
		}
	});
	select.append(items_html);
}

function number_format(number, decimals, dec_point, thousands_sep){
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf ("e");
  var i, z;
  if(eindex > -1){
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  
  if(decimals != null){
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? 
      Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? (dec_point + fractional.substring (1)) : "";
  if(decimals != null && decimals > 0){
    for(i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }
  
  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
                  thousands_sep : null;
  if(thousands_sep != null && thousands_sep != ""){
  for (i = integer.length - 3; i > 0; i -= 3)
   integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }
  return sign + integer + fractional + exponent;
}

function intval(value)
{
    $('<input type="hidden" id="intval_tmp"/>').appendTo("body"); 
    $('#intval_tmp').val(value);
    var value = parseInt($('#intval_tmp').val() * 1);
    $('#intval_tmp').remove();
    return(value);
}

