// Define the images for colorway swaps.
var images = [];
images[3] = [];
images[3][3] = 116;
images[3][5] = 128;
images[3][7] = 113;
images[3][8] = 119;
images[3][9] = 122;
images[3][10] = 125;
images[3][11] = 131;
images[3][12] = 134;
images[3][13] = 137;
images[3][15] = 140;
images[3][16] = 143;
images[3][17] = 146;
images[3][18] = 147;
images[3][19] = 161;
images[3][20] = 101;
images[3][21] = 104;
images[3][22] = 107;
images[3][23] = 110;
images[3][24] = 160;
images[3][25] = 153;
images[3][26] = 156;
images[3][27] = 164;
images[3][28] = 167;
images[3][29] = 170;
images[3][30] = 173;
images[3][32] = 179;
images[3][33] = 182;
images[3][34] = 185;
images[3][35] = 188;
images[3][36] = 191;
images[3][37] = 194;
images[3][38] = 197;
images[3][39] = 200;
images[2] = [];
images[2][3] = 115;
images[2][5] = 127;
images[2][7] = 112;
images[2][8] = 118;
images[2][9] = 121;
images[2][10] = 124;
images[2][11] = 130;
images[2][12] = 133;
images[2][13] = 136;
images[2][15] = 139;
images[2][16] = 142;
images[2][17] = 145;
images[2][18] = 148;
images[2][19] = 162;
images[2][20] = 100;
images[2][21] = 103;
images[2][22] = 106;
images[2][23] = 109;
images[2][24] = 151;
images[2][25] = 159;
images[2][26] = 157;
images[2][27] = 165;
images[2][28] = 168;
images[2][29] = 171;
images[2][30] = 174;
images[2][32] = 180;
images[2][33] = 183;
images[2][34] = 186;
images[2][35] = 189;
images[2][36] = 192;
images[2][37] = 195;
images[2][38] = 198;
images[2][39] = 201;
images[1] = [];
images[1][3] = 114;
images[1][5] = 126;
images[1][7] = 111;
images[1][8] = 117;
images[1][9] = 120;
images[1][10] = 123;
images[1][11] = 129;
images[1][12] = 132;
images[1][13] = 135;
images[1][15] = 138;
images[1][16] = 141;
images[1][17] = 144;
images[1][18] = 149;
images[1][19] = 163;
images[1][20] = 99;
images[1][21] = 102;
images[1][22] = 105;
images[1][23] = 108;
images[1][24] = 152;
images[1][25] = 155;
images[1][26] = 158;
images[1][27] = 166;
images[1][28] = 169;
images[1][29] = 172;
images[1][30] = 175;
images[1][32] = 181;
images[1][33] = 184;
images[1][34] = 187;
images[1][35] = 190;
images[1][36] = 193;
images[1][37] = 196;
images[1][38] = 199;
images[1][39] = 202;

// Define the items and their categories.
var items = [];
items[3] = "invitations";
items[5] = "rsvp_cards";
items[7] = "invitations";
items[8] = "invitations";
items[9] = "invitations";
items[10] = "rsvp_cards";
items[11] = "rsvp_cards";
items[12] = "rsvp_cards";
items[13] = "thank_you_cards";
items[15] = "thank_you_cards";
items[16] = "thank_you_cards";
items[17] = "thank_you_cards";
items[18] = "outer_envelopes";
items[19] = "rsvp_envelopes";
items[20] = "direction_cards";
items[21] = "direction_cards";
items[22] = "direction_cards";
items[23] = "direction_cards";
items[24] = "outer_envelopes";
items[25] = "outer_envelopes";
items[26] = "outer_envelopes";
items[27] = "rsvp_envelopes";
items[28] = "rsvp_envelopes";
items[29] = "rsvp_envelopes";
items[30] = "rubber_stamps";
items[32] = "rubber_stamps";
items[33] = "rubber_stamps";
items[34] = "rubber_stamps";
items[35] = "rubber_stamps";
items[36] = "rubber_stamps";
items[37] = "welcome_tote_bags";
items[38] = "welcome_tote_bags";
items[39] = "welcome_tote_bags";

