function copy_registrant_info() {
	if ($('same_as_registrant').checked) {
		document.forms['complete_info'].registry_reg2_address.value = document.forms['complete_info'].registry_reg1_address.value;
		document.forms['complete_info'].registry_reg2_address2.value = document.forms['complete_info'].registry_reg1_address2.value;
		document.forms['complete_info'].registry_reg2_city.value = document.forms['complete_info'].registry_reg1_city.value;
		document.forms['complete_info'].registry_reg2_state_id.value = document.forms['complete_info'].registry_reg1_state_id.value;
		document.forms['complete_info'].registry_reg2_zip_code.value = document.forms['complete_info'].registry_reg1_zip_code.value;
		document.forms['complete_info'].registry_reg2_day_phone.value = document.forms['complete_info'].registry_reg1_day_phone.value;
		document.forms['complete_info'].registry_reg2_eve_phone.value = document.forms['complete_info'].registry_reg1_eve_phone.value;
	} else {
		document.forms['complete_info'].registry_reg2_address.value = '';
		document.forms['complete_info'].registry_reg2_address2.value = '';
		document.forms['complete_info'].registry_reg2_city.value = '';
		document.forms['complete_info'].registry_reg2_state_id.value = 0;
		document.forms['complete_info'].registry_reg2_zip_code.value = '';
		document.forms['complete_info'].registry_reg2_day_phone.value = '';
		document.forms['complete_info'].registry_reg2_eve_phone.value = '';
	}
}

function copy_registrant_info_edit() {
	if ($('same_as_registrant').checked) {
		document.forms['edit_info'].registry_reg2_address.value = document.forms['edit_info'].registry_reg1_address.value;
		document.forms['edit_info'].registry_reg2_address2.value = document.forms['edit_info'].registry_reg1_address2.value;
		document.forms['edit_info'].registry_reg2_city.value = document.forms['edit_info'].registry_reg1_city.value;
		document.forms['edit_info'].registry_reg2_state_id.value = document.forms['edit_info'].registry_reg1_state_id.value;
		document.forms['edit_info'].registry_reg2_zip_code.value = document.forms['edit_info'].registry_reg1_zip_code.value;
		document.forms['edit_info'].registry_reg2_day_phone.value = document.forms['edit_info'].registry_reg1_day_phone.value;
		document.forms['edit_info'].registry_reg2_eve_phone.value = document.forms['edit_info'].registry_reg1_eve_phone.value;
	} else {
		document.forms['edit_info'].registry_reg2_address.value = $('original_reg2_address').value;
		document.forms['edit_info'].registry_reg2_address2.value = $('original_reg2_address2').value;
		document.forms['edit_info'].registry_reg2_city.value = $('original_reg2_city').value;
		document.forms['edit_info'].registry_reg2_state_id.value = $('original_reg2_state_id').value;
		document.forms['edit_info'].registry_reg2_zip_code.value = $('original_reg2_zip_code').value;
		document.forms['edit_info'].registry_reg2_day_phone.value = $('original_reg2_day_phone').value;
		document.forms['edit_info'].registry_reg2_eve_phone.value = $('original_reg2_eve_phone').value;
	}
}

// share registry form
function share_registry_form(path) {
	new Ajax.Request(path, {
		method: 'get',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			if (transport.getHeader('ACDC_ERROR') == 't') {
				;
			} else {
				showModalDialog(transport.responseText);
			}
		}
	});
}

function show_consultant(path) {
	new Ajax.Request(path, {
		method: 'get',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			showModalDialog(transport.responseText);
		}
	});
}

function submit_share_registry_form(servlet_path) {
	new Ajax.Request(servlet_path, {
		method: 'post',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			showModalDialog(transport.responseText);
		},
		postBody: get_form_values('share')
	});
}

function check_top_pick(agent_sku_list_item_id) {
	if ($('top_pick_' + agent_sku_list_item_id).checked) {
		$('priority_' + agent_sku_list_item_id).value = 0;
	} else {
		$('priority_' + agent_sku_list_item_id).value = 1;
	}
}

function edit_registry_list() {
	$('edit_registry_list').submit();
}

function remove_item_from_list(agent_sku_list_item_id) {
	$('delete_item_id').value = agent_sku_list_item_id;
	$('delete_registry_item').submit();
}

function hide_no_remaining_items() {
	if ($('hide_remaining_checkbox').checked) {
		for (var i = 0; i < to_hide.length; i++) {
			//$('row_' + to_hide[i]).style.visibility = 'hidden';
			$('row_' + to_hide[i]).fade();
		}
	} else {
		for (var i = 0; i < to_hide.length; i++) {
			//$('row_' + to_hide[i]).style.visibility = 'visible';
			$('row_' + to_hide[i]).appear();
		}
	}
}

