/*!
 * CKBE Cart Functions
 * http://www.ckbe.com/
 *
 * Copyright 2011, CKBE
 * All Rights Reserved.
 *
 * Requires jQuery JavaScript Library v1.4.2
 * Copyright 2010, John Resig
 * http://www.jquery.com/
 * Released under the MIT or GPL Version 2 licenses.
 *
 * Date: Sun Jun 06 18:57:26 2010
 */

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Global Variables.

var ckbe_default_delivery_company = "EDF";
var ckbe_default_delivery_min = 25;
var ckbe_default_delivery_max = 750;
var ckbe_default_delivery_percentage = 0.05;
var ckbe_last_zip_checked = "";

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Get & Set (Calculated) Value Functions.

function ckbeCart_getValues() {
	
	var subtotal_amount_product = parseFloat(document.getElementById("subtotal_amount_product").value);
	var subtotal_amount_product_WH = parseFloat(document.getElementById("subtotal_amount_product_WH").value);
	var subtotal_amount_product_DS = parseFloat(document.getElementById("subtotal_amount_product_DS").value);
	var subtotal_amount_giftcard = parseFloat(document.getElementById("subtotal_amount_giftcard").value);
	var subtotal_amount_insurance = parseFloat(document.getElementById("subtotal_amount_insurance").value);
	var subtotal_amount_delivery = parseFloat(document.getElementById("subtotal_amount_delivery").value);
	var subtotal_amount_delivery_calculate = document.getElementById("subtotal_amount_delivery_calculate").value;
	var subtotal_amount_discount = parseFloat(document.getElementById("subtotal_amount_discount").value);
	var subtotal_amount = parseFloat(document.getElementById("subtotal_amount").value);
	var tax_calculate = document.getElementById("tax_calculate").value;
	var tax_rate = document.getElementById("tax_rate").value;
	var tax_amount = parseFloat(document.getElementById("tax_amount").value);
	var total_amount = parseFloat(document.getElementById("total_amount").value);
	
	return [subtotal_amount_product, subtotal_amount_product_WH, subtotal_amount_product_DS, subtotal_amount_giftcard, subtotal_amount_insurance, subtotal_amount_delivery, subtotal_amount_delivery_calculate, subtotal_amount_discount, subtotal_amount, tax_calculate, tax_rate, tax_amount, total_amount];
	
}

function ckbeCart_setValues(subtotal_amount_product, subtotal_amount_product_WH, subtotal_amount_product_DS, subtotal_amount_giftcard, subtotal_amount_insurance, subtotal_amount_delivery, subtotal_amount_delivery_calculate, subtotal_amount_discount, tax_calculate, tax_rate) {
	
	var subtotal_amount = 0;
	var tax_amount = 0;
	var total_amount = 0;
	var taxable_amount = 0;
	
	if (document.getElementById("delivery_available").value == "true") {
		
		if (subtotal_amount_product_WH == 0) {
			
			subtotal_amount_delivery = 0;
			
		} else {
			
			if (document.getElementById("delivery_company").value == ckbe_default_delivery_company) {
			
				subtotal_amount_delivery = ckbeCart_calculateDelivery_default(subtotal_amount_product_WH - subtotal_amount_discount);
			
			}
			
			if (document.getElementById("delivery_company").value != ckbe_default_delivery_company && document.getElementById("delivery_company").value != "") {
			
				subtotal_amount_delivery = ckbeCart_calculateDelivery_other();
			
			}
			
		}
		
	}
	
	taxable_amount = (subtotal_amount_product + subtotal_amount_insurance + subtotal_amount_delivery) - subtotal_amount_discount;
	
	if (tax_calculate == "true") {
		
		if (subtotal_amount_product > 0 || subtotal_amount_delivery > 0) {
			
			if (tax_rate.toString().indexOf("|") > -1) {
				
				var tax_list = tax_rate.split(",");
				
				var this_floor = 0.00;
				var this_amount = 0.00;
				var this_tax = 0.00;
				
				for (i = 0; i < tax_list.length; i++) {
					
					if (taxable_amount >= this_floor) {
					
						var this_item = tax_list[i].split("|");
						
						var this_rate = parseFloat(this_item[0] / 100);
						var this_ceiling = parseFloat(this_item[1]);
						
						if (taxable_amount >= this_ceiling) {
							
							this_amount = this_ceiling - this_floor;
							
						}
						
						if (taxable_amount < this_ceiling) {
							
							this_amount = taxable_amount - this_floor;
							
						}
						
						this_tax = this_amount * this_rate;
						tax_amount = tax_amount + this_tax;
						
						this_floor = this_ceiling + 0.01;
						
					}
					
				}
				
				if (taxable_amount > this_floor) {
				
					this_amount = taxable_amount - this_floor;
					
					this_tax = this_amount * 0.07;
					tax_amount = tax_amount + this_tax;
				
				}
				
			} else {
			
				tax_amount = taxable_amount * parseFloat(tax_rate);
			
			}
			
		}
		
	}
	
	document.getElementById("subtotal_amount_discount").value = subtotal_amount_discount;
	
	document.getElementById("subtotal_amount_delivery").value = subtotal_amount_delivery;
	
	if (document.getElementById("delivery_available").value == "true" || subtotal_amount_product_WH == 0) {
		
		document.getElementById("summary_cell_subtotal_amount_delivery").innerHTML = "$" + ckbeCart_formatCurrency(subtotal_amount_delivery);
		
	} else if (document.getElementById("delivery_method_1").checked == true) {
	
		document.getElementById("summary_cell_subtotal_amount_delivery").innerHTML = "$0.00";
	
	} else if (document.getElementById("delivery_available").value == "false") {
		
		document.getElementById("summary_cell_subtotal_amount_delivery").innerHTML = "<a href=\"#\" onclick=\"ckbeCommon_windowModal('/popup/follow_up_shipping_quote.html', 600, 480); return false;\" style=\"font-size:9px;\">For a Quote Click Here</a>";
		
	} else {
		
		document.getElementById("summary_cell_subtotal_amount_delivery").innerHTML = "<span class=\"body-listtext\" style=\"font-size:9px;\">Provide Zip to Calculate</span>";
		
	}
	
	document.getElementById("tax_amount").value = tax_amount;
	document.getElementById("summary_cell_tax_amount").innerHTML = "$" + ckbeCart_formatCurrency(tax_amount);
	
	subtotal_amount = (subtotal_amount_product + subtotal_amount_giftcard + subtotal_amount_insurance + subtotal_amount_delivery) - subtotal_amount_discount;
	
	document.getElementById("subtotal_amount").value = subtotal_amount;
	document.getElementById("summary_cell_subtotal_amount").innerHTML = "$" + ckbeCart_formatCurrency(subtotal_amount);
	
	total_amount = subtotal_amount + tax_amount;
	
	document.getElementById("total_amount").value = total_amount;
	document.getElementById("summary_cell_total_amount").innerHTML = "$" + ckbeCart_formatCurrency(total_amount);
	
}

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Currency Functions.

function ckbeCart_formatCurrency(num) {
	
	num = num.toString().replace(/\$|\,/g,'');
	
	if(isNaN(num))
		
		num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		
	if(cents<10)
	
		cents = "0" + cents;
		
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
			num = num.substring(0,num.length-(4*i+3))+','+
			num.substring(num.length-(4*i+3));
	
	return (((sign)?'':'-') + num + '.' + cents);

}

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Update Count Functions.

