
// for adding a single Product to cart.
function add_to_cart(product_id, cart_servlet, cart_page) {
	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'
	});
}

// incrementing the quantity.  Also updates subtotal.
function inc_quantity(product_id, dollars) {
	var curr_quantity = $('quantity_' + product_id).value.strip();
	
	if (!is_non_negative_integer(curr_quantity)) {
		curr_quantity = 0;
	}
	
	if (curr_quantity >= '99') {
		return;
	}
	
	var new_quantity = curr_quantity * 1 + 1;
	$('quantity_' + product_id).value = new_quantity;
	var new_pennies = convert_to_pennies(dollars) * new_quantity;
	var new_dollars = convert_to_dollars(new_pennies, true, true);
	$('subtotal_' + product_id).innerHTML = '$' + new_dollars;
}

// decrementing the quantity.  Also updates subtotal.
function dec_quantity(product_id, dollars) {
	var curr_quantity = $('quantity_' + product_id).value.strip();
	
	if (!is_non_negative_integer(curr_quantity)) {
		curr_quantity = 1;
	}

	if (curr_quantity == 0) {
		return;
	}
	
	var new_quantity = curr_quantity - 1;
	$('quantity_' + product_id).value = new_quantity;
	var new_pennies = convert_to_pennies(dollars) * new_quantity;
	var new_dollars = convert_to_dollars(new_pennies, true, true);
	$('subtotal_' + product_id).innerHTML = '$' + new_dollars;
}

// for adding multiple items to the cart - items in pattern or other items in same pattern.
// success and failure pages are not used.
function add_items_to_bag(cart_servlet, cart_page, product_ids_arr) {
	
	// make sure one of the Product's quantity is greater than zero.
	var total_quantity = 0;
	
	for (i = 0; i < product_ids_arr.length; i++) {
		
		var q = $('quantity_' + product_ids_arr[i]).value.strip();
		
		if (is_non_negative_integer(q)) {
			total_quantity = total_quantity + q;
		} else {
			//alert('Ignoring bad quantity.');
		}
	}
	
	if (total_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: 'success=/shopping/show_cart&failure=/shopping/show_cart&checkout_manager=customer_order' + 
			'&ADD_PRODUCTS_ShoppingCart&' + get_form_values('product_cart')
	});
}

// it checks if you are logged in and if not, shows the login page.
// if you are logged in, it tries to add to your Registry.
// this is when you are adding ONE Product at a time!
function registry_login_check(product_id, logged_in_check_page, login_page, current_page, registries_count_page, add_item_servlet) {
	var quantity = 1;
	
	if ($('quantity_' + product_id)) {
		quantity = $('quantity_' + product_id).value.strip();
	}
	
	if (!is_non_negative_integer(quantity) || quantity == 0) {
		//alert('Total quantity is zero.');
		return;
	} 
	
	new Ajax.Request(logged_in_check_page, {
		method: 'get',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			var json = transport.responseJSON;
						
			if (json.logged_in == 'true') {
				add_item_to_registry(product_id, quantity, registries_count_page, add_item_servlet);
			} else {
				show_registry_login_form(login_page, current_page);
			}
		}
	});
}

// if the Agent has multiple Registries, let them pick one.
// if the Agent has one Registry, just add to it.
// if the Agent doesn't have a Registry, present a link to create one.
// this is for when you are adding ONE Product to Registry.
function add_item_to_registry(product_id, quantity, registries_count_page, add_item_servlet) {
	new Ajax.Request(registries_count_page, {
		method: 'get',
		parameters: {randid: Math.random()},
		onSuccess: function(transport) {
			var json = transport.responseJSON;
			
			if (json.length > 1) {
				show_modal_registry_choose_item(add_item_servlet, product_id, quantity);
			} else if (json.length == 1) {
				new Ajax.Request(add_item_servlet, {
					method: 'post',
					parameters: { randid: Math.random() },
					onSuccess: function(transport) {
						
						// for now, we show the same page on success and failure.
						if (transport.getHeader('ACDC_ERROR') == 't') {
							showModalDialog(transport.responseText);
						} else {
							showModalDialog(transport.responseText);
						}
					},
					postBody: 'product_id=' + product_id + '&quantity=' + quantity + '&modal=true&registry_id=' + json[0].registry_id
				});
			} else {
				alert('You have no registry');
			}
		}
	});
}

function close_modal_registry_added() {
	close_modal();
}

// it checks if you are logged in and if not, shows the login page.
// if you are logged in, it tries to add to your Registry.
// this is when you are adding MANY Products at once!
function registry_login_check_many(form_id, logged_in_check_page, login_page, product_page, registries_count_page, add_item_servlet, product_ids_arr) {
	
	// make sure one of the Product's quantity is greater than zero.
	var total_quantity = 0;
	
	for (i = 0; i < product_ids_arr.length; i++) {
		var q = $('quantity_' + product_ids_arr[i]).value.strip();
		
		if (is_non_negative_integer(q)) {
			total_quantity = total_quantity + q;
		} else {
			//alert('Ignoring bad quantity.');
		}
	}
	
	if (total_quantity == 0) {
		//alert('Total quantity is zero.');
		return;
	}

	new Ajax.Request(logged_in_check_page, {
		method: 'get',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			var json = transport.responseJSON;
			
			if (json.logged_in == 'true') {
				add_items_to_registry(form_id, registries_count_page, add_item_servlet);
			} else {
				show_registry_login_form(login_page, product_page);
			}
		}
	});
}

