Calc_Class = function() {this.init();}


Calc_Class.prototype.init = function(){
	this.events();
}

Calc_Class.prototype.getNumber = function(currency) {
	return Number(currency.replace(/[^0-9\.]+/g,""));
}

Calc_Class.prototype.events = function() {
	
	
	$("input.currency").blur(function(){
		$.validity.start();
		$(this).match("number");
			
		var result = $.validity.end();	
		if(result.valid){
	    	$(this).format({format:"$#,###.00", locale:"us"});
		}
	});
	
	$("input.currency").focus(function(){
    	if(!$(this).val()){
    		return;
    	}
    	var currency = $(this).val();
    	var number = Number(currency.replace(/[^0-9\.]+/g,""));
    		
		$(this).val(number);    		
	});
	
	
	
	$("input.percent").blur(function(){
		$.validity.start();
    	$(this).match("number")
    	
		var result = $.validity.end();	
		
		if(result.valid){
			var value = $(this).val()/100;
			$(this).val(value);
    		$(this).format({format:"#.0#%", locale:"us"});
		}
	});
	
	$("input.percent").focus(function(){
		if(!$(this).val()){
    		return;
    	}
    	var currency = $(this).val();
    	var number = Number(currency.replace(/[^0-9\.]+/g,""));
    		
		$(this).val(number);    		
	});	

	

	
	// 
	//
	//
	this.loanPayment = $('.calculate-loan-payment');
	this.howMuchLoan = $('.how-much-loan');

	 this.loanPayment.bind('click',$.bindContext(this.changeCalc, this));
	 this.howMuchLoan.bind('click',$.bindContext(this.changeCalc, this));


	// calculate-loan-payment
	// Loan Evt

	this.loanAmount = $('.loan-amount', this.loanPayment);
	this.interestRate = $('.interest-rate', this.loanPayment);
	this.loanTerm = $('.loan-term', this.loanPayment); //in years

	this.loanAmount.bind('keyup',$.bindContext(this.monthlyPaymentCalc, this));
	this.interestRate.bind('keyup',$.bindContext(this.monthlyPaymentCalc, this));
	this.loanTerm.bind('keyup',$.bindContext(this.monthlyPaymentCalc, this));

	//Income evt
	this.annualIncome = $('.annual-income',this.loanPayment);
	this.otherAnnualIncome = $('.other-annual-income',this.loanPayment);

	this.annualIncome.bind('keyup',$.bindContext(this.incomeCalc, this));
	this.otherAnnualIncome.bind('keyup',$.bindContext(this.incomeCalc, this));
	//debt evt

	this.property = $('.property', this.loanPayment);

	this.creditCard = $('.credit-card', this.loanPayment);
	this.otherPayments = $('.other-payments', this.loanPayment);

	this.property.bind('keyup',$.bindContext(this.debtCalc, this));
	this.creditCard.bind('keyup',$.bindContext(this.debtCalc, this));
	this.otherPayments.bind('keyup',$.bindContext(this.debtCalc, this));

	//calculate evt
	this.initCalc = $('input.do-calculation', this.loanPayment);
		$('.calculate-loan-payment form').bind('submit',function(event){
			event.preventDefault();
	});

	this.initCalc.bind('click', $.bindContext(this.doCalculation, this));
//
// -------------------------------------------------------
//
	//how-much-loan
	
	this.hmlAnnualIncome = $('.annual-income',this.howMuchLoan);
	this.hmlAnnualDepreciation = $('.annual-depreciation',this.howMuchLoan);
	
			this.hmlAnnualIncome.bind('keyup', $.bindContext(this.hmlIncomeCalc,this));
			this.hmlAnnualDepreciation.bind('keyup', $.bindContext(this.hmlIncomeCalc,this));


	this.hmlProperty = $('.property',this.howMuchLoan);
	this.hmlCreditLine = $('.credit-line',this.howMuchLoan);
	this.hmlEquipmentLoan = $('.equipment-loan',this.howMuchLoan);
	this.hmlOtherLoan = $('.other-loan',this.howMuchLoan);


			this.hmlProperty.bind('keyup', $.bindContext(this.hmlDebtCalc,this));
			this.hmlCreditLine.bind('keyup', $.bindContext(this.hmlDebtCalc,this));
			this.hmlEquipmentLoan.bind('keyup', $.bindContext(this.hmlDebtCalc,this));
			this.hmlOtherLoan.bind('keyup', $.bindContext(this.hmlDebtCalc,this));



	this.hmlInitCalc = $('input.do-calculation', this.howMuchLoan);
		$('.how-much-loan form').bind('submit',function(event){
			event.preventDefault();
	});

		this.hmlInitCalc.bind('click', $.bindContext(this.hmlDoCalculation, this));


}

