//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 calc() {

      for (var i=0; i < document.Myform.bio.length; i++ )
      {
        if (document.Myform.bio[i].checked) {
           var bio = document.Myform.bio[i].value;}
      }

      adg = document.Myform.adg.value;
      bw = document.Myform.bw.value;
      dmibw = document.Myform.dmibw.value;
      dmi=dmibw*0.01*bw;
      mpmreq=3.07;
      if (bio == 1) {
          mpgreq=0.404;
          }
      if (bio > 1) {
          mpgreq=0.290;
      }
      mbw = Math.pow(bw,.75);
//      mpm = mbw * mpmreq;
//      mpg = adg * mpgreq;

pc = 15.722 - (0.069 * bw);
if (adg >= 0) {mpg = adg * mpgreq;}
if (adg < 0) {mpg = 0;}
mpmu = mbw * mpmreq;
mpm = mpmu;
if (adg < 0) {mpm = mpmu + ((pc / 100) * adg);}

      mptot = mpm + mpg;
      mnmppt= mptot/10/dmi;
      cpgr20 = mptot / 0.672;
      cpgr40 = mptot / 0.704;
      cpgr60 = mptot / 0.736;
      cpgr20lb = mptot / 0.672/454;
      cpgr40lb = mptot / 0.704/454;
      cpgr60lb = mptot / 0.736/454;
      cpgr20pt = cpgr20/10/dmi;
      cpgr40pt = cpgr40/10/dmi;
      cpgr60pt = cpgr60/10/dmi;
      
      var dmif = new ToFmt(dmi);
      var mpmf = new ToFmt(mpm);
      var mpmuf = new ToFmt(mpmu);
      var mpgf = new ToFmt(mpg);
      var mptotf = new ToFmt(mptot);
      var mnmpptf = new ToFmt(mnmppt);
      var cpgr20f = new ToFmt(cpgr20);
      var cpgr40f = new ToFmt(cpgr40);
      var cpgr60f = new ToFmt(cpgr60);
      var cpgr20lbf = new ToFmt(cpgr20lb);
      var cpgr40lbf = new ToFmt(cpgr40lb);
      var cpgr60lbf = new ToFmt(cpgr60lb);
      var cpgr20ptf = new ToFmt(cpgr20pt);
      var cpgr40ptf = new ToFmt(cpgr40pt);
      var cpgr60ptf = new ToFmt(cpgr60pt);

//      document.Myform.text0.value=mpgreq;
      document.Myform.text1.value=dmif.fmtF(8,2)+"  kg";
      document.Myform.text2.value=mpmf.fmtF(8,2)+"  g";
      document.Myform.text2a.value=mpmuf.fmtF(8,2)+"  g";
      document.Myform.text3.value=mpgf.fmtF(8,2)+"  g";
      document.Myform.text4.value=mptotf.fmtF(8,2)+"  g";
      document.Myform.text6.value=mnmpptf.fmtF(8,2)+"  %";
      document.Myform.text7.value=cpgr20f.fmtF(8,2)+" g   ("+LTrim(cpgr20lbf.fmtF(8,2))+" lbs)";
      document.Myform.text8.value=cpgr40f.fmtF(8,2)+" g   ("+LTrim(cpgr40lbf.fmtF(8,2))+" lbs)";
      document.Myform.text9.value=cpgr60f.fmtF(8,2)+" g   ("+LTrim(cpgr60lbf.fmtF(8,2))+" lbs)";
      document.Myform.text10.value=cpgr20ptf.fmtF(8,2)+"  %";
      document.Myform.text11.value=cpgr40ptf.fmtF(8,2)+"  %";
      document.Myform.text12.value=cpgr60ptf.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 MPValues = "";

MPValues += "&"+"mp1"+":"+"Growing Goats";
MPValues += ":"+bio;
MPValues += ":"+bw;
MPValues += ":"+adg;
MPValues += ":"+dmibw;
MPValues += ":"+mpm;
MPValues += ":"+mpmu;
MPValues += ":"+mpg;
MPValues += ":"+mptot;
MPValues += ":"+mnmppt;

document.cookie = "MP_Values="+MPValues+";expires="+expireDate.toGMTString();
if (MPValues == "") {document.cookie = "MP_Values="+MPValues+"; 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>Metabolizable Protein (MP) and Crude Protein (CP) Requirements for Maintenance and Growth of Growing 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 Protein (MP) and Crude Protein (CP) Requirements for Maintenance and Growth of Growing Goats</p>\n');
win1.document.write('<P class=full>(Meat, Dairy, and Indigenous)</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>');

if (document.Myform.hidden1.value != "") {
win1.document.write('<tr>');
win1.document.write('<td width="50%">Gender</td>');
var  gen = document.Myform.hidden1.value;
if (gen == 1) {win1.document.write('<td align="center">female or wether</td>');}
else if (gen == 2) {win1.document.write('<td align="center">intact male</td>');} 
else {win1.document.write('<td align="center">undefined</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.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%">DM intake (% of body weight)</td><td align="center">');
win1.document.write(document.Myform.dmibw.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%">DM 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%">MP requirement for maintenance</td><td align="center">');
win1.document.write(document.Myform.text2a.value);
win1.document.write('</td>');
win1.document.write('</tr>');
win1.document.write('<tr>');
win1.document.write('<td width="50%">Dietary MP used for maintenance</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%">MP requirement for growth or gain</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%">Total dietary MP requirement</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%">Total dietry MP requirement (% of DM intake)</td><td align="center">');
win1.document.write(document.Myform.text6.value);
win1.document.write('</td>');
win1.document.write('</tr>');
win1.document.write('<tr>');
win1.document.write('<td width="50%">Total dietary CP requirement, diet with 20% UIP and 80% DIP (g)</td><td align="center">');
win1.document.write(document.Myform.text7.value);
win1.document.write('</td>');
win1.document.write('</tr>');
win1.document.write('<tr>');
win1.document.write('<td width="50%">Total dietary CP requirement, diet with 40% UIP and 60% DIP (g)</td><td align="center">');
win1.document.write(document.Myform.text8.value);
win1.document.write('</td>');
win1.document.write('</tr>');
win1.document.write('<tr>');
win1.document.write('<td width="50%">Total dietary CP requirement, diet with 60% UIP and 40% DIP (g)</td><td align="center">');
win1.document.write(document.Myform.text9.value);
win1.document.write('</td>');
win1.document.write('</tr>');
win1.document.write('<tr>');
win1.document.write('<td width="50%">Total dietary CP requirement, diet with 20% UIP and 80% DIP (% DM)</td><td align="center">');
win1.document.write(document.Myform.text10.value);
win1.document.write('</td>');
win1.document.write('</tr>');
win1.document.write('<tr>');
win1.document.write('<td width="50%">Total dietary CP requirement, diet with 40% UIP and 60% DIP (% DM)</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%">Total dietary CP requirement, diet with 60% UIP and 40% DIP (% DM)</td><td align="center">');
win1.document.write(document.Myform.text12.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;
}

