var globalsc; 

function Product(numSkus, numColors, colorTable, sizeTable, dimTable) {
	this.skus = new Array(numSkus);
	this.counter = 0;
	this.setSku = setSku;
	this.loadPicture = loadPicture;
	this.dropDownUpdate = dropDownUpdate;
	this.resetMenuForm = resetMenuForm;
	this.updateMenus = updateMenus;
	this.updateGiftCard = updateGiftCard;
	this.updateColors = updateColors;
	this.showColors = showColors;
	this.showAllColors = showAllColors;
	this.numColors = numColors;
	this.colorTable = colorTable;
	this.sizeTable = sizeTable;
	this.dimTable = dimTable;
	this.getTotalColors = getTotalColors;
	this.fixURL = fixURL;
	
	function setSku(skuCode, size, color, dim, dImage, altImages, price, other, thumbs, promoText) {
		var sku = new Sku(skuCode, size, color, dim, dImage, altImages, price, other, thumbs, promoText);
		this.skus[this.counter] = sku;
		this.counter = this.counter + 1;
	}
	function loadPicture(skuCode, id) {
	  var Sku;
	  var found = false;
	  for (i in this.skus) { 
	    if (this.skus[i].skuCode == skuCode) {
	      Sku = this.skus[i];
	      found = true;
	    } 
	  }
	  if (Sku.altImages.length > 0 && (id.length == 6 || id.length == 7) && id.substring(0,5) == 'front') {
      if (document.getElementById('back' + id.substring(5,id.length))) {
	  		document.getElementById('back' + id.substring(5,id.length)).src = Sku.altImages[0];
	  	}
	  }
	  
	  <!-- swap out the detail image and cross sell images -->
    if (document.images && found) {
      document.images[id].src=Sku.dImage;
	    if (id == 'detailpic0' || (id.length > 5 && id.substring(0,5) == 'thumb')) {
		    var j = 0;
		    if (id == 'detailpic0') {
		    	
          // Swap the onclick to load different sku to print off
          if (document.getElementById('print_page')) {
            globalsc = Sku.skuCode;
          	var printButton = document.getElementById('print_page');
            printButton.onclick = function(){
              showPrintPage(globalsc);
            }
			    }
          
			    //This code allows the javascript to swap out cross sells.
			    //for (j in Sku.altImages) {
			     //document.getElementById('crossSell' + j).style.display = 'block';
			     //var cross = Sku.altImages[j];
			     //document.getElementById('crossSwatches' + j).innerHTML = cross[1];
			     //document.getElementById('desc' + j).innerHTML = cross[3];
			     //document.getElementById('mainlink' + j).href = cross[0];
			     //document.getElementById('images' + j).innerHTML=cross[2];
			     //document.getElementById('price' + j).innerHTML = cross[4];
			    //}
			    //while (j < 3) {
			       //document.getElementById('crossSell' + j).style.display = 'none';
			       //j++;
			   // }
		    }
		    if (id.substring(0,5) == 'thumb') {
		    	for (k in Sku.other) {
			    	document.images['moreViews' + id.substring(5,6) + k].src = Sku.other[k];
			    	document.images['smallViews' + id.substring(5,6) + k].src = Sku.thumbs[k];
			    }
		    } else {
		    	if (document.images['moreViews' + 0]) {
				    for (k in Sku.other) {
				    	document.images['moreViews' + k].src = Sku.other[k];
				    	document.images['smallViews' + k].src = Sku.thumbs[k];
				    }
			    } 
			}
		 }
	  } 
	}
	
	function fixURL(prdCode, skuCode) {
		if (document.getElementById('moreFrame')) {
			var dim = "";
			var size = "";
			var color = "";
			if (document.getElementById('Size')) {
				size = document.getElementById('Size').value;
			}
			if (document.getElementById('Dim')) {
				dim = document.getElementById('Dim').value;
			}
			if (document.images['detailpic']) {
				var split = document.images['detailpic'].src.split('_');
				if (split.length == 3) {
					color = document.images['detailpic'].src.split('_')[1];
				}
			}
			if (color == "") {
				if (document.getElementById('Color')) {
					color = document.getElementById('Color').value;
				}
			}
			var skucode = "";
			for (i in this.skus) {
				var Sku = this.skus[i];
				if (Sku.color[0] == color) {
					if (size != "" && dim != "") {
						if (Sku.size[0] == size && Sku.dim[0] == dim) {
							skucode = Sku.skuCode;
						}
					} else if (size != "") {
						if (Sku.size[0] == size) {
							skucode = Sku.skuCode;
						}
					} else if (dim != "") {
						if (Sku.dim[0] == dim) {
							skucode = Sku.skuCode;
						}
					} else {
						skucode = Sku.skuCode;
					}
				}
			}
			if (skucode == "") {
				skucode = skuCode;
			}
			var url = document.getElementById('moreFrame').src;
			if (url.indexOf('?') != -1) {
				url = url.substring(0, url.indexOf('?'));
			}
			document.getElementById('moreFrame').src = url + '?prd=' + prdCode + '&sku=' + skucode + '&size=' + size + '&dim=' + dim;
	    }
	}
	
	function updateColors(sizeNM, dimNM, form, notAllSkus, index) {
	    //alert('function updateColors index = '+index);
		var sizeValue = "";
		var dimValue = "";
		if (form.elements[sizeNM]) {
		  sizeValue = form.elements[sizeNM].value;
		}
		if (form.elements[dimNM]) {
		  dimValue = form.elements[dimNM].value;
		}
		var colors = new Array(this.skus.length);
		var counter = 0;
		for (i in this.skus) {
		  if (sizeValue != "" && dimValue != "") {
			  if (this.skus[i].size[0] == sizeValue && this.skus[i].dim[0] == dimValue) {
			  	var found = false;
			  	for (j in colors) {
			  		if (this.skus[i].color[0] == colors[j]) {
			  			found = true;
			  		}
			  	}
			  	if (!found) {
				  	colors[counter] = this.skus[i].color[0];
				  	counter++;
			  	}
			  }
		  } else if (sizeValue == "" && dimValue != "") {
		  	if (this.skus[i].dim[0] == dimValue) {
			  	var found = false;
			  	for (j in colors) {
			  		if (this.skus[i].color[0] == colors[j]) {
			  			found = true;
			  		}
			  	}
			  	if (!found) {
				  	colors[counter] = this.skus[i].color[0];
				  	counter++;
			  	}
			  }
		  } else if (sizeValue != "" && dimValue == "") {
			  if (this.skus[i].size[0] == sizeValue) {
				  	var found = false;
				  	for (j in colors) {
				  		if (this.skus[i].color[0] == colors[j]) {
				  			found = true;
				  		}
				  	}
				  	if (!found) {
					  	colors[counter] = this.skus[i].color[0];
					  	counter++;
				  	}
			   }
		  } else {
		  	var found = false;
		  	for (j in colors) {
		  		if (this.skus[i].color[0] == colors[j]) {
		  			found = true;
		  		}
		  	}
		  	if (!found) {
			  	colors[counter] = this.skus[i].color[0];
			  	counter++;
		  	}
		  }
		  
		}
		this.showColors(colors, notAllSkus, index);
	}
	
	function getTotalColors() {
		var colors = new Array(this.skus.length);
		var counter = 0;
		for (i in this.skus) {
			var found = false;
		  	for (j in colors) {
		  		if (this.skus[i].color[0] == colors[j]) {
		  			found = true;
		  		}
		  	}
		  	if (!found) {
			  	colors[counter] = this.skus[i].color[0];
			  	counter++;
		  	}
		 }
		 return counter;
	}
	
	function showColors(colors, notAllSkus, index) {
		for (var k = 1; k <= this.numColors; k++) {
			var counter = 0;
			for (i in this.skus) {
				var a = document.getElementById(k + 'a_' + this.skus[i].color[0] + '_' + this.skus[i].skuCode);
				var img = document.getElementById(k + 'img_' + this.skus[i].color[0] + '_' + this.skus[i].skuCode);
				if (img && a) {
					//alert('Setting i && a to a class');
					img.className = "transparent";
					a.href = "javascript:;";
					for (j in colors) {
						if (colors[j] == this.skus[i].color[0]) {
							counter++;
							img.className = "swatchQ";
							a.href = "javascript:jsproduct" + index + ".updateMenus('" + this.skus[i].color[1] + "', notAllSkus, "+ index + ");";
						}
					}
				}
			}
			//alert("Number visible " + counter + " Number possible " + this.getTotalColors());
			if (counter == this.getTotalColors()) {
					if (document.getElementById('showColorsLink')) {
					document.getElementById('showColorsLink').style.visibility = 'hidden';
				}
			} else {
				if (document.getElementById('showColorsLink')) {
					document.getElementById('showColorsLink').style.visibility = 'visible';
				}
			}
		}
	}
	
	function updateMenus(color, notAllSkus, index) {
	   //alert(' updateMenus ');
	   var elementId = "Color" + index;
	   //alert(' elementID = '+elementId);
	   if (document.getElementById(elementId)) {
			var colors = document.getElementById(elementId).options;
			var found = false;
			var id='detailpic'+index;
			for (var i = 0; i < colors.length; i++) {
			    //alert('colors['+i+'] = '+colors[i] + ' color = '+color);
			    if (colors[i].text == color) {
					found = true;
					document.getElementById(elementId).options[i].selected = true;
					//alert('going to dropDownUpdate. id = ' + id);
					this.dropDownUpdate('colorChange', document.forms['shopping_cart'], id, index, notAllSkus);
				}
			}
		
			if (!found) {
				this.resetMenuForm();
				colors = document.getElementById(elementId).options;
				for (var i = 0; i < colors.length; i++) {
					if (colors[i].text == color) {
						document.getElementById(elementId).options[i].selected = true;
						//alert('going to dropDownUpdate. id = ' + id);
						this.dropDownUpdate('colorChange', document.forms['shopping_cart'], id, index, notAllSkus);
					}
				}
			}
		}
	}
	
	function showAllColors(notAllSkus, index) {
		for (i in this.skus) {
			for (var k = 1; k <= this.numColors; k++) {
				var a = document.getElementById(k + 'a_' + this.skus[i].color[0] + '_' + this.skus[i].skuCode);
				var img = document.getElementById(k + 'img_' + this.skus[i].color[0] + '_' + this.skus[i].skuCode);
				if (i && a) {
					//alert('Setting i && a to no class');
					img.className = "swatchQ";
					a.href = "javascript:jsproduct" + index + ".updateMenus('" + this.skus[i].color[1] + "', notAllSkus, " + index + ");";
				}	
			}
		}
		if (document.getElementById('showColorsLink')) {
			document.getElementById('showColorsLink').style.visibility = 'hidden';
		}
		if (document.getElementById('skuPrice')) {
	   		document.getElementById('skuPrice').innerHTML = "&nbsp;";
	   		if (notAllSkus == 'true') {
	   			if (document.getElementById('promoDiv')) {
		   			document.getElementById('promoDiv').innerHTML = "";
		   		}
	    	}
	   	}
	}
	
	function resetMenuForm() {
		if (document.getElementById('Color0')) {
			document.getElementById('Color0').options.length = 1;
			for (i in this.skus) {
	    		var Sku = this.skus[i];
	    		addOption(Sku.color, 'Color0', document.forms['shopping_cart']);
		    }
		    document.getElementById('Color0').options[0].selected = true;
		}
		if (document.getElementById('Size0')) {
			document.getElementById('Size0').options.length = 1;
			for (i in this.skus) {
	    		var Sku = this.skus[i];
	    		addOption(Sku.size, 'Size0', document.forms['shopping_cart']);
		    }
		    document.getElementById('Size0').options[0].selected = true;
		}
		if (document.getElementById('Dim0')) {
			document.getElementById('Dim0').options.length = 1;
			for (i in this.skus) {
	    		var Sku = this.skus[i];
	    		addOption(Sku.dim, 'Dim0', document.forms['shopping_cart']);
		    }
		    document.getElementById('Dim0').options[0].selected = true;
		}
	}
	
	function dropDownUpdate(selectName, form, id, index, notAllSkus) {
	
	 //alert('Firing dropDownUpdate id=' + id);
	 var selectedColor="";	//value of the color dropdown
	 var selectedSize="";	//value of the size dropdown
	 var selectedDim=""; 	//value of the dim dropdown
	 var colorSelect="";	//name of the color dropdown 
	 var sizeSelect="";		//name of the size dropdown
	 var dimSelect="";		//name of the dim dropdown
	 
	if (index == null || index == -1) {
		index = 0;
	}
	var skucdElement = "skucd"+index;
	var skuPriceElement = "skuPrice" + index;
	//alert('index = ' + index);
	if (this.colorTable != '' && this.colorTable != 'null') { 
		var colorElementId = "Color" + index;
	    //alert("colorElementId = " + colorElementId);
	    selectedColor = document.getElementById(colorElementId).value; 
		colorSelect='Color' + index;
	    //alert("Color dropdown = " + colorSelect);
	}
	if (this.sizeTable != '' && this.sizeTable != 'null') { 
		var sizeElementId = "Size" + index;
		selectedSize = document.getElementById(sizeElementId).value; 
		sizeSelect='Size' + index;
		//alert("Size dropdown = " + sizeSelect);
	}
	if (this.dimTable != '' && this.dimTable != 'null') {   
		var dimElementId = "Dim" + index;
		selectedDim = document.getElementById(dimElementId).value; 
		dimSelect='Dim' + index;
		//alert("Dim dropdown = " + dimSelect);
	} 
	 
	if (selectName=="colorChange"){
		if (sizeSelect != "") {
			//alert('Updating Size');
	    	if (selectedDim == "") {
		    	document.getElementById(sizeSelect).options.length = 1;
		    	for (i in this.skus) {
		    		var Sku = this.skus[i];
		    		//alert('Color ' + Sku.color[0] + ' ' + selectedColor);
		    		if (Sku.color[0] == selectedColor || selectedColor == '') {
		    			//alert('Matches');
		    			addOption(Sku.size, 'Size' + index, form);
		    		}
		    	}
		    	setValue(selectedSize, 'Size' + index, form);
		    	
	    	} else {
		    	document.getElementById(sizeSelect).options.length = 1;
		    	for (i in this.skus) {
		    		var Sku = this.skus[i];
		    		//alert('Color ' + Sku.color[0] + ' ' + selectedColor);
		    		//alert('Dim ' + Sku.dim[0] + ' ' + selectedDim);
		    		if ((Sku.color[0] == selectedColor && Sku.dim[0] == selectedDim) || (selectedColor == '' && Sku.dim[0] == selectedDim)) {
		    			//alert('Matches');
		    			addOption(Sku.size, 'Size' + index, form);
		    		}
		    	}
		        setValue(selectedSize, 'Size' + index, form);
			}
		}
	    if (dimSelect != "") {
	    	//alert('Updating Dim');
	    	if (selectedSize == "") {
		    	document.getElementById(dimSelect).options.length = 1;
		    	for (i in this.skus) {
		    		var Sku = this.skus[i];
		    		//alert('Color ' + Sku.color[0] + ' ' + selectedColor);
		    		if (Sku.color[0] == selectedColor || selectedColor == '') {
		    			//alert('Matches');
		    			addOption(Sku.dim, 'Dim' + index, form);
		    		}
		    	}
		    	setValue(selectedDim, 'Dim' + index, form);
		    	
	    	} else {
		    	document.getElementById(dimSelect).options.length = 1;
		    	for (i in this.skus) {
		    		var Sku = this.skus[i];
		    		//alert('Color ' + Sku.color[0] + ' ' + selectedColor);
		    		//alert('Size ' + Sku.size[0] + ' ' + selectedSize);
		    		if ((Sku.color[0] == selectedColor && Sku.size[0] == selectedSize) || (selectedColor == '' && Sku.size[0] == selectedSize)) {
		    			addOption(Sku.dim, 'Dim' + index, form);
		    			//alert('Matches');
		    		}
		    	}
		    	setValue(selectedDim, 'Dim' + index, form);
	    	}
	    }
	  }
	   
	  if (selectName=="sizeChange"){
	  //alert('selectName==sizeChange');
	  	this.updateColors(sizeSelect, dimSelect, form, notAllSkus, index);
	    if (colorSelect != "") {
	    	//alert('Updating Color');
	    	if (selectedDim == "") {
	    	    //alert('no dim on this product');
		    	document.getElementById(colorSelect).options.length = 1;
		    	for (i in this.skus) {
		    		var Sku = this.skus[i];
		    		//alert('Size ' + Sku.size[0] + ' ' + selectedSize);
		    		if (Sku.size[0] == selectedSize || selectedSize == '') {
		    			//alert('Matches');
		    			addOption(Sku.color, 'Color' + index, form);
		    		}
		    	}
		    	setValue(selectedColor, 'Color' + index, form);
	    	} else {
		    	document.getElementById(colorSelect).options.length = 1;
		    	for (i in this.skus) {
		    		//alert('Checking i ' + i);
		    		var Sku = this.skus[i];
		    		//alert('Size ' + Sku.size[0] + ' ' + selectedSize);
		    		//alert('Dim ' + Sku.dim[0] + ' ' + selectedDim);
		    		if ((Sku.size[0] == selectedSize && Sku.dim[0] == selectedDim) || ('' == selectedSize && Sku.dim[0] == selectedDim)) {
		    			//alert('Matches');
		    			addOption(Sku.color, 'Color' + index, form);
		    		}
		    	}
		    	setValue(selectedColor, 'Color' + index, form);
	    	}
	    }
	    if (dimSelect != "") {
	    	//alert('Updating Dim');
	    	if (selectedColor == "") {
		    	document.getElementById(dimSelect).options.length = 1;
		    	for (i in this.skus) {
		    		var Sku = this.skus[i];
		    		//alert('Size ' + Sku.size[0] + ' ' + selectedSize);
		    		if (Sku.size[0] == selectedSize || selectedSize == '') {
		    			//alert('Matches');
		    			addOption(Sku.dim, 'Dim' + index, form);
		    		}
		    	}
		    setValue(selectedDim, 'Dim' + index, form);
	    	} else {
		    	document.getElementById(dimSelect).options.length = 1;
		    	for (i in this.skus) {
		    		var Sku = this.skus[i];
		    		//alert('Color ' + Sku.color[0] + ' ' + selectedColor);
		    		//alert('Size ' + Sku.size[0] + ' ' + selectedSize);
		    		if ((Sku.size[0] == selectedSize && Sku.color[0] == selectedColor) || ('' == selectedSize && Sku.color[0] == selectedColor)) {
		    			//alert('Matches');
		    			addOption(Sku.dim, 'Dim' + index, form);
		    		}
		    	}
		    	setValue(selectedDim, 'Dim' + index, form);
	    	}
	    }
	  }
	  
	  if (selectName=="dimChange"){
	    this.updateColors(sizeSelect, dimSelect, form, notAllSkus, index);
	    if (sizeSelect != "") {
	    	//alert('Updating Size');
	    	if (selectedColor == "") {
		    	document.getElementById(sizeSelect).options.length = 1;
	    		for (i in this.skus) {
	    			var Sku = this.skus[i];
	    			//alert('Dim ' + Sku.dim[0] + ' ' + selectedDim);
	    			if (Sku.dim[0] == selectedDim || selectedDim == '') {
	    				//alert('Matches');
	    				addOption(Sku.size, 'Size' + index, form);
	    			}
	    		}
	    		setValue(selectedSize, 'Size' + index, form);
    		} else  {
	    		document.getElementById(sizeSelect).options.length = 1;
	    		for (i in this.skus) {
	    			var Sku = this.skus[i];
	    			//alert('Color ' + Sku.color[0] + ' ' + selectedColor);
	    			//alert('Dim ' + Sku.dim[0] + ' ' + selectedDim);
	    			if ((Sku.color[0] == selectedColor && Sku.dim[0] == selectedDim) || (Sku.color[0] == selectedColor && '' == selectedDim)) {
	    				//alert('Matches');
	    				addOption(Sku.size, 'Size' + index, form);
	    			}
	    		}
	    		setValue(selectedSize, 'Size' + index, form);
    		}
    	}
    	if (colorSelect != "") {
    		//alert('Updating Color');
    		if (selectedSize == "") {
	    		document.getElementById(colorSelect).options.length = 1;
	    		for (i in this.skus) {
	    			var Sku = this.skus[i];
	    			//alert('Dim ' + Sku.dim[0] + ' ' + selectedDim);
	    			if (Sku.dim[0] == selectedDim || '' == selectedDim) {
	    				//alert('Matches');
	    				addOption(Sku.color, 'Color' + index, form);
	    			}
	    		}
	    		setValue(selectedColor, 'Color' + index, form);
    		} else {
	    		document.getElementById(colorSelect).options.length = 1;
	    		for (i in this.skus) {
	    			//alert('Dim ' + Sku.dim[0] + ' ' + selectedDim);
	    			//alert('Size ' + Sku.size[0] + ' ' + selectedSize);
	    			var Sku = this.skus[i];
	    			if ((Sku.dim[0] == selectedDim && Sku.size[0] == selectedSize) || ('' == selectedDim && Sku.size[0] == selectedSize)) {
	    				//alert('Matches');
	    				addOption(Sku.color, 'Color' + index, form);
	    			}
	    		}
	    		setValue(selectedColor, 'Color' + index, form);
    		}
    	}
  	}
  	//alert("selectName = " + selectName);
  	//alert("selectedColor = " + selectedColor);
  	//alert("id = " + id);
	if (selectedColor != "" && selectName=="colorChange") {
		for (i in this.skus) {
			Sku = this.skus[i];
			if (Sku.color[0] == selectedColor) {
				//alert("Loading picture " + id);
				this.loadPicture(Sku.skuCode, id);
			}
		}
	}
	//alert("selectedColor:" + selectedColor + "END");
	//alert("selectedSize:" + selectedSize + "END");
	//alert("selectedDim:" + selectedDim + "END");
	if (selectedColor == "" && selectedSize == "" && selectedDim == "") {
		//alert("Calling show all colors!!!");
		this.showAllColors(notAllSkus, index);
	}
	//alert('Size: ' + sizeSelect + ' Dim: ' + dimSelect + ' Color: ' + colorSelect);
	if (sizeSelect != "" && dimSelect != "") {
		//alert('I have Size, Dim, and Color.  ');
		if (selectedColor != "" && selectedSize != "" && selectedDim != "") {
			if (document.getElementById('skuPrice') ||  
			    document.getElementById(skuPriceElement) || 
			    document.getElementById(skucdElement)){
		   		for (var i = 0; i < this.skus.length; i++) {
		   			var Sku = this.skus[i];
					if (Sku.color[0] == selectedColor && Sku.size[0] == selectedSize && Sku.dim[0] == selectedDim) {
	    				if(document.getElementById(skucdElement)){
	    					document.getElementById(skucdElement).value = Sku.skuCode;
	    				}
	    				if(document.getElementById('skuPrice') || document.getElementById(skuPriceElement))  {
	    					if(document.getElementById('skuPrice')) {
	    						document.getElementById('skuPrice').innerHTML = Sku.price; 
	    					}
	    					if(document.getElementById(skuPriceElement)) {
	    						document.getElementById(skuPriceElement).innerHTML = Sku.price; 
	    					}
	    					if (notAllSkus == 'true' && document.getElementById('promoDiv')) {
		    					document.getElementById('promoDiv').innerHTML = Sku.promoText;
							}
	    				}						
					}
				}
			}
		} else {
			if (document.getElementById('skuPrice') || document.getElementById(skuPriceElement)) {
		   		if(document.getElementById('skuPrice')){
		   			document.getElementById('skuPrice').innerHTML = "&nbsp;";
		   		}
		   		if(document.getElementById(skuPriceElement)) {
		   			document.getElementById(skuPriceElement).innerHTML = "&nbsp;";
		   		}
		   		if (notAllSkus == 'true') {
			   		if (document.getElementById('promoDiv')) {
			   			document.getElementById('promoDiv').innerHTML = "";
		    		}
		    	}
		   	}
		}
	} else if (sizeSelect != "") {
		//alert('I have Size and Dim.');
		if (selectedColor != "" && selectedSize != "") {
	   		if (document.getElementById('skuPrice') ||  
	   			document.getElementById(skuPriceElement)||
	   			document.getElementById(skucdElement)){
	   			for (var i = 0; i < this.skus.length; i++) {
	   				var Sku = this.skus[i];
	   				if (Sku.color[0] == selectedColor && Sku.size[0] == selectedSize) {
	   					if(document.getElementById(skucdElement)){
							document.getElementById(skucdElement).value = Sku.skuCode;
						}
						if(document.getElementById('skuPrice') || document.getElementById(skuPriceElement)){
							if(document.getElementById('skuPrice')){
	   							document.getElementById('skuPrice').innerHTML = Sku.price; 
	   						}
	   						if(document.getElementById(skuPriceElement)) {
	   							document.getElementById(skuPriceElement).innerHTML = Sku.price; 
	   						}
	   						if (notAllSkus == 'true' && document.getElementById('promoDiv')) {
	   							document.getElementById('promoDiv').innerHTML = promoText;
	   						}
	   					}
	   				}
				}
	   		}
	   	} else {
	   		if (document.getElementById('skuPrice') || document.getElementById(skuPriceElement)) {
	   			if(document.getElementById('skuPrice')){
	   				document.getElementById('skuPrice').innerHTML = "&nbsp;";
	   			}
	   			if(document.getElementById(skuPriceElement)){
	   				document.getElementById(skuPriceElement).innerHTML = "&nbsp;";
	   			}
	   			if (notAllSkus == 'true') {
		   			if (document.getElementById('promoDiv')) {
		   				document.getElementById('promoDiv').innerHTML = "";
					}
				}
			}
		}
	} else if (dimSelect != "") {
		//alert('I have Dim and Color');
		if (selectedColor != "" && selectedDim != "") {
		   	if (document.getElementById('skuPrice') || 
		   		document.getElementById(skuPriceElement) ||
		   		document.getElementById(skucdElement)){
		   		for (var i = 0; i < this.skus.length; i++) {
		   			var Sku = this.skus[i];
		   			if (Sku.color[0] == selectedColor && Sku.dim[0] == selectedDim) {
	    				if(document.getElementById(skucdElement)){
							document.getElementById(skucdElement).value = Sku.skuCode;
						}
	    				if (document.getElementById('skuPrice') || document.getElementById(skuPriceElement))  {
	    					if(document.getElementById('skuPrice')){
	    						document.getElementById('skuPrice').innerHTML = Sku.price; 
	    					}
	    					if(document.getElementById(skuPriceElement)){
	    						document.getElementById(skuPriceElement).innerHTML = Sku.price; 
	    					}
	    					if (notAllSkus == 'true' && document.getElementById('promoDiv')) {
	    						document.getElementById('promoDiv').innerHTML = Sku.promoText;
	    					}
	    				}	
	    			}
	    		}
	    	}
	   } else {
	   		if (document.getElementById('skuPrice') || document.getElementById(skuPriceElement) ) {
	   			if(document.getElementById('skuPrice')){
	   				document.getElementById('skuPrice').innerHTML = "&nbsp;";
	   			}
	   			if(document.getElementById(skuPriceElement)){
	    			document.getElementById(skuPriceElement).innerHTML = Sku.price; 
	    		}
	   			if (notAllSkus == 'true') {
		   			if (document.getElementById('promoDiv')) {
		   				document.getElementById('promoDiv').innerHTML = "";
					}
				}
			}
		}
	} else {
		//alert('I have only COLOR ');
		if (selectedColor != "") {
			if(document.getElementById('skuPrice') || 
			document.getElementById(skuPriceElement) ||
			document.getElementById(skucdElement)){
				for (var i = 0; i < this.skus.length; i++) {
				    var Sku = this.skus[i];
					if(selectedColor == Sku.color[0]) {
						if(document.getElementById(skucdElement)){
							document.getElementById(skucdElement).value = Sku.skuCode;
						}
						if(document.getElementById('skuPrice') || document.getElementById(skuPriceElement)) {
							if(document.getElementById('skuPrice')) {
								document.getElementById('skuPrice').innerHTML = Sku.price; 
							}
							if(document.getElementById(skuPriceElement)) {
								document.getElementById(skuPriceElement).innerHTML = Sku.price; 
							}
							if (notAllSkus == 'true' && document.getElementById('promoDiv')) {
								document.getElementById('promoDiv').innerHTML = Sku.promoText;
							}
						}
					}
				}
			}
		} else {
			if (document.getElementById('skuPrice') || document.getElementById(skuPriceElement)) {
				if(document.getElementById('skuPrice')) {
					document.getElementById('skuPrice').innerHTML = "&nbsp;";
				}
				if(document.getElementById(skuPriceElement)) {
					document.getElementById(skuPriceElement).innerHTML = "&nbsp;";
				}
				if (notAllSkus == 'true') {
					if (document.getElementById('promoDiv')) {
						document.getElementById('promoDiv').innerHTML = "";
					}
				}
			}
		}
	}
	  	
	} 
	
	function updateGiftCard(form, id) {
		this.loadPicture(form.elements['ADD_CART_ITEM<>sku_id'].value, id);
		if (document.getElementById('skuPrice')) {
	   		for (var i = 0; i < this.skus.length; i++) {
	   			var Sku = this.skus[i];
	   			if (Sku.skuCode == form.elements['ADD_CART_ITEM<>sku_id'].value) {
    				if(document.getElementById('skuPrice')){
    					document.getElementById('skuPrice').innerHTML = Sku.price; 
    				}
    				if (notAllSkus == 'true' && document.getElementById('promoDiv')) {
    					document.getElementById('promoDiv').innerHTML = Sku.promoText;
	    			}
	    		} 
    		}
    	}
	}
}

