function ConversionCaculator() {
  this.InchFactor = 0.000039370079;
  this.mmFactor = 0.001;
  this.MicroinchFactor = 39.370079;
  this.AngstromFactor = 10000;
  this.MilFactor = 0.039370079;
  this.NanometerFactor = 1000;

  if (typeof ConversionCaculator._initialized == "undefined") {

    ConversionCaculator.prototype.init = function () {

      var InUnit = document.getElementById('InUnit');
      InUnit.pageObj = this;
      addEvent(InUnit,'keyup',this.Calculate, false);
      addEvent(InUnit,'focus',this.ClearField, false);

      var UnitList = document.getElementById('UnitList');
      UnitList.pageObj = this;
      addEvent(UnitList,'change',this.Calculate, false);
    }

    ConversionCaculator.prototype.ClearField = function (e,el) {
      var el = (el) ? el : getTarget(e);

      if (!el) {
        return;
      }

      el.value = "";
    }

    ConversionCaculator.prototype.Calculate = function (e) {
      var el = null;

      el = getTarget(e);

      if (!el) {
        return;
      }

      var pageObj = (this.pageObj) ? this.pageObj: el.pageObj;
      var el_id = el.id;

      if (pageObj.CheckForm(el,pageObj)) {
        pageObj.runCalculations(el,pageObj);
      }
    }

    ConversionCaculator.prototype.CheckForm = function (el,pageObj) {
      switch (el.id) {
        case "InUnit":
          if (el.value && isNum(el.value)) {
            //make sure we have units chosen
            var UnitList = document.getElementById('UnitList');
            if (UnitList.value.length > 0) {
              return true;
            }
          } else if (el.value) {
            alert ("Numbers only please");
            pageObj.ClearField(null,el);
            return false;
          }
          break;
        case "UnitList":
          if (el.value.length > 0) {
            //make sure we have input amount
            var InUnit = document.getElementById('InUnit');
            if (!InUnit.value) {
              alert ("Please enter an input amount");
              return false;
            } else {
              return true;
            }
          }
          break;
      }
      return false;
    }

    ConversionCaculator.prototype.runCalculations = function (el,pageObj) {
      if (!el) {
        return;
      }
      var pageObj = (pageObj) ? pageObj: el.pageObj;
      if (!pageObj) {
        return;
      }

      var frmCalc = document.getElementById('frmCalc');
      var InUnit = null;
      var UnitList = null;
      var Results = null;

      switch (el.id) {
        case "InUnit":
          InUnit = el;
          UnitList = document.getElementById('UnitList');
          break;
        case "UnitList":
          InUnit = document.getElementById('InUnit');
          UnitList = el;
          break;
      }
      switch (UnitList.value) {
        case "Microns":
          Results = InUnit.value;
          break;
        case "Inches":
          Results = InUnit.value / pageObj.InchFactor;
          break;
        case "mm":
          Results = InUnit.value / pageObj.mmFactor;
          break;
        case "Nanometers":
          Results = InUnit.value / pageObj.NanometerFactor;
          break;
        case "Microinches":
          Results = InUnit.value / pageObj.MicroinchFactor;
          break;
        case "Angstroms":
          Results = InUnit.value / pageObj.AngstromFactor;
          break;
        case "Mils":
          Results = InUnit.value / pageObj.MilFactor;
          break;
      }

      changeText("Microns",Results);
      changeText("Inches",Results * pageObj.InchFactor);
      changeText("mm",Results * pageObj.mmFactor);
      changeText("Nanometers",Results * pageObj.NanometerFactor);
      changeText("Microinches",Results * pageObj.MicroinchFactor);
      changeText("Angstroms",Results * pageObj.AngstromFactor);
      changeText("Mils",Results * pageObj.MilFactor);
    }

    ConversionCaculator._initialized = true;
  }
}


function initConversionCaculator_obj() {
  calculator.init();
}
