// measurement conversion
var kgToLb = 2.204585537918871;
var gToLb = 0.002204585537918871;
var oneHundredGToLb = gToLb * 100;
var ozToLb = 0.062498897707231035;

var mlToL = 0.001;
var gallonToL = 4.54609;
var flOzToL = 0.029573529563;

// returns the unit price
function getUnitPrice(domItemCountPerPackageID, domSizeID, domUnitsOfMeasureID, domPriceID, rowCount)
{
	var domItemCountPerPackage = document.getElementById(domItemCountPerPackageID);
	var domSize = document.getElementById(domSizeID);
	var domUnitsOfMeasure = document.getElementById(domUnitsOfMeasureID);
	var domPrice = document.getElementById(domPriceID);

	if (domItemCountPerPackage != null && 
		domSize != null && 
		domUnitsOfMeasure != null &&
		domPrice != null)
	{

		var itemCountPerPackage = parseInt(domItemCountPerPackage.value);
		if (isNaN(itemCountPerPackage) || itemCountPerPackage == 0)
		{
			// if item count per package is null or 0 then user did not enter value in
			// so most likely this product is bought by the weight, eg. 0.89/lb
			itemCountPerPackage = 1;
		}

		var size = parseFloat(domSize.value);
		if (!isValidNumber(size))
		{
			alert(domSize.value + " is not a number, please enter a valid size for row " + rowCount);
			domSize.focus();
			return null;
		}

		var price = domPrice.value;
		if (isEmpty(price) || isNaN(parseFloat(price)) || parseFloat(price) == 0)
		{
			alert("Please enter a price for row " + rowCount);
//			domPrice.focus();
			return null;
		}

		var unitsOfMeasure = domUnitsOfMeasure.value;
		price = getValidPrice(price);

		var divisionCalculation = itemCountPerPackage * size;
		if (divisionCalculation == 0)
		{
			alert(itemCountPerPackage + " and " + size + " are invalid because 0 is not permitted.  Please enter valid values row " + rowCount);
			return null;		
		}
	
		var unitPrice;

		if (unitsOfMeasure == "lb")
		{
			unitPrice = roundToNearestCent(price / (divisionCalculation)) + "/lb";
		}
		else if (unitsOfMeasure == "each")
		{
			unitPrice = roundToNearestCent(price / (divisionCalculation)) + "/each";
		}
		else if (unitsOfMeasure == "L")
		{
			unitPrice = roundToNearestCent(price / (divisionCalculation)) + "/L";
		}
		else if (unitsOfMeasure == "g")
		{
			unitPrice = roundToNearestCent(price / (divisionCalculation * gToLb)) + "/lb";
		}
		else if (unitsOfMeasure == "100g")
		{
			unitPrice = roundToNearestCent(price / (divisionCalculation * oneHundredGToLb)) + "/lb";
		}
		else if (unitsOfMeasure == "kg")
		{
			unitPrice = roundToNearestCent(price / (divisionCalculation * kgToLb)) + "/lb";
		}
		else if (unitsOfMeasure == "oz")
		{
			unitPrice = roundToNearestCent(price / (divisionCalculation * ozToLb)) + "/lb";
		}
		else if (unitsOfMeasure == "ml")
		{
			unitPrice = roundToNearestCent(price / (divisionCalculation * mlToL)) + "/L";
		}
		else if (unitsOfMeasure == "gallon")
		{
			unitPrice = roundToNearestCent(price / (divisionCalculation * gallonToL)) + "/L";
		}
		else if (unitsOfMeasure == "flOz")
		{
			unitPrice = roundToNearestCent(price / (divisionCalculation * flOzToL)) + "/L";
		}

		return unitPrice;
	}
}

// 3 cases to handle
// normal case $0.99
// 2/$3.99
// $0.99/lb
function getValidPrice(price)
{
	// replace \ with /	
	price = price.replace(/\\/g, "/");
	// remove starting $
	price = price.replace(/^\$/, "");

	var validPrice;
	var slashIndex = price.indexOf("/");

	// normal case: eg. 0.99
	if (slashIndex == -1)
	{
		validPrice = parseFloat(price);
	}
	// eg. 2/$3.99
//	else if (price.search(/^\d\$[\d\.]+/i) != -1 && slashIndex != -1)
	else if (price.search(/^\d+\/\$[\d\.]+/i) != -1)
	{
		var priceArray = price.split("/");
		var quantityPerPrice = priceArray[0];
		var tempPrice = priceArray[1].replace(/\$/g, "");
		validPrice = roundToNearestCent(tempPrice/quantityPerPrice);
	}
	// eg. $0.99/lb
	else if (slashIndex != -1)
	{
		var priceArray = price.split("/");
		validPrice = priceArray[0];
	}

	return validPrice;
}

function roundToNearestCent(value)
{
	return Math.round(value * 100)/100;
}
