/* Main Javascript */

if (navigator.appVersion.indexOf("MSIE")!=-1){
	
   var temp		=	navigator.appVersion.split("MSIE");
	
   var versionNav	=	parseFloat(temp[1]);

}

$(document).ready(function(){

   //Action du checkBox Seconde Addre de Facturation
  $("#cboxFac").click(function(){
    if (!document.getElementById('cboxFac').checked){
      $('#facDiff').hide();
      $(".requiered_fal").each( function(){
        if( $(this).val() == ''){
          $(this).removeClass('requiered');
          $(this).removeClass('error');
          $(this).focus( function() { $(this).removeClass('error'); } );
        }
      });
    }else{
      $('#facDiff').show();
      $(".requiered_fal").each( function(){
        if( $(this).val() == ''){
          $(this).addClass('requiered');
          $(this).focus( function() { $(this).removeClass('error'); } );
        }
      });    }
  });

  $("#college").change(
      function(){
         if (jQuery(this).val() == "Autre"){
            jQuery('.hideOtherChoise').show();
         }else{
            jQuery('.hideOtherChoise').hide();
         }
      }
  );

  $('.nbr').keypress(function (event){
  	// Compatibilité IE / Firefox
  	if(!event&&window.event) {
  		event=window.event;
  	}
  	// IE
  	if((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 8 && event.keyCode != 9 && event.keyCode != 46 && event.keyCode != 37 && event.keyCode != 39 && event.keyCode != 40 && event.keyCode != 116) {
  		event.returnValue = false;
  		event.cancelBubble = true;
  	}
  	// DOM
  	if( (event.which < 48 || event.which > 57) && event.which != 8 && event.which != 9 && event.which != 46 && event.which != 37 && event.which != 39 && event.which != 40 && event.which != 116) {
  		event.preventDefault();
  		event.stopPropagation();
  	}
  });

  //Action du checkBox Seconde Addre de Facturation
  $("#btnOne").click(function (){
    var findError = false;
    var i = 0;
    var msgError = new Array();
    $(".requiered").each( function(){
        if( $(this).val() == ''){
          findError = true;
          $(this).addClass('error');
          $(this).focus( function() { $(this).removeClass('error'); $('#msgErrorAll').hide(); } );
          msgError[i] = new Array();
          msgError[i]['champ'] = $(this).attr('id');
          msgError[i]['erreur'] = 'Ce champ est obligatoire.<br />';
          i++;
        }
    });
    
    if ( $('#email').val() != '' && $('#cemail').val() != '' )
    {
      if (  $('#email').val() != $('#cemail').val() )
      {
        msgError[i] = new Array();
        msgError[i]['champ'] = 'email';
        msgError[i]['erreur'] = 'Les adresses mail doivent être identiques.<br />';
        i++;
        msgError[i] = new Array();
        msgError[i]['champ'] = 'cemail';
        msgError[i]['erreur'] = 'Les adresses mail doivent être identiques.<br />';
        
        findError = true;
        $('#email').addClass('error');
        $('#email').focus( function() { $(this).removeClass('error'); $('#msgErrorAll').hide(); } );
        $('#cemail').addClass('error');
        $('#cemail').focus( function() { $(this).removeClass('error'); $('#msgErrorAll').hide(); } );
      }else{
        var reg= /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
        if(reg.test($('#email').val())==false) {
          msgError[i] = new Array();
          msgError[i]['champ'] = 'email';
          msgError[i]['erreur'] = 'La syntaxe est incorrecte.<br />';
          i++;
          msgError[i] = new Array();
          msgError[i]['champ'] = 'cemail';
          msgError[i]['erreur'] = 'La syntaxe est incorrecte.<br />';
          findError = true;
          $('#email').addClass('error');
          $('#email').focus( function() { $(this).removeClass('error'); $('#msgErrorAll').hide(); } );
          $('#cemail').addClass('error');
          $('#cemail').focus( function() { $(this).removeClass('error'); $('#msgErrorAll').hide(); } );          
        }
      }
    }
    if ($("#cboxCGV").attr("checked") != true ){
        msgError[i] = new Array();
        msgError[i]['champ'] = 'CGV';
        msgError[i]['erreur'] = 'Vous devez acceptez les conditions générales de vente.<br />';
        i++;
        findError = true;
   }

    if (!findError){
      window.scrollTo(0,0);
	   $('form').submit();
    }else{
      var msgAlert = '<span class="close"><img src="/fileadmin/templates/img/fermer.gif" alt="fermer" /></span>';
      for(i=0;i<msgError.length;i++)
      {
        msgAlert = msgAlert+'<span class="idChamp">'+idToName(msgError[i]['champ'])+'</span>'+'<span class="raison">'+msgError[i]['erreur']+'</span>';
      }
      $('#msgError').html(msgAlert);
      $('#msgErrorAll').show();
      $('#msgError').show();
      $('#msgErrorAll').click(function (){
        $('#msgError').hide();
        $(this).hide();
      });
      $('#msgError').click(function (){
        $(this).hide();
        $('#msgErrorAll').hide();
      });
    }
  });
  
  $("#loginBtn").click(function (){
    var findError = false;
    $(".requiered").each( function(){
        if( $(this).val() == ''){
          findError = true;
          $(this).addClass('error');
          $(this).focus( function() { $(this).removeClass('error'); } );
        }
    });

    if (!findError)
      $('form').submit();  
  });
  
  $(".choix").change( function() { 
    var id = ($(this).attr("id")).replace('choix','');
    $('#choix1').attr({disabled:"disabled"});
    $('#choix2').attr({disabled:"disabled"});
    $('#choix3').attr({disabled:"disabled"});
    
    var deleted = new Array();
    switch(id)
    {
      case '1'  :
                deleted[0] = 2;
                deleted[1] = 3;
                break;
      case '2'  :
                deleted[0] = 1;
                deleted[1] = 3;
                break;
      case '3'  :
                deleted[0] = 1;
                deleted[1] = 2;
                break;
    }
    
    var valueSelectOne = document.getElementById('choix1');
    var valueSelectTwo = document.getElementById('choix2');
    var valueSelectThree = document.getElementById('choix3');
    
    for(j=0;j<2;j++){
      var i = 0;
      var listOption = document.getElementById('choix'+deleted[j]);
      var listOptionSave = document.getElementById('choix4');
      var listOptionValueSave = listOption.value;
      while (i < listOption.length){
        listOption.options[i] = null;
        i++;
      }
      
      i=0;
      while (i < listOptionSave.length){
        listOption.options[i] = new Option(listOptionSave[i].text,listOptionSave[i].value);
        i++;
      }
      
      i = 0;
      while (i < listOption.length){
        if (  (listOption.options[i].value != '0') && (listOption.options[i].value ==  document.getElementById('choix'+id).value)  ){
          //alert('if 0 :'+listOption.options[i].value+' = '+document.getElementById('choix'+id).value);
          listOption.options[i] = null;
          i--;
        }
        if ( (id == '1' && deleted[j] == '2') || (id == '2' && deleted[j] == '1')){
          if ( (listOption.options[i]) &&(listOption.options[i].value != '0') && (document.getElementById('choix3').value != '0') && (listOption.options[i].value ==  document.getElementById('choix3').value) ){
            //alert('if 1 :'+listOption.options[i].value+' = '+document.getElementById('choix3').value);
            listOption.options[i] = null;
            i--;
          }
        }
        if ( (id == '1' && deleted[j] == '3') || ( id == '3' && deleted[j] == '1') ){
          if ( (listOption.options[i]) &&(listOption.options[i].value != '0') && (document.getElementById('choix2').value != '0') && (listOption.options[i].value ==  document.getElementById('choix2').value) ){
            //alert('if 2 :'+listOption.options[i].value+' = '+document.getElementById('choix2').value);
            listOption.options[i] = null;
            i--;
          }
        }
        if ( (id == '2' && deleted[j] == '3') || (id == '3' && deleted[j] == '2') ){
          if ( (listOption.options[i]) &&(listOption.options[i].value != '0') && (document.getElementById('choix1').value != '0') && (listOption.options[i].value ==  document.getElementById('choix1').value) ){
            //alert('if 3 :'+listOption.options[i].value+' = '+document.getElementById('choix1').value);
            listOption.options[i] = null;
            i--;
          }
        }
        i++;
      }
      
      i = 0;
      while ( i < listOption.length ){
        //alert(listOption.options[i].value+' : '+listOptionValueSave);
        if ( listOption.options[i].value == listOptionValueSave ){
          listOption.options[i].selected = true;
        }
        i++;
      }
    }
    $('#choix1').attr({disabled:""});
    $('#choix2').attr({disabled:""});
    $('#choix3').attr({disabled:""});
    
  });

  $("#btnTwoP").click(function (){
    parent.window.scrollTo(0,0);
	 $('form#etape2R').submit();
  });

  $("#btnTwo").click(function (){
    var findError = false;
    var msgError = new Array();
    var i = 1;
    var j = 0;
    $(".choix").each( function(){
        
        if( $(this).val() == '0' && i < 4){
          findError = true;
          msgError[j] = new Array();
          msgError[j]['champ'] = $(this).attr('id');
          msgError[j]['erreur'] = 'Ce champ est obligatoire.<br />';
          j++;
          $(this).addClass('error');
          $(this).focus( function() { $(this).removeClass('error'); } );
        }
        i++;
    });
    if (!findError){
      parent.window.scrollTo(0,0);
      $('form#etape2').submit();
    }
    else{
      var msgAlert = '<span class="close"><img src="/fileadmin/templates/img/fermer.gif" alt="fermer" /></span>';
      for(i=0;i<msgError.length;i++)
      {
        msgAlert = msgAlert+'<span class="idChamp">'+idToName(msgError[i]['champ'])+'</span>'+'<span class="raison">'+msgError[i]['erreur']+'</span>';
      }
      $('#msgError').html(msgAlert);
      $('#msgErrorAll').show();
      $('#msgError').show();
      $('#msgErrorAll').click(function (){
        $('#msgError').hide();
        $(this).hide();
      });
      $('#msgError').click(function (){
        $(this).hide();
        $('#msgErrorAll').hide();
      });
    }
  });

  $("#btnTwoA").click(function (){
    var findError = false;
    var msgError = new Array();
    var i = 1;
    var j = 0;
    $(".choix").each( function(){
        if( $(this).val() == '0' && i < 4){
          findError = true;
          msgError[j] = new Array();
          msgError[j]['champ'] = $(this).attr('id');
          msgError[j]['erreur'] = 'Ce champ est obligatoire.<br />';
          j++;
          $(this).addClass('error');
          $(this).focus( function() { $(this).removeClass('error'); } );
        }
        i++;
    });

    $(".requiered").each( function(){
        if( $(this).val() == ''){
          findError = true;
          $(this).addClass('error');
          $(this).focus( function() { $(this).removeClass('error'); $('#msgErrorAll').hide(); } );
          msgError[j] = new Array();
          msgError[j]['champ'] = $(this).attr('id');
          msgError[j]['erreur'] = 'Ce champ est obligatoire.<br />';
          j++;
        }
    });
 
    
    if (!findError){
      $('form#etape2').submit();
    }else{
      var msgAlert = '<span class="close"><img src="/fileadmin/templates/img/fermer.gif" alt="fermer" /></span>';
      for(i=0;i<msgError.length;i++)
      {
        msgAlert = msgAlert+'<span class="idChamp">'+idToName(msgError[i]['champ'])+'</span>'+'<span class="raison">'+msgError[i]['erreur']+'</span>';
      }
      $('#msgError').html(msgAlert);
      $('#msgErrorAll').show();
      $('#msgError').show();
      $('#msgErrorAll').click(function (){
        $('#msgError').hide();
        $(this).hide();
      });
      $('#msgError').click(function (){
        $(this).hide();
        $('#msgErrorAll').hide();
      });
    }
  });

  $("#btnThreeP").click(function (){
    $('form#etape3R').submit();
  });
  
  $('.bonCommande').click(function(){
    if ($(this).val() == '1')
    {
      $('#bonCommande').show();
    }else{
      $('#bonCommande').hide();
    }
  });
    
  $("#btnThree").click(function (){
    var findError = true;
    var msgError = new Array();
    var i = 0;
    msgError[i] = new Array();
    msgError[i]['champ'] = 'paiement';
    msgError[i]['erreur'] = 'Vous devez choisir un type de paiement';
    i++;
          
    $(".paiment").each( function(){
        if( $(this).attr('checked') ){
          findError = false;
        }
    });

    if (!findError){
      parent.window.scrollTo(0,0);
      $('form#etape3').submit();
    }else{
      var msgAlert = '<span class="close"><img src="/fileadmin/templates/img/fermer.gif" alt="fermer" /></span>';
      for(i=0;i<msgError.length;i++)
      {
        msgAlert = msgAlert+'<span class="idChamp">'+idToName(msgError[i]['champ'])+'</span>'+'<span class="raison">'+msgError[i]['erreur']+'</span>';
      }
      $('#msgError').html(msgAlert);
      $('#msgErrorAll').show();
      $('#msgError').show();
      $('#msgErrorAll').click(function (){
        $('#msgError').hide();
        $(this).hide();
      });
      $('#msgError').click(function (){
        $(this).hide();
        $('#msgErrorAll').hide();
      });
    }
  });
  
  $('#userChange').change(function (){
    document.location.href='http://www.assises-risques.com/fileadmin/templates/Insc/index.php?who=admin&where=fiche&ref='+$(this).val();
  });
  
  $('#statut').change(function(){
      if ( $(this).val() == '0' )
        $('#changeStatus').hide();
      if ( $(this).val() == '1' || $(this).val() == '2' )
        $('#changeStatus').show();
  });

  $('#mode_paie').change(function(){
      if ( $(this).val() == '0' )
        $('#changeStatus').hide();
      if ( $(this).val() == '1' || $(this).val() == '2' || $(this).val() == '3'|| $(this).val() == '4'|| $(this).val() == '5' )
        $('#changeStatus').show();
  });

  $('#mailStatus').click( function(){
    $.ajax({
      type: "POST",
      url: "http://www.assises-risques.com/fileadmin/templates/Insc/ajax/mailStatut.php",
      data: "id_insc="+$('#id_insc').val()+'&status='+$('#statusVal').val()+'&mode_paie='+$('#mode_paie').val(),
      success: function(ret){
        switch(ret){
          case 'ok' :
                    if ( $('#statusVal').val() == '1'){
                      $('#statusPaiement').hide('slow');
                      $('#statusPaiement').html('&nbsp;&nbsp;Encaissé&nbsp;&nbsp;<input class="btn" id="mailStatus" type="button" value="Renvoyer le status du paiment" /><input id="statusVal" type="hidden" value="1" />');
                      $('#statusPaiement').show('slow');
                    }else if ($('#statusVal').val() == '2'){
                      $('#statusPaiement').hide('slow');
                      $('#statusPaiement').html('&nbsp;&nbsp;Refusé&nbsp;&nbsp;<input class="btn" id="mailStatus" type="button" value="Renvoyer le status du paiment" /><input id="statusVal" type="hidden" value="2" />');
                      $('#statusPaiement').show('slow');
                    }
                    break;
          default :
                    break;
        }
      }
    });
  });

  $('#changeStatus').click( function(){
    $.ajax({
      type: "POST",
      url: "http://www.assises-risques.com/fileadmin/templates/Insc/ajax/changeStatut.php",
      data: "id_insc="+$('#id_insc').val()+'&status='+$('#statut').val()+'&mode_paie='+$('#mode_paie').val(),
      success: function(ret){
        switch(ret){
          case 'ok' :
                    $('#changeStatus').hide();
                    if ( $('#statut').val() == '1'){
                      $('#statusPaiement').hide('slow');
                      $('#statusPaiement').html('&nbsp;&nbsp;Encaissé&nbsp;&nbsp;<input id="mailStatus" class="btn" type="button" value="Prévenir la personne inscrite" class="" /><input id="statusVal" type="hidden" value="1" />');
                      $('#statusPaiement').show('slow');
                      var paiement = $('.paiement').html();
                      paiement = paiement.replace('(Pf)','');
                      paiement = paiement.replace('FactPf','');
                      $('.paiement').html(paiement);
                    }else if ($('#statut').val() == '2'){
                      $('#statusPaiement').hide('slow');
                      $('#statusPaiement').html('&nbsp;&nbsp;Refusé&nbsp;&nbsp;<input id="mailStatus" class="btn" type="button" value="Prévenir la personne inscrite" class="" /><input id="statusVal" type="hidden" value="2" />');
                      $('#statusPaiement').show('slow');
                    }
                    $.ajax({
                      type: "POST",
                      url: "http://www.assises-risques.com/fileadmin/templates/Insc/ajax/factId.php",
                      data: "id_insc="+$('#id_insc').val(),
                      success: function(ret){
                        switch(ret){
                          default :
                                  $('.facture').html(ret);
                                  break;
                        }
                      }
                    });
                                                          
                    $('#mailStatus').click( function(){
                      $.ajax({
                        type: "POST",
                        url: "http://www.assises-risques.com/fileadmin/templates/Insc/ajax/mailStatut.php",
                        data: "id_insc="+$('#id_insc').val()+'&status='+$('#statusVal').val(),
                        success: function(ret){
                          switch(ret){
                            case 'ok' :
                                      if ( $('#statusVal').val() == '1')
                                      $('#statusPaiement').html('&nbsp;&nbsp;Encaissé&nbsp;&nbsp;<input class="btn" id="mailStatus" type="button" value="Renvoyer le status du paiment" /><input id="statusVal" type="hidden" value="1" />');
                                      else if ($('#statusVal').val() == '2')
                                      $('#statusPaiement').html('&nbsp;&nbsp;Refusé&nbsp;&nbsp;<input class="btn" id="mailStatus" type="button" value="Renvoyer le status du paiment" /><input id="statusVal" type="hidden" value="2" />');
                                      break;
                            default :
                                      break;
                          }
                        }
                      });  
                    });
                    break;
          default :
                    break;
        }
      }
    });  
  });
  
  $('.nom').keyup( function(){
    $('.nom').val($('.nom').val());
  });

  $('.prenom').keyup( function(){
    $('.prenom').val($('.prenom').val());
  });
  
  $('.fonction').keyup( function(){
    $('.fonction').val($('.fonction').val());
  });
  
  $('.organisme').keyup( function(){
    $('.organisme').val($('.organisme').val());
  });

  $('.email').keyup( function(){
    $('.email').val($('.email').val());
  });
  
  $('#logout').click(function(){
    $.ajax({
      type: "POST",
      url: "./ajax/logOut.php",
      success: function(ret){
        switch(ret){
          case 'ok' :
                    document.location.href='http://www.assises-risques.com/fileadmin/templates/Insc/index.php?who=admin';
                    break;
          default :
                    break;
        }
      }
    });     
  });
  
  $('#dlDej').click(function (){
    document.location.href='http://www.assises-risques.com/fileadmin/templates/Insc/stats/listDej.csv';
  });
  
  $('#dlAt').click(function (){
    document.location.href='http://www.assises-risques.com/fileadmin/templates/Insc/stats/listAt.csv';
  });
  
  $('#dlNav').click(function (){
    document.location.href='http://www.assises-risques.com/fileadmin/templates/Insc/stats/listNav.csv';
  });
  
  $('#dlInv').click(function (){
    document.location.href='http://www.assises-risques.com/fileadmin/templates/Insc/stats/listInv.csv';
  });
  
  $('#dlTt').click(function (){
    document.location.href='http://www.assises-risques.com/fileadmin/templates/Insc/stats/listTt.csv';
  });
});

function idToName(id){
  switch(id){
    case  'nom'     :
              return 'Nom';
              break;
    case  'prenom'  :
              return 'Prénom';
              break;
    case  'fonction':
              return 'Fonction';
              break;
    case  'organisme':
              return 'Organisme';
              break;
    case  'adresse' :
              return 'Adresse';
              break;
    case  'cpostal' :
              return 'Code Postal';
              break;
    case  'ville'   :
              return 'Ville';
              break;
    case  'tel'     :
              return 'Tél.';
              break;
    case  'fax'     :
              return 'Fax';
              break;
    case  'email'   :
              return 'E-mail';
              break;
    case  'cemail'   :
              return 'Confirmation E-mail';
              break;
    case  'organisme_bis'   :
              return 'Facturation : Organisme';
              break;
    case  'adresse_bis'   :
              return 'Facturation : Adresse';
              break;
    case  'cpostal_bis'   :
              return 'Facturation : Code Postal';
              break;
    case  'ville_bis'   :
              return 'Facturation : Ville';
              break;
    case  'choix1'   :
              return 'Choix N°1';
              break;
    case  'choix2'   :
              return 'Choix N°2';
              break;
    case  'choix3'   :
              return 'Choix N°3';
              break;
    case  'code'   :
              return 'Code confidentiel';
              break;
    case  'paiement'   :
              return 'Règlement';
              break;
    default:
            return id;
            break;
  }
  
}   
