﻿var bEvents=true;
var oLon=null;
var oRat=null;
window.onload=function(){
	bEvents=false;
	$("btnSend").onclick=btnSend_onclick;
	$("txtcPrincipal").onpropertychange=EmptyResults;
	$("txtcPayment").onpropertychange=EmptyResultsPayment;
	$("txtnTerms").onpropertychange=EmptyResults;
	$("txtpRateYear").onpropertychange=EmptyResults;

	window.document.body.onkeypress=body_keypress;

	oLon=new Loan();
	oLon.cPrincipal=100000;
	oLon.cPayment=0;
	oLon.nTerms=120;
	oLon.pRateTerm=.05/oLon.eTerm;
	oLon.Show();

	oRat=new Rating();
	oRat.cIncome=100000;
	oRat.cPaymentOther=500;
	oRat.bPrimaryResidence=true;
	oRat.Show();

	$("txtcPrincipal").focus();
	bEvents=true;
}
function body_keypress(evt){
   evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode :
        ((evt.which) ? evt.which : evt.keyCode);
    if (charCode == 13 || charCode == 3) {
		btnSend_onclick();
        return false;
    }
    return true;
}
function EmptyResults(){
	if (!bEvents) return;
	if($("txtcPrincipal").value=="") return;
	if($("txtnTerms").value=="") return;
	if($("txtpRateYear").value=="") return;
	bEvents=false;
	$("divResults").innerHTML="";
	$("txtcPayment").value="";
	bEvents=true;
}
function EmptyResultsPayment(){
	if (!bEvents) return;
	if($("txtcPayment").value=="") return;
	if($("txtnTerms").value=="") return;
	if($("txtpRateYear").value=="") return;
	bEvents=false;
	$("divResults").innerHTML="";
	$("txtcPrincipal").value="";
	bEvents=true;
}
function btnSend_onclick(){
	//send(cbf, oLon.toQry());
	var sAction="";
	oLon.Upd();
	oRat.Upd();
	if (oLon.cPayment==0) sAction="calcPayment";
	else if (oLon.cPrincipal==0) sAction="calcPrincipal";
	else if (oLon.nTerms==0) sAction="calcTerms";
	else sAction="calcPayment";
    if (pkario.inProduction()) pageTracker._trackPageview('/CALC/btnCreditRating');	
	oJd.send(cbf, "sAction="+sAction+"&"+oLon.toQry()+"&"+oRat.toQry());
}
function cbf(oResult){
	if((!oResult) || (oResult.status && oResult.status.code != "200")){
		alert("error");
	} else {
		//alert(oResult.x);
		oLon.cPrincipal=oResult.oLon.cPrincipal;
		oLon.cPayment=oResult.oLon.cPayment;
		oLon.nTerms=oResult.oLon.nTerms;
		oLon.pRateTerm=oResult.oLon.pRateTerm;
		oLon.Show();

		oRat.cPaymentMonthMax=oResult.oRat.cPaymentMonthMax;
		oRat.cPrincipalMax=oResult.oRat.cPrincipalMax;
		oRat.cPaymentGrace=oResult.oRat.cPaymentGrace;
		oRat.Show();
		if (oRat.cPaymentMonthMax>oLon.cPayment){
			$("divResults").innerHTML="Μπορείτε να πάρετε το δάνειο.";
		} else {
			$("divResults").innerHTML="ΔΕΝ μπορείτε να εξυπηρετήσετε το δάνειο.<br />"+
			"Το μέγιστο δάνειο που μπορείτε να πάρετε είναι "+oRat.cPrincipalMax.pkToString(0,"gr");
		}

	}
}
//---Rating---------------------------->
function Rating(){
	this.cIncome=0;
	this.cPaymentOther=0;
	this.bPrimaryResidence=false;
	this.eGraceType="ΧΩΡ";
	this.nGraceTerms=0;

	this.cPaymentMonthMax=0;
	this.cPrincipalMax=0;
	this.cPaymentGrace=0;
}
Rating.prototype.toQry=function(){
	var sQry="cIncome=" + this.cIncome.toString()+
		"&cPaymentOther="+ this.cPaymentOther.toString()+
		"&bPrimaryResidence="+ (this.bPrimaryResidence?"T":"F")+
		"&eGraceType="+ this.eGraceType+
		"&nGraceTerms="+ this.nGraceTerms.toString();
	return sQry;
}
Rating.prototype.Show=function(){
	bEvents=false;
	$("txtcIncome").value=(this.cIncome>0?this.cIncome.pkToString(0,"gr"):"");
	$("txtcPaymentOther").value=(this.cPaymentOther>0?this.cPaymentOther.pkToString(0,"gr"):"");
	$("chkbPrimaryResidence").checked=this.bPrimaryResidence;
	$("cboeGraceType").value=this.eGraceType;
	$("cbonGraceTerms").value=this.nGraceTerms.toString();

	$("txtcPaymentMonthMax").value=(this.cPaymentMonthMax>0?this.cPaymentMonthMax.pkToString(0,"gr"):"");
	$("txtcPrincipalMax").value=(this.cPrincipalMax>0?this.cPrincipalMax.pkToString(0,"gr"):"");
	$("txtcPaymentGrace").value=(this.cPaymentGrace>0?this.cPaymentGrace.pkToString(0,"gr"):"");
	bEvents=true;
}
Rating.prototype.Upd=function(){
	this.cIncome=Number.pkParse($("txtcIncome").value,"gr");
	this.cPaymentOther=Number.pkParse($("txtcPaymentOther").value,"gr");
	this.bPrimaryResidence=$("chkbPrimaryResidence").checked;
	this.cPaymentMonthMax=Number.pkParse($("txtcPaymentMonthMax").value,"gr");
	this.eGraceType=$("cboeGraceType").value;
	this.nGraceTerms=Number.pkParse($("cbonGraceTerms").value,"gr");

	if (isNaN(this.cIncome)) this.cIncome=0;
	if (isNaN(this.cPaymentOther)) this.cPaymentOther=0;
	if (isNaN(this.cPaymentMonthMax)) this.cPaymentMonthMax=0;
	if (isNaN(this.nGraceTerms)) this.nGraceTerms=0;
}
var enTerm={
		none:0,
		Month:12,
		Trimester:4,
		Semester:2,
		Year:1
	};
