// new simple calculator script 
var myDays=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

// new simple calculator script 

var timer = null;
var display = getCookie("zf_display") ? getCookie("zf_display") : 0;
var dot = getCookie("zf_dot") ? getCookie("zf_dot") : 0;
var bracket = getCookie("zf_bracket") ? getCookie("zf_bracket") : 0;
var mem = getCookie("zf_mem") ? getCookie("zf_mem") : 0;
var rate = getCookie("zf_rate") ? getCookie("zf_rate") : 1;

// Event Calendar
var dA = new Array();
var tb = 't'; // top or bottom (t or b)
var headbg = '#cccccc';  // table heading background colour
var todaybg = '#ffffff'; // current selected date background colour
var textclr = '#000000'; // text colour
var linkclr = '#ffcccc'; // link text colour
var noMessage =  ''; // message to display when no entry in array

var drawStart;
var drawCounter=0;

function loaded(i,f) {if (document.getElementById && document.getElementById(i) != null) f(); else if (!pageLoaded) setTimeout('loaded(\''+i+'\','+f+')',100);
}

function init()
{
	if(document.calculator != null){
		if (document.calculator.digiwatch || document.calculator.datewatch) start();
		showDisplay();
		document.onunload = stopcalc;
		document.onkeyup = keyboard;
	}
}



function showDisplay()
{
	document.calculator.expr.value = display;
	var lcdSymbols = (mem != 0) ? 'M' : '&nbsp;';
	document.getElementById('lcd-symbols').innerHTML = lcdSymbols;
}

function keyboard(evt)
{
    if (!evt) {
		if (window.event) evt = window.event;
		else return;
	}
	if (typeof(evt.keyCode) == 'number') {
		key = evt.keyCode; // DOM
	}
	else if (typeof(evt.which) == 'number') {
		key = evt.which; // NS4
	}
	else if (typeof(evt.charCode) == 'number') {
		key = evt.charCode; // NS 6+, Mozilla 0.9+
	}
	else return;
	if ((key < 106) && (key > 95)) enter(key - 96);
    if ((key < 58) && (key > 47)) enter(key - 48);
    if ((key == 8) || (key == 46) || (key == 67)) clearDisplay();
    if (key == 13) calc();        // "Enter"
    if ((key == 110) || (key == 188) || (key == 190)) enter(".");
    if ((key == 107) || (key == 187)) enter("+");
    if ((key == 109) || (key == 189)) enter("-");
    if ((key == 106) || (key == 191)) enter("*");
    if (key == 111) enter("/");   // "/" 
    if (key == 77) pop_mem();     // "M" 
    if (key == 83) push_mem();    // "S" 
    if (key == 82) set_rate();    // "R" 
    if (key == 84) to_rate();     // "T" 
    if (key == 89) from_rate();   // "Y" 
}

function setCookie(name, value)
{
	var curCookie = name + "=" + value + "; expires=" + "Wednesday, 09-Nov-50 23:12:40 GMT";
	document.cookie = curCookie;
}

function getCookie(name)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
}

function error(errorstring)
{
	alert("Calculator error:\n" + errorstring);
}

function enter(string)
{
	if (document.calculator.expr.value == '0') {
		if (string == "(") bracket ++;
		if (string == ".") dot ++;
		else dot = 0;
		if ((string == ")") || (string == "e")) {
			error("Illegal entry");
			string = "0";
		}
		document.calculator.expr.value = string;
		if (string == "*" || string == "/" || string == ".") {
			document.calculator.expr.value = "0" + string;
		}
	}
	else {
		disp = new String(document.calculator.expr.value);
		last = disp.substring(disp.length - 1, disp.length);
		if (last == "(") {
		    if (string == '.') dot ++;
			if (string == "*" || string == "/" || string == ")" || string == "e") {
				error("Illegal entry");
				string = "";
			}
			if (string == "(") bracket ++;
		}
		if (last == ")") {
			if (string == '.') dot ++;
			if (string == "*" || string == "/" || string == "-" || string == "+") {
				print(string);
				string = "";
			}
			else {
				if (((string != "") && (bracket < 0)) || (string == ".")) {
					error("Illegal entry");
					string = "";
				}
			}
			if (string == ")") {
				bracket --;
				if (bracket < 0) {
					calc();
					string = '';
				}
			}
		}
		if (last=="e") {
			if (string == '.') {
				error("Illegal entry");
				string = "";
				dot = 0;
			}
			if (string == "*" || string == "e" || string == "/" || string == "(" || string == ")") {
				error("Illegal entry");
				dot = 0;
				string = "";
			}
		}
		if (last == ".") {
			if (string == '.') {
				error("Illegal entry");
				string = "";
				dot = 0;
			}
			if (string == "*" || string == "." || string == "e" || string == "/" ||
				string == "-" || string == "+" || string == "(" || string == ")") {
				error("Illegal entry");
				string = "";
			}
		}
		if (last == "+" || last == "-" || last == "*" || last == "/") {
			if (string == '.') {
				dot ++;
				if (dot > 1) {
					error("Illegal entry");
					string="";
					dot = 0;
				}
			}
			if (string == "e" || string == ")" || string == "*" || string == "/" ||
				string == "-" || string == "+") {
				error("Illegal entry");
				string = "";
			}
			if (string == "(") {
				bracket ++;
				print(string);
				string = "";
			}
		}
		if (last=="0" || last=="1" || last=="2" || last=="3" || last=="4" ||
			last=="5" || last=="6" || last=="7" || last=="8" || last=="9") {
			if (string == '.') {
				dot ++;
				if (dot > 1) {
					error("Illegal entry");
					string = "";
				}
			}
			if (string == "*" || string=="e" || string=="/" || string=="-" || string=="+" || string==")") dot=0;
			if (string == "(") {
				error("Illegal entry");
				string = "";
				dot = 0;
			}
			if (string == ")") {
				bracket --;
				if (bracket < 0) {
					calc();
					dot = 0;
					string = "";
				}
			}
		}
		print(string);
	}
}

