//***********************************
//*                                 *
//*    Boîte à outils javascript    *
//*                                 *
//***********************************



// Version :			2
// comptabilité js : 	1
// Auteur :				François Chauveau (sauf mentions contraires)
// Copyright :			(c)2007 newplv.com (sauf mentions contraires)
// contact :			contact@newplv.com



// Centre un div dans une windows
function centrerDiv(id, w, horiz, vertic) {
	if (w == null) { w = window; }
	if (horiz == null) { horiz = true; }
	if (vertic == null) { vertic = true; }
	if (id != null) {
		if (w.document.getElementById(id) != null) {
			var d2 = w.document.getElementById(id);
			
			// Récup taille div
			var s, L, H;
			if (w.getComputedStyle) {//NN 6+
				/* alert("NN");*/
				s = w.getComputedStyle(d2, "");
				L = parseFloat( s.getPropertyValue("width") );
				H = parseFloat( s.getPropertyValue("height") );
			/*} else if (d2.currentStyle) {// IE 5+ win (faut width et height par style)
				s = d2.currentStyle;
				L = parseFloat( s["width"] );
				H = parseFloat( s["height"] );
				alert("IE5" + " " + s["width"]);*/
			} else {// IE 4 Win, ou IE Mac (ou NN<6 : échec)    OU IE 5+ Win (faut position:absolute)
				L = d2.clientWidth;
				H = d2.clientHeight;
			}
			
			// Récup taille window
			var wL, wH;
			if (w.innerWidth) {// NN 4+ (intérieur seul)
				wL = w.innerWidth;
				wH = w.innerHeight;
			} else {// IE 4+
				wL = w.document.body.clientWidth;
				wH = w.document.body.clientHeight;
			}
			/* alert(L + " " + H + " " + wL + " " + wH);*/
			
			// Application
			if (horiz && L>0 && wL>0) { d2.style.left = ((wL-L)/2) + "px"; }
			if (vertic && H>0 && wH>0) { d2.style.top = ((wH-H)/2) + "px"; }
		}
	}
}



// Gère plusieurs fonctions sur le même évènement window.onload
//
// Auteur : 		Simon Willison
// Contact :		http://simon.incutio.com
function addLoadEvent(w, func)
{
	if (w == null) { w = window; }
	var oldonload = w.onload;
	if(typeof w.onload != 'function')
	{
		w.onload = func;
	}
	else
	{
		w.onload = function()
		{
			oldonload();
			func();
		}
	}
}



// Gère plusieurs fonctions sur le même évènement form.onsubmit
//
// (Inspiré par Simon Willison, http://simon.incutio.com)
function addSubmitEvent(f, func, param)
{
	if (f == null) { return; }
	var oldonsubmit = f.onsubmit;
	if(typeof f.onsubmit != 'function')
	{
		f.onsubmit = function()
		{
			var r1 = (param != null) ? func(param) : func();
			return r1;
		}
	}
	else
	{
		f.onsubmit = function()
		{
			var r0 = oldonsubmit();
			var r1 = (param != null) ? func(param) : func();
			return (r0 && r1);
		}
	}
}



// Nettoie et formate une chaîne de tél. Format national ou international, en entrée comme en sortie.
//
// En cas de problème, la méthode renvoie null. => Utilisation possible en test de conformité.
// Indicatif international ajouté par défaut (si téléphone donné national) : France (33).
// En cas de téléphone français, la méthode groupe les chiffres par 2 et vérifie qu'il y a bien 9 ou 10 chiffres.
//
// Exemple 1 : "01 48.51-00_01"				=> "01 48 51 00 01"
// Exemple 2 : "(+ 0024) 01 48.51-00_01"	=> "+ 24 0148510001"
// Exemple 3 : "(+ 0024) 01 48.51-00_01"	=> "+ 33 1 48 51 00 01"
String.prototype.formatTel = function(fautInternatEnSortie) {
	if (fautInternatEnSortie == null) { fautInternatEnSortie = false; }
	var tel2 = this;
	
	// alert("on arrive bien ici av rechargement de la page 1 !!!");// ok
	
	// SI indicatif international => ALORS l'extraire
	var internat_reg_str = "^";
	internat_reg_str += "[ ]*";
	internat_reg_str += "[\(]?";
    internat_reg_str += "[ ]*";
	internat_reg_str += "[+]";
	internat_reg_str += "[ ]*";
	internat_reg_str += "[\(]?";
	internat_reg_str += "[0-9]{1,4}";
	internat_reg_str += "[ ]*";
	internat_reg_str += "[\)]?";
	var internat_reg = new RegExp(internat_reg_str, "g");// Ancien BUG (rechargement de la page si appellé pdt onsumbit)
	// alert("on arrive bien ici av rechargement de la page 2 !!!");
	var internat_arr = tel2.match(internat_reg_str);
	var internat_str = (internat_arr != null) ? internat_arr[0] : "";
	if (internat_str != "") {
		tel2 = tel2.slice(internat_str.length);
	}
	
	// Reformatage indicatif internat (suppression du +, des espaces, de la ponctuation, et des zéros entre + et 33)
	var nettoyage_internat_reg = new RegExp("[0-9]*", "g");	
	var nb = parseInt((internat_str.match(nettoyage_internat_reg)).join(""), 10);
	var internat_nb = (isNaN(nb)) ? 0 : nb;
	
	// France par défaut
	if (internat_nb == 0) { internat_nb = 33; }
	
	// Suppression des caractères non désirés (ponctuation, lettres, espaces)
	var nettoyage_1_reg = new RegExp("[^0-9]*", "g");
	tel2 = tel2.replace(nettoyage_1_reg, "");
	
	// Cas de la France : vérification que 9 ou 10 caractères + formatage
	if (internat_nb == 33) {
		
		// Vérif longueur téléphone (normalement, il reste "0148510001" ou "148510001" càd 9/10 chiffres)
		if (tel2.length < 9 && tel2.length > 10) { return null; }
		
		// Version française internationale => Suppr du 1er chiffre français (le plus souvent 0) si présent
		if (fautInternatEnSortie && tel2.length == 10) {
			tel2 = tel2.slice(1);
		}
		
		// Formatage français par groupe de 2 (ou de 1 pour le 1er)
		if (tel2.length == 9) {
			tel2 =	        tel2.substr(0, 1)
					+ " " + tel2.substr(1, 2)
					+ " " + tel2.substr(3, 2)
					+ " " + tel2.substr(5, 2)
					+ " " + tel2.substr(7, 2);
		} else {
			tel2 =	        tel2.substr(0, 2)
					+ " " + tel2.substr(2, 2)
					+ " " + tel2.substr(4, 2)
					+ " " + tel2.substr(6, 2)
					+ " " + tel2.substr(8, 2);
		}		
	}
	
	// Valeur retour
	if (fautInternatEnSortie) {
		tel2 = "+ " + internat_nb + " " + tel2;
	}
	return tel2;
}



// A partir d'une adresse mail, renvoie une adresse nettoyée.
//
// En cas de problème, la méthode renvoie null. => Utilisation possible en test de conformité.
String.prototype.formatMail = function() {
	var mail2 = this;
	var nettoyage_reg = new RegExp("[A-Za-z0-9.\-]+@([A-Za-z0-9\-]+.)+[A-Za-z]{2,4}");
	var mail_arr = mail2.match(nettoyage_reg);
	var mail2 = (mail_arr != null) ? mail_arr[0] : null;
	return mail2;
}



