var texte = 0; //on initialise la variable texte en variable globale pour pouvoir l'utilser ds tt le script

// Function for check the contact form
function emailCheck (emailStr) 
{

	var emailPat=/^(.+)@(.+)$/
	
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	
	var validChars="\[^\\s" + specialChars + "\]"
	
	var quotedUser="(\"[^\"]*\")"
	
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	
	var atom=validChars + '+'
	
	var word="(" + atom + "|" + quotedUser + ")"
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	/* The following pattern describes the structure of a normal symbolic
	   domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	
	/* Finally, let's start trying to figure out if the supplied address is
	   valid. */
	
	/* Begin with the coarse pattern to simply break up user@domain into
	   different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) 
	{
	  /* Too many/few @'s or something; basically, this address doesn't
		 even fit the general mould of a valid e-mail address. */
		writediv_email('<span style="color:#cc0000;font-size:10px;font-family:arial;"><b>"'+emailStr+'" :</b> Email address seems incorrect (check @ and .\'s)</span>');
		//alert("Email address seems incorrect (check @ and .'s)")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	
	// See if "user" is valid 
	if (user.match(userPat)==null) 
	{
		// user is not valid
		writediv_email('<span style="color:#cc0000;font-size:10px;font-family:arial;"><b>"'+emailStr+'" :</b> The username doesn\'t seem to be valid.</span>');
		//alert("The username doesn't seem to be valid.")
		return false
	}
	
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	   host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) 
	{
		// this is an IP address
		for (var i=1;i<=4;i++) 
		{
			if (IPArray[i]>255) 
			{
				writediv_email('<span style="color:#cc0000;font-size:10px;font-family:arial;"><b>"'+emailStr+'" :</b> Destination IP address is invalid!</span>');
				//alert("Destination IP address is invalid!")
				return false
			}
		}
		return true
	}
	
	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) 
	{
		writediv_email('<span style="color:#cc0000;font-size:10px;font-family:arial;"><b>"'+emailStr+'" :</b> The domain name doesn\'t seem to be valid !</span>');
		//alert("The domain name doesn't seem to be valid.")
		return false
	}
	
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) 
	{
	   // the address must end in a two letter or three letter word.
	   writediv_email('<span style="color:#cc0000;font-size:10px;font-family:arial;"><b>"'+emailStr+'" :</b> The address must end in a three-letter domain, or two letter country !</span>');
	   //alert("The address must end in a three-letter domain, or two letter country.")
	   return false
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2) 
	{
	   //var errStr="This address is missing a hostname!"
	   writediv_email('<span style="color:#cc0000;font-size:10px;font-family:arial;"><b>"'+emailStr+'" :</b> This address is missing a hostname !</span>');
	   //alert(errStr)
	   return false
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}

function file(fichier)
{
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return(false);
	xhr_object.open("GET", fichier, false);
	xhr_object.send(null);
	if(xhr_object.readyState == 4) 
		return(xhr_object.responseText);
	else 
		return(false);
}

function writediv_email(texte) //permet de specifier les renseignements sur l'email saisi(AJAX)
{
	document.getElementById('emailbox').innerHTML = texte;
}

function verifEmail(email, param)
{
	if(email != '')
	{
		if(!emailCheck(email))
		{
			writediv_email('<span style="color:#ffa217;font-size:13px;font-family:tahoma;font-weight:bold;">"'+email+'" : email format is invalid !</span>');
			return (false);
		}
		else
		{
			writediv_email(' ');
			return (false);
		}
	}
	else
	{
		writediv_email(' ');
		return (false);
	}
}

function checkFormContact()
{
	var verification = 1;
	
	var first_name = document.forms["contact"].elements["first_name"];
	var last_name = document.forms["contact"].elements["last_name"];
	var email_address = document.forms["contact"].elements["email_address"];
	var select_category = document.forms["contact"].elements["select_category"];
	var message = document.forms["contact"].elements["message"];
	
	
	if (first_name.value.length == 0)
	{
		first_name.className = "empty_input";
		verification = 0;
	}
	else
	{
		first_name.className = "form_align";
	}
	
	if (last_name.value.length == 0)
	{
		last_name.className = "empty_input";
		verification = 0;
	}
	else
	{
		last_name.className = "form_align";
	}
	
	if (email_address.value.length == 0)
	{
		email_address.className = "empty_input";
		verification = 0;
	}
	else
	{
		email_address.className = "form_align";
	}
	
	if (select_category.value.length == 0)
	{
		select_category.className = "empty_input";
		verification = 0;
	}
	else
	{
		select_category.className = "form_align";
	}
	
	if (message.value.length == 0 || message.value == "  ")
	{
		message.className = "empty_input_area";
		verification = 0;
	}
	else
	{
		message.className = "form_align_area";
	}
	
	
	if (verification == 0) //Si tous les champs ne sont pas tous renseignes
	{
		document.getElementById('intro_contact').style.display = "none";
		document.getElementById('miss_contact').style.display = "block";
		return(false);
	}
	else 
	{
		document.forms.contact.submit();
		return(true);
	}
}

