// 
//	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 a color and size have been selected
	if (checkColorField() && checkSizeField())
	{
		setHiddenFields();
	
		if (!busy) {		
			var qtyS=form.quantityAdd.value;			    
			if((qtyS =='') ||  (qtyS==null) || !(isInteger(qtyS, false))) {
				alert("The value in the Quantity field is invalid. Ensure the value is a positive integer and try again");
				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 form is submitted
var busy = false;
function Add2Cart(form)
{
   
	// if a color and size have been selected
	if (checkColorField() && checkSizeField())
	{
		if(checkprice())
		{
			setHiddenFields();
	
			if(!busy) 
			{			
				var qtyS=form.quantityAdd.value;			    
				if((qtyS =='') ||  (qtyS==null) || !(isInteger(qtyS, false))) {
					alert("The value in the Quantity field is invalid. Ensure the value is a positive integer and try again");
					form.quantityAdd.select();	
					return false;
				}else{
					form.quantity.value=form.quantityAdd.value;
					 busy=true;
//					form.action="OrderItemAdd";
					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];
	
	//color=color.substring(color.indexOf("|")+1);
	if (sizeAndDimExist())
	{
		sdString = document.OrderItemAddForm.SizeAndDimension.options[document.OrderItemAddForm.SizeAndDimension.selectedIndex].value;
		// parse to get the price"size|price|dim
		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(",");
				//alert("splitstring : "+splitstring[i]);
				//alert("splitprice 0:"+splitprice[0]+" color: "+color+" price: "+splitprice[1]);
				if((splitprice[0]==color)&&(splitprice[1]>0))
					return true;			
			}
			return false;
		}
		else
			return false;
	}else{
		//parse to get the price"size|price
		sdString = document.OrderItemAddForm.Size.options[document.OrderItemAddForm.Size.selectedIndex].value;
		// parse to get the price"size|price|dim
		//price  =  sdString.substring(sdString.lastIndexOf("|")+1);
		price  =  sdString.substring(sdString.indexOf("|")+1,sdString.lastIndexOf("|"));
		//alert("price :"+ price);
		if(price!=null)
		{
			splitstring	=price.split(";");
			for(var i=0;i<splitstring.length;i++)
			{
				splitprice = splitstring[i].split(",");
				//alert("splitstring : "+splitstring[i]);
				//alert("splitprice 0:"+splitprice[0]+" color: "+color+" price: "+splitprice[1]);
				if((splitprice[0]==color)&&(splitprice[1]>0))
					return true;		
			}
			return false;
		}
		else
			return false;
	}
}

// makes sure a value has been selected for color; returns true if size is selected, false otherwise
function checkColorField()
{
	return checkSelectBox(document.OrderItemAddForm.Color, "Color", "");
}

// makes sure that a size has been selected;  returns true if size is selected, false otherwise
function checkSizeField()
{
	if (sizeAndDimExist())
	{
		return checkSelectBox(document.OrderItemAddForm.SizeAndDimension, "Size", "");
	}
	else
	{
		return checkSelectBox(document.OrderItemAddForm.Size, "Size", "");
	}
}

// function sets the size value of the WhereToBuy form and submits
function where2Buy()
{
	if (checkSizeField())
	{
		document.WhereToBuyForm.product_size.value = getProductSize();
		document.WhereToBuyForm.submit();
	}
}

// function sets the size value of the WhereToBuy form and submits
function buyOnline()
{
	if (checkSizeField())
	{
		document.BuyOnlineForm.productSize.value = getProductSize();
		document.BuyOnlineForm.submit();
	}
}


// checks to see if size and dimension exist for the product
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;
		// parse the size and dimension
		size = getProductSize();
		dimension = sdString.substring(sdString.lastIndexOf("|")+1);
		//get the color	
		colorField = document.OrderItemAddForm.Color;
		color = colorField.options[colorField.selectedIndex].value;
		color=color.substring(0,color.indexOf("|"));
	}
	else
	{
		//get the color	
		colorField = document.OrderItemAddForm.Color;
		color = colorField.options[colorField.selectedIndex].value;
		color=color.substring(0,color.indexOf("|"));
		
		// get the size
		sizeField = document.OrderItemAddForm.Size;
		size = getProductSize();
		
	}
		
	// set the hidden fields; they have the same name, attrValue
	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="swatch_selected";
	setColor(color);
}

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="swatch";
		}
	}
	document.getElementById(icon).className="swatch_selected";
	
	var colorSelections = document.getElementById('attrValueColor').options;
	for (i=0; i < colorSelections.length; i++)
	{
		if (colorSelections[i].value.indexOf(color) >= 0)
		{
			colorSelections[i].selected = true;
		}
	}
	
	changeSize(document.OrderItemAddForm, document.getElementById('attrValueSize'));
	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="swatch_selected";
			}
			else
			{
				thisImg.className="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='+ imageSet;			//  delete this entry after next line is uncommented
	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);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    // All characters are numbers.
    return true;
}
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}