//---Loan------------------------------>
function Loan(){
	this.cPrincipal=0;
	this.cPayment=0;
	this.nTerms=0;
	this.pRateTerm=0;
	this.eTerm=enTerm.Month;
}
Loan.prototype.toQry=function(){
	var sQry="cPrincipal=" + this.cPrincipal.toString()+
		"&cPayment="+ this.cPayment.toString()+
		"&nTerms="+ this.nTerms.toString()+
		"&pRateTerm="+ this.pRateTerm.toString()+
		"&eTerm="+this.eTerm.toString();
	return sQry;
}
Loan.prototype.Show=function(){
	bEvents=false;
	$("txtcPrincipal").value=(this.cPrincipal>0?this.cPrincipal.pkToString(0,"gr"):"");
	$("txtcPayment").value=(this.cPayment>0?this.cPayment.pkToString(0,"gr"):"");
	$("txtnTerms").value=(this.nTerms>0?(this.nTerms/12).pkToString(0,"gr"):"");
	$("txtpRateYear").value=(this.pRateTerm>0?(this.pRateTerm*100*this.eTerm).pkToString(2,"gr"):"");
	bEvents=true;
}
Loan.prototype.Upd=function(){
	this.cPrincipal=Number.pkParse($("txtcPrincipal").value,"gr");
	this.pRateTerm=Number.pkParse($("txtpRateYear").value,"gr");
	this.pRateTerm /= (100*this.eTerm);
	this.nTerms=Number.pkParse($("txtnTerms").value,"gr")*12;
	this.cPayment=Number.pkParse($("txtcPayment").value,"gr");

	if (isNaN(this.cPrincipal)) this.cPrincipal=0;
	if (isNaN(this.pRateTerm)) this.pRateTerm=0;
	if (isNaN(this.nTerms)) this.nTerms=0;
	if (isNaN(this.cPayment)) this.cPayment=0;
}
//---Loan------------------------------<
var oJd=new pkario.jd();
if (document.domain=="localhost") {
    oJd.base="http://localhost:50941/LoansServer/jd.aspx";
} else if (document.domain=="test") {
    oJd.base="http://test/insServer/LoansServer/jd.aspx";
} else {
    //oJd.base="http://212.70.216.52/insMoney/LoansServer/jd.aspx"; //aegaleo
    oJd.base="http://79.129.44.153/insMoney/LoansServer/jd.aspx";  //ins
    //oJd.base="http://83.235.21.39/insMoney/LoansServer/jd.aspx";  //ins
}

//------------------------------------------------------------