// for adding a single Product to cart.
function add_to_registry_cart(product_id, cart_servlet, cart_page, agent_sku_list_item_id) {
	var quantity = $('quantity_' + product_id).value.strip();
	
	if (!is_non_negative_integer(quantity) || quantity == 0) {
		//alert('Total quantity is zero.');
		return;
	}
	
	new Ajax.Request(cart_servlet, {
		method: 'post',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			fill_modal_cart_with_page(cart_page);
		},
		postBody: 'product_id=' + product_id + '&quantity=' + quantity + '&success=/shopping/show_cart'
			+ '&failure=/shopping/show_cart&checkout_manager=customer_order&ADD_SKU_ShoppingCart.x&agent_sku_list_item_id='
			+ agent_sku_list_item_id
	});
}

function order_items_by_dropdown(url) {
	var url = $('item_order').options[$('item_order').selectedIndex].value;
	document.location = url;
}

function print_friendly_list(page) {
	if ($('hide_remaining_checkbox').checked) {
		page = page + '&zero=true';
	}
	
	window.open(page, '', 'height=600,width=800,scrollbars=yes').focus();
}

function show_registry_login_form(login_page, success_page) {
	new Ajax.Request(login_page + '&success=' + escape(success_page), {
		method: 'get',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			showLoginModal(transport.responseText);
		}
	});
}

function close_registry_login_form() {
	if ($('registry_login_popup_div')) {
		$('registry_login_popup_div').innerHTML = '';
		$('registry_login_popup_div').style.visibility = 'hidden';
	}	
}
 
function submit_registry_log_in_form(login_servlet, success_url) {
	new Ajax.Request(login_servlet, {
		method: 'post',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			if (transport.getHeader('ACDC_ERROR') == 't') {
				parent.showLoginModal(transport.responseText);
			} else {
				parent.location.href = success_url;
			}
		},
		postBody: get_form_values('registry_login')
	});
}

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

function show_copy_address_to_picker_edit_reg(page, form_name, destination) {
	new Ajax.Request(page + '?form_name=' + form_name + '&destination=' + destination + '&chooser=true&edit_reg=t', {
		method: 'get',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			$('theform').style.display = 'none';
			$('address_chooser').insert(transport.responseText);
			$('address_chooser').style.display = '';
		}
	});
}

function copy_registry_address_edit_reg(form_name, destination, count) {
	var form = eval('document.forms["' + form_name + '"]');
	
	if (destination == 'reg1') {
		form.registry_reg1_salutation_id.value = $('address_' + count + '_salutation_id').value;
		form.registry_reg1_first_name.value = $('address_' + count + '_first_name').value;
		form.registry_reg1_last_name.value = $('address_' + count + '_last_name').value;
		form.registry_reg1_address.value = $('address_' + count + '_address').value;
		form.registry_reg1_address2.value = $('address_' + count + '_address2').value;
		form.registry_reg1_city.value = $('address_' + count + '_city').value;
		form.registry_reg1_state_id.value = $('address_' + count + '_state_id').value;
		form.registry_reg1_zip_code.value = $('address_' + count + '_zip_code').value;
		form.registry_reg1_day_phone.value = $('address_' + count + '_day_phone').value;
		form.chosen_address_id_reg1.value = $('address_' + count + '_address_id').value;
	} else if (destination == 'reg2') {
		form.registry_reg2_salutation_id.value = $('address_' + count + '_salutation_id').value;
		form.registry_reg2_first_name.value = $('address_' + count + '_first_name').value;
		form.registry_reg2_last_name.value = $('address_' + count + '_last_name').value;
		form.registry_reg2_address.value = $('address_' + count + '_address').value;
		form.registry_reg2_address2.value = $('address_' + count + '_address2').value;
		form.registry_reg2_city.value = $('address_' + count + '_city').value;
		form.registry_reg2_state_id.value = $('address_' + count + '_state_id').value;
		form.registry_reg2_zip_code.value = $('address_' + count + '_zip_code').value;
		form.registry_reg2_day_phone.value = $('address_' + count + '_day_phone').value;
		form.registry_reg2_email.value = $('address_' + count + '_email_address').value;
		form.chosen_address_id_reg2.value = $('address_' + count + '_address_id').value;
	} else if (destination == 'shipping') {
		form.shipping_salutation_id.value = $('address_' + count + '_salutation_id').value;
		form.shipping_first_name.value = $('address_' + count + '_first_name').value;
		form.shipping_last_name.value = $('address_' + count + '_last_name').value;
		form.shipping_address.value = $('address_' + count + '_address').value;
		form.shipping_address2.value = $('address_' + count + '_address2').value;
		form.shipping_city.value = $('address_' + count + '_city').value;
		form.shipping_state_id.value = $('address_' + count + '_state_id').value;
		form.shipping_zip_code.value = $('address_' + count + '_zip_code').value;
		form.chosen_address_id_shipping.value = $('address_' + count + '_address_id').value;
	}
	
	disable_save_address_checkbox(destination);
	$('theform').style.display = '';
	$('address_chooser').style.display = 'none';
	$('address_chooser').update('');
}

