//var glo
var tabParticipant = new Array();

//Pour cr�er une instance ajax en fonction du navigateur
function httpRequest()
{
  var xhr; 
  try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
  catch (e){
    try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
    catch (e2){
      try {  xhr = new XMLHttpRequest();     }
      catch (e3) {  xhr = false;   }
      }
    }
  return xhr ;  
}



function resa(action){
  tabParticipant = new Array();
  var elem_act ;
  if (action=='nuit'){
    elem_act = document.getElementById('sejour').options[document.getElementById('sejour').selectedIndex].value ;
  }
  if (action=='participant'){
     var nuit = document.getElementById('nuits').options[document.getElementById('nuits').selectedIndex].value ;
     var sejour = document.getElementById('sejour').options[document.getElementById('sejour').selectedIndex].value ;
     elem_act=nuit ;
  }
  if (action=='hebergement'){ 
     var nuit = document.getElementById('nuits').options[document.getElementById('nuits').selectedIndex].value ;
     var sejour = document.getElementById('sejour').options[document.getElementById('sejour').selectedIndex].value ;
     var participant = document.getElementById('participants').options[document.getElementById('participants').selectedIndex].value ;
     elem_act=sejour+"-"+participant+"-"+nuit ;
  }
  if (action=='formulaire'){
     var nuit = document.getElementById('nuits').options[document.getElementById('nuits').selectedIndex].value ;
     var participant = document.getElementById('participants').options[document.getElementById('participants').selectedIndex].value ;
     var hebergement = document.getElementById('hebergements').options[document.getElementById('hebergements').selectedIndex].value ;

     document.getElementById('datenuitsejour').value = nuit ;
     document.getElementById('hebergementsejour').value = hebergement ;

     elem_act = participant+"-"+nuit+"-"+hebergement  ;

  }

  var xhr = httpRequest() ;
  xhr.onreadystatechange  = function()
  {
    if(xhr.readyState  == 4)
      {
        if(xhr.status  == 200){ 
          var resp =  xhr.responseText;
          document.getElementById(action).innerHTML = resp ;
        }
        else alert("Error code " + xhr.status);
      }
  }
  xhr.open( 'GET', "modules/reservation/resa.php?act="+action+"&elem_act="+elem_act,  true);
  xhr.send(null);
}


function calcul_prix(participant, choix){
  var nbparticipant = document.getElementById('participants').options[document.getElementById('participants').selectedIndex].value ;
  nbparticipant = nbparticipant.split('-');
  nbparticipant = nbparticipant[0];
  var i ;
  var prixTotal = 0 ;

  //prix par participant
  for(i = 1; i<=nbparticipant ; i++){ 
    prix = document.getElementById('prix_sanspromo_'+i).value ;
	if(i == participant){
		//assurance
		if(choix == 1){prix = Math.round((parseFloat(prix)+parseFloat(document.getElementById('prix_assurance_'+participant).value))*100)/100;}
		//tranport
		if(choix == 2){prix = Math.round((parseFloat(prix)+parseFloat(document.getElementById('prix_transport_'+participant).value))*100)/100;}
		//on fait la reduc si besoin
                if(choix == 4)
                {
                    if (document.getElementById('prixpromo_'+i).value!=0 )
                    {
                        prix = Math.round((parseFloat(prix)-parseFloat(document.getElementById('prixpromo_'+i).value))*100)/100;
                    }
                }
	}
    if ( document.getElementById('pourcentagepromo_'+i).value!=0 ){
       prix = Math.round((parseFloat(prix)-(parseFloat(document.getElementById('prix_heberg_'+i).value)*(parseFloat(document.getElementById('pourcentagepromo_'+i).value)/100)))*100)/100;
    }
    document.getElementById('prix_sanspromo_'+i).value = prix ;
    document.getElementById('prix_avecpromo_'+i).value = prix ;
    document.getElementById('prix_'+i).innerHTML =  prix ;
    prixTotal = Math.round((parseFloat(prixTotal)+parseFloat(prix))*100)/100;
    
  }
  //prix total
  document.getElementById('prix_total').innerHTML = Math.round(((prixTotal)*100)/100) ;

  return true ;
}


