
var input_field_length=0;
var printURL="tool_bmi_result_pf.html";
function TabNext(obj,event,len,next_field) {
	if (event == "down") {
		input_field_length=obj.value.length;
		}
	else if (event == "up") {
		if (obj.value.length != input_field_length) {
			input_field_length=obj.value.length;
			if (input_field_length == len) {
				next_field.focus();
				}
			}
		}
	}


function calBMI() {
    hideAllLayer();
	if(document.forms.input.unitType.options[1].selected) {
		heightInM = document.forms.input.unit2.value * .01; 
		heightInM += document.forms.input.unit1.value * 1; 
	}
	
	else  {
		heightInM = document.forms.input.unit2.value * .0254;
		heightInM += document.forms.input.unit1.value * 12 * .0254;
	}
	
	
	if(document.forms.input.weightType.options[1].selected) {
		weightInKG = document.forms.input.weight.value * 1; 
	}
	else 
		weightInKG = document.forms.input.weight.value * .4536; 
	//height under 3 ft and over 7 ft will show error
	if(isNaN(heightInM) || (heightInM <= 0.9144)||(heightInM >= 2.1336)){ 
	    showLayer("sponsor_logo");
		showLayer("errorMessageHeight");
		showLayer("inputLayer");
		showLayer("inputBot");
	}	//weight under 5 lb and over 800lb ft will show error
	else if(isNaN(weightInKG)||(weightInKG <= 2.267962)||(weightInKG >= 362.873896)) {
	    showLayer("sponsor_logo");
		showLayer("errorMessageWeight");
		showLayer("inputLayer");
		showLayer("inputBot");
    }		
	else{
		var bmi = weightInKG / (heightInM * heightInM) ;
		
		var outputString = "" + bmi;
		if(outputString.indexOf(".")){
		document.getElementById('bmiNumber').innerHTML = outputString.substr(0,outputString.indexOf(".")+3);
		}else{
		document.getElementById('bmiNumber').innerHTML = outputString;
		}
		showLayer("result");
		showLayer("info");
		showLayer("inputBot");
		showLayer("feedback");
		showLayer("toolLegal");
		showLayer("inputLayer");
		showLayer("resultBot");
	}

}

var layerArray = ["sponsor_logo","result","errorMessageHeight","errorMessageWeight","inputBot"];

function setFormData(){
  var query = window.location.search.substring(1);
     if (query != ""){
	  var vars = query.split("&");
	  for (var i=0;i<vars.length-2;i++) {
	    var pair = vars[i].split("=");
		if (pair[0]=="unitType"){
			if (pair[1]=="Metric"){
				document.input.unitType.options[1].selected=true;
			}else{
				document.input.unitType.options[0].selected=true;
			}
		}else if (pair[0]=="weightType"){
			if (pair[1]=="Kilograms"){
				document.input.weightType.options[1].selected=true;
			}else{
				document.input.weightType.options[0].selected=true;
			}
		}else{
		eval("document.input." + pair[0] + ".value = '" + pair[1]+"'");
		}
	  }
	  calBMI();
  }else{
  showLayer("inputLayer");
  showLayer("inputBot");
  }
}


function doSubmit()
{
	document.forms.input.submit(); 
}

function getNewWindow(url){
	var targetVal = "_self";
	msgWindow=open(url,null,'scrollbars=yes,menubar=yes,status=no,toolbar=no,resizable=yes,width=700,height=500');
    if (msgWindow.opener == null) msgWindow.opener = _self;
}