function ckbeCart_updateCount_product(recID, newCount, lastCount, pricePer, containerObj, containerGrp, typeCount, allCount, stockType, weightPer, volumePer) {
	
	var newCount_val = 0;
	
	if (newCount != null) {
		newCount_val = document.getElementById(newCount).value;
	}
	
	if (newCount_val == "") {
		newCount_val = 0;
	}
	
	var lastCount_val = parseFloat(document.getElementById(lastCount).value);
	var pricePer_val = parseFloat(document.getElementById(pricePer).value);
	var weightPer_val = parseFloat(document.getElementById(weightPer).value);
	var volumePer_val = parseFloat(document.getElementById(volumePer).value);
	
	var count_change = 0;
	
	var bad_character_found = false;
	var bad_character_list = "abcdefghijklmnopqrstuvwxyz,<.>/?;:'\"[{]}\\|`~!@#$%^&*()-_=+ ";
	
	var message_remove = "Are you sure you want to remove this product from the shopping cart?";
	
	if (newCount != null) {
	
		for (i = 0; i < bad_character_list.length; i++) {
			
			if (document.getElementById(newCount).value.toLowerCase().indexOf(bad_character_list[i]) > -1) {
				bad_character_found = true;
			}
		}
		
	}
	
	if (bad_character_found == true) {
		
		document.getElementById(newCount).value = lastCount_val;
		document.getElementById(newCount).focus();
		
	} else {
		
		if (newCount_val < 1) {
			
			if (confirm(message_remove)) {
				
				document.getElementById(typeCount).value = document.getElementById(typeCount).value - lastCount_val;
				document.getElementById(allCount).value = document.getElementById(allCount).value - lastCount_val;
				
				document.getElementById(containerObj).style.display = "none";
				
				if (document.getElementById(typeCount).value == 0) {
					document.getElementById(containerGrp).style.display = "none";
				}
				
				if (document.getElementById(allCount).value == 0) {
					document.getElementById("cartFull").style.display = "none";
					document.getElementById("cartClear").style.display = "block";
				}
				
				var aURL = "/frame/frame_action_cart_item_remove.html?type=Product&id=" + recID;
				
				ckbeCommon_frameSet("frameAction", aURL);
				
				ckbeCart_updateSummary_product(lastCount_val, newCount_val, pricePer_val, stockType, weightPer_val, volumePer_val);
				
			} else {
				
				if (newCount != null) {
					document.getElementById(newCount).value = lastCount_val;
				}
			
			}
		
		} else {
			
			if (newCount_val != lastCount_val) {
				
				if (newCount_val > lastCount_val) {
					count_change = newCount_val - lastCount_val;
					document.getElementById(typeCount).value = parseInt(document.getElementById(typeCount).value) + count_change;
					document.getElementById(allCount).value = parseInt(document.getElementById(allCount).value) + count_change;
				}
				
				if (newCount_val < lastCount_val) {
					count_change = lastCount_val - newCount_val;
					document.getElementById(typeCount).value = parseInt(document.getElementById(typeCount).value) - count_change;
					document.getElementById(allCount).value = parseInt(document.getElementById(allCount).value) - count_change;
				}
				
				document.getElementById(lastCount).value = newCount_val;
				
				var aURL = "/frame/frame_action_cart_item_update_qty.html?type=Product&id=" + recID + "&qty=" + newCount_val;
				
				ckbeCommon_frameSet("frameAction", aURL);
				
				ckbeCart_updateSummary_product(lastCount_val, newCount_val, pricePer_val, stockType, weightPer_val, volumePer_val);
				
			}
			
		}
		
	}
	
}

function ckbeCart_updateCount_product_byKey(e, recID, newCount, lastCount, pricePer, containerObj, containerGrp, typeCount, allCount, stockType, weightPer, volumePer) {
	
	var key;
	
	if (window.event) {
		key = window.event.keyCode; // IE & Safari.
	} else {
		key = e.which; // Firefox.
	}
	
	if (key == 13) {
		
		if (newCount != null) {
			document.getElementById(newCount).blur();
		}
		
		return false;
		
	} else {
		
		return true;
		
	}
	
}

function ckbeCart_updateCount_giftcard(recID, pricePer, containerObj, containerGrp, typeCount, allCount) {
	
	// DevNote: This is type of object ALWAYS has a quantity of "1" and can ONLY be removed.
	
	var message_remove = "Are you sure you want to remove this gift card from the shopping cart?";
	
	if (confirm(message_remove)) {
				
		document.getElementById(typeCount).value = document.getElementById(typeCount).value - 1;
		document.getElementById(allCount).value = document.getElementById(allCount).value - 1;
		
		document.getElementById(containerObj).style.display = "none";
		
		if (document.getElementById(typeCount).value == 0) {
			document.getElementById(containerGrp).style.display = "none";
		}
		
		if (document.getElementById(allCount).value == 0) {
			document.getElementById("cartFull").style.display = "none";
			document.getElementById("cartClear").style.display = "block";
		}
		
		var aURL = "/frame/frame_action_cart_item_remove.html?type=Gift Card&id=" + recID;
		
		ckbeCommon_frameSet("frameAction", aURL);
		
		ckbeCart_updateSummary_giftcard(pricePer);
		
	}
	
}

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Update Summary Functions.

function ckbeCart_updateSummary_product(lastCount_val, newCount_val, pricePer_val, stockType, weightPer_val, volumePer_val) {
	
	var summary_vals = ckbeCart_getValues();
	
	var subtotal_amount_product = summary_vals[0];
	var subtotal_amount_product_WH = summary_vals[1];
	var subtotal_amount_product_DS = summary_vals[2];
	var subtotal_amount_giftcard = summary_vals[3];
	var subtotal_amount_insurance = summary_vals[4];
	var subtotal_amount_delivery = summary_vals[5];
	var subtotal_amount_delivery_calculate = summary_vals[6];
	var subtotal_amount_discount = summary_vals[7];
	var subtotal_amount = summary_vals[8];
	var tax_calculate = summary_vals[9];
	var tax_rate = summary_vals[10];
	var tax_amount = summary_vals[11];
	var total_amount = summary_vals[12];
	
	var count_change = 0;
	var cost_change = 0;
	var weight_change = 0;
	var volume_change = 0;
	
	var new_weight = 0;
	var new_volume = 0;
	
	var type_change = 0;
	var type_field = "subtotal_amount_product_" + document.getElementById(stockType).value;
	
	if (newCount_val > lastCount_val) {
		
		count_change = newCount_val - lastCount_val;
		
		// Begin Weight
		
		if (weightPer_val != 0) {
			
			weight_change = count_change * weightPer_val;
			
			new_weight = parseFloat(document.getElementById("delivery_weight").value) + weight_change;
			
			document.getElementById("delivery_weight").value = new_weight;
			
		}
		
		// End Weight
		
		// Begin Volume
		
		if (volumePer_val != 0) {
			
			volume_change = count_change * volumePer_val;
			
			new_volume = parseFloat(document.getElementById("delivery_volume").value) + volume_change;
			
			document.getElementById("delivery_volume").value = new_volume;
			
		}
		
		// End Volume
		
		cost_change = count_change * pricePer_val;
		
		subtotal_amount_product = subtotal_amount_product + cost_change;
		
		type_change = parseFloat(document.getElementById(type_field).value) + cost_change;
		
	}
	
	if (newCount_val < lastCount_val) {
		
		count_change = lastCount_val - newCount_val;
		
		// Begin Weight
		
		if (weightPer_val != 0) {
			
			weight_change = count_change * weightPer_val;
			
			new_weight = parseFloat(document.getElementById("delivery_weight").value) - weight_change;
			
			if (new_weight < 0) {
				new_weight = 0;
			}
			
			document.getElementById("delivery_weight").value = new_weight;
			
		}
		
		// End Weight
		
		// Begin Volume
		
		if (volumePer_val != 0) {
			
			volume_change = count_change * volumePer_val;
			
			new_volume = parseFloat(document.getElementById("delivery_volume").value) - volume_change;
			
			if (new_volume < 0) {
				new_volume = 0;
			}
			
			document.getElementById("delivery_volume").value = new_volume;
			
		}
		
		// End Volume
		
		cost_change = count_change * pricePer_val;
		
		subtotal_amount_product = subtotal_amount_product - cost_change;
		
		type_change = parseFloat(document.getElementById(type_field).value) - cost_change;
		
	}
	
	document.getElementById(type_field).value = type_change;
	
	document.getElementById("subtotal_amount_product").value = subtotal_amount_product;
	document.getElementById("summary_cell_subtotal_amount_product").innerHTML = "$" + ckbeCart_formatCurrency(subtotal_amount_product);
	
	// Begin Discount
	
	if (subtotal_amount_discount != 0) {
		
		var discount_valid = ckbeCart_checkDiscount_floor();
		
		if (discount_valid == false) {
			
			subtotal_amount = subtotal_amount + subtotal_amount_discount;
			subtotal_amount_discount = 0;
			
			ckbeCart_clearDiscount_content();
			ckbeCart_clearDiscount_search();
			
			if (document.getElementById("delivery_available").value == "true") {
				
				ckbeCart_setValues(subtotal_amount_product, subtotal_amount_product_WH, subtotal_amount_product_DS, subtotal_amount_giftcard, subtotal_amount_insurance, subtotal_amount_delivery, subtotal_amount_delivery_calculate, subtotal_amount_discount, tax_calculate, tax_rate);
				
			}
			
		}
		
	}
	
	// End Discount
	
	if (document.getElementById("delivery_available").value == "true") {
		
		ckbeCart_setDelivery();
		
	} else {
		
		ckbeCart_setValues(subtotal_amount_product, subtotal_amount_product_WH, subtotal_amount_product_DS, subtotal_amount_giftcard, subtotal_amount_insurance, subtotal_amount_delivery, subtotal_amount_delivery_calculate, subtotal_amount_discount, tax_calculate, tax_rate);
		
	}
	
}

