﻿// Fonction verifiant la validite d'une adresse mail
function verifmail(email) {
	var proto  = "(mailto:)?";
	var usr    = "([a-zA-Z0-9][a-zA-Z0-9_.-]*|\"([^\\\\\x80-\xff\015\012\"]|\\\\[^\x80-\xff])+\")";
	var domain = "([a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5}";
	var regex  = "^" + proto + "?" + usr + "\@" + domain + "$";
	
	var rgx    = new RegExp(regex);
	return rgx.exec(email) ? true : false;
}


// Verification du remplissage des champs du formulaire "Contact"
function valider(formulaire)
{	
	if(formulaire.nom.value=='') // 1
	{
		alert("Veuillez entrer votre nom.");
		formulaire.nom.focus();
		return false;
	}
	else if(formulaire.mail.value=='') // 1
	{
		alert("Veuillez entrez votre adresse mail.");
		formulaire.mail.focus();
		return false;
	}
	else if(!verifmail(formulaire.mail.value))
    { 
        alert ('Veuillez entrez une adresse mail valide'); 
        formulaire.mail.focus(); 
        return false; 
    }
	else if(formulaire.message.value=='') // 1
	{
		alert("Veuillez entrez votre message.");
		formulaire.message.focus();
		return false;
	}
	else
	{
		return true;
	}
}


function ouvrirfermer(events)	{ if(document.getElementById(events).style.display=="block")	{	document.getElementById(events).style.display="none";	}
																				else			{	document.getElementById(events).style.display="block";	}
								}

function ouvrir(events)	{ 	document.getElementById(events).style.display="block";	}
function fermer(events)	{ 	document.getElementById(events).style.display="none";	}

function affichage_alertes(textealerte)	{	document.getElementById('affichage_alertes').innerHTML = textealerte;
											document.getElementById('affichage_alertes').style.display = 'block';
										}







function ajoutlien()
{
		var link = prompt('Saisissez l\'URL après avoir sélectionné un ou plusieurs mots','http://');
		
		if(link=='http://' || link=='' || link==null)
		{
			
		}
		else
		{
				if (confirm("La cible du lien s'ouvre t'elle dans une nouvelle fenêtre?"))
					var cible = "&cible=new";
				else 
					var cible = "&cible=no";
				insertion('[URL='+link+cible+']','[/URL]');		
		}
				
}

function insertion(repdeb, repfin) 
{
	  var input = document.forms['form_caravaning'].elements['texte'];
	  input.focus();
	  /* pour l'Explorer Internet */
	  if(typeof document.selection != 'undefined') 
	  {
		    /* Insertion du code de formatage */
		    var range = document.selection.createRange();
		    var insText = range.text;
		    range.text = repdeb + insText + repfin;
		    /* Ajustement de la position du curseur */
		    range = document.selection.createRange();
		    if (insText.length == 0) 
		    {
		      range.move('character', -repfin.length);
		    } 
		    else 
		    {
		      range.moveStart('character', repdeb.length + insText.length + repfin.length);
		    }
		    range.select();
	    }
		else if(typeof input.selectionStart != 'undefined')  /* pour navigateurs plus récents basés sur Gecko*/
		{
			    /* Insertion du code de formatage */
			    var start = input.selectionStart;
			    var end = input.selectionEnd;
			    var insText = input.value.substring(start, end);
			    input.value = input.value.substr(0, start) + repdeb + insText + repfin + input.value.substr(end);
			    /* Ajustement de la position du curseur */
			    var pos;
			    if (insText.length == 0) 
			    {
			      	pos = start + repdeb.length;
			    } 
			    else 
			    {
			      	pos = start + repdeb.length + insText.length + repfin.length;
			    }
			    input.selectionStart = pos;
			    input.selectionEnd = pos;
		  }
		  /* pour les autres navigateurs */
		  else
		  {
		    /* requête de la position d'insertion */
		    var pos;
		    var re = new RegExp('^[0-9]{0,3}$');
		    while(!re.test(pos)) 
		    {
		      	pos = prompt("Insertion à la position (0.." + input.value.length + "):", "0");
		    }
		    if(pos > input.value.length) 
		   	{
		      	pos = input.value.length;
		    }
		    /* Insertion du code de formatage */
		    var insText = prompt("Veuillez entrer le texte à formater:");
		    input.value = input.value.substr(0, pos) + repdeb + insText + repfin + input.value.substr(pos);
		}
}




