//<xsl:template match="product" mode="js">
function addToWishList(l, s) {
	var sStyle = $('#itemnumber_1').val();
	if (sStyle == '') {
		HandleAlertMessage('You must choose style information (size, color) before you add this item to your wish list.', null, 'notice');
	}
	else {
		trackAddToWishList(l, s);
		var sEDP = $('#edp_1').val();
		$('#wl-item').val(sEDP);
		$('#wl-update-form').submit();
	}
}

function giftToggle() {
	if (document.prodform.giftbag.checked) {
		$('#giftcolor').show();
	} else {
		$('#giftcolor').hide();
	}
}

var product = { "l1": null, "l2": null, "l3": null, "l1val": "", "l2val": "", "l3val": "", "styleCount": 0, "pricelevel": 1 }
var inproc = false;

function InitStyleElements() {
	product.l1 = $('select[name="l1"], input[name="l1"]');
	product.l2 = $('select[name="l2"], input[name="l2"]');
	product.l3 = $('select[name="l3"], input[name="l3"]');

	product.styleCount = styles.length;
	if (product.l1.length > 0)
		ReloadStyles(product.l1[0], 0);
}
function GetStyleObj(level) {
	switch (level) {
		case 1:
			return product.l1;
		case 2:
			return product.l2;
		case 3:
			return product.l3;
		default:
			return undefined;
	}
}
function SetSelectedValues() {
	//get current style selections
	if (product.l1.length > 0)
		product.l1val = product.l1.val();
	if (product.l2.length > 0)
		product.l2val = product.l2.val();
	if (product.l3.length > 0)
		product.l3val = product.l3.val();
}
function GetSelectedValue(level) {
	if (level == 1)
		return product.l1val;
	if (level == 2)
		return product.l2val;
	if (level == 3)
		return product.l3val;
	return "";
}
function GetNodeName(obj) {
	if (obj.nodeName)
		return obj.nodeName.toLowerCase();
	else
		return "";
}
/*
1. Test for Color
2. Get level
3. Clear all levels greater than the current level
4. Set next level options based on levels equal to and less than current level
5. If at last level, set item no
	
//level will line up with the correct value in prodStyles array for next style to update
//0 = l1 styles, 1 = l2 styles, 2 = l3 styles;
*/
function ReloadStyles(obj, level) {
	//if no styles, fall out
	if (!prodStyles || prodStyles.length == 0)
		return;
	//lock function from running while in progress
	if (!inproc) {
		$('#error-message').hide();
		inproc = true;
		//get price level
		product.pricelevel = ($('#product_pricelevel').length > 0 ? $('#product_pricelevel').val() : $('input[name="pricelevel"]:checked').val());

		//if only style option avail is color, product color hidden input passed in, do look up on current 'here' color
		if ($(obj).attr('id') == 'product_color')
			obj = $('#detailswatchnav div.here a')[0];

		//get current selections
		SetSelectedValues();

		//clears the styles for levels greater than the current level
		ClearStyles(level);

		//check for next set of options, call to set available values
		//set available styles for the next style selector
		if (level < prodStyles.length)
			SetAvailableStyles(level, obj);

		//validates object if it is the current selection and color style
		ValidateColor(obj, level);

		//get current selections
		SetSelectedValues();
		ReloadItemNumber();
	}
	inproc = false;
}

function ClearStyles(level) {
	//clear levels greater than the current selection
	for (var level_counter = level; level_counter < prodStyles.length; level_counter++) {
		var style_level_element = GetStyleObj(level_counter + 1);
		//if select object, remove all options but index 0
		if (GetNodeName(style_level_element[0]) == "select")
			style_level_element.children('option:not(:first)').remove();
		//if it's the color selection, then clear all available classes
		else if (style_level_element[0].id == "product_color")
			$('#detailswatchnav div a').removeClass('available').removeClass('unavailable');
		var step = (level_counter + 1);
		if (level_counter > level) {
			$('#step-' + step).removeClass('here');
			style_level_element.attr("disabled", "disabled");
		} else {
			$('#step-' + step).addClass('here');
			style_level_element.removeAttr("disabled");
		}
	}
}