function assurance(participant,idAssurance,prix,choix){
  var idAssu = 'assurance_'+participant+'_'+idAssurance;
  var idAssuSans = 'assurance_sans_'+participant;
  var prixParticipant = document.getElementById('prix_sanspromo_'+participant).value ;
  if(choix == 0)
  {
	  if (document.getElementById(idAssu).checked){
		//alert('Participant '+participant+' : '+prixParticipant+' + '+prix+' euros ');
		document.getElementById('prix_sanspromo_'+participant).value = Math.round((parseFloat(prixParticipant)+parseFloat(prix))*100)/100;
	  }
	  else{
		//alert('Participant '+participant+' - '+prix+' euros ');
		document.getElementById('prix_sanspromo_'+participant).value = Math.round((parseFloat(prixParticipant)-parseFloat(prix))*100)/100;
	  }
  }
  else if(choix == 1)
  {
	  if(document.getElementById(idAssu).checked)
	  {
		  document.getElementById('prix_sanspromo_'+participant).value = Math.round((parseFloat(prixParticipant)-parseFloat(prix))*100)/100;
	  }
  }
  calcul_prix(participant,1);
}


//Quand on change l'assurance : variation de prix
/*function assurance(participant,idAssurance,prix){
  var sejour = document.getElementById('nuits').options[document.getElementById('nuits').selectedIndex].value ;

  var xhr = httpRequest() ;
  xhr.onreadystatechange  = function(){
      if(xhr.readyState  == 4){
          if(xhr.status  == 200){
        var resp =  xhr.responseText.split('#');
          prix = resp[0] ;
          	document.getElementById('prix_assurance_'+participant).value = prix - document.getElementById('prix_assurance_hidden_'+participant).value;
		  //Modif
		  document.getElementById('prix_assurance_hidden_'+participant).value = prix;
		  //Fin modif
          calcul_prix(participant,1);
          }
          else alert("Error code " + xhr.status);
        }
    }
  xhr.open( 'GET', "modules/reservation/assurance.php?sej="+sejour+"&idAssurance="+idAssurance, true);
  xhr.send(null);

}*/

//Quand on change le transport : variation de prix
function transport(participant){
  var sejour = document.getElementById('dates').options[document.getElementById('dates').selectedIndex].value ;
  var idTransport = document.getElementById('transport_'+participant).options[document.getElementById('transport_'+participant).selectedIndex].value ;
  var prixParticipant = document.getElementById('prix_sanspromo_'+participant).value ;

  var xhr = httpRequest() ;
  xhr.onreadystatechange  = function(){
      if(xhr.readyState  == 4){
          if(xhr.status  == 200){
        var resp =  xhr.responseText.split('#');
          prix = resp[0] ;
          document.getElementById('prix_transport_'+participant).value = prix;
          calcul_prix(participant,3);
          }
          else alert("Error code " + xhr.status);
        }
    }
  xhr.open( 'GET', "modules/reservation/transport.php?sej="+sejour+"&idtrans="+idTransport, true);
  xhr.send(null);
}

//Quand on coche une option : variation de prix
function option(option,participant,prix){
  var idOption = 'option_'+option+'_'+participant ;
  var prixParticipant = document.getElementById('prix_sanspromo_'+participant).value ;
  if (document.getElementById(idOption).checked){
    //alert('Participant '+participant+' : '+prixParticipant+' + '+prix+' euros ');
    document.getElementById('prix_sanspromo_'+participant).value = Math.round((parseFloat(prixParticipant)+parseFloat(prix))*100)/100;
  }
  else{
    //alert('Participant '+participant+' - '+prix+' euros ');
    document.getElementById('prix_sanspromo_'+participant).value = Math.round((parseFloat(prixParticipant)-parseFloat(prix))*100)/100;
  }
  calcul_prix(participant,2);
}

