var useBSNns = true;

function disableEnterKey(e)
{
	var key;
	if(window.event)
		key = window.event.keyCode; //IE
	else
		key = e.which; //firefox

	return (key != 13);
}

function disableBackspaceKey(e)
{	
	var key;
	if(window.event)
		key = window.event.keyCode; //IE
	else
		key = e.which; //firefox

	return (key != 8);
}

function EnterIsNewLine(e, intRow)
{	
	var key;
	if(window.event)
		key = window.event.keyCode; //IE
	else
		key = e.which; //firefox
	if (key == 13)
	{
		InputNew(intRow);
		document.getElementById("IngredientQuantity" + intRow).focus();
		return false
	}
	
	return (key != 8);
}

function InputNew(intRow)
{
	document.getElementById("IngredientQuantity" + intRow).className  = "IngredientQuantity";
	document.getElementById("IngredientUnit" + intRow).className  = "IngredientUnit";
	document.getElementById("IngredientDescription" + intRow).className  = "IngredientDescription";
	document.getElementById("IngredientNumber" + intRow).className  = "IngredientNumber";
	document.getElementById("IngredientUnitPrice" + intRow).className  = "IngredientUnitPrice";
	document.getElementById("IngredientCostPrice" + intRow).className  = "IngredientCostPrice";

	if (document.getElementById("IngredientQuantity" + intRow).value == "<aantal>")
		document.getElementById("IngredientQuantity" + intRow).value  = "";
	if (document.getElementById("IngredientUnit" + intRow).value == "<eenheid>")
		document.getElementById("IngredientUnit" + intRow).value  = "";
	if (document.getElementById("IngredientDescription" + intRow).value == "<ingredient>")
		document.getElementById("IngredientDescription" + intRow).value  = "";
	if (document.getElementById("IngredientNumber" + intRow).value == "<heoveelheid>")
		document.getElementById("IngredientNumber" + intRow).value  = "";
	if (document.getElementById("IngredientUnitPrice" + intRow).value == "<bedrag>")
		document.getElementById("IngredientUnitPrice" + intRow).value  = "";
	if (document.getElementById("IngredientCostPrice" + intRow).value == "<bedrag>")
		document.getElementById("IngredientCostPrice" + intRow).value  = "";

	CleanField(intRow);
	CalculateCostPrice(intRow);
	CheckUnitSelected(intRow);
	CheckDescription(intRow);
	document.getElementById("Row" + (intRow+1)).style.display = "block";
}

function CalculateUnit(intRow)
{
	var dblQuantity = document.getElementById("IngredientQuantity" + intRow).value;
	var dblWeightFactor = document.getElementById("IngredientQuantity" + intRow).value;
	var dblKiloMatches = document.getElementById("IngredientKiloMatches" + intRow).value;
	if ((dblQuantity != "") && !(dblKiloMatches != ""))
	{
		dblQuantity = parseFloat(dblQuantity.replace(',', '.'));
		document.getElementById("IngredientNumber" + intRow).value = dblQuantity;
		document.getElementById("IngredientNumber" + intRow).readOnly  = false;
		document.getElementById("IngredientNumber" + intRow).onkeydown = function() {};
	}
	
	if ((dblQuantity != "") && (dblKiloMatches != ""))
	{
		if (dblWeightFactor != "")
		{
			dblWeightFactor = parseFloat(dblWeightFactor.replace(',', '.'));
		}
		else
		{
			dblWeightFactor = 0;
		}		

		dblQuantity = parseFloat(dblQuantity.replace(',', '.'));
		dblKiloMatches = parseFloat(dblKiloMatches.replace(',', '.'));
		if (dblKiloMatches > 0)
		{
			document.getElementById("IngredientNumber" + intRow).value = (dblQuantity / dblKiloMatches).toFixed(3).toString().replace(".", ",");
			//document.getElementById("IngredientNumber" + intRow).value = ((dblQuantity / dblKiloMatches) * dblWeightFactor).toString().replace(".", ",");
			document.getElementById("IngredientNumber" + intRow).readOnly  = true;
			document.getElementById("IngredientNumber" + intRow).onkeydown = function(e) {return disableBackspaceKey(e);};
		}
		else
		{
			document.getElementById("IngredientNumber" + intRow).value = 0;
		}
	}
	CleanField(intRow)
	CalculateCostPrice(intRow);
}

