function fill_modal_cart_with_page(page) {
	new Ajax.Request(page + '?modal=true', {
		method: 'get',
		parameters: {randid: Math.random()},
		onSuccess: function(transport) {
			darken_bg();
			$('cart_popup_div').innerHTML = transport.responseText;
			$('cart_popup_div').style.visibility = 'visible';
		}
	});
}

function close_modal_cart_window() {
	undarken_bg();
	$('cart_popup_div').innerHTML = '';
	$('cart_popup_div').style.visibility = 'hidden';
}

// 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'
	});
}



//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 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();
}


//save all the items in the cart to agent_sku_list for type SAVED ITEMS
function save_shopping_cart() {
	
	var cartForm = $('cart');
	
	var cartFormTextElements = cartForm.getInputs('text');
	
	var skuQuantityHash = new Hash();
	
	cartFormTextElements.each(function(item) {
		var elementName = item.readAttribute('name');
		if(elementName.startsWith('QUANTITY')) {
			var quantityForSku = item.getValue();
			var cartPositions = elementName.sub('QUANTITY_', '');
			var skuID = $('SKUID_' + cartPositions).getValue();
			
			if(skuQuantityHash.get(skuID)) {
				var currentQtyForSku = skuQuantityHash.get(skuID);
				var newQtyForSku = quantityForSku + currentQtyForSku;
				skuQuantityHash.update({skuID : newQtyForSku});
			} else {
				skuQuantityHash.set(skuID, quantityForSku);
			}
		}
	});
	
	skuQuantityHash.each(function(pair) {
		append_hidden_field('save_shopping_cart_hidden_div', 'QUANTITY_' + pair.key, pair.value);
	});
	
	if(skuQuantityHash.keys().size() > 0) {
		$('save_shopping_cart').submit();	
	} else {
		alert("There are no items to save.")
	}
	
}

//call this when checking out from the view cart page
//page is not modal

//iterator through the form to see if customer has decided to remove any product from the cart
//and set the qty to 0 and remove the input field.  If we dont remove the input field
//when redirecting on success, the system think its a delete action.
function checkout_cart(new_success_page) {
	$('success_page').value = new_success_page;
	
	$A($('cart').getElementsByClassName('remove_item')).each(function(item) {
		
		if(item.checked) {
			$('QUANTITY_' + item.value).value = 0;
		}
		item.remove();
	});
	
	add_hidden_field('cart_hidden_div', 'CHECKOUT_ShoppingCart.x', '');
	$('cart').submit();
}

//call when you forget the password
function checkout_forgot_password(element_id_to_populate, forgot_password_page) {

	clearCartWarningMessage();
	
	new Ajax.Request(forgot_password_page + '?from_cart=true', {
		method: 'get',
		onSuccess: function(transport) {
			$(element_id_to_populate).innerHTML = transport.responseText;
		}
	});
}



//call when checking out
//login, shipping, billing, confirmation, order place page
function checkout(form_id, servlet, element_id_to_populate, error_message_page, error_element_id_to_populate, order_info_page, order_info_element_id_to_populate, success_page_to_redirect) {

	// POST
	new Ajax.Request(servlet, {
		method: 'post',
		parameters: {randid: Math.random()},
		onSuccess: function(transport) {
			
			//we are going to redirect to a thank you page.  It is possible that order submit fails, so we check to see if
			//the response text contains "checkout_confirmation" to determine if we should redirect
			if(form_id == 'checkout_confirmation' && !transport.responseText.include("checkout_confirmation")) 
			{
				window.location = success_page_to_redirect;
			} else {
			
				populate_cart_error_message(error_message_page, error_element_id_to_populate);
				populate_checkout_page(order_info_element_id_to_populate, order_info_page);
				$(element_id_to_populate).update(transport.responseText);
			}
		},
		postBody: get_form_values(form_id)
	});
	
}

