// JavaScript for Artificial Grass Expense Calculator

var errors = Array('Gardener/Maintenance Fees', 'Water Expenses', 'Other', 'Anticipated water price increase', 'width', 'length');
var fields = Array('fees', 'expenses', 'other', 'inflate', 'width', 'length');
var errNum = 0;

function formatDollar(num)
{ /*Commas, no decimal, dollar sign*/
  return (num > 0 ? '$' + formatMoney(Math.abs(num),0) : '$0');
}

function formatMoney(n, c)
{
    var m = (c = Math.abs(c) + 1 ? c : 0,/(\d+)(?:(\.\d+)|)/.exec(n + "")), x = m[1].length > 3 ? m[1].length % 3 : 0;
    return (x ? m[1].substr(0, x) + ',' : "") + m[1].substr(x).replace(/(\d{3})(?=\d)/g,"$1" + ',') + (c ? '.' + (+m[2] || 0).toFixed(c).substr(2) : "");
}

function getDollarAmnt(str)
{ /*Might need to strip dollar sign*/
  var tmp = str.match(/^\s*\$?(\d+(\.?\d+)?)\s*$/);
  if (tmp === null)
  {
    alert("Please enter a number in the " + errors[errNum] + " field.\n" + 
          'If you have no monthly ' + errors[errNum] + ', please enter "0".');
    document.getElementById(fields[errNum]).focus();
    return false;
  }

  errNum++;
  return tmp[1] * 1;
}

function getNumber(str)
{ /*Just numeric*/
  var tmp = str.match(/^\s*(\d+(\.\d+)?)\s*$/);
  if (tmp === null)
  {
    alert("Please enter the length and width (in feet) of the area you are considering for turf.");
    /*alert("Please enter the " + errors[errNum] + " (in feet) of the area you are considering for turf.");*/
    document.getElementById(fields[errNum]).focus();
    return false;
  }

  errNum++;
  return tmp[1] * 1;
}

function getPercent(str)
{ /*Just numeric*/
  var tmp = str.match(/^\s*(\d+(\.\d+)?)%?\s*$/);
  if (tmp === null)
  {
    alert("Please enter the percentage.");
    document.getElementById(fields[errNum]).focus();
    return false;
  }
  errNum++;

  return tmp[1] / 100;
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}



function new_calc() {
	document.getElementById('water_bill_rate_increase').value = '';
	document.getElementById('ten_year_total_savings').value = '';
	document.getElementById('five_year_total_savings').value = '';
	document.getElementById('landscaping_costs').value = '';
	
	//alert('test');
	
	expenses = document.getElementById('expenses').value.replace(/[\D\.]+/g,'');
	fees = document.getElementById('fees').value.replace(/[\D\.]+/g,'');
	other = document.getElementById('other').value.replace(/[\D\.]+/g,'');
	if(expenses == '') {
		alert("Please provide a dollar amount for your monthly water bill.");
	}
	else {
		if(fees == '') {
			fees = parseInt(0);
		}
		if(other == '') {
			other = parseInt(0);
			document.getElementById('other').value = other;
		}
		
		monthly_expense = parseInt(expenses) + parseInt(fees) + parseInt(other);		
		new_monthly_water_cost = parseFloat(expenses*1.3);
		//alert('new_monthly_water_cost - ' + new_monthly_water_cost);
		new_monthly_total_savings_water_cost = parseFloat(new_monthly_water_cost*.7);
		new_monthly_total_savings = parseInt(new_monthly_total_savings_water_cost)+parseInt(fees)+parseInt(other);
		display_monthly_total_savings = parseInt(expenses*.7)+parseInt(fees)+parseInt(other);
		//alert('new_monthly_total_savings_water_cost - ' + new_monthly_total_savings_water_cost);
		//alert('new_monthly_total_savings - ' + new_monthly_total_savings);
		five_year_total_savings = parseFloat(new_monthly_total_savings*60);
		ten_year_total_savings = parseFloat(new_monthly_total_savings*120);
		
		//alert('savings - ' + new_monthly_total_savings);
		//document.getElementById('landscaping_costs').value = '$' + addCommas(monthly_expense);
		document.getElementById('landscaping_costs').value = '$' + addCommas(display_monthly_total_savings);
		document.getElementById('five_year_total_savings').value = '$'+addCommas(five_year_total_savings);
		document.getElementById('ten_year_total_savings').value = '$'+addCommas(ten_year_total_savings);
		document.getElementById('water_bill_rate_increase').value = '$'+addCommas(parseInt(new_monthly_water_cost));
		
	}
}

