// if default color is not available select the first one available (use the function that is already created)
function skuPicker(skusArray,idPrefix,type,imgArray,checkAddToBag) 
{
	var that = this;
	this.skusArray = skusArray;
	this.multiSkusArray = new Array();
	this.imagesArray = imgArray;
	this.defaultColor = this.imagesArray[0];
	this.idPrefix = idPrefix;
	this.type = type;

	this.hasWidth = false;
	this.hasSize = false;
	this.hasSC_01 = false;
	this.hasSC_02 = false;
	this.hasSC_03 = false;
	this.defaultSkusArrayName = "";
	this.sizeClassPrefix = 'SC_';

	this.addToBagDetails = new addToBagDetail();
	this.selectionStates = null;
	this.detailViewer = null;
	this.selectedImageType = "Main";
	this.checkAddToBag = checkAddToBag;
	this.MiniCart = null;
	this.hasSale = false;
	this.LAST_ADDED_ITEM=null;
	var that = this;
	this.unavailableMsgId = idPrefix+'unavailableMsg';

	this.init = function()
	{
		this.checkErrorMessageArea();
		this.checkIfHasWidthSize();
		this.createMultiSkusArray();

		this.BAG = "BAG";
			
		for (var img in this.imagesArray)
		{
			new Image().src = this.imagesArray[img].PD[this.selectedImageType];
		}
		switch(this.type)
		{
			case 'productLocator':
				this.submitProdLoc = new submitProductLocator(this);
				this.submitProdLoc.init();
				break;
			case 'productDetail':
				break;
			case 'quickView':
				break;
		}

		this.selectDefaultColor();

		this.selectionStates = new skuPickingLogic(this);
		this.selectionStates.init();

		this.selectionStates.selectedSizeClass = this.defaultSkusArrayName;
		this.selectionStates.clearSelection();
		this.selectionStates.changeSizeClass();
		this.selectionStates.populateAll();
		try{$(that.idPrefix+this.defaultSkusArrayName).className = 'selectedSizeClass';}catch(err){}
		this.selectionStates.selectedColor = this.defaultColor;
        this.selectionStates.availForColor();
		this.selectionStates.fillPreselections();
		this.createMiniCart();

        this.printViewMoreLinks(that.defaultColor);
        this.printEnlargeViewMoreLinks(that.defaultColor);

		this.selectionStates.selectIfSingle();
		this.selectionStates.setMessage();
		this.selectionStates.highLightSelection();
		
		this.initImageTypeSelectors();
		if (this.checkAddToBag == true) {
			this.initAddToBagButton();
		}
		$(that.idPrefix+'locErrText').style.visibility = 'visible';
		this.adjustPromoArea();
	}
	this.adjustPromoArea = function(){
		var promoDivContainer = $('promotionsSpan');
		var maxPromoNum = $('maxPromoNum')
		var promoHeight = 0;
		if(maxPromoNum){
			var intmaxPromoNum = parseInt(maxPromoNum.value);
			if(promoDivContainer && !isNaN(intmaxPromoNum) && intmaxPromoNum > 0){
				promoHeight = 16 * intmaxPromoNum;
				promoDivContainer.style.height = promoHeight+'px';
				promoDivContainer.style.display = 'block';
			}
		}
	}
	this.selectDefaultColor = function()
    {
        var defaultImg = that.imagesArray._default.PD[that.selectedImageType];
        var defaultColor;
        var backupDefaultColor = null;
        var defaultHasNoAvailability = true;
        for (var img in that.imagesArray)
        {
            if(defaultImg.indexOf(img) != -1)
            {
                defaultColor = img;
                break;
            }   
        }
       
        for (sku in that.skusArray) { // check to make sure default is available
            if (backupDefaultColor == null && that.skusArray[sku].availability != "NOT_AVAILABLE") {
                backupDefaultColor = that.skusArray[sku].color_code; // grab an available color just in case
            }
            if(that.skusArray[sku].color_code == defaultColor && that.skusArray[sku].availability != "NOT_AVAILABLE"){
                defaultHasNoAvailability = false;   
                break;
            }
        }
        if(defaultHasNoAvailability){
            defaultColor = backupDefaultColor;   
        }
        that.defaultColor = defaultColor;   
       
    }
	
	this.printViewMoreLinks = function(selectedColorCode)
	{
		var dataContainer = $$('#viewsMenu input.viewMoreData');
		var viewMoreLinks = this.getViewMoreLinksData(selectedColorCode, dataContainer);
		
		for (var i = 0; i < viewMoreLinks.length; i++) {
			var element = '<span class="imageTypeSelector">' + viewMoreLinks[i]['type'] + '</span>';
			element += (i + 1) != viewMoreLinks.length ? '<span class="dividerBar">|</span>' : '';
			if (skuPickerConstants.viewMoreLinksBefore == true) {
				$('heading').insert({before: element});
			} else {
				var divider = $$('#viewsMenu .dividerBar');
				if (!divider || divider.length == 0) { 
					$('heading').insert({after: element});
				} else {
					divider[divider.length - 1].insert({after: element});
				}
			}
		}
	}
	this.highlightSelectedImageType = function() 
	{
		var that = this;
		$$('#viewsMenu span.imageTypeSelector,#enlargeDesc .imageTypeSelector').each(function(element) {
			var elText = element.textContent ? element.textContent: (element.innerText ? element.innerText : null);
			if (elText != null && that.selectedImageType ==elText.replace('|','')) {
				element.addClassName('selected');
			} else {
				element.removeClassName('selected');
			}
		});
	}
	this.printEnlargeViewMoreLinks = function(selectedColorCode)
	{
		var dataContainer = $$('#enlargeDesc input.viewMoreData');
		var viewMoreLinks = this.getViewMoreLinksData(selectedColorCode, dataContainer);
		
		for (var i = 0; i < viewMoreLinks.length; i++) {
		
			var element = '<a href="#" class="imageTypeSelector">' + viewMoreLinks[i]['type'] + '</a>';
			element += (i + 1) != viewMoreLinks.length ? ' | ' : '';
			
			$('enlargeDesc').insert({bottom: element});
		}
	}
	this.getViewMoreLinksData = function(selectedColorCode, dataContainer)
	{
		var viewMoreLinks		= new Array();
		var viewMoreLinksCount	= 0;
		
		for (var i = 0; i < dataContainer.length; i++) {
		
			if(dataContainer[i] != null) {
				
				var element		= dataContainer[i].value;
				var splitEl		= element.split('|');
				
				var colorCode	= (splitEl.length >= 1) ? splitEl[0] : '';
				var imageType	= (splitEl.length >= 2) ? splitEl[1] : '';
				
				if((colorCode == selectedColorCode) && (imageType != null)) {
				
					viewMoreLinks[viewMoreLinksCount]			= new Object();
					viewMoreLinks[viewMoreLinksCount]['type']	= imageType;
					viewMoreLinksCount++;
				}
			}
		}
		
		return viewMoreLinks;
	}
	this.createMultiSkusArray = function()
	{
		this.multiSkusArray.SC_03 = {};
		this.multiSkusArray.SC_03Colors = new Array();
		this.multiSkusArray.SC_03Widths = new Array();
		this.multiSkusArray.SC_03Sizes = new Array();

		this.multiSkusArray.SC_01 = {};
		this.multiSkusArray.SC_01Colors = new Array();
		this.multiSkusArray.SC_01Widths = new Array();
		this.multiSkusArray.SC_01Sizes = new Array();
		
		this.multiSkusArray.SC_02 = {};
		this.multiSkusArray.SC_02Colors = new Array();
		this.multiSkusArray.SC_02Widths = new Array();
		this.multiSkusArray.SC_02Sizes = new Array();

		for(sku in that.skusArray){
			that.skusArray[sku].standardPrice = '$'+Number(that.skusArray[sku].standardPrice).toFixed(2);
			that.skusArray[sku].salesPrice = '$'+Number(that.skusArray[sku].salesPrice).toFixed(2);
			switch(that.skusArray[sku].sizeClass){
				case ('SC_02'):
					that.hasSC_02 = true;
					that.multiSkusArray.SC_02[sku] = that.skusArray[sku];
					that.multiSkusArray.SC_02Colors.push(that.skusArray[sku].color_code);
					that.multiSkusArray.SC_02Widths.push(that.skusArray[sku].width);
					that.multiSkusArray.SC_02Sizes.push(that.skusArray[sku].size);
					break;
				case ('SC_03'):
					that.hasSC_03 = true;
					that.multiSkusArray.SC_03[sku] = that.skusArray[sku];
					that.multiSkusArray.SC_03Colors.push(that.skusArray[sku].color_code);
					that.multiSkusArray.SC_03Widths.push(that.skusArray[sku].width);
					that.multiSkusArray.SC_03Sizes.push(that.skusArray[sku].size);
					break;
				default:
					that.hasSC_01 = true;
					that.multiSkusArray.SC_01[sku] = that.skusArray[sku];
					that.multiSkusArray.SC_01Colors.push(that.skusArray[sku].color_code);
					that.multiSkusArray.SC_01Widths.push(that.skusArray[sku].width);
					that.multiSkusArray.SC_01Sizes.push(that.skusArray[sku].size);
					break;
			}
		}
		that.multiSkusArray.SC_02Colors = that.multiSkusArray.SC_02Colors.uniq();
		that.multiSkusArray.SC_02Widths = that.multiSkusArray.SC_02Widths.uniq();
		that.multiSkusArray.SC_02Sizes = that.multiSkusArray.SC_02Sizes.uniq();

		that.multiSkusArray.SC_03Colors = that.multiSkusArray.SC_03Colors.uniq();
		that.multiSkusArray.SC_03Widths = that.multiSkusArray.SC_03Widths.uniq();
		that.multiSkusArray.SC_03Sizes = that.multiSkusArray.SC_03Sizes.uniq();
			
		that.multiSkusArray.SC_01Colors = that.multiSkusArray.SC_01Colors.uniq();
		that.multiSkusArray.SC_01Widths = that.multiSkusArray.SC_01Widths.uniq();
		that.multiSkusArray.SC_01Sizes = that.multiSkusArray.SC_01Sizes.uniq();

		var inFlames = 0;
		if(that.hasSC_01)
			inFlames += 1;
		if(that.hasSC_02)
			inFlames += 1;
		if(that.hasSC_03)
			inFlames += 1;
		$(that.idPrefix+'sizeClassTabs').className = "sizeClassTabs"+inFlames;

		if(that.hasSC_01){
			that.defaultSkusArrayName = 'SC_01';
		} else if(that.hasSC_02){
			that.defaultSkusArrayName = 'SC_02';
		} else if(that.hasSC_03){
			that.defaultSkusArrayName = 'SC_03';
		} else{
			that.defaultSkusArrayName = 'SC_01';
		}

		that.skusArray = that.multiSkusArray[that.defaultSkusArrayName];
	}
	this.checkIfHasWidthSize = function()
	{
		that.hasWidth = true;
		that.hasSize = true;
		that.hasSizeClass = true;
		
		var firstSku = null;
		for(var sku in that.skusArray)
		{
			firstSku = sku;
			break;
		}

		if(firstSku != null)
		{
			if(!that.skusArray[firstSku].width)
				that.hasWidth = false;
			if(!that.skusArray[firstSku].size)
				that.hasSize = false;
			if(!that.skusArray[firstSku].sizeClass)
				that.hasSizeClass = false;
		} else {
			that.hasSizeClass = false;
			that.hasSize = false;
			that.hasWidth = false;
		}
		if(!that.hasWidth)
		{
			var tmp=document.createElement('span');
			tmp.title="H";
			tmp.id=that.idPrefix+"H";
			tmp.className=that.idPrefix+"CARTwidth";
			tmp.appendChild(document.createTextNode("H"));
			
			tmp.chipType == 'widthChip';
			var tmp4=document.createElement('div');
			tmp4.id=that.idPrefix+"currentWidth";
			
			var tmp5=document.createElement('div');
			tmp5.id=that.idPrefix+"errWidth";
			
			var widths=document.createElement('div');
			widths.id=that.idPrefix+"widths";
			widths.appendChild(tmp5);
			widths.appendChild(tmp4);
			widths.appendChild(tmp);
			widths.style.display="none";
			document.body.appendChild(widths);
			
			for (var sku in that.skusArray) 
			{
				that.skusArray[sku].width = 'H';
				that.skusArray[sku].width_description = 'H';
			}
		}
		if(!that.hasSize)
		{
			var tmp2=document.createElement('span');
			tmp2.title="S";
			tmp2.id=that.idPrefix+"S";
			tmp2.className=that.idPrefix+"CARTsize";
			tmp2.chipType == 'sizeChip';
			tmp2.appendChild(document.createTextNode("S"));
			
			var tmp3=document.createElement('div');
			tmp3.id=that.idPrefix+"currentSize";
			tmp.appendChild(tmp3);
			var tmp6=document.createElement('div');
			tmp6.id=that.idPrefix+"errSize";

			var sizes=document.createElement('div');
			sizes.id=that.idPrefix+"sizes";
			sizes.appendChild(tmp6);
			sizes.appendChild(tmp3);
			sizes.appendChild(tmp2);
			sizes.style.display="none";
			document.body.appendChild(sizes);
			
			for (var sku in that.skusArray) 
			{
				that.skusArray[sku].size = 'S';
				that.skusArray[sku].size_description = 'S';
			}
		}
		if(!that.hasSizeClass)
		{
			var tmp=document.createElement('div');
			tmp.id=that.idPrefix+"sizeClassTabs";
			tmp.style.display="none";
			tmp.innerHTML='<div title="Missy" id="'+that.idPrefix+'SC_01" class="'+that.idPrefix+'CARTSizeClass">Missy</div>';
			document.body.appendChild(tmp);
		} else {
			var sizeTabs = $(that.idPrefix+'sizeClassTabs');
			var pickerArea = $(that.idPrefix+'pickerArea');

			pickerArea.insert({before: sizeTabs});
			pickerArea.className = "pickerArea SPbox";
			sizeTabs.style.display = 'block';
		}
	}
	this.initImageTypeSelectors = function() {
		var imageTypeSelectorsMain = $$('#viewsMenu span.imageTypeSelector');
		for (var i = 0; i < imageTypeSelectorsMain.length; i++) {
			var imageType = imageTypeSelectorsMain[i];
			imageType.imageHandler = this;
			imageType.onclick = this.imageTypeSelectorHandler;
		}
		var imageTypeSelectorsEnlarge = $$('#enlargeDesc .imageTypeSelector');
		for (var i = 0; i < imageTypeSelectorsEnlarge.length; i++) {
			var imageType = imageTypeSelectorsEnlarge[i];
			imageType.imageHandler = this;
			imageType.onclick = this.imageTypeSelectorHandler;
		}
		this.highlightSelectedImageType();
	}
	this.imageTypeSelectorHandler = function() {
		var imageHandler = this.imageHandler;
		var textContent = null;
		if (this.textContent != null) {
			textContent = this.textContent;
		} else if (this.innerText != null) {
			textContent = this.innerText;
		}
		if (textContent != null) {
			var selected = textContent.replace('|', '');
			imageHandler.switchImageType(selected);
		}
		return false;
	}
	this.switchImageType = function(type) {
		this.selectedImageType = type;
		this.selectionStates.highLightSelection();
		this.highlightSelectedImageType();
	}
	this.initAddToBagButton = function() {
		var button = $(this.idPrefix + 'addToCartButton');
		if (button != null) {
			button.onclick = this.addToBagClick;
		}
	}
	this.addToBagClick = function() {
		if ($(that.idPrefix+'pid').value != '' && $(that.idPrefix+'availability').value != '' &&  $(that.idPrefix+'availability').value != 'NOT_AVAILABLE') {
			if (that.MiniCart != null) {
				miniCart.addToCart(); 
				return false;
			}
			return true;		
		} else {
			that.selectionStates.setMessage('force');
			return false;
		}
	}

	this.checkErrorMessageArea = function(){
		if(!$(that.idPrefix + "errMessageHolder")){
			var emh = document.createElement('div');
			emh.id=that.idPrefix + "errMessageHolder";
			emh.style.display = 'none';
			emh.className="errMessageHolderClass";
			emh.innerHTML = '<div class="errMessageHolderTop"></div><div align="center" class="errorText" id="errorText"></div><div class="errMessageHolderBottom"></div>';
			if($('quickViewImgHolder')){
				$('quickViewImgHolder').appendChild(emh);
			}else{
				document.body.appendChild(emh);
			}
		}
		if(!$(that.idPrefix + "inTheCart")){
			var itc = document.createElement('div');
			itc.id=that.idPrefix + "inTheCart";
			itc.style.display = 'none';
			itc.className="inTheCart";
			itc.innerHTML='<div class="inTheCartTop"></div><div align="center" class="inTheCartText" id="inTheCartText">THIS ITEM HAS BEEN ADDED TO YOUR BAG</div><div class="inTheCartBottom"></div>';
			if($('quickViewImgHolder')){
				$('quickViewImgHolder').appendChild(itc);
			}else{
				document.body.appendChild(itc);
			}
		}
	}

	this.createMiniCart = function ()
	{	var a= 0;
		miniCart=null;
		miniCart = 
		{
			form: null,
			ajaxAction: null,
			ajaxWishAction: null,
			viewer: null,
			picker: null,
			clicked: null,
			
			initialize: function() {
				var addToCartButton = $('addToCartButton');
				if (window.$('addToWishListButton') && $('addToWishListButton').id == 'addToWishListButton') var addToWishListButton = $('addToWishListButton');
				if (addToCartButton != null) {
					this.MiniCart = true;
					this.form = addToCartButton.form;
					this.ajaxAction = this.form.ajaxAction.value;
					this.ajaxWishAction = this.form.ajaxWishListAction.value;
					if(that.type != "quickView")
						this.viewer = new easyViewer('ShopBag');
					addToCartButton.onclick = this.buttonHandler;
					if (addToWishListButton != null) {						
						addToWishListButton.onclick = this.wishButtonHandler;
					}
				}
			},
			
			ajaxSuccess: function(transport) {
				var response = transport.responseText;
				try{
					if(response.indexOf('pre-order=true') != -1){
						picker.addToBagDetails.preOrder = true;
					} else {
						picker.addToBagDetails.preOrder = false;
					}
					if(response.indexOf('outOfStock=true') != -1){
						picker.addToBagDetails.error = true;
					} else {
						picker.addToBagDetails.error = false;
					}
				}catch(err){}
				var splitted = response.split('$$$SPLITHERE$$$');
				$('shoppingBagMain').innerHTML=splitted[0];//.update(splitted[0]);
				if (minicartConstants.easyViewerAddToBag == true) {
					$('ShopBagContent').innerHTML=splitted[1];//.update(splitted[1]);
					if ($('shopBagAction')) {
						$('shopBagAction').show();
					}
					miniCart.viewer.show();
				} else {
					$('inTheCartText').innerHTML="THIS ITEM HAS BEEN ADDED TO YOUR "+miniCart.clicked;
					if ($('inTheCart')) {
						$('inTheCart').style.display = 'block';
					}
					var data = 'picker.LAST_ADDED_ITEM = function(color,width,size){if(color == "'+picker.addToBagDetails.color+'" && size == "'+picker.addToBagDetails.size+'" && width == "'+picker.addToBagDetails.width+'")return true; return false; }';
					eval('(' + data + ')' );
				}
				//alert('running mine');
				document.body.style.cursor='default';
                                // site scroll to top
                                window.scrollTo(0,0);
				try{
					omnAddtoBagReport(picker.addToBagDetails);
					siteClarityAddtoBag(picker.addToBagDetails);
				}catch(err){}
			},
			
			addToCart: function() {
				try{
					that.addToBagDetails.color = that.selectionStates.selectedColor;
					if(that.hasSize)
						that.addToBagDetails.size = that.selectionStates.selectedSize;
					if(that.hasWidth)
						that.addToBagDetails.width = that.selectionStates.selectedWidth;
					that.addToBagDetails.upc = this.form.pid.value;
					that.addToBagDetails.price = that.selectionStates.currentSKU.salesPrice.replace('$','');
				}catch(err){}
				if (this.form.pid.value != '' && this.form.availability.value != '' &&  this.form.availability.value != 'NOT_AVAILABLE') {
					document.body.style.cursor='wait';
					this.clicked = 'BAG';
					new Ajax.Request(this.ajaxAction, {method:'post', postBody: Form.serialize(this.form), onSuccess: this.ajaxSuccess, onFailure: function() {alert('failure');document.body.style.cursor='default';}});
				} else {
					picker.selectionStates.setMessage('force');		
				}
			},
			addToWishList: function() {
				if (this.form.pid.value != '' && this.form.availability.value != '' &&  this.form.availability.value != 'NOT_AVAILABLE') {
					document.body.style.cursor='wait';
					this.clicked = 'WISHLIST';
					new Ajax.Request(this.ajaxWishAction, {method:'post', postBody: Form.serialize(this.form), onSuccess: this.ajaxSuccess, onFailure: function() {alert('failure');document.body.style.cursor='default';}});
				} else {
					picker.selectionStates.setMessage('force');		
				}
			},
			createCart : function() {
				document.createElement('div');
				dive.innerHTML = "me";
			},
			
			buttonHandler: function() {
				miniCart.addToCart();
				return false;
			},
			wishButtonHandler: function() {				
				miniCart.addToWishList();
				return false;
			}
		}

	}
}

