//All content contained in this file is copyright 2008 by FirstHousePage.com.
//Copying is strictly forbidden without prior consent from Tim Ballard.

function calculateBudget() {

	//Income
	income1 = 1 * document.getElementById('income1').value;
	income1_frq = 1 * document.getElementById('income1_frq').value;
	benefits = 1 * document.getElementById('benefits').value;
	benefits_frq = 1 * document.getElementById('benefits_frq').value;
	interest = 1 * document.getElementById('interest').value;
	interest_frq = 1 * document.getElementById('interest_frq').value;
	other_income = 1 * document.getElementById('other_income').value;
	other_income_frq = 1 * document.getElementById('other_income_frq').value;

	//Outgoings
	//Household spending
	food_drink = 1 * document.getElementById('food_drink').value;
	food_drink_frq = 1 * document.getElementById('food_drink_frq').value;
	council_tax = 1 * document.getElementById('council_tax').value;
	council_tax_frq = 1 * document.getElementById('council_tax_frq').value;
	energy = 1 * document.getElementById('energy').value;
	energy_frq = 1 * document.getElementById('energy_frq').value;
	water = 1 * document.getElementById('water').value;
	water_frq = 1 * document.getElementById('water_frq').value;
	phone = 1 * document.getElementById('phone').value;
	phone_frq = 1 * document.getElementById('phone_frq').value;
	other_household = 1 * document.getElementById('other_household').value;
	other_household_frq = 1 * document.getElementById('other_household_frq').value;

	//Financial spending
	loan = 1 * document.getElementById('loan').value;
	loan_frq = 1 * document.getElementById('loan_frq').value;
	insurance = 1 * document.getElementById('insurance').value;
	insurance_frq = 1 * document.getElementById('insurance_frq').value;
	other_financial = 1 * document.getElementById('other_financial').value;
	other_financial_frq = 1 * document.getElementById('other_financial_frq').value;

	//Leisure spending
	going_out = 1 * document.getElementById('going_out').value;
	going_out_frq = 1 * document.getElementById('going_out_frq').value;
	alcohol_smoking = 1 * document.getElementById('alcohol_smoking').value;
	alcohol_smoking_frq = 1 * document.getElementById('alcohol_smoking_frq').value;
	tv = 1 * document.getElementById('tv').value;
	tv_frq = 1 * document.getElementById('tv_frq').value;
	holidays = 1 * document.getElementById('holidays').value;
	holidays_frq = 1 * document.getElementById('holidays_frq').value;
	leisure_other = 1 * document.getElementById('leisure_other').value;
	leisure_other_frq = 1 * document.getElementById('leisure_other_frq').value;

	//Spending on children
	childcare = 1 * document.getElementById('childcare').value;
	childcare_frq = 1 * document.getElementById('childcare_frq').value;
	other_children = 1 * document.getElementById('other_children').value;
	other_children_frq = 1 * document.getElementById('other_children_frq').value;

	//Travel spending
	commuting = 1 * document.getElementById('commuting').value;
	commuting_frq = 1 * document.getElementById('commuting_frq').value;
	other_travel = 1 * document.getElementById('other_travel').value;
	other_travel_frq = 1 * document.getElementById('other_travel_frq').value;

	//Other Spending
	other_other = 1 * document.getElementById('other_other').value;
	other_other_frq = 1 * document.getElementById('other_other_frq').value;



	//Calculate total annual income
	total_annual_income = income1 * income1_frq +
	benefits * benefits_frq +
	interest * interest_frq +
	other_income * other_income_frq;

	//Calculate total household spending
	total_household_spending = food_drink * food_drink_frq +
	council_tax * council_tax_frq +
	energy * energy_frq +
	water * water_frq +
	phone * phone_frq +
	other_household * other_household_frq;

	//Calculate total financial spending
	total_financial_spending = loan * loan_frq +
	insurance * insurance_frq +
	other_financial * other_financial_frq;

	//Calculate total leisure spending
	total_leisure_spending = going_out * going_out_frq +
	alcohol_smoking * alcohol_smoking_frq +
	tv * tv_frq +
	holidays * holidays_frq +
	leisure_other * leisure_other_frq;

	//Calculate total spending on children
	total_children_spending = childcare * childcare_frq +
	other_children * other_children_frq;

	//Calculate total travel spending
	total_travel_spending = commuting * commuting_frq +
	other_travel * other_travel_frq;

	//Calculate total other spending
	total_other_spending = other_other * other_other_frq;

	//Calculate total annual outgoings
	total_annual_spending = total_household_spending + total_financial_spending + total_leisure_spending + total_children_spending + total_travel_spending + total_other_spending;

	//Calculate budget
	budget = total_annual_income - total_annual_spending;
	budgetMonthly = budget / 12;
	// String to capture output...
	var str = '';
	
	// Do calculations and build output...

	household_spending_percent = Math.round(total_household_spending / total_annual_income * 100)/100;
	financial_spending_percent = Math.round(total_financial_spending / total_annual_income * 100)/100;
	leisure_spending_percent = Math.round(total_leisure_spending / total_annual_income * 100)/100;
	children_spending_percent = Math.round(total_children_spending / total_annual_income * 100)/100;
	travel_spending_percent = Math.round(total_travel_spending / total_annual_income * 100)/100;
	other_spending_percent = Math.round(total_other_spending / total_annual_income * 100)/100;

	str = str +
			"<h3>Budget Results</h3>" + 
			"<p>Total annual income:" + formatMoney(total_annual_income) +
			"<br />Total annual spending:" + formatMoney(total_annual_spending) +
			"</p><p>You have a budget of " + formatMoney(budget) +
			" each year, which is " + formatMoney(budgetMonthly) + " each month.</p>" +
			"<h4>Spending Analysis</h4>" +
			"<div style=\"text-align:center\"><table class=\"border\">" +
			"<tr>" +
			"<th>Area of Spending</th>" +
			"<th>Spending per year</th>" +
			"<th>Spending per month</th>" +
			"<th>% of income</th>" +
			"</tr>" +
			"<tr>" +
			"<td>Household spending</td>" +
			"<td>&pound;" + FormatNumber(total_household_spending, 2) + "</td>" +
			"<td>&pound;" + FormatNumber(total_household_spending / 12, 2) + "</td>" +
			"<td>" + household_spending_percent * 100 + "%</td>" +
			"</tr>" +
			"<tr>" +
			"<td>Financial spending</td>" +
			"<td>&pound;" + FormatNumber(total_financial_spending, 2) + "</td>" +
			"<td>&pound;" + FormatNumber(total_financial_spending / 12, 2) + "</td>" +
			"<td>" + financial_spending_percent * 100 + "%</td>" +
			"</tr>" +
			"<tr>" +
			"<td>Leisure spending</td>" +
			"<td>&pound;" + FormatNumber(total_leisure_spending, 2) + "</td>" +
			"<td>&pound;" + FormatNumber(total_leisure_spending / 12, 2) + "</td>" +
			"<td>" + leisure_spending_percent * 100 + "%</td>" +
			"</tr>" +
			"<tr>" +
			"<td>Children spending</td>" +
			"<td>&pound;" + FormatNumber(total_children_spending, 2) + "</td>" +
			"<td>&pound;" + FormatNumber(total_children_spending / 12, 2) + "</td>" +
			"<td>" + children_spending_percent * 100 + "%</td>" +
			"</tr>" +
			"<tr>" +
			"<td>Travel spending</td>" +
			"<td>&pound;" + FormatNumber(total_travel_spending, 2) + "</td>" +
			"<td>&pound;" + FormatNumber(total_travel_spending / 12, 2) + "</td>" +
			"<td>" + travel_spending_percent * 100 + "%</td>" +
			"</tr>" +
			"<tr>" +
			"<td>Other spending</td>" +
			"<td>&pound;" + FormatNumber(total_other_spending, 2) + "</td>" +
			"<td>&pound;" + FormatNumber(total_other_spending / 12, 2) + "</td>" +
			"<td>" + other_spending_percent * 100 + "%</td>" +
			"</tr>" +
			"</table></div>";
	
	document.getElementById('result').innerHTML = str;
	
}