function calculate() {
  var turfPriceLo = 11;
  var turfPriceHi = 16;
  var financeTerm = 60;
  var term = 120;
  
  errNum = 0;

  /*get values*/
  var fees = getDollarAmnt(document.getElementById('fees').value);
  if (fees === false) { return; }
  var expenses = getDollarAmnt(document.getElementById('expenses').value);
  if (expenses === false) { return; }
  var other = getDollarAmnt(document.getElementById('other').value);
  if (other === false) { return; }
  var inflate = getPercent(document.getElementById('inflate').value);
  if (inflate === false) { return; }
  
  var width = getNumber(document.getElementById('width').value);
  if (width === false) { return; }
  var length = getNumber(document.getElementById('length').value);
  if (length === false) { return; }
  
  var savingsDef = document.getElementById('savingsDef');
  var savingsDefMo = document.getElementById('SaveDefMonth');
  var tenSaveDef = document.getElementById('tenSaveDef');
  
  /*calc stuff*/
	var displayNetExpense = fees + expenses + other;
	var netExpense = fees + other;
	var monthExpense = 0;
	
	var waterBillIncrease = expenses;
	// Old One is - var waterBillIncrease = expenses + (expenses * inflate);
	//alert('waterBillIncrease - ' + waterBillIncrease);
	
	if (expenses == 0) 
	{
		monthExpense = netExpense;
	}
  else 
	{
		monthExpense = waterBillIncrease + netExpense;
	}
	
  var costLow = (turfPriceLo * width * length);
  var costHigh = (turfPriceHi * width * length);
	
	/*apply the financeTerm variable */
	costLow = costLow / financeTerm;
	costHigh = costHigh / financeTerm;
	/*done applying the financeTerm variable */
	
  var savingsHigh = monthExpense - costLow;
  var savingsLow = monthExpense - costHigh;
  
  var tenExpense = term * monthExpense;
  var tenCostLow = width * length * turfPriceLo;
  var tenCostHigh = width * length * turfPriceHi
  var tenSaveHigh = tenExpense - tenCostLow;
  var tenSaveLow = tenExpense - tenCostHigh;
  
	if (expenses == 0)
	{
		costLow = 0;
		costHigh = 0;
	}
  
  /*write the information back to the webpage*/
  document.getElementById('netExpense').value = formatDollar(displayNetExpense);
  document.getElementById('monthExpense').value = formatDollar(monthExpense);
  
  document.getElementById('costLow').value = formatDollar(costLow);
  document.getElementById('costHigh').value = formatDollar(costHigh);
  
  document.getElementById('savingsLow').value = formatDollar(savingsLow);
  document.getElementById('savingsHigh').value = formatDollar(savingsHigh);
  if (savingsLow <= 0)
  {
    if (savingsHigh <= 0)
    {
      savingsDef.value = "$0"
    }
    else
    {
      savingsDef.value = "up to " + formatDollar(savingsHigh);
    }
    savingsDef.parentNode.style.visibility = "visible";
    savingsDef.parentNode.parentNode.style.visibility = "hidden";
    if (savingsHigh <= 0)
    {
      savingsDefMo.style.visibility = "hidden";
    }
    else
    {
      savingsDefMo.style.visibility = "visible";
    }
  }
  else
  {
    savingsDef.parentNode.style.visibility = "hidden";
    savingsDefMo.style.visibility = "hidden";
    savingsDef.parentNode.parentNode.style.visibility = "visible";
  }
  
  document.getElementById('tenExpense').value = formatDollar(tenExpense);
  document.getElementById('tenSaveLow').value = formatDollar(tenSaveLow);
  document.getElementById('tenSaveHigh').value = formatDollar(tenSaveHigh);
  if (tenSaveLow <= 0)
  {
    if (tenSaveHigh <= 0)
    {
      tenSaveDef.value = "$0"
    }
    else
    {
      tenSaveDef.value = "up to " + formatDollar(tenSaveHigh)
    }
    tenSaveDef.parentNode.style.visibility = "visible";
    tenSaveDef.parentNode.parentNode.style.visibility = "hidden";
  }
  else
  {
    tenSaveDef.parentNode.style.visibility = "hidden";
    tenSaveDef.parentNode.parentNode.style.visibility = "visible";
  }
}

$(document).ready(function() {
	// set the stage
	/*
	$("#sitemap-nav li:nth-child(3)").addClass("go-right");
	$("#sitemap-nav li:nth-child(5)").addClass("go-right");
	$("#sitemap-container").append("<span class='the-closer smaller pointer'><a>[close]</a></span>");
	$("div#turf-login").prepend("<div class='smaller'></div>");
	
	// clickable link to show site-map
	$("#jqsm").hover(
		function () {
			$("#sitemap-container").slideDown('slow');
		}, 
  		function () {
			$("#sitemap-wrapper").show();
		}
    );
  
    // clickable close button
	$("#sitemap-container .the-closer").click(
		function () {
			$("#sitemap-container").slideUp('slow');
		}
    );
  
    // close on hover
	$("#sitemap-container").hover(
  		function () {
			$("#sitemap-wrapper").show();
		}, 
  		function () {
			$("#sitemap-container").slideUp('slow');
  		}
    );
*/
// close jQuery
});