function checkout_apply_promo(form_id, servlet, element_id_to_populate, error_message_page, error_element_id_to_populate, order_info_page, order_info_element_id_to_populate) {

	// POST
	new Ajax.Request(servlet, {
		method: 'post',
		parameters: {randid: Math.random()},
		onSuccess: function(transport) {
			populate_cart_error_message(error_message_page, error_element_id_to_populate);
			populate_checkout_page(order_info_element_id_to_populate, order_info_page);
			//alert('got a respone back ' + transport.responseText);
			$('STEP').value = old_step_value;
			$('success_checkout').value = old_success_checkout_value;  
			
		},
		postBody: get_form_values(form_id)
	});
	
}

//show error messages when checking out
function populate_cart_error_message(error_page, error_element_id) {
	
	new Ajax.Request(error_page, {
		method: 'get',
		parameters: {randid: Math.random()},
		onSuccess: function(transport) {
			$(error_element_id).innerHTML = transport.responseText;
			if (transport.responseText.indexOf('Please correct the following errors before continuing')>0){
				// if there are errors, bring page to the top
				window.location='#';
			}
			$(cart_popup_div).style.visibility = 'visible';
		}
	});
}

function populate_checkout_page(element_id_to_populate, page_to_get_content) {

	new Ajax.Request(page_to_get_content, {
		method: 'get',
		parameters: {randid: Math.random()},
		onSuccess: function(transport) {
			//alert('got a respone back ' + transport.responseText);
			$(element_id_to_populate).innerHTML = transport.responseText;
		}
	});
}

/* this is a hack; but we basically remembers the old values before apply promo code, and set it back after promo code is apply. 
 * so that we dont' have to reload the billing section and loose info someone already entered.
 */
var old_step_value; 
var old_success_checkout_value;
function add_promo_code(form_id, servlet, element_id_to_populate, error_message_page, error_element_id_to_populate, new_success_page, order_info_page, order_info_element_id_to_populate) {
	old_step_value = $('STEP').value;
	old_success_checkout_value = $('success_checkout').value; 
	
	$('STEP').value = 10;
	$('success_checkout').value = new_success_page;
	checkout_apply_promo(form_id, servlet, element_id_to_populate, error_message_page, error_element_id_to_populate, order_info_page, order_info_element_id_to_populate);
	
}


function add_promo_code_from_view_cart(new_success_page) {
	$('success_page').value = new_success_page;
	$('STEP').value = 10;
	add_hidden_field('cart_hidden_div', 'promo_code', $('promo_code').value);
	append_hidden_field('cart_hidden_div', 'CHECKOUT_ShoppingCart.x', '');
	$('cart').submit();
}

function copyShipping(){
	if ($('copy_address').checked){
		$('BILLING_CARD_NAME').value = $('TOCOPY_NAME').value;
		$('BILLING_ADDRESS_1').value = $('TOCOPY_ADDRESS').value;
		$('BILLING_ADDRESS_2').value = $('TOCOPY_ADDRESS2').value;
		$('BILLING_CITY').value = $('TOCOPY_CITY').value;
		$('BILLING_STATE_ID').value = $('TOCOPY_STATEID').value;
		$('BILLING_ZIP_CODE').value = $('TOCOPY_ZIPCODE').value;
		$('BILLING_PHONE_NUMBER').value = $('TOCOPY_PHONE_NUMBER').value;
	}else{
		$('BILLING_CARD_NAME').value = '';
		$('BILLING_ADDRESS_1').value = '';
		$('BILLING_ADDRESS_2').value = '';
		$('BILLING_CITY').value = '';
		$('BILLING_STATE_ID').value = 0;
		$('BILLING_ZIP_CODE').value = '';
		$('BILLING_PHONE_NUMBER').value = '';
	}
}

function store_pickup(method){
	if (method == 'pickup'){
		// in store pickup
		$('shipping_method_id').selectedIndex = 0;
		$('pickup_span').style.display = '';
		$('shipping_span').style.display = 'none';
	}else{
		// regular shipping
		$('shipping_method_store').selectedIndex = 0;
		$('pickup_span').style.display = 'none';
		$('shipping_span').style.display = '';
	}
}

function populateStorePickup(){
	
	document.getElementById("storepickupbox").style.cssText="display : block";
}

function clearCartWarningMessage() {
	$('cart_warning').innerHTML = '';
}

