var Miles;
var Mpgcity;
var Mpghwy;
var Pctcity;
var Pcthwy;
var Price;
var Citytotal;
var Hwytotal;
var Total;

function Calculate(form)
{
    Citytotal = ((Miles / Mpgcity) * Price) * (Pctcity / 100);
    Hwytotal = ((Miles / Mpghwy) * Price) * (Pcthwy / 100);
    Total = Citytotal + Hwytotal;
    form.Total.value = parseFloat(Total);
}

function SetMiles(miles)
{
    Miles = miles.value;
}

function SetMpgcity(mpgcity)
{
    Mpgcity = mpgcity.value;
}

function SetMpghwy(mpghwy)
{
    Mpghwy = mpghwy.value;
}

function SetPctcity(pctcity)
{
    Pctcity = pctcity.value;
}

function SetPcthwy(pcthwy)
{
    Pcthwy = pcthwy.value;
}

function SetPrice(price)
{
    Price = price.value;
}

function ClearForm(form) 
{
    form.Miles.value = " ";
    form.Mpgcity.value = " ";
    form.Mpghwy.value = " ";
    form.Pctcity.value = " ";
    form.Pcthwy.value = " ";
    form.Price.value = " ";
    form.Total.value = " ";
}

function formCheck(objForm)
{
	document.getElementById("btnCalculate").disabled = true;

	if(objForm.Miles.value == "")
	{
		alert("Please enter yearly miles driven.");
		objForm.Miles.focus();
	}
	else if(objForm.Mpgcity.value == "")
	{
		alert("Please enter mpg city");
		objForm.Mpgcity.focus();
	}
	else if(objForm.Mpghwy.value == "")
	{
		alert("Please enter mpg highway.");
		objForm.Mpghwy.focus();
	}
	else if(objForm.Pctcity.value == "")
	{
		alert("Please enter % city.");
		objForm.Pctcity.focus();
	}
	else if(objForm.Pcthwy.value == "")
	{
		alert("Please enter % highway.");
		objForm.Pcthwy.focus();
	}
	else if(objForm.Price.value == "")
	{
		alert("Please enter Gas Price.");
		objForm.Price.focus();
	}
	else
	{
		Calculate(objForm);
	}
	document.getElementById("btnCalculate").disabled = false;
}


function onlyNumbersAndDashes(e)
{
    var keyCode;
    var c;
    
    if (window.event) { // Internet Explorer
        keyCode = e.keyCode;
    }
    else if (e.which) { // Everyone Else
        keyCode = e.which;
    }
    c = String.fromCharCode(keyCode);
    //alert (keyCode);
    if (isNaN(c) && 
        keyCode != 109 && keyCode != 189 && // dash key
        (keyCode < 96 || keyCode > 105) && // number pad
        (keyCode < 35 || keyCode > 40) && // arrow keys, home, end
        keyCode != 9 && keyCode != 46 && keyCode != 8) // tab, backspace, delete
    { 
        return false;
    }
    return true;
}