function $SA (Noeud, Att, Val) {Noeud.setAttribute(Att, Val);}

function Modif (IDJour)
{
  var Current = document.getElementById(IDJour).className;
  if (Current == '')
    Current = 'Reserved';
  else
    Current = '';
  if (document.all)
    document.getElementById(IDJour).style.setAttribute('class', Current);
  else
    document.getElementById(IDJour).setAttribute('class', Current);
  var Href = document.getElementById("MaJCalendrier").href + ';' + IDJour;
  document.getElementById("MaJCalendrier").href = Href;
} // Modif ()

function Uploader()
{
  $('#Upload').uploadify({
    'uploader'   : 'Include/Upload/uploadify.swf',
    'script'     : 'Include/Upload/uploadify.php',
    'folder'     : 'Img/' + document.getElementById('Categorie').value + '/Upload',
    'cancelImg'  : 'Include/Upload/cancel.png',
    'queueID'    : 'fileQueue',
    'fileDesc'   : 'JPEG (*.jpg; *.jpeg)',
    'fileExt'    : '*.jpg;*.jpeg',
    'sizeLimit'  : '8388608',
    'buttonText' : 'CHOISIR',
    'auto'       : true
  });
} // Uploader ()

function ChangeCategorie ()
{
  var element = document.getElementById("Uploader");
  while (element.firstChild) {
    element.removeChild(element.firstChild);
  } // while ()
  var input = document.createElement("input");
  input.setAttribute("type", "file");
  input.setAttribute("name", "uploadify");
  input.setAttribute("id", "Upload");
  element.appendChild(input);
  Uploader();
} // Changecategorie ()

function ColorPicker (Elem1, Elem2)
{
  SuppColorPicker ();
  if (document.all)
    document.getElementById('Supp').style.setAttribute('cssText', '');
  else
    $SA (document.getElementById('Supp'), 'style', '');
  var Color = document.getElementById(Elem2).value;
  $('#colorpickerHolder').ColorPicker({flat: true, color: '#' + Color, onSubmit: function (hsb, hex, rgb, el) {ChangeColor(hsb, hex, rgb, el, Elem1, Elem2);}});
} // ColorPicker ()

function ChangeColor (hsb, hex, rgb, el, Elem1, Elem2)
{
  document.getElementById(Elem2).value = hex;
  if (document.all)
    document.getElementById(Elem1).style.setAttribute('cssText', 'background-color: #' + hex + ';');
  else
    $SA (document.getElementById(Elem1), 'style', 'background-color: #' + hex + ';');
  SuppColorPicker ();
  MaJColor ();
} // ChangeColor

function SuppColorPicker ()
{
  var element = document.getElementById("ColorPicker");
  while (element.firstChild) {
    element.removeChild(element.firstChild);
  } // while ()
  var p = document.createElement("p");
  p.setAttribute("id", "colorpickerHolder");
  element.appendChild(p);
  if (document.all)
    document.getElementById('Supp').style.setAttribute('cssText', 'display: none;');
  else
    $SA (document.getElementById('Supp'), 'style', 'display: none;');
} // SuppColorPicker ()

function MaJColor ()
{
  var Href  = 'Admin.php?Color1=' + document.getElementById('Color1').value + '&Color2=' + document.getElementById('Color2').value;
  Href += '&Color3=' + document.getElementById('Color3').value + '&Color4=' + document.getElementById('Color4').value;
  Href += '&Color5=' + document.getElementById('Color5').value + '&Color6=' + document.getElementById('Color6').value + '#Div5';
  var element = document.getElementById("MaJColor");
  while (element.firstChild) {
    element.removeChild(element.firstChild);
  } // while ()
  var a = document.createElement("a");
  a.setAttribute("href", Href);
  var txt = document.createTextNode("Mettre à jour les couleurs");
  a.appendChild(txt);
  element.appendChild(a);
} // MaJColor ()

function MouseOver ()
{
  document.body.style.cursor="pointer";
} // MouseOver ()

function MouseOut ()
{
  document.body.style.cursor="default";
} // MouseOut ()

