// Executes when user clicks on a swatch	
function handleMaterialSelection(sap_product_code, sap_material_code, sel, color_status, catalogMode, is_employee_discountable, max_qty) {
	//alert("sap_product_code=" + sap_product_code + ", sap_material_code=" + sap_material_code + ", sel=" + sel + ", color_status=" + color_status);
	var sap_product_code_lc = sap_product_code.toLowerCase();
	var ar = arVs[sap_product_code_lc];

	// Remove the class color_down from everything, then add it to the selected material
	$("#productColor .color_out").removeClass("color_down");
	var clicked_selector = "#productColor .mat-" + sap_material_code;
	$(clicked_selector).addClass("color_down");

	document.cartForm.sap_variant_sku.value = '';
	selected_material_code[sap_product_code_lc] = sap_material_code;
	setImageBySelectedMaterialCode(sap_product_code, sap_material_code);
	setColorStatus(color_status, true);
	
	if( catalogMode == 'false'){
		setDisplayPriceFromCurrentSelections();
		var qty_selected = document.cartForm.qty_value.value - 1;
		if (document.getElementById('idQtyDropdown').options.length == 1) {
			document.getElementById('idQtyDropdown').options.length = 0;
			for (var i = 1; i < max_qty; i++) {
					document.getElementById('idQtyDropdown').options[document.getElementById('idQtyDropdown').options.length] = new Option(i, i);
			}
			
			$("#idQtyDropdown").attr("selectedIndex",qty_selected);
		}
	
		// i18n:  Disable the select option evaluation if in catalog mode
		if (sel != undefined) {
			populateVariantSelect(sap_product_code, sap_material_code, sel);
		} 
		setDisplayPriceFromCurrentSelections();
	}

	// Display the selected Style/Color to the user
	var current_sku_style_code = "";
	var current_sku_color_code = "";
	var current_split = sap_material_code.split("-",2);
	if(current_split[0]!=undefined) current_sku_style_code = current_split[0];
	if(current_split[1]!=undefined) current_sku_color_code = current_split[1];
	$("#idSkuStyle").text(current_sku_style_code);
	$("#idSkuColor").text(current_sku_color_code);

	//  i18n: Catalog Sites - Add logic to toggle the buy now / find a retailer links
	if( catalogMode == 'true'){

		// Loop over the materials		
		for (var i = 0; i < ar.length; i++) {
			
			// Find the active / selected one
			if (ar[i].sap_material_code == selected_material_code[sap_product_code_lc]) {
				
				// Was a valid link provided?
				if( ar[i].url_link && ar[i].url_link.length > 0){
					
					// Hide / show the retailer / buy online links
					$("#findARetailerSpan").hide();
					$("#buyOnlineSpan").show();
					
					// Set the url of the buy online link
					$("#buyOnlineLink").attr("href", ar[i].url_link);
					
					// exit the loop;
					break;
									
				} else {
					
					// Hide / show the retailer / buy online links
					$("#buyOnlineSpan").hide();
					$("#findARetailerSpan").show();
										
				}
			}
		}
	} else {
		if( is_employee_discountable == 0 )
			$('#employeeDiscountMessage').show();
		else
			$('#employeeDiscountMessage').hide();
		
	}
	
}

function setImageBySelectedMaterialCode(sap_product_code, sap_material_code) {
	var imageName = getProductImageForMaterialCode(sap_product_code);
	
	//if (typeof(insertZoomViewer) == "function") insertZoomViewer(imageName, sap_product_code);
	changeImageSet(sap_product_code, sap_material_code);
}

function getProductImageForMaterialCode(sap_product_code) {
	var sap_product_code_lc = sap_product_code.toLowerCase();
	var ar = arVs[sap_product_code_lc];
	for (var i = 0; i < ar.length; i++) {
		if (ar[i].sap_material_code == selected_material_code[sap_product_code_lc]) {
			return ar[i].product_image;
		}
	}

	return null;
}

