// Call this when you need to remove items from the cart and the
// cart page is modal.
function remove_items_from_modal_cart(cartItemGroupKey, cartServlet, cartPage) {

	var postData = 'DELETE_ShoppingCart_' + cartItemGroupKey + '&success=/shopping/show_cart' + '&failure=/shopping/show_cart&checkout_manager=customer_order';

	// POST
	$.post(cartServlet, postData, 
		function(data) {
			fill_modal_cart_with_page(cartPage);
		}
	);
}

//fill in the the modal cart with page
function fill_modal_cart_with_page(cartPage) {

	$.get(cartPage + '?modal=true', 
		{"randid" : Math.random() }, 
		function(data) {
			$('#cart').html(data).show();
			userInfoUpdate();
			window.scrollTo(0,0);
		}
	);
}

var maxItems = 1;
var lowestIndex = 1;

//show the next left item in the cart modal
function showNextLeftItemInCartModal() {

   if(lowestIndex > 1) {
        var indexToShow = lowestIndex - 1;
        var indexToHide = lowestIndex + 2;
        $('#cartModalItem_' + indexToHide).hide();
        $('#cartModalItem_' + indexToShow).show();
        lowestIndex = lowestIndex - 1;
   }

}

//show the next right item in the cart modal
function showNextRightItemInCartModal() {

	if((lowestIndex + 2) < maxItems) {
		var indexToHide = lowestIndex;
		var indexToShow = lowestIndex + 3;
		$('#cartModalItem_' + indexToHide).hide();
		$('#cartModalItem_' + indexToShow).show();
		lowestIndex = lowestIndex + 1;
	}

}


//close the modal cart
function closeCartModal() {
	$('#cart').fadeOut('fast');
}

//add listener to close the modal cart when clicking the close button
$(function(){
	$('#cart a.close').live('click', 
			function() {closeCartModal(); }
	);
	return false;
})


//add hidden variable for removing item from cart page
function removeCartItem(cartItemGroupKey) {
	append_hidden_field('cart_hidden_div', 'DELETE_ShoppingCart_' + cartItemGroupKey, '');
	$('#cartform').submit();
}

//add hidden value for updating cart page
function updateCart() {
	append_hidden_field('cart_hidden_div', 'RECALCULATE_ShoppingCart.x', '');
}

//add hidden value to start checkout from cart page
function cartCheckout() {
	append_hidden_field('cart_hidden_div', 'CHECKOUT_ShoppingCart.x', '');
}

//show item edit on cart page
function showProductEdit(cartItemGroupKey) {
	
	hideAllProductEdits();
	
	var cartItemGroupKey = escapedSuffixKey(cartItemGroupKey);
	
	$('#details_' + cartItemGroupKey).hide();
	$('#options_edit_' + cartItemGroupKey).show();
	$('#update_btn_' + cartItemGroupKey).show();

}

//hide all item edit on cart page
function hideAllProductEdits() {
	
	$("ol[id*='details_']").show();
	$("ol[id*='options_edit_']").hide();
	$("fieldset[id*='update_btn_']").hide();
}



//called when shipping info is different from billing info
function resetShippingInfoForm() {
	$('#SHIPPING_FIRST_NAME').val('');
	$('#SHIPPING_LAST_NAME').val('');
	$('#SHIPPING_ADDRESS_1').val('');
	$('#SHIPPING_ADDRESS_2').val('');
	$('#SHIPPING_CITY').val('');
	$('#SHIPPING_STATE_ID').val('0');
	$('#SHIPPING_ZIP_CODE').val('');
	$('#SHIPPING_PHONE_NUMBER').val('');
}

//called when shipping info is same as billing info
function copyBillingInfoToShippingInfo() {

	if($('#SHIP_TO_BILLING_ADDRESS').attr('checked')) {
		$('#SHIPPING_FIRST_NAME').val($('#BILLING_FIRST_NAME').val());
		$('#SHIPPING_LAST_NAME').val($('#BILLING_LAST_NAME').val());
		$('#SHIPPING_ADDRESS_1').val($('#BILLING_ADDRESS_1').val());
		$('#SHIPPING_ADDRESS_2').val($('#BILLING_ADDRESS_2').val());
		$('#SHIPPING_CITY').val($('#BILLING_CITY').val());
		$('#SHIPPING_STATE_ID').val($('#BILLING_STATE_ID').val());
		$('#SHIPPING_ZIP_CODE').val($('#BILLING_ZIP_CODE').val());
		$('#SHIPPING_PHONE_NUMBER').val($('#BILLING_PHONE_NUMBER').val());
	}
}

//called from checkout page to apply promo code
function addPromoCode() {
		$('#cartform > input[name="STEP"]').val(10);
		$('#cartform > input[name="success"]').val($('#promoCodeForm > input[name="success"]').val());
		$('#cartform > input[name="success_checkout"]').val($('#promoCodeForm > input[name="success_checkout"]').val());
		$('#cartform > input[name="failure"]').val($('#promoCodeForm > input[name="failure"]').val());
		//window.console.debug($('#cartform'));
		$('#cartform').submit();
}

function add_to_wishlist_from_cart_page(positions) {
	if (logged_in) {
		$('#move_to_wishlist_positions').val(positions);
		$('#move_to_wishlist').submit();
	} else {
		document.location = pathInfo["secureUri"] + pathInfo["contextPath"] + '/agent/login?path=' + pathInfo["contextPath"] + '/shopping/move_items_to_list_do' 
			+ '&query=positions%3D' + positions;
	}
}