// Define the order of items.
var item_order = [];
item_order[0] = 37;
item_order[1] = 38;
item_order[2] = 39;
item_order[3] = 7;
item_order[4] = 3;
item_order[5] = 8;
item_order[6] = 9;
item_order[7] = 10;
item_order[8] = 5;
item_order[9] = 11;
item_order[10] = 12;
item_order[11] = 35;
item_order[12] = 34;
item_order[13] = 33;
item_order[14] = 30;
item_order[15] = 36;
item_order[16] = 32;
item_order[17] = 13;
item_order[18] = 15;
item_order[19] = 16;
item_order[20] = 17;
item_order[21] = 20;
item_order[22] = 21;
item_order[23] = 22;
item_order[24] = 23;
item_order[25] = 18;
item_order[26] = 24;
item_order[27] = 25;
item_order[28] = 26;
item_order[29] = 19;
item_order[30] = 27;
item_order[31] = 28;
item_order[32] = 29;

// Define the categories.
var categories = ["welcome_tote_bags", "invitations", "rsvp_cards", "rubber_stamps", "thank_you_cards", "direction_cards", "outer_envelopes", "rsvp_envelopes"];

// Define the colorways.
var colorways = [];
colorways[3] = "Fuschia/Gray";
colorways[2] = "Kelly Green/Chartreuse";
colorways[1] = "Navy/Turquoise";

// Define the papers.
var papers = [];
papers[3] = "Smooth";
papers[4] = "Textured";

// Set the default colorway and paper selections globally.  These may be later overridden by melodie-papers.php.
var colorway_id = 3;
var paper_id = 3;

// Set up everything.
var preload = [];
function init (past_items, past_colorway_id, past_paper_id)
{
	// After a pause, begin preloading the images needed for colorway changes.
	preload[0] = new Image();
	preload[0].src = "../../images/blade_expanded.png";
	setTimeout (preload_images, 3000);

	// Set the colorway ID.
	if (past_colorway_id != 0) change_colorway (past_colorway_id);

	// Set the paper ID.
	if (past_paper_id != 0) change_paper (past_paper_id);

	// Load the drawing board.
	if (past_items.length > 0) add_item (past_items);

	// Position and then show the bird.
	position_bird ();
}

// Preload the Melodie item images.
function preload_images ()
{
	// For each colorway...
	for (colorway in images)
	{
		// ...except the currently selected one (since the HTML will take care of that)...
		if (colorway != colorway_id)
		{
			// ...preload the item images.
			for (item_id in images[colorway])
			{
				var image_id = images[colorway][item_id];
				preload[image_id] = new Image();
				preload[image_id].src = "../../images/melodie_items/"+image_id+"_thumb.jpg";
			}
		}
	}
}

// Position the "review" bird.
function position_bird ()
{
	var bird = find_element ("review_suite");
	var divider = find_element ("suite_divider");
	bird.style.left = (find_position (divider)[0] + divider.offsetWidth - 84) + "px";
	bird.style.top = (find_position (divider)[1] - bird.offsetHeight - 10) + "px";
	bird.style.visibility = "visible";
}

// Return an array of the item ID(s) that have been selected for a given category.
function determine_selected_items (category)
{
	if (category == "rubber_stamps") category += "[]";

	// Make a var referring to the category's button(s) so the coming code can be a bit easier to read.
	var button = document.forms["melodie"].elements[category];

	// If there are multiple buttons for the category...
	if (button.length)
	{
		// Loop through the buttons for this category.
		var selections = [];
		for (var i = 0, len = button.length; i < len; i++)
		{
			// If we've found the one that's checked, store or return its value (e.g. the item ID) as necessary.
			if (button[i].type == "radio") if (button[i].checked) return [button[i].value];
			if (button[i].type == "checkbox") if (button[i].checked)
			{
				selections.push (button[i].value);
			}
		}
		if (button[0].type == "checkbox") return selections;

	// Or, if there's just one button for the category, return its value.
	} else {
		if (button.type == "radio") return [button.value];
		if (button.type == "checkbox") return [button.value];
	}
}

