var xmlHttp;
var langue;
var element;
var encouremail;
var messagemailnonvalide='entrer une adresse valide';

function email(element,strEmail,langues)
{
	this.element=element;
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	this.langue=langues;
	if(reg.test(strEmail))
	{	
		/*if(langue=='fr')
		{
		document.getElementById('strEmail').value="Votre courriel";	
		}
		else
		{
		document.getElementById('strEmail').value="Your email adress";	
		}*/
		subEmail(strEmail);
	}
	else
	{
		//document.getElementById('strEmail').style.color='black';
		document.getElementById('strEmail').style.backgroundColor='red';
		
		encouremail = document.getElementById('strEmail').value;
		if(langue=='fr')
		{
			document.getElementById('strEmail').value=messagemailnonvalide;
		}
		else
		{
			
			document.getElementById('strEmail').value='enter a valid address';
		}
	}


}

function subEmail(strEmail)
{
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="mailingListe.php";
	url=url+"?strEmail="+strEmail;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		//document.getElementById('strEmail').style.color='black';
		document.getElementById('strEmail').style.backgroundColor='#cd2817';
			
		if(langue=='fr')
		{
			document.getElementById('strEmail').value='COURRIEL AJOUT\311 !';
		}
		else
		{
			document.getElementById('strEmail').value='COURRIEL AJOUT\311 !';
		}
	} 
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
		
	}
	catch (e)
	{
		//Internet Explorer
		try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	}
	return xmlHttp;
}
function emailfocus(thisemail,message,messagemailnonvalide)
{
	if(thisemail.value == messagemailnonvalide)
	{
		thisemail.value =encouremail;
	}
	if(thisemail.value == message)
	{
		thisemail.value = '';
	}
	
}

function emailblur(thisemail,message)
{
	if(thisemail.value == '')
	{
		thisemail.value = message;
	}
}

var prenom;
var nom;
var courriel;
var ville;
var checkbox;
var commentaire;
function formulaire(initNom,initPrenom,initCourriel,initVille,initCommentaire)
{
	var isvalide=true;
	
	nom=document.getElementById('formnom').value;
    courriel=document.getElementById('formcourriel').value;
	ville=document.getElementById('formville').value;
	prenom=document.getElementById('formprenom').value;
	commentaire=document.getElementById('formcommentaires').value;
	
	if(checkIsInit(initNom,document.getElementById('formnom')))
	{
		isvalide=false;
	}	
	
	if(checkIsInit(initPrenom,document.getElementById('formprenom')))
	{
		isvalide=false;
	}	
	
	if(checkIsEmail(document.getElementById('formcourriel')))
	{
		isvalide=false;
	}
	
	if(checkIsInit(initVille,document.getElementById('formville')))
	{
		isvalide=false;
	}
	if(checkIsInit(initCommentaire,document.getElementById('formcommentaires')))
	{
		isvalide=false;
	}
	
	if(document.getElementById('formcheckbox').checked)
	{
		checkbox='oui';
	}
	else
	{
		checkbox='non';
	}
	
	
	if(isvalide)
	{
		envoieformulaire();
	}
}
function checkIsInit(valeurinit,champ)
{
	if(valeurinit==champ.value)
	{
		champ.style.backgroundColor='red';	
		return true;
	}
	else
	{
		champ.style.backgroundColor='#CD2817';
		return false;
	}
}
function checkIsEmail(champ)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(!reg.test(champ.value))
	{	
		champ.style.backgroundColor='red';	
		return true;
	}
	else
	{
		champ.style.backgroundColor='#CD2817';
		return false;
	}
}
function envoieformulaire()
{
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="babillard.php";
	url=url+"?&nom="+encodeURI(nom)+"&prenom="+encodeURI(prenom)+"&courriel="+encodeURI(courriel)+"&ville="+encodeURI(ville)+"&commentaire="+encodeURI(commentaire)+"&checkbox="+checkbox;
	xmlHttp.onreadystatechange=stateChangedForm;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
/*function nl2br (str,is_xhtml) {
    
	 var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '' : '<br>';

    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}*/
function formfocus(thisemail,message)
{
	
	if(thisemail.value == message)
	{
		thisemail.value = '';
	}
	
}
function stateChangedForm() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById('forminfo').style.display='none';
		document.getElementById('formulaireMerci').style.display='block';
		document.getElementById('formnom').value='Nom';
		document.getElementById('formcourriel').value='Courriel';
		document.getElementById('formville').value='Ville';
		document.getElementById('formprenom').value='Prénom';
		document.getElementById('formcommentaires').value='';
		
	} 
}
function voirFormulaire()
{
	document.getElementById('forminfo').style.display='block';
	document.getElementById('balbillard').style.display='none';
	document.getElementById('formulaireMerci').style.display='none';
}
function voirBalbillard()
{
	document.getElementById('forminfo').style.display='none';
	document.getElementById('formulaireMerci').style.display='none';
	document.getElementById('balbillard').style.display='block';
}

