function changeProductImage(fileName)
{
	var imgElement = document.getElementById("productImage");
	imgElement.src = "images/products/"+fileName;
}

function openProductImages(productId)
{
	var pageLink = "product_images.php?product_id="+productId;
	window.open(pageLink, "ProductImages", "width=400,height=500");
}

function calculateAttributePrice(attributeIds, productPrice, itemNumber)
{
	if(attributeIds != undefined && attributeIds != "" && productPrice != undefined && productPrice != "")
	{
		var attributeIdArray = attributeIds.split(",");
		
		var productPriceValue = document.getElementById("productPriceValue");
		var attributeId = "productAttribute_";
		var price = productPrice;
		var itemNumberFinal = itemNumber;

		for(i=0; i<attributeIdArray.length; i++)
		{
			selectElement = document.getElementById(attributeId+String(attributeIdArray[i]));
			
			if(selectElement.options[selectElement.selectedIndex].value != "")
			{
				var valPrice = selectElement.options[selectElement.selectedIndex].value;
				var parts = valPrice.split("|");

				var thisVal = parts[0];
				var valNumber = parts[1];
				
				if(valNumber)
				{
					itemNumberFinal = itemNumberFinal + "-" + valNumber;
				}

				//price = 	selectElement.options[selectElement.selectedIndex].value;
				price = roundDecimals(Number(price) + Number(thisVal), 2);
				price = roundProperly(price);
			}
		}
		
		clearInnerHTML(productPriceValue);
		productPriceValue.appendChild(document.createTextNode("$"+price));

		clearInnerHTML(productNumberValue);
		productNumberValue.appendChild(document.createTextNode(itemNumberFinal));
	}
	
	return true;
}

function calculateAttributeNumber(attributeIds, productPrice)
{
	if(attributeIds != undefined && attributeIds != "" && productPrice != undefined && productPrice != "")
	{
		var attributeIdArray = attributeIds.split(",");
		
		var productPriceValue = document.getElementById("productPriceValue");
		var attributeId = "productAttribute_";
		var price = productPrice;

		for(i=0; i<attributeIdArray.length; i++)
		{
			selectElement = document.getElementById(attributeId+String(attributeIdArray[i]));
			if(selectElement.options[selectElement.selectedIndex].value != "")
				//price = 	selectElement.options[selectElement.selectedIndex].value;
				price = roundDecimals(Number(price) + Number(selectElement.options[selectElement.selectedIndex].value), 2);
				price = roundProperly(price);
		}
		
		clearInnerHTML(productPriceValue);
		productPriceValue.appendChild(document.createTextNode("$"+price));
	}
	
	return true;
}

function expandCollapseSubCategories(tplPath)
{
	var subCategories = document.getElementById("subCategories");
	var scTitle = document.getElementById("scTitle");
	var scImage = document.getElementById("scImage");
	
	if(subCategories.style.display == "block")
	{
		subCategories.style.display = "none";
		scTitle.title = "Expand subcategories";
		scImage.alt   = "Expand subcategories";
		scImage.src   = tplPath+"img/plus.gif";
	}
	else
	{
		subCategories.style.display = "block";
		scTitle.title = "Collapse subcategories";
		scImage.alt   = "Collapse subcategories";
		scImage.src   = tplPath+"img/minus.gif";
	}
}

function addProduct(container, productId, attributeIds)
{
	if(container.toLowerCase() == "wishlist")
		locationHref = "addtowish.php?product_id="+productId;
	else
		locationHref = "addtocart.php?product_id="+productId;
	
	if(attributeIds != undefined && attributeIds != "")
	{
		var attributeId = "productAttribute_";
		var attributeIdArray = attributeIds.split(",");
		var valueIds = "";
		
		for(i=0; i<attributeIdArray.length; i++)
		{
			selectElement = document.getElementById(attributeId+String(attributeIdArray[i]));
			valueIds += document.getElementById("attributeValueId_"+String(attributeIdArray[i])+"_"+selectElement.selectedIndex).value + ",";
		}
		
		valueIds = valueIds.substr(0, valueIds.length - 1);
		locationHref += "&attributes="+attributeIds+"&values="+valueIds;
	}
	
	document.location = locationHref;
}

function roundProperly(amt)
{
	split_amt = new String(amt).split('.');

	if(split_amt.length == 1)
	{
		fixed_amt = amt + '.00';
	}
	else if(split_amt.length == 2)
	{
		if(split_amt[1].length == 1)
		{
			fixed_amt = split_amt[0] + '.' + split_amt[1] + '0';
		}
		else if(split_amt[1].length == 2)
		{
			fixed_amt = amt;
		}
		else if(split_amt[1].length > 2)
		{
			fixed_amt = split_amt[0] + '.' + split_amt[1].substr(0,2);
		}
	}

	return  fixed_amt;
}
