//  Conversion Functions
//  Written:  2009-02-22 by James Alarie <jalarie@umich.edu>
//    http://spruce.flint.umich.edu/~jalarie/

        function chg_unit(Which,Zero,Form) {
          if (!Zero) { Zero='yes'; }                // process zeros
          if (!Form) { Form=0; }                    // form number
          f1=document.forms[Form];                  // abbreviation
          for (ix1=0; ix1<Units.length-1; ix1++) {
            Unit=Units[ix1];
            Items=Unit.split(',');                  // code, label, factor, [comment]
            if (Items[0] == Which) {
              ix1=Units.length*2;                   // kill the loop
            }
          }
          Items[2]=eval(Items[2]);
          eval('IValue=f1.'+Prefix+Which+'.value;');
          IValue=eval(IValue);
          IValue*=Items[2];
          SpecConv=new Array();  SpecConvIndex=-1;
          for (ix1=0; ix1<Units.length-1; ix1++) {
            Unit=Units[ix1];
            Items=Unit.split(',');
            if (Items[0] != Which) {
              if (Items[2] == 0) {                   // special conversion
                SpecConvIndex++;
                SpecConv[SpecConvIndex]=Items[0];
              } else {
                Items[2]=eval(Items[2]);
                OValue=IValue/Items[2];
                OValue=SignificantDigits(OValue,12);
                eval('f1.'+Prefix+Items[0]+'.value='+OValue+';');
              }
            }
          }
          if (Zero == 'yes') {                       // process zeros
            for (ix1=0; ix1<=SpecConvIndex; ix1++) {
              eval('chg_unit_'+SpecConv[ix1]+'_out();');
            }
          }
          return true;
        } // chg_unit
        
        function SignificantDigits(What,Digits) {
          var Log=Math.floor(Math.log(What)/Math.LN10);
          var Exponent=Digits-1-Log;
          var Factor=Math.pow(10,Exponent);
          return Math.floor(What*Factor+0.5)/Factor;
        } // SignificantDigits

        function Setup(Form) {
          if (!Form) { Form=0; }                    // form number
          for (ix1=0; ix1<Units.length-1; ix1++) {
            Unit=Units[ix1];
            Items=Unit.split(',');                  // code, label, factor, [comment]
            if (!Items[3]) {                        // no comment
              Items[3]='';                          // ...make it null
            }
            Out ='';
            Out+='<div class="UColumnA0">\n';
            Out+='  <span class="UColumnA1">\n';
            Out+='    <label for="'+Prefix+Items[0]+'">'+Items[1]+'<\/label> \n';
            Out+='  <\/span>\n';
            Out+='  <input type="text" value="0" size="'+Size+'" name="'+Prefix+Items[0]+'" id="'+Prefix+Items[0]+'" alt="'+Prefix+Items[0]+'" ';
            if (Items[2] == 0) {                    // special conversion
              Out+='onblur="chg_unit_'+Items[0]+'();"';
            } else {                                // standard conversion
              Out+='onblur="chg_unit(\''+Items[0]+'\',\'\',\''+Form+'\');"';
            }
            Out+=' onfocus="this.select();" \/>&nbsp; \n';
            Out+='  <input type="button" value="ok" alt="ok" title="ok" \/>&nbsp; \n';
            Out+='  <input type="reset" value="clear" alt="clear" title="clear" \/>&nbsp; \n';
            Out+=Items[3]+'\n';
            Out+='<\/div><!\-\- UColumnA0 \-\->\n';
              document.write(Out);
          }
        } // Setup
