// JavaScript Document

var Panier = {

	TVQ : 0,
	TPS :0,
	totalTaxable :0,
	totalNonTaxable : 0,
	totalTaxer : 0,
	totalNonTaxer : 0, 
	total :0, 
	
	init : function(nbrTotaleChampTaxable, nbrTotaleChampNonTaxable)
	{
		
    	this.nbrTotaleChampTaxable = nbrTotaleChampTaxable;
	  	this.nbrTotaleChampNonTaxable = nbrTotaleChampNonTaxable;
		
    	for(i = 1; i<=this.nbrTotaleChampTaxable; i++)
    	{
    		this['totalItemTaxable'+i]= 0;
    	}
    	
    	for(i = 1; i<=this.nbrTotaleChampNonTaxable; i++)
    	{
    		this['totalItemNonTaxable'+i]= 0;
    	}


	},//init
	//======================================================================================
	//======================================= CALCULE ======================================
	//======================================================================================
	_calculer : function(valeur, multiple, id, taxe, prefix)
	{
		//on cible le champ qui 
		//contient la somme a ajouter

		var cible = document.getElementById(prefix+id);
		
		//on test quelle taxe est appliquable
		if(taxe == 'non')
		{
			//on multiplie cette somme au nombre 
			//de produits commander
		
			this['totalItemNonTaxable'+id] = valeur*multiple;
			val = this['totalItemNonTaxable'+id];

		
			//on ajoute la somme au total non taxable
			this._calculerTotalNonTaxable();
			
		
			//on affiche le resultat
			this._afficherTotalNonTaxable();
		}
		else if(taxe == 'oui' || taxe =="Autre" )
		{
			//on multiplie cette somme au nombre 
			//de produits commander
			this['totalItemTaxable'+id] = valeur*multiple;
			//si les taxes sont appliquables 
			//on appliques la TVQ et la TPS
			this._appliquerTaxes(taxe);
		}
		
		this._afficherGrandTotal();
	}, //calcule
	
	//======================================================================================
	//================================= GESTION DES TAXES ==================================
	//======================================================================================
	_appliquerTaxes : function(taxe)
	{
	
	   	//on ajoute la somme au total taxable
		this._calculerTotalTaxable();
		
		//on ajoute les taxe
		this._calculerTaxes(taxe);
		
		//on affiche le resultat
		this._afficherTotalTaxable();
	
	}, //gestionDesTaxes
	
	_calculerTaxes : function(taxe)
	{
		//si le client vit dans une region autre que celle du quebec
		//on applique pas la TPS
		cibleProvince = document.getElementById('province_-informations_professionnelles');
		if(taxe != 'Autre' && cibleProvince.value != 'Autre' )
		{
			this._calculerTPS();		
		}
		else
		{
      this.TPS = 0;
      this._afficheTPS();
    }
		
		this._calculerTVQ();
		
	},//_calculerTaxes
	
	_calculerTPS : function()
	{
		//on calcule la TVQ
		this.TPS = ((5 * this.totalTaxable) / 100);
		
		//on arrondi a deux chiffre apres la virgule
		 this.TPS.toFixed(2)
		
		//on affiche les TPS
		this._afficheTPS();
	},//_calculeTPS
	
	_afficheTPS : function()
  {
     cible = document.getElementById('afficherTPS');
     cible.innerHTML = this.TPS;
	 
	 cible2 = document.getElementById('tpstotal-cout_total');
     cible2.value = this.TPS;
	 
	 
  },//_afficheTPS
  
	_calculerTVQ : function()
	{
		//on calcule la TVQ
		this.TVQ = parseFloat(((7.50 * (parseFloat(this.totalTaxable)+parseFloat(this.TPS))) / 100));
		
		//on arrondi a deux chiffre apres la virgule
		 this.TVQ.toFixed(2)
					
		//on affiche les TVQ
		this._afficheTVQ();
	},//_calculeTVQ
	
	_afficheTVQ : function()
  {
     cible = document.getElementById('afficherTVQ');
     cible.innerHTML =  this.TVQ.toFixed(2);
	 
	 cible2 = document.getElementById('tvqtotal-cout_total');
     cible2.value =  this.TVQ.toFixed(2);
	 
	
  },//_afficheTVQ
	
	
	//======================================================================================
	//=================================== TOTAL TAXABLE ====================================
	//======================================================================================
	_afficherTotalTaxable : function()
	{
		this._calculerTotalTaxer();

		//on cible ou afficher le resultat
		cible = document.getElementById('afficherTotalTaxable');
		cible.innerHTML = this.totalTaxable;
		
		cible2 = document.getElementById('soustotaltotal-cout_total');
		cible2.value = this.totalTaxable;
		
	},//_afficherTotalTaxable
	
	_calculerTotalTaxer : function()
	{

		this.totalTaxer = this.TVQ  + this.TPS  + this.totalTaxable;
	}, //_calculerTotalTaxable
	
	
  _calculerTotalTaxable : function()
	{
  	this.totalTaxable =0;
  	
  	
    for(i = 1; i<=this.nbrTotaleChampTaxable; i++)
		{
			this.totalTaxable += this['totalItemTaxable'+i];
		}
	}, //_calculerTotalTaxe
	
	//======================================================================================
	//================================= TOTAL NON TAXABLE ==================================
	//======================================================================================
	_afficherTotalNonTaxable : function()
	{
		this._calculerTotalNonTaxable();
		
		//on cible ou afficher le resultat
		cible = document.getElementById('afficherTotalNonTaxer');
		cible.innerHTML = this.totalNonTaxer.toFixed(2);
		this._afficherGrandTotal();
		
		cible2 = document.getElementById('commanditetotal-cout_total');
		cible2.value = this.totalNonTaxer.toFixed(2);
	
	},
	
	_calculerTotalNonTaxable : function()
	{
		this.totalNonTaxer = 0 ;
		
    for(i = 1; i<=this.nbrTotaleChampNonTaxable; i++)
		{
			this.totalNonTaxer += this['totalItemNonTaxable'+i];
			test = this['totalItemNonTaxable'+i];
		}
		this.totalNonTaxer.toFixed(2);
	}, //_calculerTotalNonTaxable
	
	//======================================================================================
	//====================================== TOTAL =========================================
	//======================================================================================
	
	_afficherGrandTotal : function()
	{
		
		//on cible ou afficher le resultat
		cible = document.getElementById('afficherGrandTotal');
		this.grandTotal =  this.totalNonTaxer + this.totalTaxer 
		cible.innerHTML = this.grandTotal.toFixed(2);
		
		cible2 = document.getElementById('totaltotal-cout_total');
		cible2.value = this.grandTotal.toFixed(2);
		
		
	}, //_afficherGrandTotal
	
	//======================================================================================
	//==================================== NOUVEAU CHAMP ===================================
	//======================================================================================
	_afficherChamp : function(valeur, id, participant, vNom, vFonction, vCourriel, table)
	{
		fonction= '';
		sortie = '';
	
		 var nbrNoeud = this._compterNombreTagName('affiche-val'+id);
	 	cible = document.getElementById('affiche-val'+id);
		cibleSelect = document.getElementById('select'+id);
		//this.compterNombreTagName('affiche-val'+id, 'fieldset');

	   //supprimer
		 if(valeur < nbrNoeud)
		 {
	  
			for(i = nbrNoeud; i > valeur; i-- )
			{
			  this._effacerNoeud('affiche-val'+id, (i-1));
			}
		 }//ajouter
		 else if(id=='5')
		 {
	
				for(i = nbrNoeud; i < valeur; i++ )
				{
				  if(i <4)
				  {
					this._creerChamp('affiche-val'+id, (i+1),  participant, vNom, vFonction, vCourriel, table);
				  }
				}
	
		
		 }
		 else
		 {
	       for(i = nbrNoeud; i < valeur; i++ )
				{
			
					this._creerChamp('affiche-val'+id, (i+1),  participant, vNom, vFonction, vCourriel, table);
			
				}
		  }
		 
		 this._afficherSelectionEquipe(valeur, id, participant);
	},//afficheChamp
	
	_afficherSelectionEquipe : function(valeur, id, participant )
	{
		cibleSelect = document.getElementById('select'+id);
		//alert(participant)
		if(participant == 'Droit de jeu individuel' && valeur < 4 && valeur > 0)
		 {
			cibleSelect.innerHTML = '<p style="clear:both;"><label style="width:300px; font-size:1.1em;"><strong>Mon équipe de quatre joueurs sera</strong> </label>\n<select name="etatEquipe" id="SelectEquipe" onchange=\'Panier._definirEquipe(this.value, "select'+id+'", "");\'>\n<option value="">---</option>\n<option value="jumelée avec">complétée par les joueurs suivants : </option>\n<option value="jumelée par Éole">complétée par Éole Québec</option>\n</select>\n</p>\n<p id="jumelerAvec"></p>\n';
			this._choisirHorraire(id, '');
		
		 
		 }
		 else if(participant == 'Droit de jeu individuel' && valeur == 0 || valeur == 4)
		 {
		cibleSelect.innerHTML = '';	 
	
		 }
		
	},//_afficherSelectionEquipe
	
	_definirEquipe : function(equipe, id, value)
	{
		
			parent = document.getElementById(id);
			cible = document.getElementById('jumelerAvec');
			if(equipe == 'jumelée avec' && cible.innerHTML == '' )
			{         
			
	
				cible.innerHTML = '<input name="jumelerAvec" type="text" value="'+value+'" /> <br/><strong>S.V.P. nous informer ici</strong>';
				selectEquipe = document.getElementById('SelectEquipe');
				selectEquipe.options[1].selected=true;
				
			}
			else  if( equipe == 'jumelée par Éole' && cible.innerHTML != '')
			{
			
				cible.innerHTML='';
			}
			
	},//_definirEquipe
	
	_choisirHorraire : function(id, value)
	{
		cibleSelect = document.getElementById('select'+id);
		cible = document.getElementById('horraire'+id);
		
		if(cible == null)
		{
			horraire = "<p id='choixHorraire' >\n &nbsp;L'heure  de départ est fixée à 10h30\n</p>\n"	;
						
			var div = document.createElement('div');
			div.setAttribute('id', 'horraire'+id);
			cibleSelect.appendChild(div);
			div.innerHTML = horraire;

			
		}
		if(value == '13h00')
		{
		   document.getElementById('horraire').options[1].selected=true;
		}
	}, //_choisirHorraire

	
	   _creerChamp : function(parent, id, participant, vNom, vFonction, vCourriel, table)
     {

         // recherche du noeud parent
        var divParent = document.getElementById(parent);
         
        // création des nouveaux noeuds
        var nouveauFieldset= document.createElement('fieldset');
        var idFieldset = (participant == "Droit d'accès journalier par personne" || participant == 'Droit de jeu individuel' ) ? 'participant'+id : 'invite'+id  ;
        nouveauFieldset.setAttribute("class","autreParticipant");
		nouveauFieldset.setAttribute("id",idFieldset);
        
		divParent.appendChild(nouveauFieldset);
        var fieldsetLegend = document.createElement('legend');
        
        var nomP = document.createElement('p');
        var nomLabel = document.createElement('label');
        var nomInput = document.createElement('input');
        
		
		var courrielP = document.createElement('p');
		var courrielLabel = document.createElement('label');
		var courrielInput = document.createElement('input');
		
		if(participant == "Droit d'accès journalier par personne")
        {
			courrielP.style.display='none';
		}
		
        if(participant == "Droit d'accès journalier par personne" || participant == 'Droit de jeu individuel' ) 
        { 
          var fonctionP = document.createElement('p');
          var fonctionLabel = document.createElement('label');
          var fonctionInput = document.createElement('input');
		}
		
		
         if(participant == 'Droit de jeu individuel')
        { 
          var participantP = document.createElement('p');
          var participantLabel = document.createElement('label');
          var participantSelect = document.createElement('select');
          var participantOption1 = document.createElement('option');
          var participantOption2 = document.createElement('option');
          var participantOption3 = document.createElement('option');
        }
        
		   // raccord des noeuds
       
        nouveauFieldset.appendChild(fieldsetLegend);
        
        nouveauFieldset.appendChild(nomP);
        nomP.appendChild(nomLabel);
        nomP.appendChild(nomInput);
        
        if(participant == "Droit d'accès journalier par personne" || participant == 'Droit de jeu individuel')
        {
          nouveauFieldset.appendChild(fonctionP);
          fonctionP.appendChild(fonctionLabel);
          fonctionP.appendChild(fonctionInput);
        }
		
		nouveauFieldset.appendChild(courrielP);
		courrielP.appendChild(courrielLabel);
		courrielP.appendChild(courrielInput);
	
		
        // paramétrage des nouveaux noeuds
         var legend = (participant == "Droit d'accès journalier par personne" || participant == 'Droit de jeu individuel' ) ? 'Participant n° ' + id : 'Invité n° ' + id  ;
        fieldsetLegend.appendChild(document.createTextNode(legend));
        
        
        nomLabel.appendChild(document.createTextNode('Nom : '));
        nomLabel.setAttribute("for",'nom'+id);
        nomInput.name = 'nomParticipant'+table+'[]';
        nomInput.id = 'nom'+id;
        nomInput.type = 'text';
        nomInput.value = vNom;

		if(participant == 'Droit de jeu individuel')
	 	{
			valeurChamp = 'Courriel :';
		}
		else
		{
			valeurChamp = 'Entreprise :';	
		}
		courrielLabel.appendChild(document.createTextNode(valeurChamp));
		
		courrielLabel.setAttribute("for",'courriel'+id);
		courrielInput.name = 'courrielParticipant'+table+'[]';
		courrielInput.id = 'courriel'+id;
		courrielInput.type = 'text';
		courrielInput.value = vCourriel;
	
		
        if(participant == "Droit d'accès journalier par personne" ||participant ==  'Droit de jeu individuel')
        {
          fonctionLabel.appendChild(document.createTextNode('Entreprise : '));
          fonctionLabel.setAttribute("for",'fonction'+id);
          fonctionInput.name = 'fonctionParticipant'+table+'[]';
          fonctionInput.id = 'fonction'+id;
          fonctionInput.type = 'text';
          fonctionInput.value = vFonction;
        }
		
		
        
     

     },
	
	
	_compterNombreTagName : function(id)
	{
		cible = document.getElementById(id);
		noeud = cible.getElementsByTagName('fieldset');
		total = noeud.length;
		return total;
	
	},//_compterNombreTagName
	
	_effacerNoeud :function(id, idNoeud)
	{
        mere = document.getElementById(id);
        noeud = mere.getElementsByTagName('fieldset');
        mere.removeChild(noeud[idNoeud]);
		
	}, //_effacerNoeud
	
	_reAfficherChamp : function(sortie, id)
	{
		cible = document.getElementById('affiche-val'+id);
		cible.innerHTML += sortie;
	}//_reAfficherChamp


	
	
}//panier