function skuPickingLogic(parent) 
{
	var that = this;
	this.skuPicker = parent;
	this.uniqueSizeClasses = new Array();
	this.uniqueColors = new Array();
	this.uniqueWidths = new Array();
	this.uniqueSizes = new Array();
	this.selectedColor = -1;this.selectedWidth = -1;this.selectedSize = -1;this.selectedSizeClass = -1;
	this.prevSelectedColor = -1;this.prevSelectedWidth = -1;this.prevSelectedSize = -1;

	this.rollOut = true;
	this.rollOverColor = -1;
	this.rollOverWidth = -1;
	this.rollOverSize = -1;
	this.currentSKU = null;

	this.init = function ()
	{
		var sizeClasses = $$('.'+this.skuPicker.idPrefix+'CARTSizeClass');
		var colors = $$('.'+this.skuPicker.idPrefix+'CARTchip');
		var widths = $$('.'+this.skuPicker.idPrefix+'CARTwidth');
		var sizes = $$('.'+this.skuPicker.idPrefix+'CARTsize');
//---------------------
		var i=0;
		for(i=0; i<sizeClasses.length; i++)
		{
			if (sizeClasses[i] != null) { // safari fix
				sizeClasses[i].chipType = 'sizeClassChip';
				Event.observe(sizeClasses[i], 'click', that.updateFields);
				sizeClasses[i].className = 'availableSizeClass';
			}
		}
		that.uniqueSizeClasses = sizeClasses;
//---------------------
		var i=0;
		for(i=0; i<colors.length; i++)
		{
			if (colors[i] != null) { // safari fix
				Event.observe(colors[i].id, 'mouseover', that.swatchRollOver);
				Event.observe(colors[i].id, 'mouseout', that.swatchRollOut);
				colors[i].chipType = 'colorChip';
				Event.observe(colors[i], 'click', that.updateFields);
				colors[i].className = 'available';
			}
		}
		that.uniqueColors = colors;
		if(that.skuPicker.hasWidth)
		{
			for(i=0; i<widths.length; i++)
			{
				if (widths[i] != null) { // safari fix
					Event.observe(widths[i].id, 'mouseover', that.swatchRollOver);
					Event.observe(widths[i].id, 'mouseout', that.swatchRollOut);
					widths[i].chipType = 'widthChip';
					Event.observe(widths[i].id, 'click', that.updateFields);
					widths[i].className = 'available';
				}
			}
			that.uniqueWidths = widths;
		} else {
			if ($(that.skuPicker.idPrefix+'widths')) {
				var singleWidth = $(that.skuPicker.idPrefix+'widths').getElementsByTagName('span');
				this.selectedWidth = singleWidth[0].id.substring(that.skuPicker.idPrefix.length);
				singleWidth[0].className = "selectedButton";
				singleWidth[0].chipType = 'widthChip';
				that.uniqueWidths = singleWidth;
			}
		}
		if(that.skuPicker.hasSize)
		{
			for(i=0; i<sizes.length; i++)
			{
				if (sizes[i] != null) { // safari fix
					Event.observe(sizes[i].id, 'mouseover', that.swatchRollOver);
					Event.observe(sizes[i].id, 'mouseout', that.swatchRollOut);
					sizes[i].chipType = 'sizeChip';
					Event.observe(sizes[i].id, 'click', that.updateFields);
					sizes[i].className = 'available';
				}
			}
			that.uniqueSizes = sizes;
		} else {

			if ($(that.skuPicker.idPrefix+'sizes')) {
				var singleSize = $(that.skuPicker.idPrefix+'sizes').getElementsByTagName('span');
				this.selectedSize = singleSize[0].id.substring(that.skuPicker.idPrefix.length);
				singleSize[0].className = "selectedButton";
				singleSize[0].chipType = 'sizeChip';
				that.uniqueSizes = singleSize;
			}
		}
	}
	this.updateFieldsForElement = function(e) {
			var greyClicked = false;
			
			if(e.className == 'notAvailable') {
				greyClicked = true;
				that.clearAndRememberSelection();
				that.populateAll();
			}

       		if(e.chipType == 'sizeClassChip') {
				that.skuPicker.skusArray = that.skuPicker.multiSkusArray[e.id.substring(that.skuPicker.idPrefix.length)];
				that.selectedSizeClass = e.id.substring(that.skuPicker.idPrefix.length);
				that.clearSelection();
				that.changeSizeClass();
				that.populateAll();
				that.selectedColor = that.skuPicker.multiSkusArray[that.selectedSizeClass+'Colors'][0];
				e.className = 'selectedSizeClass';
				that.availForColor();
			}

       		if(e.chipType == 'colorChip') {
  				if(that.selectedColor != -1)
  					$(that.skuPicker.idPrefix+that.selectedColor).className = 'available';
				that.selectedColor = e.id.substring(that.skuPicker.idPrefix.length);
				that.availForColor();
			}

       		if(e.chipType == 'widthChip') {
  				if(that.selectedWidth != -1)
  					$(that.skuPicker.idPrefix+that.selectedWidth).className = 'available';
				that.selectedWidth = e.id.substring(that.skuPicker.idPrefix.length);
		 		that.selectedWidthName = e.title;
				that.availForWidth();
			}
       		if(e.chipType == 'sizeChip') {
  				if(that.selectedSize != -1)
  					$(that.skuPicker.idPrefix+that.selectedSize).className = 'available';
		 		that.selectedSize = e.id.substring(that.skuPicker.idPrefix.length);
		 		that.selectedSizeName = e.title;
				that.availForSize();
			}
			if(greyClicked)
				that.selectRemembered(e);
			that.selectIfSingle();
			that.setMessage();
			that.highLightSelection();
	}
	this.updateFields = function (e) {
		that.rollOut = false;
		that.updateFieldsForElement(Event.element(e));
		if(that.selectedColor == -1){
			that.selectFirstAvailColor();
		}
   	}
	this.selectFirstAvailColor = function() {
		var i=1;
		for(i=1;i<=that.skuPicker.multiSkusArray[that.selectedSizeClass+'Colors'].length; i++) {
			if($(that.skuPicker.idPrefix+that.skuPicker.multiSkusArray[that.selectedSizeClass+'Colors'][i-1]).className == 'available'){
				that.updateFieldsForElement($(that.skuPicker.idPrefix+that.skuPicker.multiSkusArray[that.selectedSizeClass+'Colors'][i-1]));
				break;
			}
		}
	}
	this.selectIfSingle = function() {
			var availPointer = null;
			var avail = 0;
			var i;
			for(i=0; i<that.uniqueColors.length; i++) {
				if (that.uniqueColors[i] != null) { // safari fix
					if(that.uniqueColors[i].className == 'available') {
						availPointer = that.uniqueColors[i];
						avail++;
					}
				}
			}
			if(avail == 1 && that.selectedColor == -1) {
				that.selectedColor = availPointer.id.substring(that.skuPicker.idPrefix.length);
				that.availForColor();
			}
//------------------------------------------
			availPointer = null;
			avail = 0;
			for(i=0; i<that.uniqueWidths.length; i++) {
				if (that.uniqueWidths[i] != null) { // safari fix
					if(that.uniqueWidths[i].className == 'available') {
						availPointer = that.uniqueWidths[i];
						avail++;
					}
				}
			}
			if(avail == 1 && that.selectedWidth == -1) {
				that.selectedWidth = availPointer.id.substring(that.skuPicker.idPrefix.length);
				that.availForWidth();
			}
//------------------------------------------
			availPointer = null;
			avail = 0;
			for(i=0; i<that.uniqueSizes.length; i++) {
				if (that.uniqueSizes[i] != null) { // safari fix
					if(that.uniqueSizes[i].className == 'available') {
						availPointer = that.uniqueSizes[i];
						avail++;
					}
				}
			}
			if(avail == 1 && that.selectedSize == -1) {
		 		that.selectedSize = availPointer.id.substring(that.skuPicker.idPrefix.length);
				that.availForSize();
			}
	}
	this.availForColor = function ()
	{
				var state = 100;
				if(that.selectedWidth != -1)
					state += 10;
				if(that.selectedSize != -1)
					state += 1;
				switch (state)
				{
					case 100:
						that.WSColor();
						break;
					case 101:
						that.WSColor();
						that.WColorSize();
						break;
					case 110:
						that.WSColor();
						that.SColorWidth();
						break;
					case 111:
						that.WColorSize();
						that.SColorWidth();
						break;
				}	
	}

	this.availForWidth = function ()
	{
				var state = 10;
				if(that.selectedColor != -1)
					state += 100;
				if(that.selectedSize != -1)
					state += 1;
				switch (state)
				{
					case 10:
						that.CSWidth();
						break;
					case 11:
						that.CSWidth();
						that.CWidthSize();
						break;
					case 110:
						that.CSWidth();
						that.SColorWidth();
						break;
					case 111:
						that.CWidthSize();
						that.SColorWidth();
						break;
				}		
	}
	this.availForSize = function ()
	{
				var state = 1;
				if(that.selectedColor != -1)
					state += 100;
				if(that.selectedWidth != -1)
					state += 10;
				switch (state)
				{
					case 1:
						that.CWSize();
						break;
					case 11:
						that.CWSize();
						that.CWidthSize();
						break;
					case 101:
						that.CWSize();
						that.WColorSize();
						break;
					case 111:
						that.CWidthSize();
						that.WColorSize();
						break;
				}		
	}
	this.WSColor = function ()
	{
			var i=0;
			for(i=0; i<that.uniqueWidths.length; i++) {
				if (that.uniqueWidths[i] != null) { // safari fix
					that.uniqueWidths[i].className = 'notAvailable';
				}
			}
			for(i=0; i<that.uniqueSizes.length; i++) {
				if (that.uniqueSizes[i] != null) { // safari fix
					that.uniqueSizes[i].className = 'notAvailable';
				}
			}

			for (var sku in that.skuPicker.skusArray) {
				if(that.skuPicker.skusArray[sku].color_code == that.selectedColor) {
					if (that.skuPicker.skusArray[sku].width) {
						$(that.skuPicker.idPrefix+that.skuPicker.skusArray[sku].width).className = 'available';
					}
					if (that.skuPicker.skusArray[sku].size) {
						$(that.skuPicker.idPrefix+that.skuPicker.skusArray[sku].size).className = 'available';
					}
					that.currentSKU = that.skuPicker.skusArray[sku]; 
				}
			}
	}
	this.CSWidth = function ()
	{
			var i=0;

			for(i=0; i<that.uniqueColors.length; i++) {
				if (that.uniqueColors[i] != null) { // safari fix
					that.uniqueColors[i].className = 'notAvailable';
				}
			}
			for(i=0; i<that.uniqueSizes.length; i++) {
				if (that.uniqueSizes[i] != null) { // safari fix
					that.uniqueSizes[i].className = 'notAvailable';
				}
			}

			for (var sku in that.skuPicker.skusArray) {
				if(that.skuPicker.skusArray[sku].width == that.selectedWidth){
					var tmp = that.skuPicker.skusArray[sku].color_code;
					if($(that.skuPicker.idPrefix+tmp))
						$(that.skuPicker.idPrefix+tmp).className = 'available';
					$(that.skuPicker.idPrefix+that.skuPicker.skusArray[sku].size).className = 'available';
					that.currentSKU = that.skuPicker.skusArray[sku];
				}
			}
	}
	this.CWSize = function ()
	{
			var i=0;

			for(i=0; i<that.uniqueColors.length; i++)
				that.uniqueColors[i].className = 'notAvailable';
			for(i=0; i<that.uniqueWidths.length; i++)
				that.uniqueWidths[i].className = 'notAvailable';
			
			for (var sku in that.skuPicker.skusArray) {
				if(that.skuPicker.skusArray[sku].size == that.selectedSize) {
					var tmp = that.skuPicker.skusArray[sku].color_code;
					if($(that.skuPicker.idPrefix+tmp))
						$(that.skuPicker.idPrefix+tmp).className = 'available';

					$(that.skuPicker.idPrefix+that.skuPicker.skusArray[sku].width).className = 'available';
					that.currentSKU = that.skuPicker.skusArray[sku];					
				}
			}
	}
	this.SColorWidth = function ()
	{
			var i=0;
			for(i=0; i<that.uniqueSizes.length; i++) {
				if (that.uniqueSizes[i] != null) { // safari fix
					that.uniqueSizes[i].className = 'notAvailable';
				}
			}
			
			for (var sku in that.skuPicker.skusArray) {
				if(that.skuPicker.skusArray[sku].color_code == that.selectedColor && that.skuPicker.skusArray[sku].width == that.selectedWidth) {
					$(that.skuPicker.idPrefix+that.skuPicker.skusArray[sku].size).className = 'available';
					that.currentSKU = that.skuPicker.skusArray[sku];
				}
			}
	}
	this.WColorSize = function ()
	{
			var i=0;
			for(i=0; i<that.uniqueWidths.length; i++)
				that.uniqueWidths[i].className = 'notAvailable';						
			for (var sku in that.skuPicker.skusArray) {
				if(that.skuPicker.skusArray[sku].color_code == that.selectedColor && that.skuPicker.skusArray[sku].size == that.selectedSize){
					$(that.skuPicker.idPrefix+that.skuPicker.skusArray[sku].width).className = 'available';
					that.currentSKU = that.skuPicker.skusArray[sku];
				}
			}
	}
	this.CWidthSize = function ()
	{
			var i=0;
			for(i=0; i<that.uniqueColors.length; i++)
				that.uniqueColors[i].className = 'notAvailable';
			for (var sku in that.skuPicker.skusArray) {
				if(that.skuPicker.skusArray[sku].width == that.selectedWidth && that.skuPicker.skusArray[sku].size == that.selectedSize) {
					var tmp = that.skuPicker.skusArray[sku].color_code;
					if($(that.skuPicker.idPrefix+tmp))
						$(that.skuPicker.idPrefix+tmp).className = 'available';
					that.currentSKU = that.skuPicker.skusArray[sku];
				}
			}
	}
	this.swatchRollOver = function(e){
		that.rollOut = true;
   		if(Event.element(e).chipType == 'colorChip'){
			if(Event.element(e).className != "selectedColorButton"){
				if(Event.element(e).className != "notAvailable"){
					that.rollOverColor = that.selectedColor;
					that.rollOverWidth = that.selectedWidth;
					that.rollOverSize = that.selectedSize;
					that.updateFieldsForElement(Event.element(e));
				} else {
					that.rollOut = false;
					var size = true;
					var width = true;
		
					var errMsg = Event.element(e).alt+' is not available in';
					if(that.selectedSize != -1 && that.skuPicker.hasSize) {
						var currSize = $(that.skuPicker.idPrefix+that.selectedSize).title;
						if(currSize == '')
							currSize = $(that.skuPicker.idPrefix+that.selectedSize).id;
						errMsg += ' '+currSize;
					} else size = false;
					if(that.selectedWidth != -1 && that.skuPicker.hasWidth) {
						var currWidth = $(that.skuPicker.idPrefix+that.selectedWidth).title;
						if(currWidth == '')
							currWidth = $(that.skuPicker.idPrefix+that.selectedWidth).id;
						errMsg += ' '+currWidth;
					} else width = false;

					if(width == false && size == false)
						errMsg = 'Click '+ Event.element(e).alt + ' chip for available sizes and widths.'
					$(that.skuPicker.unavailableMsgId).innerHTML = errMsg;
					$(that.skuPicker.unavailableMsgId).style.display = 'block';
				}				
			} else {
				that.rollOut = false;
			}
		}
		if(Event.element(e).chipType == 'widthChip') {
			if(Event.element(e).className != "selectedButton") {
				if(Event.element(e).className != "notAvailable") {
					that.rollOverColor = that.selectedColor;
					that.rollOverWidth = that.selectedWidth;
					that.rollOverSize = that.selectedSize;
					that.updateFieldsForElement(Event.element(e));
				} else {
					that.rollOut = false;
					var errMsg = '';
					if(that.selectedColor != -1) 
						errMsg = $(that.skuPicker.idPrefix+that.selectedColor).alt+' is not available in';
					else
						errMsg = 'Select a color for ';
					if(that.selectedSize != -1 && that.skuPicker.hasSize) {
						var currSize = $(that.skuPicker.idPrefix+that.selectedSize).title;
						if(currSize == '')
							currSize = $(that.skuPicker.idPrefix+that.selectedSize).id;
						errMsg += ' '+currSize;
					}
					var widthInfo = Event.element(e).title;
					if(widthInfo == '')
						widthInfo = Event.element(e).id.substring(that.skuPicker.idPrefix.length);
						
					errMsg += ' '+widthInfo;
					$(that.skuPicker.unavailableMsgId).innerHTML = errMsg;
					$(that.skuPicker.unavailableMsgId).style.display = 'block';
				}				
			} else {
				that.rollOut = false;
			}
		}
		if(Event.element(e).chipType == 'sizeChip') {
			if(Event.element(e).className != "selectedButton") {
				if(Event.element(e).className != "notAvailable") {
					that.rollOverColor = that.selectedColor;
					that.rollOverWidth = that.selectedWidth;
					that.rollOverSize = that.selectedSize;
					that.updateFieldsForElement(Event.element(e));
				} else {
					that.rollOut = false;
					var errMsg = '';
					if(that.selectedColor != -1) 
						errMsg = $(that.skuPicker.idPrefix+that.selectedColor).alt+' is not available in';
					else
						errMsg = 'Select a color for ';
					var sizeInfo = Event.element(e).title;
					if(sizeInfo == '')
						sizeInfo = Event.element(e).id.substring(that.skuPicker.idPrefix.length);
					errMsg += ' '+sizeInfo;
					if(that.selectedWidth != -1 && that.skuPicker.hasWidth)
						errMsg += ' '+that.selectedWidth;

					$(that.skuPicker.unavailableMsgId).innerHTML = errMsg;
					$(that.skuPicker.unavailableMsgId).style.display = 'block';
				}				
			} else {
				that.rollOut = false;
			}		
		}
	}
	this.swatchRollOut = function(e){
		$(that.skuPicker.unavailableMsgId).style.display = 'none';
		if(that.rollOut) {
			that.selectedWidth = that.rollOverWidth;
			that.selectedSize = that.rollOverSize;
			that.updateFieldsForElement($(that.skuPicker.idPrefix+that.rollOverColor));
			if(that.selectedWidth != -1)
				that.updateFieldsForElement($(that.skuPicker.idPrefix+that.selectedWidth));
			if(that.selectedSize != -1)
				that.updateFieldsForElement($(that.skuPicker.idPrefix+that.selectedSize));
		}
	}
	this.highLightSelection = function()
	{
			var tmp = that.selectedColor;
			if($(that.skuPicker.idPrefix+tmp))
				$(that.skuPicker.idPrefix+tmp).className = 'selectedColorButton';
			if($(that.skuPicker.idPrefix+that.selectedWidth))
				$(that.skuPicker.idPrefix+that.selectedWidth).className = 'selectedButton';
			if($(that.skuPicker.idPrefix+that.selectedSize))
				$(that.skuPicker.idPrefix+that.selectedSize).className = 'selectedButton';
			
			if(that.selectedColor && that.selectedColor != -1) {
				$(that.skuPicker.idPrefix+'currentColor').innerHTML = $(that.skuPicker.idPrefix+that.selectedColor).title;
				$(that.skuPicker.idPrefix+'errColor').style.display = 'none';

				var pdImage = that.skuPicker.imagesArray[that.selectedColor].PD[that.skuPicker.selectedImageType];
				var peImage = that.skuPicker.imagesArray[that.selectedColor].PE[that.skuPicker.selectedImageType];
				if(pdImage == null) {
					pdImage = $('emptyPDImage').value;
				}
				if(peImage == null) {
					peImage = $('emptyPEImage').value;
				}
				
				$(that.skuPicker.idPrefix+'mainDisplay').src = pdImage;
				if ($(that.skuPicker.idPrefix+'printDisplay') != null) {
					$(that.skuPicker.idPrefix+'printDisplay').src = pdImage;
				}
				if ($(that.skuPicker.idPrefix+'enlargeImage') != null) {
					$(that.skuPicker.idPrefix+'enlargeImage').src = peImage;
				}
				if(that.skuPicker.detailViewer)
					that.skuPicker.detailViewer.menuSetup();
				
				var colorChip = that.currentSKU;//$(that.skuPicker.idPrefix + tmp);
				if (colorChip) {
					var promoDivContainer = $('promotionsSpan');
					if(promoDivContainer){
						promoDivContainer.innerHTML = "";//'<div style="float:left;">'+colorChip.promotions.length+' '+that.currentSKU.color_code+' '+that.currentSKU.width+' '+that.currentSKU.size+'</div><br />';
						if(typeof(colorChip.promotions)!="undefined" && colorChip.promotions.length != 0){
							var iPromo=0;
							var promoMarkUp='';
							for(iPromo=0; iPromo<colorChip.promotions.length; iPromo++){
								promoMarkUp += '<div class="productPromotion">'+colorChip.promotions[iPromo]+'</div>';
							}
							promoDivContainer.innerHTML += promoMarkUp;
						}
					}
					that.showSalePrice_(colorChip);
					$(that.skuPicker.idPrefix+'currentColor').innerHTML += ' - '+colorChip.salesPrice;
					// enlarged view
					if ($('enlargeName')) {
						var enlargeNotSale = $$('#enlargeName .notSale')[0];
						var enlargeSale = $('enlargeNameValue');
						var enlargeSaleWas = $('enlargeWas');
						var enlargeSaleNow = $('enlargeNow');
						var enlargeSaleNowValue = $('enlargeNowValue');
						if (colorChip.showStandardPrice) {
							enlargeNotSale.update(colorChip.standardPrice);
							enlargeSale.update('');
							enlargeSaleNow.update('');
							enlargeSaleNowValue.update('');
						} else {
							enlargeNotSale.update('');
							if(enlargeSaleWas != null) enlargeSaleWas.update($(that.skuPicker.idPrefix+'skuPickerWasText').value);
							enlargeSale.update(colorChip.standardPrice);
							enlargeSaleNow.update($(that.skuPicker.idPrefix+'skuPickerNowText').value);
							enlargeSaleNowValue.update(colorChip.salesPrice);
						}
					}
					
					// print view
					if ($('printName')) {
						var printNotSale = $$('#printName .notSale')[0];
						var printSale = $('printNameValue');
						var printSaleWas = $('printWas');
						var printSaleNow = $('printNow');
						var printSaleNowValue = $('printNowValue');
						if (colorChip.showStandardPrice) {
							printNotSale.update(colorChip.standardPrice);
							printSale.update('');
							printSaleNow.update('');
							printSaleNowValue.update('');

						} else {
							printNotSale.update('');
							if(printSaleWas != null) printSaleWas.update($(that.skuPicker.idPrefix+'skuPickerWasText').value);
							printSale.update(colorChip.standardPrice);
							printSaleNow.update($(that.skuPicker.idPrefix+'skuPickerNowText').value);
							printSaleNowValue.update(colorChip.salesPrice);
						}
					}
				}
			} else { 
				try{
					$(that.skuPicker.idPrefix+'currentColor').innerHTML = '';
					if($('minPrice') && that.skuPicker.type == "productDetail")
						$('minPrice').innerHTML = '&nbsp;';
					var tempColor = null;
					var imageNameUrlParts = $('mainDisplay').src.split("/");
					var imageName = imageNameUrlParts[imageNameUrlParts.length-1]
					if(imageName.indexOf("PG") != -1) {
						var imageNameParts = imageName.split(".");
						tempColor = imageNameParts[imageNameParts.length-3];
					}

					if($(that.skuPicker.idPrefix+tempColor)) {
						var pdImage = that.skuPicker.imagesArray[tempColor].PD[that.skuPicker.selectedImageType];
						var peImage = that.skuPicker.imagesArray[tempColor].PE[that.skuPicker.selectedImageType];
						if(pdImage == null) {
							pdImage = $('emptyPDImage').value;
						}
						if(peImage == null) {
							peImage = $('emptyPEImage').value;
						}
						
						$(that.skuPicker.idPrefix+'mainDisplay').src = pdImage;
						if ($(that.skuPicker.idPrefix+'printDisplay') != null) {
							$(that.skuPicker.idPrefix+'printDisplay').src = pdImage;
						}
						if ($(that.skuPicker.idPrefix+'enlargeImage') != null) {
							$(that.skuPicker.idPrefix+'enlargeImage').src = peImage;
						}
						if(that.skuPicker.detailViewer)
							that.skuPicker.detailViewer.menuSetup();
					}
				}catch(e){}
			}
			if(that.selectedWidth != -1){
				var currWidth = $(that.skuPicker.idPrefix+that.selectedWidth).title;
				if(currWidth == '')
					currWidth = $(that.skuPicker.idPrefix+that.selectedWidth).id;
				$(that.skuPicker.idPrefix+'currentWidth').innerHTML = currWidth;
				$(that.skuPicker.idPrefix+'errWidth').style.display = 'none';
			} else if ($(that.skuPicker.idPrefix+'currentWidth')) {
				$(that.skuPicker.idPrefix+'currentWidth').innerHTML = '';
			}
			if(that.selectedSize != -1){
				var currSize = $(that.skuPicker.idPrefix+that.selectedSize).title;
				if(currSize == '')
					currSize = $(that.skuPicker.idPrefix+that.selectedSize).id;
				$(that.skuPicker.idPrefix+'currentSize').innerHTML = currSize;
				$(that.skuPicker.idPrefix+'errSize').style.display = 'none';
			} else if ($(that.skuPicker.idPrefix+'currentSize')) {
				$(that.skuPicker.idPrefix+'currentSize').innerHTML = '';
			}
	}
	this.showSalePrice_ = function(colorChip)
	{
		// main view
		if ($(that.skuPicker.idPrefix+'displayNameAndPrice')) {
			var notSale = $$('#'+that.skuPicker.idPrefix+'displayNameAndPrice .notSale')[0];

			var onSale = $$('#'+that.skuPicker.idPrefix+'displayNameAndPrice .onSale')[0];
			var salePrice = $$('#'+that.skuPicker.idPrefix+'displayNameAndPrice .salePrice')[0];
			if (colorChip.showStandardPrice) {
				notSale.update(colorChip.standardPrice);
				onSale.update('');
				salePrice.update('&nbsp;');
			} else {
				notSale.update('');
				onSale.update($(that.skuPicker.idPrefix+'skuPickerWasText').value + ' ' + colorChip.standardPrice);
				salePrice.update($(that.skuPicker.idPrefix+'skuPickerNowText').value + ' ' + colorChip.salesPrice);
			}
		}
					
		if ($(that.skuPicker.idPrefix+'displayNameAndPrice2')) {
			var notSale2 = $$('#'+that.skuPicker.idPrefix+'displayNameAndPrice2 .notSale')[0];
			var onSale2 = $$('#'+that.skuPicker.idPrefix+'displayNameAndPrice2 .onSale')[0];
			var salePrice2 = $$('#'+that.skuPicker.idPrefix+'displayNameAndPrice2 .salePrice')[0];
			if (colorChip.showStandardPrice) {
				notSale2.update(colorChip.standardPrice);
				onSale2.update('');
				salePrice2.update('&nbsp;');
			} else {
				notSale2.update('');
				onSale2.update($(that.skuPicker.idPrefix+'skuPickerWasText').value + ' ' + colorChip.standardPrice);
				salePrice2.update($(that.skuPicker.idPrefix+'skuPickerNowText').value + ' ' + colorChip.salesPrice);
			}
		}
	}
	this.clearSelection = function()
	{
			that.selectedColor = -1;
			if(that.skuPicker.hasWidth)
				that.selectedWidth = -1;
			if(that.skuPicker.hasSize)
				that.selectedSize = -1;
	}
	this.clearAndRememberSelection = function()
	{
			that.prevSelectedColor = that.selectedColor;		
			that.prevSelectedWidth = that.selectedWidth;
			that.prevSelectedSize = that.selectedSize;

			that.selectedColor = -1;
			if(that.skuPicker.hasWidth)
				that.selectedWidth = -1;
			if(that.skuPicker.hasSize)
				that.selectedSize = -1;
	}
	this.selectRemembered = function(e)
	{
		if(e.chipType == 'colorChip')
		{
			if(that.prevSelectedWidth && that.prevSelectedWidth != -1)
			{
				if($(that.skuPicker.idPrefix+that.prevSelectedWidth).className == 'available')
				{
					that.selectedWidth = that.prevSelectedWidth;
					that.availForWidth();
				}
			}
			if(that.prevSelectedSize && that.prevSelectedSize != -1)
			{
				if($(that.skuPicker.idPrefix+that.prevSelectedSize).className == 'available')
				{
					that.selectedSize = that.prevSelectedSize;
					that.availForSize();
				}
			}
		}
		if(e.chipType == 'widthChip')
		{
			if(that.prevSelectedColor && that.prevSelectedColor != -1)
			{
				if($(that.skuPicker.idPrefix+that.prevSelectedColor).className == 'available')
				{
					that.selectedColor = that.prevSelectedColor;
					that.availForColor();
				}
			}
			if(that.prevSelectedSize && that.prevSelectedSize != -1)
			{
				if($(that.skuPicker.idPrefix+that.prevSelectedSize).className == 'available')
				{
					that.selectedSize = that.prevSelectedSize;
					that.availForSize();
				}				
			}
		}
		if(e.chipType == 'sizeChip')
		{
			if(that.prevSelectedColor && that.prevSelectedColor != -1)
			{
				if($(that.skuPicker.idPrefix+that.prevSelectedColor).className == 'available')
				{
					that.selectedColor = that.prevSelectedColor;
					that.availForColor();
				}				
			}
			if(that.prevSelectedWidth && that.prevSelectedWidth != -1)
			{
				if($(that.skuPicker.idPrefix+that.prevSelectedWidth).className == 'available')
				{
					that.selectedWidth = that.prevSelectedWidth;
					that.availForWidth();
				}
			}
		}
	}
	this.setMessage = function (force)
	{
			if(that.selectedColor != -1 && (!that.skuPicker.hasWidth || that.selectedWidth != -1) && (!that.skuPicker.hasSize || that.selectedSize != -1))
			{
				//if(that.skuPicker.type == 'productDetail' || that.skuPicker.type == 'quickView') 
				//{
					if(that.skuPicker.LAST_ADDED_ITEM && that.skuPicker.LAST_ADDED_ITEM(that.selectedColor, that.selectedWidth, that.selectedSize))
					{
						$(that.skuPicker.idPrefix+'inTheCartText').innerHTML = 'THIS ITEM HAS BEEN ADDED TO YOUR '+that.skuPicker.BAG;
		 				$(that.skuPicker.idPrefix+'inTheCart').style.display = 'block';
					} else {
						$(that.skuPicker.idPrefix+'inTheCart').style.display = 'none';
					}
				//} else {
					//$(that.skuPicker.idPrefix+'inTheCart').style.display = 'none';
				//}
				var currColor = that.selectedColor;
				if($(that.skuPicker.idPrefix+that.selectedColor))
					currColor = $(that.skuPicker.idPrefix+that.selectedColor).alt;

				if ($(that.skuPicker.idPrefix+that.selectedWidth)) {
					var currWidth = $(that.skuPicker.idPrefix+that.selectedWidth).title;
					if(currWidth == '') {
						currWidth = $(that.skuPicker.idPrefix+that.selectedWidth).id;
					}
				}
				
				if ($(that.skuPicker.idPrefix+that.selectedSize)) {	
					var currSize = $(that.skuPicker.idPrefix+that.selectedSize).title;
					if(currSize == '') {
						currSize = $(that.skuPicker.idPrefix+that.selectedSize).id;
					}
				}
					
				that.loadValidUPC();

				var msg = '<div id="'+that.skuPicker.idPrefix+'errTitle" class="errTitle">You have selected:</div><div id="theError" style="color:'+skuPickerConstants.locErrTextColor+';">'+currColor;
				if(that.skuPicker.hasWidth)
				 	msg+=', '+ currWidth;
				if(that.skuPicker.hasSize) 
					msg+=', '+ currSize;
				msg+='.</div>';
				$(that.skuPicker.idPrefix+'locErrText').innerHTML = msg;
				//$(that.skuPicker.idPrefix+'locErrText').style.color = skuPickerConstants.locErrTextColor;
				$(that.skuPicker.idPrefix+'locErrText').style.backgroundColor = '';
				// message for product inventory NOT AVAILABLE 
				if(force) 
				{
					$(that.skuPicker.idPrefix+'locErrText').style.color = skuPickerConstants.forceColor;
					$(that.skuPicker.idPrefix+'locErrText').style.backgroundColor = skuPickerConstants.forceBackgroundColor;
					$(that.skuPicker.idPrefix+'locErrText').innerHTML = 'This product is NOT AVAILABLE, but you can add to wishlist.';
				}				
				
			} else {
				$(that.skuPicker.idPrefix+'pid').value = '';
				var msg = [];

				if(that.selectedColor == -1)
				{
					if(force)
						$(that.skuPicker.idPrefix+'errColor').style.display = 'block';
					msg.push('color/material');
				}
				if(that.selectedWidth == -1)
				{
					if(force && $(that.skuPicker.idPrefix+'errWidth')) {
						$(that.skuPicker.idPrefix+'errWidth').style.display = 'block';
					}
					if(that.skuPicker.hasWidth)
						msg.push('width');
					
				}
				if(that.selectedSize == -1)
				{
					if(force && $(that.skuPicker.idPrefix+'errSize')) {
						$(that.skuPicker.idPrefix+'errSize').style.display = 'block';
					}
					if(that.skuPicker.hasSize)
						msg.push('size');
				}
				var acc2;
				if (msg.length > 1) 
				{
	 				var last = ' and ' + msg[ msg.length-1 ];
	  				var rest = msg.slice(0, msg.length-1);
	  				acc2 = rest.join(', ').concat(last);
				} else {
	  				acc2 = msg;
				}
				$(that.skuPicker.idPrefix+'locErrText').style.color = '';
				$(that.skuPicker.idPrefix+'locErrText').style.backgroundColor = '';
				$(that.skuPicker.idPrefix+'locErrText').innerHTML = '<div id="'+that.skuPicker.idPrefix+'errTitle" class="errTitle">Please select a ' + acc2 + '.</div>';
				/* var message = "";
				if(that.selectedWidth!=-1) {
					message = "You have selected width " + that.selectedWidthName + ', please select a ' + acc2 + '.';
				} else if(that.selectedSize != -1) {
					message = "You have selected size " + that.selectedSizeName + ', please select a ' + acc2 + '.';
				} else {
					message = 'Please select a ' + acc2 + '.';
				}
				$(that.skuPicker.idPrefix+'locErrText').innerHTML = message;
				*/

				if(force)
				{
					$(that.skuPicker.idPrefix+'errTitle').style.color = skuPickerConstants.forceColor;
					$(that.skuPicker.idPrefix+'errTitle').style.backgroundColor = skuPickerConstants.forceBackgroundColor;
				}
			}
	}
	
	// function for product inventory IN STOCK or NOT AVAILABLE 
	this.addTo = function () {
	    for (var sku in that.skuPicker.skusArray) {
	    	if($(that.skuPicker.idPrefix+'pid').value == sku){
	    		var msgAvailability = that.skuPicker.skusArray[sku].availability;
				$(that.skuPicker.idPrefix+'availability').value = msgAvailability;
			}
		}
	}


	this.loadValidUPC = function (){
			var valid = false;
			for (var sku in that.skuPicker.skusArray){
				if(that.skuPicker.skusArray[sku].color_code == that.selectedColor && (!that.skuPicker.hasWidth || that.skuPicker.skusArray[sku].width == that.selectedWidth) && (!that.skuPicker.hasSize || that.skuPicker.skusArray[sku].size == that.selectedSize)){
					valid = true;
					$(that.skuPicker.idPrefix+'pid').value = sku;
					//if($(that.skuPicker.idPrefix+'prodSelected'))
						//$(that.skuPicker.idPrefix+'prodSelected').checked = true;
					$(that.skuPicker.idPrefix+'availability').value = that.skuPicker.skusArray[sku].availability;
					//that.addTo();
					that.currentSKU = that.skuPicker.skusArray[sku];
				}
			}
			if(!valid){
				$(that.skuPicker.idPrefix+'pid').value = '';
				that.populateAll();
			}
	}
	this.changeSizeClass = function()
	{
		var i;
		for(i=0; i<that.uniqueColors.length; i++)
			that.uniqueColors[i].parentNode.parentNode.style.display = 'none';
		for(i=0; i<that.uniqueWidths.length; i++)
			that.uniqueWidths[i].style.display = 'none';
		for(i=0; i<that.uniqueSizes.length; i++)
			that.uniqueSizes[i].style.display = 'none';

		var numOfColors = that.skuPicker.multiSkusArray[that.selectedSizeClass+'Colors'].length;
		//$('colorChips').style.height = Math.ceil(numOfColors/4)*22+'px';
		var currentChip = null;
		var left = 1;
		var top = 0;
		for(i=1; i<=numOfColors; i++)
		{
			currentChip = $(that.skuPicker.idPrefix+that.skuPicker.multiSkusArray[that.selectedSizeClass+'Colors'][i-1]);
			currentChip.parentNode.parentNode.style.left = left+'px';
			currentChip.parentNode.parentNode.style.top = top+'px';
			currentChip.parentNode.parentNode.style.display = 'block';
			left += 55;

			if(i%4 == 0)
			{
				left = 1;
				top += 22;
			}
		}
		if(that.skuPicker.hasWidth){
			for(i=0; i<that.skuPicker.multiSkusArray[that.selectedSizeClass+'Widths'].length; i++)
				$(that.skuPicker.idPrefix+that.skuPicker.multiSkusArray[that.selectedSizeClass+'Widths'][i]).style.display = 'block';
		}
		if(that.skuPicker.hasSize){
			for(i=0; i<that.skuPicker.multiSkusArray[that.selectedSizeClass+'Sizes'].length; i++)
				$(that.skuPicker.idPrefix+that.skuPicker.multiSkusArray[that.selectedSizeClass+'Sizes'][i]).style.display = 'block';
		}
		if(that.skuPicker.hasSizeClass){
			for(i=0; i<that.uniqueSizeClasses.length; i++)
				that.uniqueSizeClasses[i].className = 'availableSizeClass';
		}
		if($(that.skuPicker.idPrefix+'errColor'))
			$(that.skuPicker.idPrefix+'errColor').style.display = 'none';
		if($(that.skuPicker.idPrefix+'errWidth'))
			$(that.skuPicker.idPrefix+'errWidth').style.display = 'none';
		if($(that.skuPicker.idPrefix+'errSize'))
			$(that.skuPicker.idPrefix+'errSize').style.display = 'none';
	}
	this.populateAll = function()
	{
		var i;
		for(i=0; i<that.uniqueColors.length; i++)
			that.uniqueColors[i].className = 'available';
		for(i=0; i<that.uniqueWidths.length; i++)
			that.uniqueWidths[i].className = 'available';
		for(i=0; i<that.uniqueSizes.length; i++)
			that.uniqueSizes[i].className = 'available';

		if(that.selectedColor != -1)
			$(that.skuPicker.idPrefix+that.selectedColor).className = 'available';
		that.selectedColor = -1;
		if(that.selectedWidth != -1)
			$(that.skuPicker.idPrefix+that.selectedWidth).className = 'available';
		if(that.skuPicker.hasWidth)	
			that.selectedWidth = -1;
		if(that.selectedSize != -1)
			$(that.skuPicker.idPrefix+that.selectedSize).className = 'available';
		if(that.skuPicker.hasSize)
			that.selectedSize = -1;
	}
	this.fillPreselections = function() {
		var sizeClassPreselection = $('sizeClassPreselected');
		if (sizeClassPreselection != null) {
			var sizeClassElement = $(that.skuPicker.sizeClassPrefix+sizeClassPreselection.value);
			if (sizeClassElement != null) {
				that.updateFieldsForElement(sizeClassElement);
			}
		}
		var sizeClassPreselectionURL = $('sizeClassPreselectedURL');
		if (sizeClassPreselectionURL != null) {
			var sizeClassElementURL = $(that.skuPicker.sizeClassPrefix+getSizeClassMapping(sizeClassPreselectionURL.value));
			if (sizeClassElementURL != null) {
				that.updateFieldsForElement(sizeClassElementURL);
			}
		}
		var colorPreselection = $('colorPreselected');
		if (colorPreselection != null) {
			var colorElement = $(colorPreselection.value);
			if (colorElement != null) {
				that.updateFieldsForElement(colorElement);
			}
		}
		var colorPreselection = $('colorPreselectedURL');
		if (colorPreselection != null) {
			var colorElement = $(colorPreselection.value);
			if (colorElement != null) {
				that.updateFieldsForElement(colorElement);
			}
		}
		var widthPreselection = $('widthPreselected');
		if (widthPreselection != null) {
			var widthElement = $(widthPreselection.value);	
			if (widthElement != null) {
				that.updateFieldsForElement(widthElement);
			}
		}
		var sizePreselection = $('sizePreselected');
		if (sizePreselection != null) {
			var sizeElement = $(sizePreselection.value);
			if (sizeElement != null) {
				that.updateFieldsForElement(sizeElement);
			}
		}
	}
}

