if (document.images){
  pic1 = new Image(238,230);
  pic1.src = path + "images/mail/envoi.jpg";
  pic2 = new Image(50,50);
  pic2.src = path + "images/mail/fleche_droite.png";
}

function showwaitinggMail(div)
{																	 
    document.getElementById(div).innerHTML = "<div class='envoi'><div id='envoiMail'></div></div>";
}

function inbox(user, start)
{	
	showwaiting("waitMail");
	$.ajax({
		type: "POST", 
		url: path + "mail/inbox.php", 
		data: "user=" + user + "&start=" + start,
		complete: function(data)
		{	
			leselect = data.responseText;
			tab = new Array();
			tab = leselect.split("###!!!###");
			$("#inboxx").html(tab[1]);
			if(tab[0] > 0) 
			{
				$("#mmail").html(tab[0]);
				$("#countMail").html(" (" + tab[0] + ")");
			}
			hidewaiting("waitMail");			
			$("#head_inbox").addClass("active");
			$("#head_write").removeClass("active");
			$("#head_sent").removeClass("active");				
		}
	});	
}
function sent(user, start)
{		
	showwaiting("waitMail");
	$.ajax({
		type: "POST", 
		url: path + "mail/sent.php", 
		data: "user=" + user + "&start=" + start,
		complete: function(data)
		{	
			leselect = data.responseText;
			document.getElementById('inboxx').innerHTML = leselect;
			hidewaiting("waitMail");
			
			$("#head_inbox").removeClass("active");
			$("#head_write").removeClass("active");
			$("#head_sent").addClass("active");			
		}
	});			 	  	   
}
function write2(user){				 	   
    
	showwaiting("waitMail");
	$.ajax({
		type: "POST", url: path + "mail/write.php", 
		data: "user=" + user,
		complete: function(data)
		{	
			leselect = data.responseText;
			$("#inboxx").html(leselect);
			hidewaiting("waitMail");
			
			$("#head_inbox").removeClass("active");
			$("#head_sent").removeClass("active");
			$("#head_write").addClass("active");
			
			elasticTextArea("texte");
		}
	});
}
function inboxLecture(id, user, lu, start, box, box2)
{			
	showwaiting("waitMail");
	if(lu == 0) setMailLu();
	$.ajax({
		type: "POST", 
		url: path + "mail/inboxLecture.php", 
		data: "idMail=" + id + "&moi=" + user + "&start=" + start + "&box=" + box + "&box2=" + box2,
		complete: function(data)
		{	
			leselect = data.responseText;
			
			$('#inboxx').html(leselect);
			elasticTextArea("texte");
			hidewaiting("waitMail");		
		}
	});		
}

function sentLecture(id, user, start)
{		  						  
    var xhr = getXhr();
    xhr.onloading = showwaiting("waitMail");
    xhr.onreadystatechange = function()
    {
        if(xhr.readyState == 4 && xhr.status == 200)
        {
            leselect = xhr.responseText;
            document.getElementById('inboxx').innerHTML = leselect;
			xhr.onComplete = hidewaiting("waitMail");
        }
    }
    xhr.open("POST", path + "mail/sentLecture.php",true);
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.send("idMail=" + id + "&moi=" + user + "&start=" + start);
}

//Répondre à un message--------------------------------------------------------------
function inboxRepondre()
{	
	$("#bl").removeClass("hiddenn");
	$("#foot").hide();
}    
function inboxRepondreMail(idMail, moi)
{
	var mail = encodeURIComponent($("#texte").val());
	if(mail != "")
	{
		showwaiting("error");
		$("#buttonn").hide();
		$.ajax({
			type: "POST", 
			url: path + "mail/inboxRepondre.php", 
			data: "idMail=" + idMail + "&moi=" + moi + "&mail=" + mail,
			complete: function(data)
			{	
				leselect = data.responseText;
				$("#bl").hide();
				$("#new").hide();
				$("#new").html(leselect);
				if (checkIt('msie')) $('#new').show(500);
				else $('#new').slideDown(500);
			}
		});	
	}
}
//fin de répondre------------------------------------------------------------------------------------------------------

function deleteMaill(id, user, del, start)
{
    var xhr = getXhr();
    xhr.onloading = showwaiting("waitMail");
    xhr.onreadystatechange = function()
    {
        if(xhr.readyState == 4 && xhr.status == 200)
        {	  
			leselect = xhr.responseText;  
            window.location.href = path + 'mail/inbox/'
        }
    }							
    xhr.open("POST", path + "mail/ajaxDeleteMail.php",true);
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.send("id=" + id + "&del=" + del + "&start=" + start + "&moi=" + user);
}
function setMailLu()
{
	var chiffre = $("#mmail").html();
	chiffre--;
    if(chiffre > 0)	
	{
		$("#mmail").html(chiffre);
		$("#countMail").html(" (" + chiffre + ")");
	}
    else {
        chiffre = "";
        $("#mmail").html(chiffre);
		$("#countMail").html("");
        $("#mmmail").removeClass();        
        $("#mmmail").addClass("mail_zero_pt");        
    }
	
}	
function writeMail(moi)
{
	var mail = encodeURIComponent($("#texte").val());	 
	var sujet = encodeURIComponent(document.getElementById("sujet").value);	
	var dest = $("#demo-input-facebook-theme").val();
	if(mail != "" && dest != "")
	{
		showwaiting("error");
		$("#buttonn").hide();
		$.ajax({
			type: "POST", 
			url: path + "mail/ajaxWriteMail.php", 
			data: "mail=" + mail + "&dest=" + dest + "&moi=" + moi + "&sujet=" + sujet,
			complete: function(data)
			{	
				leselect = data.responseText;
				$("#bl").hide();
				$("#new").hide();
				$("#new").html(leselect);
				if (checkIt('msie')) $('#new').show(500);
				else $('#new').slideDown(500);				
			}
		});	
	}
}

function lireAutreMail(idMail, date, dateReelle)
{
	if(document.getElementById(idMail).style.display == "none") 
	{
		document.getElementById(idMail).style.display = "block";
		document.getElementById("date_" + idMail).innerHTML = dateReelle;
	}
	else 
	{
		document.getElementById(idMail).style.display = "none";
		document.getElementById("date_" + idMail).innerHTML = date;
	}
}

function old()
{
	//$('#messacheCache').hide();
	$('#messacheCache').removeClass("hidden");
	//if (checkIt('msie')) $('#messacheCache').show(500);
	//else $('#messacheCache').slideDown(500);
	
	$("#old").html("");
	$("#old").removeClass("anciens");	
}

function retour(box, start, user)
{
	var b = (box == "1") ? inbox(user, start) : sent(user, start);
}

