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

// this hides or shows.  if the checbox exists, use it.
// otherwise, look at the variable.
function hide_no_remaining_items() {
	var hide = true;
	
	if ($('hide_remaining_checkbox')) {
		if ($('hide_remaining_checkbox').checked) {
		} else {
			hide = false;
		}
	} else {
		hide = hide_zero_remaining_items;
	}
	
	if (hide) {
		hide_zero_remaining_items = true;
		
		for (var i = 0; i < to_hide.length; i++) {
			$('row_' + to_hide[i]).fade();
		}
	} else {
		hide_zero_remaining_items = false;
		
		for (var i = 0; i < to_hide.length; i++) {
			$('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;
	
	if (hide_zero_remaining_items) {
		url = url + '&hide_remaining=t';
	}
	
	if (show_thumb_images) {
		url = url + '&show_thumbs=t';
	}
	
	document.location = url;
}

// not used???
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;
}

function show_registry_item_thumbnails(product_docs, img_path, static_url, print_page, show_short_pattern_name) {
	if ($('thumb')) {
		$('thumb').writeAttribute('class', 'showthumbs');
		$('thumb').onclick = function() {
			hide_registry_item_thumbnails(products.response.docs, img_path, static_url, print_page, show_short_pattern_name);
		};
		$('thumb').update('Hide thumbnail item images');
	}
	
	show_thumb_images = true;
	
	for (var i = 0; i < product_docs.length; i++) {
		$('image_' + product_docs[i].id).update('');
		var product_name = product_docs[i].name;
		
		// we display a longer name if we are explicitly told not to
		// or it's not a tableware pattern item.
		if (!show_short_pattern_name || (product_docs[i].pattern_id == 0 || product_docs[i].type != 'T')) {
			product_name = product_name + '<br>by ' + product_docs[i].brand;
			
			if (product_docs[i].pattern_id > 0) {
				product_name = product_docs[i].pattern_name + '<br>' + product_name;
			}
		}
		
		if (product_docs[i].has_image) {
			var onclick = "registry_item_detail_photo('" + contextPath + "/product/index?product_id=" + product_docs[i].id + "&registry_item_detail_photo=t'); go_to_top_of_page(); return false;";
			product_name = '<a href="#" onclick="' + onclick + '">' + product_name + '</a>';
		}
		
		product_name = '<p>' + product_name + '</p>';
		$('image_' + product_docs[i].id).update(product_name);
		
		if (print_page) {
			insert_registry_item_image('image_' + product_docs[i].id, product_docs[i], img_path, static_url, 'after', !print_page);
		} else {
			insert_registry_item_image('image_' + product_docs[i].id, product_docs[i], img_path, static_url, 'after', !print_page);
		}
	}
}

function hide_registry_item_thumbnails(product_docs, img_path, static_url, print_page, show_short_pattern_name) {
	if ($('thumb')) {
		$('thumb').writeAttribute('class', 'hidethumbs');
		$('thumb').onclick = function() {
			show_registry_item_thumbnails(products.response.docs, img_path, static_url, print_page, show_short_pattern_name);
		};
		$('thumb').update('Show thumbnail item images');
	}
	
	show_thumb_images = false;
	
	for (var i = 0; i < product_docs.length; i++) {
		$('image_' + product_docs[i].id).update('');
		var product_name = product_docs[i].name;
		
		// we display a longer name if we are explicitly told not to
		// or it's not a tableware pattern item.
		if (!show_short_pattern_name || (product_docs[i].pattern_id == 0 || product_docs[i].type != 'T')) {
			product_name = product_name + '<br>by ' + product_docs[i].brand;
			
			if (product_docs[i].pattern_id > 0) {
				product_name = product_docs[i].pattern_name + '<br>' + product_name;
			}
		}
		
		if (product_docs[i].has_image) {
			var onclick = "registry_item_detail_photo('" + contextPath + "/product/index?product_id=" + product_docs[i].id + "&registry_item_detail_photo=t'); go_to_top_of_page(); return false;";
			product_name = '<a href="#" onclick="' + onclick + '">' + product_name + '</a>';
		}
		
		product_name = '<p>' + product_name + '</p>';
		$('image_' + product_docs[i].id).update(product_name);
	}
}

function open_registry_item_list_print_page(path) {
	var url = path + '&print=true';
	
	if (hide_zero_remaining_items) {
		url = url + '&hide_remaining=t';
	}
	
	if (show_thumb_images) {
		url = url + '&show_thumbs=t';
	}
	
	window.open(url, '', 'location=0,status=0,scrollbars=1,width=1050,height=600');
}

// container_id: where to put the image
// img_path: ${properties.managedImagePath}/fluid/customers/c905
// static_url: whether to use use static url or dynamic url (true == static)
// position: "before" (existing content in container_id), anything else (after)
// link: whether to include anchor tag or not (true == create link)
function insert_registry_item_image(container_id, product, img_path, static_url, position, link) {
	if ($(container_id)) {
		if (product.has_image) {
			var file_height = 100;
			var file_width = 100;
			var height = 88;
			var width = 88;
			var img_src = img_path + '/' + product.sku + '/generated/' + product.sku
					+ '_default_1_' + file_width + 'x' + file_height + '.jpg';
			var onclick = "registry_item_detail_photo('" + contextPath + "/product/index?product_id=" + product.id + "&registry_item_detail_photo=t'); go_to_top_of_page(); return false;";
		} else {
			var height = 88;
			var width = 88;
			var img_src = '/images/photo_not_available.jpg';
			var onclick = "request_photo('" + contextPath + "/product/index?product_id=" + product.id + "&request_photo=t&show_goto_product_page=t'); go_to_top_of_page(); return false;";
		}
		
		var html_snippet = '';
		
		if (link) {
			html_snippet = html_snippet + '<a href="#" onclick="' + onclick + '">';
		}
		
		html_snippet = html_snippet + '<img height="' + height + '" width="' + height + '" src="' + img_src + '">';
		
		if (link) {
			html_snippet = html_snippet + '</a>';
		}
		
		var existing_html_snippet = $(container_id).innerHTML;
		
		if (position == 'before') {
			$(container_id).update(html_snippet + existing_html_snippet);
		} else {
			$(container_id).insert(html_snippet);
		}
	}
}

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