/**
 * 
 * 
 * Objet ForfaitFlex
 * 
 * 
 */

var PACKAGE_EXCEEDING_TRX 	   = 0.90;    //Avec Forfait - Transaction excedentaire
var TRX_COUNTER          	   = 0.90;    //Transactions comptoire*
var TRX_ELECT            	   = 0.80;    //Transactions electronique
var DEBIT_CHECK          	   = 0.90;	  //Cheques*
var INTERAC               	   = 0.80;    //Interact
var DEPOSIT_CASH          	   = 0.00215; //Dépot billet (2.15 par 1000$)
var DEPOSIT_MONEY              = 0.0215;  //Monnaie metalique (2.15 par 100$)
var DEPOSIT_CHECK              = 0.18;    //Cheques
var SBI                        = 9.95;    //SBI
var TRX_MAX_DEPOSIT_MONEY_FREE = 500;     // Montant maximum des dépots gratuit pour la monnaie

/* Not used */
//var BILL_PAYMENT_COUNTER = 2;
//var BILL_GOVERNMENT = 0.5;
//var BILL_PROVIDER_FAX = 0.5;
//var BILL_PROVIDER_EMAIL = 0.4;
//var PUBLIC_SERVICES_ANNOUNCEMENT = 1;

var ForfaitFlex = function(a,b,c,d,e,f,g,h,i,j) {
	if ( window == this || !this.init )
		return new ForfaitFlex(a,b,c,d,e,f,g,h,i,j);
	return this.init(a,b,c,d,e,f,g,h,i,j);
};
ForfaitFlex.fn = ForfaitFlex.prototype = {
	init: function(name, base, trAutomatedCredit, trDebit, trCheque, trTickets, SBI, Telnat, Card) {
		this.name = name;

		/* Spécificités du forfait */
		this.base = base;

		this.trAutomatedCredit = trAutomatedCredit;
		this.trDebit = trDebit;
		this.trCheque = trCheque;
		this.trTickets = trTickets;
		this.SBI = SBI;
		this.Telnat = Telnat;
		this.Card = Card;

		/* Valeurs utilisateur */
		this.uCDebit;
		this.uAutomatedCredit;
		this.uAutomatedDebit1;
		this.uAutomatedDebit2;
		this.uAutomatedDebit3;
		this.uCTransactions;
		this.uCCheques;
		this.uCTickets;
		this.uCCurrency;
		this.uECheques;
		this.uETickets;
		this.uECurrency;
		
		/* Excédents */
		this.exElectronicTransactions = 0;
		this.exCheques = 0;
		this.exTickets = 0;
		this.exTransactions = 0;
		this.exCurrency     = 0;
		
		this.totalWithout = 0;
		this.totalWith    = 0;

		return true;
	},
	setUserValues: function(uCDebit, uAutomatedCredit, uAutomatedDebit1, uAutomatedDebit2, uAutomatedDebit3, uCTransactions, uCCheques, uCTickets, uCCurrency, uECheques, uETickets, uECurrency, uSBI) {	
		
		this.uCDebit          = parseInt(uCDebit>0?uCDebit:0);
		this.uAutomatedCredit = parseInt(uAutomatedCredit>0?uAutomatedCredit:0);
		this.uAutomatedDebit1 = parseInt(uAutomatedDebit1>0?uAutomatedDebit1:0);
		this.uAutomatedDebit2 = parseInt(uAutomatedDebit2>0?uAutomatedDebit2:0);
		this.uAutomatedDebit3 = parseInt(uAutomatedDebit3>0?uAutomatedDebit3:0);
		this.uCTransactions   = parseInt(uCTransactions>0?uCTransactions:0);
		this.uCCheques        = parseInt(uCCheques>0?uCCheques:0);
		this.uCTickets        = parseInt(uCTickets>0?uCTickets:0);
		this.uCCurrency       = parseInt(uCCurrency>0?uCCurrency:0); 
		this.uECheques        = parseInt(uECheques>0?uECheques:0);
		this.uETickets        = parseInt(uETickets>0?uETickets:0);
		this.uECurrency       = parseInt(uECurrency>0?uECurrency:0); 
		this.uSBI             = uSBI==true?true:false; 

		this.getTotalWithout();
		this.getTotalWith();
		
		
		//alert(this.totalWithout);
		this.getSavePercentage();
		return true;
	},
	getBestForfait: function() {
		return;
	},
	
	// to complete

	getTotalWithout: function() {
		// Calcule total sans forfait
		//alert(this.uAutomatedCredit);
		//this.exCurrency = ((this.uCCurrency + this.uECurrency) <= TRX_MAX_DEPOSIT_MONEY_FREE)? 0 : ((this.uCCurrency + this.uECurrency) - TRX_MAX_DEPOSIT_MONEY_FREE) * 0.021;
		/*
		var dep            = (this.uAutomatedCredit * TRX_ELECT);					 // Dépot au Guichet Automatique
		var chequesemis    = (this.uAutomatedDebit1 * DEBIT_CHECK);					 // Cheques émis
		var monnaiedeposee = ((this.uCCurrency + this.uECurrency) * 0.021);               		 // Monaie déposée
		var interac        = (this.uAutomatedDebit2 * INTERAC);						 // Interac
		var debitelec      = (this.uAutomatedDebit3 * TRX_ELECT);					 // Débit Electronique
		var depots         = (this.uCTransactions   * TRX_COUNTER);					 // Dépot au comptoir
		var cheques        = ((this.uCCheques  + this.uECheques)  * DEPOSIT_CHECK);	 // Chèques déposés
		var billets        = ((this.uCTickets  + this.uETickets)  * DEPOSIT_CASH);	 // Argent déposé
			
		this.totalWithout = dep + chequesemis + monnaiedeposee + interac + debitelec + depots + cheques + billets;
		
		this.totalWithout = this.totalWithout<7.5?7.5:this.totalWithout;		
		//this.totalWithout = this.totalWithout<7.5?7.5:this.totalWithout;			// Le minimum des frais mensuels est 7.50$
        this.totalWithout = this.totalWithout + (this.uSBI?(SBI):0);                // Service Bancaire par Internet
		
		return this.totalWithout;
		*/
		
		
		var dep            = (this.uAutomatedCredit * TRX_ELECT);					 // Dépot au Guichet Automatique
		var chequesemis    = (this.uAutomatedDebit1 * DEBIT_CHECK);					 // Cheques émis
		var monnaiedeposee = ((this.uCCurrency + this.uECurrency) * DEPOSIT_MONEY);  // Monaie déposée
		var interac        = (this.uAutomatedDebit2 * INTERAC);						 // Interac
		var debitelec      = (this.uAutomatedDebit3 * TRX_ELECT);					 // Débit Electronique
		var depots         = (this.uCTransactions   * TRX_COUNTER);					 // Dépot au comptoir
		var cheques        = ((this.uCCheques  + this.uECheques)  * DEPOSIT_CHECK);	 // Chèques déposés
		var billets        = ((this.uCTickets  + this.uETickets)  * DEPOSIT_CASH);	 // Argent déposé
			
		this.totalWithout = dep + chequesemis + monnaiedeposee + interac + debitelec + depots + cheques + billets;
		
		this.totalWithout = this.totalWithout<7.5?7.5:this.totalWithout;
        this.totalWithout = this.totalWithout + (this.uSBI?(SBI):0);                // Service Bancaire par Internet
		
		return this.totalWithout;
	},
	getTotalWith: function() {
		
		var transactionCount = this.uAutomatedCredit + this.uAutomatedDebit1 + this.uAutomatedDebit2 + this.uAutomatedDebit3 + this.uCTransactions + this.uCDebit;
		var amounts = new Array();
		
		//TRANSACTIONS
		if(transactionCount <= this.trAutomatedCredit) {
			
			this.exElectronicTransactions = 0;
			this.exTransactions = 0;
            
		} else {
			
			//TODO: Remplacer la serie de FOR par un while qui bouclerait dans une collection de type classe par prix jusqua ce que l'excedant soit zero.
			
			var trxToEliminate = this.trAutomatedCredit;

			while(trxToEliminate > 0 && this.uCTransactions > 0) {
				this.uCTransactions--;
				trxToEliminate--;
			}
			while(trxToEliminate > 0 && this.uCDebit > 0) {
				this.uCDebit--;
				trxToEliminate--;
			}			
			while(trxToEliminate > 0 && this.uAutomatedDebit1 > 0) {
				this.uAutomatedDebit1--;
				trxToEliminate--;
			}
			while(trxToEliminate > 0 && this.uAutomatedCredit > 0) {
				this.uAutomatedCredit--;
				trxToEliminate--;
			}
			while(trxToEliminate > 0 && this.uAutomatedDebit3 > 0) {
				this.uAutomatedDebit3--;
				trxToEliminate--;
			}
			while(trxToEliminate > 0 && this.uAutomatedDebit2 > 0) {
				this.uAutomatedDebit2--;
				trxToEliminate--;
			}
            
            this.exElectronicTransactions = (this.uAutomatedCredit * PACKAGE_EXCEEDING_TRX) +
                                            (this.uAutomatedDebit3 * PACKAGE_EXCEEDING_TRX) +
                                            (this.uAutomatedDebit2 * PACKAGE_EXCEEDING_TRX);
			
			this.exTransactions = (this.uCTransactions * PACKAGE_EXCEEDING_TRX) +
			                      (this.uAutomatedDebit1 * PACKAGE_EXCEEDING_TRX);
                                  
            this.exElectronicTransactions = this.exElectronicTransactions + this.exTransactions;
		}
		
		//CHECKING

		var checkCount = (this.uCCheques + this.uECheques);
		if(checkCount <= this.trCheque) {
			this.exCheques = 0;
		} else {
			this.exCheques = (checkCount - this.trCheque) * DEPOSIT_CHECK;
		}
		
		var cashAmount = (this.uCTickets + this.uETickets); 

		//BANK NOTES
		if(cashAmount <= this.trTickets) {
			this.exTickets = 0;
		} else {
			this.exTickets = (cashAmount - this.trTickets) * DEPOSIT_CASH;
		}
		
		/* LUC */
		if((this.uCCurrency + this.uECurrency) > TRX_MAX_DEPOSIT_MONEY_FREE)
		{
			this.exCurrency = ((this.uCCurrency + this.uECurrency) - TRX_MAX_DEPOSIT_MONEY_FREE) * DEPOSIT_MONEY;
		} else {
			this.exCurrency = 0;  /* VAL(this.uCCurrency + this.uECurrency) * DEPOSIT_MONEY;   */
		}
		/* /LUC et /VAL */
        
        this.totalWith = this.base + 
                         this.exElectronicTransactions +
                         this.exCheques +
                         this.exTickets +
						 this.exCurrency;
/*
		this.totalWith = this.base + 
                         this.exElectronicTransactions +
                         this.exTransactions +
                         this.exCheques +
                         this.exTickets;
*/




// ANCIEN CODE------

		// calcule total avec forfait
		/*
		this.exElectronicTransactions = ((this.uAutomatedDebit1 + this.uAutomatedDebit2 + this.uAutomatedDebit3 - this.trDebit)>0)?(this.uAutomatedDebit1 + this.uAutomatedDebit2 + this.uAutomatedDebit3 - this.trDebit)*1.25:0;
		this.exElectronicTransactions += ((this.uAutomatedCredit - this.trAutomatedCredit)>0)?(this.uAutomatedCredit - this.trAutomatedCredit)*1.25:0;
		
		this.exCheques = (this.uCCheques+this.uECheques-this.trCheque>0)?(Math.round(this.uCCheques+this.uECheques-this.trCheque) * 0.18):0;
		this.exTickets = (this.uCTickets+this.uETickets-this.trTickets>0)?(Math.round(this.uCTickets+this.uETickets-this.trTickets) * 0.0021):0;
		this.exTransactions = this.uCTransactions * 1.25;
		this.exCurrency = (this.uCCurrency + this.uECurrency) * 0.021;
		this.exSBI = this.uSBI?(tSBI * this.SBI):0;
		
		this.totalWith = 	this.base +
							this.exElectronicTransactions + 
							this.exCheques +
							this.exTickets + 
							this.exTransactions + 
							this.exSBI +
							this.exCurrency;
							
		return this.totalWith;
		*/
	},
	getSavePercentage: function() {
		this.savePercentage = ((this.totalWithout - this.totalWith) / this.totalWithout) * 100
		//if(this.savePercentage < 0) this.savePercentage = 0;
		return this.savePercentage;
	}
}

/************************************* Objets ForfaitFlex *****************************************/
var forfaits = new Array(
					//name, base, trAutomatedCredit, trDebt, trCheque, trTickets, SBI, Telnat, Card
					new ForfaitFlex('Forfait 20',  14.99,  20, 0, 15,  1000, 0, 0, 0, 0, 0, 0, 0, 0, 0),
					new ForfaitFlex('Forfait 35',  26.99,  35, 0, 20,  3000, 0, 0, 0, 0, 0, 0, 0, 0, 0),
					new ForfaitFlex('Forfait 50',  37.99,  50, 0, 30,  5000, 0, 0, 0, 0, 0, 0, 0, 0, 0),
					new ForfaitFlex('Forfait 75',  54.99,  75, 0, 40,  8000, 0, 0, 0, 0, 0, 0, 0, 0, 0),
					new ForfaitFlex('Forfait 100', 74.99, 100, 0, 60, 15000, 0, 0, 0, 0, 0, 0, 0, 0, 0)
				);
/**************************************************************************************************/