// Show or hide the contents of the Melodie blade corresponding with a given category.
function toggle_category (category)
{
	blade = find_element ("blade-"+category);
	arrow = find_element ("arrow-"+category);

	// If visibility hasn't yet been toggled, look to the div's offset to indicate whether or not it's visible.
	if (blade.style.display == '' && blade.offsetWidth != undefined && blade.offsetHeight != undefined) blade.style.display = (blade.offsetWidth != 0 && blade.offsetHeight != 0) ? 'block' : 'none';

	// Flip the blade's content's visbility.
	blade.style.display = (blade.style.display == '' || blade.style.display == 'block') ? 'none' : 'block';

	// Rotate the arrow in the blade header.
	arrow.src = (blade.style.display == '' || blade.style.display == 'block') ? '../../images/blade_expanded.png' : '../../images/blade_collapsed.png';
}

// Change which category is selected.
function change_colorway (new_colorway)
{
	// Adjust the global variable.
	colorway_id = new_colorway;

	// Update the server-side cart.
	http_request = make_http_request ();
	http_request.onreadystatechange = receive_paper_colors; // Start monitoring our request with receive_paper_colors().
	http_request.open("GET", "http://thoughtfulday.com/js/../xml.php?do=change_colorway&colorway_id="+new_colorway, true); // Create the request.
	http_request.send(null);

	// Adjust the relevant text in the drawing board.
	for (item_id in items)
	{
		var target_text = find_element ("ink-"+item_id);
		if (target_text) target_text.innerHTML = colorways[colorway_id];
	}

	// Swap the images in the blades.
	for (item_id in items)
	{
		var image_url = '../../images/melodie_items/'+images[new_colorway][item_id]+'_thumb.jpg';
		if (find_element ("image-"+item_id)) find_element ("image-"+item_id).src = image_url;
		if (find_element ("db_image-"+item_id)) find_element ("db_image-"+item_id).src = image_url;
	}

	// Swap the image in the photo showcase, if present.
	if (find_element("showcase_img")) find_element("showcase_img").src = '../../images/melodie_items/'+images[new_colorway][showcase_item]+'_big.jpg';
}

function receive_paper_colors ()
{
	// Once the request is answered, proceed.
	if (http_request.readyState == 4 && http_request.status == 200)
	{
		var paper_colors = http_request.responseXML.getElementsByTagName("item");
		for (var i = 0; i < paper_colors.length; i++)
		{
			// Extract the information from the XML.
			var item_id = handle_null_node (paper_colors[i].getElementsByTagName("item_id")[0].firstChild);
			var paper_color = handle_null_node (paper_colors[i].getElementsByTagName("paper_color")[0].firstChild);

			// Change the "paper color" text for the given drawing board item.
			var target_text = find_element ("paper_color-"+item_id);
			if (target_text) target_text.innerHTML = paper_color;
		}
	}
}

// Change which paper is selected.
function change_paper (new_paper)
{
	// Adjust the global variable.
	paper_id = new_paper;

	// Update the server-side cart.
	http_request = make_http_request ();
	http_request.open("GET", "http://thoughtfulday.com/js/../xml.php?do=change_paper&paper_id="+new_paper, true); // Create the request.
	http_request.send(null);

	// Adjust the relevant text in the drawing board.
	for (item_id in items)
	{
		var target_text = find_element ("paper-"+item_id);
		if (target_text) target_text.innerHTML = papers[paper_id];
	}
}

function add_item (item_id)
{
	// If we're trying to add an item that already exists (e.g. the person is un-checking a checkbox item), remove it instead.
	if (find_element ("board-"+item_id))
	{
		remove_item (item_id);

	// Otherwise, go ahead and add it.
	} else {
		// If item_id is an array of item IDs, flatten them into a comma-separated string.
		if (item_id.constructor.toString().indexOf("Array") != -1)
		{
			item_id = item_id.join(",");
		}
	
		// Create and send the HTTP request.
		http_request = make_http_request ();
		if (http_request.overrideMimeType) http_request.overrideMimeType("text/xml");
		if (!http_request) return false;
		http_request.onreadystatechange = receive_item_info; // Start monitoring our request with receive_item_info().
		http_request.open("GET", "http://thoughtfulday.com/js/../xml.php?do=add_item&item_id="+item_id+"&colorway_id="+colorway_id, true); // Create the request.
		http_request.send(null);
	}
}

