/*
** utiltiaires
*/

function wOpen(href, width, height)
{
	w = window.open(href, 'target', 'toolbar=0, location=0, directories=0, status=1, scrollbars=1, resizable=1, copyhistory=0, menuBar=1, width='+width+', height='+height+', left=763, top=200');
	w.focus();
	return false;
}

function do_check_login (frm)
{
	var err = '';
	if (!frm.email.value.length)
		err += "Veuillez indiquer votre email ou votre code client\n";
	else if (frm.email.value.match(/@/))
	{
		if (!frm.email.value.match(/^[a-z0-9\.\-\_]+@([a-z0-9\-\_]+\.)+[a-z]{2,3}$/i))
			err += "Veuillez indiquer une adresse email valide\n";
	}
	else if (checkNumeroClient(frm.email.value))
	{
		frm.action = BASE_URL + 'guides/servicesdomestiques.html';	
		frm.numeroClient.value = frm.email.value;
		frm.email.value = '';
		if (frm.log) frm.log.value = '0';
	}
	else
		err += "Veuillez indiquer un email ou un code client valide\n";
	if (!err.length) return true;
	alert (err);
	return false;
}

function do_check_mail(frm)
{
	var err = "";
	if (!frm.email.value.length)
		err += "Veuillez indiquer votre email\n";
	else if(!frm.email.value.match(/^[a-z0-9\.\-\_]+@([a-z0-9\-\_]+\.)+[a-z]{2,3}$/i))
		err += "Veuillez indiquer une adresse email valide\n";
	if(!err.length) return true;
	alert(err);
	return false;
}
function goURL (page)
{
	var url = BASE_URL + page;
	document.location.href = url;
}
function doBlur(c){ if (!c.value.length) c.value = c.defaultValue;}
function doFocus(c){ if (c.value == c.defaultValue) c.value=''; }
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/*
** pour le header
*/
function check_pwd_bg()
{
	this.style.background = this.value.length ? 'none' : 'transparent url(img/bg_pwd.gif) no-repeat 0 0';
}

function window_setActive (tab, isActive)
{
	clearTimeout(to);
	for (i=0; i<menuItems.length; i++)
	{
		if (menuItems[i].className.indexOf("courant") >= 0)
		{
			currentTab = menuItems[i].id;
			if (!tab) tab = currentTab;
		}
		document.getElementById("sm_" + menuItems[i].id).style.display = (menuItems[i].id == tab) ? "block" : "none";
		y = (menuItems[i].id == tab) ? ((tab == currentTab) ? -116 : -58) : 0;
		menuItems[i].style.backgroundPosition = " 0 " + y + "px";
	}
	// on affiche le sous-menu de l'espace personnel si aucun autre sous-menu n'est affiché
	document.getElementById("sm_vot").style.display = (tab ? 'none' : 'block');
}

/*
** lightAjax
*/
lightAjax=function(){this.req={};};
lightAjax.prototype.makeRequest=function(url,fnComplete){
	this.onComplete=fnComplete;
	var pointer=this;
	this.req = (window.ActiveXObject)?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest();
	if (this.req){
		this.req.onreadystatechange=function(){pointer.statusChange()};
		this.req.open("GET",url,true);
		this.req.send(null);
	}
}
lightAjax.prototype.statusChange=function(){
	if (this.req.readyState==4 && this.req.status==200)
		this.onComplete(this);
}
function getElementsByClassName(node, classname)
{
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for(var i=0; i<els.length; i++)
		if(re.test(els[i].className)) a.push(els[i]);
	return a;
}

/*
** glossaire
*/

// mapDfn
function mapDfn(elt)
{
	if (!elt) elt = document;
	var toolTip = document.getElementById('tooltip');
	if(!toolTip){
		var toolTip = document.createElement('div');	
		toolTip.id = 'tooltip';
		document.body.appendChild(toolTip);			
	}
	var dfns = elt.getElementsByTagName('dfn');
	for (var i=0;i<dfns.length;i++) {
		dfns[i].onclick = showGlossaire;
	}
}

// showGlossaire
function showGlossaire(e){
	var a = new lightAjax();
	a.makeRequest("show_glossaire.php?terme="+escape(this.innerHTML), doUpdate);
	
	if (!e) e = window.event;				
	var mouseX = e.pageX || (e.clientX ? e.clientX + document.body.scrollLeft : 0);
	var mouseY = e.pageY || (e.clientY ? e.clientY + document.body.scrollTop : 0);
	
	var toolTip = document.getElementById('tooltip');
	
	if(toolTip.style.display != 'block'){
		toolTip.style.left = mouseX - 15 +"px";
		toolTip.style.top = mouseY - 15 + "px";
		toolTip.style.display = "block";
	}
}

// hideGlossaire
function hideGlossaire(e){
/*
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	if (tg.nodeName != 'DIV') return;
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	while (reltg != tg && reltg.nodeName != 'BODY')
		reltg= reltg.parentNode
	if (reltg== tg) return;
*/	
	var toolTip = document.getElementById('tooltip');
	toolTip.innerHTML = "";
	toolTip.style.display = "none";
}
		
// doUpdate
function doUpdate(ajax){
	if (!(txt = ajax.req.responseText) || !txt.length)
		return;
	var tooltip = document.getElementById('tooltip');
	tooltip.innerHTML = txt;
	mapDfn(tooltip);
}

addLoadEvent(mapDfn);


/*
** checkNumeroClient : vérifier un numéro client
*/
function checkNumeroClient(numeroClient)
{
	var str = '', i = 0, a = null, c = null, l = numeroClient.length;
	var debug = numeroClient + "\n";
	if (l != 10) return false;
	numeroClient = numeroClient.toUpperCase();
	// on vérifie la clé
	l--;
	var cle = numeroClient.charAt(l);
	debug += "cle: " + cle + "\n";
	// convertir les lettres en chiffres
	for (i=0; i < l; i++)
	{
		c = numeroClient.charAt(i);
		a = c.charCodeAt(0);
		// soit entre A et Z
		if (a >= 65 && a <= 90)
		{
			debug += c;
			while (a >= 10)
			{
				c = (a % 10) + (a - a % 10) / 10;
				debug += "\t" + a.toString() + "\t" + c.toString() + "\n";
				a = c;
			}
			str += a.toString();
		}
		// soit entre 0 et 9
		else if (a >= 48 && a <= 57)
			str += c;
		else
			return false;
		debug += str + "\n";
	}
	// puis on parcourt la chaîne
	// on additionne les chiffres en multipliant par 3 un chiffre sur deux
	var x = 0;
	for(i=0; i < l; i++)
	{
		debug += (i+1)+': ' + x + ' + ' + ((i % 2)==0 ? '3' : '1') + " * " + str.charAt(l-1-i) + " = ";
		x += ((i % 2)==0 ? 3 : 1) * parseInt(str.charAt(l-1-i));
		debug += x.toString() + "\n";
	}
	debug += "x: " + x.toString() + "\n";
	x = (10 - (x % 10)) % 10;
	debug += "x: " + x.toString() + "\n";
	debug += (x == cle) ? 'OK' : 'ECHEC' + "\n";
//	alert (debug);
	return (x == cle);
}