function print(string)
{
	document.calculator.expr.value = document.calculator.expr.value + string;
}

function evaluate()
{
	try {
		var val = eval(document.calculator.expr.value);
	}
	catch(e) {
		error("Illegal entry");
		val = document.calculator.expr.value;
	}
	return val;
}

function calc()
{
	document.calculator.expr.value = evaluate();
	bracket = 0;
	dot = 0;
}

function clearDisplay()
{
	document.calculator.expr.value = "0";
	bracket = 0;
	dot = 0;
}

function set_rate()
{
	rate = evaluate();
	document.calculator.expr.value = rate;
	setCookie("zf_rate", rate);
}

function from_rate()
{
	document.calculator.expr.value = rate * evaluate();
}

function to_rate()
{
	document.calculator.expr.value = evaluate() / rate;
}

function push_mem()
{
	document.calculator.expr.value = evaluate();
	mem = evaluate();
	setCookie("zf_mem", mem);
	document.getElementById('lcd-symbols').innerHTML = 'M';
}

function clear_mem()
{
	mem = '0';
	setCookie("zf_mem", mem);
	document.getElementById('lcd-symbols').innerHTML = '&nbsp;';
}

function pop_mem()
{
	if (mem != 0) {
		enter(mem)
	}
}

function plus_mem()
{
	document.calculator.expr.value = evaluate();
	mem = evaluate() + mem;
	setCookie("zf_mem", mem);
	document.getElementById('lcd-symbols').innerHTML = 'M';
}

function minus_mem()
{
	document.calculator.expr.value = evaluate();
	mem = mem - evaluate();
	setCookie("zf_mem", mem);
	document.getElementById('lcd-symbols').innerHTML = 'M';
}

function power(y)
{
	document.calculator.expr.value = eval(Math.pow(evaluate(), y));
}

function power_x(y)
{
	document.calculator.expr.value = eval(Math.pow(y, evaluate()));
}

function power_m()
{
	document.calculator.expr.value = evaluate();
	document.calculator.expr.value = eval(Math.pow(evaluate(), eval(mem)));
}

function fac()
{
	x = evaluate();
	if ((x < 0) || ((x - Math.floor(x)) != 0)) {
        document.calculator.expr.value = 'Error'
	}
    else {
		if ((x == 0) || (x == 1)) {
			y = '1';
			document.calculator.expr.value = eval(y);
		}
		else {
			y = '1';
			for (i = 1; i <= x; i ++) {
				y = y * i;
			}
            document.calculator.expr.value = eval(y);
		}
	}
}

function sin()
{
	document.calculator.expr.value = eval(Math.sin(evaluate()));
}

function cos()
{
	document.calculator.expr.value = eval(Math.cos(evaluate()));
}

function tan()
{
	document.calculator.expr.value = eval(Math.tan(evaluate()));
}

function invsin()
{
	document.calculator.expr.value = 
		eval(Math.atan2(evaluate(), Math.sqrt(1 - Math.pow(evaluate(), 2))));
}

function invcos()
{
	document.calculator.expr.value =
		eval(Math.atan2(Math.sqrt(1 - Math.pow(evaluate(), 2)), evaluate()));
}

function invtan()
{
	x = evaluate();
	document.calculator.expr.value =
		eval(Math.atan2(x / Math.sqrt(1 + Math.pow(x, 2)), 1 / Math.sqrt(1 + Math.pow(x, 2))));
}

function asin()
{
	document.calculator.expr.value = eval(Math.asin(evaluate()));
}

function acos()
{
	document.calculator.expr.value = eval(Math.acos(evaluate()));
}

function atan()
{
	document.calculator.expr.value = eval(Math.atan(evaluate()));
}

