﻿// Class Commun
// Gestion des fonctions communs a l'application

// Function GetAbsoluteLeft
function getAbsoluteLeft(oElement)
{
  var iReturnValue = 0;
  while (oElement != null)
  {
    iReturnValue += oElement.offsetLeft;
    oElement = oElement.offsetParent;
  }
  return iReturnValue;
}
// Function GetAbsoluteTop
function getAbsoluteTop(oElement)
{
  var iReturnValue = 0;
  while (oElement != null)
  {
    iReturnValue += oElement.offsetTop;
    oElement = oElement.offsetParent;
  }
  return iReturnValue;
}
// Function getRealHeight
function getRealHeight(oElement)
{
  var iReturnValue = 0;

  iReturnValue += oElement.offsetHeight;

  return iReturnValue;
}
/*
Fonction : getDocHeight
Résumé : Donne la hauteur de la page (document)
*/
function getDocHeight()
{
  var D = document;
  return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}
// function qui permet d'attacher un evenement à un objet
function Add_Event(obj_, event_, func_, mode_)
{
  if (obj_.addEventListener)
  {
    obj_.addEventListener(event_, func_, mode_ ? mode_ : false);
  }
  else
  {
    obj_.attachEvent('on' + event_, func_);
  }
}
// Fonction MouseCoords (Retourne les coordonnées X,Y de la souris)
function mouseCoords(ev)
{
  if (ev.pageX || ev.pageY)
  {
    return { x: ev.pageX, y: ev.pageY };
  }
  return {
    x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,
    y: ev.clientY + document.body.scrollTop - document.body.clientTop
  };
}
function GetClientWidth()
{
  return f_filterResults(window.innerWidth ? window.innerWidth : 0, document.documentElement ? document.documentElement.clientWidth : 0, document.body ? document.body.clientWidth : 0);
}
function GetClientHeight()
{
  return f_filterResults(window.innerHeight ? window.innerHeight : 0, document.documentElement ? document.documentElement.clientHeight : 0, document.body ? document.body.clientHeight : 0);
}
function GetScrollLeft()
{
  return f_filterResults(window.pageXOffset ? window.pageXOffset : 0, document.documentElement ? document.documentElement.scrollLeft : 0, document.body ? document.body.scrollLeft : 0);
}
function GetScrollTop()
{
  return f_filterResults(window.pageYOffset ? window.pageYOffset : 0, document.documentElement ? document.documentElement.scrollTop : 0, document.body ? document.body.scrollTop : 0);
}
function f_filterResults(n_win, n_docel, n_body)
{
  var n_result = n_win ? n_win : 0;
  if (n_docel && (!n_result || (n_result > n_docel)))
  {
    n_result = n_docel;
  }
  return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
function isNumeric(sText)
{
  var ValidChars = "0123456789";
  var IsNumber = true;
  var Char;
  for (i = 0; i < sText.length && IsNumber == true; i++)
  {
    Char = sText.charAt(i);
    if (ValidChars.indexOf(Char) == -1)
    {
      IsNumber = false;
    }
  }
  return IsNumber;
}
//recherche les urls de la page
function getimages(n, tab)
{
  if (n.nodeType == 1)
  {
    var Url = "";
    if (n.getAttribute("src"))
    {
      Url = NetoyUrl(n.getAttribute("src"));
    }
    if (n.getAttribute("background"))
    {
      Url = NetoyUrl(n.getAttribute("background"));
    }

    if (document.all)
    {
      try
      {
        if (n.currentStyle.backgroundImage != "none")
        {
          Url = NetoyUrl(n.currentStyle.backgroundImage);
        }
      }
      catch (ex)
      {
      }
    }
    else if (document.defaultView.getComputedStyle(n, '').getPropertyValue("background-image") != "none")
    {
      Url = NetoyUrl(document.defaultView.getComputedStyle(n, '').getPropertyValue("background-image"));
    }
    if (Url != "")
    {
      tab.push(Url);
    }
  }

  var children = n.childNodes;
  for (var i = 0; i < children.length; i++)
  {
    getimages(children[i], tab);
  }
}
//netoy les js ou autre et enleve les "url("
function NetoyUrl(Url)
{
  var url = Url.toLowerCase();
  if (url.indexOf(".jpg") > -1 || url.indexOf(".jpeg") > -1 || url.indexOf(".gif") > -1 || url.indexOf(".png") > -1)
  {
    if (url.substr(url.length - 2, 2) == '")')
    {
      url = url.substr(0, url.length - 2);
    }
    if (url.substr(0, 5) == 'url("')
    {
      url = url.substr(5);
    }
  }
  else
  {
    url = "";
  }
  return url;
}

var Url = {

  // public method for url encoding
  encode: function(string)
  {
    return escape(this._utf8_encode(string));
  },

  // public method for url decoding
  decode: function(string)
  {
    return this._utf8_decode(unescape(string));
  },

  // private method for UTF-8 encoding
  _utf8_encode: function(string)
  {
    string = string.replace(/\r\n/g, "\n");
    var utftext = "";

    for (var n = 0; n < string.length; n++)
    {

      var c = string.charCodeAt(n);

      if (c < 128)
      {
        utftext += String.fromCharCode(c);
      }
      else if ((c > 127) && (c < 2048))
      {
        utftext += String.fromCharCode((c >> 6) | 192);
        utftext += String.fromCharCode((c & 63) | 128);
      }
      else
      {
        utftext += String.fromCharCode((c >> 12) | 224);
        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
        utftext += String.fromCharCode((c & 63) | 128);
      }

    }

    return utftext;
  },

  // private method for UTF-8 decoding
  _utf8_decode: function(utftext)
  {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;

    while (i < utftext.length)
    {

      c = utftext.charCodeAt(i);

      if (c < 128)
      {
        string += String.fromCharCode(c);
        i++;
      }
      else if ((c > 191) && (c < 224))
      {
        c2 = utftext.charCodeAt(i + 1);
        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
        i += 2;
      }
      else
      {
        c2 = utftext.charCodeAt(i + 1);
        c3 = utftext.charCodeAt(i + 2);
        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
        i += 3;
      }

    }

    return string;
  }
};