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

// this is for handling the drop down (which only shows up
// when doing multi-ship).
function handle_copy_shipping_address_choose() {
	if ($('shipping_dropdown').selectedIndex > 0) {
		if ($('card_choose')) {
			$('card_choose').selectedIndex = 0;
		}
		
		var array_index = $('shipping_dropdown').options[$('shipping_dropdown').selectedIndex].value;
		$('title').value = shipping_addresses[array_index].salutation_id;
		$('first_name').value = shipping_addresses[array_index].first_name;
		$('last_name').value = shipping_addresses[array_index].last_name;
		$('address').value = shipping_addresses[array_index].address;
		$('address2').value = shipping_addresses[array_index].address2;
		$('city').value = shipping_addresses[array_index].city;
		
		if ($('state')) {
			$('state').value = shipping_addresses[array_index].state_id;
		}
		
		$('zip_code').value = shipping_addresses[array_index].zip_code;
		
		if ($('ph1')) {
			$('ph1').value = shipping_addresses[array_index].phone1;
		}
		
		if ($('ph2')) {
			$('ph2').value = shipping_addresses[array_index].phone2;
		}
		
		if ($('ph3')) {
			$('ph3').value = shipping_addresses[array_index].phone3;
		}
		
		if ($('phone_number')) {
			$('phone_number').value = shipping_addresses[array_index].phone;
		}
	} else {
		$('title').value = 0;
		$('first_name').value = '';
		$('last_name').value = '';
		$('address').value = '';
		$('address2').value = '';
		$('city').value = '';
		
		if ($('state')) {
			$('state').value = 0;
		}
		
		$('zip_code').value = '';
		
		if ($('ph1')) {
			$('ph1').value = '';
		}
		
		if ($('ph2')) {
			$('ph2').value = '';
		}
		
		if ($('ph3')) {
			$('ph3').value = '';
		}
		
		if ($('phone_number')) {
			$('phone_number').value = '';
		}
	}
}

// this is for the checkbox (when NOT doing multi-ship).
function handle_copy_shipping_address(shipping_index) {
	if ($('copy_shipping')) {
		if ($('copy_shipping').checked) {
			if ($('card_choose')) {
				$('card_choose').selectedIndex = 0;
			}
			
			$('title').value = shipping_addresses[shipping_index].salutation_id;
			$('first_name').value = shipping_addresses[shipping_index].first_name;
			$('last_name').value = shipping_addresses[shipping_index].last_name;
			$('address').value = shipping_addresses[shipping_index].address;
			$('address2').value = shipping_addresses[shipping_index].address2;
			$('city').value = shipping_addresses[shipping_index].city;
			
			if ($('state')) {
				$('state').value = shipping_addresses[shipping_index].state_id;
			}
			
			$('zip_code').value = shipping_addresses[shipping_index].zip_code;
			
			if ($('ph1')) {
				$('ph1').value = shipping_addresses[shipping_index].phone1;
			}
			
			if ($('ph2')) {
				$('ph2').value = shipping_addresses[shipping_index].phone2;
			}
			
			if ($('ph3')) {
				$('ph3').value = shipping_addresses[shipping_index].phone3;
			}
			
			if ($('phone_number')) {
				$('phone_number').value = shipping_addresses[shipping_index].phone;
			}
		} else {
			$('title').value = 0;
			$('first_name').value = '';
			$('last_name').value = '';
			$('address').value = '';
			$('address2').value = '';
			$('city').value = '';
			
			if ($('state')) {
				$('state').value = 0;
			}
			
			$('zip_code').value = '';
			
			if ($('ph1')) {
				$('ph1').value = '';
			}
			
			if ($('ph2')) {
				$('ph2').value = '';
			}
			
			if ($('ph3')) {
				$('ph3').value = '';
			}
			
			if ($('phone_number')) {
				$('phone_number').value = '';
			}
		}
	}
}

// this will uncheck and unselect the shipping address when
// choosing an existing credit card.  this will also unset
// credit card info.
function handle_choose_credit_card() {
	if ($('card_choose').selectedIndex > 0) {
		if ($('shipping_dropdown')) {
			$('shipping_dropdown').selectedIndex = 0;
		}
		
		if ($('copy_shipping').checked) {
			$('copy_shipping').checked = false;
		}
		
		$('title').value = 0;
		$('first_name').value = '';
		$('last_name').value = '';
		$('address').value = '';
		$('address2').value = '';
		$('city').value = '';
		
		if ($('state')) {
			$('state').value = 0;
		}
		
		$('zip_code').value = '';
		
		if ($('ph1')) {
			$('ph1').value = '';
		}
		
		if ($('ph2')) {
			$('ph2').value = '';
		}
		
		if ($('ph3')) {
			$('ph3').value = '';
		}
		
		if ($('phone_number')) {
			$('phone_number').value = '';
		}
		
		$('email').value = '';
		$('cctype').selectedIndex = 0;
		$('ccnumber').value = '';
		$('ccexp_month').selectedIndex = 0;
		$('exp_year').selectedIndex = 0;
		$('cvv').value = '';
		
		if ($('save')) {
			$('save').checked = false;
		}
	}		
}