function ckbeCart_updateSummary_giftcard(pricePer) {
	
	var summary_vals = ckbeCart_getValues();
	
	var subtotal_amount_product = summary_vals[0];
	var subtotal_amount_product_WH = summary_vals[1];
	var subtotal_amount_product_DS = summary_vals[2];
	var subtotal_amount_giftcard = summary_vals[3];
	var subtotal_amount_insurance = summary_vals[4];
	var subtotal_amount_delivery = summary_vals[5];
	var subtotal_amount_delivery_calculate = summary_vals[6];
	var subtotal_amount_discount = summary_vals[7];
	var subtotal_amount = summary_vals[8];
	var tax_calculate = summary_vals[9];
	var tax_rate = summary_vals[10];
	var tax_amount = summary_vals[11];
	var total_amount = summary_vals[12];
	
	var cost_change = pricePer;
	
	subtotal_amount_giftcard = subtotal_amount_giftcard - cost_change;
	
	if (subtotal_amount_giftcard == 0) {
		document.getElementById("summary_row_subtotal_amount_giftcard").style.display = "none";	
	}
	
	document.getElementById("subtotal_amount_giftcard").value = subtotal_amount_giftcard;
	document.getElementById("summary_cell_subtotal_amount_giftcard").innerHTML = "$" + ckbeCart_formatCurrency(subtotal_amount_giftcard);
	
	ckbeCart_setValues(subtotal_amount_product, subtotal_amount_product_WH, subtotal_amount_product_DS, subtotal_amount_giftcard, subtotal_amount_insurance, subtotal_amount_delivery, subtotal_amount_delivery_calculate, subtotal_amount_discount, tax_calculate, tax_rate);
	
}

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Set Tax, Delivery and Discount Value Functions.

function ckbeCart_setTax(obj) {
	
	var val = ckbeForm_stringTrim(obj.value);
	
	var summary_vals = ckbeCart_getValues();
	
	var subtotal_amount_product = summary_vals[0];
	var subtotal_amount_product_WH = summary_vals[1];
	var subtotal_amount_product_DS = summary_vals[2];
	var subtotal_amount_giftcard = summary_vals[3];
	var subtotal_amount_insurance = summary_vals[4];
	var subtotal_amount_delivery = summary_vals[5];
	var subtotal_amount_delivery_calculate = summary_vals[6];
	var subtotal_amount_discount = summary_vals[7];
	var subtotal_amount = summary_vals[8];
	var tax_calculate = summary_vals[9];
	var tax_rate = summary_vals[10];
	var tax_amount = summary_vals[11];
	var total_amount = summary_vals[12];
	
	<!--- DevNote: "document.getElementById("delivery_method_1")" is the radio button for pick-up which are always taxed. --->
	if (val.length == 2 && val.toLowerCase() == document.getElementById("tax_state").value.toLowerCase() || document.getElementById("delivery_method_1").checked == true) {
		
		tax_calculate = "true";
		
		document.getElementById("tax_calculate").value = true;
		
	} else {
		
		tax_calculate = "false";
		
		document.getElementById("tax_calculate").value = false;
	
	}
	
	ckbeCart_setValues(subtotal_amount_product, subtotal_amount_product_WH, subtotal_amount_product_DS, subtotal_amount_giftcard, subtotal_amount_insurance, subtotal_amount_delivery, subtotal_amount_delivery_calculate, subtotal_amount_discount, tax_calculate, tax_rate);
	
}

function ckbeCart_setTax_byKey(e, obj) {
	
	if (document.getElementById("acp_zip").value == "") {
		
		ckbeCart_setTax(obj);
		
	}
	
}

function ckbeCart_setDelivery() {
	
	// Show/Hide Elements
	
	document.getElementById("div_delivery_upgrade_title").style.display = "none";
	document.getElementById("div_delivery_upgrade_group").style.display = "none";
	
	document.getElementById("delivery_upgrade_room").checked = false;
	document.getElementById("delivery_upgrade_room_amount").value = "";
	document.getElementById("div_delivery_upgrade_room_label").innerHTML = "";
	document.getElementById("summary_delivery_upgrade_room").style.display = "none";
	
	document.getElementById("delivery_upgrade_debris").checked = false;
	document.getElementById("delivery_upgrade_debris_amount").value = "";
	document.getElementById("div_delivery_upgrade_debris_label").innerHTML = "";
	document.getElementById("summary_delivery_upgrade_debris").style.display = "none";
	
	document.getElementById("delivery_upgrade_assembly").checked = false;
	document.getElementById("delivery_upgrade_assembly_amount").value = "";
	document.getElementById("div_delivery_upgrade_assembly_label").innerHTML = "";
	document.getElementById("summary_delivery_upgrade_assembly").style.display = "none";
	
	document.getElementById("div_delivery_upgrade_saturday").style.display = "none";
	document.getElementById("delivery_upgrade_saturday_amount").value = "";
	document.getElementById("div_delivery_upgrade_saturday_label").innerHTML = "";
	document.getElementById("summary_delivery_upgrade_saturday").style.display = "none";
	
	document.getElementById("delivery_time").value = "";
	document.getElementById("delivery_time").options.length = 0;
	document.getElementById("delivery_time_last_selected").value = "";
	document.getElementById("summary_delivery_time").style.display = "none";
	
	var summary_vals = ckbeCart_getValues();
	
	var subtotal_amount_product = summary_vals[0];
	var subtotal_amount_product_WH = summary_vals[1];
	var subtotal_amount_product_DS = summary_vals[2];
	var subtotal_amount_giftcard = summary_vals[3];
	var subtotal_amount_insurance = summary_vals[4];
	var subtotal_amount_delivery = summary_vals[5];
	var subtotal_amount_delivery_calculate = summary_vals[6];
	var subtotal_amount_discount = summary_vals[7];
	var subtotal_amount = summary_vals[8];
	var tax_calculate = summary_vals[9];
	var tax_rate = summary_vals[10];
	var tax_amount = summary_vals[11];
	var total_amount = summary_vals[12];
	
	var default_delivery_found = document.getElementById("default_delivery_found").value;
	var other_delivery_found = document.getElementById("other_delivery_found").value;
	
	var default_cost = "";
	var other_cost = "";
	
	if (default_delivery_found == "false" && other_delivery_found == "false") {
		
		subtotal_amount_delivery = 0;
		
		ckbeCart_clearDelivery_content();
		
	}
	
	if (default_delivery_found == "true" && other_delivery_found == "false") {
		
		subtotal_amount_delivery = ckbeCart_calculateDelivery_default(subtotal_amount_product_WH - subtotal_amount_discount);
		
		ckbeCart_showDelivery_content(document.getElementById("default_delivery_company").value, document.getElementById("default_delivery_zone").value, document.getElementById("default_delivery_date_list").value, delivery_date_list_label = document.getElementById("default_delivery_date_list_label").value);
		
	}
	
	if (default_delivery_found == "false" && other_delivery_found == "true") {
		
		subtotal_amount_delivery = ckbeCart_calculateDelivery_other();
		
		ckbeCart_showDelivery_content(document.getElementById("other_delivery_company").value, document.getElementById("other_delivery_zone").value, document.getElementById("other_delivery_date_list").value, delivery_date_list_label = document.getElementById("other_delivery_date_list_label").value);
		
	}
	
	if (default_delivery_found == "no_date" && other_delivery_found == "false") {
		
		ckbeCart_clearDelivery_content();
		
	}
	
	if (default_delivery_found == "no_date" && other_delivery_found == "true") {
		
		subtotal_amount_delivery = ckbeCart_calculateDelivery_other();
		
		ckbeCart_showDelivery_content(document.getElementById("other_delivery_company").value, document.getElementById("other_delivery_zone").value, document.getElementById("other_delivery_date_list").value, delivery_date_list_label = document.getElementById("other_delivery_date_list_label").value);
		
	}
	
	if (default_delivery_found == "true" && other_delivery_found == "true") {
		
		default_cost = ckbeCart_calculateDelivery_default(subtotal_amount_product_WH - subtotal_amount_discount);
		other_cost = ckbeCart_calculateDelivery_other();
		
		if (default_cost <= other_cost) {
			
			subtotal_amount_delivery = default_cost;
			
			ckbeCart_showDelivery_content(document.getElementById("default_delivery_company").value, document.getElementById("default_delivery_zone").value, document.getElementById("default_delivery_date_list").value, delivery_date_list_label = document.getElementById("default_delivery_date_list_label").value);
			
		} else {
			
			subtotal_amount_delivery = other_cost;
			
			ckbeCart_showDelivery_content(document.getElementById("other_delivery_company").value, document.getElementById("other_delivery_zone").value, document.getElementById("other_delivery_date_list").value, delivery_date_list_label = document.getElementById("other_delivery_date_list_label").value);
			
		}
		
	}
	
	// Begin Taxes
	
		if (document.getElementById("tax_calculate").value == "true") {
			
			document.getElementById("tax_calculate").value = true;
			tax_calculate = "true";
			
		} else {
		
			if (document.getElementById("acp_state").value == document.getElementById("tax_state").value) {
				
				document.getElementById("tax_calculate").value = true;
				tax_calculate = "true";
				
			} else {
				
				document.getElementById("tax_calculate").value = false;
				tax_calculate = "false";
				
			}
		
		}
		
		document.getElementById("tax_calculate").value = tax_calculate;
	
	// End Taxes
	
	document.getElementById("subtotal_amount_delivery").value = subtotal_amount_delivery;
	ckbeCart_setValues(subtotal_amount_product, subtotal_amount_product_WH, subtotal_amount_product_DS, subtotal_amount_giftcard, subtotal_amount_insurance, subtotal_amount_delivery, subtotal_amount_delivery_calculate, subtotal_amount_discount, tax_calculate, tax_rate);
	document.getElementById("div_activity_delivery").style.display = "none";
	ckbeForm_errorClear_simple("fieldErrorMarker_delivery_date");
	
	// Show/Hide Bubble
	
	if (document.getElementById("delivery_company").value == ckbe_default_delivery_company) {
		
		document.getElementById("bubble_shipping_info").style.visibility = "visible";
		
	} else {
		
		document.getElementById("bubble_shipping_info").style.visibility = "hidden";
		
	}
	
}

