// Ajouts de prototype

// Général
function isnull (p) {
return typeof(p)=='undefined' || typeof(p)=='null';
}
function isregexp (o) {
return typeof(o)=='object' && o.test && o.exec;
}

//String
String.prototype.trim = function () {
return this.replace(/^\s*/g,'').replace(/\s*$/g,'');
}
String.prototype.strtr = function (str1, str2) {
var result = '';
for (var i=0; i < this.length; i++) {
var c = this.charAt(i);
var n = str1.indexOf(c);
if (n!= -1) c = str2.charAt(n);
result += c;
}
return result;
}
String.prototype.desaccent = function () {
return this.strtr(
'âäàáêëèéîïìíôöòóûüùúÂÄÀÁÊËÈÉÎÏÌÍÔÖÒÓÛÜÙÚçÇãÃñÑõÕÿŸýÝ',
'aaaaeeeeiiiioooouuuuAAAAEEEEIIIIOOOOUUUUcCaAnNoOyYyY'
);//f
}
String.prototype.normalize = function () {
return this
.toLowerCase()
.desaccent()
.strtr(
'©²³¹µ .:!?"«»??¨~%&+|¦*@/()[]{}=§<>—–©™^,;\'\\# ¡¬­®¯°±¶¸',
'c231u----------------------------------------------------------'
).replace(/-+/g, '-')
.replace(/^-+/g, '')
.replace(/-+$/g, '');
}


//Array
Object.prototype.contains = function (o) {
var regexp = isregexp(o);
for (var i in this) {
if (this[i]==o) return true;
if (regexp && o.test(this[i])) return true;
}
return false;
}
Array.prototype.contains = function (o) {
var regexp = isregexp(o);
for (var i=0; i < this.length; i++) {
if (this[i]==o) return true;
if (regexp && o.test(this[i])) return true;
}
return false;
}

//Number
Number.prototype.format = function (b) {
if (typeof(b)=='undefined' || typeof(b)=='null') b=false;
var sepg = ' ', sepd = ',', chap=(b? 2:0), chpg=3;

var str = "";
var w = Math.pow(10, chap);
var s= ""+Math.round(this*w)/w;


if (chap > 0) {
var i = s.indexOf(".");
if (i==-1) s+=".";
var i = s.indexOf(".");
i = s.substring(i+1).length;

while (i < chap) { s += "0";  i++; }
}
i = s.indexOf(".");
if (i==-1) i = s.length;
var k = s.substring(0,i);
var k2 = s.substring(i+1);
var n = 0;
for (var i=k.length-1; i >= 0; i--) {
if (k.charAt(i)!='-') n++;
str = k.charAt(i) + str;

if (n >= chpg && i > 0) { 
str = sepg + str; 
n=0;
}
}

if (chap > 0) {
str += "." + k2;
}

return str.split(".").join(sepd);
}
Number.prototype.formatTime = function () {
var h = Math.floor(this / 3600), m = Math.floor(this / 60)%60, s = Math.floor(this)%60;

return (
(h<10? '0' : '')
+ h + ':' 
+ (m<10? '0':'')
+ m + ':'
+ (s<10? '0':'')
+s);
}


// DOM/document
$ = function (n) { return document.getElementById(n); }
$c = document.getElementsByClass = function (cln, tgn, ob) {
if (isnull(ob)) ob = document;
if (isnull(tgn)) tgn = '*';
var tmp = ob.getElementsByTagName(tgn);
var t = [];
for (var i=0; i < tmp.length; i++) {
var o = tmp[i];
if (typeof(o.className)!='undefined' && o.className) {
var classes = o.className.split(' ');
if (classes.contains(cln)) t.push(o);
}}
return t;
}
$a = document.getElementsByAttr = function (attrN, attrV, tgn, ob) {
if (isnull(ob)) ob = document;
if (isnull(tgn)) tgn = '*';
//alert('search for attribute ' + attrN + ' = ' + attrV + ' in ' + ob.tagName + ' for tag ' + tgn);

var tmp = ob.getElementsByTagName(tgn);
var t = [];
for (var i=0; i < tmp.length; i++) {
var o = tmp[i];
if (typeof(o[attrN])!='undefined' && o[attrN]) {
if (isnull(attrV)
|| o[attrN]==attrV
|| (isregexp(attrV) && attrV.test(o[attrN]))
) t.push(o);
}}
return t;
}
function modalPopup (url, args, options) {
if (window.showModalDialog) showModalDialog(url,args,options);
else {
var w = window.open(url,'modaldialog',options);
w.dialogArguments = args;
window.onfocus = document.onfocus = function () {
if (w.closed) { window.onfocus = document.onfocus = null; return; }
if (window.blur) window.blur();
w.focus();
};
}}
function include (src) {
var s = document.createElement('script');
s.setAttribute('src', src);
s.setAttribute('type', 'text/javascript');
s.type = 'text/javascript';
s.src = src;
document.getElementsByTagName('head')[0].appendChild(s);
}

function confirmLink_click (href) {
return function () {
window.tmp0=this;
window.tmp1=this.href;
this.href = "javascript: if (confirm('Êtes-vous sûr de vouloir effectuer cette opération ?')) { window.tmp0.onclick=null; window.tmp0.href=window.tmp1; window.tmp0.click(); } else void(window.tmp0.href=window.tmp1); ";
return true;
};}

registeredOnloads = [];
function addLoad (f) { registeredOnloads.push(f); }
window.onload = function () {
var cl1 = document.getElementsByClass('confirm', 'a');
for (var i=0; i < cl1.length; i++) {
var o = cl1[i];
var href = o.href;
o.onclick = confirmLink_click(href);
}

var cl2 = document.getElementsByClass('secret', 'div');
for (var i=0; i < cl2.length; i++) {
var d = cl2[i];
var p = d.getElementsByTagName('p')[0];
p.appendChild(document.createTextNode('['));
var a = document.createElement('a');
a.href='#';
a.onclick = function () {
var o = this.parentNode.parentNode.getElementsByTagName('div')[0];
o.style.display = (o.style.display=='block'? 'none':'block');
return false;
};
a.appendChild(document.createTextNode('Afficher/masquer'));
p.appendChild(a);
p.appendChild(document.createTextNode(']'));
d.getElementsByTagName('div')[0].style.display='none';
}

var cl3 = document.getElementsByClass('tree', 'ul');
for (var i=0; i < cl3.length; i++) {
var cl3x = cl3[i].getElementsByTagName('ul');
for (var j=0; j < cl3x.length; j++) {
var ul = cl3x[j];
var li = ul.parentNode;
var a = document.createElement('a');
a.href = '#';
a.onclick = function () {
var ul = this.parentNode.getElementsByTagName('ul')[0];
ul.style.display = (ul.style.display=='none'? 'block':'none');
this.innerHTML = (ul.style.display=='none'? '(+)' : '(-)' );
return false;
};
a.appendChild(document.createTextNode('(+)'));
li.insertBefore(a, li.childNodes[0]);
ul.style.display = 'none';
}}


// Other pre-existing automatic loads

for (var i in registeredOnloads) registeredOnloads[i]();
}


