$(document).ready(function() {
  $("footer>ul>li>a").click(function() {
     u = "http://www.bonzai-life.com/clic/3/9/19/84/168/?v2"
     t = "Grand Jeu VVF Villages - Hiver Gagnant"
     c = "<a href=\"http://www.bonzai-life.com/clic/3/9/19/84/168/?v2\" target=\"_blank\"><img border=\"0\" width=\"357\" height=\"225\" alt=\"Grand Jeu VVF Villages\" src=\"http://vvfvillages-lejeu.com/img/miniature.png\" />Gagnez des appareils &agrave; raclette, des raquettes &agrave; neige et des ch&egrave;ques cadeaux!</a><br/><br/>"
     switch($(this).attr('id'))
     {
        case "facebook" : window.open('https://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436,resizable=1'); break;
        case "google" : window.open('http://www.google.com/bookmarks/mark?op=edit&bkmk='+encodeURIComponent(u)+'&title='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=750,height=520,resizable=1'); break;
        case "twitter" : window.open('http://twitter.com/home?status='+encodeURIComponent(t)+' : '+encodeURIComponent(u),'sharer','toolbar=0,status=0,width=800,height=450,resizable=1'); break;
        case "myspace" : window.open('http://www.myspace.com/Modules/PostTo/Pages/?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t)+'&c='+encodeURIComponent(c),'sharer','toolbar=0,status=0,width=626,height=436,resizable=1'); break;
     }
     return false;
  });
  $("a#reglement").colorbox({
    iframe:true,
    width:"80%",
    height:"80%",
    onOpen: function() {
      $("#flash_home").css('visibility', 'hidden');
    },
    onClosed: function() {
      $("#flash_home").css('visibility', 'visible');
    }
  });
  $("#inscription").submit(function() {
    if ($(".bloc_civ input:checked").length == 0) {
      $("<div align=\"center\">Merci de préciser votre civilité.</div>").dialog({open: function() { $("#flash_home").css('visibility', 'hidden'); }, close: function() { $("#flash_home").css('visibility', 'visible'); }, modal:true, resizable:false, title: 'Erreur', buttons: { "Ok": function() { $(this).dialog("close"); $(this).dialog("destroy"); $(this).remove(); }}});
      return false;
    }
    if ($("#nom").val() == "") {
      $("<div align=\"center\">Merci de préciser votre nom.</div>").dialog({open: function() { $("#flash_home").css('visibility', 'hidden'); }, close: function() { $("#flash_home").css('visibility', 'visible'); }, modal:true, resizable:false, title: 'Erreur', buttons: { "Ok": function() { $(this).dialog("close"); $("#nom").focus(); $(this).dialog("destroy"); $(this).remove(); }}});
      return false;
    }
    if ($("#prenom").val() == "") {
      $("<div align=\"center\">Merci de préciser votre prénom.</div>").dialog({open: function() { $("#flash_home").css('visibility', 'hidden'); }, close: function() { $("#flash_home").css('visibility', 'visible'); }, modal:true, resizable:false, title: 'Erreur', buttons: { "Ok": function() { $(this).dialog("close"); $("#prenom").focus(); $(this).dialog("destroy"); $(this).remove(); }}});
      return false;
    }
    if ($("#email").val() == "") {
      $("<div align=\"center\">Merci de préciser votre adresse email.</div>").dialog({open: function() { $("#flash_home").css('visibility', 'hidden'); }, close: function() { $("#flash_home").css('visibility', 'visible'); }, modal:true, resizable:false, title: 'Erreur', buttons: { "Ok": function() { $(this).dialog("close"); $("#email").focus(); $(this).dialog("destroy"); $(this).remove(); }}});
      return false;
    }
    if (check_email($("#email").val()) == false)
    {
      $("<div align=\"center\">Merci de saisir une adresse email valide.</div>").dialog({open: function() { $("#flash_home").css('visibility', 'hidden'); }, close: function() { $("#flash_home").css('visibility', 'visible'); }, modal:true, resizable:false, title: 'Erreur', buttons: { "Ok": function() { $(this).dialog("close"); $("#email").focus(); $(this).dialog("destroy"); $(this).remove(); }}});
      return false;
    }
    if ($("#cp").val() == "")
    {
      $("<div align=\"center\">Merci de préciser votre code postal.</div>").dialog({open: function() { $("#flash_home").css('visibility', 'hidden'); }, close: function() { $("#flash_home").css('visibility', 'visible'); }, modal:true, resizable:false, title: 'Erreur', buttons: { "Ok": function() { $(this).dialog("close"); $("#cp").focus(); $(this).dialog("destroy"); $(this).remove(); }}});
      return false;
    }
    var reg = /^[0-9]{5}$/
    if (reg.exec($("#cp").val()) == null)
    {
      $("<div align=\"center\">Merci de préciser votre code postal à 5 chiffres et sans espaces.</div>").dialog({open: function() { $("#flash_home").css('visibility', 'hidden'); }, close: function() { $("#flash_home").css('visibility', 'visible'); }, modal:true, resizable:false, title: 'Erreur', buttons: { "Ok": function() { $(this).dialog("close"); $("#cp").focus(); $(this).dialog("destroy"); $(this).remove(); }}});
      return false;
    }
    if ($("#ville").val() == "")
    {
      $("<div align=\"center\">Merci de préciser votre ville.</div>").dialog({open: function() { $("#flash_home").css('visibility', 'hidden'); }, close: function() { $("#flash_home").css('visibility', 'visible'); }, modal:true, resizable:false, title: 'Erreur', buttons: { "Ok": function() { $(this).dialog("close"); $("#ville").focus(); $(this).dialog("destroy"); $(this).remove(); }}});
      return false;
    }
    if ($("#jour").val() == "" || $("#mois").val() == "" || $("#annee").val() == "" )
    {
      $("<div align=\"center\">Merci de préciser votre date de naissance.</div>").dialog({open: function() { $("#flash_home").css('visibility', 'hidden'); }, close: function() { $("#flash_home").css('visibility', 'visible'); }, modal:true, resizable:false, title: 'Erreur', buttons: { "Ok": function() { $(this).dialog("close"); $(this).dialog("destroy"); $(this).remove(); }}});
      return false;
    }
    if (date_valide($("#jour").val(), $("#mois").val(), $("#annee").val()) == false)
    {
      $("<div align=\"center\">Merci de préciser une date de naissance valide.</div>").dialog({open: function() { $("#flash_home").css('visibility', 'hidden'); }, close: function() { $("#flash_home").css('visibility', 'visible'); }, modal:true, resizable:false, title: 'Erreur', buttons: { "Ok": function() { $(this).dialog("close"); $(this).dialog("destroy"); $(this).remove(); }}});
      return false;
    }
    if ($("#optin").val() == "")
    {
      $("<div align=\"center\">Merci de préciser si vous souhaitez recevoir des offres de VVF Villages.</div>").dialog({open: function() { $("#flash_home").css('visibility', 'hidden'); }, close: function() { $("#flash_home").css('visibility', 'visible'); }, modal:true, resizable:false, title: 'Erreur', buttons: { "Ok": function() { $(this).dialog("close"); $(this).dialog("destroy"); $(this).remove(); }}});
      return false;
    }
    return true;
  });
  $("#viralajax").click(function() {
    $('#dialog-viral').dialog({
      modal: true,
      open: function()
      {
        $(this).load('viralajax/form.php');
      },
      height: 400,
      width: 800,
      title: 'Inviter mes amis'
    });
  });
  $("#bt_envoyez").click(function() {
    $("#mode").val('yes');
    return true;
  });
  $("#viral").submit(function() {
    if ($("#mode").val() == "no")
      return true;
    if ($("#filleul_1").val() == "")
    {
      $("<div align=\"center\">Pour valider, vous devez saisir au moins une adresse email.</div>").dialog({modal:true, resizable:false, title: 'Erreur', buttons: { "Ok": function() { $(this).dialog("close"); $("#filleul_1").focus(); $(this).dialog("destroy"); $(this).remove(); }}});
      return false;
    }
    for (var i = 1; i <= 5; i++) {
      if ($("#filleul_" + i).val() != "" && check_email($("#filleul_" + i).val()) == false)
      {
        $("<div align=\"center\">Merci de saisir une adresse email valide dans le champ Email " + i + ".</div>").dialog({modal:true, resizable:false, title: 'Erreur', buttons: { "Ok": function() { $(this).dialog("close"); $("#filleul_" + i).focus(); $(this).dialog("destroy"); $(this).remove(); }}});
        return false;
      }
    }
    for (var i = 1; i <= 4; i++)
    {
      var j = i+1;
      if (($("#filleul_" + i).val() != "") && ($("#filleul_" + j).val() != ""))
      {
        if ($("#filleul_" + i).val() == $("#filleul_" + j).val())
        {
          $("<div align=\"center\">Merci de mettre des adresses email différentes les unes des autres.</div>").dialog({modal:true, resizable:false, title: 'Erreur', buttons: { "Ok": function() { $(this).dialog("close"); $(this).dialog("destroy"); $(this).remove(); }}});
          return false;
        }
      }
    }

    for (var i = 1; i <= 3; i++)
    {
      var j = i+2;
      if (($("#filleul_" + i).val() != "") && ($("#filleul_" + j).val() != ""))
      {
        if ($("#filleul_" + i).val() == $("#filleul_" + j).val())
        {
          $("<div align=\"center\">Merci de mettre des adresses email différentes les unes des autres.</div>").dialog({modal:true, resizable:false, title: 'Erreur', buttons: { "Ok": function() { $(this).dialog("close"); $(this).dialog("destroy"); $(this).remove(); }}});
          return false;
        }
      }
    }

    for (var i = 1; i <= 2; i++)
    {
      var j = i+3;
      if (($("#filleul_" + i).val() != "") && ($("#filleul_" + j).val() != ""))
      {
        if ($("#filleul_" + i).val() == $("#filleul_" + j).val())
        {
          $("<div align=\"center\">Merci de mettre des adresses email différentes les unes des autres.</div>").dialog({modal:true, resizable:false, title: 'Erreur', buttons: { "Ok": function() { $(this).dialog("close"); $(this).dialog("destroy"); $(this).remove(); }}});
          return false;
        }
      }
    }

    for (var i = 1; i <= 1; i++)
    {
      var j = i+4;
      if (($("#filleul_" + i).val() != "") && ($("#filleul_" + j).val() != ""))
      {
        if ($("#filleul_" + i).val() == $("#filleul_" + j).val())
        {
          $("<div align=\"center\">Merci de mettre des adresses email différentes les unes des autres.</div>").dialog({modal:true, resizable:false, title: 'Erreur', buttons: { "Ok": function() { $(this).dialog("close"); $(this).dialog("destroy"); $(this).remove(); }}});
          return false;
        }
      }
    }

    for (var i = 1; i <= 5; i++)
    {
      if ($("#filleul_" + i).val() != "")
      {
        if ($("#filleul_" + i).val() == $("#parrain").val())
        {
          $("<div align=\"center\">Vous ne pouvez pas vous parrainer vous même.</div>").dialog({modal:true, resizable:false, title: 'Erreur', buttons: { "Ok": function() { $(this).dialog("close"); $(this).dialog("destroy"); $(this).remove(); }}});
          return false;
        }
      }
    }
    return true;
  });
});

function check_email(email)
{
  var place = email.indexOf("@",1);
  var point = email.indexOf(".", place + 2);
  if ((place > -1)&&(email.length > 2)&&(point > 1))
  {
    var reg = /^[a-zA-Z0-9\._-]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
    return(reg.exec(email) != null);
  } else {
    return false;
  }
}

function date_valide(x,y,z)
{
  if (x <= nbjours_mois(y,z))
    return(true);
  else
    return(false);
}

function nbjours_mois(mois,annee)
{
  if ((mois == 1) ||(mois == 3) || (mois == 5) ||(mois == 7) || (mois == 8) ||(mois == 10) || (mois == 12))
    return(31);
  if ((mois == 4) ||(mois == 6) ||(mois == 9) ||(mois == 11))
    return(30);
  if ((mois == 2) && (annee % 4 == 0))
    return(29);
  return(28);
}