function ckbeCart_setDelivery_byFrame(obj) {
	
	var val = ckbeForm_stringTrim(obj.value);
	
	if (val.length < 5) {
		
		ckbe_last_zip_checked = "";
		
	}
	
	if (val != ckbe_last_zip_checked || val == "" && ckbe_last_zip_checked == "") {
	
		var summary_vals = ckbeCart_getValues();
		
		var subtotal_amount_product = summary_vals[0];
		var subtotal_amount_product_WH = summary_vals[1];
		var subtotal_amount_product_DS = summary_vals[2];
		var subtotal_amount_giftcard = summary_vals[3];
		var subtotal_amount_insurance = summary_vals[4];
		var subtotal_amount_delivery = summary_vals[5];
		var subtotal_amount_delivery_calculate = summary_vals[6];
		var subtotal_amount_discount = summary_vals[7];
		var subtotal_amount = summary_vals[8];
		var tax_calculate = summary_vals[9];
		var tax_rate = summary_vals[10];
		var tax_amount = summary_vals[11];
		var total_amount = summary_vals[12];
		
		if (subtotal_amount_delivery_calculate == "true" && subtotal_amount_product_WH > 0) {
		
			if (val.length >= 5 && ckbeForm_checkFormat_zip(obj) == true) {
				
				document.getElementById("div_activity_delivery").style.display = "block";
				
				var aURL = "/frame/frame_action_cart_set_value_delivery.html?zip=" + val;
				
				ckbeCommon_frameSet("frameAction", aURL);
				
				ckbe_last_zip_checked = val;
				
			} else {
				
				subtotal_amount_delivery = 0;
				
				document.getElementById("delivery_available").value = "";
				window.parent.ckbeMotion_fadeSwap("div_message_delivery_unavailable", "div_message_delivery_undefined", 400);
				document.getElementById("delivery_date").options.length = 0;
				document.getElementById("delivery_date_last_selected").value = "";
				document.getElementById("delivery_date_list").value = "";
				document.getElementById("delivery_company").value = "";
				document.getElementById("delivery_zone").value = "";
				document.getElementById("delivery_time_list_mf").value = "";
				document.getElementById("delivery_time_list_s").value = "";
				document.getElementById("div_activity_delivery").style.display = "none";
				
				document.getElementById("subtotal_amount_delivery").value = subtotal_amount_delivery;
				document.getElementById("summary_cell_subtotal_amount_delivery").innerHTML = "<span class=\"body-listtext\" style=\"font-size:9px;\">Provide Zip to Calculate</span>";
				
				document.getElementById("tax_rate").value = 0.07;
				
				document.getElementById("div_delivery_upgrade_title").style.display = "none";
				document.getElementById("div_delivery_upgrade_group").style.display = "none";
				
				document.getElementById("delivery_upgrade_room").checked = false;
				document.getElementById("delivery_upgrade_room_amount").value = "";
				document.getElementById("div_delivery_upgrade_room_label").innerHTML = "";
				document.getElementById("summary_delivery_upgrade_room").style.display = "none";
				
				document.getElementById("delivery_upgrade_debris").checked = false;
				document.getElementById("delivery_upgrade_debris_amount").value = "";
				document.getElementById("div_delivery_upgrade_debris_label").innerHTML = "";
				document.getElementById("summary_delivery_upgrade_debris").style.display = "none";
				
				document.getElementById("delivery_upgrade_assembly").checked = false;
				document.getElementById("delivery_upgrade_assembly_amount").value = "";
				document.getElementById("div_delivery_upgrade_assembly_label").innerHTML = "";
				document.getElementById("summary_delivery_upgrade_assembly").style.display = "none";
				
				document.getElementById("div_delivery_upgrade_saturday").style.display = "none";
				document.getElementById("delivery_upgrade_saturday_amount").value = "";
				document.getElementById("div_delivery_upgrade_saturday_label").innerHTML = "";
				document.getElementById("summary_delivery_upgrade_saturday").style.display = "none";
				
				document.getElementById("summary_delivery_time").style.display = "none";
				
				ckbeCart_setTax(document.getElementById("acp_state"));
				
			}
			
		}
		
	}
	
}

function ckbeCart_setDelivery_byKey(e, obj) {
	
	var key;
	
	if (window.event) {
		key = window.event.keyCode; // IE & Safari.
	} else {
		key = e.which; // Firefox.
	}
	
	ckbeCart_setDelivery_byFrame(obj);
	
	if (key == 13) {
		
		return false;
		
	} else {
		
		return true;
		
	}
	
}

function ckbeCart_calculateDelivery_default(amount) {
	
	if (amount == 0) {
		
		return 0;
		
	} else {
		
		var delivery_calc = amount * ckbe_default_delivery_percentage;
			
			if (delivery_calc > ckbe_default_delivery_max) {
				delivery_calc = ckbe_default_delivery_max;
			}
			
			if (delivery_calc < ckbe_default_delivery_min) {
				delivery_calc = ckbe_default_delivery_min;
			}
			
		return delivery_calc;
		
	}
	
}

function ckbeCart_calculateDelivery_other() {
	
	var fee_max = parseFloat(document.getElementById("other_delivery_fee_max").value);
	var fee_min = parseFloat(document.getElementById("other_delivery_fee_min").value);
	
	var fee_delivery = ckbeCart_calculateDelivery_other_fee(document.getElementById("other_delivery_fee_delivery"));
	var fee_fuel = ckbeCart_calculateDelivery_other_fee(document.getElementById("other_delivery_fee_fuel"));
	var fee_gate = ckbeCart_calculateDelivery_other_fee(document.getElementById("other_delivery_fee_gate"));
	var fee_insurance = ckbeCart_calculateDelivery_other_fee(document.getElementById("other_delivery_fee_insurance"));
	var fee_custom = ckbeCart_calculateDelivery_other_fee(document.getElementById("other_delivery_fee_custom"));
	
	var amount = fee_delivery + fee_fuel + fee_gate + fee_insurance + fee_custom;
	
	if (amount < fee_min) {
		amount = fee_min;
	}
	
	if (amount > fee_max) {
		amount = fee_max;
	}
	
	document.getElementById('other_delivery_cost').value = amount;
	
	return amount;
	
}

function ckbeCart_calculateDelivery_other_fee(obj) {
	
	var weight_string = "/LB";
	var volume_string = "/CI";
	var percent_string = "%";
	var separator = "|";
	
	var fee = 0;
	
	if (obj.value != "") {
		fee = obj.value;
	}
	
	var delivery_product_amount = document.getElementById('subtotal_amount_product_WH').value;
	
	if (delivery_product_amount == "") {
		delivery_product_amount = 0;
	}
	
	delivery_product_amount = parseFloat(delivery_product_amount);
	
	var delivery_weight = document.getElementById('delivery_weight').value;
	
	if (delivery_weight == "") {
		delivery_weight = 0;
	}
	
	delivery_weight = parseFloat(delivery_weight);
	
	var delivery_volume = document.getElementById('delivery_volume').value;
	
	if (delivery_volume == "") {
		delivery_volume = 0;
	}
	
	delivery_volume = parseFloat(delivery_volume);
	
	var this_floor = 0;
	var this_ceiling = 0;
	var this_list = "";
	
	// By Weight.
	
	var this_per_weight = 0;
	
	if (fee.toString().indexOf(weight_string) > -1) {
		
		if (delivery_weight == 0) {
			
			fee = 0;
			
		} else {
			
			if (fee.toString().indexOf(separator) > -1) {
				
				this_floor = 0;
				this_ceiling = 0;
				this_list = fee.toString().substring(0, fee.toString().indexOf(weight_string));
				this_list = this_list.split(",");
				
				for (var i = 0; i < this_list.length; i++) {
					
					this_per_weight = parseFloat(this_list[i].substring(0, this_list[i].indexOf(separator)));
					this_ceiling = parseFloat(this_list[i].substring(this_list[i].indexOf(separator)+1, this_list[i].length));
					
					if (delivery_weight >= this_floor && delivery_weight <= this_ceiling) {
						
						fee = this_per_weight + weight_string;
						break;
						
					} else {
						
						this_floor = this_ceiling + 0.01;
						
					}
					
				}
				
				this_per_weight = parseFloat(fee.toString().substring(0, fee.toString().indexOf(weight_string)));
				
				if (this_per_weight == 0) {
					
					fee = 0;
					
				} else {
					
					fee = delivery_weight * this_per_weight;
					
				}
				
			}
			
		}
		
	}
	
	// By Volume.
	
	var this_per_volume = 0;
	
	if (fee.toString().indexOf(volume_string) > -1) {
		
		if (delivery_volume == 0) {
			
			fee = 0;
			
		} else {
			
			if (fee.toString().indexOf(separator) > -1) {
				
				this_floor = 0;
				this_ceiling = 0;
				this_list = fee.toString().substring(0, fee.toString().indexOf(volume_string));
				this_list = this_list.split(",");
				
				for (var a = 0; a < this_list.length; a++) {
					
					this_per_volume = parseFloat(this_list[a].substring(0, this_list[a].indexOf(separator)));
					this_ceiling = parseFloat(this_list[a].substring(this_list[a].indexOf(separator)+1, this_list[a].length));
					
					if (delivery_volume >= this_floor && delivery_volume <= this_ceiling) {
						
						fee = this_per_volume + volume_string;
						break;
						
					} else {
						
						this_floor = this_ceiling + 0.01;
						
					}
					
				}
				
				this_per_volume = parseFloat(fee.toString().substring(0, fee.toString().indexOf(volume_string)));
				
				if (this_per_volume == 0) {
					
					fee = 0;
					
				} else {
					
					fee = delivery_volume * this_per_volume;
					
				}
				
			}
			
		}
		
	}
	
	// By Percent.
	
	var this_percent = 0;
	
	if (fee.toString().indexOf(percent_string) > -1) {
		
		if (delivery_product_amount == 0) {
			
			fee = 0;
			
		} else {
			
			if (fee.toString().indexOf(separator) > -1) {
				
				this_floor = 0;
				this_ceiling = 0;
				this_list = fee.toString().substring(0, fee.toString().indexOf(percent_string));
				this_list = this_list.split(",");
				
				for (var b = 0; b < this_list.length; b++) {
					
					this_percent = parseFloat(this_list[b].substring(0, this_list[b].indexOf(separator)));
					this_ceiling = parseFloat(this_list[b].substring(this_list[b].indexOf(separator)+1, this_list[b].length));
					
					if (delivery_product_amount >= this_floor && delivery_product_amount <= this_ceiling) {
						
						fee = this_percent + percent_string;
						break;
						
					} else {
						
						this_floor = this_ceiling + 0.01;
						
					}
					
				}
				
				this_percent = parseFloat(fee.toString().substring(0, fee.toString().indexOf(percent_string)));
				
				if (this_percent == 0) {
					
					fee = 0;
					
				} else {
					
					fee = delivery_product_amount * (this_percent / 100);
					
				}
				
			}
			
		}
		
	}
	
	fee = parseFloat(fee);
	
	return fee;
	
}