function ChangePage (Id, Total)
{
  if (Total > 10)
  {
    var Temp = Math.floor(Id / 10);
    for (var i = 0; i < Math.ceil(Total / 10); i++)
    {
      if (i != Temp)
      {
        if (document.all)
        {
          document.getElementById('PageAffi' + i).style.setAttribute('cssText', 'display: none;');
          document.getElementById('LienPageLivre' + i).style.setAttribute('cssText', 'color: #777777;');
        } // if ()
        else
        {
          $SA (document.getElementById('PageAffi' + i), 'style', 'display: none;');
          $SA (document.getElementById('LienPageLivre' + i), 'style', 'color: #777777;');
        } // else
      } // if ()
      else
      {
        if (document.all)
        {
          document.getElementById('PageAffi' + i).style.setAttribute('cssText', '');
          document.getElementById('LienPageLivre' + i).style.setAttribute('cssText', 'font-weight: bold;');
        } // if ()
        else
        {
          $SA (document.getElementById('PageAffi' + i), 'style', '');
          $SA (document.getElementById('LienPageLivre' + i), 'style', 'font-weight: bold;');
        } // else
      } // else
    } // for ()
  } // if ()

  for (var i = 0; i < Total; i++)
  {
    if (i != Id)
    {
      if (document.all)
        {
          document.getElementById('PageLivre' + i).style.setAttribute('cssText', 'display: none;');
          document.getElementById('LienPageLivre' + i).style.setAttribute('cssText', 'color: #777777;');
        } // if ()
        else
        {
          $SA (document.getElementById('PageLivre' + i), 'style', 'display: none;');
          $SA (document.getElementById('LienPageLivre' + i), 'style', 'color: #777777;');
        } // else
      } // if ()
      else
      {
        if (document.all)
        {
          document.getElementById('PageLivre' + i).style.setAttribute('cssText', '');
          document.getElementById('LienPageLivre' + i).style.setAttribute('cssText', 'font-weight: bold;');
        } // if ()
        else
        {
          $SA (document.getElementById('PageLivre' + i), 'style', '');
          $SA (document.getElementById('LienPageLivre' + i), 'style', 'font-weight: bold;');
        } // else
      } // else
  } // for ()
} // ChangePage ()

function NewsLivreDOr ()
{
  A = ((document.getElementById('IsNews').checked)?'GestionNouvelle':'GestionLivreDOr');
  B = ((document.getElementById('IsNews').checked)?'GestionLivreDOr':'GestionNouvelle');

  if (document.all)
  {
    document.getElementById(A).style.setAttribute('cssText', '');
    document.getElementById(B).style.setAttribute('cssText', 'display: none;');
  } // if ()
  else
  {
    $SA (document.getElementById(A), 'style', '');
    $SA (document.getElementById(B), 'style', 'display: none;');
  } // else
} // NewsLivreDOr ()

function array_unique (array)
{
  var p, i, j, tmp_arr = array;
  for (i = tmp_arr.length; i;)
  {
    for (p = --i; p > 0;)
    {
      if (tmp_arr[i] === tmp_arr[--p])
      {
        for(j = p; --p && tmp_arr[i] === tmp_arr[p];);
        i -= tmp_arr.splice(p + 1, j - p).length;
      } // if ()
    } // for ()
  } // for ()
  return tmp_arr;
} // array_unique ()

