// file: metric_calculator.js
// field validation and metric calculator for event entry forms
// $Id: metric_calculator.js,v 1.3 2004/10/27 05:22:03 bill Exp $

// Author: Matt Kruse http://www.mattkruse.com 
// modified by: William.Haynes 2002 william.haynes@usa.net

// convert feet-inches to meters 
// =============================================================
function convert2m(item,ft,inch,m) {
   var feet   = ft.value;
   var inches = inch.value;

   var cfactor = 0.0254;  // inches to meters conversion factor
   var meters = 0;
   var maxin = 11.99;
   var maxft = 265;
   var minvalue = 0;
   var no_mark = "NM";

// replace null with 0
   feet = mkNull0(feet);
   inches = mkNull0(inches);

// verify feet and inches values are numeric and within range
// feet
   if (isNaN(feet)) {
// accept NM for no mark
      feet = feet.toUpperCase();
      if ((feet == "N") || (feet == "NM")) {
         m.value = no_mark;
         m.focus();

// WARNING - select box must be checked too
         if (eval(item.checked) == false) {
            window.alert("The select box must also be checked");
         }

         return true;
      }
   }

   if ( ( ! isInt(feet) ) || ( feet < minvalue ) || ( feet > maxft ) ) {
      window.alert( "feet value must be numeric and between " + 
         minvalue + " and " +maxft );
      ft.select();
      ft.focus();
      m.value = no_mark;
      return false;
   } 

// inches
   if (isNaN(inches)) {
// accept NM for no mark
      inches = inches.toUpperCase();
      if ((inches == "N") || (inches == "NM")) {
         m.value = no_mark;
         m.focus();

// WARNING - select box must be checked too
         if (eval(item.checked) == false) {
            window.alert("The select box must also be checked");
         }

         return true;
      }
   }

   if ( ( ! isFloat(inches) ) || ( inches < minvalue ) || ( inches > maxin ) ) {
      window.alert( "inches value must be numeric and between " + 
         minvalue + " and " + maxin );
      inch.select();
      inch.focus();
      m.value = no_mark;
      return false;
   } 

// convert feet-inches to meters
   inches = ( ( parseInt(feet) * 12 ) + parseFloat(inches) );
   meters = inches * cfactor;
   meters = round2d(meters);

// update form
// default to null if value is 0
   if ( meters == 0 ) {
      m.value = no_mark;

   } else {
      m.value = meters;
   }

// WARNING - select box must be checked too
   if (eval(item.checked) == false) {
      window.alert("The select box must also be checked");
   }

   return true;
}

// convert feet-inches to nnn-nn.nn
// =============================================================
function convert2e(item,ft,inch,e) {
   var feet   = ft.value;
   var inches = inch.value;
   var maxft = 265;
   var maxin = 11.99;
   var minvalue = 0;
   var no_mark = "NM";

// replace null with 0
   if ((feet == null) || (feet == "")) {
      feet = 0;
   }
   if ((inches == null) || (feet == "")) {
      inches = 0;
   }

// verfy feet and inches values are numeric and within range
// feet
   if (isNaN(feet)) {
// accept NM for no mark
      feet = feet.toUpperCase();
      if ((feet == "N") || (feet == "NM")) {
         e.value = no_mark;
         e.focus();

// WARNING - select box must be checked too
         if (eval(item.checked) == false) {
            window.alert("The select box must also be checked");
         }

         return true;
      }
   }

   if ( ( ! isInt(feet) ) || ( feet < minvalue ) || ( feet > maxft ) ) {
      window.alert( "feet value must be numeric and between " + 
         minvalue + " and " +maxft );
      ft.select();
      ft.focus();
      e.value = no_mark;
      return false;
   } 

// inches
   if (isNaN(inches)) {
// accept NM for no mark
      inches = inches.toUpperCase();
      if ((inches == "N") || (inches == "NM")) {
         e.value = no_mark;
         e.focus();

// WARNING - select box must be checked too
         if (eval(item.checked) == false) {
            window.alert("The select box must also be checked");
         }

         return true;
      }
   }

   if ( ( ! isFloat(inches) ) || ( inches < minvalue ) || ( inches > maxin ) ) {
      window.alert( "inches value must be numeric and between " + 
         minvalue + " and " + maxin );
      inch.select();
      inch.focus();
      e.value = no_mark;
      return false;
   } 

   if ((feet == 0) && (inches == 0)) {
      e.value = no_mark;

   } else {
// convert feet-inches to string for English units
      e.value = feet + "-" + inches;
   }

// WARNING - select box must be checked too
   if (eval(item.checked) == false) {
      window.alert("The select box must also be checked");
   }

   return true;
}