//V�rification du code promo
function code_promo(participant){
  var sejour = document.getElementById('dates').options[document.getElementById('dates').selectedIndex].value ;
  var sexe = document.getElementById('sexe_'+participant).options[document.getElementById('sexe_'+participant).selectedIndex].value ;
  if (document.getElementById('promo_'+participant).value!=''){
    var erreur = 0 ;
    for (var i=0; i<tabParticipant.length;i++){
      if(tabParticipant[i]==participant){ /* Le gars � d�ja un code d'activ� */ erreur=1 ;}
    }

    if (!erreur){
    var xhr = httpRequest() ;
    xhr.onreadystatechange  = function()
    {
      if(xhr.readyState  == 4)
        {
          if(xhr.status  == 200){
            var resp =  xhr.responseText.split('#');
            if (resp['0']==0){ alert(resp['1']); document.getElementById('promo_'+participant).value='' ; }
            if (resp['0']==1){
               document.getElementById('pourcentagepromo_'+participant).value=resp['1'] ;
               document.getElementById('prixpromo_'+participant).value=resp['2'] ;
			   
               alert('Code Promo pris en compte !');
               calcul_prix(participant,4,0);
              //une seule r�duc par participant
              tabParticipant.push(participant);
            }
          }
          else alert("Error code " + xhr.status);
        }
    }
    xhr.open( 'GET', "modules/reservation/promo.php?sej="+sejour+"&sexe="+sexe+"&participant="+participant+"&code="+document.getElementById('promo_'+participant).value, true);
    xhr.send(null);
    }
  }
}

//Quand on valide on fait les v�rification
function validation(nbparticipant){
  //alert('Validation : verification pour les '+nbparticipant+' participants');
  var message = 'Erreur :' ;
  var erreur = 0 ;
  var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/ ;

  //verification : champs remplis
  for(var i = 1; i<=nbparticipant ; i++){ 
    message += '\n Participant '+i+' : \n ' ;
    var checked = false;
    //var choix_assurance = document.getElementsByName('assurance_'+i) ;

    if (document.getElementById('nom_'+i).value==''){ erreur=1; message+='Remplissez le nom\n '; }
    if (document.getElementById('prenom_'+i).value==''){ erreur=1; message+='Remplissez le pr�nom\n '; }
    if (document.getElementById('nationalite_'+i).value==''){ erreur=1; message+='Remplissez la nationalit�\n '; }
    if ((document.getElementById('ddn_'+i).value=='')||(document.getElementById('ddn_'+i).value=='JJ/MM/AAAA')){ erreur=1; message+='Remplissez le date de naissance\n '; }
    if (document.getElementById('sexe_'+i).options[document.getElementById('sexe_'+i).selectedIndex].value=='rien'){ erreur=1; message+='Selectionn� le sexe\n '; }
    if (document.getElementById('adresse_'+i).value==''){ erreur=1; message+='Remplissez l\'adresse\n '; }
    if (document.getElementById('cp_'+i).value==''){ erreur=1; message+='Remplissez le code postal\n '; }
    if (document.getElementById('ville_'+i).value==''){ erreur=1; message+='Remplissez la ville\n '; }
    if (document.getElementById('pays_'+i).value==''){ erreur=1; message+='Remplissez le pays\n '; }
    if (document.getElementById('tel_'+i).value==''){ erreur=1; message+='Remplissez le num�ro de t�l�phone\n '; }
    if (document.getElementById('mail_'+i).value==''){ erreur=1; message+='Remplissez l\'adresse mail\n '; }
    if (document.getElementById('mail2_'+i).value==''){ erreur=1; message+='Remplissez l\'adresse mail de v�rification\n '; }
    if (document.getElementById('cgv_'+i).checked==false){ erreur=1; message+='Acceptez les conditions g�n�rales de vente\n '; }

    /*if (choix_assurance){
      var max_choix_assurance = choix_assurance.length

      for(var cpt = 0; cpt < max_choix_assurance; cpt++){
      	if(document.getElementsByName('assurance_'+i)[cpt].checked==true){ checked = true ; }
      }
      if (!checked){ erreur=1; message+='Selectionnez un mode d\'assurance\n '; }
    }*/

    //verification : champs correct
    if (erreur==0){
      if (document.getElementById('tel_'+i).value.length<5){ erreur=1; message+='Erreur : num�ro de t�l�phone trop court\n '; }
      if (document.getElementById('tel_'+i).value.length>17){ erreur=1; message+='Erreur : num�ro de t�l�phone trop long\n '; }
      if ((reg.exec(document.getElementById('mail_'+i).value)!=null)==false){ erreur=1; message+='Erreur : Rentrez une adresse mail valide\n '; }
      if ((reg.exec(document.getElementById('mail2_'+i).value)!=null)==false){ erreur=1; message+='Erreur : Rentrez une adresse mail de v�rification valide\n '; }
      if ((document.getElementById('mail2_'+i).value)!=(document.getElementById('mail_'+i).value)){ erreur=1; message+='Erreur : l\'adresse mail et l\'adresse mail de v�rification sont diff�rentes\n ';
      }
    }
  }


  if (erreur!=0){
     alert(message) ;
     return false ;
  }
  else{ return true ; }
  
}


