	var tbl_codes = new Array();
	// code var min
	// code var max
	// famille
	//pourcentage (calculé en 2009...)
	tbl_codes[0] = [0,59,0];
	tbl_codes[1] = [60,119,2];
	tbl_codes[2] = [120,239,4];
	tbl_codes[3] = [240,399,6];
	tbl_codes[4] = [400,599,8];
	tbl_codes[5] = [600,799,10];
	tbl_codes[6] = [800,999,12];
	tbl_codes[7] = [1000,1399,14];
	tbl_codes[8] = [1400,1799,16];
	tbl_codes[9] = [1800,2399,17];
	tbl_codes[10] = [2400,2999,18];
	tbl_codes[11] = [3000,3999,19];
	tbl_codes[12] = [4000,5999,20];
	tbl_codes[13] = [6000,10000000000000,21];
	

	
window.onload = function() {
	ajoutOnChange();
	
}

var identifiants = new Array (
'champ_0',
'champ_1',
'champ_2',
'champ_3',
'champ_4',
'champ_5',
'champ_6',
'champ_7',
'champ_8',
'champ_9',
'champ_10',
'champ_11',
'champ_remise_volume',
'champ_remise_saisonnalite',
'champ_remise_total_plafonnees',
'champ_remise_happy_day',
'champ_remise_total_happy_day',
'champ_remise_referencielle',
'champ_remise_mandataire',
'champ_taux_financier',
'champ_net_facture',
'champ_remise_developpement_annuel'
);	

	var erreur = new Array();
	erreur[0]="Attention, vous devez saisir des donnees numeriques.";
	erreur[1]="Champ obligatoire, ne pouvant être egal a 0.";
	erreur[2]="Ne peut recevoir que la valeur 1 ou 0.";
	erreur[3]="Code variete invalide.";
	erreur[4]="Montant BAB 2009 invalide.";
	erreur[5]="Remise 'Happy day' Ne peut recevoir q'un nombre inférieur ou égal à 100.";
	
function ajoutOnChange(){
	
	for(var j=0;j<identifiants.length;j++){
		var obj = document.getElementById(identifiants[j]);
		obj.onkeyup = calcul;
	}
}





