//Gestion des pubs
var tabPub = new Array();

$(document).ready(function() {
    jQuery.getJSON('http://publicite.editions-lariviere.fr/ecommerce-publicite/publicite-json.php?callback=?',
        {pubs: tabPub},
        function(data) {
            jQuery.each(data, function(k,v) {
                jQuery('#'+k).writeCapture().html(v);
            });
        }
    );
});

function updateCompte() {
	// var 
}
function diseableContrat() {
    $('#cotr_id1').attr('disabled', 'disabled');
    $('#cotr_id2').attr('disabled', 'disabled');
    $('#cotr_id3').attr('disabled', 'disabled');
    $('#cotr_id4').attr('disabled', 'disabled');
    $('#cotr_id5').attr('disabled', 'disabled');
}
function updateEtatCandidat(cv_id,offre_id)
{
	var etat=$('#ofcv_cvet_id').val();
	alert(etat);
	$.ajax({   type: "GET",
        url: "/suivit_candidat_recruteur.php",
        data: "etat="+etat+"&cv_id="+cv_id+"&offre_id="+offre_id,
        cache:false,
        success: function(msg){
            $('#dialog').html("Votre modification a bien été prise en compte");
            openDialogReload();
            
        }
      });
}

function supprimeCandidature(cv_id,id_offre)
{
	$.ajax({   type: "GET",
        url: "/suivit_candidat_recruteur.php",
        data: "cv_id="+cv_id+"&offre_id="+id_offre,
        cache:false,
        success: function(msg){
            $('#dialog').html("Votre modification a bien été prise en compte");
           
            openDialogReload();
            
        }
      });
}