function checkForUnique(value, array){
	var i;
	for(i = 0; i < array.length; i++){
		if(array[i] == value)
			return false;
	}
	return true;
}
function checkForUniqueColor(value, array){
	var i;
	for(i = 0; i < array.length; i++){
		if(array[i].colorCode == value)
			return false;
	}
	return true;
}
function Color(cCode,cDescription){
	var temp = cDescription.strip();
	if(temp == '')
		this.colorDescription = cCode;
	else
		this.colorDescription = temp;
	this.colorCode = cCode;	
	temp = null;
}
function getSizeClassMapping(strSizeClass){
	var sizeClasses = {};
	sizeClasses["Missy"] = "01";
	sizeClasses["Petite"] = "02";
	sizeClasses["Woman"] = "03";
	return sizeClasses[strSizeClass];
}
function addToBagDetail(){
	this.preOrder = false;
	this.size = "";
	this.width = "";
	this.color = "";
	this.error = false;
	this.upc = "";
	this.price = "";
}
function omnAddtoBagReport(addedItemDetails){
	if(addedItemDetails.error) return;
	
	var colorAdded = addedItemDetails.color;
	var widthAdded = addedItemDetails.width;
	var sizeAdded = addedItemDetails.size;

	s.pageName="Shopping Bag: Add To Bag";
	s.server="";
	s.channel="";
	s.pageType="";
	s.prop1="Add to Bag";
	s.prop2="";
	s.prop3="";
	s.prop4="";
	s.prop5="";
	/* Conversion Variables */
	s.campaign="";
	s.state="";
	s.zip="";
	s.events="scAdd";
	s.event="";
	s.products=omnProducts.unescapeHTML();
	s.purchaseID="";
	s.eVar1="";
	s.eVar2="";
	s.eVar3=addedItemDetails.size;
	s.eVar4=addedItemDetails.width;
	s.eVar5=addedItemDetails.color;
	s.eVar6="";
	s.eVar7="";
	s.eVar8="";
	s.eVar9="";
	if(addedItemDetails.preOrder)
		s.eVar6=omnProducts.unescapeHTML().replace(";","","g");
	void(s.t());
}
function siteClarityAddtoBag(addedItemDetails){
	if(addedItemDetails.error) 
		return;
	if(siteClarityAddBag)
		siteClarityAddBag(addedItemDetails.upc,addedItemDetails.price);
}