function ckbeCart_clearDelivery_content() {
	
	document.getElementById("delivery_available").value = false;
	ckbeMotion_fadeSwap("div_message_delivery_undefined", "div_message_delivery_unavailable", 400);
	document.getElementById("delivery_date").options.length = 0;
	document.getElementById("delivery_date_last_selected").value = "";
	document.getElementById("delivery_date_list").value = "";
	
}

function ckbeCart_showDelivery_content(delivery_company, delivery_zone, delivery_date_list, delivery_date_list_label) {
	
	var option_date_list = delivery_date_list.split(",");
	var option_date_list_label = delivery_date_list_label.split(",");
	
	document.getElementById("delivery_available").value = true;
	ckbeMotion_fadeOut("div_message_delivery_undefined", 400);
	ckbeMotion_fadeOut("div_message_delivery_unavailable", 400);
	
	document.getElementById("delivery_date").options.length = 0;
	document.getElementById("delivery_date_last_selected").value = "";
	
	var this_option_value = "";
	var this_option_label = "";
	
	for (i = 0; i < option_date_list.length; i++) {
		
		this_option_value = option_date_list[i];
		this_option_label = option_date_list[i] + " - " + option_date_list_label[i];
		
		document.getElementById("delivery_date").options[i] = new Option(this_option_label, this_option_value);
		
		if (i == 0) {
			
			document.getElementById("delivery_date_last_selected").value = this_option_value;
			
		}
		
	}
	
	document.getElementById("delivery_date_list").value = delivery_date_list;
	
	document.getElementById("delivery_company").value = delivery_company;
	document.getElementById("delivery_zone").value = delivery_zone;
	
	if (delivery_company != ckbe_default_delivery_company) {
		
		var available_saturday = ckbeCart_showHide_upgradeCommon("other_upgrade_saturday", "delivery_upgrade_saturday");
		
		var available_room = ckbeCart_showHide_upgradeCommon("other_upgrade_room", "delivery_upgrade_room");
		var available_debris = ckbeCart_showHide_upgradeCommon("other_upgrade_debris", "delivery_upgrade_debris");
		var available_assembly = ckbeCart_showHide_upgradeCommon("other_upgrade_assembly", "delivery_upgrade_assembly");
		
		var available_time = ckbeCart_showHide_upgradeTime();
		
		if (available_room == true || available_debris == true || available_assembly == true || available_time == true) {
			
			document.getElementById("div_delivery_upgrade_title").style.display = "block";
			document.getElementById("div_delivery_upgrade_group").style.display = "block";
			
		}
		
		if (available_room == false && available_debris == false && available_assembly == false && available_time == false) {
			
			document.getElementById("div_delivery_upgrade_title").style.display = "none";
			document.getElementById("div_delivery_upgrade_group").style.display = "none";
			
		}
		
	}
	
}

function ckbeCart_setDiscount() {
	
	var summary_vals = ckbeCart_getValues();
	
	var subtotal_amount_product = summary_vals[0];
	var subtotal_amount_product_WH = summary_vals[1];
	var subtotal_amount_product_DS = summary_vals[2];
	var subtotal_amount_giftcard = summary_vals[3];
	var subtotal_amount_insurance = summary_vals[4];
	var subtotal_amount_delivery = summary_vals[5];
	var subtotal_amount_delivery_calculate = summary_vals[6];
	var subtotal_amount_discount = summary_vals[7];
	var subtotal_amount = summary_vals[8];
	var tax_calculate = summary_vals[9];
	var tax_rate = summary_vals[10];
	var tax_amount = summary_vals[11];
	var total_amount = summary_vals[12];
	
	if (document.getElementById("discount_code").value == "") {
		
		subtotal_amount_discount = 0;
		
	} else {
		
		var discount_valid = ckbeCart_checkDiscount_floor();
		
		if (discount_valid == true) {
			
			subtotal_amount_discount = parseFloat(document.getElementById("discount_amount").value);
			
		} else {
			
			subtotal_amount_discount = 0;
			
		}
		
	}
	
	ckbeCart_setValues(subtotal_amount_product, subtotal_amount_product_WH, subtotal_amount_product_DS, subtotal_amount_giftcard, subtotal_amount_insurance, subtotal_amount_delivery, subtotal_amount_delivery_calculate, subtotal_amount_discount, tax_calculate, tax_rate);
	document.getElementById("div_activity_discount").style.display = "none";
	
	if (subtotal_amount_discount == 0) {
		
		ckbeCart_clearDiscount_content();
		
		if (document.getElementById("discount_code").value == "") {
			
			alert("We were unable to find a discount for the code you provided." + "\n\n" + "Please check your information and try again.");
			
		} else{
			
			alert("The code you provided is only available to purchases above $" + ckbeCart_formatCurrency(document.getElementById('discount_floor').value) + "." + "\n\n" + "Please check your information and try again.");
			
		}
		
	} else{
		
		ckbeCart_showDiscount_content();
		
	}
	
}

function ckbeCart_setDiscount_byFrame(obj) {
	
	var val = ckbeForm_stringTrim(obj.value);
	
	var summary_vals = ckbeCart_getValues();
	
	var subtotal_amount_product = summary_vals[0];
	var subtotal_amount_product_WH = summary_vals[1];
	var subtotal_amount_product_DS = summary_vals[2];
	var subtotal_amount_giftcard = summary_vals[3];
	var subtotal_amount_insurance = summary_vals[4];
	var subtotal_amount_delivery = summary_vals[5];
	var subtotal_amount_delivery_calculate = summary_vals[6];
	var subtotal_amount_discount = summary_vals[7];
	var subtotal_amount = summary_vals[8];
	var tax_calculate = summary_vals[9];
	var tax_rate = summary_vals[10];
	var tax_amount = summary_vals[11];
	var total_amount = summary_vals[12];
	
	if (subtotal_amount_product_WH > 0) {
		
		if (val.length >= 3) {
			
			document.getElementById("div_activity_discount").style.display = "block";
			
			document.getElementById("frameAction").src="/frame/frame_action_cart_set_value_discount.html?code=" + val;
			
		} else {
			
			subtotal_amount_discount = 0;
			
			document.getElementById("discount_code").value = "";
			document.getElementById("discount_floor").value = "";
			document.getElementById("discount_amount").value = "";
			document.getElementById("discount_terms").value = "";
			document.getElementById("div_activity_discount").style.display = "none";
			
			ckbeCart_setValues(subtotal_amount_product, subtotal_amount_product_WH, subtotal_amount_product_DS, subtotal_amount_giftcard, subtotal_amount_insurance, subtotal_amount_delivery, subtotal_amount_delivery_calculate, subtotal_amount_discount, tax_calculate, tax_rate);
			ckbeCart_clearDiscount_content();
			
		}
		
	}
	
}