function checkAllRegion(bState)
{
 var f = document.cv_modif;
  var i = 0 ;
  var val = "" ;
  
  while (true)
  {
    eval("val = " + "f.rere_regi_id["+i+"]") ;
    if (val == undefined)
    {
      break ;
    } 
    val.checked = bState ;
    i++ ;
  }  	
}
function supprimer_logo(id)
{
	$.ajax({   type: "GET",
        url: "/supprime_logo.php",
        data: "id="+id,
        cache:false,
        success: function(msg){
            $('#dialog').html(msg);
            $('#offre_'+id).html("");
            openDialogReload();
            
        }
      });
}
function supprimer_cv(id)
{
	$.ajax({   type: "GET",
        url: "/supprimer_cv.php",
        data: "id="+id,
        cache:false,
        success: function(msg){
            $('#dialog').html(msg);
            $('#offre_'+id).html("");
            openDialogReload();
            
        }
      });
}
function verifFormUploadLogo() {
	
    if($('#logo_libelle').val()=="") {
        $('#dialog').html("vous devez donner un nom de logo");
        openDialog();
        return false;
    } else if($('#logo').val()=="") {
        $('#dialog').html("Vous devez choisir un fichier");
        openDialog();
        return false;
    }

    if($('#logo').val()=="" && $('#logo_libelle').val()=="" )
    {
        var valeur=$('#logo').val();
        var erreurFormat=false;
        alert(valeur.indexOf('.jpg'));
        if(!valeur.indexOf('.jpg') ) erreurFormat=true;
        if(!valeur.indexOf('.gif') ) erreurFormat=true;
        if(!valeur.indexOf('.png') ) erreurFormat=true;

        if(erreurFormat)
        {
                $('#dialog').html("Votre fichier doit etre un fichier image au format : jpg, gif ou png.");
                openDialog();
                return false;
        }else
        {
                return true;
        }
    }
}
// permet de desactiver les select box pour le salaire en cas de case a coché : a négocier 
function griser_select_salaire()
{
    if($('#offr_salaireMin').attr('disabled')=='disabled')
    {
        $('#offr_salaireMin').removeAttr('disabled');
        $('#offr_salaireMax').removeAttr('disabled');
    } else {
        $('#offr_salaireMin').attr('disabled',function() {
            return 'disabled';
        }
        );
        $('#offr_salaireMax').attr('disabled',function() {
            return 'disabled';
        }
        );
        $('#salaire_a_negocier').attr('checked');
    }
}
function deleteOffre(id)
{
	$.ajax({   type: "GET",
        url: "/supprimer_offre.php",
        data: "action=open&id="+id,
        cache:false,
        success: function(msg){
            $('#dialog').html(msg);
            $('#offre_'+id).html("");
            openDialog();
            
        }
      });
}
function deleteCandidature(id)
{
	$.ajax({   type: "GET",
        url: "/supprimer_candidature.php",
        data: "action=open&id="+id,
        cache:false,
        success: function(msg){
            $('#dialog').html(msg);
            $('#candidature_'+id).html("");
            openDialog();
        }
      });
}
function openFormForgetPass(param) {
    $.ajax({   type: "GET",
               url: "/forget_pass.php",
               data: "action=open&"+param+"=true",
               cache:false,
               success: function(msg){
                   $('#dialog').html(msg);
                   openDialog();
               }
             });
}
function verifFormatEmail(email)
{
    /*var re=/^[a-z0-9](\.?[a-z0-9_-])*[@]([a-z0-9_-]+\.)+([a-z]{2,4})$/i;
     return re.test(email)?true:false; */
    return true;
}
// permet d'envoyer un email pour le cas de l'oublie de mot de passe 
function sendEmailForgetPass(id_input)
{
    var id='#email';
    var val=$(id).val();
    if(val=="")
    {
        $('#dialog').html('Veuillez saisir un email !');
        openDialog();
    }
    else
    {
        var email='email='+val+'&'+id_input;
        if(verifFormatEmail(val))
        {
        $.ajax({
               type: "GET",
               url: "/forget_pass.php",
               data: email,
               cache:false,
               success: function(msg){
                   $('#dialog').html(msg);
                   openDialog();
               }
             });
        }else
        {
            $('#dialog').html('Votre email n\'as pas le bon format, veuillez le corriger.');
            openDialog();
        }
    }
}
// permet de changer ces identifiants 
function checkFormchangeMdp()
{
    if($('#copa_email').val() == '' || $('#copa_email_confirmation').val()=='' || $('#copa_motDePasse').val()=='' || $('copa_motDePasse_confirmation').val()=='')
    {
        $('#dialog').html('Veuillez saisir tous les champs ');
        openDialog();
    } else {
        $('#changepass').submit();
    }
}
// recupere les fonctions d'un secteur d'activité 
function getFonction(id_secteur) {
    var parametre = "id_sect="+id_secteur;
    $.ajax({   type: "GET",
               url: "/getfonction.php",
               data: parametre,
               cache:false,
               success: function(msg){
                   $("#offr_fonc_id").html(msg);
               }
             });
}
// permet de postuler a une annonce 
function postuler(copa_id,offre_id)
{
    if(copa_id!="") {
        $("#dialog").html("");
        var parametre = "copa_id="+copa_id+"&offre_id="+offre_id;
        $.ajax({
                   type: "GET",
                   url: "/postuler-annonce.php",
                   data: parametre,
                   cache:false,
                   success: function(msg){
                           $("#dialog").html(msg);
                           openDialog();
                   }
                 });
    } else {
        $("#dialog").html("Vous devez etre authentifié sur le site pour pouvoir postuler");
        openDialog();
    }
} 
//  ouvre une popin en mode modal 
function openDialog()
{
    $( "#dialog:ui-dialog" ).dialog( "destroy" );
    $( "#dialog" ).dialog({
        modal: true,
        buttons: {
            Ok: function() {
                $( this ).dialog( "close" );
                $( this ).remove();
                $('#ctn_dialog').html('<div id="dialog"></div>');
            }
        }
    });
}
// ouvre une popup en mode modal et reload la page 
function  openDialogReload()
{
	$( "#dialog:ui-dialog" ).dialog( "destroy" );
    $( "#dialog" ).dialog({
        modal: true,
        buttons: {
            Ok: function() {
                $( this ).dialog( "close" );
                $( this ).remove();
                window.location.reload();
            }
        }
    });
}
function IncDec(champ,augmenter){
    var valeur = parseInt( $('#'+champ).val() );
    
    if(augmenter) {
        $('#'+champ).val( valeur + 1 );
    } else {
        if(valeur > 0) {
            $('#'+champ).val( valeur - 1);
        }
    }
    calculer_tt();
}

function verifier_valeur_entier(champ) {
    var valeur = parseInt( $('#'+champ).val() );

    if( isNaN(valeur) || valeur < 0 ||  parseInt(valeur) != valeur) {
        alert("vous devez saisir un nombre entier !");
        $('#'+champ).val( 0 );
    }
    calculer_tt();
}

$(document).ready(function(){
    $(':input[type=text]',     $('#form_commande')).keyup(function(){ verifier_valeur_entier( $(this).attr('id') ); calculer_tt(); });
    $(':input[type=checkbox]', $('#form_commande')).change(function(){ calculer_tt(); });
    $(':input[type=radio]',    $('#form_commande')).change(function(){ calculer_tt(); });
});

function calculer_tt() {
    var total = 0;
    $(':input', $('#form_commande')).each(
        function(){
            switch( $(this).attr('type') ) {
                case 'text':
                    if( parseInt($(this).val()) > 0 ) {
                        var infos = $(this).attr('name').split('_');
                        total += parseInt(infos[1]) * parseInt($(this).val());
                    }
                break;
                case 'checkbox': 
                    if( $(this).attr('checked') ) {
                        var infos = $(this).attr('name').split('_');
                        total += parseInt(infos[1]);
                    }
                break;
                case 'radio':
                   if( $(this).attr('checked') ) {
                        var infos = $(this).attr('value').split('_');
                        total += parseInt(infos[1]);
                    }
                break;
            }
        }
    );
    $('#valeur_tt').text(total);
    $('#total').val(total);
}