function ln()
{
	document.calculator.expr.value = eval(Math.log(evaluate()));
}

function log()
{
	document.calculator.expr.value = eval(Math.log(evaluate()) / Math.LN10);
}

function log()
{
	document.calculator.expr.value = eval(Math.log(evaluate()) / Math.LN10);
}

function inv()
{
	calc();
	document.calculator.expr.value = '1/' + document.calculator.expr.value;
	document.calculator.expr.value = evaluate();
}

function neg()
{
	calc();
	document.calculator.expr.value = -evaluate();
}

function stopcalc()
{
	setCookie("zf_display", document.calculator.expr.value);
	clearTimeout(timer);
}

function start()
{
	var time = new Date();
	var date = time.getDate();
	var month = time.getMonth() + 1;
	var year = time.getYear() % 100;
	var hours = time.getHours();
	var minutes = time.getMinutes();
	var seconds = time.getSeconds();
	year += (year < 38) ? 2000 : 1900;
	year = year - ((year > 100) ? ((Math.floor(year/100))*100) : 0);
	year = ((year < 10) ? "0" : "") + year;
	month = ((month < 10) ? "0" : "") + month;
	date = ((date < 10) ? "0" : "") + date;
	hours = ((hours < 10) ? "0" : "") + hours;
	minutes = ((minutes < 10) ? "0" : "") + minutes;
	seconds = ((seconds < 10) ? "0" : "") + seconds;
	var clock = hours + ":" + minutes + ":" + seconds;
	var calendar = year + "-" + month + "-" + date;
	if(document.calculator != null){
		if (document.calculator.digiwatch) document.calculator.digiwatch.value = clock;
		if (document.calculator.datewatch) document.calculator.datewatch.value = calendar;
	}
	var timer = setTimeout("start()", 1000);
}

function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

addLoadEvent(init); // Attach the setup function without interfering with other scripts

//Script of Mortgage calculator
var num=0;
var amt=0;
var per=0;
var months=0;
var nls="";
var rr="\r";
var b="                               ";
var d="---------------------------------------------------"
+"----------------------------------";
var s="";
function iA(){
 this.length=iA.arguments.length;
 for (var i=0;i<this.length;i++){
  this[i]=iA.arguments[i];
  }
 }
var pwr=new iA(10);
var dec=new iA(16);
pwr[0]=1;
for (var i=0;i<9;i++){
 pwr[i+1]=pwr[i]*10;
}
dec[0]=.1;
dec[1]=.01;
dec[2]=.001;
dec[3]=.0001;
dec[4]=.00001;
dec[5]=.000001;
dec[6]=.0000001;
dec[7]=.00000001;
dec[8]=.000000001;
dec[9]=.0000000001;
dec[10]=.00000000001;
dec[11]=.000000000001;
dec[12]=.0000000000001;
dec[13]=.00000000000001;
dec[14]=.000000000000001;
dec[15]=.0000000000000001;
   
var ns="01234567890";
var cr="";
var str="";

function stn(){
 num=0;
 pos=str.indexOf(".");
 sfx="";
 if (pos>-1){
  sfx=str.substring(pos+1,str.length);
  str=str.substring(0,pos);
  }
 strl=str.length;
 for (var i=strl-1;i>-1;i--){
  cr=str.substring(i,i+1);
  pos=ns.indexOf(cr);
  num+=pos*pwr[strl-i-1];
  }
  if (sfx!=""&&sfx.length>dp){
   pos=ns.indexOf(sfx.charAt(dp+1));
   if (pos>4){
    pos=ns.indexOf(sfx.charAt(dp));
    sfx=sfx.substring(0,dp-1)+(pos+1);
    }
   }
  if (sfx!=""){
   for (var i=0;i<dp;i++){
    cr=sfx.substring(i,i+1);
    pos=ns.indexOf(cr);
    num+=pos*dec[i];
    }
/*    sfx="";
    sfx+=num;
    pos=sfx.indexOf(".");
    sfx=sfx.substring(pos+1,sfx.length);
    if (sfx.charAt(dp+1)=="9"){
     num+=dec[sfx.length-2];
     } */
  }
 }

function testIt(form){
 str=document.isn.amt.value;
 fmtIt();
 bl=str.length+3;
 dp=2;
 stn();
 amt=num;
 str=document.isn.per.value;
 dp=4;
 stn();
 per=num;
 str=""+(document.isn.months.value*12);
 dp=0;
 stn();
 months=num;
 if (months<1||months>999||per<.0001||per>99||amt<1||amt>pwr[9]){
  alrt();
  }
 else{
 computeForm();
 }
}

function computeForm(){
 ls="";
 isnnum=1;
 i=per/12/100;
 fpv=0;
 for (var j=0;j<months;j++)
  isnnum=isnnum*(1+i);
  tmp=(amt*isnnum*i)/(isnnum-1);
  fpv+=tmp;
  fcalc=((months*fpv)-amt);
  
 prtSched();
 }

