function isEmpty(s) {
	if (s.length == 0) return true;
	if (trim(s).length == 0) return true;
	return false;
}

function trim(s) {
	var i = 0;
	while(isspace(s.charAt(i))) i++;
	if (i == s.length) return new String('');
	
	var j = s.length - 1;
	while(isspace(s.charAt(j))) j--;
	
	return s.substring(i, j + 1);
}

function isspace(c) {
	if (c == ' ') return true;
	if (c == '\n') return true;
	return false;
}


// BMI metric calculator
function calculate_bmi_metric(){
	
	var f = document.bmi_calculator;
	var h = f.height_cm.value;
	var w = f.weight_kg.value;
		
	if (isEmpty(h) || isEmpty(w)) {
		return false;
	}
			
	h = h/100;
	bmi = w/(h * h);
	f_bmi = Math.floor(bmi);
	diff  = bmi - f_bmi;
	diff = diff * 10;
	diff = Math.round(diff);
	
	if (diff == 10){
		f_bmi += 1;
		diff = 0;
	}
	
	bmi = f_bmi + "." + diff;
	formatbmi(bmi);
	
}

// BMI imperial calculator
function calculate_bmi_imperial(){
	
	var f = document.bmi_calculator;
	var ft = parseFloat(f.height_ft.value * 12.0);
	var i1 = parseFloat(f.height_in.value * 1.0);
	var ins = (ft + i1);
	
	var st = parseFloat(f.weight_st.value * 14.0);
	var lb = parseFloat(f.weight_lb.value * 0.1);
	var lbs = (st + lb);

	h2 = ins * ins;
	bmi = lbs/h2 * 737
	f_bmi = Math.floor(bmi);
	diff  = bmi - f_bmi;
	diff = diff * 10;
	diff = Math.round(diff);

	if (diff == 10){
		f_bmi += 1;
		diff   = 0;
	}
	
	bmi = f_bmi + "." + diff;
	formatbmi(bmi);

}

// BMI calculator
function formatbmi(x) {
	bmi = x;
	score.innerHTML = 'Your BMI score: <span class="score">'+x+'</span>';  
	
	var level = document.getElementById("level"); // amend the image
	
	str  = ''; // amend the content
	
	if (bmi < 18.5) {
		msg  = '<p><strong>Underweight</strong></p>';
		msg += '<p>You are approaching the level where you may need our services. Be careful.</p>';
		level.style.height = '30px';
		
	} else if (bmi >18.49 && bmi <= 25) {
		msg  = '<p><strong>Healthy</strong></p>';
		msg += '<p>Congratulations! You are in the healthy weight range.</p>';
		level.style.height = '60px';
		
	} else if (bmi >25 && bmi <= 27) {
		msg  = '<p><strong>Overweight</strong></p>';
		msg += '<p>You are approaching the level where you may need our services. Be careful.</p>';
		level.style.height = '90px';
		
	} else if (bmi >27 && bmi <= 35) {
		msg  = '<p><strong>Mildly obese</strong></p>';
		msg += '<p>In most circumstances, you will be best suited by the insertion of a stomach balloon. Occasionally, patients with this BMI might be better managed by an adjustable gastric band. Maybe you are a very profound yo-yo dieter and this is your lowest (and generally unsustainable weight). However, this is a specialised decision that must only be taken by you after a thorough consultation.</p>';
		level.style.height = '120px';
		
	} else if (bmi >35 && bmi <= 40) {
		msg  = '<p><strong>Severely obese</strong>';
		msg += '<p>Patients in this group are generally equally well suited to either a stomach balloon or an adjustable gastric band. Which one they opt for usually depends on a consideration of factors, such as:</p>';
		msg += '<ul>';
		msg += '<li>Personal choice - some patients simply will not consider having what they consider to be a "proper" operation, and are attracted by the simplicity of the stomach balloon.</li>';
		msg += '<li>Previous surgery - occasionally patients may have had previous abdominal surgery that might preclude them from having a gastric band placed via keyhole surgery.</li>';
		msg += '<li>Obesity-associated co-morbidities - these include medical conditions such as type 2 diabetes, heart disease, asthma, hiatus hernia, gall stones, or arthritis. Under UK Government guidelines such patients are usually better suited to an adjustable gastric band or other more major anti-obesity surgery. Indeed, the laparoscopically-placed adjustable gastric band allows your surgeon to treat your gall stones and hiatus hernia at the same time.</li>';
		msg += '</ul>';
		msg += '<p>Whichever you choose, it is an important decision, and must only be taken by you after a thorough consultation.</p>';
		level.style.height = '150px';
		
	} else if (bmi > 40 && bmi <= 50) {
		msg  = '<p><strong>Morbidly obese</strong></p>';
		msg += '<p>This is potentially a very dangerous situation. In almost all cases of patients with a BMI of this magnitude we would recommend placement of an adjustable gastric band by keyhole surgery. This is wholly in line with UK Government guidelines. Our specialist surgeons and their support teams are ideally placed to undertake this form of minimally invasive surgery as safely as is possible anywhere in the world, and the gastric band provides a much longer-term solution to what is, after all, a long-term problem.</p>';
		msg += '<p>However, occasionally we might be prepared to offer a stomach balloon for patients within this group. This might be for any of the following reasons:</p>';
		msg += '<ul>';
		msg += '<li>Personal choice - some patients simply will not consider having what they consider to be a "proper" operation, and are attracted by the simplicity of the stomach balloon.</li>';
		msg += '<li>Previous surgery - occasionally patients may have had previous abdominal surgery that might preclude them from having a gastric band placed via keyhole surgery.</li>';
		msg += '<li>Patients who need to lose significant amounts of weight before they might be considered for a gastric band.</li>';
		msg += '<li>Patients who need to show that they can follow a prescribed behaviour modification regime before their surgeon is prepared to undertake the insertion of a gastric band.</li>';
		msg += '<li>Patients who just need to lose weight before they will be considered for other non-obesity surgery, such a major joint replacement.</li>';
		msg += '</ul>';
		level.style.height = '180px';
		
	} else {
		msg  = '<p><strong>Super obese</strong></p>';
		msg += '<p>This has extremely significant implications for both your length and quality of life. All patients such as yourself are managed on a highly individualised basis by The National Obesity Surgery Centre. You have several treatment options, that might include both a stomach balloon and a gastric band placed sequentially over the course of a year or more.</p>';
		level.style.height = '210px';
	}
	
	str += msg;
	results.innerHTML = str;  
	
}
