//	Javascript for the CachedProductOnlyDisplay.jsp
// This javascript function is used by the 'Add to Wish List' button to set appropriate values before the form is submitted
var busy = false;
var currentColor="";
function Add2WishList(form)
{
	if (checkColorField() && checkSizeField())
	{
		setHiddenFields();
		if (!busy) {		
			var qtyS=form.quantityAdd.value;			    
			if((qtyS =='') ||  (qtyS==null) || !(isInteger(qtyS, false))) {
				 document.getElementById("quantityregular").style.color="#ff0000";
				 document.getElementById("qtyreg").style.border="1px solid #ff0000";
				form.quantityAdd.select();	
				return false;
			}
			else{			
				busy = true;
				form.action="InterestItemAdd";
				form.URL.value="InterestItemDisplay";
				form.quantity.value=qtyS;
				form.submit();
			}
		}
	}
}
// This javascript function is used by the 'Add to Cart' button to set appropriate values before the orderItemAddForm is submitted
var busy = false;
function Add2Cart(form)
{
	if (checkColorField() && checkSizeField())
	{
		if(checkprice())
		{
			setHiddenFields();
			if(!busy)
			{			
				var qtyS=form.quantityAdd.value;	    
				if((qtyS =='') ||  (qtyS==null) || !(isInteger(qtyS, false))) {
					 document.getElementById("quantityregular").style.color="#ff0000";
				     document.getElementById("qtyreg").style.border="1px solid #ff0000";
					form.quantityAdd.select();	
					return false;
				}else{
					form.quantity.value=form.quantityAdd.value;
					 busy=true;
					form.action="B2CInventoryControllerCmd";
					form.URL.value="OrderCalculate?updatePrices=1&calculationUsageId=-1&orderItemId*=&quantity*=&URL=OrderItemDisplay";
					form.submit();	
			 	}
			}	
			 busy=true;
		}
		else
		{
			alert("This item cannot be purchased at this time \n Please select a different item");
			document.OrderItemAddForm.quantityAdd.focus();
			return false;
		}		
	}
}
//checks if price is set to 0; if price is set to zero prevent item from being added to cart
function checkprice()
{
	var price;
	colorField = document.OrderItemAddForm.Color;
	color = colorField.options[colorField.selectedIndex].value;
	color = color.split("|")[1];
	if (sizeAndDimExist())
	{
		sdString = document.OrderItemAddForm.SizeAndDimension.options[document.OrderItemAddForm.SizeAndDimension.selectedIndex].value;
		price  =  sdString.substring(sdString.indexOf("|")+1,sdString.lastIndexOf("|"));
		if(price!=null)
		{
			splitstring	=price.split(";");
			for(var i=0;i<splitstring.length;i++)
			{
				splitprice = splitstring[i].split(",");
				if((splitprice[0]==color)&&(splitprice[1]>0))
				return true;			
			}
			return false;
		}
		else
		return false;
	}else{
		sdString = document.OrderItemAddForm.Size.options[document.OrderItemAddForm.Size.selectedIndex].value;
		price  =  sdString.substring(sdString.indexOf("|")+1,sdString.lastIndexOf("|"));
		if(price!=null)
		{
			splitstring	=price.split(";");
			for(var i=0;i<splitstring.length;i++)
			{
			splitprice = splitstring[i].split(",");
			if((splitprice[0]==color)&&(splitprice[1]>0))
			return true;		
			}
			return false;
		}
		else
			return false;
	}
}

function checkColorField()
{
	return checkSelectBox(document.OrderItemAddForm.Color, "Color", "");
}

function checkSizeField()
{
	if (sizeAndDimExist())
	{
	return checkSelectBox(document.OrderItemAddForm.SizeAndDimension, "Size", "");
	}
	else
	{
	return checkSelectBox(document.OrderItemAddForm.Size, "Size", "");
	}
}

function where2Buy()
{
	if (checkSizeField())
	{
	document.WhereToBuyForm.product_size.value = getProductSize();
	document.WhereToBuyForm.submit();
	}
}