function ckbeCart_setDiscount_byKey(e, obj) {
	
	var key;
	
	if (window.event) {
		key = window.event.keyCode; // IE & Safari.
	} else {
		key = e.which; // Firefox.
	}
	
	ckbeCart_setDiscount_byFrame(obj);
	
	if (key == 13) {
		
		return false;
		
	} else {
		
		return true;
		
	}
	
}

function ckbeCart_clearDiscount_content() {
	
	ckbeMotion_fadeOut("summary_row_subtotal_amount_discount", 400);
	ckbeMotion_fadeOut("summary_cell_discount_terms", 400);
	document.getElementById("summary_cell_subtotal_amount_discount").innerHTML = "-$0.00";
	document.getElementById("summary_cell_discount_terms").innerHTML = "";
	
}

function ckbeCart_showDiscount_content() {
	
	var summary_vals = ckbeCart_getValues();
	
	var subtotal_amount_product = summary_vals[0];
	var subtotal_amount_product_WH = summary_vals[1];
	var subtotal_amount_product_DS = summary_vals[2];
	var subtotal_amount_giftcard = summary_vals[3];
	var subtotal_amount_insurance = summary_vals[4];
	var subtotal_amount_delivery = summary_vals[5];
	var subtotal_amount_delivery_calculate = summary_vals[6];
	var subtotal_amount_discount = summary_vals[7];
	var subtotal_amount = summary_vals[8];
	var tax_calculate = summary_vals[9];
	var tax_rate = summary_vals[10];
	var tax_amount = summary_vals[11];
	var total_amount = summary_vals[12];
	
	document.getElementById("summary_cell_subtotal_amount_discount").innerHTML = "-$" + ckbeCart_formatCurrency(subtotal_amount_discount);
	document.getElementById("summary_cell_discount_terms").innerHTML = document.getElementById("discount_terms").value;
	ckbeMotion_fadeIn("summary_row_subtotal_amount_discount", 400);
	ckbeMotion_fadeIn("summary_cell_discount_terms", 400);
	
}

function ckbeCart_checkDiscount_floor() {
	
	var summary_vals = ckbeCart_getValues();
	
	var subtotal_amount_product = summary_vals[0];
	var subtotal_amount_product_WH = summary_vals[1];
	var subtotal_amount_product_DS = summary_vals[2];
	var subtotal_amount_giftcard = summary_vals[3];
	var subtotal_amount_insurance = summary_vals[4];
	var subtotal_amount_delivery = summary_vals[5];
	var subtotal_amount_delivery_calculate = summary_vals[6];
	var subtotal_amount_discount = summary_vals[7];
	var subtotal_amount = summary_vals[8];
	var tax_calculate = summary_vals[9];
	var tax_rate = summary_vals[10];
	var tax_amount = summary_vals[11];
	var total_amount = summary_vals[12];
	
	if (subtotal_amount_product_WH >= parseFloat(document.getElementById("discount_floor").value)) {
		
		return true;
		
	} else {
		
		return false;
		
	}
	
}

function ckbeCart_clearDiscount_search() {
	
	document.getElementById("discount_code").value = "";
	document.getElementById("discount_floor").value = "";
	document.getElementById("discount_amount").value = "";
	document.getElementById("discount_terms").value = "";
	document.getElementById("discount_search").value = "";
	
}

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Option Select Functions.

function ckbeCart_optionDeliveryMethod_delivery() {
	
	document.getElementById("subtotal_amount_delivery_calculate").value = true;
	
	ckbeForm_errorClear_simple("fieldErrorMarker_delivery_date");
	
	ckbeCart_setDelivery_byFrame(document.getElementById("acp_zip"));
	
	ckbeCart_setTax(document.getElementById("acp_state"));
	
}

function ckbeCart_optionDeliveryMethod_pickup() {
	
	var summary_vals = ckbeCart_getValues();
	
	var subtotal_amount_product = summary_vals[0];
	var subtotal_amount_product_WH = summary_vals[1];
	var subtotal_amount_product_DS = summary_vals[2];
	var subtotal_amount_giftcard = summary_vals[3];
	var subtotal_amount_insurance = summary_vals[4];
	var subtotal_amount_delivery = summary_vals[5];
	var subtotal_amount_delivery_calculate = summary_vals[6];
	var subtotal_amount_discount = summary_vals[7];
	var subtotal_amount = summary_vals[8];
	var tax_calculate = summary_vals[9];
	var tax_rate = summary_vals[10];
	var tax_amount = summary_vals[11];
	var total_amount = summary_vals[12];
	
	subtotal_amount_delivery = 0;
	
	document.getElementById("div_delivery_upgrade_title").style.display = "none";
	document.getElementById("div_delivery_upgrade_group").style.display = "none";
	
	document.getElementById("delivery_upgrade_room").checked = false;
	document.getElementById("delivery_upgrade_room_amount").value = "";
	document.getElementById("div_delivery_upgrade_room_label").innerHTML = "";
	document.getElementById("summary_delivery_upgrade_room").style.display = "none";
	
	document.getElementById("delivery_upgrade_debris").checked = false;
	document.getElementById("delivery_upgrade_debris_amount").value = "";
	document.getElementById("div_delivery_upgrade_debris_label").innerHTML = "";
	document.getElementById("summary_delivery_upgrade_debris").style.display = "none";
	
	document.getElementById("delivery_upgrade_assembly").checked = false;
	document.getElementById("delivery_upgrade_assembly_amount").value = "";
	document.getElementById("div_delivery_upgrade_assembly_label").innerHTML = "";
	document.getElementById("summary_delivery_upgrade_assembly").style.display = "none";
	
	document.getElementById("div_delivery_upgrade_saturday").style.display = "none";
	document.getElementById("delivery_upgrade_saturday_amount").value = "";
	document.getElementById("div_delivery_upgrade_saturday_label").innerHTML = "";
	document.getElementById("summary_delivery_upgrade_saturday").style.display = "none";
	
	document.getElementById("summary_delivery_time").style.display = "none";
	
	document.getElementById("delivery_available").value = "";
	
	document.getElementById("delivery_date").options.length = 0;
	
	document.getElementById("delivery_time").value = "";
	document.getElementById("delivery_time").options.length = 0;
	document.getElementById("delivery_time_last_selected").value = "";
	
	document.getElementById("subtotal_amount_delivery").value = subtotal_amount_delivery;
	document.getElementById("summary_cell_subtotal_amount_delivery").innerHTML = "$" + parent.ckbeCart_formatCurrency(subtotal_amount_delivery);
	
	document.getElementById("subtotal_amount_delivery_calculate").value = false;
	
	document.getElementById("tax_rate").value = 0.07;
	
	ckbeCart_setTax(document.getElementById("tax_state"));
	
}

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Upgrade Select Functions.

function ckbeCart_upgradeDelivery_common(obj) {
	
	var summary_vals = ckbeCart_getValues();
	
	var subtotal_amount_product = summary_vals[0];
	var subtotal_amount_product_WH = summary_vals[1];
	var subtotal_amount_product_DS = summary_vals[2];
	var subtotal_amount_giftcard = summary_vals[3];
	var subtotal_amount_insurance = summary_vals[4];
	var subtotal_amount_delivery = summary_vals[5];
	var subtotal_amount_delivery_calculate = summary_vals[6];
	var subtotal_amount_discount = summary_vals[7];
	var subtotal_amount = summary_vals[8];
	var tax_calculate = summary_vals[9];
	var tax_rate = summary_vals[10];
	var tax_amount = summary_vals[11];
	var total_amount = summary_vals[12];
	
	var amt_field = obj.id + "_amount";
	var amt = parseFloat(document.getElementById(amt_field).value);
	
	var summary_div = "summary_" + obj.id;
	
	if (obj.checked == true) {
		
		subtotal_amount_delivery = subtotal_amount_delivery + amt;
		
		document.getElementById(summary_div).style.display = "block";
		
	} else {
		
		subtotal_amount_delivery = subtotal_amount_delivery - amt;
		
		document.getElementById(summary_div).style.display = "none";
		
	}
	
	ckbeCart_setValues(subtotal_amount_product, subtotal_amount_product_WH, subtotal_amount_product_DS, subtotal_amount_giftcard, subtotal_amount_insurance, subtotal_amount_delivery, subtotal_amount_delivery_calculate, subtotal_amount_discount, tax_calculate, tax_rate);
	
}

