$(document).ready(function(){

  jQuery(".numbersonly").keypress(function(event) {
    // Backspace, tab, enter, end, home, left, right
    // We don't support the del key in Opera because del == . == 46.
    var controlKeys = [8, 9, 13, 35, 36, 37, 39];
    // IE doesn't support indexOf
    var isControlKey = controlKeys.join(",").match(new RegExp(event.which));
    // Some browsers just don't raise events for control keys. Easy.
    // e.g. Safari backspace.
    if (!event.which || // Control keys in most browsers. e.g. Firefox tab is 0
        (49 <= event.which && event.which <= 57) || (event.which == 46) || // Always 1 through 9 and .
        (48 == event.which && $(this).attr("value")) || // No 0 first digit
        isControlKey) { // Opera assigns values for control keys.
      return;
    } else {
      event.preventDefault();
    }
  });
/*
  jQuery(".limit2").mask("99");
  jQuery(".limit10").mask("9999999999");
  jQuery(".limit-rate").mask("99.99");
*/

  jQuery(".limit2").keypress(function(event) {
    if ($(this).val().length >= 2) {
      event.preventDefault();
    }
  });

  jQuery(".limit10").keypress(function(event) {
    if ($(this).val().length >= 10) {
      event.preventDefault();
    }
  });

  jQuery(".limit-rate").keypress(function(event) {
    if ($(this).val().replace(".","").length >= 2) {
      event.preventDefault();
    }
  });

  // support quick tool.
  $("#tool-getPayment").click(function() {
		var tool_id="#content-cal-g";
		var d=parseFloat($(tool_id+" .loan").val());
		var c=parseFloat($(tool_id+" .loan_rate").val());
		var e=parseFloat($(tool_id+" .loan_year").val());

		Function_Payment(tool_id, d, c, e);

    // track visitor profile.
    if (d != "" && !isNaN(d)) { jQuery.ajax({url:'/behavior/?a=profile&n=loan_amount&v='+d}); }
    if (c != "" && !isNaN(c)) { jQuery.ajax({url:'/behavior/?a=profile&n=loan_rate&v='+c}); }
    if (e != "" && !isNaN(e)) { jQuery.ajax({url:'/behavior/?a=profile&n=loan_year&v='+e}); }
	});
	$("#tool-clearPayment").click(function() {
    Function_Clear_Payment("#content-cal-g");
  });

	$("#tool-getCrediteLine").click(function() {
		var tool_id="#content-cal-g";
		var c=parseFloat($(tool_id+" .salary").val());
		var b=parseFloat($(tool_id+" .borrow_rate").val());
		var d=parseFloat($(tool_id+" .borrow_year").val());

    Function_CreditLine(tool_id, c, b, d);

    // track visitor profile.
    if (c != "" && !isNaN(c)) { jQuery.ajax({url:'/behavior/?a=profile&n=salary&v='+c}); }
    if (b != "" && !isNaN(b)) { jQuery.ajax({url:'/behavior/?a=profile&n=loan_rate&v='+b}); }
    if (d != "" && !isNaN(d)) { jQuery.ajax({url:'/behavior/?a=profile&n=loan_year&v='+d}); }
	});

  //-$("#tool-getCrediteLine").click(Function_CreditLineMulti)
	$("#tool-clearCrediteLine").click(function() {
    Function_Clear_CrediteLine("#content-cal-g");
  });
});

