/* {{{ FONCTIONS A LA PHP ====================== */ // fonctions pour (de)serializer un tableau javascript function serialize(a) { return a.toString(); } function unserialize(s) { return s.split(","); } function trim(s) { return s.replace(/^\s+/, '').replace(/\s+$/, ''); } function ltrim(s) { return s.replace(/^\s+/, ''); } function rtrim(s) { return s.replace(/\s+$/, ''); } /* {{{ is_array */ function is_array(obj) { return (obj.constructor.toString().indexOf("Array") != -1) // return (obj.constructor.toString()) } /* }}} */ /* {{{ strpad : equivalent de la fonction PHP */ function strpad(val, longueur, remplissage) { if (!isNaN(val)) { while ((val.toString().length < longueur)) val = remplissage+val; return val; } else return false; } /* }}} */ /* {{{ print_r : equivalent de la fonction PHP */ function print_r (theObj, stripfunctions) { if (theObj.constructor == Array || theObj.constructor == Object) { document.write('
');
if (name) document.write(name+' : ');
print_r(theObj, stripfunctions);
document.write('<\/pre>');
}
/* }}} */
/* {{{ strip_tags */
function strip_tags (tags)
{
stripped = tags.replace(/<[^<>]*>/gi, "");
return stripped;
}
/* }}} */
/* {{{ surcharge du prototype Array pour ajouter la fonction in_array */
Array.prototype.in_array = function(search_term) {
for (var i = 0; i < this.length; ++i) {
if (this[i] === search_term) {
return true;
}
}
return false;
}
/* }}} */
// }}}
/* {{{ FONCTIONS PRATIQUES POUR FORMULAIRES
======================================== */
/* {{{ function getCheckedElementByTagName : renvoie l'objet du DOM coche (dans le cas d'un bouton radio), dans un formulaire donne eventuellement */
function getCheckedElementByTagName(tagname, form)
{
if (form)
{
var obj = document.forms[form].elements[tagname];
}
else
{
var obj = document.getElementsByName(tagname);
}
var lg = obj.length;
var valeur = false;
var found = false;
var i = 0;
for (; i < obj.length; ++i)
{
if (obj[i].checked)
{
found = true;
valeur = obj[i].value;
break;
}
}
if (found) return obj[i];
else return false;
}
/* }}} */
/* {{{ function vide_onevent : vide un input type=text quand on "clique" dessus ssi il est initialise a sa valeur par defaut */
function vide_onevent(event,id,name,val)
{
var cur_val;
cur_val = document.getElementById(id).value;
switch (event) {
case 'focus':
if (cur_val == name)
{
document.getElementById(id).value = val;
}
break;
case 'blur':
if (!cur_val.length || cur_val == name)
{
document.getElementById(id).value = name;
}
break;
}
}
/* }}} */
/* {{{ function if_confirm : demande une confirmation avant de rediriger vers une url */
function if_confirm (url, message, blank)
{
if (confirm(message)) { document.location = url; }
}
/* }}} */
/* {{{ function compare_fields : compare 2 champs et met a jour un div selon qu'ils sont identiques ou non. pratique en onkeyup, onblur et onclick pour pass et pass_confirm */
function compare_fields (field1, field2, id_span_ok, inner_no, inner_yes, min_length)
{
val1 = document.getElementById(field1).value;
val2 = document.getElementById(field2).value;
if (val1 == val2 && (!min_length || (val1.length >= min_length)))
{
document.getElementById(id_span_ok).innerHTML = inner_yes;
return 1;
}
else
{
document.getElementById(id_span_ok).innerHTML = inner_no;
return 0;
}
}
/* }}} */
/* {{{ function valide_email : valide le format d'une adresse email */
function valide_email (email)
{
reg = /^\s*[a-z0-9\._-]+@([a-z0-9-]+\.)+[a-z0-9]+\s*$/i;
return email.match(reg);
}
/* }}} */
/* {{{ function valide_email_confirm : valide le format d'une adresse email avec 2e champ pour confirmation et met a jour un div selon qu'ils sont identiques ou non. pratique en onkeyup, onblur et onclick pour les email et email_confirm */
function valide_email_confirm (field1, field2, id_span_ok, inner_no, inner_yes, min_length, icase)
{
val1 = document.getElementById(field1).value;
val2 = document.getElementById(field2).value;
if (icase)
{
val1 = val1.toLowerCase();
val2 = val2.toLowerCase();
}
if (val1 == val2 && (!min_length || (val1.length >= min_length)) && valide_email(val1) && valide_email(val2))
{
document.getElementById(id_span_ok).innerHTML = inner_yes;
return 1;
}
else
{
document.getElementById(id_span_ok).innerHTML = inner_no;
return 0;
}
}
/* }}} */
/* {{{ function valide_num : valide les caractères d'un nombre, avec taille min */
function valide_num (field, id_span_ok, inner_no, inner_yes, min_length, max_length)
{
reg = /^[0-9]+$/i;
return valide_champ(field, '', id_span_ok, inner_no, inner_yes, min_length, max_length, reg);
}
/* }}} */
/* {{{ function valide_login : valide les caractères d'un login, avec taille min */
function valide_login (field, id_span_ok, inner_no, inner_yes, min_length, max_length)
{
reg = /^[a-z0-9@\._-]+$/i;
return valide_champ(field, '', id_span_ok, inner_no, inner_yes, min_length, max_length, reg);
}
/* }}} */
/* {{{ function valide_alnum : valide les caractères d'une chaine alphanumerique, avec taille min */
function valide_alnum (field, id_span_ok, inner_no, inner_yes, min_length, max_length)
{
reg = /^[a-z0-9]+$/i;
return valide_champ(field, '', id_span_ok, inner_no, inner_yes, min_length, max_length, reg);
}
/* }}} */
/* {{{ function valide_tel : valide les caractères d'un numero de telephone, avec taille min */
function valide_tel (field, id_span_ok, inner_no, inner_yes, min_length, max_length)
{
reg = /^[0-9()+ \.-]+$/i;
return valide_champ(field, '', id_span_ok, inner_no, inner_yes, min_length, max_length, reg);
}
/* }}} */
/* {{{ function valide_cp : valide un code_postal */
function valide_cp (field, id_span_ok, inner_no, inner_yes)
{
reg = /^[a-z0-9]+$/i;
return valide_champ(field, '', id_span_ok, inner_no, inner_yes, 5, 5, reg);
}
/* }}} */
/* {{{ function value_is_in : verifie que la valeur du champ field correspond a l'une des valeurs du tableau liste */
function value_is_in (field, liste, id_span_ok, inner_no, inner_yes, min_length, max_length)
{
var obj = document.getElementsByName(field);
var lg = obj.length;
var valeur;
for (i = 0; i < obj.length; ++i)
{
if (obj[i].checked)
{
valeur = obj[i].value;
break;
}
}
if (liste.in_array(valeur))
{
document.getElementById(id_span_ok).innerHTML = inner_yes;
return 1;
}
else
{
document.getElementById(id_span_ok).innerHTML = inner_no;
return 0;
}
}
/* }}} */
/* {{{ function valide_champ : valide si un champ est rempli, avec taille min, taille max, et expreg de validation des caracteres autorises */
function valide_champ (field, form, id_span_ok, inner_no, inner_yes, min_length, max_length, reg)
{
// valeurs par defaut
if (!id_span_ok) id_span_ok = field+'_ok';
if (!inner_no) inner_no = '__REQUIREMENT__';
if (!inner_yes) inner_yes = '__REQUIREMENT_OK__';
if (!min_length) min_length = 1;
if (is_array(field)) // valide_champ renvoie vrai si et seulement si au moins un element du tableau est ok
{
valide = 0;
cnt = field.length;
for (i=0; i < cnt; ++i)
{
valide += parseInt(valide_champ(field[i], form, id_span_ok, inner_no, inner_yes, min_length, max_length, reg));
}
if (valide)
{
document.getElementById(id_span_ok).innerHTML = inner_yes;
return 1;
}
else
{
document.getElementById(id_span_ok).innerHTML = inner_no;
return 0;
}
}
retour = 1;
is_radio = 0;
champ = '';
// on essaye d'abord de recuperer la valeur par le name dans un formulaire donne, eventuellement
obj = getCheckedElementByTagName(field, form);
if (obj)
{
champ = obj.value;
is_radio = 1;
}
else
{ // sinon on recupere par l'id
obj = document.getElementById(field);
if (obj) champ = obj.value;
}
champ = trim(champ);
// teste la taille
if ((max_length && (champ.length > max_length))
|| (min_length && (champ.length < min_length)))
{
retour = 0;
}
// evite que la fonction retourne vrai quand on n'a pas selectionne d'element pour des boutons radio. Ne fonctionne pas sous IE...
if (is_radio && champ == false) retour = 0;
// teste la conformite a l'expreg
if (retour && (reg && champ.match(reg) || !reg))
{
document.getElementById(id_span_ok).innerHTML = inner_yes;
return 1;
}
else
{
document.getElementById(id_span_ok).innerHTML = inner_no;
return 0;
}
}
/* }}} */
/* {{{ function incremente : incremente la valeur numerique d'un champ input avec l'increment passe en parametre, eventuellement negatif. */
function increment (obj, increment, min_val)
{
var val = parseInt(obj.value);
if (isNaN(val)) val = 0;
if ((val > min_val && increment < 0) || (increment > 0)) val = val + increment;
return val;
}
/* }}} */
// }}}
/* {{{ FONCTIONS SHOW/HIDE
======================= */
/* {{{ function showhide : show/hide un element du DOM, et change la source d'une image en fonction de l'état caché/affiché */
function showhide (eltid, eltimg, img_reduire, img_restaurer, path, forceshow, visible)
{
if (!img_reduire) img_reduire = 'img/reduire.png';
if (!img_restaurer) img_restaurer = 'img/restaurer.png';
if (!visible)
{
oldval = document.getElementById(eltid).style.display;
newval = (oldval == 'none') ? 'block' : 'none';
if ('block' == forceshow) newval = 'block';
if ('none' == forceshow) newval = 'none';
newimg = (newval == 'block') ? path+img_reduire : path+img_restaurer;
if (eltimg) eltimg.src= newimg;
zindex = (newval == 'block') ? '7' : '1';
document.getElementById(eltid).style.display = 'none';
document.getElementById(eltid).style.zIndex = zindex;
document.getElementById(eltid).style.display = newval;
// if ('block' == forceshow) fade_element('tableau_error_handler');
}
else
{
oldval = document.getElementById(eltid).style.visibility;
newval = (oldval == 'hidden') ? 'visible' : 'hidden';
if ('visible' == forceshow) newval = 'visible';
if ('hidden' == forceshow) newval = 'hidden';
newimg = (newval == 'visible') ? path+img_reduire : path+img_restaurer;
if (eltimg) eltimg.src= newimg;
zindex = (newval == 'visible') ? '7' : '1';
document.getElementById(eltid).style.visibility = 'hidden';
document.getElementById(eltid).style.zIndex = zindex;
document.getElementById(eltid).style.visibility = newval;
// if ('block' == forceshow) fade_element('tableau_error_handler');
}
}
/* }}} */
/* {{{ function opendiv : open (display:'block') un element du DOM */
function opendiv (eltid)
{
if (document.getElementById(eltid)) document.getElementById(eltid).style.display = 'block';
}
/* }}} */
/* {{{ function closediv : close (display:'none') un element du DOM */
function closediv (eltid)
{
document.getElementById(eltid).style.display = 'none';
}
/* }}} */
/* {{{ function empty_div : vide (innerHTML = '') un element du DOM */
function empty_div (eltid)
{
document.getElementById(eltid).innerHTML = '';
}
/* }}} */
// }}}
/* {{{ FONCTIONS DU GESTIONNAIRE D'ERREURS UNIFIE
============================================== */
/* {{{ function trigger_error : trigger_error en javascript pour faire sortir la console d'erreurs utilisateur */
function trigger_error (str, errtype, div_name)
{
if (document.getElementById('tableau_error_handler') && (document.getElementById('tableau_error_handler').innerHTML.length))
{
switch (errtype)
{
case 'raz':
if (div_name)
{
mondiv = document.getElementById(div_name);
if (mondiv)
{
mondiv.innerHTML = "";
mondiv.style.display = "none";
mondiv.style.width = "1px";
mondiv = null;
}
}
else
{
document.getElementById('div_error_handler_showhide').innerHTML = "";
}
break;
case 'notice':
str = "\n"+str+"
\n<\/div><\/div>";
break;
case 'warning':
str = "\n"+str+"
\n<\/div><\/div>";
break;
case 'fatal':
str = "\n"+str+"
\n<\/div><\/div>";
break;
default:
str = "\n"+str+"
\n<\/div><\/div>";
break;
}
if (div_name && errtype != 'raz')
{
mondiv = document.getElementById(div_name);
if (!mondiv)
{
str = ''+str+'<\/div>';
document.getElementById('div_error_handler_showhide').innerHTML += str;
}
else
{
mondiv.innerHTML = str;
window.status = mondiv.style.width;
if (mondiv.style.width == "1px") mondiv.style.width = "100%";
mondiv.style.display = "block";
}
}
else
{
if (str.length)
{
str = ''+str+'<\/div>';
document.getElementById('div_error_handler_showhide').innerHTML += str;
}
}
// auto hide la console d'erreurs si elle est vide
txt_erreurs = trim(strip_tags(document.getElementById('div_error_handler_showhide').innerHTML));
if(!txt_erreurs.length)
{
closediv('tableau_error_handler');
closediv('div_error_handler_showhide');
// alert(txt_erreurs.length+' : '+txt_erreurs);
}
// else opendiv('tableau_error_handler');
}
}
/* }}} */
// }}}
/* {{{ FONCTIONS AJAX
================== */
/* {{{ function send_data : envoie des données en GET ou POST en utilisant XmlHttpRequest */
function send_data (id, data, page, method, appel_posteriori)
{
if (!appel_posteriori) appel_posteriori = "content.innerHTML = XHR_object.responseText ;";
// Cree un objet XMLHttpRequest
if(document.all)
{ // Internet Explorer
var XHR_object = new ActiveXObject("Microsoft.XMLHTTP") ;
}
else
{ // Mozilla
var XHR_object = new XMLHttpRequest();
}
// Emplacement pour affichage
var content = document.getElementById(id);
// Méthode GET
if ("GET" == method)
{
if ("null" == data)
{ // Ouverture du fichier demandé
XHR_object.open("GET", page, true);
}
else
{ // Ouverture du fichier en methode GET
XHR_object.open("GET", page+"?"+data, true);
}
}
// Méthode POST
else if ("POST" == method)
{ // Ouverture du fichier en methode POST
XHR_object.open("POST", page, true);
}
// Ok pour la page cible
XHR_object.onreadystatechange = function()
{
if (XHR_object.readyState == 4
&& XHR_object.status == 200)
{
appel_posteriori = appel_posteriori.replace(/{_AJAXDATA_}/g, XHR_object.responseText);
eval(appel_posteriori);
}
}
if (method == "GET")
{
XHR_object.send (null);
}
else if(method == "POST")
{
XHR_object.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
XHR_object.send(data);
}
} // send_data ()
/* }}} */
/* {{{ function get_file : ouvre le fichier désiré en GET */
function get_file (id, page, appel_posteriori)
{
send_data (id, 'null', page, 'GET', appel_posteriori);
} // get_file ()
/* }}} */
// }}}
/* {{{ integrer_flash : ecrire le code HTML pour integrer un flash, et contourne la restriction IE, en conservant la validite XHTML */
function integrer_flash (largeur, hauteur, chemin, title)
{
document.write("\n");
}
/* }}} */