/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Morley Computing | http://www.morley-computing.co.uk/
Modified by Tim Ballard */
var currency = '&pound;';

//
// Do the calculation. 
//
function calculatePayment(form) {

	// P = Lr(1+r/1200)^n/[1200{(1+r/1200)^n - 1}]
	// P is monthly payment
	// L is loan amount
	// r is interest rate
	// n is number of months

	L = 1 * form.amount.value;
	r = 1 * form.interest.value;
	n = 12 * form.years.value;//Convert years to months
	
	// String to capture output...
	var str = '';
	
	// Do calculations and build output...
	P = doCompoundCalculation(L, r, n);
	str = str +
			"<h3>Mortgage Repayment Results</h3>" + 
			"<strong>Total amount repayable:</strong> " + formatMoney(n*P) +
			"<br /><strong>Monthly repayment:</strong> " + formatMoney(P) +
			"<p>For a more detailed analysis of your mortgage repayments, including the affect of inflation, download our <a title=\"Mortgage Repayment Spreadsheet\" href=\"Mortgage_Repayment_Spreadsheet.xls\">Mortgage Repayment Spreadsheet</a>.  You may also want to consider whether you can afford your mortgage repayments after living expenses such as food, insurance, petrol, going out etc.  To work out your living expenses, check out our <a href=\"http://www.firsthousepage.com/budget_calculator.php\">budget calculator</a>.</p>";
	
	document.getElementById('result').innerHTML = str;
	
}

//
// Calculate payment using a compounded rate. 
//
function doCompoundCalculation(L, r, n) {
	// L is loan amount
	// r is APR
	// n is number of months

	P = L * r * Math.pow(1+r/1200,n) / (1200 * (Math.pow(1+r/1200,n) - 1));
	return P;
}

