function isAlien(a) {
   return isObject(a) && typeof a.constructor != 'function';
}

function isArray(a) {
    return isObject(a) && a.constructor == Array;
}

function isBoolean(a) {
    return typeof a == 'boolean';
}

function isEmpty(o) {
    var i, v;
    if (isObject(o)) {
        for (i in o) {
            v = o[i];
            if (isUndefined(v) && isFunction(v)) {
                return false;
            }
        }
    }
    return true;
}

function isFunction(a) {
    return typeof a == 'function';
}

function isNull(a) {
    return a === null;
}

function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}

function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function isString(a) {
    return typeof a == 'string';
}

function isUndefined(a) {
    return typeof a == 'undefined';
}

function twodp(v){
	return Math.round(v*100)/100;
}

function getObject(name) {
	var ns4 = (document.layers) ? true : false;
	var w3c = (document.getElementById) ? true : false;
	var ie4 = (document.all) ? true : false;
	if (ns4) return eval('document.' + name);
	if (w3c) return document.getElementById(name);
	if (ie4) return eval('document.all.' + name);
	return false;
}

function validateText(obj,list,valid){
	var Invalid = false;
	var Text = obj.value;
	var Len = Text.length;
	var i;
	var LetterPos;
	if(Len > 0){
		for(i=0;i<Len;i++){
			LetterPos = list.indexOf(Text.charAt(i),0);
			if ((valid == false) && (LetterPos >= 0))
				Invalid = true;
			if ((valid == true) && (LetterPos == -1))
				Invalid = true;
		}
	}
	return Invalid;
}

function checkEmpty(obj){
	if(isNumber(obj.length)){
		return (obj.length > 0);
	}else{
		return (obj.value.length > 0);
	}
}


function checkEmails(form) {
	var Valid = true;

	if(!checkEmpty(form.email.value)){
		alert("Your email address is required");
		Valid = false;
	}
	if(!checkEmpty(form.email2.value)){
		alert("You have to enter you email address in twice");
		Valid = false;
	}
	if(!checkConfirmation(form.email.value,form.email2.value)){
		alert("Your emails do not match!");
		Valid = false;
	}
	return Valid;
}

function hidetoggle (e) {
    document.getElementById(e).style.display = (document.getElementById(e).style.display == 'none') ? 'block' : 'none';
    return false;
}

function hide (e) {
    document.getElementById(e).style.display = 'none';
    return false;
}

function show (e) {
    document.getElementById(e).style.display = 'block';
    return false;
}

function checkConfirmation(v1, v2) {
    if (v1 == v2)
        return true;
    return false;
}

function validateAlpha(obj){
	var Invalid = false;
	var InvalidLetters ="`~!@#$%^&*()_+=-/\,.<>;':[]{}|1234567890";
	var Text = obj.value;
	var Len = Text.length;
	var i;
	var LetterPos;
	if(Len > 0){
	for(i=0;i<Len;i++){
		LetterPos = InvalidLetters.indexOf(Text.charAt(i),0);
		if (LetterPos >= 0)
			Invalid = true;
		}
		if (Invalid){
			alert("The text entered includes an illegal character");
			obj.focus();
			obj.select();
		}
	}
	return;
}

function validateNumeric(obj){
	var Invalid = false;
	var ValidLetters ="1234567890";
	var Text = obj.value;
	var Len = Text.length;
	var i;
	var LetterPos;
	if(Len > 0){
		for(i=0;i<Len;i++){
			LetterPos = ValidLetters.indexOf(Text.charAt(i),0);
			if (LetterPos == -1)
				Invalid = true;
		}
		if (Invalid){
			alert("You can only enter numeric values only.");
			obj.focus();
			obj.select();
		}
	}
	return;
}

function validateMeasurement(obj){
	var Invalid = false;
	var ValidLetters = '1234567890/';
	var Text = obj.value;
	var Len = Text.length;
	var i;
	var LetterPos;
	if(Len > 0){
		for(i=0;i<Len;i++){
			LetterPos = ValidLetters.indexOf(Text.charAt(i),0);
			if (LetterPos == -1)
				Invalid = true;
		}
		if (Invalid){
			alert("You can only enter numeric values only.");
			obj.focus();
			obj.select();
		}
	}
	return;
}