function ckbeCart_upgradeDelivery_saturday() {
	
	var summary_vals = ckbeCart_getValues();
	
	var subtotal_amount_product = summary_vals[0];
	var subtotal_amount_product_WH = summary_vals[1];
	var subtotal_amount_product_DS = summary_vals[2];
	var subtotal_amount_giftcard = summary_vals[3];
	var subtotal_amount_insurance = summary_vals[4];
	var subtotal_amount_delivery = summary_vals[5];
	var subtotal_amount_delivery_calculate = summary_vals[6];
	var subtotal_amount_discount = summary_vals[7];
	var subtotal_amount = summary_vals[8];
	var tax_calculate = summary_vals[9];
	var tax_rate = summary_vals[10];
	var tax_amount = summary_vals[11];
	var total_amount = summary_vals[12];
	
	if (document.getElementById("delivery_company").value != ckbe_default_delivery_company) {
		
		var amt = document.getElementById("delivery_upgrade_saturday_amount").value;
		
		if (amt == "") {
			amt = 0;
		}
		
		amt = parseFloat(amt);
		
		var time_selected = document.getElementById('delivery_time').value;
		var time_match = false;
		
		var time_val_mf = document.getElementById('delivery_time_list_mf').value;
		var time_val_s = document.getElementById('delivery_time_list_s').value;
		
		var time_list_mf = time_val_mf.split(",");
		var time_list_s = time_val_s.split(",");
		
		var time_amt_mf = document.getElementById('delivery_time_amount_mf').value;
		var time_amt_s = document.getElementById('delivery_time_amount_s').value;
		
		if (time_amt_mf == "") {
			
			time_amt_mf = 0;
			
		}
		
		if (time_amt_s == "") {
			
			time_amt_s = 0;
			
		}
		
		time_amt_mf = parseFloat(time_amt_mf);
		time_amt_s = parseFloat(time_amt_s);
		
		var this_option_value = "";
		var this_option_label = "";
		
		var option_time_list = "";
		
		var days = new Array(7);
		
		days[0] = "Sunday";
		days[1] = "Monday";
		days[2] = "Tuesday";
		days[3] = "Wednesday";
		days[4] = "Thursday";
		days[5] = "Friday";
		days[6] = "Saturday";
		
		var obj_selected = document.getElementById('delivery_date');
		var obj_last = document.getElementById('delivery_date_last_selected');
		
		var val_selected = obj_selected.value;
		var val_last = obj_last.value;
		
		var day_selected = new Date(val_selected);
		var day_last = new Date(val_last);
		
		day_selected = days[day_selected.getDay()];
		day_last = days[day_last.getDay()];
		
		if (time_selected == "") {
		
			if (day_selected == "Saturday") {
				
				time_match = ckbeCart_upgradeDelivery_timeList(time_list_s, time_selected);
				
				subtotal_amount_delivery = subtotal_amount_delivery + amt;
				
				document.getElementById("summary_delivery_upgrade_saturday").style.display = "block";
				
			}
			
			if (day_last == "Saturday") {
				
				time_match = ckbeCart_upgradeDelivery_timeList(time_list_mf, time_selected);
				
				subtotal_amount_delivery = subtotal_amount_delivery - amt;
				
				document.getElementById("summary_delivery_upgrade_saturday").style.display = "none";
				
			}
		
		} else {
			
			if (day_selected == "Saturday") {
				
				if (time_val_s == "") {
					
					document.getElementById("delivery_time").options.length = 0;
					document.getElementById("delivery_time_last_selected").value = "";
					
					subtotal_amount_delivery = subtotal_amount_delivery - time_amt_mf;
					
					document.getElementById("summary_delivery_upgrade_saturday").style.display = "none";
					
					document.getElementById("summary_delivery_time").style.display = "none";
					
					alert("Please Note: You can expect your Saturday delivery between 9 AM and 5 PM, but specific delivery times for Saturdays are currently not available.")
					
				} else {
					
					time_match = ckbeCart_upgradeDelivery_timeList(time_list_s, time_selected);
					
					if (time_match == true) {
						
						subtotal_amount_delivery = subtotal_amount_delivery - time_amt_mf;
						subtotal_amount_delivery = subtotal_amount_delivery + time_amt_s;
						
						document.getElementById("delivery_time").value = time_selected;
						document.getElementById("delivery_time_last_selected").value = time_selected;
						
					}
					
					if (time_match == false) {
						
						subtotal_amount_delivery = subtotal_amount_delivery - time_amt_mf;
						
						document.getElementById("summary_delivery_upgrade_saturday").style.display = "none";
						
						document.getElementById("summary_delivery_time").style.display = "none";
					
						alert("Please Note: The delivery time you had previously selected is not available on Saturdays." + "\n\n" + "If you would still like a specific delivery time, please select another time.")
						
					}
					
				}
				
				subtotal_amount_delivery = subtotal_amount_delivery + amt;
				
				document.getElementById("summary_delivery_upgrade_saturday").style.display = "block";
				
			}
			
			if (day_last == "Saturday") {
				
				if (time_val_mf == "") {
					
					document.getElementById("delivery_time").options.length = 0;
					document.getElementById("delivery_time_last_selected").value = "";
					
					subtotal_amount_delivery = subtotal_amount_delivery - time_amt_s;
					
					document.getElementById("summary_delivery_upgrade_saturday").style.display = "none";
					
					document.getElementById("summary_delivery_time").style.display = "none";
					
					alert("Please Note: You can expect your delivery between 9 AM and 10 PM, but specific delivery times during the week are currently not available.")
					
				} else {
					
					time_match = ckbeCart_upgradeDelivery_timeList(time_list_mf, time_selected);
					
					if (time_match == true) {
						
						subtotal_amount_delivery = subtotal_amount_delivery - time_amt_s;
						subtotal_amount_delivery = subtotal_amount_delivery + time_amt_mf;
						
						document.getElementById("delivery_time").value = time_selected;
						document.getElementById("delivery_time_last_selected").value = time_selected;
						
					}
					
					if (time_match == false) {
						
						subtotal_amount_delivery = subtotal_amount_delivery - time_amt_s;
						
						document.getElementById("summary_delivery_upgrade_saturday").style.display = "none";
						
						document.getElementById("summary_delivery_time").style.display = "none";
					
						alert("Please Note: The delivery time you had previously selected is not available during the week." + "\n\n" + "If you would still like a specific delivery time, please select another time.")
						
					}
					
				}
				
				subtotal_amount_delivery = subtotal_amount_delivery - amt;
				
				document.getElementById("summary_delivery_upgrade_saturday").style.display = "none";
				
			}
			
		}
		
		ckbeCart_setValues(subtotal_amount_product, subtotal_amount_product_WH, subtotal_amount_product_DS, subtotal_amount_giftcard, subtotal_amount_insurance, subtotal_amount_delivery, subtotal_amount_delivery_calculate, subtotal_amount_discount, tax_calculate, tax_rate);
		
		obj_last.value = val_selected;
		
	}
	
}

function ckbeCart_upgradeDelivery_timeList(list, selected) {
	
	var time_match = false;
	
	document.getElementById("delivery_time").options.length = 0;
	document.getElementById("delivery_time_last_selected").value = "";
	
	option_time_list = list;
	
	document.getElementById("delivery_time").options[0] = new Option("", "");
	
	var a = 0;
	
	for (i = 1; i <= option_time_list.length; i++) {
		
		a = i - 1;
		
		this_option_value = option_time_list[a];
		this_option_label = option_time_list[a];
		
		document.getElementById("delivery_time").options[i] = new Option(this_option_label, this_option_value);
		
	}
	
	for (i = 0; i < list.length; i++) {
		
		if (list[i] == selected) {
			
			time_match = true;
			break;
			
		}
		
	}
	
	return time_match;
	
}

function ckbeCart_upgradeDelivery_time() {
	
	var summary_vals = ckbeCart_getValues();
	
	var subtotal_amount_product = summary_vals[0];
	var subtotal_amount_product_WH = summary_vals[1];
	var subtotal_amount_product_DS = summary_vals[2];
	var subtotal_amount_giftcard = summary_vals[3];
	var subtotal_amount_insurance = summary_vals[4];
	var subtotal_amount_delivery = summary_vals[5];
	var subtotal_amount_delivery_calculate = summary_vals[6];
	var subtotal_amount_discount = summary_vals[7];
	var subtotal_amount = summary_vals[8];
	var tax_calculate = summary_vals[9];
	var tax_rate = summary_vals[10];
	var tax_amount = summary_vals[11];
	var total_amount = summary_vals[12];
	
	if (document.getElementById("delivery_company").value != ckbe_default_delivery_company) {
		
		var amt = 0;
		
		var days = new Array(7);
		
		days[0] = "Sunday";
		days[1] = "Monday";
		days[2] = "Tuesday";
		days[3] = "Wednesday";
		days[4] = "Thursday";
		days[5] = "Friday";
		days[6] = "Saturday";
		
		var date_selected = document.getElementById('delivery_date').value;
		var day_selected = new Date(date_selected);
		
		day_selected = days[day_selected.getDay()];
		
		var time_selected = document.getElementById('delivery_time');
		var time_last = document.getElementById('delivery_time_last_selected');
		
		if (time_selected.value == "") {
			
			if (time_last.value != "") {
				
				if (day_selected == "Saturday") {
					
					amt = parseFloat(document.getElementById('delivery_time_amount_s').value);
					
				} else {
					
					amt = parseFloat(document.getElementById('delivery_time_amount_mf').value);
					
				}
				
				subtotal_amount_delivery = subtotal_amount_delivery - amt;
				
				document.getElementById("summary_delivery_time").style.display = "none";
				
			}
			
		} else {
			
			if (time_last.value == "") {
				
				if (day_selected == "Saturday") {
					
					amt = parseFloat(document.getElementById('delivery_time_amount_s').value);
					
				} else {
					
					amt = parseFloat(document.getElementById('delivery_time_amount_mf').value);
					
				}
				
				subtotal_amount_delivery = subtotal_amount_delivery + amt;
				
				document.getElementById("summary_delivery_time").style.display = "block";
				
			}
			
		}
		
		ckbeCart_setValues(subtotal_amount_product, subtotal_amount_product_WH, subtotal_amount_product_DS, subtotal_amount_giftcard, subtotal_amount_insurance, subtotal_amount_delivery, subtotal_amount_delivery_calculate, subtotal_amount_discount, tax_calculate, tax_rate);	
		
		time_last.value = time_selected.value;
		
	}
	
}