// Monitor the HTTP request for completion.
function receive_item_info ()
{
	// Once the request is answered, proceed.
	if (http_request.readyState == 4 && http_request.status == 200)
	{
		var info = http_request.responseXML.getElementsByTagName("info");
		for (i = 0; i < info.length; i++)
		{
			var item_id = handle_null_node (info[i].getElementsByTagName("item_id")[0].firstChild);
			var image_id = handle_null_node (info[i].getElementsByTagName("image_id")[0].firstChild);
			var item_name = handle_null_node (info[i].getElementsByTagName("item_name")[0].firstChild);
			var item_process = handle_null_node (info[i].getElementsByTagName("item_process")[0].firstChild);
			var paper_color = handle_null_node (info[i].getElementsByTagName("paper_color")[0].firstChild);
			var item_weight = handle_null_node (info[i].getElementsByTagName("item_weight")[0].firstChild);
			var item_material = handle_null_node (info[i].getElementsByTagName("item_material")[0].firstChild);
			var item_dimensions = handle_null_node (info[i].getElementsByTagName("item_dimensions")[0].firstChild);
			var item_notes = handle_null_node (info[i].getElementsByTagName("item_notes")[0].firstChild);
			var item_cost = handle_null_node (info[i].getElementsByTagName("item_cost")[0].firstChild);
			var category = handle_null_node (info[i].getElementsByTagName("category")[0].firstChild);
			var unaltered_category = handle_null_node (info[i].getElementsByTagName("unaltered_category")[0].firstChild);
			var selected_cost = handle_null_node (info[i].getElementsByTagName("selected_cost")[0].firstChild);	

			// If one exists, remove the previous selection for this category.
			if (category != "rubber_stamps")
			{
				// Make an array of items in the given category.
				var items_in_category = [];
				for (item_id_loop in items) if (items[item_id_loop] == category) items_in_category.push(item_id_loop);
//alert (category+" : "+items_in_category);

				// See if any items in the given category exist.
				for (item_in_category in items_in_category)
				{
//alert ("does "+items_in_category[item_in_category]+" exist? "+(find_element("board-"+items_in_category[item_in_category]) ? "true" : "false"));
					var old_one = find_element("board-"+items_in_category[item_in_category]);
					if (old_one) old_one.parentNode.removeChild(old_one);
				}
			}

			// Create the HTML for the quantity menu.
			if (item_cost != "")
			{
				var options_html = "";
				cost_array = item_cost.split (",");
				for (j = 0, len = cost_array.length; j < len; j++)
				{
					cost = cost_array[j].split("@");
					if (cost[1].indexOf(".") == -1) cost[1] = cost[1]+".00";
					options_html += '<option value="'+cost_array[j]+'"';
					if (selected_cost == cost_array[j]) options_html += ' selected="selected"';
					options_html += '>'+cost[0]+' @ $'+cost[1]+'</option>';
				}
			}

			// Create the new item to be added to the drawing board.
			new_div = document.createElement("div");
			new_div.setAttribute("id", "board-"+item_id);
			new_div.setAttribute("class", "db_item");
			new_div.setAttribute("className", "db_item");
			var item_html = '<div class="db_item_image"><img src="http://thoughtfulday.com/js/../images/melodie_items/'+image_id+'_thumb.jpg" alt="'+item_name+'" id="db_image-'+item_id+'" onclick="make_photo(\'item\', '+item_id+');" /><br/><input type="checkbox" name="remove-'+item_id+'" value="remove" onclick="remove_item(\''+item_id+'\');"> remove</div><div class="db_item_data">';
			item_html += '<span class="tinycaps">Category:</span> '+unaltered_category+'<br/>';
			item_html += '<span class="tinycaps">Name:</span> '+item_name+'<br/>';
			if (category != "rubber_stamps") item_html += '<span class="tinycaps">Printing Process:</span> '+item_process+'<br/>';
			if (category != "rubber_stamps" && category != "welcome_tote_bags") item_html += '<span class="tinycaps">Paper Color:</span> <span id="paper_color-'+item_id+'">'+paper_color+'</span><br/>';
			if (category == "rubber_stamps" || category == "welcome_tote_bags") item_html += '<span class="tinycaps">Material:</span> '+item_material+'<br/>';
			item_html += '<span class="tinycaps">Ink Color: </span><span id="ink-'+item_id+'">'+colorways[colorway_id]+'</span><br/>';
			if (category != "rubber_stamps" && category != "welcome_tote_bags") item_html += '<span class="tinycaps">Paper Type: </span><span id="paper-'+item_id+'">'+papers[paper_id]+'</span><br/>';
			if (category != "rubber_stamps" && category != "welcome_tote_bags") item_html += '<span class="tinycaps">Paper Weight:</span> '+item_weight+'</span><br/>';
			item_html += '<span class="tinycaps">Dimensions:</span> '+item_dimensions+'<br/>';
			if (category == "rubber_stamps" || category == "welcome_tote_bags") item_html += '<span class="tinycaps">Notes:</span> '+item_notes+'<br/>';
			item_html += '<span class="tinycaps">Quantity:</span> <select name="cost-'+item_id+'" id="cost-'+item_id+'" onchange="calculate_total (\''+item_id+'\');">'+options_html+'</select></div>';
			new_div.innerHTML = item_html;

			// Figure out which item, if any, appears in the drawing board before the one we're adding.
			var prev_item = -1;
			var found_this = false;
			for (item_id_loop in item_order)
			{
				if (item_order[item_id_loop] == item_id) found_this = true;
				if (find_element ("board-"+item_order[item_id_loop]) && found_this == false) prev_item = item_order[item_id_loop];
			}

			// Add the item to the appropriate place in the drawing board's order.
			if (prev_item != -1)
			{
				// Add the element after the element it should appear after.
				find_element("drawing_board_content").insertBefore(new_div, find_element("board-"+prev_item).nextSibling);

			} else if (prev_item == -1 && find_element("drawing_board_content").getElementsByTagName("div").length >= 1) {
				// Add the element before the first element.
				find_element("drawing_board_content").insertBefore(new_div, find_element(find_element("drawing_board_content").getElementsByTagName("div")[0].id));

			} else {
				// Add the element to the bottom of the drawing board.
				find_element("drawing_board_content").appendChild(new_div);
			}
		}
		calculate_total (item_id);
	}
}