function Sku(skuCode, size, color, dim, dImage, altImages, price, other, thumbs, promoText) {
	this.skuCode = skuCode;
	this.size = size;
	this.color = color;
	this.dim = dim;
	this.dImage = dImage;
	this.altImages = altImages;
	this.price = price;
	this.other = other;
	this.thumbs = thumbs;
	this.promoText = promoText;
}

function compareStrings(string1, string2) {
		var comp1;
		var comp2;
		try {
			comp1 = parseInt(string1);
			comp2 = parseInt(string2);
		} catch(err) {
			comp1 = string1;
			comp2 = string2;
		}
		if (!comp1 || !comp2) {
			comp1 = string1;
			comp2 = string2;
		}
		var sizes = new Array(18);
		sizes[0] = 'Youth XXX-Small';
		sizes[1] = 'Youth XX-Small';
		sizes[2] = 'Youth X-Small';
		sizes[3] = 'Youth Small';
		sizes[4] = 'Youth Medium';
		sizes[5] = 'Youth Large';
		sizes[6] = 'Youth X-Large';
		sizes[7] = 'Youth XX-Large';
		sizes[8] = 'Youth XXX-Large';
		sizes[9] = 'XXX-Small';
		sizes[10] = 'XX-Small';
		sizes[11] = 'X-Small';
		sizes[12] = 'Small';
		sizes[13] = 'Medium';
		sizes[14] = 'Large';
		sizes[15] = 'X-Large';
		sizes[16] = 'XX-Large';
		sizes[17] = 'XXX-Large';
		
		for (var i = 0; i < sizes.length; i++) {
			if (string1 == sizes[i]) {
				comp1 = i;
			}
			if (string2 == sizes[i]) {
				comp2 = i;
			}
		}
		
		return (comp1 > comp2);
}

