function ajouterPieceJointe(id, img, type) {
	if ($('pj1').value == '') {
		$('pj1').value = id;
		$('type_pj1').value = type;
		$('img_pj1').innerHTML = '<img src="'+ img +'" height="50" width="70" />';
	}
	else if ($('pj2').value == '') {
		$('pj2').value = id;
		$('type_pj2').value = type;
		$('img_pj2').innerHTML = '<img src="'+ img +'" height="50" width="70" />';
	}
	else if ($('pj3').value == '') {
		$('pj3').value = id;
		$('type_pj3').value = type;
		$('img_pj3').innerHTML = '<img src="'+ img +'" height="50" width="70" />';
	}
	else if ($('pj4').value == '') {
		$('pj4').value = id;
		$('type_pj4').value = type;
		$('img_pj4').innerHTML = '<img src="'+ img +'" height="50" width="70" />';
	}
	else if ($('pj5').value == '') {
		$('pj5').value = id;
		$('type_pj5').value = type;
		$('img_pj5').innerHTML = '<img src="'+ img +'" height="50" width="70" />';
	}
	else {
		alert(tradTropPj);
	}
}

// TODO: a recoder avec le template qui a �t� fait
function checkRecipient() {
	if ( typeof to_user_tip == "undefined" || $('to_user').value == to_user_tip )
		return false;
	else
		return true;
}

function autocompleteTip(e) {
	/* si le champ est vide et hors focus ajoute le tip*/
	if ( e.type=='blur' && $('to_user').value == '' )
	{
		$('to_user').value = to_user_tip
	}
	/* si le champ a le focus et contient le tip, efface le tip */
	else if ( e.type=='focus' && $('to_user').value != '' )
	{
		/* first time */
		if (typeof to_user_tip =="undefined") {
			to_user_tip = $('to_user').value
			$('to_user').value = ''
		}
		else if ( $('to_user').value == to_user_tip)
		{
			$('to_user').value = ''
		}
	}
}

function retirerPj(idPj) {
	$(idPj).value = '';
	$('img_'+ idPj).innerHTML = '';
	$('type_'+ idPj).innerHTML = '';
}
	
function envoyerMessage() {
	if ($('to_user').value != '' && checkRecipient() == true ) {
		var args="navigation=voir&sujet="+ $('sujet').value +"&to_user="+ $('to_user').value +"&contenu="+ $('contenu').value +"&pj1="+ $('pj1').value +"&pj2="+ $('pj2').value +"&pj3="+ $('pj3').value +"&pj4="+ $('pj4').value +"&pj5="+ $('pj5').value +"&type_pj1="+ $('type_pj1').value +"&type_pj2="+ $('type_pj2').value +"&type_pj3="+ $('type_pj3').value +"&type_pj4="+ $('type_pj4').value +"&type_pj5="+ $('type_pj5').value;
		navigationAjax('messagerie-ecrire',args);
	}
	else
	{
		loadPopup('send_message')
	}
}

function repondreMessage() {
	var args="navigation=voir&sujet="+ $('sujet').value +"&idDestinataire="+ $('idDestinataire').value +"&idThread="+ $('idThread').value +"&contenu="+ $('contenu').value +"&pj1="+ $('pj1').value +"&pj2="+ $('pj2').value +"&pj3="+ $('pj3').value +"&pj4="+ $('pj4').value +"&pj5="+ $('pj5').value +"&type_pj1="+ $('type_pj1').value +"&type_pj2="+ $('type_pj2').value +"&type_pj3="+ $('type_pj3').value +"&type_pj4="+ $('type_pj4').value +"&type_pj5="+ $('type_pj5').value;
	navigationAjax('messagerie-ecrire',args, "mailbox_message_info", "voirThread(" + $('idThread').value + ", 'reception')");
}
	
function voirThread(idThread, boite) {
	ajaxCall('/voir-thread-ajax.html', "action=voir&thread="+ idThread +"&boite="+ boite, "thread_"+ idThread, "checkMail()");
}
	
function fermerThread(idThread, boite) {
	ajaxCall('/voir-thread-ajax.html', "action=fermer&thread="+ idThread +"&boite="+ boite, "thread_"+ idThread);
}

function checkMail() {
	ajaxCall('/check-mail.html', '', '', "unreadMailNb=xhr.responseText.trim(); updateMailStatus(unreadMailNb)");
}

function updateMailStatus(unreadMailNb)
{
	if (myUnreadMailNb=='' || myUnreadMailNb != unreadMailNb)
	{
		myUnreadMailNb=unreadMailNb
		if ( unreadMailNb > 0 ) {
			$('messagerie-reception_sub_unreadMail_txt').set('html', unreadMailNb );
			$('messagerie-reception_sub_unreadMail').removeClass('hidden');
		}
		else {
			$('messagerie-reception_sub_unreadMail').addClass('hidden');
		}
	}
}

var myUnreadMailNb;
window.addEvent('domready', function(){
	checkMail()
	setInterval('checkMail()', 30000);
});