//pre-load image for printable
Image1= new Image(312,245);
Image1.src = "../../images/new_garza.png";

var state = 'block'; 

function showhide(mylayer_ref1,mylayer_ref2,mylayer_ref3,mylayer_ref4,mylayer_ref5) { 

if (state == 'none') { 
state = 'block'; 
} 
else { 
state = 'none'; 
} 
if (document.all) { //IS IE 4 or 5 (or 6 beta) 
eval( "document.all." + mylayer_ref1 + ".style.display = state"); 
eval( "document.all." + mylayer_ref2 + ".style.display = state"); 
eval( "document.all." + mylayer_ref3 + ".style.display = state"); 
eval( "document.all." + mylayer_ref4 + ".style.display = state"); 
eval( "document.all." + mylayer_ref5 + ".style.display = state"); 
} 
if (document.layers) { //IS NETSCAPE 4 or below 
document.layers[mylayer_ref1].display = state; 
document.layers[mylayer_ref2].display = state; 
document.layers[mylayer_ref3].display = state; 
document.layers[mylayer_ref4].display = state; 
document.layers[mylayer_ref5].display = state; 
} 
if (document.getElementById &&!document.all) { 
hza1 = document.getElementById(mylayer_ref1); 
hza1.style.display = state; 
hza2 = document.getElementById(mylayer_ref2); 
hza2.style.display = state; 
hza3 = document.getElementById(mylayer_ref3); 
hza3.style.display = state; 
hza4 = document.getElementById(mylayer_ref4); 
hza4.style.display = state; 
hza5 = document.getElementById(mylayer_ref5); 
hza5.style.display = state; 
} 
} 


function openWin1( windowURL, windowName, windowFeatures ) {
   return window.open( windowURL, windowName); //, config='toolbar=0,location=0,directories=0,top=10,left=10, fullscreen=0,status=0,menuBar=0,scrollBars=1,resizable=0,width=375,height=350')
 }  



function regular(string) {
if (!string) return false;
var Chars = "0123456789.-";

for (var i = 0; i < string.length; i++)
{ if (Chars.indexOf(string.charAt(i)) == -1)
return false;
}
return true;
} 

function convert() {
      var conbwf = new ToFmt(document.Myform.conbw.value*.454);
if (document.Myform.conbw.value != "") {document.Myform.bw.value=LTrim(conbwf.fmtF(6,1));}
//    else {document.Myform.bw.value="";}
      var conadgf = new ToFmt(document.Myform.conadg.value*454);
if (document.Myform.conadg.value != "") {document.Myform.adg.value=LTrim(conadgf.fmtI(6));}
//    else {document.Myform.adg.value="";}
      }

function convert2() {
      var conmjf = new ToFmt(document.Myform.conmfat.value*0.4025+1.4694);
      document.Myform.textmj.value=conmjf.fmtF(6,2);
      var conmcf = new ToFmt((document.Myform.conmfat.value*0.4025+1.4694) / 4.184);
      document.Myform.textmc.value=conmcf.fmtF(6,2);
      var b = document.Myform.text3.value;
      bb = b.substring(0,b.length-3)*1;
      var a = document.Myform.conmfat.value*0.4025+1.4694;
      milk = bb/a;
      milklb = milk/.454;
      var milkf = new ToFmt(milk);
      var milklbf = new ToFmt(milklb);
      document.Myform.text4.value=milkf.fmtF(8,2)+"  kg of "+document.Myform.conmfat.value+"% milk ("+LTrim(milklbf.fmtF(8,2))+"  lbs of "+document.Myform.conmfat.value+"% milk)";
      }

function convert3() {
      var conmjf = new ToFmt(document.Myform.contdn.value*15.104/100);
//	  conmjf = conmjf-1.8749;
      document.Myform.textcmj.value=conmjf.fmtF(6,2);
      var conmcf = new ToFmt(document.Myform.contdn.value*3.61/100);
//	  conmcf = conmcf-0.45998;
      document.Myform.textcmc.value=conmcf.fmtF(6,2);
      document.Myform.mediet.value=conmjf.fmtF(6,2);

      }


