// JavaScript Document

// Net Income calulation
function calcnet(num){
	tax=0;
	medicare=0;
	ygrossinc=num*12;
	if (ygrossinc < 6001) tax=0;
	if (ygrossinc > 6000 && ygrossinc < 21601) tax=((parseInt(ygrossinc)-6000)*0.17)/12;
	if (ygrossinc > 21600 && ygrossinc < 58001) tax=(parseInt(2562)+(parseInt(ygrossinc)-21600)*0.3)/12;
	if (ygrossinc > 58000 && ygrossinc < 70001) tax=(parseInt(13572)+(parseInt(ygrossinc)-58000)*0.42)/12;
	if (ygrossinc > 70000) tax=(parseInt(18612)+(parseInt(ygrossinc)-70000)*0.47)/12;
	if (ygrossinc > 16788) medicare=(ygrossinc*0.015)/12;
	return (num-parseInt(tax,10)-parseInt(medicare,10));
	}


function bank1(){
	//Establish total gross income, payment cap, and total expenses.
	tgrossinc=parseInt(grossincome,10)+parseInt(otherincome,10);
	tgrossinc2=parseInt(grossincome2,10)+parseInt(otherincome2,10);
	tgrossincjoint=parseInt(tgrossinc,10)+parseInt(tgrossinc2,10);
	paymentcap=tgrossincjoint*0.5;
	texp=parseInt(otherloans,10)+(parseInt(creditcardlimit,10)*0.03);
	texpjoint=parseInt(texp,10);
	//Set correct DSR.
	if (tgrossincjoint < parseInt(30000/12)) dsr=0;
	if (tgrossincjoint < parseInt(40000/12) && tgrossincjoint >= parseInt(30000/12)) dsr=0.32;
	if (tgrossincjoint < parseInt(50000/12) && tgrossincjoint >= parseInt(40000/12)) dsr=0.35;
	if (tgrossincjoint < parseInt(80000/12) && tgrossincjoint >= parseInt(50000/12)) dsr=0.42;
	if (tgrossincjoint >= parseInt(80000/12))dsr=0.47;
	// Establish maximum payments.
	maxpayments=tgrossincjoint*dsr;
	if (parseInt(maxpayments,10)+parseInt(texpjoint,10) > paymentcap)
		maxpayments=parseInt(paymentcap,10)-parseInt(texpjoint,10);
	periodmaxpayments=(maxpayments*12)/repaymentfreq;
	// Set variables for calulating borrowing power. Change the qualification rate here when necessary.
	qualrate=8.5;
	periodrate=(qualrate/repaymentfreq)/100;
	repayments=loanterm*repaymentfreq;
	// Apply figures to ammortisation formula to work out the maximum lend amount.
	maxlend=((periodmaxpayments)*(1-Math.pow(1+parseFloat(periodrate, 10),-repayments)))/periodrate;
	//document.write (tgrossincjoint+" "+dsr+" "+texpjoint+" "+maxpayments+" "+periodmaxpayments+" "+periodrate+" "+repayments+" "+maxlend);
	if (maxlend < 0) maxlend=0;
	return parseInt(maxlend,10);
	
}


function bank2(){
	//Establish total net income.
	tnetinc=calcnet(parseInt(grossincome,10)+parseInt(otherincome,10));
	tnetinc2=calcnet(parseInt(grossincome2,10)+parseInt(otherincome2,10));
	tnetincjoint=parseInt(tnetinc,10)+parseInt(tnetinc2,10);
	//Establish living expenses.
	jointadependants=adults;
	jointcdependants=dependants;
	alivingexp=0;
	if (jointadependants == 1) alivingexp=10400/12;
	if (jointadependants > 1) alivingexp=(((parseInt(jointadependants,10)-1)*4600)+10400)/12;
	clivingexp=0;
	if (jointcdependants == 1) clivingexp=3000/12;
	if (jointcdependants > 1) clivingexp=(((parseInt(jointcdependants,10)-1)*2800)+3000)/12;
	tlivingexp=parseInt(clivingexp,10)+parseInt(alivingexp,10);
	//Establish available income.
	availincome=parseInt(tnetincjoint,10)-parseInt(tlivingexp,10);
	//Establish other expenses.
	texp=parseInt(otherloans,10)+(parseInt(creditcardlimit,10)*0.05);
	texpjoint=parseInt(texp,10);
	// Establish maximum payments.
	maxpayments=((availincome)/1.1)-parseInt(texpjoint);
	//if (maxpayments < 0) maxpayments=0;
	periodmaxpayments=(maxpayments*12)/repaymentfreq;
	// Set variables for calulating borrowing power. Change the qualification rate here when necessary.
	qualrate=10.25;
	periodrate=(qualrate/repaymentfreq)/100;
	repayments=loanterm*repaymentfreq;
	// Apply figures to ammortisation formula to work out the maximum lend amount.
	maxlend=((periodmaxpayments)*(1-Math.pow(1+parseFloat(periodrate, 10),-repayments)))/periodrate;
	//document.write (tgrossincjoint+" "+dsr+" "+texpjoint+" "+maxpayments+" "+periodmaxpayments+" "+periodrate+" "+repayments+" "+maxlend);
	if (maxlend < 0) maxlend=0;
	//test=maxlend+" "+availincome+" "+tnetincjoint+" "+tlivingexp+" "+jointadependants+" "+alivingexp;
	return parseInt(maxlend,10);
	//return test;
	
}


