Subversion Repositories wimsdev

Rev

Rev 3425 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*************************************************************************************************************/
  2. /* FONCTIONS UTILS                                                                                                                                                            */
  3. /*************************************************************************************************************/
  4.  
  5. /* GLOBAL VARS ******************************************************************************************************/
  6.  
  7. var tempVar= false;
  8.  
  9.  
  10. /* EVENTS ******************************************************************************************************/
  11.  
  12.     function addEvent(obj, type, fn) {
  13.         if (obj.addEventListener) {
  14.             obj.addEventListener(type, fn, false);
  15.         }
  16.         else if (obj.attachEvent) {
  17.             obj["e"+type+fn] = fn;
  18.             obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
  19.             obj.attachEvent("on"+type, obj[type+fn]);
  20.         }
  21.     }
  22.  
  23.  
  24. /* DOM ******************************************************************************************************/
  25.  
  26. function navigateurEstIncompatible() {
  27.         return (!document.getElementById || !document.createElement || !document.appendChild);
  28. }
  29.  
  30. function getElementsByClassName(className, tag, elm) {
  31.         var testClass = new RegExp("(^|\s)" + className + "(\s|$)");
  32.         var tag = tag || "*";
  33.         var elm = elm || document;
  34.         var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
  35.         var returnElements = [];
  36.         var current;
  37.         var length = elements.length;
  38.         for(var i=0; i<length; i++){
  39.                 current = elements[i];
  40.                 if(testClass.test(current.className)){
  41.                         returnElements.push(current);
  42.                 }
  43.         }
  44.         return returnElements;
  45. }
  46.  
  47. function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
  48.     var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
  49.     var arrReturnElements = new Array();
  50.     var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
  51.     var oCurrent;
  52.     var oAttribute;
  53.     for(var i=0; i<arrElements.length; i++){
  54.         oCurrent = arrElements[i];
  55.         oAttribute = oCurrent.getAttribute(strAttributeName);
  56.         if(typeof oAttribute == "string" && oAttribute.length > 0){
  57.             if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
  58.                 arrReturnElements.push(oCurrent);
  59.             }
  60.         }
  61.     }
  62.     return arrReturnElements;
  63. }
  64.  
  65. function chercherElementsDeFormulaire(obj) {
  66.         result = new Array();
  67.         searchTags = ['INPUT', 'TEXTAREA', 'OPTION'];
  68.         for (i=0; i<searchTags.length; i++) {
  69.                 tags = obj.getElementsByTagName(searchTags[i]);
  70.                 for (j=0; j<tags.length; j++) {
  71.                         result.push(tags[j]);
  72.                 }
  73.         }
  74.         return result;
  75. }
  76.  
  77. function insertAfter(parent, node, referenceNode) {
  78.   parent.insertBefore(node, referenceNode.nextSibling);
  79. }
  80.  
  81. function createImageLink(src, href, title, noPngFix) {
  82.         img = document.createElement("img");
  83.         img.setAttribute('src', src);
  84.         img.setAttribute('border', '0');
  85.         img.setAttribute('alt', title);
  86.         img.setAttribute('noPngFix', noPngFix);
  87.         a = document.createElement("a");
  88.         a.onclick = function(){ eval(href); }
  89.         //a.setAttribute('href', ''); // document.URL+'#'
  90.         a.removeAttribute('href');
  91.         a.setAttribute('title', title);
  92.         a.style.cursor = "pointer";
  93.         a.appendChild(img);
  94.         return a;
  95. }
  96.  
  97.  
  98. function createSpacer() {
  99.         spacer = document.createElement("div");
  100.         spacer.setAttribute('class', '_spacer');
  101.         return spacer;
  102. }
  103.  
  104.  
  105. /* FORMULAIRES  ******************************************************************************************************/
  106.  
  107. function sauvegarderValeursElementsForm(obj) {
  108.         elementsForm = chercherElementsDeFormulaire(obj);
  109.  
  110.         for (i=0; i<elementsForm.length; i++) { // boucle sur les elements pour sauvegarder leur valeur
  111.                 elementForm             = elementsForm[i];
  112.                 elementTagName  = elementForm.tagName.toLowerCase();
  113.                 elementType             = elementForm.type;
  114.  
  115.                 if (elementType == 'textarea' || elementType == 'text') { // textarea ou text  => on prend la "value"
  116.                         elementForm.valueDepart = elementForm.value;
  117.                 } else if (elementType == 'checkbox' || elementType == 'radio') { // checkbox ou radio  => on prend la "checked"
  118.                         elementForm.valueDepart = elementForm.checked;
  119.                 } else if (elementTagName == "option") { // option (tagName car pas de type) => on prend la "selected"
  120.                         elementForm.valueDepart = elementForm.selected;
  121.                 }
  122.         }
  123. }
  124.  
  125.  function restaurerValeursElementsForm(obj) {
  126.         elementsForm = chercherElementsDeFormulaire(obj);
  127.  
  128.         for (i=0; i<elementsForm.length; i++) { // boucle sur les elements pour restaurer leur valeur
  129.                 elementForm             = elementsForm[i];
  130.                 elementTagName  = elementForm.tagName.toLowerCase();
  131.                 elementType             = elementForm.type;
  132.  
  133.                 if (elementType == 'textarea' || elementType == 'text') { // textarea ou text  => on prend la "value"
  134.                         elementForm.value = elementForm.valueDepart;
  135.                 } else if (elementType == 'checkbox' || elementType == 'radio') { // checkbox ou radio  => on prend la "checked"
  136.                         elementForm.checked = elementForm.valueDepart;
  137.                 } else if (elementTagName == "option") { // option (tagName car pas de type) => on prend la "selected"
  138.                         elementForm.selected = elementForm.valueDepart;
  139.                         if (elementForm.selected && elementForm.parentNode.type == "select-one") { // bug refresh de la liste safari
  140.                                 elementForm.parentNode.selectedIndex = elementForm.index;
  141.                         }
  142.                 }
  143.         }
  144. }
  145.  
  146. function desactiverAutocomplete(obj) {
  147.         var inputElements = obj.getElementsByTagName("input");
  148.         for (var i=0; i<inputElements.length; i++) {
  149.                  if (inputElements[i].type == "text" || inputElements[i].type == "password") {
  150.                          inputElements[i].setAttribute("autocomplete","off");
  151.                  }
  152.         }
  153. }
  154.  
  155. /* CSS ******************************************************************************************************/
  156.  
  157. function getDimensions(element) {
  158.     element = $(element);
  159.     var display = getDisplay(element);
  160.     if (display != 'none' && display != null) // Safari bug
  161.                 return {width: element.offsetWidth, height: element.offsetHeight};
  162.     var els = element.style;
  163.     var originalVisibility = els.visibility;
  164.     var originalPosition = els.position;
  165.     var originalDisplay = els.display;
  166.     els.visibility = 'hidden';
  167.     els.position = 'absolute';
  168.     els.display = 'block';
  169.     var originalWidth = element.clientWidth;
  170.     var originalHeight = element.clientHeight;
  171.     els.display = originalDisplay;
  172.     els.position = originalPosition;
  173.     els.visibility = originalVisibility;
  174.     return {width: originalWidth, height: originalHeight};
  175.   }
  176.  
  177. function getDisplay(element) {
  178.         element = $(element);
  179.         var value = element.style['display'];
  180.         if (!value) {
  181.                 if (navigator.appVersion.indexOf("MSIE")!=-1) { // IE
  182.                         value = element.currentStyle.display;
  183.                 } else {
  184.                         var css = document.defaultView.getComputedStyle(element, null);
  185.                         value = css ? css['display'] : null;
  186.                 }
  187.         }
  188.         return value;
  189. }
  190.  
  191. function tailleDoc() {
  192.         var test1 = document.body.scrollHeight;
  193.         var test2 = document.body.offsetHeight
  194.         if (test1 > test2) { // all but Explorer Mac
  195.                 return {width: document.body.scrollWidth, height: document.body.scrollHeight};
  196.         } else { // Explorer Mac; would also work in Explorer 6 Strict, Mozilla and Safari
  197.                 return {width: document.body.offsetWidth, height: document.body.offsetHeight};
  198.         }
  199. }
  200.  
  201. function tailleDocVisible() {
  202.         if (self.innerHeight) { // all except Explorer
  203.                 return {width: self.innerWidth, height: self.innerHeight};
  204.         } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
  205.                 return {width: document.documentElement.clientWidth, height: document.documentElement.clientHeight};
  206.         } else if (document.body) { // other Explorers
  207.                 return {width: document.body.clientWidth, height: document.body.clientHeight};
  208.         }
  209. }
  210.  
  211. function positionDocVisible() {
  212.         if (self.pageYOffset) { // all except Explorer
  213.                 return {x: self.pageXOffset, y: self.pageYOffset};
  214.         } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
  215.                 return {x: document.documentElement.scrollLeft, y: document.documentElement.scrollTop};
  216.         } else if (document.body) { // all other Explorers
  217.                 return {x: document.body.scrollLeft, y: document.body.scrollTop};
  218.         }
  219. }
  220.  
  221. function permuterAffichageDivs(lIdBlocAfficher, lIdBlocMasquer) {
  222.       tempBlocAfficher = document.getElementById(lIdBlocAfficher);
  223.       tempBlocMasquer = document.getElementById(lIdBlocMasquer);
  224.       if (tempBlocAfficher && tempBlocMasquer) {
  225.             tempBlocAfficher.style.display = 'block';
  226.             tempBlocMasquer.style.display = 'none';
  227.       }
  228. }
  229.  
  230. /* AJAX ******************************************************************************************************/
  231.  
  232. function lancerRequeteAjax(url, http_vars, method, selector, obj) {
  233.         req = false;
  234.     if(window.XMLHttpRequest && !(window.ActiveXObject)) {
  235.         try {
  236.                         req = new XMLHttpRequest();
  237.         } catch(e) {
  238.                         req = false;
  239.         }
  240.     } else if(window.ActiveXObject) {
  241.         try {
  242.                 req = new ActiveXObject("Msxml2.XMLHTTP");
  243.         } catch(e) {
  244.                 try {
  245.                         req = new ActiveXObject("Microsoft.XMLHTTP");
  246.                 } catch(e) {
  247.                         req = false;
  248.                 }
  249.                 }
  250.     }
  251.         if(req) {
  252.                 if (method.toUpperCase() == "GET") {
  253.                         url = url+"?"+  http_vars;
  254.                         http_vars = "";
  255.                 }
  256.                 req.onreadystatechange = function() {
  257.                         processReponseAjax(req, selector, obj);
  258.                 }
  259.                 req.open(method, url, true);
  260.                 req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  261.                 req.send(http_vars);
  262.         }
  263.         return req;
  264. }
  265.  
  266. function processReponseAjax(req, selector, obj) {
  267.     if (req.readyState == 4) {
  268.                 selector.call("", req.responseText, obj, (req.status != 200));
  269.     }
  270. }
  271.  
  272.  
  273. /* CORRECTION TRANSPARENCE PNG DANS IE6 ******************************************************************************************/
  274.  
  275. function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
  276. {
  277.    var arVersion = navigator.appVersion.split("MSIE")
  278.    var version = parseFloat(arVersion[1])
  279.    if ((version >= 5.5) && (document.body.filters))
  280.    {
  281.       for(var i=0; i<document.images.length; i++)
  282.       {
  283.          var img = document.images[i]
  284.          var imgName = img.src.toUpperCase()
  285.          if (imgName.substring(imgName.length-3, imgName.length) == "PNG" && !img.getAttribute('noPngFix')) {
  286.             var imgID = (img.id) ? "id='" + img.id + "' " : ""
  287.             var imgClass = (img.className) ? "class='" + img.className + "' " : ""
  288.             var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
  289.             var imgStyle = "display:inline-block;" + img.style.cssText
  290.             if (img.align == "left") imgStyle = "float:left;" + imgStyle
  291.             if (img.align == "right") imgStyle = "float:right;" + imgStyle
  292.             if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
  293.             var strNewHTML = "<span " + imgID + imgClass + imgTitle
  294.             + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
  295.             + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
  296.             + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
  297.             img.outerHTML = strNewHTML
  298.             i = i-1
  299.          }
  300.       }
  301.    }
  302. }
  303.  
  304. /* RECUPERATION PARAMETRES GET *****************************************************************************************/
  305.  
  306. function getURLParam(strParamName){
  307.   var strReturn = "";
  308.   var strHref = window.location.href;
  309.   if ( strHref.indexOf("?") > -1 ){
  310.     var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
  311.     var aQueryString = strQueryString.split("&");
  312.     for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
  313.       if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
  314.         var aParam = aQueryString[iParam].split("=");
  315.         strReturn = aParam[1];
  316.         break;
  317.       }
  318.     }
  319.   }
  320.   return unescape(strReturn);
  321. }
  322.  
  323.