// JavaScript Document

$(document).ready(function() {					   
	
	var path = location.href;
	var temp = path.split('/');
	var urlPath = "";
	for(i = 3; i < temp.length-2; i++) {
		urlPath = urlPath + "/" + temp[i];
	}
	
	function fnLoanType(loantype)
	{
		switch(loantype)
		{
			case '1': break; 
			case '2': $('#span-dpayment').hide();
					  $('#span-loanamount').show();
					  break;
			case '3': $('#span-dpayment').show();
					  break;
			default: break;
		}
	}
	
	function fnResult()
	{
		var termArr = [12, 18, 24, 36, 48, 60];
	  	var rateArr = [1, 1.10, 1.20, 1.30, 1.40, 1.50];
	  	var downArr = [];
		var carArr = [];
	  	var totArr = [];

	  	// compute for the results
	  	for(i=0; i<6; i++) {
			var interest = (rateArr[i] / 100) * termArr[i];
			var allowi = unformat( $('#loanamount') ) * termArr[i];
			var allowl = allowi / (1+interest); 
			downArr[i] = ( allowl / ( 1 - ( $('#dpayment').val() / 100 ) ) ) - allowl;
			carArr[i] = allowi / ( 1 + interest );
			totArr[i] = downArr[i] + carArr[i];
	 	}	
		// show the results
		for(i=0; i<termArr.length; i++) {
			$('#down'+i).text( format( Math.round( downArr[i] ) ) );
			$('#car'+i).text( format( Math.round( carArr[i] ) ) );
			$('#tot'+i).text( format( Math.round( totArr[i] ) ) );
		}
		var ratio = unformat( $('#loanamount') ) / unformat( $('#pbspcombine') );
		if(isNaN(ratio) || ratio < 0) ratio = 0;
		else ratio = ratio.toFixed(2)*100;
		
		if(ratio > 35) {
			$('#ratio').html('<span style="color:#FF0000">'+ratio+'%</span>');
			$('#ratio').blink();	
		}
		else {
			clearInterval(interval);
			$('#ratio').text(ratio+"%").css('visibility','visible');
		}
	}
	
	var path = (location.href).split('/');
	if(path[path.length-1] == "afford" || path[path.length-1] == "afford#")
	{
		fnResult();
		// monthly income
		$('#pbsalary').autoNumeric({mDec: 0});
		$('#spsalary').autoNumeric({mDec: 0});
		$('#pbbonus').autoNumeric({mDec: 0});
		$('#spbonus').autoNumeric({mDec: 0});
		$('#pbrental').autoNumeric({mDec: 0});
		$('#sprental').autoNumeric({mDec: 0});
		$('#pbcomm').autoNumeric({mDec: 0});
		$('#spcomm').autoNumeric({mDec: 0});
		$('#pbtotal').autoNumeric({mDec: 0});
		$('#sptotal').autoNumeric({mDec: 0, mNum: 15});
		$('#pbspcombine').autoNumeric({mDec: 0, mNum: 15});
		// monthly expense
		$('#rentalex').autoNumeric({mDec: 0});
		$('#houseex').autoNumeric({mDec: 0});
		$('#educex').autoNumeric({mDec: 0});
		$('#creditex').autoNumeric({mDec: 0});
		$('#otherex').autoNumeric({mDec: 0});
		$('#totalex').autoNumeric({mDec: 0, mNum: 15});
		$('#netincome').autoNumeric({mDec: 0, mNum: 15});
		// loan calculator
		$('#calcrate').autoNumeric({aSep: '', mNum: 3});
		$('#loanamount').autoNumeric();
		
		// compute the total income of the principal borrower
		var buffer = 35;
		$('input[name=pbincome]').keyup(function() {
			pbTotal = unformat($('#pbsalary')) + unformat($('#pbbonus')) + unformat($('#pbrental')) + unformat($('#pbcomm'));
			$('#pbtotal').val(format(pbTotal));
			combine = unformat($('#pbtotal')) + unformat($('#sptotal'));
			$('#pbspcombine').val(format(combine));
			net = combine - unformat($('#totalex'));
			$('#netincome').val(format(net));
			$('#loanamount').val( format( net * (buffer / 100) ) );
			fnResult();
		});
		
		// compute the total spouse income
		$('input[name=spincome]').keyup(function() {
			spTotal = unformat($('#spsalary')) + unformat($('#spbonus')) + unformat($('#sprental')) + unformat($('#spcomm'));								 
			$('#sptotal').val(format(spTotal));
			combine = unformat($('#pbtotal')) + unformat($('#sptotal'));
			$('#pbspcombine').val(format(combine));
			net = combine - unformat($('#totalex'));
			$('#netincome').val(format(net));
			$('#loanamount').val( format( net * (buffer / 100) ) );
			fnResult();
		});
		
		// compute the net total income
		$('input[name=pbexpense]').keyup(function() {
			exTotal = unformat($('#rentalex')) + unformat($('#houseex')) + unformat($('#educex')) + 
					  unformat($('#creditex')) + unformat($('#otherex'));
			$('#totalex').val(format(exTotal));
			net = unformat($('#pbspcombine')) - exTotal;
			$('#netincome').val(format(net));
			$('#loanamount').val( format( net * (buffer / 100) ) );
			fnResult();
		});
		
		// trigger button for loan type
		$('#loantype').live("click", function() { 
			fnLoanType($(this).val());
		});
		
		// downpayment trigger
		$('#dpayment').change(function() {
			fnResult();					   
		});
		
		/* 
		 * For tooltip
		 */
		// salary tooltip
		$('#tipsalary').aToolTip({  
         	tipContent: 'It is advisable to put aside enough funds for savings and a buffer as it could come handy one day for unexpected expenses such as health, dentist, vacations, birthdays, Christmas, etc..'  
   	  	});
		
		// tip for total income
		$('#tiptotal').aToolTip({  
         	tipContent: 'It is important to arrive at the amount you can afford to pay for the monthly amortization of the loan so that you will still have enough funds left for emergencies that may occur.'  
   	  	});
		
		// tip for combine total income
		$('#tipcombine').aToolTip({  
         	tipContent: 'Most banks won&#8217;t allow you to loan more than 35% of your Debt to Equity ratio.'  
   	  	});
		
		// tip for total car
		$('#total-car').aToolTip();
		
		// tip for downpayment
		$('#span-dpayment-title').aToolTip({  
         	tipContent: 'Make sure you have set aside money for the downpayment which is usually a minimum of 20%.'  
   	  	});
	}
	
});