function bank3(){
	//Establish total net income.
	tnetinc=calcnet(parseInt(grossincome,10)+parseInt(otherincome,10));
	tnetinc2=calcnet(parseInt(grossincome2,10)+parseInt(otherincome2,10));
	tnetincjoint=parseInt(tnetinc,10)+parseInt(tnetinc2,10);
	//Establish living expenses.
	jointadependants=adults;
	jointcdependants=dependants;
	alivingexp=0;
	if (adults == 1) {
		if (jointcdependants == 0) tlivingexp=10500/12;
		if (jointcdependants == 1) tlivingexp=14200/12;
		if (jointcdependants == 2) tlivingexp=17800/12;
		if (jointcdependants > 2) tlivingexp=(((parseInt(jointcdependants,10)-2)*3644)+17800)/12;
	}
	if (adults == 2) {
		if (jointcdependants == 0) tlivingexp=15000/12;
		if (jointcdependants == 1) tlivingexp=18700/12;
		if (jointcdependants == 2) tlivingexp=22300/12;
		if (jointcdependants > 2) tlivingexp=(((parseInt(jointcdependants,10)-2)*3644)+22300)/12;
	}
	//Establish available income.
	availincome=parseInt(tnetincjoint,10)-parseInt(tlivingexp,10);
	//Establish other expenses.
	texp=parseInt(otherloans,10)+(parseInt(creditcardlimit,10)*0.03);
	texpjoint=parseInt(texp,10);

	// Establish maximum payments.
	maxpayments=((availincome)/1)-parseInt(texpjoint);
	//if (maxpayments < 0) maxpayments=0;
	periodmaxpayments=(maxpayments*12)/repaymentfreq;
	// Set variables for calulating borrowing power. Change the qualification rate here when necessary.
	qualrate=8.47;
	periodrate=(qualrate/repaymentfreq)/100;
	repayments=loanterm*repaymentfreq;
	// Apply figures to ammortisation formula to work out the maximum lend amount.
	maxlend=((periodmaxpayments)*(1-Math.pow(1+parseFloat(periodrate, 10),-repayments)))/periodrate;
	//document.write (tgrossincjoint+" "+dsr+" "+texpjoint+" "+maxpayments+" "+periodmaxpayments+" "+periodrate+" "+repayments+" "+maxlend);
	if (maxlend < 0) maxlend=0;
	//test=maxlend+" "+availincome+" "+tnetincjoint+" "+tlivingexp+" "+jointadependants+" "+alivingexp;
	return parseInt(maxlend,10);
	//return test;
	
}


function bank4(){
	//Establish total net income.
	tnetinc=calcnet(parseInt(grossincome,10)+parseInt(otherincome,10));
	tnetinc2=calcnet(parseInt(grossincome2,10)+parseInt(otherincome2,10));
	tnetincjoint=parseInt(tnetinc,10)+parseInt(tnetinc2,10);
	//Establish living expenses.
	jointadependants=adults;
	jointcdependants=dependants;
	if (jointadependants == 1) {
		tlivingexp=(12000+(parseInt(jointcdependants,10)*2100))/12;
		}
	if (jointadependants == 2) {
		tlivingexp=(15600+(parseInt(jointcdependants,10)*2100))/12;
		}
	if (jointadependants > 2) {
		tlivingexp=(3600+((parseInt(jointadependants,10)-1)*12000)+(parseInt(jointcdependants,10)*2100))/12;
		}
		
	//Establish available income.
	availincome=parseInt(tnetincjoint,10)-parseInt(tlivingexp,10);
	//Establish other expenses.
	texp=parseInt(otherloans,10)+(parseInt(creditcardlimit,10)*0.05);
	texpjoint=parseInt(texp,10);
	// Establish maximum payments.
	maxpayments=(availincome/1)-parseInt(texpjoint);
	if (tnetincjoint/(parseInt(texpjoint,10)+parseInt(maxpayments,10)) < 1.4) 
		maxpayments = (tnetincjoint/1.4)-parseInt(texpjoint,10);
	//if (maxpayments < 0) maxpayments=0;
	periodmaxpayments=(maxpayments*12)/repaymentfreq;
	// Set variables for calulating borrowing power. Change the qualification rate here when necessary.
	qualrate=7.95;
	periodrate=(qualrate/repaymentfreq)/100;
	repayments=loanterm*repaymentfreq;
	// Apply figures to ammortisation formula to work out the maximum lend amount.
	maxlend=((periodmaxpayments)*(1-Math.pow(1+parseFloat(periodrate, 10),-repayments)))/periodrate;
	if (maxlend < 0) maxlend=0;
	return parseInt(maxlend,10);
	
}


