var shipping_addresses = []

if (!Array.prototype.every)
{
  Array.prototype.every = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this &&
          !fun.call(thisp, this[i], i, this))
        return false;
    }

    return true;
  };
}

$(document).ready(function() {

	if ($('#placement-1 img').length > 1) {
		$('#placement-1').cycle({fx: 'fade', speed: 2000});
	}
	
	$("form#manageSkus").submit(function() {
		
		/**
		 * Store the available current locations in a dictionary
		 */		
		var locations = { 1: "One Stop - Grand Rapids, MI", 2: "Mill Direct - Elizabethtown, KY", 3: "Mill Direct - Pageland, SC", 4: "Mill Direct - Statesville, NC", 
		5: "Mill Direct - Hartwell, GA", 9: "Mill Direct - Whites Creek, TN", 10: "Mill Direct - Seattle, WA", 11: "Mill Direct - Anaheim, CA", 12: "Mill Direct - Brinkley, AR" };
		
		/**
		 * Store locations found in the tables
		 */
		var availableLocations = [];
		
		/**
		 * Store each id into the available locations array
		 */
		var n = 0;
		$("table.location").each(function() {
			availableLocations[n] = this.id;
			n++;
		});
		n = 0;
		
		/**
		 * Check to see if all fields are empty
		 */
		var checkFields = function (location) {
			var bool = false;
			var locationNumber = location.replace("location-", "");
			$("table#" + location + " tr.inputs td.qty").each(function() {
				var qtyValue = $(this).children("input[@name='entry[" + locationNumber + "][" + n + "][qty]']").val();
				
				if (qtyValue != "") {
					bool = true;
				}
				n++;
			});
			n = 0;
			if (bool == false) {
				alert("Please enter a quantity in the fields in order to add items to your cart.");
			}
			return bool;
		}

		/**
		 * Makes sure that all fields contain integers
		 */
		var checkFieldsInt = function (location) {
			var bool = true;
			var locationNumber = location.replace("location-", "");
			$("table#" + location + " tr.inputs td.qty").each(function() {
				var qtyValue = $(this).children("input[@name='entry[" + locationNumber + "][" + n + "][qty]']").val();
				if (qtyValue.length != 0 && !qtyValue.match(/^[0-9]+$/)) { 
					alert("Under the " + locations[parseInt(locationNumber)] +
						  " location, there is invalid input for size " + 
						  $(this).children("input[@name='entry[" + locationNumber + "][" + n + "][size_code]']").val() + ".");
					bool = false;
				}
				n++;
			});
			n = 0;
			return bool;
		}
		
		/**
		 * Make sure that the quantity entered is not more than the inventory
		 */
		var validateLocationQtys = function (location) {
			var bool = true;
			var locationNumber = location.replace("location-", "");
			
			if (parseInt(locationNumber) < 10) {
				$("table#" + location + " tr.inputs td.qty").each(function() {
					var invValue = parseInt($(this).children("input[@name='entry[" + locationNumber + "][" + n + "][inventory]']").val());
					var qtyValue = parseInt($(this).children("input[@name='entry[" + locationNumber + "][" + n + "][qty]']").val());

					if (qtyValue != "") {
						if (qtyValue > invValue) {
							alert(
								"Under the " + locations[parseInt(locationNumber)] + 
								" location, you will be ordering more than is currently available for size " + 
								$(this).children("input[@name='entry[" + locationNumber + "][" + n + "][size_code]']").val() + ".");
							bool = false;
						}
					}
					n++;
				});
				n = 0;
			}
			
			return bool;
		}
		
		/**
		 * Use Array.every to check all the fields for each location
		 * If all checks return true, return true
		 */
		if (availableLocations.every(checkFieldsInt) && availableLocations.every(validateLocationQtys)) {
			return true;
		}
		
		/**
		 * Do not submit the form
		 */
		return false;
	});
	
	/**
	 * Catalog Order
	 */
	$(document).ready(function() {

		var custom_lifestyle_cover_with_pricing = 0.0;
		var custom_lifestyle_cover_without_pricing = 0.0;
		var custom_activeware_cover_with_pricing = 0.0;
		var custom_activeware_cover_without_pricing = 0.0;
		var custom_corporate_cover_with_pricing = 0.0;
		var custom_corporate_cover_without_pricing = 0.0;
		var custom_imprint_minimum = 100;
		var custom_imprint_total = 0.0;
		var custom_imprint_prices = {"100 - 499 pieces" : 1.25, "500+ pieces" : 1.00 };
		var custom_imprint_artwork_selected = false;

		var personalized_with_pricing = 0.0;
		var personalized_without_pricing = 0.0;
		var personalized_total = 0.0;
		var personalized_minimum = 200;
		var personalized_prices = {	"200+ pieces" : 3.00 };

		var blank_lifestyle_cover_with_pricing = 0.0;
		var blank_lifestyle_cover_without_pricing = 0.0;
		var blank_activeware_cover_with_pricing = 0.0;
		var blank_activeware_cover_without_pricing = 0.0;
		var blank_corporate_cover_with_pricing = 0.0;
		var blank_corporate_cover_without_pricing = 0.0;
		var blank_catalogs_total = 0.0;
		var blank_catalogs_prices = { "1 - 99 pieces" : 0.79, "100 - 499 pieces" : 0.89, "500+ pieces" : 0.99 };

		var asi_pricelist_qty = 0.0;
		var asi_pricelist_total = 0.0;
		var asi_pricelist_prices = { "1 - 99 pieces" : 0.25, "100 - 499 pieces" : 0.25, "500+ pieces" : 0.25 };

		var athletic_apparel_with_pricing = 0.0;
		var ladies_collection_with_pricing = 0.0;
		var enza_collection_with_pricing = 0.0;
		var special_catalogs_total = 0.0;
		var special_catalogs_prices = {	"1 - 99 pieces" : 0.50,	"100 - 499 pieces" : 0.50,"500+ pieces" : 0.50 };

		/**
		 * Custom Imprint Lifestyle Cover With Pricing on change
		 */
		$("#custom_imprint #custom_lifestyle_cover_with_pricing").change(function() {

			var t = $(this).val();

			if (t == "") {
				custom_lifestyle_cover_with_pricing = 0.0;
				retotalCustomImprint();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			if ((custom_imprint_minimum - 1) >= t) {
				alert("Must order more than " + t + " pieces. (Minimum: " + custom_imprint_minimum + " pieces)");
				return false;
			}

			var cost = 0.0;
			if (t >= 500) {
				cost = t * custom_imprint_prices['500+ pieces'];
			} else {
				cost = t * custom_imprint_prices['100 - 499 pieces'];
			}

			custom_lifestyle_cover_with_pricing = cost;

			retotalCustomImprint();

		});

		/**
		 * Custom Imprint Lifestyle Cover Without Pricing on change
		 */
		$("#custom_imprint #custom_lifestyle_cover_without_pricing").change(function() {

			var t = $(this).val();

			if (t == "") {
				custom_lifestyle_cover_without_pricing = 0.0;
				retotalCustomImprint();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			if ((custom_imprint_minimum - 1) >= t) {
				alert("Must order more than " + t + " pieces. (Minimum: " + custom_imprint_minimum + " pieces)");
				return false;
			}

			var cost = 0.0;
			if (t >= 500) {
				cost = t * custom_imprint_prices['500+ pieces'];
			} else {
				cost = t * custom_imprint_prices['100 - 499 pieces'];
			}

			custom_lifestyle_cover_without_pricing = cost;

			retotalCustomImprint();

		});

		/**
		 * Custom Imprint Activeware Cover With Pricing on change
		 */
		$("#custom_imprint #custom_activeware_cover_with_pricing").change(function() {

			var t = $(this).val();

			if (t == "") {
				custom_activeware_cover_with_pricing = 0.0;
				retotalCustomImprint();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			if ((custom_imprint_minimum - 1) >= t) {
				alert("Must order more than " + t + " pieces. (Minimum: " + custom_imprint_minimum + " pieces)");
				return false;
			}

			var cost = 0.0;
			if (t >= 500) {
				cost = t * custom_imprint_prices['500+ pieces'];
			} else {
				cost = t * custom_imprint_prices['100 - 499 pieces'];
			}

			custom_activeware_cover_with_pricing = cost;

			retotalCustomImprint();

		});

		/**
		 * Custom Imprint Activeware Cover Without Pricing on change
		 */
		$("#custom_imprint #custom_activeware_cover_without_pricing").change(function() {

			var t = $(this).val();

			if (t == "") {
				custom_activeware_cover_without_pricing = 0.0;
				retotalCustomImprint();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			if ((custom_imprint_minimum - 1) >= t) {
				alert("Must order more than " + t + " pieces. (Minimum: " + custom_imprint_minimum + " pieces)");
				return false;
			}

			var cost = 0.0;
			if (t >= 500) {
				cost = t * custom_imprint_prices['500+ pieces'];
			} else {
				cost = t * custom_imprint_prices['100 - 499 pieces'];
			}

			custom_activeware_cover_without_pricing = cost;

			retotalCustomImprint();

		});

		/**
		 * Custom Imprint Corporate Cover With Pricing on change
		 */
		$("#custom_imprint #custom_corporate_cover_with_pricing").change(function() {

			var t = $(this).val();

			if (t == "") {
				custom_corporate_cover_with_pricing = 0.0;
				retotalCustomImprint();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			if ((custom_imprint_minimum - 1) >= t) {
				alert("Must order more than " + t + " pieces. (Minimum: " + custom_imprint_minimum + " pieces)");
				return false;
			}

			var cost = 0.0;
			if (t >= 500) {
				cost = t * custom_imprint_prices['500+ pieces'];
			} else {
				cost = t * custom_imprint_prices['100 - 499 pieces'];
			}

			custom_corporate_cover_with_pricing = cost;

			retotalCustomImprint();

		});

		/**
		 * Custom Imprint Corporate Cover Without Pricing on change
		 */
		$("#custom_imprint #custom_corporate_cover_without_pricing").change(function() {

			var t = $(this).val();

			if (t == "") {
				custom_corporate_cover_without_pricing = 0.0
				retotalCustomImprint();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			if ((custom_imprint_minimum - 1) >= t) {
				alert("Must order more than " + t + " pieces. (Minimum: " + custom_imprint_minimum + " pieces)");
				return false;
			}

			var cost = 0.0;
			if (t >= 500) {
				cost = t * custom_imprint_prices['500+ pieces'];
			} else {
				cost = t * custom_imprint_prices['100 - 499 pieces'];
			}

			custom_corporate_cover_without_pricing = cost;

			retotalCustomImprint();

		});

		$("input[@name='custom_imprint_artwork']").change(function() {

			var t = $(this).val();		
			if (t == "file") {
				$("input[@name='custom_imprint_artwork_file']").attr('disabled', '');
			} else {
				$("input[@name='custom_imprint_artwork_file']").attr('disabled', 'disabled');
			}

		});

		$("input[@name='reset']").click(function() {
			$("table.main .total").each(function() {
				$(this).text("$0.00");
			});
		});

		/**
		 * Personalized Covers With Pricing on change
		 */
		$("#personalized_covers #personalized_cover_with_pricing").change(function() {

			var t = $(this).val();

			if (t == "") {
				personalized_with_pricing = 0.0;
				retotalPersonalized();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			if ((personalized_minimum - 1) >= t) {
				alert("Must order more than " + t + " pieces. (Minimum: " + personalized_minimum + " pieces)");
				return false;
			}

			var cost = 0.0;
			if (t >= 200) {
				cost = t * personalized_prices['200+ pieces'];
			}

			personalized_with_pricing = cost;

			retotalPersonalized();

		});

		/**
		 * Personalized Covers Without Pricing on change
		 */
		$("#personalized_covers #personalized_cover_without_pricing").change(function() {

			var t = $(this).val();

			if (t == "") {
				personalized_without_pricing = 0.0;
				retotalPersonalized();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			if ((personalized_minimum - 1) >= t) {
				alert("Must order more than " + t + " pieces. (Minimum: " + personalized_minimum + " pieces)");
				return false;
			}

			var cost = 0.0;
			if (t >= 200) {
				cost = t * personalized_prices['200+ pieces'];
			}

			personalized_without_pricing = cost;

			retotalPersonalized();

		});

		/**
		 * Blank Catalogs Blank Lifestyle Cover With Pricing on change
		 */
		$("#blank_catalogs #blank_lifestyle_cover_with_pricing").change(function() {

			var t = $(this).val();

			if (t == "") {
				blank_lifestyle_cover_with_pricing = 0.0;
				retotalBlankCatalogs();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			var cost = 0.0;
			if (t >= 500) {
				cost = t * blank_catalogs_prices['500+ pieces'];
			} else if (t >= 100 && t <= 499) {
				cost = t * blank_catalogs_prices['100 - 499 pieces'];
			} else {
				cost = t * blank_catalogs_prices['1 - 99 pieces'];
			}

			blank_lifestyle_cover_with_pricing = cost;

			retotalBlankCatalogs();

		});

		/**
		 * Blank Catalogs Blank Lifestyle Cover Without Pricing on change
		 */
		$("#blank_catalogs #blank_lifestyle_cover_without_pricing").change(function() {

			var t = $(this).val();

			if (t == "") {
				blank_lifestyle_cover_without_pricing = 0.0;
				retotalBlankCatalogs();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			var cost = 0.0;
			if (t >= 500) {
				cost = t * blank_catalogs_prices['500+ pieces'];
			} else if (t >= 100 && t <= 499) {
				cost = t * blank_catalogs_prices['100 - 499 pieces'];
			} else {
				cost = t * blank_catalogs_prices['1 - 99 pieces'];
			}

			blank_lifestyle_cover_without_pricing = cost;

			retotalBlankCatalogs();

		});

		/**
		 * Blank Catalogs Blank Activeware Cover With Pricing on change
		 */
		$("#blank_catalogs #blank_activeware_cover_with_pricing").change(function() {

			var t = $(this).val();

			if (t == "") {
				blank_activeware_cover_with_pricing = 0.0;
				retotalBlankCatalogs();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			var cost = 0.0;
			if (t >= 500) {
				cost = t * blank_catalogs_prices['500+ pieces'];
			} else if (t >= 100 && t <= 499) {
				cost = t * blank_catalogs_prices['100 - 499 pieces'];
			} else {
				cost = t * blank_catalogs_prices['1 - 99 pieces'];
			}

			blank_activeware_cover_with_pricing = cost;

			retotalBlankCatalogs();

		});

		/**
		 * Blank Catalogs Blank Activeware Cover Without Pricing on change
		 */
		$("#blank_catalogs #blank_activeware_cover_without_pricing").change(function() {

			var t = $(this).val();

			if (t == "") {
				blank_activeware_cover_without_pricing = 0.0;
				retotalBlankCatalogs();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			var cost = 0.0;
			if (t >= 500) {
				cost = t * blank_catalogs_prices['500+ pieces'];
			} else if (t >= 100 && t <= 499) {
				cost = t * blank_catalogs_prices['100 - 499 pieces'];
			} else {
				cost = t * blank_catalogs_prices['1 - 99 pieces'];
			}

			blank_activeware_cover_without_pricing = cost;

			retotalBlankCatalogs();

		});

		/**
		 * Blank Catalogs Blank Corporate Cover With Pricing on change
		 */
		$("#blank_catalogs #blank_corporate_cover_with_pricing").change(function() {

			var t = $(this).val();

			if (t == "") {
				blank_corporate_cover_with_pricing = 0.0;
				retotalBlankCatalogs();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			var cost = 0.0;
			if (t >= 500) {
				cost = t * blank_catalogs_prices['500+ pieces'];
			} else if (t >= 100 && t <= 499) {
				cost = t * blank_catalogs_prices['100 - 499 pieces'];
			} else {
				cost = t * blank_catalogs_prices['1 - 99 pieces'];
			}

			blank_corporate_cover_with_pricing = cost;

			retotalBlankCatalogs();

		});

		/**
		 * Blank Catalogs Blank Corporate Cover Without Pricing on change
		 */
		$("#blank_catalogs #blank_corporate_cover_without_pricing").change(function() {

			var t = $(this).val();

			if (t == "") {
				blank_corporate_cover_without_pricing = 0.0;
				retotalBlankCatalogs();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			var cost = 0.0;
			if (t >= 500) {
				cost = t * blank_catalogs_prices['500+ pieces'];
			} else if (t >= 100 && t <= 499) {
				cost = t * blank_catalogs_prices['100 - 499 pieces'];
			} else {
				cost = t * blank_catalogs_prices['1 - 99 pieces'];
			}

			blank_corporate_cover_without_pricing = cost;

			retotalBlankCatalogs();

		});

		/**
		 * ASI Pricelist on change
		 */
		$("#asi_pricelist #asi_pricelist_qty").change(function() {

			var t = $(this).val();

			if (t == "") {
				asi_pricelist_total = 0.0;
				retotalAsiPricelist();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			var cost = 0.0;
			if (t >= 500) {
				cost = t * asi_pricelist_prices['500+ pieces'];
			} else if (t >= 100 && t <= 499) {
				cost = t * asi_pricelist_prices['100 - 499 pieces'];
			} else {
				cost = t * asi_pricelist_prices['1 - 99 pieces'];
			}

			asi_pricelist_total = cost;

			retotalAsiPricelist();

		});

		/**
		 * Specialty Catalogs Athletic Apparel With Pricing on change
		 */
		$("#specialty_catalogs #athletic_apparel_with_pricing").change(function() {

			var t = $(this).val();

			if (t == "") {
				athletic_apparel_with_pricing = 0.0;
				retotalSpecialCatalogs();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			var cost = 0.0;
			if (t >= 500) {
				cost = t * special_catalogs_prices['500+ pieces'];
			} else if (t >= 100 && t <= 499) {
				cost = t * special_catalogs_prices['100 - 499 pieces'];
			} else {
				cost = t * special_catalogs_prices['1 - 99 pieces'];
			}

			athletic_apparel_with_pricing = cost;

			retotalSpecialCatalogs();

		});

		/**
		 * Specialty Catalogs Ladies Collection With Pricing on change
		 */
		$("#specialty_catalogs #ladies_collection_with_pricing").change(function() {

			var t = $(this).val();

			if (t == "") {
				ladies_collection_with_pricing = 0.0;
				retotalSpecialCatalogs();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			var cost = 0.0;
			if (t >= 500) {
				cost = t * special_catalogs_prices['500+ pieces'];
			} else if (t >= 100 && t <= 499) {
				cost = t * special_catalogs_prices['100 - 499 pieces'];
			} else {
				cost = t * special_catalogs_prices['1 - 99 pieces'];
			}

			ladies_collection_with_pricing = cost;

			retotalSpecialCatalogs();

		});

		/**
		 * Specialty Catalogs Enza Collection With Pricing on change
		 */
		$("#specialty_catalogs #enza_collection_with_pricing").change(function() {

			var t = $(this).val();

			if (t == "") {
				enza_collection_with_pricing = 0.0;
				retotalSpecialCatalogs();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			var cost = 0.0;
			if (t >= 500) {
				cost = t * special_catalogs_prices['500+ pieces'];
			} else if (t >= 100 && t <= 499) {
				cost = t * special_catalogs_prices['100 - 499 pieces'];
			} else {
				cost = t * special_catalogs_prices['1 - 99 pieces'];
			}

			enza_collection_with_pricing = cost;

			retotalSpecialCatalogs();

		});

		function isInt(n) {

			var reInteger = /^\d+$/;
			return reInteger.test(n);

		}

		function retotalCustomImprint () {

			custom_imprint_total = 	custom_lifestyle_cover_with_pricing + 
									custom_lifestyle_cover_without_pricing + 
									custom_activeware_cover_with_pricing +
									custom_activeware_cover_without_pricing + 
									custom_corporate_cover_with_pricing +
									custom_corporate_cover_without_pricing;

			$("#custom_imprint .total").text("$" + custom_imprint_total.toFixed(2));

		}

		function retotalPersonalized () {

			personalized_total = 	personalized_with_pricing +
									personalized_without_pricing;

			$("#personalized_covers .total").text("$" + personalized_total.toFixed(2));

		}

		function retotalBlankCatalogs () {

			blank_catalogs_total = 	blank_lifestyle_cover_with_pricing + 
									blank_lifestyle_cover_without_pricing + 
									blank_activeware_cover_with_pricing +
									blank_activeware_cover_without_pricing + 
									blank_corporate_cover_with_pricing +
									blank_corporate_cover_without_pricing;

			$("#blank_catalogs .total").text("$" + blank_catalogs_total.toFixed(2));

		}

		function retotalAsiPricelist () {
			$("#asi_pricelist .total").text("$" + asi_pricelist_total.toFixed(2));
		}

		function retotalSpecialCatalogs () {
			special_catalogs_total = 	athletic_apparel_with_pricing +
										ladies_collection_with_pricing +
										enza_collection_with_pricing;

			$("#specialty_catalogs .total").text("$" + special_catalogs_total.toFixed(2));
		}

		$("#custom_imprint #custom_lifestyle_cover_with_pricing").trigger("change");
		$("#custom_imprint #custom_lifestyle_cover_without_pricing").trigger("change");
		$("#custom_imprint #custom_activeware_cover_with_pricing").trigger("change");
		$("#custom_imprint #custom_activeware_cover_without_pricing").trigger("change");
		$("#custom_imprint #custom_corporate_cover_with_pricing").trigger("change");
		$("#custom_imprint #custom_corporate_cover_without_pricing").trigger("change");
		$("#personalized_covers #personalized_cover_with_pricing").trigger("change");
		$("#personalized_covers #personalized_cover_with_pricing").trigger("change");
		$("#blank_catalogs #blank_lifestyle_cover_with_pricing").trigger("change");
		$("#blank_catalogs #blank_lifestyle_cover_without_pricing").trigger("change");
		$("#blank_catalogs #blank_activeware_cover_with_pricing").trigger("change");
		$("#blank_catalogs #blank_activeware_cover_without_pricing").trigger("change");
		$("#blank_catalogs #blank_corporate_cover_with_pricing").trigger("change");
		$("#blank_catalogs #blank_corporate_cover_without_pricing").trigger("change");
		$("#blank_catalogs #blank_corporate_cover_without_pricing").trigger("change");
		$("#asi_pricelist #asi_pricelist_qty").trigger("change");
		$("#specialty_catalogs #athletic_apparel_with_pricing").trigger("change");
		$("#specialty_catalogs #ladies_collection_with_pricing").trigger("change");
		$("#specialty_catalogs #enza_collection_with_pricing").trigger("change");
	});
	
	/**
	 * Display Room Kit Orders
	 */
	$(document).ready(function() {

		var enza_racks_large = 0.0;
		var enza_racks_small = 0.0;
		var enza_racks_total = 0.0;
		var enza_racks = { "small" : 129.00, "large" : 229.00 };

		var enza_hangers_shirt = 0.0;
		var enza_hangers_pants = 0.0;
		var enza_hangers_total = 0.0;
		var enza_hangers = { "shirt" : 1.00, "pants" : 1.00	};

		$("#enza_racks #small_enza_rack_qty").change(function() {

			var t = $(this).val();

			if (t == "") {
				enza_racks_small = 0.0;
				retotalEnzaRacks();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			var cost = t * enza_racks['small'];

			enza_racks_small = cost;

			retotalEnzaRacks();

		});

		$("#enza_racks #large_enza_rack_qty").change(function() {

			var t = $(this).val();

			if (t == "") {
				enza_racks_large = 0.0;
				retotalEnzaRacks();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			var cost = t * enza_racks['large'];

			enza_racks_large = cost;

			retotalEnzaRacks();

		});

		$("#enza_hangers #shirt_hanger_qty").change(function() {

			var t = $(this).val();

			if (t == "") {
				enza_hangers_shirt = 0.0;
				retotalEnzaHangers();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			var cost = t * enza_hangers['shirt'];

			enza_hangers_shirt = cost;

			retotalEnzaHangers();

		});

		$("#enza_hangers #pants_hanger_qty").change(function() {

			var t = $(this).val();

			if (t == "") {
				enza_hangers_pants = 0.0;
				retotalEnzaHangers();
				return false;
			}

			if (!isInt(t)) {
				return false;
			}

			var cost = t * enza_hangers['pants'];

			enza_hangers_pants = cost;

			retotalEnzaHangers();

		});

		function isInt(n) {

			var reInteger = /^\d+$/;
			return reInteger.test(n);

		}

		function retotalEnzaRacks () {

			enza_racks_total	 = 	enza_racks_large + 
									enza_racks_small;

			$("#enza_racks .total").text("$" + enza_racks_total.toFixed(2));

		}

		function retotalEnzaHangers () {

			enza_hangers_total	 = 	enza_hangers_shirt + 
									enza_hangers_pants;

			$("#enza_hangers .total").text("$" + enza_hangers_total.toFixed(2));

		}

		$("#enza_racks #small_enza_rack_qty").trigger("change");
		$("#enza_racks #large_enza_rack_qty").trigger("change");
		$("#enza_hangers #shirt_hanger_qty").trigger("change");
		$("#enza_hangers #pants_hanger_qty").trigger("change");
	});
	
	/**
	 * Dashboard
	 */
	$(document).ready(function() {
		$("form#updateSkus").submit(function() {

			/**
			 * Store the available current locations in a dictionary
			 */
			var locations = { 1: "One Stop", 2: "Hanes Outer Banks", 3: "Whispering Pines", 4: "Badger", 5: "Hartwell", 9: "Red Kap", 10: "High Five", 11: "Bayside", 12: "IZOD"};

			/**
			 * Store locations found in the tables
			 */
			var availableLocations = [];

			/**
			 * Store each id into the available locations array
			 */
			var n = 0;
			$("table.location").each(function() {
				availableLocations[n] = this.id;
				n++;
			});
			n = 0;

			/**
			 * Check to see if all fields are empty
			 */
			var checkFields = function (location) {
				var bool = true;
				var locationNumber = location.replace("location-", "").replace("-2", "");
				$("table#" + location + " tr.line td.qty").each(function() {
					var qtyValue = $(this).children("input[@name^='entry']").val();
					if (qtyValue.length == 0) {
						bool = false;
						alert("Please make sure all quantities are filled in.");
						return 0;
					}
				});
				return bool;
			}

			/**
			 * Makes sure that all fields contain integers
			 */
			var checkFieldsInt = function (location) {
				var bool = true;
				var locationNumber = location.replace("location-", "").replace("-2", "");
				$("table#" + location + " tr.line td.qty").each(function() {
					var qtyValue = $(this).children("input[@name^='entry']").val();
					if (qtyValue.length != 0 && !qtyValue.match(/^[0-9]+$/)) {
						bool = false;
						alert("Under the " + locations[parseInt(locationNumber)] + " location, there is invalid input for a quantity.");
						return 0;
					}
				});
				return bool;
			}

			/**
			 * Use Array.every to check all the fields for each location
			 * If all checks return true, return true
			 */
			if (availableLocations.every(checkFields) && availableLocations.every(checkFieldsInt)) {
				return true;
			}

			return false;
		});

		$(".shipping_via").change(function() {
			($(this).val() == "10") ? $(this).next(".pickup_time").show() : $(this).next(".pickup_time").hide();
		});
		
		$("#shippingMethodForm input[@name='continue']").click(function() {
			$(".loading").fadeIn("fast");
		});
		
		$("#shipping_address select[@name='shipping_address']").change(function() {
			
			var number = $(this).val();
			var address = shipping_addresses[number];
			
			$("#shipping_address .address .contact_name").text(address.contact_name);
			$("#shipping_address .address .company_name").text(address.company_name);
			$("#shipping_address .address .address1").text(address.address1);
			
			(address.address2 != '') ? $("#shipping_address .address .address2").text(address.address2) : $("#shipping_address .address .address2").text('');
			
			$("#shipping_address .address .locality").text(address.city + ", " + address.state + " " + address.zip);
			
		});
		
		$(".actions li a.print").click(function() {
			window.print();
		});
		
	});
	
});

var nwindow = '';
function popupShippingMap() {
	
	if (nwindow.location && !nwindow.closed) {
		nwindow.location.href = 'http://media.onestopinc.com/images/info/mill-direct-maps.jpg';
		nwindow.focus();
	} else {
		nwindow = window.open('http://media.onestopinc.com/images/info/mill-direct-maps.jpg', 'Mill Direct Shipping Map', 'width=612, height=753');
	}	
	
}
