function ShowDetail(ctrl)
{
	alert(ctrl.innerHTML);
}
function Print()
{
	var w = window.open("",null,"height=700,width=800,status=no,toolbar=no,menubar=no,location=no");
	w.document.open();
	w.document.write("<html><head><LINK href='Tarrantit.css' type='text/css' rel='stylesheet'></head><body onload='javascript:window.print();window.close()'>"+ document.getElementById("spnPrint").innerHTML +"</body></html>");
	w.document.close();
	w.focus();
}
function window_onload()
{
	var boxes = document.getElementsByTagName("input");
	for (var i=0; i< boxes.length; i++)
	{
		if (boxes[i].type=="text")
		{
			if (document.all)
				boxes[i].onkeypress = Check_IE;
			else
				boxes[i].onkeypress = Check_NS;	
		}
	}
	CalculateFluidWeight();
}
function Check_IE()
{
	var v = event.keyCode;
	if (v==46 && event.srcElement.value.indexOf(".") != -1)
 		return false;
	if (v >= 46 && v < 58)
		return true;
	return false;
}
function Check_NS(e)
{
	var v = e.which;
	if (v==46 && activeElement.value.indexOf(".") != -1)
 		e.preventDefault();
	if ((v > 47 && v < 58) || v==0 || v==8)
		return true;
	e.preventDefault();
}
function CalculateFluidWeight()
{
    var disp = "";
    var fluid = document.getElementById("ddFluids").value;
    if (fluid == "WhaleOil")
        disp = "Stop Whaling NOW!";
    else
    {
        var litres = parseFloat(document.getElementById("txtLitres").value);
        var m3 = litres / 1000;
        var weight = 0;
        if (fluid.indexOf("-") != -1)
        {            
            var from = parseFloat(fluid.substring(0, fluid.indexOf("-"))) * m3;
            var to = parseFloat(fluid.substring(fluid.indexOf("-") + 1)) * m3;
            disp = from.toFixed(2) +" to "+ to.toFixed(2);
        }
        else
        {
            weight = m3 * parseFloat(fluid);
            disp = weight.toFixed(2);
        }
    }
    document.getElementById("lblFluidWeight").innerHTML = disp;
}
var tankHeight = 2000;
var tankDiameter = 2000;
var depth = 1000;
var scaledDepth = 0;
var scaledHeight = 0;
var circ = 0;
var endArea = Math.PI * (Math.pow((tankDiameter / 2), 2));
function DrawTank()
{
    tankHeight = parseInt(document.getElementById("txtHeight").value);
    depth = parseInt(document.getElementById("txtDepth").value);
    if (depth > tankHeight)
    {
        alert("Fill depth cannot be higher than tank height");
        document.getElementById("txtDepth").value = tankHeight;
        depth = tankHeight;
    }
    tankDiameter = parseInt(document.getElementById("txtWidth").value);
    circ = Math.PI * tankDiameter;
    document.getElementById("spnVerTankCirc").innerHTML = circ.toFixed(0);
    endArea = Math.PI * (Math.pow((tankDiameter / 2), 2));
    var totalArea = ((endArea * 2) + (tankHeight * circ)) * .000001;
    document.getElementById("spnVertTankArea").innerHTML = totalArea.toFixed(2);
    var totalLitres = (endArea * tankHeight)  * .000001;
    document.getElementById("spnVerTankTotalVol").innerHTML = totalLitres.toFixed(2);
    scaledHeight = 100 * tankHeight / tankDiameter;
    document.getElementById("tblTank").style.height = scaledHeight +"px";
    ChangeLevel();
}
function ChangeLevel()
{        
    depth = parseInt(document.getElementById("txtDepth").value);
    scaledHeight = 100 * tankHeight / tankDiameter;
    scaledDepth = scaledHeight * (depth / tankHeight);
    document.getElementById("tblFill").style.height = scaledDepth +"px";
    var filledLitres = endArea * depth  * .000001;
    document.getElementById("spnVertTankVolume").innerHTML = filledLitres.toFixed(2);
    document.getElementById("spnVerTankDepth").innerHTML = document.getElementById("txtDepth").value;
    document.getElementById("txtLitres").value = filledLitres.toFixed(2);
    CalculateFluidWeight();
    var percentFull = depth / (tankHeight /100);
    document.getElementById("spnPercentFull").innerHTML = percentFull.toFixed(1) +" %";
}
var depthTimer = null;
function Level(dir)
{
    tankHeight = parseInt(document.getElementById("txtHeight").value);
    if (dir == "fill")
        depthTimer = setInterval("ChangeDepth('fill')", 1);
    else
        depthTimer = setInterval("ChangeDepth('empty')", 1);
}
function ChangeDepth(dir)
{      
    var inc = (dir == 'fill') ? 1 : -1;
    var newDepth = parseInt(document.getElementById("txtDepth").value) + inc;
    if (newDepth <= tankHeight && newDepth >= 0)
    {
        document.getElementById("txtDepth").value = newDepth;
        ChangeLevel();
    }
    else
        clearInterval(depthTimer);
}
function StopLevel()
{
    clearInterval(depthTimer);
}