function getElm (sId, vParent, sType) {
	/*
	getElm v1.1
	Crossbrowser getElementById
	
	(c) Hannes Tydén 2002
	hannes@rakbladsfisk.com
	http://rakbladsfisk.com
	*/
	var r = null;
	if (document.getElementById) {
		r = document.getElementById (sId);
		if (r) return r;
	} // if
	if (document.all && !r) {
		r = document.all[sId];
		if (r) return r;
	} // if
	if (document.layers || (!r && sType && sType != "layer")) {
		var oParent = null;
		if (vParent) {
			// If vParent isn't a string then assume it's an object.
			oParent = (vParent.constructor == String)? eval (vParent): vParent;
		} else {
			oParent = document;
		} // if
		if (!sType) var sType = "layer";
		r = oParent[sType +"s"][sId];
	} // if
	return r;
} // function getElm

function doCalc () {
	var oForm = getElm ("FTest", document, "form");
	getElm ("BMI", oForm, "element").value = Math.round (CalculateBMI (getElm ("Length", oForm, "element").value, getElm ("Weight", oForm, "element").value)*10)/10
} // function doCalc

function CalculateBMI (sLength, sWeight) {

	var r = "";
	var iLength = 0;
	var iWeight = 0;
	if (sLength != "" && sWeight != "") {
		sLength = sLength.replace (/,/, ".").replace (/\s/, "");
		sWeight = sWeight.replace (/,/, ".").replace (/\s/, "");
		iLength = new Number (sLength);
		iWeight = new Number (sWeight);
		if (iLength > 100) {
			iLength /= 100;
		} // if
		// if (iLength < 2.5 && iWeight < 300)
		r = iWeight/Math.pow (iLength, 2);
	} // if
	return r;
} // function CalculateBMI

function setMotivation (i, s, b) {
	var o = null;
	o = getElm ("IMot"+ i, document, "image");
	if (!o.locked) o.src = o.src.substr (0, o.src.lastIndexOf ("_")+1) + s +".gif";
	if (b) {
		for (var j=1;j<=10;j++) {
			o = getElm ("IMot"+ j, document, "image");
				if (i == j) {
					o.locked = true;
					o.src = o.src.substr (0, o.src.lastIndexOf ("_")+1) + "1.gif";
				} else {
					o.locked = false;
					o.src = o.src.substr (0, o.src.lastIndexOf ("_")+1) + "0.gif";
				} // if
		} // for
		oForm = getElm ("FTest", document, "form");
		getElm ("Motivation", oForm, "element").value = i;
	} // if
} // function setMotivation

function setProfile (i) {
	var oForm = getElm ("FTest", document, "form");
	getElm ("Profile", oForm, "element").value = i;
} // function setProfile

function setReqFields (o) {
	for (var i=1;i<arguments.length;i += 2) {
		o.elements[arguments[i]].required = true;
		o.elements[arguments[i]].errorMessage = arguments[i+1];
	} // for
} // function setReqFields

function checkForm (o) {
	var r = "";	
	var aElms = o.elements;
	for (var i=0;i<aElms.length;i++) {
		if (aElms[i].required && aElms[i].value == "") {
			r += aElms[i].errorMessage +"\n";
		} // if
	} // for
	return r;
} // function checkForm

function checkAndSubmit (o) {
	var sCF = checkForm (o);
	if (sCF) alert (sCF);
	else o.submit ();
} // function checkAndSubmit

function checkBMI() {
	var form = document.forms['FTest'];
	if (form.BMI.value == "") alert ("Du måste räkna ut ditt BMI-värde för att fortsätta.");
	else form.submit ();
} 
