function preload()
{
var image_1 = new Image();
image_1.src = "../images/commande/photo-commande.gif";
}

function calculCommande()
{
	var nbCarnet = document.getElementById('nbCarnet');
	var valeurCheque = document.getElementById('valeurCheque');
	
	checkNumNbCarnet(nbCarnet);
	checkNumValeurCheque(valeurCheque);
	calculMontant();
	verifPlafond();
}

function checkNumNbCarnet(cellule){
	
	if(isNaN(cellule.value) === true)
	{
 		alert("La valeur entrée doit être un nombre.");
 		cellule.value=0;
 		return false;
 	}
	if(cellule.value <0)
	{
		alert("La valeur ne peut pas être négative.");
		cellule.value=0;
		return false;
	}
	if(cellule.value != parseInt(cellule.value))
	{
		alert("La valeur entrée doit être un nombre entier.");
		cellule.value=0;
		return false;
	}
	if(cellule.value<1 || cellule.value >50)
	{
		alert("Le nombre de carnets doit être compris entre 1 et 50.");
		cellule.value=0;
		return false;
	}
	return true;
}

function formatNombre(valeur,decimal,separateur) {
// formate un chiffre avec 'decimal' chiffres après la virgule et un separateur
	var deci=Math.round( Math.pow(10,decimal)*(Math.abs(valeur)-Math.floor(Math.abs(valeur)))) ; 
	var val=Math.floor(Math.abs(valeur));
	if ((decimal===0)||(deci==Math.pow(10,decimal))) {val=Math.floor(Math.abs(valeur)); deci=0;}
	var val_format=val+"";
	var nb=val_format.length;
	for (var i=1;i<4;i++) {
		if (val>=Math.pow(10,(3*i))) {
			val_format=val_format.substring(0,nb-(3*i))+separateur+val_format.substring(nb-(3*i));
		}
	}
	if (decimal>0) {
		var decim=""; 
		for (var j=0;j<(decimal-deci.toString().length);j++) {decim+="0";}
		deci=decim+deci.toString();
		val_format=val_format+"."+deci;
	}
	if (parseFloat(valeur)<0) {val_format="-"+val_format;}
	return val_format;
}

function checkNumValeurCheque(cellule){
	
	if(isNaN(cellule.value) === true)
	{
 		alert("La valeur entrée doit être un nombre.");
 		cellule.value=0;
 		return false;
 	}
	if(cellule.value <0)
	{
		alert("La valeur ne peut pas être négative.");
		cellule.value=0;
		return false;
	}
	if(cellule.value<2.4 || cellule.value >12)
	{
		alert("La valeur du chèque doit être comprise entre 2.4 et 12.");
		cellule.value=0;
		return false;
	}
	return true;
}

function verifPlafond()
{
	var valeurCheque = document.getElementById('valeurCheque');
	var commandePour = document.getElementById('commandePour');
	var particip = getSelectedRadioValue(document.getElementsByName('participation'));
	var plafond = document.getElementById('calculPlafond');

	var continu=true;
	
	if((commandePour=="salarie" || commandePour=="agent"))
	{
		if(isNaN(particip) === true)
		{
			continu=false;
			alert('plafond non selectionnez');
		}
	}
	
	if(continu)
	{
		var partEmployeur;
		var lib;
		if(commandePour.value=="salarie" || commandePour.value=="agent")
		{
			partEmployeur = parseFloat(valeurCheque.value) * (parseFloat(particip)/100);
			lib="Part employeur calculée";
		}
		else
		{
			//volontaire
			partEmployeur = parseFloat(valeurCheque.value);
			lib="Valeur du chèque";
		}
		if(parseFloat(partEmployeur) > parseFloat(plafond.value))
		{
			alert("La valeur du chèque dépasse l'exonération : \n - "+lib+" : " + formatNombre(partEmployeur,2,' ') + " \n - Plafond d'exonération : " + formatNombre(parseFloat(plafond.value),2,' '));	
		}
	}
}

function calculMontant()
{
	var calValeurNominal = document.getElementById('calValeurNominal');
	var calPrestation = document.getElementById('calPrestation');
	var calTotal = document.getElementById('calTotal');
	var PrestaCalcul = document.getElementById('PrestaCalcul');
	var PrestaMess = document.getElementById('PrestaMess');
	
	//init
	calValeurNominal.innerHTML = "00,00";
	calPrestation.innerHTML = "00,00";
	calTotal.innerHTML = "00,00";
	PrestaCalcul.style.display= '';
	PrestaMess.style.display= 'none';

	var nbCarnet = document.getElementById('nbCarnet');
	var valeurCheque = document.getElementById('valeurCheque');
	var text1 = document.getElementById('calculQteChequeForfait');
	var text2 = document.getElementById('calculForfaitPresta');
	
	var calculQteChequeForfait = parseInt(text1.value.replace(',','.'));
	var calculForfaitPresta = parseFloat(text2.value.replace(',','.'));
	
	var valeurNominalTotal;
	var valeurTotal;
	var forfait;
	
	valeurNominalTotal = (nbCarnet.value * 20) * valeurCheque.value;
	calValeurNominal.innerHTML = formatNombre(valeurNominalTotal,2,' ');
	if((nbCarnet.value * 20)> calculQteChequeForfait ) 
	{
		forfait=0;
		PrestaCalcul.style.display= 'none';
		PrestaMess.style.display= '';
	}
	else
	{
		forfait=calculForfaitPresta;
		calPrestation.innerHTML = formatNombre(forfait,2,' ');
		PrestaCalcul.style.display= '';
		PrestaMess.style.display= 'none';
	}
	valeurTotal = parseFloat(valeurNominalTotal) + parseFloat(forfait);
	calTotal.innerHTML = formatNombre(valeurTotal,2,' ');

}

function participation()
{
	var commandePour = document.getElementById('commandePour');
	var lignePart = document.getElementById('lignePart');
	
	if(commandePour.value == 'salarie' || commandePour.value == 'agent')
	{
		lignePart.style.display= '';
	}
	else
	{
		lignePart.style.display= 'none';
	}

}

function verifLivraison()
{
	var livr = document.getElementById('livraison');
	var ligne1 = document.getElementById('livr1');
	var ligne2 = document.getElementById('livr2');
	var ligne3 = document.getElementById('livr3');
	var ligne4 = document.getElementById('livr4');
	
	if(livr.checked !== null && livr.checked===true)
	{
		ligne1.style.display = '';
		ligne2.style.display = '';
		ligne3.style.display = '';
		ligne4.style.display = '';
	}
	else
	{
		ligne1.style.display = 'none';
		ligne2.style.display = 'none';
		ligne3.style.display = 'none';
		ligne4.style.display = 'none';
	}
}

function verifAcceptCondition()
{
	var commandePour = document.getElementById('commandePour');
	var divSalarieAgent = document.getElementById('acceptConditionSalarieAgent');
	var divBenevoleVolontaire = document.getElementById('acceptConditionBenevoleVolontaire');
	
	if(commandePour.value == 'salarie' || commandePour.value == 'agent')
	{
		divSalarieAgent.style.display = 'block';
		divBenevoleVolontaire.style.display = 'none';
	}
	else
	{
		divSalarieAgent.style.display = 'none';
		divBenevoleVolontaire.style.display = 'block';
	}
}

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} 

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i;
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
}