function valider_form() {
    var total = parseInt($('#valeur_tt').text());

    if( total <= 0 || isNaN(total)) {
        alert("Votre commande est vide");
    } else {
        document.form_commande.submit();
    }
}

function valider_paiement(url_paiement) {
    if( $('#condition_generale').attr('checked') ) {
        document.location.href = url_paiement;
    } else {
        alert('Vous devez d`abord valider les conditions g\351n\351rales de vente pour payer votre commande');
    }
}

//------------------------------------------------------------------------------
//                     JS pour bloc vos criteres d'alertes
//------------------------------------------------------------------------------

    function submitReenForm()
    {
        var f = document.formReen;
        document.getElementById('erreur').innerHTML = "";

        liste_checkbox_fonc = document.getElementsByName('refo_fonc_id_values[]');
        //il faut au moins une checkbox de cochée pour continuer

        i=0;
        checkbox_fonc_cochee = false;
        while(i < liste_checkbox_fonc.length && !checkbox_fonc_cochee )
        {
            if(liste_checkbox_fonc[i].checked == true)
            {
                    checkbox_fonc_cochee = true;
            }
            i++;
        }

        if(!checkbox_fonc_cochee)
        {
            document.getElementById('erreur').innerHTML += "Vous devez choisir une fonction.<br />";
            form_valide = false;
        }

        liste_checkbox_regi = document.getElementsByName('regi_id[]');
        //il faut au moins une checkbox de cochée pour continuer

        i=0;
        checkbox_regi_cochee = false;
        while(i < liste_checkbox_regi.length && !checkbox_regi_cochee )
        {
            if(liste_checkbox_regi[i].checked == true) {
                checkbox_regi_cochee = true;
            }
            i++;
        }

        if(!checkbox_regi_cochee) {
            document.getElementById('erreur').innerHTML += "Vous devez choisir une region.<br />";
            form_valide = false;
        }

        //les ocntrats de travail
        liste_checkbox_cotr = document.getElementsByName('cotr_id[]');
        //il faut au moins une checkbox de cochée pour continuer

        i=0;
        checkbox_cotr_cochee = false;
        while(i < liste_checkbox_cotr.length && !checkbox_cotr_cochee )
        {
            if(liste_checkbox_cotr[i].checked == true)
            {
                checkbox_cotr_cochee = true;
            }
            i++;
        }

        if(!checkbox_cotr_cochee) {
            document.getElementById('erreur').innerHTML += "Vous devez choisir au moins un contrat de travail.<br />";
            form_valide = false;
        }

        if ( document.getElementById('erreur').innerHTML == "") {
            f.submit() ;
            return true ;
        } else {
            document.getElementById('erreur').innerHTML += "<br>";
        }

        return true ;
    }

    function alertErrorSpecialCheckbox(fieldObj, fieldId, errorText)
    {
        var i = 0 ;
        var val = "" ;
        while (true)
        {
            eval("val = " + fieldObj + i) ;
            if (val == undefined) {
                break ;
            }
            document.getElementById(fieldId+ i).className = "input_obligatoire";
            i++ ;
        }
	document.getElementById('erreur').innerHTML += errorText ;
	document.getElementById('erreur').innerHTML += "<br>" ;
    }

    function alertError(fieldId, errorText)
    {
      document.getElementById(fieldId).className = "input_obligatoire";
      document.getElementById('erreur').innerHTML += errorText ;
      document.getElementById('erreur').innerHTML += "<br>" ;
    }

    function reportError(fieldId, errorText)
    {
        switch (fieldId)
        {
            case 'reco_cotr_id':
                alertErrorSpecialCheckbox('document.formReen.reco_cotr_id_', 'reco_cotr_id_', errorText) ;
            break ;

            default:
                alertError(fieldId, errorText) ;
            break ;
        }
    }

    function noError(fieldId)
    {
        document.getElementById(fieldId).className = "";
    }


    function checkAllRegion(bState)
    {
        var f = document.formReen;
        var i = 0 ;
        var val = "" ;

        while (true)
        {
            eval("val = " + "f.rere_regi_id_" + i) ;
            if (val == undefined) {
                break ;
            }
            val.checked = bState ;
            i++ ;
        }
    }

    function updateRegionAll()
    {
        var f = document.formReen;
        var i = 0 ;
        var val = "" ;
        var bAllChecked = true ;

        while (true) {
            eval("val = " + "f.rere_regi_id_" + i) ;
            if (val == undefined) {
                break ;
            }
            if (!val.checked) {
                bAllChecked = false ;
                break ;
            }
            i++ ;
        }

        if (bAllChecked) {
            f.rere_regi_id_all.checked = true ;
        } else {
            f.rere_regi_id_all.checked = false ;
        }
    }
//------------------------------------------------------------------------------
//                     FIN JS pour bloc vos criteres d'alertes
//------------------------------------------------------------------------------