function addOption(SkuElement, select, form) {

	//alert("Name of select " + select);
		var selectBox = document.getElementById(select);

	//alert('My length ' + document.getElementById(select).options.length);
	for (var i = 0; i < selectBox.options.length; i++) {
		//alert('Add i ' + i);
		if (selectBox.options[i].value == SkuElement[0]) {
			//alert('Im returning');
			return;
		}
	}
	//alert('Adding ' + SkuElement[0] + ' ' + SkuElement[1]);
	
	//This following code makes sure to insert the options in order. Bug #1516
	for (var i = 0; i < selectBox.options.length; i++) {
		if(selectBox.options[i].value == '') continue;
		
		if (compareStrings(selectBox.options[i].text, SkuElement[1])) {
			for (var j = selectBox.options.length; j > i; j--) {
				selectBox.options[j] = new Option(selectBox.options[j-1].text, selectBox.options[j-1].value);
			}
			selectBox.options[i] = new Option(SkuElement[1], SkuElement[0]);
			//alert('Adding First: ' + SkuElement[1] + ', ' + SkuElement[0] + ' at ' + i);
			return;
		}
	}
	//alert('Adding End: ' + SkuElement[1] + ', ' + SkuElement[0] + ' at ' + selectBox.options.length);
	selectBox.options[selectBox.options.length] = new Option(SkuElement[1], SkuElement[0]);
}