function CheckPwd(Form, Nom1)
{
  var Pwd = document.forms[Form][Nom1].value;
  var Strength = 0;
  var Pwd_length = Pwd.length;

  if (Pwd_length < 4)
  {
    document.getElementById('Force').innerHTML = "Force du mot de passe";
    if (document.all)
      document.getElementById('Force').style.setAttribute('cssText', 'color: #FF0000;');
    else
      $SA (document.getElementById('Force'), 'style', 'color: #FF0000;');
    return;
  } // if ()
  else
    Strength = Pwd_length * 4;

  for (var i = 2; i <= 4; i++)
  {
    var Temp = Pwd.split(i);
    Strength -= (Math.ceil(Pwd_length / i) - array_unique(Temp).length);
  } // for ()

  var RegNum = new RegExp('[0-9]','g');
  var Numbers = RegNum.exec(Pwd);

  if (Numbers != null)
  {
    Numbers = Numbers[0].length;
    if (Numbers >= 3) Strength += 5;
  } // if ()
  else
    Numbers = 0;

  var RegSy = new RegExp('[\\\|!@#\$%&\*\/=\?,;\.:\-_\+~\^¨]','g');
  var Symbols = RegSy.exec(Pwd);

  if (Symbols != null)
  {
    Symbols = Symbols[0].length;
    if (Symbols >= 2) Strength += 5;
  } // if ()
  else
    Symbols = 0;

  var RegLc = new RegExp('[a-z]','g');
  var LowerCase = RegLc.exec(Pwd);
  var RegUc = new RegExp('[A-Z]','g');
  var UpperCase = RegUc.exec(Pwd);

  if (LowerCase != null)
    LowerCase = LowerCase[0].length;
  else
    LowerCase = 0;

  if (UpperCase != null)
    UpperCase = UpperCase[0].length;
  else
    UpperCase = 0;

  if ((LowerCase > 0) && (UpperCase > 0)) Strength += 10;

  var Char = LowerCase + UpperCase;

  if ((Numbers > 0)  && (Symbols > 0))  Strength += 15;
  if ((Numbers > 0)  && (Char > 0))     Strength += 15;
  if ((Symbols > 0)  && (Char > 0))     Strength += 15;
  if ((Numbers == 0) && (Symbols == 0)) Strength -= 10;
  if ((Symbols == 0) && (Char == 0))    Strength -= 10;
  if (Strength < 0)   Strength = 0;
  if (Strength > 100) Strength = 100;
  var Color = ['FF0000', 'CC0066', 'FF6600', '33CC00', '33FF00'];
  var Txt = ['Très faible', 'Faible', 'Moyen', 'Fort', 'Très fort'];
  var Niveau = 0;
  if (Strength < 20) Niveau = 0;
  else if (Strength < 45) Niveau = 1;
  else if (Strength < 68) Niveau = 2;
  else if (Strength < 88) Niveau = 3;
  else Niveau = 4;
  document.getElementById('Force').innerHTML = Txt[Niveau] + ' (' + Strength + ' %)';
  if (document.all)
    document.getElementById('Force').style.setAttribute('cssText', 'color: #'+Color[Niveau]+';');
  else
    $SA (document.getElementById('Force'), 'style', 'color: #'+Color[Niveau]+';');
} // CheckPwd ()

function SamePwd (Form, Nom1, Nom2)
{
  var Pwd1 = document.forms[Form][Nom1].value;
  var Pwd2 = document.forms[Form][Nom2].value;
  if (Pwd1 != '' && Pwd2 != '')
  {
    if (Pwd1 == Pwd2 && Pwd1.length >= 4)
    {
      document.getElementById('SameMdP').innerHTML = "Les mots de passe correspondent";
      if (document.all)
        document.getElementById('SameMdP').style.setAttribute('cssText', 'color: #33FF00;');
      else
        $SA (document.getElementById('SameMdP'), 'style', 'color: #33FF00;');
      document.forms[Form]["ModifMdp"].disabled = false;
    } // if ()
    else
    {
      document.getElementById('SameMdP').innerHTML = "Les mots de passe ne correspondent pas";
      if (document.all)
        document.getElementById('SameMdP').style.setAttribute('cssText', 'color: #FF0000;');
      else
        $SA (document.getElementById('SameMdP'), 'style', 'color: #FF0000;');
      document.forms[Form]["ModifMdp"].disabled = true;
    } // else
  } // if ()
  else
  {
    document.getElementById('SameMdP').innerHTML = "Vérification de la concordance des mots de passe";
    if (document.all)
      document.getElementById('SameMdP').style.setAttribute('cssText', 'color: #0000FF;');
    else
      $SA (document.getElementById('SameMdP'), 'style', 'color: #0000FF;');
    document.forms[Form]["ModifMdp"].disabled = true;
  } // else
  CheckPwd (Form, Nom1);
} // SamePwd ()

function ModifSupp ()
{
  var CurrentN = document.getElementById('ListeNews').value;
  var CurrentL = document.getElementById('ListeLivre').value;
  document.getElementById('SuppNews').value = CurrentN;
  document.getElementById('SuppLivre').value = CurrentL;
  if (CurrentN == '-1')
  {
    document.getElementById('SubmitModif').disabled = 'disabled';
    document.getElementById('BouttonSuppNews').disabled = 'disabled';
  } // if ()
  else
  {
    document.getElementById('SubmitModif').disabled = '';
    document.getElementById('BouttonSuppNews').disabled = '';
  } // else

  if (CurrentL == '-1')
  {
    document.getElementById('SubmitModifListe').disabled = 'disabled';
    document.getElementById('BouttonSuppListe').disabled = 'disabled';
  } // if ()
  else
  {
    document.getElementById('SubmitModifListe').disabled = '';
    document.getElementById('BouttonSuppListe').disabled = '';
  } // else
} // ModifSupp ()