function SetAvailableStyles(level, obj) {
	//test if object being passed in is a color style
	var isColor = ((GetNodeName(obj) == "a" || obj.id == "product_color") ? true : false);
	//get the next style element
	var nextStyleElement = GetStyleObj(level + 1);
	//see if next element is color style
	var nextIsColor = (nextStyleElement.attr('id') == 'product_color' ? true : false);
	//get next style array
	var nextStyleArray = prodStyles[level];

	//set can_sell to false for all
	for (var arrayIdx = 1; arrayIdx < nextStyleArray.length; arrayIdx++) {
		nextStyleArray[arrayIdx][3] = 0;
	}
	//loop through all style items	
	for (var i = 0; i < product.styleCount; i++) {
		//if style is not in the currente pricelevel, continue
		if (styles[i][5] != product.pricelevel)
			continue;

		//loop through all items in the next style array
		for (var arrayIdx = 1; arrayIdx < nextStyleArray.length; arrayIdx++) {
			//based on current level, test for available options
			switch (level) {
				//set l1 options                          
				case 0:
					//compare style code to style array style code
					if (styles[i][2] == nextStyleArray[arrayIdx][1])
						nextStyleArray[arrayIdx][3] = 1; //<!-- setting this to 1 lets the process know that this option is ok to list-->
					break;
				//set l2 options                          
				case 1:
					//compare style code to style array style code and to parent selection
					if ((styles[i][3] == nextStyleArray[arrayIdx][1]) && (styles[i][2] == product.l1val))
						nextStyleArray[arrayIdx][3] = 1; //<!-- setting this to 1 lets the process know that this option is ok to list-->
					break;
				//set l3 options                          
				case 2:
					//compare style code to style array style code and to parent's selections
					if ((styles[i][4] == nextStyleArray[arrayIdx][1]) && (styles[i][2] == product.l1val) && (styles[i][3] == product.l2val))
						nextStyleArray[arrayIdx][3] = 1; //<!-- setting this to 1 lets the process know that this option is ok to list-->
					break;
			}
		}
	}
	//update next selection options
	for (var arrayIdx = 1; arrayIdx < nextStyleArray.length; arrayIdx++) {
		if (nextIsColor) {
			var color = $('#color_' + nextStyleArray[arrayIdx][1]);
			if (nextStyleArray[arrayIdx][3] == "1") {
				color.addClass('available');
			} else {
				color.addClass('unavailable');
			}
		} else {
			if (nextStyleArray[arrayIdx][3] == "1") {
				nextStyleElement.append('<option value="' + nextStyleArray[arrayIdx][1] + '">' + nextStyleArray[arrayIdx][0] + '</option>');
			}
		}
	}
	//default to first selection if only one option available
	if (!nextIsColor && nextStyleElement.children().length == 2) {
		//set to only available option
		nextStyleElement[0].selectedIndex = 1;
		//update selections
		SetSelectedValues();
		//if there's an N2 level, then set the styles based on the selection we just made
		//i.e. if we're not at the last level, call functions as if user had made the above selection
		if ((level + 1) < prodStyles.length) {
			ClearStyles(level + 1);
			SetAvailableStyles(level + 1, nextStyleElement);
		}
	} else if (nextIsColor) {
		ValidateColor($('#detailswatchnav div.here a')[0], level + 1);
	}
}
function ValidateColor(obj, level) {
	//available colors for all style combinations show all the time
	//if we have a color style object..
	if (GetNodeName(obj) == "a") {
		//does current selection have 'available' class
		if ($(obj).hasClass('available')) {
			$('#product_color').val($(obj)[0].id.replace('color_', ''));
			$('#prod_color_notavail').hide();
		}
		//else see if only available option is color, or if previous selection has been made
		else if (level <= 1 || GetSelectedValue(level - 1) != "") {
			$('#product_color').val('');
			$('#prod_color_notavail').show();
		}
		//otherwise clear color value selection and hide error message
		else {
			$('#product_color').val('');
			$('#prod_color_notavail').hide();
		}
	}
}
function ReloadItemNumber() {
	var selection = ((product.l1val != "") || !document.prodform.l1) && ((product.l2val != "") || !document.prodform.l2) && ((product.l3val != "") || !document.prodform.l3);
	if (selection) {
		for (var i = 0; i < styles.length; i++) {
			//<!-- this test needs to go insteps for each level. -->

			//check that style matches selected pricelevel
			if ((styles[i][5] == product.pricelevel)) {

				//check that style matches l1 selection or l1 does not exist
				if ((styles[i][2] == product.l1val) || product.l1.length == 0) {

					//check that style matches l2 selection or l2 does not exist
					if ((styles[i][3] == product.l2val) || product.l2.length == 0) {

						//check that style matches l3 selection or l3 does not exist
						if ((styles[i][4] == product.l3val) || product.l3.length == 0) {

							//set itemnumber and edp fields
							$('#itemnumber_1').val(styles[i][1]);
							$('#edp_1').val(styles[i][0]);
							//fall out of loop
							break;
						}
					}
				}
			}
		}
	}
	//otherwise clear the edp and item no
	else {
		$('#edp_1').val('');
		$('#itemnumber_1').val('');
	}
}