function convert4() {
      var condietf = new ToFmt(document.Myform.condiet.value*0.454);
      var condiet3f = new ToFmt(document.Myform.condiet2.value*0.454*.9);
      var condiet2f = new ToFmt(document.Myform.condiet2.value*0.454);
if (document.Myform.condiet.value != "")  {document.Myform.diet.value=condietf.fmtF(6,2); document.Myform.diet2.value="";}
if (document.Myform.condiet2.value != "")  {document.Myform.diet2.value=condiet2f.fmtF(6,2); document.Myform.diet.value=condiet3f.fmtF(6,2);}
      }


function calc() {
      for (var i=0; i < document.Myform.sex.length; i++ )
      {
        if (document.Myform.sex[i].checked) {
           var gen = document.Myform.sex[i].value;}
      }
      if (gen == 1) {
         memreq = 0.485*0.925;
         }
      if (gen == 2) {
         memreq = 0.485*1.075;
         }
      bw = document.Myform.bw.value;
      adg = document.Myform.adg.value;
      if (adg < 0) {document.Myform.adg.value = ""; alert('Please enter a positive numeric value for adg.'); return;}
      megreq = 0.0134;
      mbw = Math.pow(bw,.75)
      mem = mbw*memreq;
      meg = adg*megreq;
      metot = mem+meg; 
	  memc = mem/4.184;
	  megc = meg/4.184;
	  metotc = metot/4.184;
      milk = metot/3.079;
      milklb = milk/.454;
    
      var memf = new ToFmt(mem);
      var megf = new ToFmt(meg);
      var metf = new ToFmt(metot);
	  var memcf = new ToFmt(memc);
      var megcf = new ToFmt(megc);
      var metcf = new ToFmt(metotc);
      var milkf = new ToFmt(milk);
      var milklbf = new ToFmt(milklb);

      document.Myform.text1.value=memf.fmtF(8,2)+"  MJ";
      document.Myform.text2.value=megf.fmtF(8,2)+"  MJ";
      document.Myform.text3.value=metf.fmtF(8,2)+"  MJ";
	  document.Myform.text1b.value=memcf.fmtF(8,2)+"  Mcal";
      document.Myform.text2b.value=megcf.fmtF(8,2)+"  Mcal";
      document.Myform.text3b.value=metcf.fmtF(8,2)+"  Mcal";
      document.Myform.text4.value=milkf.fmtF(8,2)+"  kg of 4% milk ("+LTrim(milklbf.fmtF(8,2))+"  lbs of 4% milk)";

    }
function calc2() {
      diet = document.Myform.diet.value;
      if (diet == "") {diet = document.Myform.diet2.value*.9;}
      mediet = document.Myform.mediet.value;
      med = diet*mediet;
      var b = document.Myform.text3.value;
      bb = b.substring(0,b.length-3)*1;
      needm = bb - med;
if (document.Myform.conmfat.value) {var a = document.Myform.conmfat.value*0.4025+1.4694;}
else {var a = 4*0.4025+1.4694;}
      milka = needm/a;
      milkalb = milka/.454;
    
      var medf = new ToFmt(med);
      var needmf = new ToFmt(needm);
      var milkaf = new ToFmt(milka);
      var milkalbf = new ToFmt(milkalb);


      document.Myform.text11.value=medf.fmtF(8,2)+"  MJ";
      document.Myform.text12.value=document.Myform.text3.value;
      document.Myform.text13.value=needmf.fmtF(8,2)+"  MJ";
if (document.Myform.conmfat.value) {
      document.Myform.text14.value=(milkaf.fmtF(8,2))+"  kg of "+document.Myform.conmfat.value+"% milk ("+LTrim(milkalbf.fmtF(8,2))+"  lbs of "+document.Myform.conmfat.value+"% milk)";
      }
else { document.Myform.text14.value=(milkaf.fmtF(8,2))+"  kg of 4% fat-corrected milk ("+LTrim(milkalbf.fmtF(8,2))+"  lbs of 4% fat-corrected milk)";
      }

    }