function CalculateCostPrice(intRow)
{
	var dblQuantity = "0" + document.getElementById("IngredientQuantity" + intRow).value;
	var dblNumber = "0" + document.getElementById("IngredientNumber" + intRow).value;
	var dblUnitPrice = "0" + document.getElementById("IngredientUnitPrice" + intRow).value;

	dblQuantity = parseFloat(dblQuantity.replace(',', '.'));
	dblNumber = parseFloat(dblNumber.replace(',', '.'));
	dblUnitPrice = parseFloat(dblUnitPrice.replace(',', '.'));

	if (document.getElementById('IngredientUsePiecePrice'+ intRow).value == "True")
	{
		document.getElementById("IngredientCostPrice" + intRow).value = (dblQuantity * dblUnitPrice).toFixed(2).toString().replace(".", ",");
	}
	else
	{
		document.getElementById("IngredientCostPrice" + intRow).value = (dblNumber * dblUnitPrice).toFixed(2).toString().replace(".", ",");
	}
}
function CleanField(intRow)
{
	var NumberValue = document.getElementById("IngredientNumber" + intRow).value;
	var UnitPriceValue = document.getElementById("IngredientUnitPrice" + intRow).value;

	if (NumberValue == "")
	{
			document.getElementById("IngredientNumber" + intRow).value = 0;
	}
	else
	{
		NumberValue = parseFloat(NumberValue.replace(',', '.'));
		if(isNaN(NumberValue))
		{
			document.getElementById("IngredientNumber" + intRow).value = 0;
		}
		else
		{
			document.getElementById("IngredientNumber" + intRow).value = NumberValue.toFixed(4).toString().replace(".", ",");
		}
	}
	if (UnitPriceValue == "")
	{
		document.getElementById("IngredientUnitPrice" + intRow).value = new Number(0).toFixed(2).toString().replace(".", ",");
	}
	else
	{
		UnitPriceValue = parseFloat(UnitPriceValue.replace(',', '.'));
		if(isNaN(UnitPriceValue))
		{
			document.getElementById("IngredientUnitPrice" + intRow).value = new Number(0).toFixed(2).toString().replace(".", ",");
		}
		else
		{

			document.getElementById("IngredientUnitPrice" + intRow).value = new Number(UnitPriceValue).toFixed(2).toString().replace(".", ",");
		}
	}
}

function getDescrOptions(num) {
	return {
		script: function (input) { return "GetIngredient.asp?t=descr&s="+input; },
		varname:"s",
		delay: 0,
		maxentries: 5,
		shownoresults: false,
		callback: function (obj) {
			document.getElementById('IngredientSmedesId'+ num).value = obj.id;
			document.getElementById('IngredientUnitPrice'+ num).value = obj.price;	
			if (document.getElementById('IngredientUsePiecePrice'+ num).value == "True")
			{				
				document.getElementById('IngredientUnitPrice'+ num).value = obj.pieceprice;
			}
			else
			{
				document.getElementById('IngredientUnitPrice'+ num).value = obj.price;
			}			
			document.getElementById('IngredientWeightFactor'+ num).value = obj.weightfactor;
			
			if (document.getElementById('IngredientKiloMatches'+ num).value == "")
			{
				document.getElementById('IngredientKiloMatches'+ num).value = obj.kiloequals;
			}
			
			CalculateUnit(num);
			CleanField(num);
			CalculateCostPrice(num);
			CheckUnitSelected(num);
			CheckDescription(num);
		}
	};	
}

function getUnitOptions(num) {
	return {
		script: function (input) { return "GetIngredient.asp?t=unit&s="+input; },
		varname:"s",
		delay: 0,
		maxentries: 5,
		shownoresults: false,
		callback: function (obj) {
			if (obj.kiloequals != "")
			{
				document.getElementById('IngredientKiloMatches'+ num).value = obj.kiloequals;
			}
			if(obj.usepieceprice.toLowerCase() == "true")
			{
				document.getElementById('IngredientUnitPrice'+ num).className = "IngredientUnitPricePiece";
			}
			else
			{
				document.getElementById('IngredientUnitPrice'+ num).className = "IngredientUnitPrice";	
			}
			document.getElementById('IngredientUsePiecePrice'+ num).value = obj.usepieceprice;
			CalculateUnit(num);
			CleanField(num);
			CheckUnitSelected(num);
			CheckDescription(num);
		}
	};	
}

function CheckUnitSelected(num) {
	if (((document.getElementById('IngredientKiloMatches'+ num).value == "") || (document.getElementById('IngredientKiloMatches'+ num).value == 0)) &&
			(document.getElementById('IngredientUsePiecePrice'+ num).value.toLowerCase() != "true")) {
				document.getElementById('info' + num).setAttribute('title', 'Bij het invoeren dient u indien mogelijk een waarde te selecteren bv Gram of ml waarmee het systeem de juiste prijs berekend. Dit kunt u doen door met uw muis te selecteren of met de pijltjestoetsen te selecteren en met tab of enter naar het volgende veld. (Of bekijk de demo)'); 
				document.getElementById('IngredientUnit' + num).className = "IngredientUnitActive"; 	
	}
	else {
		document.getElementById('IngredientUnit' + num).className = "IngredientUnit"; 
	}	
}

function CheckDescription(num) {
	if (document.getElementById('IngredientDescription'+ num).value == "") {
		document.getElementById('info' + num).setAttribute('title', 'Bij het invoeren dient u indien mogelijk een waarde te selecteren bv Gram of ml waarmee het systeem de juiste prijs berekend. Dit kunt u doen door met uw muis te selecteren of met de pijltjestoetsen te selecteren en met tab of enter naar het volgende veld. (Of bekijk de demo)'); 
		document.getElementById('IngredientUnit' + num).className = "IngredientUnitActive"; 		
	}
	else {
		//document.getElementById('IngredientUnit' + num).className = "IngredientUnit"; 
	}
}