//Fonction cochant et d�cochant checkbox assurance
function check_assurance(nbassu,numpart,ordre)
{
	if(ordre == 0)
	{
		j = 1;
		while(j <= nbassu)
		{
			if(document.getElementById('assurance_sans_'+numpart).checked == true){
				prix = parseFloat(document.getElementById('assurance_'+numpart+'_'+j).value);
				assurance(numpart,j,prix,1);
				document.getElementById('assurance_'+numpart+'_'+j).checked = false;
			}	
			j = j + 1;
		}
	}
	else
	{
		if(document.getElementById('assurance_sans_'+numpart).checked == true){document.getElementById('assurance_sans_'+numpart).checked = false;}
	}
}

//Fonction paiement_separe
function paiement_separe(nb)
{
	d = "0";
	if(nb > 1)
	{
		i = 1;
		while(i <= nb){
			if(document.getElementById('paiement_separe_'+i+'_0').checked != true){d = "1"}
       		i = i + 1;
        } 	
		if(d == "1")
		{
			i = 1;
			while	(i <= nb){
				if(document.getElementById('paiement_separe_'+i+'_1').checked != true){d = "2"}
       			i = i + 1;
			} 	
		}
	}
	if(nb == "2" && d == "2"){d="1";}
	document.getElementById('paiement_separe_groupe').value = d;
}

//fonction moyen de paiement
function moyen_paiement(nb,i)
{
	if(document.getElementById('paiement_separe_groupe').value == "0" || document.getElementById('paiement_separe_groupe').value == "2")
	{
		if(document.getElementById('paiement_separe_'+i+'_0').checked == true){
			j = 1;
			while(j <= nb)
			{
				if(document.getElementById('paiement_separe_'+j+'_0').checked == true)
				{
					if(document.getElementById('paiement_cheque_'+i+'_0').checked == true){
						document.getElementById('paiement_cheque_'+j+'_0').checked = true;
						document.getElementById('paiement_cheque_'+j+'_1').checked = false;
					}
					if(document.getElementById('paiement_cheque_'+i+'_0').checked == false){
						document.getElementById('paiement_cheque_'+j+'_0').checked = false;
						document.getElementById('paiement_cheque_'+j+'_1').checked = true;
					}
				}
				j = j + 1;
			}
		}
	}
}

//Fonction g�rant le choix multiple ou non des activit�s
function choix_multiple(option,participant,lesoptions)
{
	var idOption = 'option_'+option+'_'+participant ;
	if(document.getElementById(idOption).value == "1")
	{
		//On r�cup�re le tableau php
		lesoptions = lesoptions.split(',');
		if(document.getElementById(idOption).checked == true){
			for(var i=0; i<lesoptions.length; i++)
			{
				if(lesoptions[i] != option){
					if(document.getElementById('option_'+lesoptions[i]+'_'+participant).value == "1")
					{
						//Le choix n'est pas compatible
						document.getElementById('option_'+lesoptions[i]+'_'+participant).disabled = true;
					}
				}
			}
		}
		if(document.getElementById(idOption).checked == false)
		{
			for(var i=0; i<lesoptions.length; i++)
			{
				document.getElementById('option_'+lesoptions[i]+'_'+participant).disabled = false;
			}	
		}
	}
}

//Function calcul reduction
function calcul_reduction()
{

}