function setValue(val, select, form) {
	//alert('My length ' + document.getElementById(select).options.length);
	for (var i = 0; i < document.getElementById(select).options.length; i++) {
		//alert('My val ' + val);
		//alert('My obj ' + document.getElementById(select).options[i]);
		if (document.getElementById(select).options[i].value == val){
			//alert('I should be selected ' + val);
			document.getElementById(select).options[i].selected = true;
			break;
		}
	}
}

function submitForm(form, colorTable, sizeTable, dimTable, item, index) {
	
	if (index == null) {
		index = "";
	}
	
	if (((colorTable == "" || colorTable == 'null') || document.getElementById('Color' + index).value != "") && ((sizeTable == "" || sizeTable == 'null') || document.getElementById('Size' + index).value != "") && ((dimTable == "" || dimTable == 'null') || document.getElementById('Dim' + index).value != "")) {
		form.elements[item].click();
	}
	
}

  function swapTabs(num) {
    for(i=1; i <= 4; i++) {
      if (document.getElementById('tab' + i)) {
      	document.getElementById('tab' + i).className = 'tabs';
      	 document.getElementById('cnt' + i).style.display = 'none'
      }
  } 
    document.getElementById('tab' + num).className = 'selectedTab';
    document.getElementById('cnt' + num).style.display = 'block';
  }
  
  function checkHover(num) {
  	if (document.getElementById('cnt' + num).style.display == 'none') {
  		document.getElementById('tab' + num).style.color = '#999999';
  	}
  }

  function hideSelects() {
    for (var i = 0; i < document.forms.length; i++) {
      var elems = document.forms[i].elements;
      for (var j = 0; j < elems.length; j++) {
        if (elems[j].type.length >= 6 && elems[j].type.substring(0,6) == 'select') {
          elems[j].style.visibility='hidden';
  			}
  		}
  	}
  }
  
  function showDiv(name, form, otherDivs) {
  	if (document.getElementById(name)) {
  		document.getElementById(name).style.display='block';
  	}
  	if (name == 'GCDiv') {
  		var pixels = 100;
		  if( typeof( window.pageYOffset ) == 'number' ) {
		    //Netscape compliant
		    pixels = window.pageYOffset;
		  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		    //DOM compliant
		    pixels = document.body.scrollTop;
		  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		    //IE6 standards compliant mode
		    pixels = document.documentElement.scrollTop;
		  }
		pixels = pixels + 100;
		document.getElementById(name).style.top = pixels + 'px';
  	}
  	if (form == '') {
  		//alert('FORM IS EMPTY ' + document.forms.length);
  		for (var i = 0; i < document.forms.length; i++) {
  			var elems = document.forms[i].elements;
  			//alert('NUM ELEMS ' + elems.length);
  			for (var j = 0; j < elems.length; j++) {
  				//alert('TYPE ' + elems[j].type);
  				if (!elems[j].type) {
	  				alert(elems[j].name + " FORM: " + document.forms[i].name + " id: " + elems[j].id);
	  			}
  				if (elems[j].type && elems[j].type.length >= 6 && elems[j].type.substring(0,6) == 'select') {
  					//alert("HIDING SELECT");
  					elems[j].style.visibility='hidden';
  				}
  			}
  		}

  	} else {
  		var elems = document.forms[form].elements;
  		for (var j = 0; j < elems.length; j++) {
  			if (!elems[j].type) {
	  				alert(elems[j].name + " FORM: " + document.forms[form].name + " id: " + elems[j].id);
	  			}
  			if (elems[j].type && elems[j].type.length >= 6 && elems[j].type.substring(0,6) == 'select') {
  					elems[j].style.visibility='hidden';
  			}
  		}
  	}
  	if (otherDivs) {
	  	for (i in otherDivs) {
	  		if (document.getElementById(otherDivs[i])) {
	  			document.getElementById(otherDivs[i]).style.display='none';
	  		}
	  	}
	  }
  }
  
  function hideDiv(name, form) {
  	document.getElementById(name).style.display='none';
  	if (form == '') {
  		for (var i = 0; i < document.forms.length; i++) {
  			var elems = document.forms[i].elements;
  			for (var j = 0; j < elems.length; j++) {
  				if (elems[j].type && elems[j].type && elems[j].type.length >= 6 && elems[j].type.substring(0,6) == 'select') {
  					if (!document.getElementById('dynamic') || document.getElementById('dynamic').style.visibility != 'hidden' || elems[j].name != 'SHIP_TO_ADDRESS<>state_cd') {
  						elems[j].style.visibility='visible';
  					}
  				}
  			}
  		}
  	} else {
  		var elems = document.forms[form].elements;
  		for (var j = 0; j < elems.length; j++) {
  			if (elems[j].type && elems[j].type.length >= 6 && elems[j].type.substring(0,6) == 'select') {
  				if (!document.getElementById('dynamic') || document.getElementById('dynamic').style.visibility != 'hidden' || elems[j].name != 'SHIP_TO_ADDRESS<>state_cd') {
  					elems[j].style.visibility='visible';
  				}
  			}
  		}
  	}
  }
  
  function showDifLarge(show, size) {
  	for (var i = 0; i < size; i++) {
  		if (i == show) {
  			document.getElementById('moreViews' + i).style.display='block';
  		} else {
  			document.getElementById('moreViews' + i).style.display='none';
  		}	
  	}
  }
  
  function showDifLargeMany(item, show, size) {
  	for (var i = 0; i < size; i++) {
  		if (i == show) {
  			document.getElementById('moreViews' + item + i).style.display='block';
  		} else {
  			document.getElementById('moreViews' + item + i).style.display='none';
  		}	
  	}
  }
  
  function changeCross(num, l, swatches, sku) {
  	for (var i = 0; i < swatches; i++) {
  		if (document.getElementById('cross' + num + '_' + i))
  			document.getElementById('cross' + num + '_' + i).style.display='none';
  	}
  	document.getElementById('cross' + num + '_' + l).style.display='block';
  	var list = new Array(1);
  	list[0] = 'mainlink' + num;
  	editLinks(sku, list);
  }
  
  function editLinks(sku, linklist) {
  	for (i in linklist) {
  		var help = document.getElementById(linklist[i]).href;
  		var help2 = document.getElementById(linklist[i]).href;
  		if (help.indexOf('sku=') != -1) {
	  		help = help.substring(help.indexOf('sku='));
	  		if (help.indexOf('&') != -1) {
	  			help = help.substring(0, help.indexOf('&'));
	  		}
	  		var pieces = help2.split('&' + help);
	  		var url = '';
	  		for (j in pieces) {
	  			url = url + pieces[j];
	  		}
	  		url = url + '&sku=' + sku;
	  		document.getElementById(linklist[i]).href = url;
  		}
  	}
  }
  

  var showingBacks = false;
  
  /**
	 *  This little method takes an array of image names and a mode.
	 *  The src of the images on the page are then swapped out with the values
	 *  found in the array with the corresponding index.
	 *  The mode is used to identify if we're going from fronts to backs or 
	 *  from backs to fronts.
	 *
	 *  It is used for the "Show Backs/Fronts" feature on many
	 *  of the product pages.
	 */
  function flipFrontBackImages(altbacks, altfronts) {

	showingBacks = !showingBacks;
  	var alt = altbacks;
  	if (!showingBacks) {
  		alt = altfronts;
  	}
  
	  // Loop through the array of images
	  for (i = 0; i < alt.length; i++) {
	    var img = document.getElementById("img" + i);
	    if (img) {
	      if(pageProducts[i].currentSku){
	      	 setImg(
		        'img'+i,
		        pageProducts[i].currentSku.frontImage,
		        pageProducts[i].currentSku.backImage);
		    }
		    else{
		    img.src = alt[i];
        }
	    }
	  }
	  
    flipShowBackButtons();

  }
  


  /**
	 *  This little method takes a mode.
	 *  The mode is used to identify if we're going from fronts to backs or 
	 *  from backs to fronts.  The HTML is then changed out within the <div>
	 *  identified by backs or backs1 and replaces it with a different form
	 *  so the button will be aligned with the mode that's showing.
	 *
	 *  It is used for the "Show Backs/Fronts" feature on many
	 *  of the product pages.
	 */
  function flipShowBackButtons() {
    if (showingBacks) {
	  	if (document.getElementById('backbutton1')) {
	  		document.getElementById('backbutton1').value = 'Show Fronts';
	  	}
		 if (document.getElementById('backbutton2')) {
	  		document.getElementById('backbutton2').value = 'Show Fronts';
	  	}
		 	
	} else {
	  	if (document.getElementById('backbutton1')) {
	  		document.getElementById('backbutton1').value = 'Show Backs';
	  	 }
		 if (document.getElementById('backbutton2')) {
	  		document.getElementById('backbutton2').value = 'Show Backs';
		 }
	}
  }
  