// if the Agent has multiple Registries, let them pick one.
// if the Agent has one Registry, just add to it.
// if the Agent doesn't have a Registry, present a link to create one.
// this is for when you are adding MANY Products to Registry.
function add_items_to_registry(form_id, registries_count_page, add_item_servlet) {
	new Ajax.Request(registries_count_page, {
		method: 'get',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			var json = transport.responseJSON;
			
			if (json.length > 1) {
				show_modal_registry_choose_items(add_item_servlet);
			} else if (json.length == 1) {
				new Ajax.Request(add_item_servlet, {
					method: 'post',
					parameters: { randid: Math.random() },
					onSuccess: function(transport) {
						
						// For now, we show the same page on success and failure.
						if (transport.getHeader('ACDC_ERROR') == 't') {
							showModalDialog(transport.responseText);
						} else {
							showModalDialog(transport.responseText);
						}
					},
					postBody: 'modal=true&' + get_form_values('product_cart') + '&registry_id=' + json[0].registry_id
				});
			} else {
				alert('You have no registry');
			}
		}
	});
}

// this is for showing the Registry chooser when adding ONE Product.
function show_modal_registry_choose_item(add_item_servlet, product_id, quantity) {
	new Ajax.Request(add_item_servlet, {
		method: 'post',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			
			// for now, we show the same page on success and failure.
			if (transport.getHeader('ACDC_ERROR') == 't') {
				showModalDialog(transport.responseText);
			} else {
				showModalDialog(transport.responseText);
			}
		},
		postBody: 'modal=true&product_id=' + product_id + '&quantity=' + quantity + '&step=1'
	});
}

// this is for showing the Registry chooser when adding MULTIPLE Products.
function show_modal_registry_choose_items(add_item_servlet) {
	new Ajax.Request(add_item_servlet, {
		method: 'post',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			
			// for now, we show the same page on success and failure.
			if (transport.getHeader('ACDC_ERROR') == 't') {
				showModalDialog(transport.responseText);
			} else {
				showModalDialog(transport.responseText);
			}
		},
		postBody: 'modal=true&' + get_form_values('product_cart') + '&step=1'
	});
}

function close_modal_registry_choose() {
	close_modal();
}

function add_to_chosen_registry(add_item_servlet, registry_id) {
	new Ajax.Request(add_item_servlet, {
		method: 'post',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			
			// for now, we show the same page on success and failure.
			if (transport.getHeader('ACDC_ERROR') == 't') {
				showModalDialog(transport.responseText);
			} else {
				showModalDialog(transport.responseText);
			}
		},
		postBody: 'modal=true&step=2&registry_id=' + registry_id
	});
}

// if not logged in, show the login box.
function add_to_saved_items(logged_in_check_page, login_page, login_success_page) {
	new Ajax.Request(logged_in_check_page, {
		method: 'get',
		parameters: {randid: Math.random()},
		onSuccess: function(transport) {
			var json = transport.responseJSON;
						
			if (json.logged_in == 'true') {
				$('add_to_saved_items').submit();
			} else {
				login_form(login_page, login_success_page);
			}
		}
	});
}

function show_email_to_friend_form(path) {
	window.open(path, '', 'height=500,width=400,scrollbars=yes').focus();
}

function close_email_to_friend_form() {
	window.close();
}

function submit_email_to_friend_form(servlet_path) {
	var custId=jQuery("form#email_to_friend input[name='your_email']").val();
	new Ajax.Request(servlet_path, {
		method: 'post',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			$('modal-dialog_content').innerHTML = transport.responseText;
			if(transport.responseText.indexOf("Thank you")>=0){
				var order=new Object();
				order["custId"]=custId;
				order["conversionType"]="emailFriend";
			    order["items"]=[new Object({name:"email to friend",category:"lead"})];
			    
				(new MarinTracking()).track(order);
			}
		},
		postBody: get_form_values('email_to_friend')
	});
}

function show_appt_form(page, product_id) {
	show_modal(page + '&product_id=' + product_id);
}

function submit_appt_form(servlet) {
	var custId=jQuery("form#appt input[name='email_address']").val();
	new Ajax.Request(servlet, {
		method: 'post',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			var responseText=transport.responseText;
			showModalDialog(responseText);
			if(responseText.indexOf("Thank you for your request to schedule an appointment")>=0){
				var order=new Object();
				order["custId"]=custId;
				order["conversionType"]="storeAppointment";
			    order["items"]=[new Object({name:"in store appointment",category:"lead"})];
			    
				(new MarinTracking()).track(order);
			}
		},
		postBody: get_form_values('appt')
	});
}

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

function show_tab(url, product_id, div_id) {
	new Ajax.Request(addParameter(addParameter(url, 'product_id=' + product_id), 'div_id=' + div_id), {
		method: 'get',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
		}
	});
}

function show_product_reviews(url, product_id) {
	new Ajax.Request(addParameter(url, 'product_id=' + product_id), {
		method: 'get',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			$('tab-4').innerHTML = transport.responseText;
		}
	});
}

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

function product_in_stock(page, id) {
	new Ajax.Request(page, {
		method: 'get',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			$(id).innerHTML = transport.responseText;
		}
	});
}

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

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

function submit_suggestion_form(servlet) {
	new Ajax.Request(servlet, {
		method: 'post',
		parameters: { randid: Math.random() },
		onSuccess: function(transport) {
			var responseText=transport.responseText;
			showModalDialog(responseText);
		},
		postBody: get_form_values('suggestion')
	});
}