Calc_Class.prototype.changeCalc = function(e,el) {
	if($(el).hasClass('calculate-loan-payment')){
		$(el).closest('div.unit').removeClass('inactive');
		$('a.how-much-loan').closest('div.unit').addClass('inactive');
		
		$('div.how-much-loan').hide();
		$('div.calculate-loan-payment').show();
	}else{
		$(el).closest('div.unit').removeClass('inactive');
		$('a.calculate-loan-payment').closest('div.unit').addClass('inactive');
		
		$('div.calculate-loan-payment').hide();
		$('div.how-much-loan').show();
	}
}

Calc_Class.prototype.monthlyPaymentCalc = function(e,el) {
	var monthlyPaymentEl = $('.monthly-payment input');

	var loanAmount = this.getNumber(this.loanAmount.val());
	var interestRate = (this.interestRate.val());
	var loanTerm = this.loanTerm.val();
	var loanTermMonths,monthlyInterest,monthlyPaymentValue;

if(loanAmount  && interestRate &&  loanTerm) {
		// calc payment
		loanTermMonths = loanTerm *12;
		monthlyInterest  =  this.getNumber(interestRate) / (12 *  100);

		monthlyPaymentValue = loanAmount * (monthlyInterest  /  (1 - Math.pow( (1 +monthlyInterest ),  -loanTermMonths )));

		monthlyPaymentValue = Number(monthlyPaymentValue);

		if(!isNaN(monthlyPaymentValue)){
			monthlyPaymentEl.val(monthlyPaymentValue);
			monthlyPaymentEl.format({format:"$#,###.00", locale:"us"});
		}
		
	}else{
		monthlyPaymentEl.val('');
	}

	
}

Calc_Class.prototype.incomeCalc = function(e,el) {
	var otherIncome = 	this.otherAnnualIncome.val();
	var income = 	this.annualIncome.val();
	var totalIncomeEl = $('.total-income input', this.loanPayment);

	var totalIncomeValue = this.getNumber(income)+this.getNumber(otherIncome);
	
	if(!isNaN(totalIncomeValue)){
		totalIncomeEl.val(totalIncomeValue);
		totalIncomeEl.format({format:"$#,###.00", locale:"us"});

	}
}

Calc_Class.prototype.debtCalc = function(e,el) {

	var property = 	this.property.val();
	var creditCard = 	this.creditCard.val();
	var otherPayments = this.otherPayments.val();
	var totalDebtEl = $('.total-debt input', this.loanPayment);
	var totalLoanDebtEL  = $('.total-loan-debt input', this.loanPayment);
	var totalLoanDebtValue;

	var totalDebtValueYr = (this.getNumber(property)+this.getNumber(creditCard)+this.getNumber(otherPayments)) *12;
			if(!isNaN(totalDebtValueYr)){
				totalDebtEl.val(totalDebtValueYr);
				totalDebtEl.format({format:"$#,###.00", locale:"us"});

			}

	var monthlyPaymentEl = $('.monthly-payment input', this.loanPayment);


	if(monthlyPaymentEl.val() && totalDebtEl.val()) {
		totalLoanDebtValue  = totalDebtValueYr + (this.getNumber(monthlyPaymentEl.val()) * 12);
		
		if(!isNaN(totalLoanDebtValue)){
			totalLoanDebtEL.val(totalLoanDebtValue);
			totalLoanDebtEL.format({format:"$#,###.00", locale:"us"});

		}
	}else{
		totalLoanDebtEL.val('');
	}

}

Calc_Class.prototype.doCalculation = function(e,el) {

	var debtCoverageRatio = 1.3;
	var income = $('.total-income input').val();
	var debt  = $('.total-loan-debt input').val();
	var calcDebtCoverage;

	if(income && debt){
		calcDebtCoverage =  this.getNumber(income) / this.getNumber(debt);

		this.debtCoverageMessage(debtCoverageRatio,calcDebtCoverage );
	}

}

Calc_Class.prototype.debtCoverageMessage = function(debtCoverageRatio,calcDebtCoverage){
	var debtcoverageTitle = $('.message-title', this.loanPayment);
	var debtcoverageMessage =$ ('.message-body', this.loanPayment);
	var titeText, bodyText;
	if(calcDebtCoverage > debtCoverageRatio){
		titeText = "Congratulations, There is a good chance you qualify for a loan!"
		bodyText = "let\'s get your loan process started.  <a href=\"/get-a-loan\">Click Here</a> to proceede"
		debtcoverageTitle.html(titeText);
		debtcoverageMessage.html(bodyText);

	}else{
		titeText = "You may not qualify for this loan. <a href=\"/get-a-loan\">Click Here</a> and our small business loan specialist can help you find a loan that will work for you."
		debtcoverageTitle.html(titeText);
		debtcoverageMessage.html(bodyText);
	}
}


