/**
 *
 * Can show a tooltip over an element
 * Content of tooltip is the title attribute value of the element
 * copyright 2004 Laurent Jouanneau. http://ljouanneau.com/soft/javascript
 * release under LGPL Licence
 * works with dom2 compliance browser, and IE6. perhaps IE5 or IE4.. not Nestcape 4
 *
 * To use it :
 * 1.include this script on your page
 * 2.insert this element somewhere in your page
 *       <div id="tooltip"></div>
 * 3. style it in your CSS stylesheet (set color, background etc..). You must set
 * this two style too :
 *     div#tooltip { position:absolute; visibility:hidden; ... }
 * 4.the end. test it ! :-)
 *
 */


// create the tooltip object
function tooltip(){}

// setup properties of tooltip object
tooltip.id="tooltip";
tooltip.offsetx = 10;
tooltip.offsety = 10;
tooltip.x = 0;
tooltip.y = 0;
tooltip.snow = 0;
tooltip.tooltipElement=null;
tooltip.title_saved='';
tooltip.saveonmouseover=null;
tooltip.ie4 = (document.all)? true:false;		// check if ie4
tooltip.ie5 = false;
if(tooltip.ie4) tooltip.ie5 = (navigator.userAgent.indexOf('MSIE 5')>0 || navigator.userAgent.indexOf('MSIE 6')>0);
tooltip.dom2 = ((document.getElementById) && !(tooltip.ie4||tooltip.ie5))? true:false; // check the W3C DOM level2 compliance. ie4, ie5, ns4 are not dom level2 compliance !! grrrr >:-(


/**
* Open ToolTip. The title attribute of the htmlelement is the text of the tooltip
* Call this method on the mouseover event on your htmlelement
* ex :  <div id="myHtmlElement" onmouseover="tooltip.show(this)"...></div>
*/
tooltip.show = function (htmlelement) {

   if ( this.ie4 || this.dom2 ) {
      // we save text of title attribute to avoid the showing of tooltip generated by browser
      text=htmlelement.getAttribute("title");
      this.title_saved=text;
      htmlelement.setAttribute("title","");
   }
	if(this.dom2){
		this.tooltipElement = document.getElementById(this.id);
      this.saveonmouseover=document.onmousemove;
		document.onmousemove = this.mouseMove;
	}else if ( this.ie4 ) {
      this.tooltipElement = document.all[this.id].style;
      this.saveonmouseover=document.onmousemove;
      document.onmousemove = this.mouseMove;
	}

   if ( this.ie4 || this.dom2 ) {
      if(this.ie4) document.all[this.id].innerHTML = text;
      else if(this.dom2) document.getElementById(this.id).innerHTML=text;

      this.moveTo(this.x + this.offsetx , this.y + this.offsety);

      if(this.ie4) this.tooltipElement.visibility = "visible";
      else if(this.dom2) this.tooltipElement.style.visibility ="visible";
   }
   return false;
}

/**
* hide tooltip
* call this method on the mouseout event of the html element
* ex : <div id="myHtmlElement" ... onmouseout="tooltip.hide(this)"></div>
*/
tooltip.hide = function (htmlelement) {
	if ( this.ie4 || this.dom2 ) {
      htmlelement.setAttribute("title",this.title_saved);
      this.title_saved="";

		if(this.ie4) this.tooltipElement.visibility = "hidden";
      else if(this.dom2) this.tooltipElement.style.visibility = "hidden";

      document.onmousemove=this.saveonmouseover;
	}
}



// Moves the tooltip element
tooltip.mouseMove = function (e) {
   // we don't use "this", but tooltip because this method is assign to an event of document
   // and so is dreferenced

   if(tooltip.ie4 || tooltip.dom2){

      if(tooltip.dom2){
         tooltip.x = e.pageX;
         tooltip.y = e.pageY;
      }else{
         if(tooltip.ie4) { tooltip.x = event.x; tooltip.y = event.y; }
         if(tooltip.ie5) { tooltip.x = event.x + document.body.scrollLeft;
               tooltip.y = event.y + document.body.scrollTop; }
      }
      tooltip.moveTo( tooltip.x +tooltip.offsetx , tooltip.y + tooltip.offsety);
   }
}

// Move the tooltip element
tooltip.moveTo = function (xL,yL) {
	if(this.dom2){
		this.tooltipElement.style.left = xL +"px";
      this.tooltipElement.style.top = yL +"px";
	}else if(this.ie4){
      this.tooltipElement.left = xL;
      this.tooltipElement.top = yL;
   }
}

function updateFocus(objet,etat)
{
	try
	{
		if(etat==0)
		{
			objet.focus();
			etat=1;
		}
		return etat;
	}
	catch(e)
	{
		alert("updateFocus : \n"+e.message);
	}
}
function isEmail(email)
{
	try
	{
		// on passe la donnée saisie par le visiteur en variable pour plus de facilités
		adresse = email;
		taille = email.length;
		// on créé 3 variables pour valider au fur et à mesure le login, le domaine et l'extension qui constituent l'adresse e-mail
		validelog = false;
		validedom = false;
		valideext = false;
		// on extrait le 'login' qui se situe avant l'arobace
		arob = adresse.lastIndexOf("@");
		login = adresse.substring(0,arob);
		// on extrait l'extension qui se situe à partir du dernier point
		pointfinal = adresse.lastIndexOf(".");
		extension = adresse.substring(pointfinal,taille);
		// on extrait le domaine qui se trouve entre l'arobace et le dernier point
		domaine = adresse.substring(arob+1,pointfinal);
		// un login a toujours plus de 2 caractères, on fait le test
		if ( login.length > 2 ) 
		{
			validelog = true;
		}
		else 
		{
			validelog = false;
		}
		
		// un domaine a toujours plus de 1 caractère, on fait le test
		if ( domaine.length > 1 ) 
		{
			validedom = true;
		}
		else 
		{
			validelog = false;
		}
		
		// une extension a toujours 2 ou 3 caractères, on fait le test
		if ( pointfinal > -1 && (extension.length == 3 || extension.length == 4) ) 
		{
			valideext = true;
		}
		else 
		{
			valideext = false;
		}
		
		// on vérifie que nos trois variables sont vraies pour accepter la soumission du formulaire
		if ( validelog == false || validedom == false || valideext == false ) 
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	catch(e)
	{

		alert("isEmail :\n"+e.message);
	}
}

function checkContact()
{
	try
	{
		var erreur="";
		var focusSet=0;
		
		email=document.getElementById("email");
		nom=document.getElementById("nom");
		
		if(nom.value=="")
		{
			erreur+=" - le nom est obligatoire\n";
			document.getElementById("libellenom").className="erreur";
			focusSet=updateFocus(nom,focusSet);
		}
		
		if(!isEmail(email.value))
		{
			erreur+=" - l'adresse e-mail est invalide\n";
			document.getElementById("libelleemail").className="erreur";
			focusSet=updateFocus(email,focusSet);
		}
		
		if(erreur!="")
		{
			alert(erreur);
		}
		else
		{
			document.getElementById("finaliser").submit();
		}
	}
	catch(e)
	{
		alert("checkContact :\n"+e.message);
	}
}