function prtSched(){
 fpv+=.01;
 str="";
 str+=fpv;
 fmtIt();
 dp=2;
 stn();
 fpv=num;
 pct=per/12/100;
 if (bl<14){
  bl=14;
  }
 ls="Plan de amortización de hipoteca a "+document.isn.months.value
 +" años y capital de "+s+document.isn.amt.value
 +" al "+document.isn.per.value+"%"+rr+d+rr
 +"Núm.       Cuota       Interés      Capital     Pendiente"
 +rr+d+rr;
 for (var j=0;j<months;j++){
  ntr=(amt*pct);
  str="";
  str+=ntr;
  fmtIt();
  ntr1=s+str;
  prp=fpv-ntr;
  if (prp>amt){
   prp=amt;
   }
  str="";
  str+=prp;
  fmtIt();
  prp1=s+str;
  amt-=prp;
  str="";
  str+=amt;
  fmtIt();
  amt1=s+str;
  if (fpv>(ntr+prp)){
   fpv=ntr+prp;
   }
  str="";
  str+=fpv;
  fmtIt();
  fpv1=s+str;
  str="";
  str+=(j+1)+".";
  ls+=b.substring(0,2)+str+b.substring(0,12-str.length)
  +fpv1+b.substring(0,14-fpv1.length)+ntr1
  +b.substring(0,14-ntr1.length)+prp1
  +b.substring(0,14-prp1.length)+amt1
  +rr;
  }
  document.isn.sched.value=ls+d+rr
  +"  "
  +rr+"    ";
 }
function fmtIt(){
 pos=str.indexOf(".");
 if (pos==0){
  str="0"+str;
  pos++;
  }
 if (pos<0){
  str+=".00";
  pos=str.indexOf(".");
  }
 str+="0000";
 str=str.substring(0,pos+4);
 cr=str.charAt(str.length-1);
 pos=ns.indexOf(cr);
 str=str.substring(0,str.length-1);
 if (pos>5){
  fmtIt1();
  }
 }
 function fmtIt1(){
  for (var k=str.length-1;k>-1;k--){
   cr=str.charAt(k);
   posn=ns.indexOf(cr);
   if (posn<0){
    k--;
   }
   else{
    str=str.substring(0,k)+ns.substring(posn+1,posn+2)
    +str.substring(k+1,str.length);
    if (posn!=9){
     k=-1;
    }
   }
  }
 }
function alrt(){
 alert("You couldn't know. Years must be from"
 +" 1 to 999, Loan amount from 1 to "+pwr[9]
 +" and Interest from .001 to 99%");
}
//-->


//Script of ovulation calculator	
	function ovulation_date() 
	{
	// Get input values	
		var d = parseInt(document.getElementById('day').value);
		var m = parseInt(document.getElementById('month').value);
		var y = parseInt(document.getElementById('year').value);
		var c = parseInt(document.getElementById('cycle').value);
		
		if(c>65 | c<14){
			alert("Number of days in your menstrual cycle must be between 14 and 65 days");
			return false;
		}
		
	// Get the start of the ovulation    
		var starto = new Date();
		
		starto.setYear(y);
		starto.setMonth(m-1);	
		starto.setDate(d);
		var daymsecs = 60*60*24*1000;
		starto.setTime(starto.getTime() + ((c * daymsecs) - daymsecs*14));

		y = starto.getYear()+1900;
		if ((y > 100) && (y <1900)) y+=1900;
		if (y == 3900) y = 2000;
		m = starto.getMonth()+1;
		d = starto.getDate();
		
	// Report the result	
		var ovulationDate = new Date("" +m + "/" +d + "/" + y);
		
		var dayOfOvulation= ovulationDate.getDay(ovulationDate);
		document.getElementById('result').value = myDays[dayOfOvulation]+", "+d + "/" +m + "/" + y;
		
		
		starto.setTime(starto.getTime() - daymsecs*3);
		y = starto.getYear()+1900;
		if ((y > 100) && (y <1900)) y+=1900;
		if (y == 3900) y = 2000;
		m = starto.getMonth()+1;
		d = starto.getDate();
		var startDate=new Date(m+ "/" +d + "/" + y);
		var fertileStartDay=myDays[startDate.getDay()];
		var fertilespan = "between " +fertileStartDay+", "+ d+ "/" +m + "/" + y + " and ";	
		
		
		drawStart= d+ "/" +m + "/" + y;
		
		starto.setTime(starto.getTime() + daymsecs*5);
		y = starto.getYear()+1900;
		if ((y > 100) && (y <1900)) y+=1900;
		if (y == 3900) y = 2000;
		m = starto.getMonth()+1;
		d = starto.getDate();
		var endDate=new Date(m+ "/" +d + "/" + y);
		var fertileEndDay=myDays[endDate.getDay()];
		fertilespan += fertileEndDay+", "+ d+ "/" +m + "/" + y;	
		
		var startY=y;
		var startM=m;
		
		document.getElementById('fertile').value = fertilespan;
		
		
		// start calendar script
		var x = 0;
		// draw the calendar
		var pageLoaded = 0; window.onload = function() {pageLoaded = 1;}
		loaded('calendar',start);
		// display the correct year and month
		if (cala = dispCal(startY,startM)) {document.getElementById('cal').innerHTML = cala;}
		// make legend visible
		document.getElementById("legend").style.visibility='visible';
		
	}

	
	//Script of BMI calculator
	function BMICalc (form) {
	var S, W, H
	S=form.Sex.options[form.Sex.selectedIndex].value
	W=form.Weight.value/form.WeightUnit.options[form.WeightUnit.selectedIndex].value
	H=form.Height.value*form.HeightUnit.options[form.HeightUnit.selectedIndex].value/100
	form.BMI.value = Math.round(W/Math.pow(H,2)*10)/10
	  if (S == "Male") {
		 form.Ave.value= 25.9;
	}
	  if (S == "Female") {
		 form.Ave.value= 24.9;
	}
	  if (form.BMI.value >= 40) {
		 form.Class.value ="Obesidad m\u00f3rbida - Grado 3"
	}
	  if (form.BMI.value < 40 && form.BMI.value >= 35) {
		 form.Class.value ="Obesidad - Grado 2"
	}
	  if (form.BMI.value < 35 && form.BMI.value >= 30) {
		 form.Class.value ="Obesidad - Grado 1"
	}
	  if (form.BMI.value < 30 && form.BMI.value >= 25) {
		 form.Class.value ="Sobrepeso"
	}
	  if (form.BMI.value < 25 && form.BMI.value >= 18.5) {
		 form.Class.value ="Saludable"
	}
	  if (form.BMI.value < 18.5) {
		 form.Class.value ="Peso por debajo lo ideal"
	}
	}

	

