KEROSENE_BASE_PRICE = 0.719;
GAS_OIL_BASE_PRICE = 0.749;

KERO_QTY_BAND1 = 196;
KERO_QTY_BAND2 = 264;
KERO_QTY_BAND3 = 500;
KERO_QTY_BAND4 = 500;
KERO_QTY_BAND5 = 500;

GAS_OIL_QTY_BAND1 = 188;
GAS_OIL_QTY_BAND2 = 254;
GAS_OIL_QTY_BAND3 = 500;
GAS_OIL_QTY_BAND4 = 500;
GAS_OIL_QTY_BAND5 = 500;


RESULT_PRECISION = 5;          //Tne number of digits after the decimal point.

var KEROSENE_PRICE;
var GAS_OIL_PRICE;
var KEROSENE_TOTAL_COST;
var GAS_OIL_TOTAL_COST;


function CalculateKerosene(quantity)
{
	if (quantity>=KERO_QTY_BAND1 && quantity<KERO_QTY_BAND2)
	{
		KEROSENE_PRICE = KEROSENE_BASE_PRICE + 0.05;
	}
	else if (quantity>=KERO_QTY_BAND2 && quantity<KERO_QTY_BAND3)
	{
		KEROSENE_PRICE = KEROSENE_BASE_PRICE + 0.04;
	}
	else if (quantity>=KERO_QTY_BAND3 && quantity<KERO_QTY_BAND4)
	{
		KEROSENE_PRICE = KEROSENE_BASE_PRICE + 0.00;
	}
	else if (quantity>=KERO_QTY_BAND4 && quantity<KERO_QTY_BAND5)
	{
		KEROSENE_PRICE = KEROSENE_BASE_PRICE;
	}
	else if (quantity>=KERO_QTY_BAND5)
	{
		KEROSENE_PRICE = KEROSENE_BASE_PRICE;
	}
	
	KEROSENE_TOTAL_COST = Round(KEROSENE_PRICE*quantity);
}


function CalculateGasOil(quantity)
{
	if (quantity>=GAS_OIL_QTY_BAND1 && quantity<GAS_OIL_QTY_BAND2)
	{
		GAS_OIL_PRICE = GAS_OIL_BASE_PRICE + 0.05;
	}
	else if (quantity>=GAS_OIL_QTY_BAND2 && quantity<GAS_OIL_QTY_BAND3)
	{
		GAS_OIL_PRICE = GAS_OIL_BASE_PRICE + 0.04;
	}
	else if (quantity>=GAS_OIL_QTY_BAND3 && quantity<GAS_OIL_QTY_BAND4)
	{
		GAS_OIL_PRICE = GAS_OIL_BASE_PRICE + 0.00;
	}
	else if (quantity>=GAS_OIL_QTY_BAND4 && quantity<GAS_OIL_QTY_BAND5)
	{
		GAS_OIL_PRICE = GAS_OIL_BASE_PRICE;
	}
	else if (quantity>=GAS_OIL_QTY_BAND5)
	{
		GAS_OIL_PRICE = GAS_OIL_BASE_PRICE;
	}
	
	GAS_OIL_TOTAL_COST = Round(GAS_OIL_PRICE*quantity);
}


/*******************DON'T CHANGE ANYTHING BELOW******************************/

var RoundValue = Math.pow(10, RESULT_PRECISION);

function Round(value)
{
	return Math.round(value*RoundValue)/RoundValue;
}

function CalculateTotalCost(KeroseneQty, MGOQty, DervQty, PetrolQty)
{
	return Math.round((KeroseneQty * KEROSENE_PRICE + 
		MGOQty * GAS_OIL_PRICE + 
		DervQty * DERV_PRICE + 
		PetrolQty * PETROL_PRICE)*RoundValue)/RoundValue;
}

function GetKeroseneQty()
{
	var keroseneQty = document.getElementById("KeroseneQty").value;
	return (!keroseneQty.NaN && keroseneQty > 0) ? 
		keroseneQty : 0;
}

function GetMgoQty()
{
	var mgoQty = document.getElementById("MgoQty").value;
	return (!mgoQty.NaN && mgoQty > 0) ? mgoQty : 0;
}

function GetDervQty()
{
	var dervQty = document.getElementById("DervQty").value;
	return (!dervQty.NaN && dervQty > 0) ? dervQty : 0;
}

function GetPetrolQty()
{
	var qty = document.getElementById("PetrolQty").value;
	return (!qty.NaN && qty > 0) ? qty : 0;
}

function KeroseneQty_OnChange()
{
	var qty = GetKeroseneQty();
	if (qty)
	{
		if (qty<KERO_QTY_BAND1)
		{
			document.getElementById("KeroseneCostLabel").innerHTML = "";
			document.getElementById("KeroseneCostPanel").className = "hidden";
			window.alert("Minimun order quantity: " + KERO_QTY_BAND1);
		}
		else
		{
			CalculateKerosene(qty);
			document.getElementById("KeroseneCostLabel").innerHTML = "&euro;" + KEROSENE_TOTAL_COST;

			document.getElementById("KerosenePriceLabel").innerHTML = KEROSENE_PRICE;

			document.getElementById("KeroseneCostPanel").className = "";
		}
	}
	else
	{
		document.getElementById("KeroseneCostLabel").innerHTML = "";
		document.getElementById("KeroseneCostPanel").className = "hidden";
	}

	document.getElementById("KeroseneCostHidden").value = document.getElementById("KeroseneCostLabel").innerHTML;
}

function MgoQty_OnChange()
{
	var qty = GetMgoQty();
	if (qty)
	{
		if (qty<GAS_OIL_QTY_BAND1)
		{
			document.getElementById("MgoCostLabel").innerHTML = "";
			document.getElementById("MgoCostPanel").className = "hidden";
			window.alert("Minimun order quantity: " + GAS_OIL_QTY_BAND1);
		}
		else
		{
			CalculateGasOil(qty);
			document.getElementById("MgoCostLabel").innerHTML = "&euro;" + GAS_OIL_TOTAL_COST;

			document.getElementById("MgoPriceLabel").innerHTML = GAS_OIL_PRICE;

			document.getElementById("MgoCostPanel").className = "";
		}
	}
	else
	{
		document.getElementById("MgoCostLabel").innerHTML = "";
		document.getElementById("MgoCostPanel").className = "hidden";
	}

	document.getElementById("MgoCostHidden").value = document.getElementById("MgoCostLabel").innerHTML;
}

function DervQty_OnChange()
{
	var qty = GetDervQty();
	if (qty)
	{
		document.getElementById("DervCostPanel").className = "";
	}
	else
	{
		document.getElementById("DervCostPanel").className = "hidden";
	}
}

function PetrolQty_OnChange()
{
	var qty = GetPetrolQty();
	if (qty)
	{
		document.getElementById("PetrolCostPanel").className = "";
	}
	else
	{
		document.getElementById("PetrolCostPanel").className = "hidden";
	}
}