function ckbeCart_showHide_upgradeCommon(source, target) {
	
	source = document.getElementById(source).value;
	target = document.getElementById(target).id;
	
	var target_holder = "div_" + target;
	var target_amount = target + "_amount";
	var target_label = "div_" + target + "_label";
	
	document.getElementById(target).checked = false;
	
	if (source != "") {
		
		source = parseFloat(source);
		
		document.getElementById(target_amount).value = source;
		document.getElementById(target_label).innerHTML = "$" + ckbeCart_formatCurrency(source);
		document.getElementById(target_holder).style.display = "block";
		
		return true;
		
	} else {
		
		document.getElementById(target_amount).value = "";
		document.getElementById(target_label).innerHTML = "";
		document.getElementById(target_holder).style.display = "none";
		
		return false;
		
	}
	
}

function ckbeCart_showHide_upgradeTime() {
	
	var display = false;
	
	var available_mf = false;
	var available_s = false;
	
	var source_mf = document.getElementById("other_upgrade_time_mf");
	var source_s = document.getElementById("other_upgrade_time_s");
	
	var target_mf = document.getElementById("delivery_time_amount_mf");
	var target_s = document.getElementById("delivery_time_amount_s");
	
	var label_string = "<br />";
	
	if (source_mf.value != "" || source_s.value != "") {
		
		if (source_mf.value != "") {
			
			target_mf.value = source_mf.value;
			
			var source_list_mf = document.getElementById("other_upgrade_time_mf_list");
			var target_list_mf = document.getElementById("delivery_time_list_mf");
			
			if (source_list_mf.value != "") {
				
				document.getElementById("delivery_time").value = "";
				document.getElementById("delivery_time").options.length = 0;
				document.getElementById("delivery_time_last_selected").value = "";
				
				var option_time_list = source_list_mf.value.split(",");
				
				var this_option_value = "";
				var this_option_label = "";
				
				document.getElementById("delivery_time").options[0] = new Option("", "");
				
				var a = 0;
				
				for (i = 1; i <= option_time_list.length; i++) {
					
					a = i - 1;
					
					this_option_value = option_time_list[a];
					this_option_label = option_time_list[a];
					
					document.getElementById("delivery_time").options[i] = new Option(this_option_label, this_option_value);
					
				}
				
				target_list_mf.value = source_list_mf.value;
				
				label_string = label_string + "$" + ckbeCart_formatCurrency(source_mf.value) + " (Mon-Fri) ";
				
				display =  true;
				
				available_mf = true;
				
			}
			
		}
		
		if (source_s.value != "") {
			
			target_s.value = source_s.value;
			
			var source_list_s = document.getElementById("other_upgrade_time_s_list");
			var target_list_s = document.getElementById("delivery_time_list_s");
			
			if (source_list_s.value != "") {
				
				target_list_s.value = source_list_s.value;
				
				label_string = label_string + "$" + ckbeCart_formatCurrency(source_s.value) + " (Sat)";
				
				display =  true;
				
				available_s = true;
				
			}
			
		}
		
		if (available_mf == true && available_s == false) {
			
			label_string = "<br />$" + ckbeCart_formatCurrency(source_mf.value) + " (Available Mon-Fri) ";
			
		}
		
		if (available_mf == false && available_s == true) {
			
			label_string = "<br />$" + ckbeCart_formatCurrency(source_s.value) + " (Available Saturday) ";
			
		}
		
		document.getElementById("div_delivery_time_label").innerHTML = label_string;
		
	}
	
	if (display == true) {
		
		document.getElementById("div_delivery_upgrade_time").style.display = "block";
		
	} else {
		
		document.getElementById("div_delivery_upgrade_time").style.display = "none";
		
	}
	
	return display;
	
}

// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Check Form Functions.

function ckbeCart_checkForm_product(form) {

	var error = null;
	var obj = null;
	
	obj = form.quantity;
	error = ckbeForm_checkField_min(obj, form.minimum, error);
	
	if (error != null) {
		
		error.focus();
		return false;
		
	}
	
	window.location.href = form.cart.value + "?add=true&type=Product&pID=" + form.pID.value + "&quantity=" + form.quantity.value;
	return false;

}

function ckbeCart_checkForm_giftcard(form) {
	
	var error = null;
	var obj = null;
	
	obj = form.sender;
	error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_null(obj), error);
	
	obj = form.recipient;
	error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_null(obj), error);
	
	obj = form.amount;
	error = ckbeForm_checkField_min(obj, form.minimum, error);
	
	if (error != null) {
		
		error.focus();
		return false;
		
	}
	
	window.location.href = form.cart.value + "?add=true&type=Gift Card&sender=" + form.sender.value + "&recipient=" + form.recipient.value + "&amount=" + form.amount.value + "&quantity=1";
	return false;
	
}

function ckbeCart_checkForm_cart(form) {

	if (form.total_item_count.value == 0) {
	
		return false;
	
	} else {
	
		return true;
	
	}

}

function ckbeCart_checkForm_info(form) {
	
	var error = null;
	var obj = null;
	
	obj = form.acp_email;
	error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_email(obj), error);
	
	obj = form.acp_email2;
	error = ckbeForm_checkField_match(obj, form.acp_email, error);
	
	obj = form.acp_phone;
	error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_phone(obj), error);
	
	obj = form.acp_name;
	error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_name(obj), error);
	
	obj = form.acp_address;
	error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_address(obj), error);
	
	obj = form.acp_city;
	error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_text(obj), error);
	
	obj = form.acp_state;
	error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_state(obj), error);
	
	obj = form.acp_zip;
	error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_zip(obj), error);
	
	obj = form.acp_country;
	error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_text(obj), error);
	
	obj = form.delivery_available;
	error = ckbeForm_checkField_delivery(obj, form.delivery_method, form.delivery_date, error);
	
	if (form.payment_method.value == "Credit Card") {
	
		obj = form.acb_cc_name;
		error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_name(obj), error);
		
		obj = form.acb_dtc_id;
		error = ckbeForm_checkField_select(obj, error);
		
		obj = form.acb_cc_number;
		error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_ccNum(obj), error);
		
		obj = form.acb_cc_exp_month;
		error = ckbeForm_checkField_ccExp(obj, form.acb_cc_exp_year, error);
		
		obj = form.acb_cc_csc;
		error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_ccCsc(obj), error);
		
		obj = form.acb_cc_address;
		error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_address(obj), error);
		
		obj = form.acb_cc_city;
		error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_text(obj), error);
		
		obj = form.acb_cc_state;
		error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_state(obj), error);
		
		obj = form.acb_cc_zip;
		error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_zip(obj), error);
		
		obj = form.acb_cc_country;
		error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_text(obj), error);
		
		if (form.split_sale.value == "true") {
		
			obj = form.split_acb_cc_name;
			error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_name(obj), error);
			
			obj = form.split_acb_dtc_id;
			error = ckbeForm_checkField_select(obj, error);
			
			obj = form.split_acb_cc_number;
			error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_ccNum(obj), error);
			
			obj = form.split_acb_cc_exp_month;
			error = ckbeForm_checkField_ccExp(obj, form.split_acb_cc_exp_year, error);
			
			obj = form.split_acb_cc_csc;
			error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_ccCsc(obj), error);
			
			obj = form.split_acb_cc_address;
			error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_address(obj), error);
			
			obj = form.split_acb_cc_city;
			error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_text(obj), error);
			
			obj = form.split_acb_cc_state;
			error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_state(obj), error);
			
			obj = form.split_acb_cc_zip;
			error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_zip(obj), error);
			
			obj = form.split_acb_cc_country;
			error = ckbeForm_checkField_value(obj, ckbeForm_checkFormat_text(obj), error);
			
			obj = form.split_amount;
			error = ckbeForm_checkField_split(obj, form.total_amount, error);
		
		}
	
	}
	
	// ---------- Error ---------- //
	
	if (error != null) {
		
		error.focus();
		return false;
		
	}
	
}

function ckbeCart_checkForm_terms(form) {
	
	if (form.step.value == "back") {
		
		return true;
		
	} else {
		
		if (form.terms.checked == true) {
			
			return true;
			
		} else {
			
			alert(form.message.value)
			form.terms.focus();
			return false;
			
		}
		
	}
	
}