function Printable() {
var win1 = window.open('', 'Printable');
win1.document.open('text/html');
win1.document.write('<html>\n')
win1.document.write('<head><title>Metabolizable Energy (ME) Requirement For Suckling Goats</title>\n')
win1.document.write('<script type="text/javascript" language="JavaScript">\n<!--\n');
win1.document.write('function _onclick() \n{\nwindow.print();\n}\n');
win1.document.write('-->\n<\/script>\n');
win1.document.write('<link rel=stylesheet href="../scripts/scripts_css.css" type="text/css">\n');
win1.document.write('</head>\n');
win1.document.write('<body>\n');
win1.document.write('<table cellspacing="0" cellpadding="2" width="100%">');
win1.document.write('<tr><td align=left valign="bottom"><img src="../../images/new_garza.png" width="312" height="245" border="0" alt=""></td>\n');
win1.document.write('<td align=right valign="bottom"><INPUT onclick="_onclick()" type=button value="Print" style="width: 5em;"><p>');
win1.document.write('<INPUT onclick="window.close()" type=button value="Close" style="width: 5em;"></td></tr>');
win1.document.write('</table>');win1.document.write('<p class=first-line>Metabolizable Energy (ME) Requirement For Suckling Goats</p>\n');
win1.document.write('<P class=full>(In Confinement; pen, stall, small pasture)</P>\n');
win1.document.write('<h3>INPUTS</h3>\n');
for (var i=0; i < document.Myform.sex.length; i++ )
      {
        if (document.Myform.sex[i].checked) {
           var gen = document.Myform.sex[i].value;}
      }
win1.document.write('<table cellspacing="0" cellpadding="2" border="1" width="100%">');
win1.document.write('<tr>');
win1.document.write('<td width="50%">Gender</td>');
if (gen == 1) {win1.document.write('<td align="center">female or wether</td>');}
else {win1.document.write('<td align="center">intact male</td>');}
win1.document.write('</tr>');
win1.document.write('<tr>');
win1.document.write('<td width="50%">Body weight</td><td align="center">');
win1.document.write(document.Myform.bw.value);
win1.document.write(' kg (');
      var bwp = new ToFmt((document.Myform.bw.value)/.454);
win1.document.write(LTrim(bwp.fmtF(6,1)));
win1.document.write(' lbs)</td>');
win1.document.write('</tr>');
win1.document.write('<tr>');
win1.document.write('<td width="50%">Average daily gain</td><td align="center">');
win1.document.write(document.Myform.adg.value);
win1.document.write(' g/day (');
var adgp = new ToFmt((document.Myform.adg.value)/454);
win1.document.write(LTrim(adgp.fmtF(6,2)));
win1.document.write(' lbs/day)</td>');
win1.document.write('</tr>');
if (document.Myform.conmfat.value) {
win1.document.write('<tr>');
win1.document.write('<td width="50%">Milk fat concentration</td><td align="center">');
win1.document.write(document.Myform.conmfat.value);
win1.document.write('%</td>');
win1.document.write('</tr>');
}
if (document.Myform.diet.value) {
win1.document.write('<td width="50%">Starter diet intake, dry matter');
if (document.Myform.diet2.value == "") {win1.document.write('</td><td align="center">');} else win1.document.write(' [as fed]</td><td align="center">');
win1.document.write(document.Myform.diet.value/1);
win1.document.write(' kg (');
      var dietp = new ToFmt((document.Myform.diet.value)/.454);
win1.document.write(LTrim(dietp.fmtF(6,2)));
win1.document.write(' lbs)');
if (document.Myform.diet2.value !== "") {
win1.document.write('&nbsp;&nbsp;&nbsp;[');
win1.document.write(document.Myform.diet2.value/1);
win1.document.write(' kg (');
      var dietp = new ToFmt((document.Myform.diet2.value)/.454);
win1.document.write(LTrim(dietp.fmtF(6,2)));
win1.document.write(' lbs)]');
}
win1.document.write('</td>');
win1.document.write('</tr>');
win1.document.write('<tr>');
win1.document.write('<td width="50%">ME concentration in starter diet (TDN concentration in starter diet)</td><td align="center">');
win1.document.write(document.Myform.mediet.value);
win1.document.write(' MJ/kg dry matter (');
var tdndiet = new ToFmt((document.Myform.mediet.value)/.1504);
win1.document.write(LTrim(tdndiet.fmtF(6,1)));
win1.document.write('% dry matter)');

win1.document.write('</td>');
win1.document.write('</tr>');
}
win1.document.write('</table>');
win1.document.write('<h3>OUTPUTS</h3>\n');
win1.document.write('<table cellspacing="0" cellpadding="2" border="1" width="100%">');
win1.document.write('<tr>');
win1.document.write('<td width="50%">ME requirement for maintenance</td><td align="center">');
win1.document.write(document.Myform.text1.value);
win1.document.write('</td>');
win1.document.write('</tr>');
win1.document.write('<tr>');
win1.document.write('<td width="50%">ME requirement for gain</td><td align="center">');
win1.document.write(document.Myform.text2.value);
win1.document.write('</td>');
win1.document.write('</tr>');
win1.document.write('<tr>');
win1.document.write('<td width="50%">Total ME requirement</td><td align="center">');
win1.document.write(document.Myform.text3.value);
win1.document.write('</td>');
win1.document.write('</tr>');
if ((document.Myform.diet.value) && (document.Myform.mediet.value)) {
win1.document.write('<tr>');
win1.document.write('<td width="50%">ME from starter diet</td><td align="center">');
win1.document.write(document.Myform.text11.value);
win1.document.write('</td>');
win1.document.write('</tr>');
win1.document.write('<tr>');
win1.document.write('<td width="50%">ME needed from milk </td><td align="center">');
win1.document.write(document.Myform.text13.value);
win1.document.write('</td>');
win1.document.write('</tr>');
}
win1.document.write('<tr>');
win1.document.write('<td width="50%">Amount of milk needed per day</td><td align="center">');
if (document.Myform.text14.value) {win1.document.write(document.Myform.text14.value);}
else {win1.document.write(document.Myform.text4.value);}
win1.document.write('</td>');
win1.document.write('</tr>');

win1.document.write('</table>');
win1.document.write('</body>\n')
win1.document.write('</html>')
win1.document.close();

//win1.close();
}

