var curvyCornersVerbose = false;

addEvent(window, 'load', initCorners);
function initCorners() {
	var setting = {
			tl: { radius: 8 },
			tr: { radius: 8 },
			bl: { radius: 8 },
			br: { radius: 8 },
			antiAlias: true
		}
	var setting2 = {
			tl: { radius: 18 },
			tr: { radius: 18 },
			bl: { radius: 18 },
			br: { radius: 18 },
			antiAlias: true
		}
	//curvyCorners(setting, ".roundedBorder");
	curvyCorners(setting2, "#container");
}


function salary_checknum (val)
{
	str = val;
	for (var i = 0; i < str.length; i++) {
		var ch = str.substring( i, i + 1)
		if ((ch < "0" || "9" < ch) && ch != '.') {
			alert("Please try again, all values should be a number.");
			return false;
		}
	}
	return true;
}

function max(val1, val2) {
  if (val1 > val2) {
    return val1;
  }
  return val2;
}

function salary_pound_round (val)
{
	return Math.round(val * 100) / 100 ;
}

function salary_validate ()
{
	a = salary_checknum (document.salary_calc.salary.value);
	b = salary_checknum (document.salary_calc.pension.value);
}

function salary_calculate()
{
	if (salary_validate() < 1)
	{
		document.salary_calc.monthly.value = "";
		document.salary_calc.interest.value = ""; 
		return false;
	}

	allowance 		= 6475;
	if (document.salary_calc.age[1].checked) allowance 		= 9490;
	if (document.salary_calc.age[2].checked) allowance 		= 9640;
	low_allowance 	= 0;
	low_rate 		= 0.10;
	basic_allowance = 37400;
	basic_rate 		= 0.20;
	higher_rate 	= 0.40;
	ni_allowance 	= 97+64*12/52;
	ni_rate 		= 0.11;
	ni_high_allow	= 844;
	ni_high_rate	= 0.01;
	stdnt_loan_thrs = 15000;
	stdnt_loan_rate	= 0.09;
	
	pension = document.salary_calc.pension.value/100;
	salary = document.salary_calc.salary.value;
	taxable_pay = salary * (1 - pension);
	
	tax = 0;
	if (taxable_pay <= (allowance + low_allowance) && taxable_pay > allowance ) {
		tax = (taxable_pay - allowance) * low_rate ;
	}
	if (taxable_pay <= (allowance + low_allowance + basic_allowance ) && taxable_pay > (allowance + low_allowance) ) {
		tax = low_allowance * low_rate + (taxable_pay - allowance - low_allowance ) * basic_rate ;
	}
	if (taxable_pay > (allowance + low_allowance + basic_allowance ) ) {
		tax = low_allowance * low_rate + basic_allowance * basic_rate + (taxable_pay - allowance - low_allowance - basic_allowance ) * higher_rate; 
	}

	ni = 0
	if (salary > (ni_allowance * 365.0/7) && salary <= (ni_high_allow * 365.0/7)) {
		ni = (salary - (ni_allowance * 365.0/7)) * ni_rate 
	}	
	if (salary > (ni_high_allow * 365.0/7)) {
		ni = ((ni_high_allow - ni_allowance) * 365.0/7) * ni_rate + (salary - (ni_high_allow * 365.0/7)) * ni_high_rate ;
	}	

	stdnt_loan_payment = 0;
	if (document.salary_calc.stdnt_loan.checked) {
		stdnt_loan_payment = max(0, Math.round(((salary - stdnt_loan_thrs) * stdnt_loan_rate) / 12 - 0.5));
	}


	document.salary_calc.monthly_take_home.value = Math.round(((taxable_pay - tax - ni) / 12  - stdnt_loan_payment) * 100) / 100 ;
	document.salary_calc.monthly_tax.value = salary_pound_round(tax / 12);
	document.salary_calc.monthly_ni.value = salary_pound_round(ni / 12);
	document.salary_calc.monthly_pension.value = salary_pound_round(salary * pension / 12);
	document.salary_calc.monthly_loan.value = salary_pound_round(stdnt_loan_payment);

}

function checknum (val)
{
	str = val;
	for (var i = 0; i < str.length; i++) {
		var ch = str.substring( i, i + 1)
		if ((ch < "0" || "9" < ch) && ch != '.') {
			alert("Please try again, all values should be a number.  Only digits from 0 to 9, or a decimal point should be input.");
			return false;
		}
	}
	return true;
}

function validate ()
{
	time = document.mortgage_calc.time.value;
	a = checknum (document.mortgage_calc.amount.value);
	b = checknum (document.mortgage_calc.time.value);
	c = checknum (document.mortgage_calc.rate.value);

	if ((a + b + c) < 3 ) {return false;}
	if (time>=5 && time<=30)
	{
		return true
	}
	else
	{
		alert("Mortgage period must be between 5 and 30 years")
		return false
	}
}

function calculate()
{
	if (validate() < 1)
	{
		document.mortgage_calc.monthly.value = "";
		document.mortgage_calc.interest.value = ""; 
		return false;
	}
	rate = document.mortgage_calc.rate.value / 100;
	time = document.mortgage_calc.time.value;
	amount = document.mortgage_calc.amount.value;
	monthly = 100*amount * rate / 12 * (1/(1-(Math.pow(1/(1+rate),time))));
	document.mortgage_calc.monthly.value = Math.round(monthly)/100;
	document.mortgage_calc.interest.value = Math.round(100*amount * rate / 12)/100;

}



