var xhr; 
var loginform=null;

function ajax_post(adr, params,rep)
{
	
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();   }
          catch (e3) {  xhr = false;   }
        }
     }

     xhr.onreadystatechange  =rep;
     xhr.open("POST", adr,  true); 
     xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");   
     xhr.send(params); 	
}


function clear_text(cible,message)
{
	if(cible.value==message) cible.value="";
}

function set_text(cible,message)
{
	if(cible.value=="") cible.value=message;
}


function change_imgcode(cible)
{
	document.getElementById(cible).src="";	
	document.getElementById(cible).src="./script/imgcode.php?cible="+cible+"&date="+escape(new Date());
}

function change_img(cible,imgurl)
{
	document.getElementById(cible).src=imgurl;	
}

function bonmail(lemail)
{
	var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$','i');	
	if(reg.test(lemail))
	{
		return(true);
	}
	else
	{
		return(false);
	}
}

function write_html(htmltext,bal_id,clr,is_visible,add_e)
{
   var my_bal=document.getElementById(bal_id);
   
   if(htmltext!="") my_bal.style.visibility=is_visible;
   if(htmltext!="") my_bal.style.color=clr;
   if(add_e) my_bal.innerHTML+=htmltext; else my_bal.innerHTML=htmltext;
}

function verif_champ(thechamp_id, err_id, err_msg,add_e)
{
   var paramst=document.getElementById(thechamp_id).value;
   if(paramst==""){ 
     write_html(err_msg,err_id,"#CC0000","",add_e);
   } 
   
   return(paramst);
}

function add_param(param,param_name,param_val)
{
	if(param!="")
		param+="&";
	
	param+=param_name;
	param+="=";
	param+=encodeURIComponent(param_val);

	return(param);
}

function send_mail()
{
   write_html("Envoie en cours...","sending","#0000CC","visible",false);
   write_html("","mailReponse1","","",false);

   var params="";
   var paramst;
   var is_ok=true;


  if((paramst=verif_champ("mailReponse", "mailReponse1", "-Champs 'email' non sp&eacute;cifi&eacute;!<br/>",true))=="")   
    is_ok=false;

  if(is_ok)
  { 	
    if(!bonmail(paramst)){ 
      write_html("-Veuillez indiquer une adresse e-mail valide!<br/>","mailReponse1","","",true); 
      is_ok=false;
    } 
  }
  params=add_param(params,"mailReponse",paramst);

  if((paramst=verif_champ("expediteur", "mailReponse1", "-Champs 'Nom-prenom' non sp&eacute;cifi&eacute;!<br/>",true))=="")   
    is_ok=false;
  params=add_param(params,"expediteur",paramst);

  paramst=document.getElementById("your_org").value;
  params=add_param(params,"your_org",paramst);

  if((paramst=verif_champ("your_tel", "mailReponse1", "-Champs 'T&eacute;l&eacute;phone' non sp&eacute;cifi&eacute;! <br/>",true))=="")   
    is_ok=false;
  params=add_param(params,"your_tel",paramst);
 
  if((paramst=verif_champ("sujet", "mailReponse1", "-Champs 'objet' non sp&eacute;cifi&eacute;! <br/>",true))=="")   
    is_ok=false;
  params=add_param(params,"sujet",paramst);

  if((paramst=verif_champ("message", "mailReponse1", "-Champs 'message' non sp&eacute;cifi&eacute;! <br/>",true))=="")
    is_ok=false;
  params=add_param(params,"message",paramst);
  
   if(is_ok)
   {
	 ajax_post("./script/envoyer_mail.php", params,send_mail_msg);
   }else{
     write_html("Echec d'envoie! ","sending","#CC0000","",false);	   
     //write_html(err_msg,"mailReponse1","","",false);	   
   }
}

function send_mail_msg()
{
     if(xhr.readyState  == 4)
     {
              if(xhr.status  == 200){ 
			     var rep=xhr.responseText;
				 if(rep==""){
				   write_html("Message envoy&eacute;","sending","#00CC00","",false); 
				 }else{
				   write_html("Probl&egrave;me lors de l'envoie du mail!","sending","#CC0000","",false);
				   //document.getElementById("mailReponse1").innerHTML  = rep;
				 }
			  }else{ 
				   write_html("Probl&egrave;me lors de l'envoie du mail!!","sending","#CC0000","",false); 
			  }
			 
     }
}


