
// Cat this when you want to update the cart and the cart
// page is modal.
function update_modal_cart(cart_servlet, cart_page) {
	
	// POST
	new Ajax.Request(cart_servlet, {
		method: 'post',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			fill_modal_cart_with_page(cart_page);
		},
		postBody: 'RECALCULATE_ShoppingCart.x&success=/shopping/show_cart'
			+ '&failure=/shopping/show_cart&checkout_manager=customer_order&' + get_form_values('cart')
	});
}

// Call this when you want to update the cart and the cart
// page is NOT modal.
function update_cart() {
	add_hidden_field('cart_hidden_div', 'RECALCULATE_ShoppingCart.x', '');
	$('cart').submit();
}

// Call this when you need to remove items from the cart
// and the cart page is NOT modal.
function remove_items_from_cart(cart_positions) {
	add_hidden_field('remove_hidden_div', 'DELETE_ShoppingCart_' + cart_positions, '');
	$('remove').submit();
}

// Call this when you need to remove items from the cart and the
// cart page is modal.
function remove_items_from_modal_cart(cart_positions, cart_servlet, cart_page) {
	
	// POST
	new Ajax.Request(cart_servlet, {
		method: 'post',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			fill_modal_cart_with_page(cart_page);
		},
		postBody: 'DELETE_ShoppingCart_' + cart_positions + '&success=/shopping/show_cart'
			+ '&failure=/shopping/show_cart&checkout_manager=customer_order'
	});
}

function fill_modal_cart_with_page(page) {
	new Ajax.Request(page + '?modal=true', {
		method: 'get',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			showModalDialog(transport.responseText);
		}
	});
}

function close_modal_cart() {
	if ($('cart_popup_div')) {
		$('cart_popup_div').innerHTML = '';
		$('cart_popup_div').style.visibility = 'hidden';
	}
}

// called when you are not logged in.
function checkout_modal_cart(cart_servlet, new_success_page) {
	add_hidden_field('cart_hidden_div', 'CHECKOUT_ShoppingCart.x', '');
	
	// POST
	new Ajax.Request(cart_servlet, {
		method: 'post',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			fill_modal_cart_with_page(new_success_page);
		},
		postBody: get_form_values('cart')
	});
}

// called when you are logged in.
function checkout_cart(new_success_page) {
	$('success_page').value = new_success_page;
	add_hidden_field('cart_hidden_div', 'CHECKOUT_ShoppingCart.x', '');
	$('cart').submit();
}

function complete_order() {
	$('confirm').submit();
}

function log_in_for_cart(login_servlet, login_success_page) {

	// POST
	new Ajax.Request(login_servlet, {
		method: 'post',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			if (transport.getHeader('ACDC_ERROR') == 't') {
				showModalDialog(transport.responseText);
			} else {
				document.location = login_success_page;
			}
		},
		postBody: get_form_values('sign_in')
	});
}
