function changebg ( champ , erreur )
{
	if ( erreur )
	{
		document.getElementById( champ.className + '_erreur' ).style.display = "inline";
	}
	
	else
	{
		document.getElementById( champ.className + '_erreur' ).style.display = "none";
	}
}

function check (champ)
{
	if ( champ.className == 'nom' )
	{
		if ( champ.value.length < 2 || champ.value.length > 15 )
		{
			changebg( champ, true );
			return false;
		}
		else
		{
			changebg( champ, false );
			return true;
		}
	}
	
	if ( champ.className == 'prenom' )
	{
		if ( champ.value.length < 1 || champ.value.length > 15 )
		{
			changebg( champ, true );
			return false;
		}
		else
		{
			changebg( champ, false );
			return true;
		}
	}
	
	if ( champ.className == 'email' )
	{
		var regex = /^[a-zA-Z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$/;
		if( !regex.test(champ.value) )
		{
			changebg( champ, true );
			oItalic = document.getElementsByTagName('i');
			oItalic[1].style.display = "none";
			return false;
		}
		else
		{
			changebg( champ, false );
			return true;
		}
	}
	
	if ( champ.className == 'categorie' )
	{
		if ( champ.value == 'blank' )
		{
			changebg( champ, true );
			return false;
		}
		else
		{
			changebg( champ, false );
			return true;
		}
	}
	
	if ( champ.className == 'message' )
	{
		if ( champ.value.length < 4 )
		{
			changebg( champ, true );
			return false;
		}
		else
		{
			changebg( champ, false );
			return true;
		}
	}
	
	if ( champ.className == 'captcha' )
	{
		if ( champ.value.length < 1 )
		{
			changebg( champ, true );
			return false;
		}
		else
		{
			changebg( champ, false );
			return true;
		}
	}
}

function check_all ()
{
	var form = document.getElementById('form_contact');
	oNom = check ( form.nom );
	oPrenom = check ( form.prenom );
	oEmail = check ( form.email );
	oCategorie = check ( form.categorie );
	oMessage = check ( form.message );
	oCaptcha = check ( form.captcha );
	
	if ( oNom && oPrenom && oEmail && oCategorie && oMessage && oCaptcha )
		return true;
	else
		return false;
}

function request_form_contact(id) 
{
	if ( check_all () )
	{
		var xhr   = getXhr();
		xhr.onreadystatechange = 
		function() 
		{
			if ( xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0) ) 
			{
				readData_contact ( xhr.responseText );
			}
		};
					
		xhr.open("POST", "js/formulaires.php" , true);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		var oForm = document.getElementById('form_contact');
		xhr.send( 'nom' + '=' + oForm.nom.value + '&prenom' + '=' + oForm.prenom.value + '&email' + '=' + oForm.email.value + '&categorie' + '=' + oForm.categorie.value + '&message' + '=' + oForm.message.value + '&captcha' + '=' + oForm.captcha.value+ '&id' + '=' + id);
	}
	
	else
		alert ('Veuillez remplir tous les champs !');
}

function readData_contact( oData ) 
{
	if ( oData == "L'email a bien été envoyé." )
		alert ( "L'email a bien été envoyé." );
		
	else if ( oData == "Le message n'a pu être envoyé." )
		alert ( "Le message n'a pu être envoyé, le webmaster a été contacté. Veuillez réessayer plus tard" );
	
	else if ( oData == "La réponse au calcul est incorrect." )
		alert ( "La réponse au calcul est incorrect." );
	else
		alert ( "Un problème est survenu lors de l'envoi du mail, le webmaster a été contacté. Veuillez réessayer plus tard" );
}
