﻿var bEvents=true;
var oSub=null;
window.onload=function(){
	bEvents=false;
	$("btnSend").onclick=btnSend_onclick;

	//$("cboeMaritalStatus").onpropertychange=EmptyResults;
	//$("cbonProtectedMembers").onpropertychange=EmptyResults;
	$("txtcIncome").onpropertychange=EmptyResults;
	$("txtcBldValObjective").onpropertychange=EmptyResults;
	$("txtcBldValCommercial").onpropertychange=EmptyResults;

	//$("cboeMaritalStatus").onpropertychange=EmptyDays;
	//$("cbonProtectedMembers").onpropertychange=EmptyDays;
	$("chkbNewlyWed").onpropertychange=EmptyDays;
	$("chkbSeasonalWorker").onpropertychange=EmptyDays;
	$("chkbFrontier").onpropertychange=EmptyDays;
	$("chkbDisabled67").onpropertychange=EmptyDays;
	$("chkbParaplegic").onpropertychange=EmptyDays;
	$("chkbBlind").onpropertychange=EmptyDays;

	$("cboeMaritalStatus").onpropertychange=EmptyUpdate;
	$("cbonProtectedMembers").onpropertychange=EmptyUpdate;


	window.document.body.onkeypress=body_keypress;
	
	var cbo=$("cboeMaritalStatus");
	AddOpt(cbo,"Αγαμος", "0").selected=true; //values are used in server
	AddOpt(cbo,"Εγγαμος", "1");
	AddOpt(cbo,"Χήρος", "2");
	AddOpt(cbo,"Διαζευγμένος", "3");

	var cbo=$("cbonProtectedMembers");
	AddOpt(cbo,"0", "0").selected=true;
	AddOpt(cbo,"1", "1");
	AddOpt(cbo,"2", "2");
	AddOpt(cbo,"3", "3");
	AddOpt(cbo,"4", "4");
	AddOpt(cbo,"5", "5");
	AddOpt(cbo,"6", "6");
	AddOpt(cbo,"7", "7");
	AddOpt(cbo,"8", "8");
	AddOpt(cbo,"9", "9");
	AddOpt(cbo,"10", "10");
	AddOpt(cbo,"11", "11");
	AddOpt(cbo,"12", "12");
	AddOpt(cbo,"13", "13");
	AddOpt(cbo,"14", "14");
	AddOpt(cbo,"15", "15");
	
	oSub = new Subsidy();
	//$("txteMaritalStatus").value=$("cboeMaritalStatus").childNodes[$("cboeMaritalStatus").selectedIndex].innerHTML;
	$("txteMaritalStatus").value=$("cboeMaritalStatus").childNodes[$("cboeMaritalStatus").selectedIndex].innerHTML;
	$("txtnProtectedMembers").value=$("cbonProtectedMembers").value;

	oSub.cIncome=25000;
	oSub.cBldValObjective=80000;
	oSub.cBldValCommercial=110000;
	
	oSub.Show();
	$("txtcIncome").focus();
	bEvents=true;
}
function AddOpt(oCbo, sTxt, sVal){
	opt=c$("OPTION");
	opt.innerHTML=sTxt;
	opt.value=sVal;
	oCbo.appendChild(opt);
	return opt;
}
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 EmptyDays(){
	if (!bEvents) return;
	bEvents=false;
	$("txtnDays").value="";
	bEvents=true;
}
function EmptyUpdate(){
	if (!bEvents) return;
	bEvents=false;
	$("txteMaritalStatus").value=$("cboeMaritalStatus").childNodes[$("cboeMaritalStatus").selectedIndex].innerHTML;
	$("txtnProtectedMembers").value=$("cbonProtectedMembers").value;


	$("txtnDays").value="";
	$("txteSubCategory").value="";
	$("txtpSubOEK").value="";
	$("txtpSubGov").value="";
	$("txtcLoanPrincipalMax").value="";
	bEvents=true;
}
function EmptyResults(){
	if (!bEvents) return;
	bEvents=false;
	$("txteSubCategory").value="";
	$("txtpSubOEK").value="";
	$("txtpSubGov").value="";
	$("txtcLoanPrincipalMax").value="";
	bEvents=true;
}
function EmptyResultsPayment(){
	if (!bEvents) return;
	if($("txtcPayment").value=="") return;
	if($("txtnTerms").value=="") return;
	if($("txtpRate").value=="") return;
	bEvents=false;
	$("txtcPrincipal").value="";
	bEvents=true;
}
function btnSend_onclick(){
	//send(cbf, oLon.toQry());
	oSub.Upd();
	var sAction="";
	sAction = "calcSub1";
	//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/btnOEK');	
	oJd.send(cbf, "sAction="+sAction+"&"+oSub.toQry());
}
function cbf(oResult){
	if((!oResult) || (oResult.status && oResult.status.code != "200")){
		alert("error");
	} else {
		oSub.nDays=oResult.oSub.nDays;
		oSub.pSubOEK=oResult.oSub.pSubOEK;
		oSub.pSubGov=oResult.oSub.pSubGov;
		oSub.cLoanPrincipalMax=oResult.oSub.cLoanPrincipalMax;
		oSub.eSubCategory=oResult.oSub.eSubCategory;
		oSub.bSubCatD=oResult.oSub.bSubCatD;
		oSub.Show();
		//alert(oResult.x);
/*
		oLon.cPrincipal=oResult.oLon.cPrincipal;
		oLon.cPayment=oResult.oLon.cPayment;
		oLon.nTerms=oResult.oLon.nTerms;
		oLon.pRate=oResult.oLon.pRate;
		oLon.Show();

		oRat.cPaymentMonthMax=oResult.oRat.cPaymentMonthMax;
		oRat.cPrincipalMax=oResult.oRat.cPrincipalMax;
		oRat.cPaymentGrace=oResult.oRat.cPaymentGrace;
		oRat.Show();
*/	}
}
var enBeneficiaryAtr={
		none:0,
		bNewlyWed:1,
		bSeasonalWorker:2,
		bFrontier:4,
		bDisabled67:8,
		bBlind:16,
		bParaplegic:32
	};