// Script of Ideal Weight calculator 
	function CalcIt(form) {
	var Age = Number(form.Years.value);

	var weight =  Number(form.wt.value);

	if (!checkWeight(weight)) return false;
	if (form.wu.selectedIndex == 0) {	// 0 = weight in lbs
		kg = weight * 0.45359237;
	} else {								// 1 = weight in kg.
		kg = weight;
	}
	if (kg < 10) {
		alert("Weights should be heavier than 10 kilograms (22 pounds).");
		return false;
	}
	if (kg > 200) {
		alert("Weights should be lighter than 200 kilograms (441 pounds).");
		return false;
	}


	var height =  Number(form.ht.value);

	if ((isNaN(height)) || (height == null)  || (height == "") || (height < 0)) {
		feetAndInches(form);
		height =  Number(form.ht.value);
	}

	if (form.hu.selectedIndex == 0) {		//  if height units are "inches"
		heightInches = height;
		heightMeters = height * 2.54 / 100;
	} else {									// else if height units are "cm".
		heightInches = height / 2.54;
		heightMeters = height / 100;
	}
	if (heightMeters < 0.33) {
		alert("Heights should be taller than 33 centimeters (31.5 inches).");
		return false;
	}
	if (heightMeters > 2.41) {
		alert("Heights should be shorter than 241 centimeters ( 7 feet, 11 inches).");
		return false;
	}
	setFeetAndInches(form,heightInches);


	if ((isNaN(Age)) || (Age == null)  || (Age == "")) {
		Age = GetAge(form);
		if (form.AgeCat.selectedIndex == 26) {
			alert("Please enter a specific age for a child.");
			return false;
		}
	} else {
		if ( Age < 1 ) {
			alert("Ages younger than 1 year old are too young for this calculation. Sorry.");
			return false;
		} else {
			if (Age > 120) {
				alert("All ages from 70 to 120 are treated as age 75.");
				return false;
			}
		}
		if (Age > 18) {
			form.AgeCat.selectedIndex = 25;	// Set menu to "Adult"
		} else {
			form.AgeCat.selectedIndex = 26;	// Set menu to "Child".
		}
	}
 


	// Calculate BMI
	bmi = kg / Math.pow(heightMeters,2);
 	//form.bmi.value = rounding(bmi,1);
 


// Calculate IBW using Peoples Choice.  Copyrighted.
	var Gend = form.Gender.selectedIndex;  // Male=0,  Female=1
	var PeopBMI;
	if ( Gend == 0) {
		PeopBMI = 0.5*bmi + 11.5;
	} else {
		PeopBMI = 0.4*bmi + 0.03*Age + 11;
	}
	// Don't steal these copyrighted formulas.  Get permission or get sued.
	if (Age > 18.5 || heightInches > 69) {	// Use Peoples Choice for Adults
		if (form.wu.selectedIndex == 0) {  // 0 = weight in lbs
			form.idealPeoples.value = Math.round( (PeopBMI * (heightMeters*heightMeters)) * 2.2046 ) + " lbs";
		} else {  // 1 = weight in kg.
			form.idealPeoples.value = Math.round( (PeopBMI * (heightMeters*heightMeters)) ) + " kgs";
		}
	} else {	// Age < 18,  use 50th percentile.
		if ( Gend == 0 ) {  // Male
			if ( form.wu.selectedIndex == 0) {  // Weight in lbs
				form.idealPeoples.value = Math.round(0.0982*heightInches*heightInches - 6.6836*heightInches + 144.6) + " lbs";
			} else { 
				form.idealPeoples.value = Math.round(0.0445*heightInches*heightInches - 3.0317*heightInches + 65.591) + " kgs";
			}
		} else {		// Female
			if ( Age < 12 || heightInches < 63) {
				if ( form.wu.selectedIndex == 0) {  // Weight in lbs  
					form.idealPeoples.value = Math.round(0.0697*heightInches*heightInches - 3.9646*heightInches + 81.378) + " lbs";
				} else {  // y = 0.0316x2 - 1.7983x + 36.913
					form.idealPeoples.value = Math.round(0.0316*heightInches*heightInches - 1.7983*heightInches + 36.913) + " kgs";
				}
			} else {
				if ( form.wu.selectedIndex == 0) {  // Weight in lbs  y = 0.0167x2 + 2.8845x - 135.65
					form.idealPeoples.value = Math.round(0.0167*heightInches*heightInches + 2.8845*heightInches - 135.65) + " lbs";
				} else { // y = -0.0237x2 + 5.0882x - 174.91
					form.idealPeoples.value = Math.round(-0.0237*heightInches*heightInches + 5.0882*heightInches - 174.91) + " kgs";
				}
			}
		} 
	}
return null;
}