function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

    
// JS Object: ToFmt 
// Author: David Mosley, E-mail: David.Mosley@fundp.ac.be or davmos@fcmail.com
// August 1998.
// Contains a limited set of formatting routines for
// use in JavaScript scripts.
// Feel free to use this code in your scripts. I would be grateful if you
// could keep this header intact. 
// Please let me know if you find the code useful.
// Please report any bugs you find or improvements you make to the script. 
// The code has been tested, but no guarantee can be made of it functioning
// correctly. Use is entirely at your own risk.
// 
// Summary of methods
// fmt00(): Tags leading zero onto numbers 0 - 9.
// Particularly useful for displaying results from Date methods.
//
// fmtF(w,d): formats in a style similar to Fortran's Fw.d, where w is the
// width of the field and d is the number of figures after the decimal
// point. 
// The result is aligned to the right of the field.  The default
// padding character is a space " ". This can be modified using the 
// setSpacer(string) method of ToFmt. 
// If the result will not fit in the field , the field will be returned
// containing w asterisks.
//
// fmtE(w,d): formats in a style similar to Fortran's Ew.d, where w is the
// width of the field and d is the number of figures after the decimal
// point. 
// The result is aligned to the right of the field.  The default
// padding character is a space " ". This can be modified using the 
// setSpacer(string) method of ToFmt. 
// If the result will not fit in the field , the field will be returned
// containing w asterisks.
//
// fmtI(w): formats in a style similar to Fortran's Iw, where w is the
// width of the field.
// Floating point values are truncated (rounded down) for integer
// representation.
// The result is aligned to the right of the field.  The default
// padding character is a space " ". This can be modified using the 
// setSpacer(string) method of ToFmt. 
// If the result will not fit in the field , the field will be returned
// containing w asterisks.