function bank5(){
	//Establish total net income.
	tnetinc=calcnet(parseInt(grossincome,10)+parseInt(otherincome,10));
	tnetinc2=calcnet(parseInt(grossincome2,10)+parseInt(otherincome2,10));
	tnetincjoint=parseInt(tnetinc,10)+parseInt(tnetinc2,10);

	//Establish living expenses.
	jointadependants=adults;
	jointcdependants=dependants;
	alivingexp=0;
	if (adults == 1) {
		if (jointcdependants == 0) tlivingexp=800;
		if (jointcdependants == 1) tlivingexp=950;
		if (jointcdependants == 2) tlivingexp=1200;
		if (jointcdependants == 3) tlivingexp=1450;
		if (jointcdependants == 4) tlivingexp=1700;
		if (jointcdependants == 5) tlivingexp=1950;
		if (jointcdependants > 5) tlivingexp=(((parseInt(jointcdependants,10)-5)*225)+1950);
	}
	if (adults == 2) {
		if (jointcdependants == 0) tlivingexp=1010;
		if (jointcdependants == 1) tlivingexp=1265;
		if (jointcdependants == 2) tlivingexp=1500;
		if (jointcdependants == 3) tlivingexp=1775;
		if (jointcdependants == 4) tlivingexp=1990;
		if (jointcdependants == 5) tlivingexp=2250;
		if (jointcdependants > 5) tlivingexp=(((parseInt(jointcdependants,10)-5)*225)+2250);
	}
	//Establish available income.
	availincome=parseInt(tnetincjoint,10)-parseInt(tlivingexp,10);
	//Establish other expenses.
	texp=parseInt(otherloans,10)+(parseInt(creditcardlimit,10)*0.04);
	texpjoint=parseInt(texp,10);

	// Establish maximum payments.
	maxpayments=((availincome)/1)-parseInt(texpjoint);
	//if (maxpayments < 0) maxpayments=0;
	periodmaxpayments=(maxpayments*12)/repaymentfreq;
	// Set variables for calulating borrowing power. Change the qualification rate here when necessary.
	qualrate=11;
	periodrate=(qualrate/repaymentfreq)/100;
	repayments=loanterm*repaymentfreq;
	// Apply figures to ammortisation formula to work out the maximum lend amount.
	maxlend=((periodmaxpayments)*(1-Math.pow(1+parseFloat(periodrate, 10),-repayments)))/periodrate;
	//document.write (tgrossincjoint+" "+dsr+" "+texpjoint+" "+maxpayments+" "+periodmaxpayments+" "+periodrate+" "+repayments+" "+maxlend);
	if (maxlend < 0) maxlend=0;
	//test=maxlend+" "+availincome+" "+tnetincjoint+" "+tlivingexp+" "+jointadependants+" "+alivingexp;
	return parseInt(maxlend,10);
	//return test;
	
}



function bank6 (){
	//Establish total net income.
	tnetinc=calcnet(parseInt(grossincome,10)+parseInt(otherincome,10));
	tnetinc2=calcnet(parseInt(grossincome2,10)+parseInt(otherincome2,10));
	tnetincjoint=parseInt(tnetinc,10)+parseInt(tnetinc2,10);
	//Establish living expenses.
	jointadependants=adults;
	jointcdependants=dependants;
	alivingexp=0;
	if (adults == 1) {
		if (jointcdependants > 0) tlivingexp=1050+(parseInt(jointcdependants,10)*150);
		if (jointcdependants == 0) tlivingexp=1000;
	}
	if (adults == 2) {
		if (jointcdependants > 0) tlivingexp=1350+(parseInt(jointcdependants,10)*150);
		if (jointcdependants == 0) tlivingexp=1300;
	}
	//Establish available income.
	availincome=parseInt(tnetincjoint,10)-parseInt(tlivingexp,10);
	//Establish other expenses.
	texp=parseInt(otherloans,10)+(parseInt(creditcardlimit,10)*0.03);
	texpjoint=parseInt(texp,10);
	// Establish maximum payments.
	maxpayments=((availincome)/1)-parseInt(texpjoint);
	//if (maxpayments < 0) maxpayments=0;
	periodmaxpayments=(maxpayments*12)/repaymentfreq;
	// Set variables for calulating borrowing power. Change the qualification rate here when necessary.
	qualrate=8.57;
	periodrate=(qualrate/repaymentfreq)/100;
	repayments=loanterm*repaymentfreq;
	// Apply figures to ammortisation formula to work out the maximum lend amount.
	maxlend=((periodmaxpayments)*(1-Math.pow(1+parseFloat(periodrate, 10),-repayments)))/periodrate;
	//document.write (tgrossincjoint+" "+dsr+" "+texpjoint+" "+maxpayments+" "+periodmaxpayments+" "+periodrate+" "+repayments+" "+maxlend);
	if (maxlend < 0) maxlend=0;
	//test=maxlend+" "+availincome+" "+tnetincjoint+" "+tlivingexp+" "+jointadependants+" "+alivingexp;
	return parseInt(maxlend,10);
	//return test;
}


