//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=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=conadgf.fmtI(6);}
//    else {document.Myform.adg.value="";}
      }

function convert2() {
      var conmjf = new ToFmt(document.Myform.contdn.value*15.104/100);
//	  conmjf = conmjf-1.8749;
      document.Myform.textmj.value=conmjf.fmtF(6,2);
      var conmcf = new ToFmt(document.Myform.contdn.value*3.61/100);
//	  conmcf = conmcf-0.45998;
      document.Myform.textmc.value=conmcf.fmtF(6,2);
      document.Myform.mec.value=conmjf.fmtF(6,2);
      }

function calc() {

      for (var i=0; i < document.Myform.bio.length; i++ )
      {
        if (document.Myform.bio[i].checked) {
           var bio = document.Myform.bio[i].value;}
      }


      bw = document.Myform.bw.value;
      adg = document.Myform.adg.value;
      mec = document.Myform.mec.value;
      condmi = document.Myform.condmi.value/100;

      if (bio == 1) {
          memreq = 0.489;
          megreq = 23.1;
          }
      if (bio == 2) {
          memreq = 0.580;
          megreq = 23.1;
      }

      if (bio == 3) {
          memreq = 0.489;
          megreq = 19.8;
      }

	  adgn = 0;
	  adgp = 0;
	  if (adg < 0) {
	     adgn = adg/1000 * -1;
		 }
	  if (adg > 0) {
	     adgp = adg/1000;
		 } 
       adg = adg/1000;     
       mbw = Math.pow(bw,.75);

km = 0.503 + (0.019 * mec);
kg = (0.0423 * mec) + 0.006;
nem = 0.315 * mbw;
meg = adgp * megreq;
neg = meg * kg;
tec = 23.9;
met = adgn * tec;
net = met * km;
mem = mbw * memreq;
ne = nem + neg + net;
pme = ((mem * km) + (meg * kg) + (met * km)) / 0.634;
       pmei = pme - met;
pdmi = pmei / mec;
adgbw = adg / bw; 
adgmbw = adg / mbw;
adgmbw2 = adgmbw * adgmbw;
apdmi = -0.0047 + (pdmi * 0.9637) - (adgbw * 70.27)  + (38.71 * adgmbw) - (243.4 * adgmbw2);
       pmei = apdmi * mec;
       apdmibw = (apdmi/bw)*100;
       dmilb = apdmi/.454;
	    
      var pmeif = new ToFmt(pmei);
      var apdmif = new ToFmt(apdmi);
      var dmilbf = new ToFmt(dmilb);
      var apdmibwf = new ToFmt(apdmibw);

      document.Myform.text1.value=pmeif.fmtF(8,2)+"  MJ";
      document.Myform.text2.value=apdmif.fmtF(8,2)+"  kg ("+LTrim(dmilbf.fmtF(8,2))+"  lbs)";
      document.Myform.text3.value=apdmibwf.fmtF(8,2)+"  %";
      dmi = apdmi;
      dmibw2 = (dmi/condmi/bw) * 100;
      var dmi2f = new ToFmt(dmi/condmi);
      var dmilb2f = new ToFmt(dmilb/condmi);
      var dmibw2f = new ToFmt(dmibw2);
	  document.Myform.text4.value=dmi2f.fmtF(8,2)+"  kg ("+LTrim(dmilb2f.fmtF(8,2))+"  lbs)";
	  document.Myform.text5.value=dmibw2f.fmtF(8,2)+"  %";

document.cookie = "Enabled=true";
   var cookieValid = document.cookie;

   if (cookieValid.indexOf("Enabled=true") != -1)
   {
      cookiesEnabled = true;
   }
   else
   {
      cookiesEnabled = false;
   }

if(cookiesEnabled) {

expireDate = new Date;
expireDate.setMonth(expireDate.getMonth()+12);
var FIValues = "";

FIValues += "&"+"fi4"+":"+"Growing Goats";
FIValues += ":"+bw;
FIValues += ":"+bio;
FIValues += ":"+adg;
FIValues += ":";
FIValues += ":";
FIValues += ":"+mec;
FIValues += ":";
FIValues += ":"+pmei;
FIValues += ":"+apdmi;
FIValues += ":"+apdmibw;

document.cookie = "FI_Values="+FIValues+";expires="+expireDate.toGMTString();
if (FIValues == "") {document.cookie = "FI_Values="+FIValues+"; expires=Fri, 02-Jan-1970 00:00:00 GMT";}
}

if (window.opener && window.opener.location == "http://www2.luresext.edu/goats/research/rationbalancer.htm") { 
if (confirm("Your values have been saved for use in the \"Total Mixed Ration Calculator\".\n\nIf you are ready to return to the \"Total Mixed Ration Calculator\", then click \"OK\" to close this window.\n\nIf you would like to linger in this calculator, then click \"Cancel\".  You will have to manually close this window to return to the \"Total Mixed Ration Calculator\".")) {window.close();}
}

//if (window.opener && window.opener.location == "http://www2.luresext.edu/goats/research/rationbalancer.htm") {window.close();}

    }

function Printable() {
var win1 = window.open('', 'Printable');
win1.document.open('text/html');
win1.document.write('<html>\n')
win1.document.write('<head><title>Feed Intake by Growing Goats in Confinement</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>Feed Intake (Dry Matter; DM) by Growing Goats in Confinement</p>\n');
//win1.document.write('<P class=full>(Meat, Dairy, And Indigenous; &le;1.5 Years Of Age)</P>\n');
win1.document.write('<h3>INPUTS</h3>\n');
      for (var i=0; i < document.Myform.bio.length; i++ )
      {
        if (document.Myform.bio[i].checked) {
           var bio = document.Myform.bio[i].value;}
      }
win1.document.write('<table cellspacing="0" cellpadding="2" border="1" width="100%">');
win1.document.write('<tr>');
win1.document.write('<td width="50%">Biotype</td>');
if (bio == 1) {win1.document.write('<td align="center">meat, 50% or more Boer</td>');}
else if (bio == 2) {win1.document.write('<td align="center">dairy</td>');} 
else {win1.document.write('<td align="center">indigenous or local</td>');}
win1.document.write('</tr>');
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.fmtI(6)));
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>');
win1.document.write('<tr>');
win1.document.write('<td width="50%">Dietary ME concentration (Dietary TDN concentration)</td><td align="center">');
win1.document.write(document.Myform.mec.value);
win1.document.write(' MJ/kg dry matter (');
var tdndiet = new ToFmt((document.Myform.mec.value)/.1504);
win1.document.write(LTrim(tdndiet.fmtF(6,1)));
win1.document.write('% dry matter)</td>');
win1.document.write('</tr>');
win1.document.write('<tr>');
win1.document.write('<td width="50%">DM in diet</td><td align="center">');
win1.document.write(document.Myform.condmi.value);
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%">Predicted ME intake</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%">Predicted DM intake</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%">Predicted DM intake (% of BW)</td><td align="center">');
win1.document.write(document.Myform.text3.value);
win1.document.write('</td>');
win1.document.write('</tr>');
win1.document.write('<tr>');
win1.document.write('<td width="50%">Predicted as fed intake (kg)</td><td align="center">');
win1.document.write(document.Myform.text4.value);
win1.document.write('</td>');
win1.document.write('</tr>');
win1.document.write('<tr>');
win1.document.write('<td width="50%">Predicted as fed intake (% body weight)</td><td align="center">');
win1.document.write(document.Myform.text5.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;
}   