var curImgPath; var curImgType = 'swatch';

function doZoom(main_image_source, style_cd, pageid) {
	if (curImgPath == undefined)
		curImgPath = main_image_source;
	openWindow('/site/components/zoomtool/scripts/popup.asp?mode=popupdiv&path=' + curImgPath + '&width=1200&height=1200&pageid=' + pageid, 'imagepopup', 535, 559);
	var s = window.s;
	s.linkTrackVars = 'products,events';
	if (curImgType == 'swatch') {
		s.linkTrackEvents = 'event13'; s.events = 'event13';
	} else {
		s.linkTrackEvents = 'event14'; s.events = 'event14';
	}
	s.products = ';' + style_cd + ';;;;evar10=large view: ' + curImgType;
	s.tl(true, 'o', 'large view: ' + curImgType);
	return true;
}
function ProductAddToCart() {
	if ($.trim($('#itemnumber_1').val()).length == 0) {
		HandleAlertMessage(' Not so fast! You haven\'t made all your product choices yet. Please complete all selections above and try again.', null, 'alert');
	}
	else if ($('#sendto').val() == 'Add New' && $('#new-address').val() == '') {
		HandleAlertMessage('Please enter a name for this ship-to address.', null, 'notice');
	}
	else {
		$('#error-message').hide();
		var url = (("https:" == document.location.protocol) ? "https://" : "http://") + document.location.host + '/default.asp?processor=asp&asp_processor=store&action=addtocart&rngn=xml';
		$.post(url, $('#ecom_add_to_cart').serialize(), function(xml) {
			var postJson = {};
			//build json object from posted fields
			$(xml).find('form field').each(function() {
				postJson[$(this).attr('id')] = $(this).text();
			});
			//update cart count in header
			var cartCount = getCookieValue('cartcount');
			$('#cartlink').text('Shopping Cart (' + cartCount + ')');
			//track add to cart
			trackCartAdd(null, cartCount, postJson.quantity_1, postJson.sku_1);

			//check for non-custom item and show 'upsellerator'
			if (postJson.upsellerator && postJson.upsellerator == "1") {
				//render related products & update modal window
				postJson.width = 97;
				postJson.height = 97;
				postJson.limit = 10;
				postJson.ev3 = "cart-upsell";
				postJson.visibleItems = 5;
				postJson.target = $('#modal_upsell_products');
				//render upsell products and return cart summary
				renderUpsellProducts(postJson);
				//initScrollable('#also-bought', $('#also-bought div.itembox').length);
				ShowUpsellerator();
			} else {
				$('#cus-sectionpath').val(postJson.nextsection);
				$('#cus-cartitemid').val(postJson.cartitemid);
				$('#cus-itemnumber').val(postJson.itemnumber_1);
				$('#customize-form').submit();
			}
		}, 'xml');
	}
	return false;
}
/*<div id="modalwindow-addtocart">*/
function UpdateProductCartSummary(postJson, cartSummary) {
	//set main image source
	$('#product_image_main_modal').attr('src', $('#product_image_main').attr('src').replace('395x395', '97x97'));

	//todo update header: addressbookname
	if (postJson.addressbookname && postJson.addressbookname != "")
		$('#addressbook_name').text(postJson.addressbookname + '\'s');
	else
		$('#addressbook_name').text('your');

	$('#modal_price').text('$' + cartSummary.price);

	if (cartSummary.sell_cd == 'A')
		$('#modal_stock').text('In Stock');
	else if (cartSummary.sell_cd == 'B')
		$('#modal_stock').text('Backordered Until ' + cartSummary.backorder_dt);
	else if (cartSummary.sell_cd == 'U')
		$('#modal_stock').text('No longer available');
	else
		$('#modal_stock').text('');

	//update style info if there is any
	if (cartSummary.style_description) {
		$('#modal_style').text($.trim(cartSummary.style_description));
	} else {
		//otherwise hide the style display
		$('#modal_style').text('');
	}
	//update quantity added to cart
	$('#modal_qty').text('Quantity: ' + postJson.quantity_1);
}
function GetStyleName(level, code) {
	var val = code;
	if (prodStyles[level]) {
		var l = prodStyles[level].length;
		for (var i = 0; i < l; i++) {
			if (prodStyles[level][i][1] == code) {
				val = prodStyles[level][i][0];
				break;
			}
		}
	}
	return val;
}
function SetQuickLookHere() {
	var main_src = $('#item-img img').attr('src');
	if (main_src) {
		$('#detailswatchnav a').each(function() {
			if (this.href == main_src)
				$(this).trigger('click');
		});
	}
}