/************************************************************************
   *
   * The code below here is used for the new swatch flipping on product pages.
   * It is somewhat integrated with the show backs above.
   *
   *
   ********************************************************************
   */


  

	function setImg(imgId,frontImage,backImage){
	  document.getElementById(imgId).src = showingBacks ? backImage : frontImage;
	}
	
	


  function PageSku() {
  	this.frontImage = null;
  	this.backImage = null;
  	this.skuUrl = null;
  }
  
  function PageProduct() {
  	this.pageSkus = null;
  	this.currentSku = null;
  }
  
  var pageProducts;
  var front = "front";
	var back = "back";
  var frontsImageList;
  var backsImageList;
  
  pageProducts = new Array(16);
  
  for(var i = 0; i < pageProducts.length; i++) {
  	pageProducts[i] = new PageProduct();
  }
  
  function setOpacity(img,opacity){

  	var imgElement = document.getElementById(img);
  
   	if (navigator.appName.indexOf("Netscape")!=-1
  		&&parseInt(navigator.appVersion)>=5)
    	imgElement.style.MozOpacity=opacity/100
	 	else if (navigator.appName.indexOf("Microsoft")!= -1 
	  	&&parseInt(navigator.appVersion)>=4)
	    imgElement.filters.alpha.opacity=opacity

  }
  
  function swapSkuThumbAndSetIt(i,si){
  
  	pageProducts[i].currentSku = pageProducts[i].pageSkus[si]; 
  
  	setImg(
		  'img'+i,
		  pageProducts[i].currentSku.frontImage,
		  pageProducts[i].currentSku.backImage);
	  
	  

	  document.getElementById('link-t-'+i).href = pageProducts[i].currentSku.skuUrl;
	  document.getElementById('link-i-'+i).href = pageProducts[i].currentSku.skuUrl;

  }
  
  function showPrintPage(skucode) {
  	document.getElementById('printFrame').src = '../printing/product_detail.jsp?sku=' + skucode;
  	showDiv('printPage', 'shopping_cart');
  }
  
  function checkValidForm() {
  	if (document.getElementById('Color0')) {
  		if (document.getElementById('Color0').value == '') {
  			return false;
  		}
  	}
  	if (document.getElementById('Size0')) {
  		if (document.getElementById('Size0').value == '') {
  			return false;
  		}
  	}
  	if (document.getElementById('Dim0')) {
  		if (document.getElementById('Dim0').value == '') {
  			return false;
  		}
  	}
  	return true;
  }
  
  function showTAFDiv(url, form, loadingImage) {
  	var otherdiv = new Array(1);
	otherdiv[0] = 'moreViews';
	if (checkValidForm()) {
  		document.getElementById('divTitle').innerHTML = 'Tell A Friend'; 
  		doTheAjax(url, 'hiddenDivContent', form, loadingImage); showDiv('hiddenDiv', form.name, otherdiv, loadingImage);
  	} else { 
  		document.getElementById('add_to_cart_submit').click(); 
  	}
  }
  
  function showWishDiv(url, form, loadingImage) {
  	var otherdiv = new Array(1);
	otherdiv[0] = 'moreViews';
	if (checkValidForm()) {
  		document.getElementById('divTitle').innerHTML = 'Add to Wish List';
  		doTheAjax(url, 'hiddenDivContent', form, loadingImage); showDiv('hiddenDiv', form.name, otherdiv, loadingImage);
  	} else { 
  		document.getElementById('add_to_cart_submit').click(); 
  	}
  }
  
  function executeOnLoad(skuID, wishurl, tafurl, skuCode) {
	  if (document.getElementById('wish_submit')) {
	  	document.getElementById('wish_submit').innerHTML = "<input id=\"add_to_wish_1\" type=\"button\" class=\"dark100\" name=\"add_to_wish_1\" value=\"Add to Wish List\" onClick=\"javascript:showWishDiv('" + wishurl + "', this.form, loadingImage);\">";
	  }
	  if (document.getElementById('email_submit')) {
	  	document.getElementById('email_submit').innerHTML = "<input type=\"button\" class=\"dark100\" name=\"email_friend\" value=\"Email a Friend\" onClick=\"javascript:showTAFDiv('" + tafurl + "', this.form, loadingImage);\">";
	  }
	  if (document.getElementById('print_submit_span')) {
	      var otherdiv = new Array(1);
		  otherdiv[0] = 'moreViews';
		  document.getElementById('print_submit_span').innerHTML = "<input id=\"print_page\" type=\"button\" class=\"dark100\" name=\"print_page\" value=\"Print This Page\" onClick=\"javascript:showPrintPage('" + skuCode + "');\">";
	  }
	  if (document.getElementById('Color0')) {
	    jsproduct0.dropDownUpdate('colorChange', document.forms.shopping_cart, 'detailpic0', -1, notAllSkus);
	  }
	  if (document.getElementById('Size0')) {
	    jsproduct0.dropDownUpdate('sizeChange', document.forms.shopping_cart, 'detailpic0', -1, notAllSkus);
	  }
	  if (document.getElementById('Dim0')) {
	    jsproduct0.dropDownUpdate('dimChange', document.forms.shopping_cart, 'detailpic0', -1, notAllSkus);
	  }
	  if (document.getElementById('giftcard') && skuID) {
			var options = document.getElementById('giftcard').options;
			for (var i = 0; i < options.length; i++) {
				if (options[i].value == skuID) {
					document.getElementById('giftcard').selectedIndex = i;
				}
			}
			jsproduct0.updateGiftCard(document.getElementById('giftcard').form, 'detailpic0');
	  }
  }
