/*
Global.js
Fichier regroupant toutes les fonctions javascript du site quentinc.net
*/

//Fonctions d'identification du navigateur
var isIe = false, isFf = false;
if (window.external) isIe = true;
else if (window.sidebar) isFf = true;

// Exit les frames éventuelles
if (top!=self) { top.location = self.location; }

// Ajout de la fonction trim comme en php
String.prototype.trim = function () {
return this.replace(/^\s*/g,'').replace(/\s*$/g,'');
}

// Fonction de contrôle mail, date et url
function checkMail (str) {
return /^[-a-z0-9_\.]{3,}@[-a-zA-Z0-9_]{3,}\.[a-z]{2,4}$/g .test(str);
}
function checkUrl (str) {
return (new RegExp("^http://[-a-zA-Z_0-9\\\\\\?\\!'\"&%\\+\\.]+$", "g")).test(str);
}
function checkDate (str) {
var reg1 = new RegExp("^\\d{2}[\\./]\\d{2}[\\./]\\d{4}$", "g");
var reg2 = new RegExp("^\\d{4}-\\d{2}-\\d{2}$", "g");
var reg3 = new RegExp("^[\\+-]\\d+ (year|month|week|day|hour|minute|second)s?$", "g");
return reg1.test(str) | reg2.test(str) | reg3.test(str);
}


// Gestion des cookies
cookies = new Object();
var cookIndex = 0; var lastCookIndex = 0;
while (cookIndex != -1) {
cookIndex = document.cookie.indexOf(";", lastCookIndex);
var strtmp = document.cookie.substring(lastCookIndex, ((cookIndex<0)? 1048575:cookIndex));
var i = strtmp.indexOf("=");
cookies[strtmp.substring(0,i).trim()]=strtmp.substring(i+1);
lastCookIndex = cookIndex + 1;
}
lastCookIndex = undefined; cookIndex = undefined;
function writeCookie (name, value, expires)
{
var date = new Date((new Date()).getTime() + expires);
var expdate=date.toGMTString();
document.cookie=name+"="+value+";expires="+expdate+";path=/;";
}
function getCookie (name) { return readCookie(name); }
function readCookie (name) {
if (cookies[name]) return cookies[name];
else return null;
}

//Gestion du DOM
if (!document.getElementById) {
document.getElementById = function (id) {
if (document.all && document.all[id]) return document.all[id];
if (document.layers && document.layers[id]) return document.layers[id];
return null;
}}
document.getElementsByAttribute = function (attribName, attribValue, root) {
if (typeof(attribValue)=="undefined") attribValue = false;
if (typeof(root)=="undefined") root = document;
if (!root.getElementsByTagName) return new Array();
var tab = root.getElementsByTagName('*');
var res = new Array();

for (var i=0; i < tab.length; i++) {
if (tab[i] && tab[i][attribName]) {
if (!attribValue || attribValue == tab[i][attribName]) res.push(tab[i]);
}}

return res;
}