function buyOnline()
{
	if (checkSizeField())
	{
		document.BuyOnlineForm.productSize.value = getProductSize();
		document.BuyOnlineForm.submit();
	}
}
function sizeAndDimExist()
{
	var exists = document.OrderItemAddForm.sizeAndDimExists.value;
	if (exists == "yes")
		return true;
	else
		return false;
}
// gets the size whether it has dimension or not
function getProductSize()
{
	if (sizeAndDimExist())
	{
	sdString = document.OrderItemAddForm.SizeAndDimension.options[document.OrderItemAddForm.SizeAndDimension.selectedIndex].value;
	return sdString.substring(0, sdString.indexOf("|"));
	}
	else
	{
	sizeField = document.OrderItemAddForm.Size.options[document.OrderItemAddForm.Size.selectedIndex].value;
	start = sizeField.lastIndexOf("|") + 1;
	return sizeField.substring(start);
	}
}
// sets the hidden attrValue fields needed by WC by parsing the size/dimension and color values from drop-downs
function setHiddenFields()
{
	var color;
	var size;
	var dimension;
	if (sizeAndDimExist())
	{
	sdString = document.OrderItemAddForm.SizeAndDimension.options[document.OrderItemAddForm.SizeAndDimension.selectedIndex].value;
	size = getProductSize();
	dimension = sdString.substring(sdString.lastIndexOf("|")+1);
	colorField = document.OrderItemAddForm.Color;
	color = colorField.options[colorField.selectedIndex].value;
	color=color.substring(0,color.indexOf("|"));
	}
	else
	{
	colorField = document.OrderItemAddForm.Color;
	color = colorField.options[colorField.selectedIndex].value;
	color=color.substring(0,color.indexOf("|"));
	sizeField = document.OrderItemAddForm.Size;
	size = getProductSize();
	}
	var attrValueArray = document.OrderItemAddForm.elements["attrValue"];
	for(i = 0; i < attrValueArray.length; i++)
	{
	if (i == 0)
	attrValueArray[i].value = color;
	else if (i == 1)
	attrValueArray[i].value = size;
	else if (i == 2 && sizeAndDimExist())
	attrValueArray[i].value = dimension;
	}	
}
function changeSet(partNumber, color){
	document.colorset.src = "//carharttimages.carhartt.com//is/image/Carhartt/"+partNumber+color+"?$LG$";
	document.getElementById('color_icon_'+color).className="wl-swatch wl-current";
	setColor(color);
}
function changeColorIcon(color){
	var partNumber = document.getElementById('hiddenInputProductStyle').value;
	document.colorset.src = "//carharttimages.carhartt.com//is/image/Carhartt/"+partNumber+color+"?$LG$";
	for (i=0; i < 8; i++)
	{
		var thisImg = document.getElementById('color_icon_'+i);
		if (thisImg != null)
		{
			if (thisImg.name == color)
			{
			thisImg.className="wl-swatch wl-current";
			}
			else
			{
			thisImg.className="wl-swatch";
			}		
		}
	}
}

function enlarge(partNumber)
	{
		var largeImage =  "//carharttimages.carhartt.com//is/image/Carhartt/"+partNumber+getColor()+"?$BLG$";

		window.open(largeImage,"","toolbar=no,location=no,directories=no,"+
		           "status=no,menubar=no,scrollbars=1,"+
			       "resizable=1,width=500,height=500,top=40,left=60")
	}	
	
function setColor(color){
	currentColor = color;
}
function getColor(){
	return currentColor;
}
domainAddr = "http://carharttimages.carhartt.com";
function loadZoom(partNumber){
	var imageSet = 'Carhartt/'+ partNumber+"_RS";
	var args ='serverUrl=' + S7Config.isRoot; 
	args += '&config=Carhartt/test_config'; //  uncomment after company is created and published in IPS
	args += '&contentRoot='+ domainAddr +'/StaticContent';
	args += '&image=' + imageSet;		

	var query = domainAddr + "/is-viewers/flash/genericzoom.swf?" + args;	
		window.open(query,"","toolbar=no,location=no,directories=no,"+
		           "status=no,menubar=no,scrollbars=yes,"+
			       "resizable=yes,width=700,height=650,top=20,left=20")
}   
function isInteger(s){ 
	var i;
    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true;
}
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function changeSet2(partNumber, color, icon){
	document.colorset.src = "//carharttimages.carhartt.com//is/image/Carhartt/"+partNumber+color+"?$LG$";
	for (i=0; i < 8; i++)
	{
		var thisImg = document.getElementById('color_icon_'+i);
		if (thisImg != null)
		{
			thisImg.className="wl-swatch";
		}
	}
	document.getElementById(icon).className="wl-swatch wl-current";
	changeSize(document.OrderItemAddForm, document.getElementById('attrValueSize'));
	setColor(color);
}

function showSelectedColor(partNumber, color, icon, colorFullName,form) {
	document.colorset.src = "http://carharttimages.carhartt.com//is/image/Carhartt/"+partNumber+color+"?$LG$";
	var selecturl="http://carharttimages.carhartt.com//is/image/Carhartt/" + partNumber+color;
	switchit(selecturl, form);
	document.getElementById('colorname').innerHTML=colorFullName;
	for (i=0; i < 8; i++)
	{
		var thisImg = document.getElementById('color_icon_'+i);
		if (thisImg != null)
		{
			thisImg.className="wl-swatch";
		}
	}
	document.getElementById(icon).className="wl-swatch wl-current";
	var colorSelections = document.getElementById('attrValueColor').options;
	for (i=0; i < colorSelections.length; i++)
	{
		if (colorSelections[i].value.indexOf(color) >= 0)
		{
			colorSelections[i].selected = true;
		}
	}
	var test=form.tallsizecolors.value;
	if(test == "false"){
	changeSize(document.OrderItemAddForm, document.getElementById('attrValueSize'));
	}else{
	changeSize1(document.OrderItemAddForm, document.getElementById('attrValueSize'));
	}
	setColor(color);
		document.getElementById('div_sizeRadio').checked = false;
		document.getElementById('div_sizeRadio2').checked = false;
		for (var i=0; i<document.OrderItemAddForm.sizeRadio.length; i++) {
			document.OrderItemAddForm.sizeRadio[i].checked = false;	
		}
	if(navigator.appName=="Netscape"){	
	}
}
// checks a drop-down box to make sure a value has been selected
function checkSelectBox(theSelectBox, displayName, invalidValue)
{
	var selectedValue = theSelectBox.options[theSelectBox.selectedIndex].value;
	if (selectedValue == invalidValue || theSelectBox.selectedIndex == 0)
	{
	document.getElementById("attrValueSize").style.border="1px solid #ff0000";
	theSelectBox.focus();
	return false;
	}
	else
	{return true;
	}
}
