var jpg = 1;
var gif = 9;
var f = false;
var b = new Array;
var c = new Array;

function prel(){
	for(i=1;i<=jpg;i++)
	{
		str = 'buttons/but' + i + '_o.gif';
		b[i-1] = new Image();
		b[i-1].src = str;
	}
	for(i=1;i<=gif;i++)
	{
		str = 'buttons/b' + i + '_o.jpg';
		c[i-1] = new Image();
		c[i-1].src = str;
	}
}

function on(im){document.images[im].src="buttons/"+im+"_o.gif";}

function off(im){document.images[im].src = "buttons/"+im+".gif";}

function on_jpg(im, id2){
	if (id2)
		if (document.getElementById(id2).style.display != 'block')
			{document.images[im].src="buttons/"+im+"_o.jpg";}
		else {}
	else
	document.images[im].src="buttons/"+im+"_o.jpg";

}

function off_jpg(im, id2){
	if (id2)
		if (document.getElementById(id2).style.display != 'block')
		{document.images[im].src="buttons/"+im+".jpg";}
		else {}
	else
	document.images[im].src = "buttons/"+im+".jpg";
}


function cal_bmi(lbs, ins) {
    h2 = ins * ins;
    bmi = lbs / h2 * 703
    f_bmi = Math.floor(bmi);
    diff = bmi - f_bmi;
    diff = diff * 10;
    diff = Math.round(diff);
    if (diff == 10) {
        // Need to bump up the whole thing instead
        f_bmi += 1;
        diff = 0;
    }
    bmi = f_bmi + "." + diff;
    return bmi;
}

function CheckInt(w) {
    if (isNaN(parseInt(w))) {
        return false;
    } else if (w < 0) {
        return false;
    } else {
        return true;
    }
}

function ValidateBMIInput() {
    var returnValue = true;
    if (!CheckInt($("#bmiHeightFeet").val()))
        returnValue = false;
    if (!CheckInt($("#bmiHeightInches").val()))
        returnValue = false;
    if (!CheckInt($("#bmiWeight").val()))
        returnValue = false;
    return returnValue;

}

function CalculateBMI() {
    if (ValidateBMIInput()) {
        var heightFeet = parseInt($("#bmiHeightFeet").val());
        var heightInches = parseInt($("#bmiHeightInches").val());
        var weight = parseInt($("#bmiWeight").val());
        var inches = (heightFeet * 12) + heightInches;
        var BMI = cal_bmi(weight, inches);
        $("#bmiResult").val(BMI);
    }
    else {
        alert("Please enter your height and weight.");
    }
}
