﻿function SetUp()
{
    document.getElementById("_txtFrom").value = Math.floor(Math.random() * 800) + 100;
    ConvertVolumes();
}
var runs = 0;
function ConvertVolumes()
{
    var fromUnit = document.getElementById("ddFrom").value;
    var from = parseFloat(document.getElementById("_txtFrom").value);
    if (from > 100000)
    {
        document.getElementById("_txtFrom").value = 100000;
        from = 100000;
    }        
    document.getElementById("spnFromTo").innerHTML = from +" "+ document.getElementById("ddFrom").options[document.getElementById("ddFrom").selectedIndex].text +" =";
    var inch3 = 0;
    var feet3 = 0;
    var yard3 = 0;
    var mm3 = 0; 
    var cm3 = 0;
    var m3 = 0;
    var usGallons = 0;
    var impGallons = 0;
    var litres = 0;
    switch (fromUnit)
    {
        case "inch":
            inch3 = from;
            feet3 = from / 1728;
            yard3 = from / 46656;
            mm3 = from * 16387.064;
            cm3 = from * 16.387064;
            m3 = from * 0.000016387064;
            litres = from * 0.016387064;
            usGallons = from * 0.00432900431;
            impGallons = from * 0.00360464866;
            break;
        case "feet":
            inch3 = from * 1728;
            feet3 = from;
            yard3 = from / 27;
            mm3 = from * 28316846.6;
            cm3 = from * 28316.8466 ;
            m3 = from * 0.0283168466;
            litres = from * 28.3168466;
            usGallons = from * 7.48051945;
            impGallons = from * 6.22883288;
            break;
        case "yard":
            inch3 = from * 46656;
            feet3 = from * 27;
            yard3 = from;
            mm3 = from * 764554858;
            cm3 = from * 764554.858;
            m3 = from * 0.764554858;
            litres = from * 764.554858;
            usGallons = from * 201.974025;
            impGallons = from * 168.178488;
            break;
        case "mm":
            inch3 = from / 16387.064;
            feet3 = from / 28316846.6;
            yard3 = from / 764554858;
            mm3 = from;
            cm3 = from * 0.001;
            m3 = from * 0.000000001;
            litres = from * 0.000001;
            usGallons = from * 0.000000264172051;
            impGallons = from * 0.000000219969157;
            break;
        case "cm":
            inch3 = from * 0.0610237441;
            feet3 = from * 0.0000353146667;
            yard3 = from * 0.00000130795062;
            mm3 = from * 1000;
            cm3 = from;
            m3 = from * 0.000001;
            litres = from * 0.001;
            usGallons = from * 0.000264172051;
            impGallons = from * 0.000219969157;
            break;
        case "m":
            inch3 = from * 61023.7441;
            feet3 = from * 35.3146667;
            yard3 = from * 1.30795062;
            mm3 = from * 1000000000;
            cm3 = from * 1000000;
            m3 = from;
            litres = from * 1000;
            usGallons = from * 264.172051;
            impGallons = from * 219.969157;
            break;
         case "litres":
             inch3 = from * 61.0237441;
             feet3 = from * 0.0353146667;
             yard3 = from * 0.00130795062;
             mm3 = from * 1000000;
             cm3 = from * 1000;
             m3 = from / 1000;
             litres = from;
             usGallons = from * 0.264172051;
             impGallons = from * 0.219969157;
            break;
        case "usGallons":
            inch3 = from * 231.000001;
            feet3 = from * 0.133680556;
            yard3 = from * 0.00495113171;
            mm3 = from * 3785411.8;
            cm3 = from * 3785.4118;
            m3 = from * 0.0037854118;
            litres = from * 3.7854118;
            usGallons = from;
            impGallons = from * 0.832673844;
            break;
        case "impGallons":
            inch3 = from * 277.419547;
            feet3 = from * 0.16054372;
            yard3 = from * 0.00594606369;
            mm3 = from * 4546091.88 ;
            cm3 = from * 4546.09188 ;
            m3 = from * 0.00454609188;
            litres = from * 4.54609188;
            usGallons = from * 1.20095042;
            impGallons = from;
            break;
    }
    var presc = parseInt(document.getElementById("ddPrecision").value);
    document.getElementById("spnInch3").innerHTML = RoundTo(inch3, presc);
    document.getElementById("spnFeet3").innerHTML = RoundTo(feet3, presc);
    document.getElementById("spnYard3").innerHTML = RoundTo(yard3, presc);
    document.getElementById("spnMm3").innerHTML = RoundTo(mm3, presc);
    document.getElementById("spnCm3").innerHTML = RoundTo(cm3, presc);
    document.getElementById("spnM3").innerHTML = RoundTo(m3, presc);
    document.getElementById("spnUsGallons").innerHTML = RoundTo(usGallons, presc);
    document.getElementById("spnImpGallons").innerHTML = RoundTo(impGallons, presc);
    document.getElementById("spnLitres").innerHTML = RoundTo(litres, presc);
    if (runs == 1)
        LogUser("ConV");
    if (runs < 2)
        runs++;
}
function RoundTo(val, places)
{
    places = Math.pow(10, places);
    var n = Math.round(val * places) / places;
    n += '';
	x = n.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1))
	{
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}