function formatnumber(num) {
	if (isNaN(num)) { num = 0;}
/*    var p = num.toFixed(2).split(".");

    return  p[0].split("").reverse().reduce(function(acc, num, i, orig) {
        return  num + (i && !(i % 3) ? "," : "") + acc;
    }, "") ;+ "." + p[1]; */
	num = num.toFixed(2) + '';
  x = num.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function Function_Payment(id, d, c, e) {
  if (isNaN(d))
    return false;

	if(!(d<=0||e<=0||c<=0)){
		var b = 0;
		b = Calculate_Payment(d,c,e)
	}
	$(id+" .x4").html(formatnumber(Math.round(b)));

	var a=(b*100)/40;
	$(id+" .x5").html(formatnumber(Math.round(a)));

	return false
}

function Function_Payment_a(d, c, e) {
	if(!(d<=0||e<=0||c<=0)){
		var b = 0;
		b = Calculate_Payment(d,c,e)
	}
	var a=(b*100)/40;
	return a
}

function Function_Payment_b(d, c, e) {

	if(!(d<=0||e<=0||c<=0)){
		var b = 0;
		b = Calculate_Payment(d,c,e)
	}
	var a=(b*100)/40;

	return b
}

function Function_Clear_Payment(id){
	$(id+" .loan").val("");
	$(id+" .loan_rate").val("");
	$(id+" .loan_year").val("");
	$(id+" .x4").html("0");
	$(id+" .x5").html("0");
	return false
}

function Function_Clear_Payment_TT(){
	$(".x1TT").val("");
	$(".x2TT").val("");
	$(".x3TT").val("");
	$(".x4TT").html("0");
	$(".x5TT").html("0");
	return false
}

function Function_CreditLine(id, c, b, d){
  if (isNaN(c))
    return false;

	if(c>0||d>0||b>0){
		var a=0;
		a = Calculate_Salary(c,b,d);
		$(id+" .y4").html(formatnumber(Math.round(a)))

		var b=(c*40)/100;
		$(id+" .y5").html(formatnumber(Math.round(b)))
	}
	return false
}

function Function_CreditLine_a(c, b, d){

	if(c>0||d>0||b>0){
		var a=0;
		a = Calculate_Salary(c,b,d);
	}
	return a;
}

function Function_CreditLine_b(c, b, d) {

	if(c>0||d>0||b>0){
		var a=0;
		a = Calculate_Salary(c,b,d);

		var b=(c*40)/100;

	}
	return b;
}

function Function_Clear_CrediteLine(id){
	$(id+" .salary").val("");
	$(id+" .borrow_rate").val("");
	$(id+" .borrow_year").val("");
	$(id+" .y4").html("0");
	$(id+" .y5").html("0");
	return false
}

function Function_Clear_CrediteLine_TT(){
	$(".y1TT").val("");
	$(".y2TT").val("");
	$(".y3TT").val("");
	$(".y4TT").html("0");
	$(".y5TT").html("0");
	return false
}

function Function_CreditLineMulti(){
  var c=parseFloat($("#z1").val());
  var b=parseFloat($("#z2").val());
  if(!(c<=0||b<=0)){
    var a=0;creditline5=Calculate_Salary(c,b,5);
    creditline10=Calculate_Salary(c,b,10);
    creditline15=Calculate_Salary(c,b,15);
    creditline20=Calculate_Salary(c,b,20);
    creditline25=Calculate_Salary(c,b,25);
    creditline30=Calculate_Salary(c,b,30);
    $("#z3").val((c*40)/100);
    $("#z4").val(Math.round(creditline5*100)/100);
    $("#z5").val(Math.round(creditline10*100)/100);
    $("#z6").val(Math.round(creditline15*100)/100);
    $("#z7").val(Math.round(creditline20*100)/100);
    $("#z8").val(Math.round(creditline25*100)/100);
    $("#z9").val(Math.round(creditline30*100)/100)
  }
  return false
}

function Calculate_Payment(b,a,c){
  crate=(a/12)/100;cperiod=c*12;
  var1=(b*crate);
  var2=1-(1/Math.pow((1+crate),cperiod));
  return var1/var2
}

function Calculate_Salary(b,a,c){
  crate=(a/12)/100;
  cperiod=c*12;csalary=(b*40)/100;
  var1=1/(Math.pow((1+crate),cperiod));
  var2=-csalary+(csalary*Math.pow((1+crate),cperiod));
  return(var1*var2)/crate
};