function GetAge(form) {
	if (form.AgeCat.selectedIndex == 0) { // 70+ years,
		return 75;
	} else {
		if (form.AgeCat.selectedIndex < 6) { // 1 thru 5 are decades 20s thru 60s.
			return 65 - (form.AgeCat.selectedIndex -1)*10;
		} else {
			if (form.AgeCat.selectedIndex == 6 ) {
				return 19;
			} else {
				if (form.AgeCat.selectedIndex < 23) { // 7 thru 22 are ages 17 thru 2.
					return 17 - (form.AgeCat.selectedIndex-7);
				} else {
					if (form.AgeCat.selectedIndex == 23) { // age 1.5 yrs.
						return 1.5;
					} else {
						if (form.AgeCat.selectedIndex == 24) {	// age 1 yrs.
							return 1;
						} else {
							if (form.AgeCat.selectedIndex == 25) {
								return 30;
							} else {
								return 13;
							}
						}
					}
				}
			}
		}
	}
}

function SetAge(form) {
	if (form.Years.value > 0) {	// Only change the Age field, if a value is already there.
	
		if (form.AgeCat.selectedIndex == 0) { // 70+ years,
			form.Years.value = 75;
		} else {
			if (form.AgeCat.selectedIndex < 6) { // 1 thru 5 are decades 20s thru 60s.
				form.Years.value = 65 - (form.AgeCat.selectedIndex -1)*10;
			} else {
				if (form.AgeCat.selectedIndex == 6 ) {
					form.Years.value = 19;
				} else {
					if (form.AgeCat.selectedIndex < 23) { // 7 thru 22 are ages 17 thru 2.
						form.Years.value = 17 - (form.AgeCat.selectedIndex-7);
					} else {
						if (form.AgeCat.selectedIndex == 23) { // age 1.5 yrs.
							form.Years.value = 1.5;
						} else {
							if (form.AgeCat.selectedIndex == 24) {	// age 1 yrs.
								form.Years.value = 1;
							} else {
								if (form.AgeCat.selectedIndex == 25) {
									form.Years.value = 30;
								} else {
									if (form.Years.value > 19) form.Years.value = "";
								}
							}
						}
					}
				}
			}
		}
	}
	return true;
}

function setFeetAndInches(form,inchies) {
	var feet = Math.min( Math.max( Math.floor( inchies / 12 ), 1), 7);
	form.htf.selectedIndex = feet - 1;

	inchies = rounding( inchies - feet*12,0);
	form.hti.selectedIndex = Math.min( Math.max( inchies,0), 11 );
	return true;
}

function feetAndInches(form) {
	var inchies = 0;
	inchies = ((form.htf.selectedIndex+1) * 12) + form.hti.selectedIndex;

	if (form.hu.selectedIndex == 0) form.ht.value = inchies;
	if (form.hu.selectedIndex == 1) form.ht.value = rounding( inchies * 2.54,0);
	return true;
}


