/*
window.onload = function() {
if (document.getElementById('driver1Flash')) {
	var d1 = new SWFObject("flash/d1.swf", "mymovie2", "303", "173", "6", "#fff200");
	d1.write("driver1Flash");
}
if (document.getElementById('flashBanner')) {
	var so = new SWFObject("flash/home-page-banner.swf", "mymovie", "444", "209", "6", "#fff200");
	so.write("flashBanner");
}

if (document.getElementById('driver2Flash')) {
	var d2 = new SWFObject("flash/d2.swf", "mymovie3", "102", "135", "6", "#fff200");
	d2.write("driver2Flash");
}
if (document.getElementById('driver3Flash')) {
	var d3 = new SWFObject("flash/d3.swf", "mymovie4", "168", "128", "6", "#fff200");
	d3.write("driver3Flash");
}
}
*/



function Convert()
{
    var input = document.getElementById('Input').value;
    var output = document.getElementById('Results');
    var unitSelected = document.getElementById('UnitSelection').value;
    output.innerHTML = '';
    if(CheckNumber(input) == true)
	{
        if(CheckUnit(unitSelected) == true) 
		{
			StartConvertion(input, unitSelected, output);
		 }
			else 
			{
			output = ("Conversion Failed!");
			}
	}
    
}

function MathRounding(value)
{
	return (Math.round(value * 100) / 100);
}

function StartConvertion(input, unitSelected, output)
{
    var title = "<span style=\"color: #e00026\">Conversion Result(s):</span><br />"
    if(unitSelected == "Grams")
    {
        output.innerHTML = title + (MathRounding(input * 0.001) + " Kilograms<br />" 
		+ MathRounding(input * 0.0352739619) + " Ounces<br />" 
		+ MathRounding(input * 0.00220462262) + " Pounds<br />");
    }
    else if(unitSelected == "Kilograms")
    {
        output.innerHTML = title + (MathRounding(input * 1000) + " Grams<br />" 
		+ MathRounding(input * 35.27) + " Ounces<br />" 
		+ MathRounding(input * 2.21) + " Pounds<br />");
    }
    else if(unitSelected == "Ounces")
    {
        output.innerHTML = title + (MathRounding(input * 28.3495231) + " Grams<br />" 
		+ MathRounding(input * 0.0283495231) + " Kilograms<br />" 
		+ MathRounding(input * 0.0625) + " Pounds<br />");
    }
    else if(unitSelected == "Pounds")
    {
        output.innerHTML = title + (MathRounding(input * 453.59237) + " Grams<br />" 
		+ MathRounding(input * 0.45359237) + " Kilograms<br />" 
		+ MathRounding(input * 16) + " Ounces<br />");
    }
    else if(unitSelected == "Millilitres")
    {
        output.innerHTML = title + (MathRounding(input * 0.001) + " Litres<br />" 
		+ MathRounding(input * 0.0017598) + " Pints<br />" 
		+ MathRounding(input * 0.035195) + " Fluid Ounces<br />");
    }
    else if(unitSelected == "Litres")
    {
        output.innerHTML = title + (MathRounding(input * 1000) + " Millilitres<br />" 
		+ MathRounding(input * 1.76) + " Pints<br />" 
		+ MathRounding(input * 35.1950) + " Fluid Ounces<br />");
    }
	else if(unitSelected == "Fluid Ounces")
    {
        output.innerHTML = title + (MathRounding(input * 0.0284130625) + " Millilitres<br />" 
		+ MathRounding(input * 28.4130) + " Litres<br />" 
		+ MathRounding(input * 0.05) + " Pints<br />");
    }
    else if(unitSelected == "Pints")
    {
        output.innerHTML = title + (MathRounding(input * 568.26125) + " Millilitres<br />" 
		+ MathRounding(input * 0.56826125) + " Litres<br />" 
		+ MathRounding(input * 20) + " Fluid Ounces<br />");
    }
    else if(unitSelected == "Celcius")
    {
    	var fahrenheit = (212-32)/100 * input + 32;
		output.innerHTML = title + MathRounding(fahrenheit) + " Fahrenheit";
    }
    else if(unitSelected == "Fahrenheit")
    {
		var celcius = 100/(212-32) * (input - 32);
        output.innerHTML = title + MathRounding(celcius) + " Celcius";//100/(212-32) * (input - 32 ) + " Celcius<br />");
    }
}

function CheckUnit(str)
{
    if ((str == "--WEIGHTS--") || (str == "---LIQUIDS--") || (str == "--TEMPERATURES--") || (str == "")) 
    {
        alert("Please select a unit.");
        return false;
    }
    return true;
}

function CheckNumber(str)
{
    if (str == "") 
    {
        alert("Enter a number in the field, please.");
        return false;
    }
    for (var i = 0; i < str.length; i++) 
    {
    var ch = str.substring(i, i + 1);
    if (ch=="0" || ch=="1" || ch=="2" || ch=="3" || ch=="4" || ch=="5" ||
        ch=="6" || ch=="7" || ch=="8" || ch=="9" || ch==".")
        {
        // it's a number
        }
    else
        {
        alert("Please enter numbers only.");
        return false;
        }
    }
    return true;
}