// Executes when page loads, and after user clicks on a swatch
function populateVariantSelect(sap_product_code, sap_material_code, sku) {
	var sap_product_code_lc = sap_product_code.toLowerCase();
	var ar  = arVs[sap_product_code_lc];
	var isFound = false;
	// set product color
	selected_material_code[sap_product_code_lc] = sap_material_code;
	setImageBySelectedMaterialCode(sap_product_code, sap_material_code);
	
	// Determine if a size is set
	var size = '';
	var current_split = sku.split("-",3);
	if(current_split[2]!=undefined) size = current_split[2].replace('.','');
	
	$("#idNoInventoryMessage").css("display","none");
	
	// Is there at least 1 sku for sale?
	for(idx=0; idx<ar.length; idx++) {	
		if(ar[idx]["sap_material_code"]==sap_material_code && ar[idx]["qty"]>0) {
			isFound = true;
			setColorStatus(ar[idx]["color_name"], true);
			break;
		}
	}

	/* Show or hide select box*/
	if( isFound == false ) {
		document.getElementById('idQtyDropdown').style.display = 'none';
		document.getElementById('add_to_cart_btn').style.display = 'none';
		$("#qty_select_text_overlay").css('display', '');
	}
	else {
		document.getElementById('idQtyDropdown').style.display = 'block';
		document.getElementById('add_to_cart_btn').style.display = 'block';
		$("#qty_select_text_overlay").css('display', 'none');
	}
	
	/* Disable all size buttons */
	for (var i = 0; i < ar.length; i++) {
		var field = 'size_' + ar[i].sku_size_code.replace('.','');
		document.getElementById(field).className="size_button_disable tool";
		document.getElementById(field).disabled="true";
	}
	
	/* Enable only those that have inventory */
	for (var i = 0; i < ar.length; i++) {
		if(ar[i].sap_material_code==sap_material_code) {
			var field = 'size_' + ar[i].sku_size_code.replace('.','');
			document.getElementById(field).className="size_button tool";
			document.getElementById(field).name   = ar[i].sap_variant_sku;
			document.getElementById(field).disabled="";
			if (ar[i].sku_size_code.replace('.','') == size) {
				document.cartForm.sap_variant_sku.value = sku;
				document.cartForm.size_name.value = ar[i].sku_size_code;
				document.getElementById('size_' + size.replace('.','')).className="size_button_selected tool";
			}
		}
	}
	
	/* Highlight the first available size */
	if ( size != '' && document.getElementById('size_' + size.replace('.','')).className != "size_button_selected tool" ) {
		document.cartForm.sap_variant_sku.value = '';
		$("#idNoInventoryMessage").css("display","block");
		$("#idSkuSize2").text("Out of Stock");
		// Populate select box.  The first item is "Select"
		document.getElementById('idQtyDropdown').options.length = 0;
		document.getElementById('idQtyDropdown').options[0] = new Option("0","");
	} /* THIS SETS A DEFAULT SIZE 
	else if (size == '') {
		document.cartForm.sap_variant_sku.value = sap_material_code + '-' + ar[0].sku_size_code;
		document.cartForm.size_name.value = ar[0].sku_size_code.replace('.','');
		document.getElementById('size_' + ar[0].sku_size_code.replace('.','')).className="size_button_selected tool";
	}*/
	
	// Display the selected Style/Color to the user
	var current_sku_style_code = "";
	var current_sku_color_code = "";
	var current_split = sap_material_code.split("-",2);
	if(current_split[0]!=undefined) current_sku_style_code = current_split[0];
	if(current_split[1]!=undefined) current_sku_color_code = current_split[1];
	$("#idSkuStyle").text(current_sku_style_code);
	$("#idSkuColor").text(current_sku_color_code);
	
}

function setColorStatus(msg, make_selected) {
	$("#colorMsg").empty().append(msg);

	if (make_selected) {
		selected_color_msg = msg;
	}
}

function restoreColorStatus() {
	$("#colorMsg").empty().append(selected_color_msg);
}
