/********************************************
 *																					*
 *		Gestion du formulaire de calcul				*
 *        des cotisations Cipav             *
 *																					*
 ********************************************/

///// VARS /////


// Regime base vars
var Smic200;
var cotisMin;
var cotisMaxT2;

var tauxCotisationT1; // Taux de cotisation sur la tranche 1
var tauxCotisationT2; // Taux de cotisation sur la tranche 2
var plafondT1;   // Plafond sur la tranche 1
var plafondT2;   // Plafond sur la tranche 2


// RB CC vars

var rbcc_forfait;
var rbcc_cotis_1;
var rbcc_cotis_2;
var rbcc_partage_1;
var rbcc_partage_2;

// Cotis RC vars
var ptRc;

var borneRC = new Array();
var cotisRC = new Array();
var cotisConjointRC = new Array();

var txReduc = new Array();
var borneReduc = new Array();

// classes invalidité décès
var classA;
var classB;
var classC;


///// FUNCS /////


// Professionel Libéral

function calcRegimeBase(revenuAm2, accessoire) {
			
			//
			// La cotisation minimale est appelée sur un revenu correspondant 
			// à 200 heures de smic (sauf si l'activité est exercée à titre 
			// accessoire)
			//
			/*
			// Pas dans le fichier Excel : on commente...
			if (revenuAm2 < Smic200 && accessoire == false) {
					revenuAm2 = Smic200;
			}
			*/		
			//
			// Le taux est de 8,6% sur la première tranche de revenus T1, comprise 
			// entre 0 et 85% du plafond de la sécurité sociale 
			//
			var montantTranche1 = (tauxCotisationT1 * Math.min(revenuAm2, plafondT1)) / 100;
			
			
			//
			// Le taux est de 1,6% sur la seconde tranche de revenus T2, comprise entre 85% 
			// du plafond de la sécurité sociale et 5 fois ce plafond
			//
			var montantTranche2;
			if (revenuAm2 > plafondT1) {
					montantTranche2 = Math.min((tauxCotisationT2 * (Math.min(revenuAm2, plafondT2) - plafondT1)) / 100, cotisMaxT2);
			} else {
					montantTranche2 = 0;
			}
			
			
			//
			// le résultat final est la somme sur les tranches 1 et 2
			//	    
			var montantCotisation = montantTranche1 + montantTranche2;


			//
			// On fait un arrondi comptable :
			// 1.4 -> 1.0
			// 1.5 -> 1.0
			// 1.6 -> 2.0			
			//var result = Math.truncate(montantCotisation);
			/*var result = Math.floor(montantCotisation);
			if (montantCotisation - result < 0.5) {
					montantCotisation = result;
			} else {
					montantCotisation = result + 1;
			}*/
			
			
			// Montant cotisation minimum pour PL à titre exclusif
			if (accessoire == false && montantCotisation < cotisMin) {
					montantCotisation = cotisMin;
			}
						
			return Math.round(montantCotisation*100) / 100;

}

function calcRetraiteComplementaire(revenuAm1, revenuAm2, classeSup, reduc, conjoint) {
			
			var _cotisRC = 0;
			var _classeRC;
			var _classeReduc;
			var _reduc = false;
			var _sup = 0;
			
			// reduction ou non
			if (reduc == true && revenuAm1 <= borneReduc[4]) {
					_reduc = true;
			}
			
			// classe sup ou non
			if (classeSup == true) {
					_sup = 1;
			}
			
			// classe de cotisation en fonction des revenus Année - 2
			if (revenuAm2 <= borneRC[1]) {
					_classeRC = 1 + _sup;
			} else if (revenuAm2 <= borneRC[2]) {
					_classeRC = 2 + _sup;
			} else if (revenuAm2 <= borneRC[3]) {
					_classeRC = 3 + _sup;
			} else if (revenuAm2 <= borneRC[4]) {
					_classeRC = 4 + _sup;
			} else if (revenuAm2 <= borneRC[5]) {
					_classeRC = 5 + _sup;
			} else if (revenuAm2 > borneRC[5]) {
					_classeRC = 6;
			}
			
			
			if (conjoint == true) {
				 _cotisRC = cotisRC[_classeRC] + cotisConjointRC[_classeRC];
			} else if (_reduc == true) {
					// réductions en fonction des revenus Année - 2
					if (revenuAm1 <= borneReduc[1]) {
							_cotisRC = cotisRC[_classeRC] - ((cotisRC[_classeRC] * txReduc[1]) / 100);
					} else if (revenuAm1 <= borneReduc[2]) {
							_cotisRC = cotisRC[_classeRC] - ((cotisRC[_classeRC] * txReduc[2]) / 100);
					} else if (revenuAm1 <= borneReduc[3]) {
							_cotisRC = cotisRC[_classeRC] - ((cotisRC[_classeRC] * txReduc[3]) / 100);
					} else if (revenuAm1 <= borneReduc[4]) {
							_cotisRC = cotisRC[_classeRC] - ((cotisRC[_classeRC] * txReduc[4]) / 100);
					} else {
							_cotisRC = cotisRC[_classeRC];
					}
			} else {
					_cotisRC = cotisRC[_classeRC];
			}
			
			return Math.round(_cotisRC*100) / 100;
}