function validatePhone(obj){
	var Invalid = false;
	var ValidLetters = "+() 1234567890";
	var Text = obj.value;
	var Len = Text.length;
	var i;
	var LetterPos;
	if(Len > 0){
		for(i=0;i<Len;i++){
			LetterPos = ValidLetters.indexOf(Text.charAt(i),0);
			if (LetterPos == -1)
				Invalid = true;
		}
		if (Invalid){
			alert("The text entered includes an illegal character");
			obj.focus();
			obj.select();
		}
	}
	return;
}

function validateEmail(obj){
	var address = obj.value;
	if(address.length > 0){
		if((address == "") || (address.indexOf('@') == -1) || (address.indexOf('.') == -1)){
			alert("Invalid email address.");
			obj.focus();
			obj.select();
		}else if(validateText(obj,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@._-",true)){
			alert("Invalid email address.");
			obj.focus();
			obj.select();
		}
	}
	return;
}

function checkEmail(obj){
	var address = obj.value;
	var valid = true;
	if(address.length > 0){
		if((address == "") || (address.indexOf('@') == -1) || (address.indexOf('.') == -1)){
			valid = false;
		}else if(validateText(obj,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@._-",true)){
			valid = false;
		}
	}
	return valid;
}

function checkloginForm() {
    formErrors = false;
    if ((document.login.user.value.length < 1) || (document.login.pass.value.length < 1)) {
        formErrors = "You must fill in all fields.";
    }
    if (formErrors) {
        alert(formErrors);
        return false;
    } else {
        return true;
    }
}	

function validateForm(checkString){
	// Validate form objects from string
	// field:longname:allowedcharacters:check
	// eg: name:long name:alpha:empty - Check textfield "name", alpha characters (a-z) only, check if empty, display "long name" to user
	// allowedcharacters: text, azAZ, azAZ09, 09, phone, email
	// check: empty
	var elementArray=checkString.split(",");
	var element_num=0;
	var element, field, longname, allowedcharacters, check, text, valid, err;
	err = "";
	valid = true;
	while (element_num < elementArray.length){
		element = elementArray[element_num].split(":");
		field = document.getElementById(element[0]);
		longname = element[1];
		allowedcharacters = element[2];
		check = element[3];
		//alert(field+" - "+text+" - "+allowedcharacters+" - "+check);
		if(element.length > 1){
			if(!isNull(field)){
				switch(check){
					case "empty":
						if(!checkEmpty(field.value)){
							valid = false;
							err += longname+" needs to be filled in!\n";
						}
					break;
					default:
						//nothing
					break;
				}
				switch(allowedcharacters){
					case "text":
						if(validateText(field,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()_+=-/\,.<>;':[]{}| ",true)){
							valid = false;
							err += longname+" is invalid!\n";
						}
					break;
					case "azAZ":
						if(validateText(field,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ",true)){
							valid = false;
							err += longname+" is invalid!\n";
						}
					break;
					case "azAZ09":
						if(validateText(field,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,- ",true)){
							valid = false;
							err += longname+" is invalid!\n";
						}
					break;
					case "09":
						if(validateText(field,"0123456789.,- ",true)){
							valid = false;
							err += longname+" is invalid!\n";
						}
					break;
					case "phone":
						if(validateText(field,"+() 1234567890-",true)){
							valid = false;
							err += longname+" is invalid!\n";
						}
					break;
					case "email":
						if(!checkEmail(field)){
							valid = false;
							err += longname+" is invalid!\n";
						}
					break;
					default:
						//nothing
					break;
				}
			}
		}
		element_num+=1;
	}
	if(err.length > 0){
		alert("There was an error processing your form:\n\n"+err);
	}
	return valid;
}

function copyFields(str){
	// Created by Blake Kus
	// http://www.kustomwebdesigns.com
	var elementArray=str.split(",");
	var element_num=0;
	var element, a, b;
	while (element_num < elementArray.length){
		element = elementArray[element_num].split(":");
		a = getObject(element[0]);
		b = getObject(element[1]);
		if(element.length > 1){
			if(!isNull(a) && !isNull(b)){
				b.value = a.value;
			}
		}
		element_num+=1;
	}
	return false;
}

function validateForm(checkString){
	// Created by Blake Kus
	// http://www.kustomwebdesigns.com
	// Validate form objects from string
	// field:longname:allowedcharacters:check
	// eg: name:long name:alpha:empty - Check textfield "name", alpha characters (a-z) only, check if empty, display "long name" to user
	// allowedcharacters: text, azAZ, azAZ09, 09, phone, email
	// check: empty
	var elementArray=checkString.split(",");
	var element_num=0;
	var element, field, name, longname, allowedcharacters, check, text, valid, err, f, c, minreq;
	err = "";
	valid = true;
	while (element_num < elementArray.length){
		element = elementArray[element_num].split(":");
		field = getObject(element[0]);
		name = element[0];
		longname = element[1];
		allowedcharacters = element[2];
		check = element[3];
		if(element.length > 1){
			if(isNull(field)){
				switch(name){
					case "minimumrequired":
						minreq = check.split(";");
						present = 0;
						for(i=0; i < minreq.length; i++) {
							if(checkEmpty(getObject(minreq[i]))){
								present++;
							}
						}
						if(present < allowedcharacters){
							err += "You need to fill in atleast "+allowedcharacters+" "+longname+"!\n";
							valid = false;
						}
					break;
					case "equaleachother":
						if(!checkConfirmation(getObject(allowedcharacters).value,getObject(check).value)){
							err += longname+" do not match!\n";
							valid = false;
						}
					break;
					default:
						err += longname+" ("+field+") could not be found! Please contact us.\n";
						valid = false;
					break;
				}
			}else{
				switch(check){
					case "empty":
						if(field.type == "radio"){
							f = field.form;
							c = false;
							for(i=0; i < f.length; i++) {
								if((f[i].type == "radio") && (f[i].name == field.name)) {
									if(f[i].checked){
										c = f[i].value;
									}
								}
							}
							if(c == false){
								valid = false;
								err += longname+" needs to be filled in!\n";
							}
						}else if(!checkEmpty(field)){
							valid = false;
							err += longname+" needs to be filled in!\n";
						}
					break;
				}
				switch(allowedcharacters){
					case "text":
						if(validateText(field,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()_+=-/\,.<>;':[]{}| ",true)){
							valid = false;
							err += longname+" is invalid!\n";
						}
					break;
					case "azAZ":
						if(validateText(field,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ",true)){
							valid = false;
							err += longname+" is invalid!\n";
						}
					break;
					case "azAZ09":
						if(validateText(field,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,- ",true)){
							valid = false;
							err += longname+" is invalid!\n";
						}
					break;
					case "09":
						if(validateText(field,"0123456789.,- ",true)){
							valid = false;
							err += longname+" is invalid!\n";
						}
					break;
					case "phone":
						if(validateText(field,"+() 1234567890-.",true)){
							valid = false;
							err += longname+" is invalid!\n";
						}
					break;
					case "email":
						if(!checkEmail(field)){
							valid = false;
							err += longname+" is invalid!\n";
						}
					break;
				}
			}
		}
		element_num+=1;
	}
	if(err.length > 0){
		alert("There was an error processing your form:\n\n"+err);
	}
	return valid;
}

function AuthenticateForm(f){
	// Created by Blake Kus
	// http://www.kustomwebdesigns.com
	return validateForm(f.validate.value);
}

function sort(what){
	document.FormSearch.sort.value = what;
	document.FormSearch.dosort.value = 1;
	document.FormSearch.submit();
}

function checkAll(object){
	var form = object.form;
	for (i = 0; i < form.length; i++){
		if((form[i].name.substr(0,object.name.length) == object.name) && (form[i].type == "checkbox")) {
			form[i].checked = object.checked;
		}
	}
}

function getpage ( selectedpage ){
	document.FormSearch.page.value = selectedpage;
	document.FormSearch.submit();
}

function convertMeasurement(big, small, from, to){
	var allcm;
	var allinch;
	var tmpm;
	var m;
	var cm;
	var tmpi;
	var ft;
	var inch;
	var result;

	switch(from){
		case "m":
			allcm = big * 100 + small*1;
			allinch = allcm * 0.3937;
			tmpm = allcm / 100;
			tmpm = tmpm.toFixed(3);
			tmpm = tmpm.split(".");
			m = tmpm[0];
			cm = tmpm[1]/10;
			cm = Math.round(cm);
			tmpi = allcm * 0.0328;
			tmpi = tmpi.toFixed(3);
			tmpi = tmpi.split(".");
			ft = tmpi[0];
			inch = (tmpi[1] / 1000) * 12;
			inch = Math.round(inch);
      		break;
		case "i":
			allcm = big * 30.48 + small * 2.54;
			allinch = allcm * 2.54;
			tmpm = allcm / 100;
			tmpm = tmpm.toFixed(3);
			tmpm = tmpm.split(".");
			m = twodp(tmpm[0]);
			cm = tmpm[1]/10;
			cm = Math.round(cm);
			tmpi = allcm * 0.0328;
			tmpi = tmpi.toFixed(3);
			tmpi = tmpi.split(".");
			ft = tmpi[0];
			inch = (tmpi[1] / 1000) * 12;
			inch = Math.round(inch);
      		break;
	}
	switch(to){
		case "cm":
			result = Math.round(allcm);
		break;
		case "m/cm":
			result = m+"/"+cm;
		break;
		case "m":
			result = twodp(allcm/100);
		break;
		case "in":
			result = Math.round(allinch);
		break;
		case "ft/in":
			result = ft+"/"+inch;
		break;
	}
	return result;
}

function convertWeight(big, from, to){
	var allg;
	var alloz;
	var tmpkg;
	var kg;
	var g;
	var tmplb;
	var lb;
	var oz;
	var result;

	switch(from){
		case "m":
			allg = big * 1000;
			alloz = allg * 0.03527;
			tmpkg = allg / 1000;
			tmpkg = tmpkg.toFixed(3);
			tmpkg = tmpkg.split(".");
			kg = tmpkg[0];
			g = tmpkg[1];
			g = Math.round(g);
			tmplb = allg * 0.0022046;
			tmplb = tmplb.toFixed(3);
			tmplb = tmplb.split(".");
			lb = tmplb[0];
			oz = (tmplb[1] / 1000) * 16;
			oz = Math.round(oz);
      		break;
		case "i":
			allg = big * 453.59237;
			alloz = allg * 0.03527;
			tmpkg = allg / 1000;
			tmpkg = tmpkg.toFixed(3);
			tmpkg = tmpkg.split(".");
			kg = tmpkg[0];
			g = tmpkg[1];
			g = Math.round(g);
			tmplb = allg * 0.0022046;
			tmplb = tmplb.toFixed(3);
			tmplb = tmplb.split(".");
			lb = tmplb[0];
			oz = (tmplb[1] / 1000) * 16;
			oz = Math.round(oz);
      		break;
	}
	switch(to){
		case "g":
			result = Math.round(allg);
		break;
		case "kg":
			result = kg;
		break;
		case "lb":
			result = lb;
		break;
	}
	return result;
}

function getBMI(objForm){
	var weight;
	var height;
	var bmi;
	var sex;
	var high;
	var low;
	var txt;

	if(!objForm.bmiweight.value || (!objForm.bmiheight1.value && !objForm.bmiheight2.value)){
		alert("Error, one or more fields are missing data. Please check your values and try again.");
	}else{
		for (counter = 0; counter < objForm.bmisex.length; counter++){
			if (objForm.bmisex[counter].checked)
				sex = objForm.bmisex[counter].value;
		}

		weight = convertWeight(objForm.bmiweight.value,objForm.bmiunit.value,"kg")
		height = convertMeasurement(objForm.bmiheight1.value,objForm.bmiheight2.value,objForm.bmiunit.value,"m");

		bmi = Math.round(eval(weight / (height * height)) * 100) / 100;
		txt = "Your BMI value is " + bmi + "\n\n";
		if(sex != "m"){
			low = 19;
			high = 25;
			txt += "As a woman, your BMI should fall in the range of 19 and 25 depending upon your build.\n";
		} else {
			low = 20;
			high = 26;
			txt += "As a man, your BMI should fall in the range of 20 to 26.\n";
		}
		txt += "\nBy keeping your BMI in these ranges you will enjoy better general health.\n";
		txt += "\nYour BMI value puts you into the ";
		if(bmi > 29){
			txt += "Obesity category which is defined as a body mass index greater than 29.\n";
		} else if(bmi > high) {
			txt += "Overweight category which is considered to be a BMI between " + high + " and 29.\n";
		} else if(bmi < 17) {
			txt += "Anorexia nervosa category which is associated with a body mass index of less than 17.\n";
		} else {
			txt += "normal BMI category.\n";
		}
		alert(txt);
	}
	return false;
}

function getHR(objForm){
	var thr;
	var age;
	var sex;
	var txt;

	if(!objForm.hrage.value){
		alert("Error, Age needs to be filled in.");
	}else{
		for (counter = 0; counter < objForm.hrsex.length; counter++){
			if (objForm.hrsex[counter].checked)
				sex = objForm.hrsex[counter].value;
		}

		age = objForm.hrage.value;

		if(sex != "m"){
			thr = 226 - age;
			txt = "As a woman at the age of "+age+", Your Maximum Heart Rate is "+thr+".\n";
		} else {
			thr = 220 - age;
			txt = "As a male at the age of "+age+", Your Maximum Heart Rate is "+thr+".\n";
		}
		txt += "\nYour training zones are:\n";
		txt += "\nHealthy Heart Zone (Warm up) --- 50 - 60% of maximum heart rate: "+Math.round(thr*0.5)+" - "+Math.round(thr*0.6)+"\n";
		txt += "\nFitness Zone (Fat Burning) --- 60 - 70% of maximum heart rate: "+Math.round(thr*0.6)+" - "+Math.round(thr*0.7)+"\n";
		txt += "\nAerobic Zone (Endurance Training) --- 70 - 80% of maximum heart rate: "+Math.round(thr*0.7)+" - "+Math.round(thr*0.8)+"\n";
		txt += "\nAnaerobic Zone (Performance Training) --- 80 - 90% of maximum heart rate: "+Math.round(thr*0.8)+" - "+Math.round(thr*0.9)+"\n";
		txt += "\nRed Line (Maximum Effort) --- 90 - 100% of maximum heart rate: "+Math.round(thr*0.9)+" - "+Math.round(thr)+"\n";
		alert(txt);
	}
	return false;
}

function changeAllSelect(object){
	var form = object.form;
	for (i = 0; i < form.length; i++){
		if((form[i].name.substr(0,object.name.length) == object.name) && (form[i].type == "select-one")) {
			if(form[i].name != object.name){
				form[i].selectedIndex = object.selectedIndex;
			}
		}
	}
}

function toggleRadio(element)
{
	var src = element.src;
	var ext = src.substring(src.length-3,src.length);
	var dir = src.substring(0,src.lastIndexOf("/"))+"/";
	var f=document.getElementById(element.alt).form;
	for(i=0; i < f.length; i++) {
		if(f[i].type == "radio") {
			if(f[i].name == element.alt) {
				if(f[i].value == element.id) {
					f[i].checked = true;
					document.getElementById(f[i].value).src = dir+f[i].value+"_on."+ext;
				}else{
					f[i].checked = false;
					document.getElementById(f[i].value).src = dir+f[i].value+"_off."+ext;
				}
			}
		}
	}
}

function toggleCheckBox(element)
{
	var checkbox=document.getElementById(element.alt);
	var src = element.src;
	var ext = src.substring(src.length-3,src.length);
	if(src.substring(src.length-7,src.length-4) == "off"){
		var img = src.substring(0,src.length-8);
	}else{
		var img = src.substring(0,src.length-7);
	}
	if (checkbox.checked == false)
	{
		checkbox.checked = true;
		element.src = img + "_on." + ext;
	} 
	else
	{
		checkbox.checked = false;
		element.src = img + "_off." + ext;		
	}
}