function copy_registry_address(form_name, destination, count) {
	var form = eval('document.forms["' + form_name + '"]');
	
	if (destination == 'reg1') {
		form.registry_reg1_salutation_id.value = $('address_' + count + '_salutation_id').value;
		form.registry_reg1_first_name.value = $('address_' + count + '_first_name').value;
		form.registry_reg1_last_name.value = $('address_' + count + '_last_name').value;
		form.registry_reg1_address.value = $('address_' + count + '_address').value;
		form.registry_reg1_address2.value = $('address_' + count + '_address2').value;
		form.registry_reg1_city.value = $('address_' + count + '_city').value;
		form.registry_reg1_state_id.value = $('address_' + count + '_state_id').value;
		form.registry_reg1_zip_code.value = $('address_' + count + '_zip_code').value;
		form.registry_reg1_day_phone.value = $('address_' + count + '_day_phone').value;
		form.chosen_address_id_reg1.value = $('address_' + count + '_address_id').value;
	} else if (destination == 'reg2') {
		form.registry_reg2_salutation_id.value = $('address_' + count + '_salutation_id').value;
		form.registry_reg2_first_name.value = $('address_' + count + '_first_name').value;
		form.registry_reg2_last_name.value = $('address_' + count + '_last_name').value;
		form.registry_reg2_address.value = $('address_' + count + '_address').value;
		form.registry_reg2_address2.value = $('address_' + count + '_address2').value;
		form.registry_reg2_city.value = $('address_' + count + '_city').value;
		form.registry_reg2_state_id.value = $('address_' + count + '_state_id').value;
		form.registry_reg2_zip_code.value = $('address_' + count + '_zip_code').value;
		form.registry_reg2_day_phone.value = $('address_' + count + '_day_phone').value;
		form.registry_reg2_email.value = $('address_' + count + '_email_address').value;
		form.chosen_address_id_reg2.value = $('address_' + count + '_address_id').value;
	} else if (destination == 'shipping') {
		form.shipping_salutation_id.value = $('address_' + count + '_salutation_id').value;
		form.shipping_first_name.value = $('address_' + count + '_first_name').value;
		form.shipping_last_name.value = $('address_' + count + '_last_name').value;
		form.shipping_address.value = $('address_' + count + '_address').value;
		form.shipping_address2.value = $('address_' + count + '_address2').value;
		form.shipping_city.value = $('address_' + count + '_city').value;
		form.shipping_state_id.value = $('address_' + count + '_state_id').value;
		form.shipping_zip_code.value = $('address_' + count + '_zip_code').value;
		form.chosen_address_id_shipping.value = $('address_' + count + '_address_id').value;
	}
	
	disable_save_address_checkbox(destination);
	close_modal();
}

function enable_save_address_checkbox(which) {
	if (which == 'reg1') {
		$('add_reg1_address').disabled = '';
		$('chosen_address_id_reg1').value = '';
	} else if (which == 'reg2') {
		$('add_reg2_address').disabled = '';
		$('chosen_address_id_reg2').value = '';
	} else if (which == 'shipping') {
		$('add_shipping_address').disabled = '';
		$('chosen_address_id_shipping').value = '';
	}
}

function disable_save_address_checkbox(which) {
	if (which == 'reg1') {
		$('add_reg1_address').disabled = 'disabled';
		$('add_reg1_address').checked = false;
	} else if (which == 'reg2') {
		$('add_reg2_address').disabled = 'disabled';
		$('add_reg2_address').checked = false;
	} else if (which == 'shipping') {
		$('add_shipping_address').disabled = 'disabled';
		$('add_shipping_address').checked = false;
	}
}

function edit_address(page) {
	new Ajax.Request(page, {
		method: 'get',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			showModalDialog(transport.responseText);
		}
	});
}

function submit_edit_address(servlet, form_id, success_url) {
	new Ajax.Request(servlet, {
		method: 'post',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			if (transport.getHeader('ACDC_ERROR') == 't') {
				$('modal-dialog_content').innerHTML = transport.responseText;
			} else {
				location.href = success_url;
			}
		},
		postBody: get_form_values(form_id)
	});
}

function consultant_contact(page) {
		window.open(page, '', 'height=600,width=400,scrollbars=yes').focus();
}

function goto_create_registry(page) {
		parent.location.href = page;
}