function SetPercentile( pc ) {
	if (pc > 98) pc = "> 98th percentile";
	else  {  if (pc < 2) pc = "< 2nd percentile";
		else { 
			if ( (pc > 10) && (pc < 14) ) { pc = pc + "th percentile"; }
			else { if ( rightDigit(pc) == 1) { pc = pc + "st percentile"; }
			else { if ( rightDigit(pc) == 2) { pc = pc + "nd percentile"; }
			else { if ( rightDigit(pc) == 3) { pc = pc + "rd percentile"; }
			else pc = pc + "th percentile";
					}
				}
			}
		}
	}
	return pc;
}

function poundsAndKilos(form) {
	var weight = Number(form.wt.value);
	if ( weight > 0) {
		if (form.wu.selectedIndex == 0) {	// 0 = pounds.
			form.wt.value = rounding( weight / 0.45359237,0);
		} else {								// 1 = kilograms.
			if (weight > 219) {
				form.wt.value = rounding( weight * 0.45359237,0);
			} else {
				form.wt.value = rounding( weight * 0.45359237,1);
			}
		}
		form.wt.select()
		form.wt.focus()
	}
	return true;
}

function inchesCm(form) {
	var height = Number(form.ht.value);
	if (height > 0) {
		if (form.hu.selectedIndex == 0) { // is now inches, was cm.
			setFeetAndInches(form, height / 2.54); 
			form.ht.value = rounding( height / 2.54,1) ; 
		} else {								// is now cm, was inches.
			setFeetAndInches(form, height);  // Always pass inches in height to this function.
			form.ht.value = rounding( height * 2.54,0);  }  
		form.ht.select()
		form.ht.focus()
	}
	return true;
}

function rightDigit(num) {
	num = num - (Math.floor(num/10)*10);
	return num;
}

function rounding(number,decimal) {
	multi = Math.pow(10,decimal);
	number = Math.round(number * multi) / multi;
	return number;
}


function checkWeight(val) {
	if ((isNaN(val)) || (val == null)  || (val == "") || (val < 0)) {
		alert( "Please enter a value for Weight.");
		return false;
	}
	return true;
}

function microsoftKeyPress() {
    if (window.event.keyCode == 13) {
	CalcIt(document.forms[0]);
    }
    return true;
}




// new ideal weight calculator script

function weight1calculate()
{
  var heightunits = document.weight1.heightunits.options[document.weight1.heightunits.selectedIndex].value;
  var height = document.weight1.height.value;
	if(!IsNumericValue(height)){
		alert("Please enter a vaild number in height field");
		return false;
	}
  if (heightunits == "inches")
    {
        height *= 2.54;
    }

  var sex = document.weight1.sex.options[document.weight1.sex.selectedIndex].value;

  a = height / 100;
  a = a * a;

  if (sex == "Male")
    {
      w1 = a * 20.1;
      w2 = a * 25.0;
      w3 = w1 / 0.4536;
      w4 = w2 / 0.4536;
    }
  else
    {
       w1 = a * 18.7;
       w2 = a * 23.8;
       w3 = w1 / 0.4536;
       w4 = w2 / 0.4536;
    }
       
  var min = nt2dp(w1,1);
  var max = nt2dp(w2,1);
  document.weight1.avg.value= "Entre "+min+" y "+max;
}

function nt2dp(num,dp)
{
  num=num*1+(0.55/Math.pow(10,dp));
  if (dp>0) dp=dp+1;
  b=Math.floor(num).toString().length+dp;
  return num.toString().substr(0,b);
}



function IsNumericValue(input)
{
   return (input - 0) == input && input.length > 0;
}