function remove_item (item_id)
{
	// Create and send the HTTP request.
	http_request = make_http_request ();
	http_request.open("GET", "http://thoughtfulday.com/js/../xml.php?do=remove_item&item_id="+item_id, true); // Create the request.
	http_request.send(null);
	
	// Remove the category from the drawing board.
	if (find_element("board-"+item_id)) find_element("board-"+item_id).parentNode.removeChild(find_element("board-"+item_id));

	// Un-choose the button for this item.
	var category = items[item_id];
	if (items[item_id] == "rubber_stamps") category += "[]";
	var buttons = document.forms["melodie"].elements[category];
	if (buttons.length)
	{
		// Loop through the buttons for this category.
		for (i = 0, len = buttons.length; i < len; i++)
		{
			// If we've found the one whose value matches the item ID we're removing, un-check it.
			if (buttons[i].value == item_id) buttons[i].checked = false;
		}

	// Or, if there's just one radio button for the category, un-check it.
	} else return buttons.checked = false;

	// Recalculate the total.
	calculate_total (item_id);
}

function calculate_total (affected_item)
{
	// Reset the cost.
	var cost = 0;

	// Sum all the existing costs.
	for (item_id in items)
	{
		if (find_element ("cost-"+item_id)) cost += parseInt (parseFloat (find_element ("cost-"+item_id).value.split("@")[1], 10).toFixed (2) * 100);
	}
	cost = parseFloat (cost / 100).toFixed (2);
	find_element ("total_price_field").value = "$ "+cost; // Update the cost field.

	// So long as the item's cost menu exists (e.g. this isn't getting called after removing the item), update the server-side cart.
	if (find_element ("cost-"+affected_item))
	{
		http_request = make_http_request ();
		http_request.open("GET", "http://thoughtfulday.com/js/../xml.php?do=change_quantity&item_id="+encodeURIComponent (affected_item)+"&quantity="+encodeURIComponent (find_element ("cost-"+affected_item).value), true); // Create the request.
		http_request.send(null);
	}
}