function ToFmt(x){
 this.x=x;
 this.fmt00 = fmt00;
 this.fmtF = fmtF;
 this.fmtE=fmtE;
 this.fmtI=fmtI;
 this.spacer=" ";
 this.setSpacer=setSpacer;
}

function fmt00(){
 // fmt00: Tags leading zero onto numbers 0 - 9.
 // Particularly useful for displaying results from Date methods.
 //
 if (parseInt(this.x) < 0) var neg = true;
 if (Math.abs(parseInt(this.x)) < 10){
  this.x = "0"+ Math.abs(this.x);
 }
 if (neg) this.x = "-"+this.x;
 return this.x;
}

function fmtF(w,d){

 // fmtF: formats in a style similar to Fortran's Fw.d, where w is the
 // width of the field and d is the number of figures after the decimal
 // point. 
 // The result is aligned to the right of the field.  The default
 // padding character is a space " ". This can be modified using the 
 // setSpacer(string) method of ToFmt. 
 // If the result will not fit in the field , the field will be returned
 // containing w asterisks.
 var width=w;
 var dpls=d;
 var lt1=false;
 var len=this.x.toString().length;
 var junk;
 var res="";
// First check for valid format request
 if ( width < (dpls+2)){
  window.alert("Illegal format specified : w = " + d +
               " w = " + d +
                "\nUsage: [ToFmt].fmtF(w,d)" +
                "\nWidth (w) of field must be greater or equal to the number " +
                "\nof digits to the right of the decimal point (d) + 2");
  junk = filljunk(width);
  return junk;
 }
// Work with absolute value
 var absx=Math.abs(this.x);
// Nasty fix to deal with numbers < 1 and problems with leading zeros!
 if ((absx < 1) && (absx > 0)){
  lt1 = true;
  absx+=10;
 }
// Get postion of decimal point
 var pt_pos = absx.toString().indexOf(".");
 if ( pt_pos == -1){
  res+= absx;
  res+= ".";
  for (var i = 0; i < dpls; i++){
   res += 0;
  }  
 }
 else{
  res = Math.round(absx * Math.pow(10,dpls));
  res=res.toString();
  if (res.length == 
      Math.round(Math.floor(absx * Math.pow(10,dpls))).toString().length){ 
   res = res.substring(0,pt_pos) + "." + 
         res.substring(pt_pos,res.length);
  }
  else{
   pt_pos++;
   res = res.substring(0,pt_pos) + "." + 
          res.substring(pt_pos,res.length);
  } 
// Remove leading 1 from  numbers < 1 (Nasty fix!)
  if (lt1) {
   res=res.substring(1,res.length);
  }
 }
 // Final formatting statements
 // Reinsert - sign for negative numbers
 if (this.x < 0)res = "-"+res;
 // Check whether the result fits in the width of the field specified
 if (res.length > width){
  res=filljunk(width);
 }
 // If necessary, pad from the left with the spacer string
 else if (res.length < width){
  var res_bl="";
  for (var i = 0; i < (width - res.length); i++){
   res_bl += this.spacer ;
  } 
  res = res_bl + res;
 }
 return res;
}