// how much loan

Calc_Class.prototype.loanPurpose = function() {

	var oPurpose = {
		'landBuilding' : {'interest' : 7.1, 'term' : 25},
		'operatingCapital' : {'interest' : 7.75, 'term' : 7},
		'inventory' : {'interest' : 8, 'term' : 5},
		'equiptRepair' : {'interest' : 7.25, 'term' : 10},
		'construction' : {'interest' : 7.75, 'term' : 5}
	}

	var loanPurpose = $('.primary-loan-purpose :checked');
	var selectedPurposeValue = loanPurpose.val();

	var oSelectedPurpose = oPurpose[selectedPurposeValue];
	return oSelectedPurpose;
}


Calc_Class.prototype.hmlIncomeCalc = function(e,el) {
	var otherIncome = 	this.hmlAnnualIncome.val();
	var income = 	this.hmlAnnualDepreciation.val();
	var totalIncomeEl = $('.total-income input', this.howMuchLoan);

	var totalIncomeValue = this.getNumber(income)+this.getNumber(otherIncome);
	if(!isNaN(totalIncomeValue)){
		totalIncomeEl.val(totalIncomeValue);
		totalIncomeEl.format({format:"$#,###.00", locale:"us"})
		
	}
}

Calc_Class.prototype.hmlDebtCalc = function(e,el) {

	var property = 	this.hmlProperty.val();
	var creditLine = 	this.hmlCreditLine.val();
	var equipmentLoan = this.hmlEquipmentLoan.val();
	var otherPayments = this.hmlOtherLoan.val();
	var totalDebtEl = $('.total-debt input', this.howMuchLoan);

	var totalDebtValue = this.getNumber(property) +this.getNumber(creditLine)+this.getNumber(equipmentLoan)+this.getNumber(otherPayments);
	var yearlyDebt = totalDebtValue * 12;

	if(!isNaN(yearlyDebt)){
		totalDebtEl.val(yearlyDebt);
		totalDebtEl.format({format:"$#,###.00", locale:"us"})
	}
}

Calc_Class.prototype.hmlDoCalculation = function(e,el) {
	var selectedPurpose = this.loanPurpose();

	var loanTermYear  = selectedPurpose.term;
	var debtCoverageRatio = 1.3;
	var interestRate = selectedPurpose.interest;
	var monthlyInterest  =  interestRate / (12 *  100);
	var loanTermMonths = loanTermYear * 12;
	var income = this.getNumber($('.total-income input', this.howMuchLoan).val());
	var debt  =this.getNumber($('.total-debt input', this.howMuchLoan).val());
	var loanAmount, maxLoanDebt, monthlyMaxLoanDebt;

	if(income && debt){
		maxLoanDebt = (income / debtCoverageRatio )  - debt;
		monthlyMaxLoanDebt= maxLoanDebt / 12;

		loanAmount  = monthlyMaxLoanDebt  /  (monthlyInterest  /  (1 - Math.pow( (1 +monthlyInterest ),  -loanTermMonths )));

		this.hmlLoanAmountMessage(loanAmount);
	}

}

Calc_Class.prototype.hmlLoanAmountMessage = function(loanAmount){
	var loanAmountTitle = $('.message-title', this.howMuchLoan);
	var LoanAmountBody = $('.message-body', this.howMuchLoan);
	var loanAmountEl = $('.qualified-loan-amount input', this.howMuchLoan);
	var titeText, bodyText;
		if(loanAmount < 0)
		{
			loanAmount = 0;
		}
		loanAmountEl.val(loanAmount);
		loanAmountEl.format({format:"$#,###.00", locale:"us"})

	
	if(loanAmount > 5000){
		
		titeText = "Congratulations, There is a good chance this loan will work for you!";
		bodyText = "let\'s get your loan process started.  <a href=\"/get-a-loan\">Click Here</a> to proceede";
	} else {
		titeText = "please Contact us to discuss your loan";
		bodyText = "let\'s get your loan process started.  <a href=\"/get-a-loan\">Click Here</a> to proceede";
	};
	
	loanAmountTitle.html(titeText);
	LoanAmountBody.html(bodyText);
	


}

$(document).ready(function() {
	var calc = new Calc_Class();
 });