//calcualtor.js
//This file is for the various versions of the revenue calculator

//////////////////////////////////////////////////////////////
//This processes the calculator that takes the downloads 
//per month and searches per month
//////////////////////////////////////////////////////////////
function calculate_users_by_downloads() {
  var numDownloads = document.forms["calc"].downloads.value;
  var numSearches  = document.forms["calc"].searches.value;
  var commission   = .10;
  var eps          = .05;
  if(numDownloads == '' || numDownloads == null || isNaN(numDownloads)) {
    alert("Please enter the number of toolbar downloads!");
    return;
  }
  if(numSearches == '' || numSearches == null || isNaN(numSearches)) {
    alert("Please enter the average number of searches per toolbar");
    return;
  }
  var result = numDownloads * numSearches * commission * eps;
  document.getElementById("calc_result").innerHTML = 
    "<span class='calc_estimate_month_1'>" +
    "That's about " + formatCurrency(result) +
    " the 1st month, </span>" +
    "<span class='calc_estimate_month_2'> and " +           
    formatCurrency(result*2) + " the 2nd month..." +
    "</span>" +
    "<span>If you keep up those rates, that's</span> " +
    "<span class='calc_estimate_year_1'>" +
    formatCurrency(result*78) + " in the 1st year." +
    "</span>" +
    "<a href=\"index.cgi?action=create_account\">Get Started! " +
    "<p style=\"display: inline; margin: 0px; padding: 0px; font-size: 16px; font-weight: bold;\">&#187;</p>" +
    "</a>";
}

///////////////////////////////////////////////////////////////
// A different version of the calculator that assumes more
// because the affiliate may not know all the information
// this calculator is by users only
//////////////////////////////////////////////////////////////
function calculate_by_users() {
  var numUsers     = document.forms["calc"].users.value;
  var rate         = .15;
  var epu          = .15;
  if(numUsers == '' || numUsers == null || isNaN(numUsers) || numUsers < 0) {
    alert("Please enter the number of users of your app or website!");
    return;
  }

  var result = numUsers * rate * epu;
  document.getElementById("calc_result").innerHTML = 
      "<span class='calc_estimate_month_1'>" +
      "You could make " + formatCurrency(result) +
      " the 1st month, </span>" +
      "<span class='calc_estimate_month_2'> and " +           
      formatCurrency(result*2) + " the 2nd month..." +
      "</span>" +
      "<span>If you keep up those rates, that's</span> " +
      "<span class='calc_estimate_year_1'>" +
      formatCurrency(result*78) + " in the 1st year." +
      "</span>";
}

///////////////////////////////////////////////
// This turns a number into a pretty $value
//////////////////////////////////////////////
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    if(cents<10) {
        cents = "0" + cents;
    }
    
    num = Math.floor(num/100).toString();
      for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
        num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
      }
    return '\$' + num + '.' + cents;
}