// Request that the backend e-mail the suite to the user.
function email_suite ()
{
	find_element("save_info_form").elements["submit"].disabled = true;
	find_element("save_info_form").elements["submit"].value = "Saving...";

	// Submit the info.
	save_info (receive_email_suite_info);

	setTimeout ('if (save_info_request_status == "pending") { find_element("save_info_form").elements["submit"].value = "Not saved. Try again."; find_element("save_info_form").elements["submit"].disabled = false; alert ("The server took too long to answer, your suite may not have been saved. Please try again."); }', 10000);
	setTimeout ('if (save_info_request_status == "pending") { save_info_request_status = false; find_element("save_info_form").elements["submit"].value = "Save my suite"; }', 15000);
}

// Receive the results of the request that the backend e-mail the suite to the user.
function receive_email_suite_info ()
{
	// Once the request is answered, proceed.
	if (http_request.readyState == 4 && http_request.status == 200)
	{
		// So long as the browser hasn't already timed out...
		if (save_info_request_status == "pending")
		{
			var result = http_request.responseText;
			if (result == "success")
			{
				find_element("save_info_form").elements["submit"].value = "Saved.";
				find_element("save_info_form").elements["submit"].disabled = false;
				
				setTimeout('find_element("save_info_form").elements["submit"].value = "Save my suite"', 4000);
		
			} else if (result == "invalid e-mail") {
				alert ("Your suite was saved, but an e-mail was not sent because the provided e-mail address doesn't appear to be valid.  Please check the e-mail address and try again.");
				find_element("save_info_form").elements["submit"].value = "Not e-mailed.";
				find_element("save_info_form").elements["submit"].disabled = false;
				
				setTimeout('find_element("save_info_form").elements["submit"].value = "Save my suite"', 4000);
		
			} else if (result == "too frequent e-mails") {
				alert ("Your suite was saved, but an e-mail was not sent because it's already been sent several times recently.");
				find_element("save_info_form").elements["submit"].value = "Not saved.";
				find_element("save_info_form").elements["submit"].disabled = false;
				
				setTimeout('find_element("save_info_form").elements["submit"].value = "Save my suite"', 4000);
			} else {
				alert ("There was a problem saving your suite, it may not have been saved. Please use the \"Contact\" link at the top of the page to contact us for help.");
				save_info_request_status = false;
				return;
			}
			save_info_request_status = true;
		} else save_info_request_status = false;
	}
}

// Start the checkout process.
function place_order ()
{
	save_info (receive_save_info);

	setTimeout ('if (save_info_request_status == "pending") { save_info_request_status = false; alert ("There was a problem saving your suite, it may not have been saved.  Please try again."); }', 10000);
}

// Receive the results of the request that the backend save the user's suite.
function receive_save_info ()
{
	// Once the request is answered, proceed.
	if (http_request.readyState == 4 && http_request.status == 200)
	{
		// So long as the browser hasn't already timed out...
		if (save_info_request_status == "pending")
		{
			var result = http_request.responseText;
			if (result == "success")
			{
				request_paypal_data ();
		
			} else if (result == "invalid e-mail") {
				alert ("The provided e-mail address doesn't appear to be valid.  Please check the e-mail address and try again.");

			} else {
				alert ("There was a problem saving your suite. Please use the \"Contact\" link at the top of the page to contact us for help placing your order.");
				save_info_request_status = false;
				return;
			}
			save_info_request_status = true;
		} else save_info_request_status = false;
	}
}