//
// Only allow digits and decimal point.
//
function setNumericOnly(object) {
	value = object.value;
	object.value = value.replace(/[^\d\.]/g, "");
}

//
// Format money to 2dp and add commas...
// Based on script by: Cyanide_7
// See: http://javascript.internet.com/forms/currency-format.html
// 
function formatMoney(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	dec = num%100;
	num = Math.floor(num/100).toString();
	if(dec<10) dec = "0" + dec;
	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 currency + (((sign)?'':'-') + num + '.' + dec);
}


function calculate_borrowing()
  {

var income1 = document.getElementById('income1').value;
var maxborrowing1 = 0.00
var maxborrowing2 = 0.00

maxborrowing1 = FormatNumber(income1 * 3.5, 2);//Lowest maximum borrowing
maxborrowing2 = FormatNumber(income1 * 5, 2);//Highest maximum borrowing

document.getElementById('maxborrowing1').value=maxborrowing1;
document.getElementById('maxborrowing2').value=maxborrowing2;

  }
function calculate_borrowing2()
  {

var income1 = document.getElementById('income2').value * 1;
var income2 = document.getElementById('income3').value * 1;

var maxborrowing3 = 0.00
var maxborrowing4 = 0.00

totalIncome = income1 + income2;

maxborrowing3 = FormatNumber(totalIncome * 3, 2);//Lowest maximum borrowing
maxborrowing4 = FormatNumber(totalIncome * 3.5, 2);//Highest maximum borrowing

document.getElementById('maxborrowing3').value=maxborrowing3;
document.getElementById('maxborrowing4').value=maxborrowing4;

  }


function calculate_stamp()
  {

  var propertyvalue = document.getElementById('propvalue').value;
  var stamp = 0.00

 if(propertyvalue>120000)
	{
	if(propertyvalue>250000)
	  {
	  if(propertyvalue>500000)
		{
		//>£500,000
		stamp = FormatNumber((propertyvalue/100)*4, 2);
		}
	  else
		{
		//£250,000 - £500,000
		stamp = FormatNumber((propertyvalue/100)*3, 2);
		}
	  }
	else
	  {
	  //£120,000 - £250,000
	  stamp = FormatNumber(propertyvalue/100, 2);
	  }
	}
  document.getElementById('stamp').value=stamp;
  }

function FormatNumber(Number,Decimals,Separator)
{
 Number += ""          // Force argument to string.
 Decimals += ""        // Force argument to string.
 Separator += ""       // Force argument to string.
 if((Separator == "") || (Separator.length > 1))
  Separator = "."
 if(Number.length == 0)
  Number = "0"
 var OriginalNumber = Number  // Save for number too large.
 var Sign = 1
 var Pad = ""
 var Count = 0
 // If no number passed, force number to 0.
 if(parseFloat(Number)){
  Number = parseFloat(Number)} else {
  Number = 0}
 // If no decimals passed, default decimals to 2.
 if((parseInt(Decimals,10)) || (parseInt(Decimals,10) == 0)){
  Decimals = parseInt(Decimals,10)} else {
  Decimals = 2}
 if(Number < 0)
 {
  Sign = -1         // Remember sign of Number.
  Number *= Sign    // Force absolute value of Number.
 }
 if(Decimals < 0)
  Decimals *= -1    // Force absolute value of Decimals.
 // Next, convert number to rounded integer and force to string value.
 // (Number contains 1 extra digit used to force rounding)
 Number = "" + Math.floor(Number * Math.pow(10,Decimals + 1) + 5)
 if((Number.substring(1,2) == '.')||((Number + '')=='NaN'))
  return(OriginalNumber) // Number too large to format as specified.
 // If length of Number is less than number of decimals requested +1,
 // pad with zeros to requested length.
 if(Number.length < Decimals +1) // Construct pad string.
 {
  for(Count = Number.length; Count <= Decimals; Count++)
   Pad += "0"
 }
 Number = Pad + Number // Pad number as needed.
 if(Decimals == 0){
  // Drop extra digit -- Number is formatted.
  Number = Number.substring(0, Number.length -1)} else {
  // Or, format number with decimal point and drop extra decimal digit.
 Number = Number.substring(0,Number.length - Decimals -1) +
		  Separator +
		  Number.substring(Number.length - Decimals -1,
		  Number.length -1)}
 if(Sign == -1)
  Number = "-" + Number  // Set sign of number.
 if(Number.length == 0)
  Number="0"
 return(Number)
}

function ForceNumeric(nValue)
{
	validChars = "0123456789.";
	newValue="";
	for(k = 0; k < nValue.length; k++)
	{
		thisChar = nValue.charAt(k);
		if(validChars.indexOf(thisChar) != -1) newValue += thisChar;
	}
	return newValue;
}
function close_window() {
	window.close();
}