BMW_MCalc = function(container, instanceName) {
    // === Private Vars ===
    var MAX_MORTGAGE = 729750;               // Maximum qualification threshold
    var QUALIFICATION_PERCENTAGE = .31;      // Qualification percentage
    var BMW_URL = 'http://www.bankingmyway.com/product/search/MG/1?zipcode=';     // BMW Base URL
    var TOTAL_PAGES = 2;                     // Total number of tool pages (not including results)

    var containerName = container;                    // Container div name
    var containerEl = document.getElementById(container);  // Container element for widget
    var instanceName = instanceName;                  // Name of current instance
    var threshold = 0;                                // monthlyGross * 0.31
    var zip = '';                                     // User zip code


    // === Private Methods ===
    /**
     * Remove , from data entry
     * @param v Value to clean
     * @return Cleaned value
     */
    var cleanNumber = function(v) { return trim(v.replace(/[,\$]/g, '')); }

    /**
     * Checks to see if numbers qualify for program.
     * @param oForm Form object
     * @return boolean ture if user qualifies
     */
    var checkQualify = function(oForm) {
        var totalMortgage = cleanNumber(oForm.txtBal.value);
        var monthlyMortgage = cleanNumber(oForm.txtMP.value);
        var monthlyIncome = cleanNumber(oForm.txtIncome.value) / 12;
        zip = cleanNumber(oForm.txtZip.value);

        // check max mortgage
        if (totalMortgage >= MAX_MORTGAGE) { return false; }

        // check qualification amount and round to 2 digits
        threshold = monthlyIncome * QUALIFICATION_PERCENTAGE;
        threshold = threshold.toFixed(2);

        return (monthlyMortgage > parseFloat(threshold) ? true : false);
    }

    /**
     * Trim leading and trailing spaces from value
     * @param v Value to trim
     * @return Trimmed value
     */
    var trim = function(v) { return v.replace(/^\s*|\s*$/g,''); }


    // === Public Object Interface ===
    return {
/**
 * Output results page.
 * @param success boolean true if user qualifies
 */
drawResultsPage: function(success) {
                     var divResults = document.getElementById(containerName + '_results');
					 var rkd = document.getElementById('rkd').value;
					 var img = rkd+"/calculator/startover_btn.gif";
                     this.showPage('results');  // hide instruction pages

                     var url = BMW_URL + zip;

                     if (success) {
                         var html = "<div class=\"quesion_area\"><h4>Results<\/h4><p>Congratulations! Based on your answers you are likely to qualify for the Home Mortgage Modification Plan.<\/p>"; 
                         html += "<p>Under this plan, your new payment would be approximately";
                         html += "<input id=\"momthlyPayment\" type=\"text\" value=\"$" + threshold + "\"> /month<\/p><\/div>";
                         //html += "<p>To find current available mortgage rates in your area, please:<\/p>"
                     } else {
                         var html = "<div class=\"quesion_area\"><h4>Results<\/h4><p>Unfortunately, it is unlikely you will qualify for this plan.<\/p><\/div>\n"; 
                         //html += "<p>To search for alternatives to the plan or for available mortgage rates in your area, please:<\/p>";
                     }

                     //html += "<div class=\"button_area\"><a href=\""+ url + "\" target=\"_blank\"><img id=\"visitBMW\" src=\"visit_bmw_btn.gif\"><\/a>";
                     html += "<div class=\"button_area\"><a href=\"javascript:bc.showPage(1);\"><img id=\"startOver\" src=\""+img+"\"><\/a><\/div>";
                     html += "<div class=\"pagination\">3\/3<\/div>\n";

                     divResults.innerHTML = html;
                 },

/**
 * Checks to see if numbers in the form are valid.
 * @param oForm Form object
 * @return boolean ture if all fields are valid.
 */
isValid: function(oForm) {
              var fields = ['txtBal', 'txtMP', 'txtIncome', 'txtZip'];

              for (var i in fields) {
                  field = fields[i];
                  var checkValue = cleanNumber(oForm[field].value);

                  if (isNaN(checkValue) || checkValue == '') {
                      alert('Please enter a valid number.');
                      oForm[field].focus();
                      return false;
                  }
              }

              return true;
          },

/**
 * Displays proper page.
 * @param page Page number or 'results'
 */
showPage: function(page) {
              for (var i = 1; i <= TOTAL_PAGES; i++) {
                  var divName = containerName + '_'+ i;
                  var divEl = document.getElementById(divName);  // Page element
                  divEl.style.display = (page == i ? 'block' : 'none');
              }

              var divEl = document.getElementById(containerName + '_results');
              divEl.style.display = (page == 'results' ? 'block' : 'none');

              var trackTag = 'mort_tool';
              if (page != 1) { trackTag += ' ' + page; }
              if (ms_track) { TSC.reporting.sendLinkEvent(trackTag); }
          },

/**
 * Form submit. Not a real submit.
 * @param Form object
 */
submit: function(oForm) {
            if (!this.isValid(oForm)) { return; }
            this.drawResultsPage(checkQualify(oForm));
        },

/**
 * Constructor - not used.
 */
init: function() {
      }
    }
}