function loaded(i,f) {if (document.getElementById && document.getElementById(i) != null) f(); else if (!pageLoaded) setTimeout('loaded(\''+i+'\','+f+')',100);
}
function monthlength(month,year) {var dd = new Date(year, month, 0);return dd.getDate();}
var moy = ['January','February','March','April','May','June','July','August','September','October','November','December'];var today = new Date();var selDate = today.getFullYear()+getmmdd(today.getMonth()+1,today.getDate());
function dispCal(yy,mm) {
	if (mm < 0 || mm > 12) {
	alert('month must be between 1 and 12'); 
	return false;
	} 
	if (yy != 0 && (yy < 1901 || yy > 2100)) {
	alert('year must be after 1900 and before 2101'); 
	return false;
	} 
	var dow = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']; 
	var calendar = new Date();  var today = calendar.getDate(); 
	calendar.setDate(1); if (yy > 1900) calendar.setFullYear(yy); 
	if (mm > 0) 
		calendar.setMonth(mm - 1); 
	var yy = year = calendar.getFullYear(); 
	var month = calendar.getMonth(); 
	calendar.setDate(today); 
	var weekday = calendar.getDay(); 
	var daysInMonth = monthlength(month+1,year); 
	var hilite_start = '<td width="30" style="background:' + todaybg + '" align="center">'; 
	var td_start = '<td width="30" align="center">'; 
	var cal = '<div id="cal"><div style="border:1pt solid #cccccc;width:238px"><table border="0" cellspacing="0" cellpadding="2" align="center"><tr><td colspan="7" style="background:' + headbg + '" align="center"><b>' + moy[month]  + ' ' + year + '<\/b><\/td><\/tr><tr>'; 
	for(dex=0; dex < 7; dex++) {
		cal += td_start + dow[dex] + '</td>';
	} 
	cal += '<\/tr><tr>'; 
	var day2 = today; 
	for (dex = today; dex > 6; dex -=7) 
		day2 = dex; weekday -= day2 - 1; 
		while (weekday < 0) weekday += 7; 
			for(dex=0; dex < weekday; dex++) cal += td_start + ' <\/td>'; 
				for(dex=1; dex <= daysInMonth; dex++) {
					if(weekday == 7) {
						cal += '</tr><tr>'; weekday = 0;
					} 
					if(selDate==year+getmmdd(month+1,dex)) 
						cal += hilite_start +'<span '+clickDate(dex,month,year) + '>'+ dex + '<\/span><\/b><\/td>'; 
					else 
						cal += td_start + '<span '+clickDate(dex,month,year) + '>' + dex + '<\/span><\/td>'; 
						weekday += 1;
				} 
				for(dex=weekday; dex < 7; dex++) 
					cal += td_start + ' <\/td>'; 
					cal += '<\/tr><\/table><\/div>';
					if (document.getElementById) {
						var mmb = month;  mm = month + 1; 
						var yya = yyb = yy; 
						if (mmb <1) {
							mmb += 12; yyb--;
						} 
						var mma = month + 2; 
						if (mma > 12) {
							mma -= 12; yya++;
						} 
						var yb = yy -1; 
						var ya = yy +1; 
						cal += '<table style="height:25px;" border="0" cellspacing="0" cellpadding="2" width="210"><tr><td></td><td><a href="#" onclick="if (cala = dispCal('+yyb+','+mmb+')) {document.getElementById(\'cal\').innerHTML = cala; return false;}"><</a></td><td align="right"><a href="#" onclick="if (cala = dispCal('+yya+','+mma+')) {document.getElementById(\'cal\').innerHTML = cala; return false;}">></a></td><td align="right"></td></tr></table>';
						} 
						else {
							cal += '<div> </div>';
						} 
						cal += '</div>';
						return cal;
}

function start() {
	var x = '<div id="calDate" style="border:1pt solid #cccccc;width:238px"><\/div>'; 
	var y = ''; 
	if (tb == 't') 
		y = x + dispCal(0,0); 
	else y = dispCal(0,0) + x; 
		document.getElementById('calendar').innerHTML = y; ev();
}


function clickDate(day, month, year) {
var ct = nextDate(year + getmmdd(month+1,day));
if (ct == '') 
	ct = nextDate('0000' + getmmdd(month+1,day));
if (ct == '') 
	//return 'style="color:'+textclr+'"'; 
var ovDate=day+"/"+(month+1)+"/"+year;

//if(ovDate=="18/6/2010")
if(ovDate==drawStart){
	drawCounter=drawCounter+1;
	return 'style="padding:4px;cursor:pointer;background-color:#FF99FF;color:#000000;" ';
}

else if(drawCounter>0){
	
	if(drawCounter==3){
		drawCounter=drawCounter+1;
		return 'style="padding:4px;cursor:pointer;background-color:#FF6699;color:#000000;" ';
		}
	else{
		if(drawCounter<6){
			drawCounter=drawCounter+1;
			return 'style="padding:4px;cursor:pointer;background-color:#FF99FF;color:#000000;" ';
		}
		else{
			drawCounter=0;
		}
	}
	
	
}
//else 
	//return 'style="padding:4px;cursor:pointer;background-color:#99cccc;color:#000000;" ';
}
function isDate(dayVal,monthVal,yearVal) {var ct = nextDate(yearVal + getmmdd(monthVal+1,dayVal));if (ct == '') ct = nextDate('0000' + getmmdd(monthVal+1,dayVal));if (ct == '') ct = noMessage;document.getElementById('calDate').innerHTML = selDate + ':<br \/>' +ct; return false;}function nextDate(yymmdd) {var x = dA.length;for (var i = 0; i < x; i++) {if (dA[i].substr(0,8) == yymmdd) return dA[i].substr(8);}return '';}function getmmdd(mm,dd) {return (mm > 9 ? '' + mm : '0' + mm) + (dd > 9 ? dd : '0' + dd);}
function ev() {var ct = nextDate(selDate);var ct = nextDate(selDate);if (ct == '') ct = nextDate('0000' + selDate.substr(4));if (ct == '') ct = noMessage; document.getElementById('calDate').innerHTML = selDate + ':<br \/> ' +ct;}




