Object.extend(String.prototype, {
	trim: function() {
		var a = this.replace(/^\s+/, '');
		return a.replace(/\s+$/, '');
	},
	decodeHTML: function() {
		return this.replace(/&quot;/gi, "\"");
	},
	empty: function() {
		var s = this.trim();
		return (s.length == 0);
	}
});

Object.extend(Element, { 
	togglePointer: function(elem) {
		if (Element.hasClassName(elem, "pointerCursor")) {
			Element.removeClassName(elem, "pointerCursor");
		}
		else {
			Element.addClassName(elem, "pointerCursor");
		}
	}
});

Element.Methods.setValue =  function(element, value) {
	if (element.tagName.toUpperCase() == "SELECT") {
		var opt = $A(element.options).find(function(opt) {
			return (opt.value == value);
		});
		if (opt) {
			opt.selected = true;
			element.disabled = false;
		}
	}
	else {
		if (['INPUT', 'TEXTAREA'].include(element.tagName)) {
			element.value = value;
		}
	}
}
Element.Methods.blindUp = function(element, options) {
	var opts = {duration: 0.2};
	Object.extend(opts, options);
	new Effect.BlindUp(element, opts);
	return element;
}
Element.Methods.blindDown = function(element, options) {
	var opts = {duration: 0.2};
	Object.extend(opts, options);
	new Effect.BlindDown(element, opts);
	return element;
}
Element.Methods.highlight = function(element, options) {
	var opts = {duration: 2.0};
	Object.extend(opts, options);
	new Effect.Highlight(element, opts);
	return element;
}
Element.Methods.toggleBlind = function(element, options) {
	var opts = {duration: 0.2};
	Object.extend(opts, options);
	Effect.toggle(element, 'blind', opts);
	return element;
}
Element.Methods.fade = function(element, options) {
	var opts = {duration: 2.0, restorecolor: "#FFFFFF"};
	Object.extend(opts, options);
	new Effect.Fade(element, opts);
	return element;
}
//now commit the Methods we just added above
Element.addMethods();


LBSTemplate = {

ToggleSubMenu: function(elem, toggleMe) {
		if (Element.hasClassName(elem, 'expanded')) {
			Element.removeClassName(elem, 'expanded');
			Element.addClassName(elem, 'collapsed');
		}
		else {
			Element.removeClassName(elem, 'collapsed');
			Element.addClassName(elem, 'expanded');
		}
		Effect.toggle(toggleMe, 'blind', {duration:0.10});
	},
ClearTextForm : function (elem) {
        if (!LBSTemplate.IsANumber(elem.value)) {
            elem.value = "";
        }
},
PopulateTextForm : function(elem, text) {
        if (elem.value == "" || !LBSTemplate.IsANumber(elem.value)) {
            elem.value = text;
        }
},

IsANumber: function isANumber(number) {
    if (!parseFloat(number) && (number != "0")) {
        return false;
    }
    else {
        //the first digit was numeric, so check the rest
        for (var i=0; i<number.length; i++) {
            if ((number.charAt(i) != "0") && (!parseFloat(number.charAt(i)))) {
                return false;
                break;
            }
        }
    }
    return true;
},            
CalculatBMI: function ( ) { }
}

ScreenMessageType = {
	ERROR: 0,
	SUCCESS: 1
}
ScreenMessages = {
	messages: [],
	MessageType: ScreenMessageType.SUCCESS,
	Add: function (msg) {
		this.messages.push(msg);
	},	
	Show: function() {
		var msgs = "";
		for(var i=0; i < this.messages.length; i++) {
			msgs += "<li>" + this.messages[i] + "</li>";
		}		
		if (! msgs.empty()) {
			html = "<div id=\"screenMsgDisplay\" style=\"display:none;\" class=\"";
			if (this.MessageType == ScreenMessageType.ERROR) {
				html += "error\">";
				html += "<p><strong>There is missing or incorrect information:</strong></p>";
			}
			else {
				html += "success\">";
			}
			html += "<ul>";
			html += msgs + "</ul></div>";
			Element.update("screenMsgDisplay_handle", html);
			this.display();
		}
		this.messages.length = 0;
		//this.kill();
	},
	ShowMsg: function(msg) {
		var h = "<div id=\"screenMsgDisplay\" style=\"display:none;\" class=\"";
		h += (this.MessageType == ScreenMessageType.ERROR) ? "error\">" : "success\">";
		h += msg + "</div>";
		$('screenMsgDisplay_handle').update(h);
		this.display();
		//this.kill();
	},
	Hide: function() {
		Element.update("screenMsgDisplay_handle", "");
	},
	display: function() {
	    window.scrollTo(0, 0);
		$('screenMsgDisplay').show();
		this.kill();
	},
	slowDeath: function() {
		$('screenMsgDisplay').blindUp();
	},
	kill: function() {
		setTimeout(function(){ScreenMessages.slowDeath();}, 5000);
	}
}


function process(callerObj) {   

var txtWeight = document.getElementById('txtWeightLBS'); 
var txtHeightFt = document.getElementById('txtHeightFt'); 
var txtHeightIn = document.getElementById('txtHeightIn');
var txtResult =  document.getElementById('txtBMI');

if (!LBSTemplate.IsANumber(txtHeightFt.value) || (txtHeightFt.value.indexOf("-")!=-1)) {     
     
return false;   
} 
if (txtHeightIn.value == "in.") {
    txtHeightIn.value = "0";
}
if (!LBSTemplate.IsANumber(txtHeightIn.value) || (txtHeightIn.value.indexOf("-")!=-1)) {     
     
return false;   
}   
if (!LBSTemplate.IsANumber(txtWeight.value) || (txtWeight.value.indexOf("-")!=-1)) 
{        
    return false;   
}   

// Get the inches by converting ft into inches
var bFtToIn = parseInt(txtHeightFt.value) * 12; 

  
var bInchVal = parseInt(txtHeightIn.value) + bFtToIn;     
var bweight= parseInt( stripLeadingZeros( txtWeight.value ) );   
   
  
  var rs = 0;      
  rs=calc(bInchVal,bweight);   
  txtResult.value=roundToPennies(rs); } 
  
  function stripLeadingZeros( number ) {  
    /* convert to string; */  
    number = "" + number;  
    
     /* strip out any leading zeros that might be interpreted as octal; */  
     if ( number.indexOf("0") != -1 ) { /*if it includes a 0 test; */    
        while ( number.indexOf("0") == 0 ) {       
            number = number.substring( 1 );     
        }   
    }   
    
    return number; 
  } 
  
  function roundToPennies(n) {   
    pennies = n * 100;   
    pennies = Math.round(pennies);   
    strp = "" + pennies;   
    len = strp.length;   
    return strp.substring(0,len-2) + "." + strp.substring(len-2,len); 
  }


function calc (bHeight,bWeight) {   
    bWeightfinal=0.45359327*bWeight;   
    bHeightfinal=0.02540*bHeight;   
    
    var bmassIndex=bWeightfinal/ Math.pow(bHeightfinal,2);   
    return bmassIndex; 
}