// Request encrypted PayPal data.
var paypal_request_status;
function request_paypal_data ()
{
	paypal_request_status = "pending";

	// Create and send the HTTP request.
	http_request = make_http_request ();
	if (http_request.overrideMimeType) http_request.overrideMimeType("text/plain");
	if (!http_request) return false;
	http_request.onreadystatechange = receive_paypal_data; // Start monitoring our request with receive_paypal_data().
	http_request.open("GET", "http://thoughtfulday.com/js/../xml.php?do=get_paypal", true); // Create the request.
	http_request.send(null);

	var paypal_request_timed_out = false;
	setTimeout('if (paypal_request_status == "pending") { paypal_request_status = false; alert ("The server took too long to answer.  Please try placing your order again."); }', 15000);
}

// Receive encrypted PayPal data.
function receive_paypal_data ()
{
	// Once the request is answered, proceed.
	if (http_request.readyState == 4 && http_request.status == 200)
	{
		if (paypal_request_status == "pending")
		{
			if (http_request.responseText.match('^-----BEGIN PKCS7-----\n[0-9a-zA-Z\+/=\n]+\n-----END PKCS7-----$') != null)
			{
				find_element("paypal_data").value = http_request.responseText;
				find_element("paypal_form").submit();
				paypal_request_status = true;
			} else {
				paypal_request_status = false;
				alert ("There was a problem assembling your shopping cart. Please use the \"Contact\" link at the top of the page to contact us for help placing your order.");
			}
		}
	}
}

// Try to submit the suite info to the backend.
var save_info_request_status;
function save_info (receiver)
{
	save_info_request_status = "pending";

	// Assemble the form's fields into a URL string.
	form_elements = ["bride", "groom", "bride_parents", "groom_parents", "datetime", "ceremony", "reception", "address", "rsvp", "attire", "reference", "special", "custom_colors", "discuss_enclosures", "email"];
	url = "";
	var form = find_element ("save_info_form");
	for (element in form_elements)
	{
		if (form.elements[form_elements[element]].type != "checkbox")
		{
			url += "&" + form_elements[element] + "=" + encodeURIComponent (form.elements[form_elements[element]].value);
		} else {
			if (form.elements[form_elements[element]].checked == true)
			{
				url += "&" + form_elements[element] + "=yes";
			} else {
				url += "&" + form_elements[element] + "=no";
			}
		}
	}

	// Create and send the HTTP request.
	http_request = make_http_request ();
	if (http_request.overrideMimeType) http_request.overrideMimeType("text/xml");
	if (!http_request) return false;
	http_request.onreadystatechange = receiver; // Start monitoring our request with receive_email_suite_info().
	if (receiver == receive_save_info)
	{
		var todo = "submit_order";
	} else if (receiver == receive_email_suite_info) var todo = "save_info";
	http_request.open("GET", "http://thoughtfulday.com/js/../xml.php?do="+todo+url, true); // Create the request.
	http_request.send(null);
}