//XMLHttpRequest
function createXhr () {
if (window.XMLHttpRequest) 
return new XMLHttpRequest();
else if (window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
else
return null;
}


// implémentation de innerText pour firefox
if (typeof(HTMLElement)!="undefined") {
      HTMLElement.prototype.__defineGetter__("innerText", function(){
		return this.innerHTML.replace(/<[^>]+>/g,"");
	});
}

// Autogénérateur de fonctions de vérification de formulaire
function createVFC (name, param) {

var nget = "e = this.elements['"+name+"'];\r\n";
var rndvar = "v" + Math.floor(16777215 * Math.random());

var tab = param.toLowerCase().split('_');

if (tab[0] == "email") 
return nget + "if (!checkMail(e.value)) { alert(\"L'adresse e-mail que vous avez saisie est invalide.\"); e.focus(); e.select(); return false; }\r\n";

else if (tab[0] == "nickname") 
return nget + "if (!(/^[-a-zA-Z0-9_]+$/g).test(e.value.trim())) { alert(\"Le pseudonyme que vous avez saisi contient des caractères invalides.\\nUn pseudonyme est normalement constitué de lettres, de chiffres et de caractères de soulignement. IL ne doit jamais contenir d'espace.\"); "
+ "e.focus(); e.select(); return false; }\r\n";

else if (tab[0] == "password")
return nget + "if (e.value.length <= 5 || e.value.length >= 17) { alert(\"Un bon mot de passe contient entre 6 et 16 caractères.\"); e.focus(); e.select(); return false; }\r\n";

else if (tab[0] == "mustcheckone")
return nget + "var "+rndvar+" = false; for (var i=0; i < e.length; i++) { if (e[i].checked) { "+rndvar+"=true; break; }}  if (!"+rndvar+") { "
+ " alert(\"Veuillez s'il vous plaît choisir une option parmi celles qui sont proposées.\"); e[0].focus(); return false; }\r\n";

else if (tab[0] == "mustselectone")
return nget + "if (e.selectedIndex == 0) { alert(\"Veuillez s'il vous plaît choisir une option parmi celles qui sont proposées.\"); e.focus(); return false; }\r\n";

else if (tab[0] == "stringlength" || tab[0] == "string")
return nget + "var "+rndvar+" = e.value.trim(); if ("+rndvar+".length < "+tab[1]+" || "+rndvar+".length > "+tab[2]+") { alert(\"Veuillez s'il vous plaît saisir entre " + tab[1] + " et " + tab[2] + " caractères.\"); e.focus(); e.select(); return false; }\r\n";

else if (tab[0] == "passwordcheck")
return nget + "if (e.value != this.elements['"+tab[1]+"'].value) { alert(\"Le mot de passe et sa confirmation ne sont pas identiques.\"); e.focus(); e.select(); return false; }\r\n";

else if (tab[0] == "date")
return nget + "if (!checkDate(e.value)) { alert(\"La date que vous avez saisie est invalide.\"); e.focus(); e.select(); return false; }\r\n";

else if (tab[0]== "charte")
return nget + "if (!e.checked) { alert(\"Merci de bien vouloir lire les conditions d'utilisation.\"); return false; }\r\n";

else if (tab[0] == "url")
return nget + "if(!checkUrl(e.value)) { alert(\"L'adresse URL que vous avez indiquée est invalide.\"); e.focus(); e.select(); return false; }\r\n";

else if (tab[0].substring(0,7)=="emptyor") 
return nget + "if (e.value.length >0) { " + createVFC(name, param.substring(7)) + " }\r\n";

return "";
}



//Autres fonctions
window.onload = function (e) {
if (!document.forms) document.forms=document.getElementsByTagName('form');
for (var k = 0; k < document.forms.length; k++) {
var tab = document.forms[k].elements;
var checkCode = "";

for (var i=0; i < tab.length; i++) {
if (tab[i] ) {
var tag = tab[i].tagName.toLowerCase();
if (tag == "input" || tag == "textarea" || tag=="select") {
if (tab[i].className && tab[i].className.substring(0,3)=="VFC") checkCode += createVFC(tab[i].name, tab[i].className.substring(3));
if (tab[i].type && tab[i].type == "text") {
if (isIe) tab[i].onfocus = function (e) { this.select(); this.focused = true; this.className='inputFocused'; };
else tab[i].onfocus = function (e) { this.select(); };
}
else if (isIe) tab[i].onfocus = function () { this.focused = true; this.className='inputFocused'; };

if (isIe) {
tab[i].onblur = function (e) { this.focused = false; if (!this.hover) this.className='inputNormal'; };
tab[i].onmouseover = function (e) { this.hover=true; this.className='inputFocused'; };
tab[i].onmouseout = function (e) { this.hover=false; if (!this.focused) this.className='inputNormal'; };
}}
else if (tag == "button" && isIe) {
tab[i].onfocus = function (e) { this.focused = true; this.className='buttonFocused'; };
tab[i].onblur = function (e) { this.focused=false; if (!this.hover) this.className='buttonNormal'; };
tab[i].onmouseover = function (e) { this.hover=true; this.className='buttonFocused'; };
tab[i].onmouseout = function (e) { this.hover=false; if (!this.focused) this.className='buttonNormal'; };
}
}}

if (checkCode) {
//alert(checkCode);
document.forms[k].onsubmit = new Function("event", "var e;\r\n\r\n" + checkCode + "\r\n\r\nreturn true;");
}

}


if (window.onLoad) onLoad(e? e:window.event);
}

window.onunload = function (e) {
if (window.onUnload) window.onUnload(e? e:window.event);
}