function calcCotisInvalidite(choice) {
			switch (choice) {
					case 'A' : return classA;
					case 'B' : return classB;
					case 'C' : return classC;
			}
}


// Conjoint Collaborateur


function calcRegimeBaseCC(cotis, choice, percent, revenuAm2, accessoire) {
		
		var _return;

		switch (choice) {
				case 1 :	_return = plafondT1 * (rbcc_forfait / 100) * (tauxCotisationT1 / 100);
									break;
				
				case 2 :	_return = calcRegimeBase(revenuAm2 * percent, accessoire);
									break;
				case 3 :	_return = cotis * percent;
									break;
		}		 
		
		return Math.round(_return*100) / 100;
}

function calcRetraiteComplementaireCC(rcpl, percent) {
			return Math.max(cotisConjointRC[1], rcpl * percent);
}




///// SETTERS /////

function setCotis(cmin, cmax, smic, t1, t2, p1, p2) {
	cotisMin = Number(cmin);
	cotisMaxT2 = Number(cmax);
	Smic200 = Number(smic);
	tauxCotisationT1 = Number(t1);
	tauxCotisationT2 = Number(t2);
	plafondT1 = Number(p1);
	plafondT2 = Number(p2);
}

function setCotisRBCC(c1, c2, c3, c4, c5) {
	rbcc_forfait = Number(c1);
	rbcc_cotis_1 = Number(c2);
	rbcc_cotis_2 = Number(c3);
	rbcc_partage_1 = Number(c4);
	rbcc_partage_2 = Number(c5);
}




function setBornesRC(b1, b2, b3, b4, b5) {
	borneRC[1] = Number(b1);
	borneRC[2] = Number(b2);
	borneRC[3] = Number(b3);
	borneRC[4] = Number(b4);
	borneRC[5] = Number(b5);
}


function setCotisRC(c1, c2, c3, c4, c5, c6) {
	cotisRC[1] = Number(c1);
	cotisRC[2] = Number(c2);
	cotisRC[3] = Number(c3);
	cotisRC[4] = Number(c4);
	cotisRC[5] = Number(c5);
	cotisRC[6] = Number(c6);
}

function setCotisConjointRC(c1, c2, c3, c4, c5, c6) {
	cotisConjointRC[1] = Number(c1);
	cotisConjointRC[2] = Number(c2);
	cotisConjointRC[3] = Number(c3);
	cotisConjointRC[4] = Number(c4);
	cotisConjointRC[5] = Number(c5);
	cotisConjointRC[6] = Number(c6);
}

function setTauxReducRC(tx1, tx2, tx3, tx4, pl1, pl2, pl3, pl4) {
	txReduc[1] = Number(tx1);
	txReduc[2] = Number(tx2);
	txReduc[3] = Number(tx3);
	txReduc[4] = Number(tx4);
	borneReduc[1] = Number(pl1);
	borneReduc[2] = Number(pl2);
	borneReduc[3] = Number(pl3);
	borneReduc[4] = Number(pl4);
}

function setPtRc(pointRC) {
	ptRc = Number(pointRC);
}


function setClasses(a, b, c) {
	classA = Number(a);
	classB = Number(b);
	classC = Number(c);
}