function calcul(){

	

		Epr = /^[0-9]+[0-9]*$/
		if(this.id == identifiants[11]){
			Epr = /^[0-1]{1}$/
		}
		if(this.id == "champ_part_marche_pub_2007" || this.id == "champ_part_marche_pub_2008"){
			Epr = /^[0-9.]{1,5}$/
		}
		if(this.id == "champ_brut_facture_annuel_2008"){
			Epr = /^[1-9]+[0-9]*$/
		}
		if(Epr.test(this.value) == false){
			if(typeof this.value != "number"){
				//alert(erreur[0]);
			}
			if(this.id == identifiants[11]){
				if(this.value) alert(erreur[2]);
			}

			this.value = "";
			for(var j=0;j<12;j++){
				document.getElementById(identifiants[j]).disabled = true;
				document.getElementById(identifiants[j]).style.background = "#ccc";
			}
			this.disabled = false;
			this.style.background = "#fff";
			//this.focus();
		}
		else if(this.id == identifiants[10]){
			if(this.value > 100){
				alert(erreur[5]);	
				this.value = "";
				for(var j=0;j<12;j++){
					document.getElementById(identifiants[j]).disabled = true;
					document.getElementById(identifiants[j]).style.background = "#ccc";
				}
				this.disabled = false;
				this.style.background = "#fff";
			}
			else{
				continuer();
			}
		}
		else{
			continuer();
		}
	
		function continuer(){
				for(var j=0;j<12;j++){
				// si c'est ok on active le suivant
				if(identifiants[j]==this.id){
					
					// on verifie qu'il ya bien un suivant
					if(identifiants.length>(j+1)){
						document.getElementById(identifiants[j+1]).disabled = false;
						document.getElementById(identifiants[j+1]).style.background = "#fff";
						//document.getElementById(identifiants[j+1]).focus();
					}
				}
				// si c'est ok on reactive les autres champs
				if(document.getElementById(identifiants[j]).value != ""){
					document.getElementById(identifiants[j]).disabled = false;
					document.getElementById(identifiants[j]).style.background = "#fff";
					//document.getElementById(identifiants[j]).focus();
					var dernierActif = j;
				}
				// on reactive le dernier si vide
				if(identifiants.length>(j+1)){
					document.getElementById(identifiants[dernierActif+1]).disabled = false;
					document.getElementById(identifiants[dernierActif+1]).style.background = "#fff";
					//document.getElementById(identifiants[dernierActif+1]).focus();
				}
			}
		}
		
		
		function remiseAzero(){
			for(var j=0;j<identifiants.length;j++){
				document.getElementById(identifiants[j]).innerHTML = "0";
			}			
		}
		
		function verifActivation(){
			for(var j=0;j<identifiants.length;j++){
				if(document.getElementById(identifiants[j]).disabled == true){
					return false; break;
				}
				else{
					return true; continue;
				}
			}
		}
		
		
			// si tous le dernier champ est ok alors on effectue la simulation	
			if(document.getElementById(identifiants[11]).value != "" && verifActivation()){
			//Si tout est activé alors on effectue et on affiche les valeurs
			// 5 calculs
			
				//champ_remise_volume
				function calcul_remise_volume(){			
					var obj = document.getElementById(identifiants[1]);
					var valeurSaisie = parseInt(obj.value);
					for(var j=0;j<tbl_codes.length;j++){
						if((valeurSaisie >= tbl_codes[j][0]) && (valeurSaisie < tbl_codes[j][1])){
							return tbl_codes[j][2]; break;
						}	
					}		
				}	
				
				//calcul remise mandataire
				function calcul_remise_mandataire(){
					if(document.getElementById(identifiants[11]).value == 1) return 3;
					else return 0;				
				}
				
				//calcul remise referentielle
				function calcul_remise_referentielle(){
					return 15;				
				}
				
				
				
							
				// calcul remise_saisonnalite
				function calcul_remise_saisonnalite(){
					function D10 (){
						if(document.getElementById(identifiants[3]).value < 25){
							return 0;
						}
						else {
							if((document.getElementById(identifiants[3]).value - document.getElementById(identifiants[2]).value) / document.getElementById(identifiants[2]).value < 0.4) {
								return 0;
							}else if(((document.getElementById(identifiants[3]).value - document.getElementById(identifiants[2]).value) / document.getElementById(identifiants[2]).value >= 0.4) && ((document.getElementById(identifiants[3]).value - document.getElementById(identifiants[2]).value) / document.getElementById(identifiants[2]).value < 0.5)){
								return 1;
							}else{
								return 2;
							}
						}
					}
					function D11 (){
						if(document.getElementById(identifiants[5]).value < 25){
							return 0;
						}
						else {
							if((document.getElementById(identifiants[5]).value - document.getElementById(identifiants[4]).value) / document.getElementById(identifiants[4]).value < 0.4) {
								return 0;
							}else if(((document.getElementById(identifiants[5]).value - document.getElementById(identifiants[4]).value) / document.getElementById(identifiants[4]).value >= 0.4) && ((document.getElementById(identifiants[5]).value - document.getElementById(identifiants[4]).value) / document.getElementById(identifiants[4]).value < 0.5)){
								return 1;
							}else{
								return 2;
							}
						}
					}
					function D12 (){
						if(document.getElementById(identifiants[7]).value < 25){
							return 0;
						}
						else {
							if((document.getElementById(identifiants[7]).value - document.getElementById(identifiants[6]).value) / document.getElementById(identifiants[6]).value < 0.4) {
								return 0;
							}else if(((document.getElementById(identifiants[7]).value - document.getElementById(identifiants[6]).value) / document.getElementById(identifiants[6]).value >= 0.4) && ((document.getElementById(identifiants[7]).value - document.getElementById(identifiants[6]).value) / document.getElementById(identifiants[6]).value < 0.5)){
								return 1;
							}else{
								return 2;
							}
						}
					}
					function D13 (){
						if(document.getElementById(identifiants[9]).value < 25){
							return 0;
						}
						else {
							if((document.getElementById(identifiants[9]).value - document.getElementById(identifiants[8]).value) / document.getElementById(identifiants[8]).value < 0.4) {
								return 0;
							}else if(((document.getElementById(identifiants[9]).value - document.getElementById(identifiants[8]).value) / document.getElementById(identifiants[8]).value >= 0.4) && ((document.getElementById(identifiants[9]).value - document.getElementById(identifiants[8]).value) / document.getElementById(identifiants[8]).value < 0.5)){
								return 1;
							}else{
								return 2;
							}
						}
					}
					var D10 = D10();
					var D11 = D11();
					var D12 = D12();
					var D13 = D13();
					var somme = D13 + D12 + D11 + D10;
					return somme;
				}	
				
				
				
				
				//champ_remise_developpement_annuel
				function calcul_remise_developpement_annuel(){
						
						var tmp = (document.getElementById(identifiants[1]).value - document.getElementById(identifiants[0]).value) / document.getElementById(identifiants[0]).value;
						
						if(document.getElementById(identifiants[0]).value==0){
							return 2;
						}
						else{
							if(tmp<0.4)
							{
								return 0;
							}else if((tmp>=0.4) && (tmp<0.5)){
								return 1;
							}
							else if(tmp>=0.5){
								return 2;
							}
							
						}
				}
				
				
				
				
				//champ_remise_total_plafonnees
				function calcul_remise_total_plafonnees(){
				var B26 = calcul_remise_volume();
				var B27 = calcul_remise_saisonnalite();
				var B28 = calcul_remise_developpement_annuel();
				
					if ((B26 + B27 + B28) < 24){
						return B26 + B27 + B28;
					}
					else{
						return 24;
					}
				}
				
				//champ_rremise_happy_day
				function calcul_remise_happy_day(){
					if (document.getElementById(identifiants[10]).value < 40){
						return 0;
					}
					else{
						if (document.getElementById(identifiants[10]).value < 50){
						return 2;
						}
						else{
							return 4;
						}
					}
				}
				
				// calcul remise total happy day
				function calcul_remise_total_happy_day(){
					return calcul_remise_total_plafonnees() + calcul_remise_happy_day();	
				};
				
		
				//champ_taux_financier
				function calcul_taux_financier(){	
					var calcul = (calcul_remise_total_happy_day() + ((100 - calcul_remise_total_happy_day()) * calcul_remise_referentielle()/100) + ((100 - calcul_remise_total_happy_day()) * (100 - calcul_remise_referentielle()) * calcul_remise_mandataire()/10000));
					return calcul.toPrecision(4);
				}
				
				//champ_net_facture
				function calcul_net_facture(){
					return ((document.getElementById(identifiants[1]).value * (100 - calcul_taux_financier())) / 100).toFixed(2);
				}
				
				// on effecture tout les calculs résultants
				document.getElementById(identifiants[12]).firstChild.nodeValue = calcul_remise_volume();
				document.getElementById(identifiants[13]).firstChild.nodeValue = calcul_remise_saisonnalite();
				document.getElementById(identifiants[14]).firstChild.nodeValue = calcul_remise_total_plafonnees();
				document.getElementById(identifiants[15]).firstChild.nodeValue = calcul_remise_happy_day();
				document.getElementById(identifiants[16]).firstChild.nodeValue = calcul_remise_total_happy_day();
				document.getElementById(identifiants[17]).firstChild.nodeValue = calcul_remise_referentielle();
				document.getElementById(identifiants[18]).firstChild.nodeValue = calcul_remise_mandataire();
				document.getElementById(identifiants[19]).firstChild.nodeValue = calcul_taux_financier();
				document.getElementById(identifiants[20]).firstChild.nodeValue = calcul_net_facture();
				document.getElementById(identifiants[21]).firstChild.nodeValue = calcul_remise_developpement_annuel();
			
			}
			else{
				remiseAzero();
			}	
}
