var minimumMonthlyCost = 50.00;

function unitCost(employee) {
  if (employee <= 20) {
    return 8.00;
  } else if (employee <= 50) {
    return 7.00;
  } else if (employee <= 100) {
    return 6.00;
  } else {
    return 5.00;
  }
}

function pricingTable(count) {
  if (count == 0) {
    return 0;
  } else {
    return unitCost(count) + pricingTable(count - 1);
  }
}

function totalMonthlyCost(count) {
  if (count == 0) {
    return 0;
  }
  monthlyCost = pricingTable(count)
  return monthlyCost < minimumMonthlyCost ? minimumMonthlyCost : monthlyCost;
}

function calculateSavings() {
  var employees = $.trim($('input#a').val());
  var milesPerWeek = $.trim($('input#b').val());
  var reimbursementRate = $.trim($('input#c').val());
	if (employees === '' || milesPerWeek === '' || reimbursementRate === '')
	{ 
	  alert("Please fill in all fields!");
		return false; 
	}
	var total = parseFloat(employees) * parseFloat(milesPerWeek) * 52 * parseFloat(reimbursementRate);
	
	var estimatedSavings = total * 0.25;

//	var costOfMileageTracker = totalMonthlyCost(employees) * 12;
//	var savingsWithMileageTracker = estimatedSavings - costOfMileageTracker;
//	var mileageCostAfter = total - savingsWithMileageTracker;

  $('.mileage-reimbursement input').val("$" + $.currency(total, {c: 0}));
  $('.estimated-savings input').val("$" + $.currency(estimatedSavings, {c: 0}));
	return true;
//  $('.annual-cost').val("$" + $.currency(costOfMileageTracker, {c: 0}));
//  if (employees > 0) {
//    $('.annual-savings').val("$" + $.currency(savingsWithMileageTracker, {c: 0}));
//  } else {
//    $('.annual-savings').val('');
//  }
//  $('.mileage-cost-after').val("$" + $.currency(mileageCostAfter, {c: 0}));
//  $('.savings-with').val("$" + $.currency((estimatedSavings - costOfMileageTracker),{c:0}));
}

$(function() {
  $('#roi-button').click(function(e) {
    e.preventDefault();

    if(calculateSavings())
		{
			$("div#calculator").hide();
			$("#roi-button").hide();
			$('div#results').slideDown("slow",function(){$('#roi-reset').show();});
			if ($('.savings-with').length > 0) {
				$('#results .textTrebuchetBlack20').addClass('paddingTB0');
			}
		}
  });
  
  $('#roi-reset').click(function(e) {  
    //e.preventDefault();
    
    //$('div#results').hide();
    //$("#roi-reset").hide(); 
    //$('div#calculator').slideDown("slow",function(){$('#roi-button').show();});
    //if ($('div#calculator').length <= 0) {
    //	$('#roi-button').show();
    //}
  });
});

//open the popup window
function wopen(url, name, w, h)
{
	// Fudge factors for window decoration space.
	// In my tests these work well on all platforms & browsers.
	var screenWidth = screen.width;
 	var screenHeight = screen.height;
	w += 32;
	h += 96;

	var leftPos = (screenWidth - w) /2;
	var topPos = (screenHeight - h) /2;

	var win = window.open(url,
		  name,
		  'width=' + w + ', height=' + h + ', ' +
		  'top' + topPos + ', left=' + leftPos +
		  'location=no, menubar=no, ' +
		  'status=no, toolbar=no, scrollbars=no, resizable=no');
	win.resizeTo(w, h);
	win.focus();
}
