jQuery.fn.log = function (msg) {
  console.log("%s: %o", msg, this);
  return this;
};
var currentArticle;
/* Document ready */
$(document).ready(
  function() {
  $.ifixpng('im/pxl.gif');
  $('img[src$=png], #mediatheque').ifixpng();
  /* Eurelis : 20100205 : On arrete l'erreur generee par minmax pour continuer les autres operations */
  try{$('#content').minmax("min-height");}catch(e){}
  var cookie = readCookie("style");
  var title = cookie ? cookie : "medium-fonts";
  setActiveStyleSheet(title);
  $('#tools-ok').hover(function() {
    $('input', this).attr('src','im/tools/tools-ok-h.gif');
  },
  function() {
    $('input', this).attr('src','im/tools/tools-ok.gif');
  });
  updateCartCount();
  $('#finance').each(function() {
    var pageLang = 'FR';
    if ($('body').is('.en')) {
      pageLang = 'EN';
    }
    $.get("proxy.php?proxy_url=http:%2F%2Fchronocomprod.glmultimedia.com%2Fessilor%2Fbourse%2Fhome%2Easp%3Flang%3D" + pageLang,
      {},
      function(data) {
        data = $(data);
        var htmlDst = $('#finance');
        var xmlSrc = $('Actions', data);
        var tmp = $('Heure', xmlSrc).text().substr(0, 5);
        $('.date', htmlDst).html($('Date', xmlSrc).text() + " | " + tmp + " (GMT+1)");
        setData($('#ticker-essilor', htmlDst), $('Essilor', data), false);
        setData($('#ticker-cac', htmlDst), $('CAC40', data), true);
        function setData(htmlDst, xmlSrc, truncate) {
          var tmpDst, tmpSrc;
          tmpSrc = $('Valeur', xmlSrc).text();
          if (truncate) {
            tmpSrc= tmpSrc.substr(0,5);	// Will break when CAC goes under 1000 or over 9999...
          }
          $('.stock span', htmlDst).html(tmpSrc);
          tmpDst = $('.change', htmlDst);
          tmpSrc = $('Variation', xmlSrc).text();
          if (tmpSrc.substr(0,1) == '-') {
            tmpDst.removeClass("inc").addClass("dec");
          } else {
            tmpDst.removeClass("dec").addClass("inc");
          }
          tmpDst.html(tmpSrc + "%");
        }
        $('#finance').css("visibility", "visible");
      }
    );
  });
  /* contact page client side validation */
  function nonEmptySelect(elem_id) {
    var elem = $('#' + elem_id)[0];
    return elem.options[elem.selectedIndex].value != "";
  }
  $.validator.addMethod("non_empty_geozone", function() {
    return nonEmptySelect('geo_zone');
  });
  $.validator.addMethod("non_empty_object", function() {
    return nonEmptySelect('object');
  });
  $('#contact #contact-form').validate({
    event: "submit",
    errorContainer: $('#error-container'),
    errorPlacement: function(error, element) {
      error.prependTo(element.parent("td"));
    },
    rules: {
      "last_name": "required",
      "first_name": "required",
      "email": { required: true, email: true },
      "geo_zone": "non_empty_geozone",
      "object": "non_empty_object",
      "message": { required: true, maxLength: 500 }
    },
    messages: {
      "last_name": translations[1],
      "first_name": translations[2],
      "email": translations[3],
      "geo_zone": translations[4],
      "object": translations[11],
      "message": translations[5]
    }
  });
  /* contact page client side validation */
  $('#send-friend #contact-form').validate({
    event: "submit",
    errorContainer: $('#error-container'),
    errorPlacement: function(error, element) {
      error.prependTo(element.parent("td"));
    },
    rules: {
      "destination_email": {
        required: true,
        email: true
      },
      "last_name": "required",
      "first_name": "required",
      "destination_email": {
        required: true,
        email: true
      },
      "destination_email_1": "email",
      "destination_email_2": "email",
      "destination_email_3": "email",
      "destination_email_4": "email",
      "email": {
        required: true,
        email: true
      },
      "message": { required: true, maxLength: 500 }
    },
    messages: {
      "destination_email": translations[3],
      "destination_email_1": translations[10],
      "destination_email_2": translations[10],
      "destination_email_3": translations[10],
      "destination_email_4": translations[10],
      "last_name": translations[1],
      "first_name": translations[2],
      "email": translations[3],
      "message": translations[12]
    }
  });
  /* Email Alerts client side validation */
  $('#email-alerts #frm1').validate({
    submitHandler: function(form){
        $.ajax({
          type: "POST",
          url: "?page=alerte_email_edit",
          data: "ajax_check_email=1&txt-email="+$('#txt-email').val(),
          success: function(msg){
            if(msg==0){
              alert(translations[9]);
            }else{
              form.submit();
            }
          }
        });
    },
    event: "submit",
    errorPlacement: function(error, element) {
      $('#txt-email-ajax-response').before(error);
    },
    rules: {
      "txt-email":{
        required: true,
        email:true
      }
    },
    messages: {
      "txt-email" : translations[3]
    }
  });
  $.validator.addMethod("non_empty_profil", function() {
    return nonEmptySelect('profil');
  });
  $('#email-alerts #email-alerts-form').validate({
    submitHandler: function(form){
      $.ajax({
        type: "POST",
        url: document.location.href,
        data: "ajax_check_email=1&cm_sub_address="+$('#cm_sub_address').val(),
        success: function(msg){
          if(msg==1){
            $('#email-ajax-response').text(translations[7]);
            $('#email-ajax-response').removeClass('hideme');
            $('#email-ajax-response').show();
          }else{
            var empty_subscriptions = true;
            for(var i=2;i<=5;i++){
              if($('#actus'+i).attr("checked")){
                empty_subscriptions = false;
              }
            }
            if(!empty_subscriptions){
              form.submit();
            }else{
              $('#subscriptions-error').text(translations[8]);
              $('#subscriptions-error').removeClass('hideme');
              $('#subscriptions-error').show();
            }
          }
        }
      });
    },
    event: "submit",
    errorPlacement: function(error, element) {
      error.prependTo(element.parent("td"));
    },
    rules: {
      "lname": "required",
      "fname": "required",
      "profil": "non_empty_profil",
      "cm_sub_address": {
        required: true,
        email: true
      }
    },
    messages: {
      "lname": translations[1],
      "fname": translations[2],
      "profil": translations[13],
      "cm_sub_address": translations[3]
    }
  });
  $.validator.addMethod("f_month", function() {
    var aMonth = $('#f_month')[0].selectedIndex;
    var aYear = $('#f_year')[0].selectedIndex;
    if ((aYear == 0) & (aMonth > 0)) {
      alert(translations[26]);
      return false;
    }
    return true;
  });
  $.validator.addMethod("t_month", function() {
    var tMonth = $('#t_month')[0].selectedIndex;
    var tYear = $('#t_year')[0].selectedIndex;
    // Month selected, but not year, do error
    if ((tYear == 0) & (tMonth > 0)) {
      alert(translations[26]);
      return false;
    }
    var tMonth = $('#t_month')[0];
    tMonth = parseInt(tMonth.options[tMonth.selectedIndex].value);
    var tYear = $('#t_year')[0];
    tYear = parseInt(tYear.options[tYear.selectedIndex].value);
    var fYear = $('#f_year')[0];
    fYear = parseInt(fYear.options[fYear.selectedIndex].value);
    // End year must be >= start year
    if (tYear < fYear) {
      alert(translations[24]);
      return false;
    }
    if (fYear == tYear) {
      var fMonth = $('#f_month')[0];
      fMonth = parseInt(fMonth.options[fMonth.selectedIndex].value);
      if (fMonth > tMonth) {
        alert(translations[25]);
        return false;
      }
    }
    return true;
  });
  $('#mediatheque-search-form-kwd').validate({
    event: "submit",
    errorContainer: $('#error-container'),
    errorPlacement: function(error,element){
      $('#theme-error').html(error);
    },
    rules: {
      "recherche": {required: true}
    },
    messages: {
      "recherche": translations[28]
    }
  });
  $('#mediatheque-search-form').validate({
    event: "submit",
    errorContainer: $('#error-container'),
    errorPlacement: function(error, element) {
      for(var i=1;i<=12;i++){
        if(element.attr('id') == ("doc_crit"+i)){
          $('#doc_crit_error').html(error);
          return;
        }
      }
      if(element.attr('id') == "lng_fr" || element.attr('id') == "lng_en"){
        $('#langue-error').html(error);
        return;
      }
      error.prependTo(element.parent("td"));
    },
    rules: {
      "f_month": "f_month",
      "t_month": "t_month",
      "lng_fr": {required:	function() { return $('#lng_en:checked').length == 0; }},
      "lng_en": {required:	function() { return $('#lng_fr:checked').length == 0; }},
      "doc_crit1": {required:	checkRequiredCriteria},
      "doc_crit2": {required:	checkRequiredCriteria},
      "doc_crit3": {required:	checkRequiredCriteria},
      "doc_crit4": {required:	checkRequiredCriteria},
      "doc_crit5": {required:	checkRequiredCriteria},
      "doc_crit6": {required:	checkRequiredCriteria},
      "doc_crit7": {required:	checkRequiredCriteria},
      "doc_crit8": {required:	checkRequiredCriteria},
      "doc_crit9": {required:	checkRequiredCriteria},
      "doc_crit10": {required: checkRequiredCriteria},
      "doc_crit11": {required: checkRequiredCriteria},
      "doc_crit12": {required: checkRequiredCriteria}
    },
    messages: {
      "f_month": translations[20],
      "t_month": translations[21],
      "lng_fr": translations[22],
      "lng_en": translations[23],
      "doc_crit1": translations[27],
      "doc_crit2": translations[27],
      "doc_crit3": translations[27],
      "doc_crit4": translations[27],
      "doc_crit5": translations[27],
      "doc_crit6": translations[27],
      "doc_crit7": translations[27],
      "doc_crit8": translations[27],
      "doc_crit9": translations[27],
      "doc_crit10": translations[27],
      "doc_crit11": translations[27],
      "doc_crit12": translations[27]
    }
  });
  $('#email-alerts-edit #email-alerts-form').validate({
  submitHandler: function(form){
      $.ajax({
        type: "POST",
        url: document.location.href,
        data: "ajax_check_email=1&edit_check=1&txt-email="+$('#cm_sub_address').val(),
        success: function(msg){
          if(msg==1){
            $('#email-ajax-response').text(translations[7]);
            $('#email-ajax-response').removeClass('hideme');
            $('#email-ajax-response').show();
          }else{
            var options = {
              success: function(responseText, statusText){
                if(responseText==1){
                  $('#edit-link').show();
                  $('#edit-form-container').hide();
                }
              }
            };
            $('#email-alerts-form').ajaxSubmit(options);
            //form.submit();
          }
        }
      });
    },
    event: "submit",
    errorPlacement: function(error, element) {
      error.appendTo(element.parent("td"));
    },
    rules: {
      "lname": "required",
      "fname": "required",
      "cm_sub_address": {
        required: true,
        email: true
      }
    },
    messages: {
      "lname": translations[1],
      "fname": translations[2],
      "cm_sub_address": translations[3]
    }
  });

  /* save/restore text from email textbox */
/*
  var emailInitText = "";
  $('#col1 #email').focus(function() {
    if (!$(this).is('.user-typed')) {
      $(this).addClass('user-typed');
      emailInitText = $(this).val();
      $(this).val("");
    }
  });
  // On blur, if field empty, rewrite text
  $('#col1 #email').blur(function() {
    if ($(this).val() == "") {
      $(this).removeClass('user-typed');
      $(this).val(emailInitText);
    }
  });
*/
  // Div depliables
  $('div.folding, div.folding-open').each(function() {
    $('.title', $(this)).click(function() {
      $(this)
        .parent()
          .toggleClass('folding')
          .toggleClass('folding-open');
      return false;
    });
  });
  // Icones "add to mediatheque"
  // Because content admins always forget to set it...
  //$('a[href$=pdf]').attr('class','lnk-doc');
  // Don't do in mediatheque results page
  if ($('body.rubrique23, body.rubrique54').length == 0) {
    $('.lnk-doc').each(function() {
      var docId = $(this).attr('title');
      $(this).attr('title', $(this).text());
      $(this).attr('target', '_blank');
      if (isInCart(docId)) {
        $(this).after('<img class="added-doc" src="im/pxl.gif" alt=""/>');
      } else {
        $(this).after('<a class="add-doc" rel="' + docId + '" href="#" title="' + translations[6] + '"><img src="im/pxl.gif" alt="' + translations[6] + '" title="' + translations[6] + '"/></a>');
      }
    });
    $('a.add-doc').click(function() {
      var docId = $(this).attr('rel');
      if (!isInCart(docId)) {
        var notWrittenYet = true;
        for(var i = 0; i < 10; i++) {
          var cookie_name = "essilor_docs_cart[" + i + "]";
          var cook = $.cookie(cookie_name);
          if (cook == null) {
            if (notWrittenYet) {
              // sets new
              $.cookie(cookie_name, docId, { expires: 30, path: '/', domain: 'www.essilor.com' });
              // not link anymore but icon of added document
              $(this).before('<img class="added-doc" src="im/pxl.gif" alt=""/>');
              // rare: html node committing suicide !
              $(this).remove();
              updateCartCount();
              notWrittenYet = false;
            }
          } else {
            // or rewrite old to have same timeout
            $.cookie(cookie_name, cook, { expires: 30, path: '/', domain: 'www.essilor.com' });
          }
        }
      }
      return false;
    });
  }
// Prepare jqModal DHTML containers
  $('#ajax-content').jqm();
  $('#ajax-content .title').click(function() {
    $('#ajax-content').jqmHide();
  });
  $('#show-product-page').jqm();
  $('#show-product-page .title').click(function() {
    $('#show-product-page').jqmHide();
    $('#show-product-page iframe').remove();
  });
  $('a.lnk-prod, a.lnk1-prod').each(function() {
    // Creates full url of page
    $(this).attr("href", "spip.php?article" + $(this).attr("href"));
    $(this).click(function() {
      var me = $(this);
      // Useless in theory, but just in case...
      $('#show-product-page .content iframe').remove();
      $('#show-product-page .content').append('<iframe src="' + me.attr("href") + '"></iframe>');
      $('#show-product-page').jqmShow();
      return false;
    });
  });
  /* Eurelis : 20100104 : Ajouter une nouvelle class css pour faire apparaitre la popin */
  $('a.lnk-popin').each(function() {
    // Creates full url of page
    $(this).attr("href", "spip.php?article" + $(this).attr("href"));
    $(this).click(function() {
      var me = $(this);
      // Useless in theory, but just in case...
      $('#show-product-page .content iframe').remove();
      $('#show-product-page .content').append('<iframe src="' + me.attr("href") + '"></iframe>');
      $('#show-product-page').jqmShow();
      return false;
    });
  });
  /* Eurelis : 20100104 : Ajouter une nouvelle class css pour faire apparaitre la popin */
  $('#show-video-page').jqm();
  $('#show-video-page .title').click(function() {
    $('#show-video-page').jqmHide();
    $('#show-video-page iframe').remove();
  });
  $('a.lnk-video').each(function() {
    // Creates full url of page
    $(this).click(function() {
      var me = $(this);
      // Useless in theory, but just in case...
      $('#show-video-page .content iframe').remove();
      $('#show-video-page .content').append('<iframe src="' + me.attr("href") + '"></iframe>');
      $('#show-video-page').jqmShow();
      return false;
    });
  });
  // Lexique (find at least one block meaning there is a lexique
  $('div.lexique-block').eq(0).each(function() {
    var htmlListCode = '';
    $.each(["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"], function() {
      var bloc = $('div.' + this);
      var htmlCode = '';
      // If block exists
      if (bloc.length > 0) {
        htmlCode = '<a class="lexique-act ' + this + '" href="#n">' + this + '</a>';
      } else {
        // Show letter
        htmlCode = this;
      }
      htmlListCode += '<li'
                    + ((htmlListCode == '')?' class="first"':'')
                    + '>' + htmlCode + '</li>';
    });
    // Insert menu before 1st div
    $('div.lexique-block').eq(0).before('<ul class="lexique">' + htmlListCode + '</ul>');
    $('a.lexique-act').click(function() {
      $('a.lexique-act-on').removeClass('lexique-act-on').addClass('lexique-act');
      $(this).removeClass('lexique-act').addClass('lexique-act-on').blur();
      $('div.lexique-block').css("display", "none");
      $('div.' + $(this).text()).css("display", "block");
      return false;
    });
    // Selects a by default
    $('a.a').click();
    // Plugs the mechanism for inner lexique links made easy
    $('div.lexique-block a[href=#]').click(function() {
      $('a.' + $(this).text().substr(0,1).toLowerCase()).click();
    });
  });
  /* Archive */
  // if one block
  $('div.archives-block').eq(0).each(function() {
    // create dropdown with all dates found
    var selecte = '<select id="date-selector">';
    $('div.archives-block').each(function() {
      var zYear = $(this).find('.sub-title').text();
      $(this).addClass('' + zYear);
      selecte += '<option value="' + zYear + '">' + zYear + '</option>';
    });
    selecte += '</select>';
    // insert ddl in page
    $(this).before(selecte);
    // on change
    $('#date-selector').change(function() {
      // hides all
      $('div.archives-block').css('display', 'none');
      var  idx = $(this)[0].selectedIndex;
      // shows good
      $('.' + $(this)[0].options[idx].value).css('display', 'block');
    });
    // shows first one by default
    $(this).css('display', 'block');
  });
  /* wrong code, not done yet */
  $('body.article18, body.article73').each(function() {
    var htmlListCode = '';
    $.each(["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"], function() {
      var bloc = $('div.' + this);
      var htmlCode = '';

      // If block exists
      if (bloc.length > 0) {
        htmlCode = '<a class="lexique-act ' + this + '" href="#n">' + this + '</a>';
      } else {
        // Show letter
        htmlCode = this;
      }
      htmlListCode += '<li'
                    + ((htmlListCode == '')?' class="first"':'')
                    + '>' + htmlCode + '</li>';
    });
    // Insert menu before 1st div
    $('div.lexique-block').eq(0).before('<ul class="lexique">' + htmlListCode + '</ul>');
    $('a.lexique-act').click(function() {
      $('a.lexique-act-on').removeClass('lexique-act-on').addClass('lexique-act');
      $(this).removeClass('lexique-act').addClass('lexique-act-on').blur();
      $('div.lexique-block').css("display", "none");
      $('div.' + $(this).text()).css("display", "block");
      return false;
    });
    // Selects a by default
    $('a.a').click();
    // Plugs the mechanism for inner lexique links made easy
    $('div.lexique-block a[href=#]').click(function() {
      $('a.' + $(this).text().substr(0,1).toLowerCase()).click();
    });
  });
  function parseXML(xml) {
    var doc;
    //----------------------
    if (window.ActiveXObject) {
      doc = new ActiveXObject("Microsoft.XMLDOM");
      doc.async = "false";
      doc.loadXML(xml);
    } else { // code for Mozilla, Firefox, Opera, etc.
      var parser = new DOMParser();
      doc = parser.parseFromString(xml, "text/xml");
    }
    return $(doc);
  }
  jQuery.fn.extend({
     check: function() {
       return this.each(function() { this.checked = true; });
     },
     uncheck: function() {
       return this.each(function() { this.checked = false; });
     }
   });
   //Mediatheque checkboxes
   $('#doc_crit1').bind("click",function(){
    if(this.checked){
      for(var i=2;i<=4;i++){
        $('#doc_crit'+i).check();
      }
    }
   });
    //Email alerts checkboxes
    $('#actus1').bind("click",function(){
      if(this.checked){
        $('#actus2').check();
        $('#actus3').check();
      }
    });
    $('#actus2, #actus3').change(function() {
      if ($('#actus2:checked, #actus3:checked').length == 0) {
        $('#actus1').uncheck();
      }
    });
  //Mediatheque search by keyword
  $('#media-smbt-keywd').click(function(){
    $('#f_month-kwd').val($('#f_month').val());
    $('#t_month-kwd').val($('#t_month').val());
    $('#f_year-kwd').val($('#f_year').val());
    $('#t_year-kwd').val($('#t_year').val());
    $('#media_format-kwd').val($('#media_format').val());
    $('#mediatheque-search-form-kwd').submit();
    return;
  });
});
//Checking required documents criteria
function checkRequiredCriteria(){
  for(var i=1;i<=12;i++){
    if($('#doc_crit'+i+':checked').length > 0){
      return false;
    }
  }
  return true;
}
/*******************/
/*  Ma Mediatheque */
/*******************/
function makezip(lang){
  /** Binding form to jquery.form */
  var options = {
    success: function(responseText, statusText){
      window.location.href = responseText;
    }
  };
  $('#ma-mediatheque-form').ajaxForm(options);
  $('#ma-mediatheque-form').submit();
}
// Not in jQuery ready, because not executed in iframe, and needed for articles "popup"
function addToFavs() {
  var ver = navigator.appName;
  var num = parseInt(navigator.appVersion);
  if (window.sidebar) { // firefox
    window.sidebar.addPanel($('title', $(document))[0].text, document.location,"");
  } else if ((ver == "Microsoft Internet Explorer")&&(num >= 4)) {
    window.external.AddFavorite(document.location, $('title', $(document))[0].text);
  } else {
    alert(translations[0]);
  }
  $('#ico-fav a').blur();
  return false;
}
function setSmallFonts() { setActiveStyleSheet("small-fonts"); $('#small-fonts a').blur(); return false; }
function setMediumFonts() { setActiveStyleSheet("medium-fonts"); $('#medium-fonts a').blur(); return false; }
function setLargeFonts() { setActiveStyleSheet("large-fonts"); $('#large-fonts a').blur(); return false; }
function sendToFriend(theLang) {
  window.open('spip.php?page=send_friend&lang=' + theLang + '&from_url=' + escape(document.location), 'sendto', "top=150,left=200,width=520,height=680,toolbar=no,menubar=no,location=no,status=no,resizable=1");
  $('#ico-email a').blur();
  return false;
}
var envoyerValidator;
function envoyer(lang){
  /** Binding form to jquery.form */
  var options = {
    success: function(responseText, statusText) {
      $('#ajax-content-inner').html(responseText);
      envoyerValidator = $('#envoyer-form').validate({
        event: "submit",
        errorContainer: $('#error-container'),
        errorPlacement: function(error, element) {
          error.prependTo(element.parent("td"));
        },
        rules: {
          "email_destination": { required: true, email: true },
          "email": { required: true, email: true },
          "message": "required"
        },
        messages: {
          "email_destination": translations[3],
          "email": translations[3],
          "message": translations[5]
        }
      });
      $('#ajax-content').jqmShow();
    }
  };
  $('#ma-mediatheque-form').attr("action","envoyer.php?lang=" + lang);
  $('#ma-mediatheque-form').ajaxForm(options);
  $('#ma-mediatheque-form').submit();
}
function send_emails(){
  /** Binding form to jquery.form */
  var options = {
    success: function(responseText, statusText){
      if(responseText == "1"){
        $('#envoyer-loading').hide();
        $('#envoyer-success').removeClass("hideme").show();
      }else{
        alert(responseText);
      }
    }
  };
  if (envoyerValidator.form()) {
    $('#envoyer-form').ajaxForm(options);
    $('#envoyer-form-container').hide();
    $('#envoyer-loading').removeClass("hideme").show();
    $('#envoyer-form').submit();
  }
}
function supprimer(lang){
  try{
    $('#ma-mediatheque-form').ajaxFormUnbind();
  }catch(ex){
  }
  $('#ma-mediatheque-form').attr("action","?page=ma_mediatheque&lang=" + lang + "&remove=true");
  $('#ma-mediatheque-form').submit();
}
/* Cart count related functions */
function isInCart(docId) {
  var inCart = false;
  for(var i = 0; i < 10; i++) {
    var cook = $.cookie("essilor_docs_cart[" + i + "]");
    inCart |= (cook == docId);
  }
  return inCart;
}
function countInCart() {
  var cartCount = 0;
  for(var i = 0; i < 10; i++) {
    var cook = $.cookie("essilor_docs_cart[" + i + "]");
    if (cook != null)
      cartCount++;
  }
  return cartCount;
}
function updateCartCount() {
  var inCart = countInCart();
  $('#my-media-count').text(inCart);
  if (inCart > 1) $('#my-media-s').text("s");
}
/*******************/
/*Email Alerts Edit*/
/*******************/
function deleteSubscription(id){
  $.ajax({
    type: "POST",
    url: document.location.href,
    data: "ajax_remove_subscription=1&id="+id,
    success: function(msg){
      if(msg==1){
        $('#sbcr_container_'+id).hide();
        $('input:checkbox:disabled[value=' + id + ']').attr("disabled","").attr("checked","");
        // Show add link. If already shown, does nothing
        $('#add-subscriptions-lnk').show();
        $('#add-subscriptions-container').hide();
      }
    }
  });
}
function showEditForm(){
  $('#edit-link').hide();
  $('#edit-form-container').removeClass("hideme").show();
}
function showAddSubscr(){
  //Clear form
  $('#subscriptions-edit-form').clearForm();
  $.ajax({
    type: "POST",
    url: document.location.href,
    data: "ajax_check_subscriptions=1",
    success: function(msg){
      if(msg!=0){
        var result = msg.split(",");
        for(i=0;i<result.length;i++){
          $('#'+result[i]).attr("disabled","disabled");
          $('#'+result[i]).check();
        }
        $('#add-subscriptions-lnk').hide();
        $('#add-subscriptions-container').show();
      }
    }
  });
}
function emailAlertsLogout(){
  $.ajax({
    type:"POST",
    url: document.location.href,
    data: "ajax_logout=1",
    success: function(msg){
      if(msg == 1){
        var pageLang = 'fr';
        if ($('body').is('.en')) {
          pageLang = 'en';
        }
        document.location.href = "?page=alerte_email&lang="+pageLang;
      }
    }
  });
}
function emailAlertsRemoveAccount(){
  $.ajax({
    type:"POST",
    url: document.location.href,
    data: "ajax_remove_account=1",
    success: function(msg){
      if(msg == 1){
        var pageLang = 'fr';
        if ($('body').is('.en')) {
          pageLang = 'en';
        }
        document.location.href = "?page=alerte_email&lang="+pageLang;
      }
    }
  });
}