var enMaritalStatus={
		Single:0, //used in array addressing, and in js client
		Married:1, //used in array addressing
		Widow:2,
		Divorced:3,
		Unknown:4
	};
var enSubCategory={
	A:0,
	B:1,
	C:2,
	Fail:3,
	Unknown:4
};
var enSubCategoryDsc=["Α","Β","Γ","",""];
//---Subsidy---------------------------->
function Subsidy(){
	this.cIncome = 0;
	this.cBldValObjective = 0;
	this.cBldValCommercial=0;
	this.eSubCategory =enSubCategory.Unknown;
	this.bSubCatD=false;


	this.eBeneficiaryAtr=enBeneficiaryAtr.none;
	this.eMaritalStatus = enMaritalStatus.Single;
	this.nProtectedMembers = 0;

	this.pSubOEK = 0;
	this.pSubGov = 0;
	this.bSubCat_D= false;
	this.pSubOEK_D = 0;
	this.cLoanPrincipalMax=0;
	//this.cLoanPrincipalMaxIncome=0;
	//this.cLoanPrincipalMaxObjVal=0;
	//this.cLoanPrincipalMaxComVal=0;

	this.nDays=0;
}
Subsidy.prototype.toQry=function(){
	var sQry="eMaritalStatus=" + this.eMaritalStatus+
		"&nProtectedMembers="+this.nProtectedMembers.toString()+
		"&eBeneficiaryAtr="+this.eBeneficiaryAtr.toString()+
		"&cIncome="+this.cIncome.toString()+
		"&cBldValObjective="+this.cBldValObjective.toString()+
		"&cBldValCommercial="+this.cBldValCommercial.toString();
	return sQry;
}
Subsidy.prototype.Show=function(){
	bEvents=false;
	$("txtcIncome").value=(this.cIncome>0?this.cIncome.pkToString(0,"gr"):"");
	$("txtcBldValObjective").value=(this.cBldValObjective>0?this.cBldValObjective.pkToString(0,"gr"):"");
	$("txtcBldValCommercial").value=(this.cBldValCommercial>0?this.cBldValCommercial.pkToString(0,"gr"):"");


	$("cboeMaritalStatus").value=this.eMaritalStatus;
	//$("txteSubCategory").value=(this.eSubCategory=="C"?"Γ":this.eSubCategory)+ (this.bSubCatD?" ή Δ":"");
	if (isNaN(this.eSubCategory)) {
	    $("txteSubCategory").value=enSubCategoryDsc[enSubCategory[this.eSubCategory]] + (this.bSubCatD?" ή Δ":"");
	} else {
    	$("txteSubCategory").value=enSubCategoryDsc[this.eSubCategory] + (this.bSubCatD?" ή Δ":"");
	}
	$("txtpSubOEK").value=(this.pSubOEK>0?(this.pSubOEK*100).pkToString(2,"gr"):"");
	$("txtpSubGov").value=(this.pSubGov>0?(this.pSubGov*100).pkToString(2,"gr"):"");
	$("txtcLoanPrincipalMax").value=(this.cLoanPrincipalMax>0?this.cLoanPrincipalMax.pkToString(0,"gr"):"");
	$("txtnDays").value=(this.nDays>0?this.nDays.pkToString(0,"gr"):"");
	bEvents=true;
}
Subsidy.prototype.Upd=function(){
	this.cIncome=Number.pkParse($("txtcIncome").value,"gr");
	this.cBldValObjective=Number.pkParse($("txtcBldValObjective").value,"gr");
	this.cBldValCommercial=Number.pkParse($("txtcBldValCommercial").value,"gr");
	this.nProtectedMembers=Number.pkParse($("cbonProtectedMembers").value,"gr");

	this.eBeneficiaryAtr=enBeneficiaryAtr.none;
	if ($("chkbNewlyWed").checked) this.eBeneficiaryAtr |= enBeneficiaryAtr.bNewlyWed;
	if ($("chkbSeasonalWorker").checked) this.eBeneficiaryAtr |= enBeneficiaryAtr.bSeasonalWorker;
	if ($("chkbFrontier").checked) this.eBeneficiaryAtr |= enBeneficiaryAtr.bFrontier;
	if ($("chkbDisabled67").checked) this.eBeneficiaryAtr |= enBeneficiaryAtr.bDisabled67;
	if ($("chkbBlind").checked) this.eBeneficiaryAtr |= enBeneficiaryAtr.bBlind;
	if ($("chkbParaplegic").checked) this.eBeneficiaryAtr |= enBeneficiaryAtr.bParaplegic;

	this.eMaritalStatus=$("cboeMaritalStatus").value;

	if (isNaN(this.cIncome)) this.cIncome=0;
	if (isNaN(this.cBldValObjective)) this.cBldValObjective=0;
	if (isNaN(this.cBldValCommercial)) this.cBldValCommercial=0;
	if (isNaN(this.nProtectedMembers)) this.nProtectedMembers=0;
	if (isNaN(this.cPaymentOther)) this.cPaymentOther=0;
	if (isNaN(this.cPaymentMonthMax)) this.cPaymentMonthMax=0;
	if (isNaN(this.nGraceTerms)) this.nGraceTerms=0;
}
//------------------------------------------------------------
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
}