// Create the photo showcase.
function make_photo (type, item_id)
{
	// Measure the height and width of the visible area, and where we are scrolled to.
	if (self.innerHeight) { // Non-IE browsers
		window_width = window.innerWidth;
		window_height = window.innerHeight;
		window_scroll_top = window.pageYOffset;
	} else if (document.documentElement && document.documentElement.clientHeight) { // IE 6 strict
		window_width = document.documentElement.clientWidth;
		window_height = document.documentElement.clientHeight;
		window_scroll_top = document.documentElement.scrollTop;
	} else if (document.body) { // IE 7
		window_width = document.body.clientWidth;
		window_height = document.body.clientHeight;
		window_scroll_top = document.body.scrollTop;
	}

	// Make the "loading" indicator.
	if (find_element("loading")) find_element("loading").parentNode.removeChild(find_element("loading"));
	loading_div = document.createElement("div");
	loading_div.setAttribute("id", "loading");
	loading_div.style.left = ((window_width/2)-40)+"px";
	loading_div.style.top = ((window_height/2)-20+window_scroll_top)+"px";
	loading_div.innerHTML = "Loading...";
	document.body.appendChild(loading_div);

	// Make the div containing the photo.
	if (find_element("showcase_div")) find_element("showcase_div").parentNode.removeChild(find_element("showcase_div"));
	new_div = document.createElement("div");
	new_div.setAttribute("id", "showcase_div");

	// Make the close box.
	try
	{
		new_close = document.createElement('<img onclick="close_photo()">');
	} catch (ex) {
		new_close = document.createElement("img");
	}
	new_close.setAttribute("id", "showcase_close");
	new_close.setAttribute("onclick", "close_photo()");
	new_close.setAttribute("src", "http://thoughtfulday.com/js/../images/close.png");
	new_close.style.position = "absolute";
	new_close.style.left = "-15px";
	new_close.style.top = "-15px";
	new_div.appendChild(new_close);

	// Make the photo.
	try
	{
		new_img = document.createElement('<img onclick="close_photo()">');
	} catch (ex) {
		new_img = document.createElement("img");
	}
	new_img.setAttribute("id", "showcase_img");
	new_img.setAttribute("onclick", "close_photo()");

	// Put the photo in the div, then the div in the body.
	new_div.appendChild(new_img);
	document.body.appendChild(new_div);
	showcase_item = item_id;

	// Start preloading the photo, then unhide it when it's finished.
	preload["showcase"] = new Image;
	preload["showcase"].onload = function () { show_photo(type, item_id, preload["showcase"].width, preload["showcase"].height); }

	if (type == "item")
	{
		preload["showcase"].src = "http://thoughtfulday.com/js/../images/melodie_items/"+images[colorway_id][item_id]+"_big.jpg";
	} else if (type == "paper") {
		preload["showcase"].src = "http://thoughtfulday.com/js/../images/melodie_paper/"+item_id+"_big.jpg";
	} else if (type == "colorway") {
		preload["showcase"].src = "http://thoughtfulday.com/js/../images/melodie_colorways/"+item_id+"_big.jpg";
	}
}

// Un-hide the photo showcase.
function show_photo (type, item_id, image_width, image_height)
{
	div_padding = 10;
	div = find_element("showcase_div");
	img = find_element("showcase_img");

	// Now that the image is preloaded, set the source to it.
	if (type == "item")
	{
		img.setAttribute("src", "http://thoughtfulday.com/js/../images/melodie_items/"+images[colorway_id][item_id]+"_big.jpg");
	} else if (type == "paper") {
		img.setAttribute("src", "http://thoughtfulday.com/js/../images/melodie_paper/"+item_id+"_big.jpg");
	} else if (type == "colorway") {
		img.setAttribute("src", "http://thoughtfulday.com/js/../images/melodie_colorways/"+item_id+"_big.jpg");
	}

	// Figure out the dimensions for the photo.
	if (window_width < image_width || window_height < image_height)
	{
		heightIfSetByWidth = window_width * (image_height / image_width);
		widthIfSetByHeight = window_height * (image_width / image_height);
		if (heightIfSetByWidth > window_height)
		{
			height = widthIfSetByHeight * (image_height / image_width);
			width = widthIfSetByHeight;
		} else {
			height = heightIfSetByWidth;
			width = heightIfSetByWidth * (image_width / image_height);
		}
	} else {
		width = image_width;
		height = image_height;
	}

	// Set the photo's position and dimensions.
	img.style.width = ((width * 0.9) - (div_padding * 2))+"px";
	img.style.height = ((height * 0.9) - (div_padding * 2))+"px";
	div.style.left = ((window_width - img.width) / 2 - div_padding)+"px";
	div.style.top = ((window_height - img.height) / 2 + window_scroll_top - div_padding)+"px";

	// Destroy the "loading" div and make the photo visible.
	if (find_element("loading")) find_element("loading").parentNode.removeChild(find_element("loading"));
	div.style.visibility = "visible";

	// When the window gets resized, recreate the photo showcase so that it'll use the new window dimensions.
	window.onresize = function () { make_photo (type, item_id); };
}

// Destroy the photo showcase.
function close_photo ()
{
	window.onresize = null;
	if (find_element("showcase_div")) find_element("showcase_div").parentNode.removeChild(find_element("showcase_div"));
}