function fmtE(w,d){

 // fmtE: formats in a style similar to Fortran's Ew.d, where w is the
 // width of the field and d is the number of figures after the decimal
 // point. 
 // The result is aligned to the right of the field.  The default
 // padding character is a space " ". This can be modified using the 
 // setSpacer(string) method of ToFmt. 
 // If the result will not fit in the field , the field will be returned
 // containing w asterisks.
 //
 var width=w;
 var dpls=d;
 var e="E+";
 var len=this.x.toString().length;
 var pow10;
 var xp10;
 var junk;
 var res="";
// First check for valid format request
 if ( width < (dpls+5)){
  window.alert("Illegal format specified : w = " + d +
               " w = " + d +
                "\nUsage: [ToFmt].fmtE(w,d)" +
                "\nWidth (w) of field must be greater or equal to the number " +
                "\nof digits to the right of the decimal point (d) + 6");
  junk = filljunk(w);
  return junk;
 }
// Work with absolute value
 var absx=Math.abs(this.x);
// Get postion of decimal point
 var pt_pos = absx.toString().indexOf(".");
// For x=0
 if (absx == 0){
  res +="0.";
  for (var i=0; i< dpls; i++){
   res += "0";
  }
  res  += "E+00";
 }
// For abs(x) >= 1 
 else if (absx >= 1.0){
  pow10=1;
  xp10 = absx;
  while (xp10 >= 1.){
   pow10++;
   xp10 /= 10;
  }
  res = Math.round(xp10 * Math.pow(10,dpls));
  res=res.toString();
  if (res.length == 
      Math.round(Math.floor(xp10 * Math.pow(10,dpls))).toString().length){ 
    pow10--;
  }
  res = "0." + res.substring(0,dpls) + e + (new ToFmt(pow10)).fmt00();
 }
// For abs(x) < 1
 else if (absx < 1.0){
  pow10=1;
  xp10 = absx;
  while (xp10 < 1.){
   pow10--;
   xp10 *= 10;
  }
  res = Math.round(xp10/10 * Math.pow(10,dpls));
  res=res.toString();
  if (res.length != 
      Math.round(Math.floor(xp10/10 * Math.pow(10,dpls))).toString().length){ 
    pow10++;
  }
  if (pow10 < 0) e = "E-";
  res = "0." + res.substring(0,dpls) + e + (new ToFmt(Math.abs(pow10))).fmt00();
 }
 
 if (this.x < 0)res = "-"+res;
 if (res.length > width){
  res=filljunk(width);
 }
 else if (res.length < width){
  var res_bl="";
  for (var i = 0; i < (width - res.length); i++){
   res_bl += this.spacer ;
  } 
  res = res_bl + res;
 }
 return res;
 
}

function fmtI(w){

 // fmtI: formats in a style similar to Fortran's Iw, where w is the
 // width of the field.
 // Floating point values are truncated (rounded down) for integer
 // representation.
 // The result is aligned to the right of the field.  The default
 // padding character is a space " ". This can be modified using the 
 // setSpacer(string) method of ToFmt. 
 // If the result will not fit in the field , the field will be returned
 // containing w asterisks.
 var width=w;
 var lt0=false;
 var len=this.x.toString().length;
 var junk;
 var res="";
// Work with absolute value
 var absx = Math.abs(this.x);

// Test for < 0
 if (parseInt(this.x) < 0){
  lt0 = true;
 }
 res = Math.round(Math.floor((absx))).toString();
 if (lt0){
  res = "-"+res;
 }
 if (res.length > width){
  res=filljunk(width);
 }
 else if (res.length < width){
  var res_bl="";
  for (var i = 0; i < (width - res.length); i++){
   res_bl += this.spacer ;
  } 
  res = res_bl + res;
 }
 return res;
}

function filljunk(lenf){
 // Fills field of length lenf with asterisks
 var str="";
 for (var i=0; i < lenf; i++){
  str +="*";
 }
 return str;
}

function setSpacer(spc){
 var spc;
 this.spacer=spc;
 return this.spacer;
}

   
