﻿/* ::::::::::::::::::::::::::::::::::::::::::
   ::::::::::::::::::::::::::::::::::::::::::
   GESTION DU POPUP MESSAGE
   ::::::::::::::::::::::::::::::::::::::::::
   ::::::::::::::::::::::::::::::::::::::::::
*/

var array_mail= new Array();

function infos_mail(id,destinator_id,author_id,expd_picture,expd,object,date,txt,type,doc_url){
	this.id = id;
	this.destinator_id = destinator_id;
	this.author_id = author_id;
	this.expd_picture = expd_picture;
	this.expd = expd;
	this.object = object;
	this.date = date;
	this.txt = txt;
	this.type= type;
	this.doc_url = doc_url;

}


var current_action_mail;
var current_destinator_id;
var current_destinator_login;

function send_from_member(destinator_id,destinator_login){
	current_destinator_id= destinator_id;
	current_destinator_login=destinator_login;
	OpenCloseMail(1,'send_from_member');
}

function send_from_my_mail(nb_btn){
	current_destinator_id= '';
	OpenCloseMail(nb_btn,'send_from_my_mail');
}
	
function OpenCloseMail(nb_btn, action){
	current_action_mail=action;
	
	HidePage('mail');
	if(current_action_mail == 'send_from_member' || current_action_mail == 'send_from_my_mail' || current_action_mail == 'read' ){
		
		//pour la page membre
		if(current_action_mail == 'send_from_member'){
			
			for(i=1; i<=4;i++){
				document.getElementById("send_mail_"+i).style.display='none';	
				}
			document.getElementById("send_mail_"+nb_btn).style.display='block';
			for(i=1; i<=3;i++){
				document.getElementById("btn_send_mail_"+i).firstChild.style.visibility='visible';
				//document.getElementById("options_my_mail_"+i).style.display='block';
				}
			
			document.getElementById("btn_send_mail_"+nb_btn).firstChild.style.visibility='hidden';
		}

		//pour la page mail et les alertes
		if(current_action_mail == 'send_from_my_mail' || current_action_mail=="read" ){
			
			//on cache la navigation
			document.getElementById("mail_tool").style.display='none';
			for(i=1; i<=4;i++){
				document.getElementById("send_mail_"+i).style.display='none';
				}
			document.getElementById("send_mail_"+nb_btn).style.display='block';
				
			if(current_action_mail == 'send_from_my_mail'){	
			
				//On vide les champs.
				document.getElementById("mail_member_login_"+nb_btn).style.display = 'inline';
				document.getElementById("mail_member_login_"+nb_btn).value = '';
				document.getElementById("my_friend_list_"+nb_btn).options[0].selected=true;
				document.getElementById("send_mail_"+nb_btn).style.display='block';
				
				
				for(i=1; i<=3;i++){	

					document.getElementById("options_my_mail_"+i).style.display='block';	
				}
			}
		}

		if(current_action_mail == 'send_from_member' || current_action_mail == 'send_from_my_mail' ){
		//On vide les champs.
		document.getElementById("mail_title").value = '';
		document.getElementById("mail_title_send_file").value = '';
		document.getElementById("mail_title_audio").value = '';
		document.getElementById("mail_body").value = '';
		document.getElementById("mail_body_send_file").value = '';
		document.getElementById("send_info").innerHTML  = '';
		document.getElementById("send_info_send_file").innerHTML  = '';
		document.getElementById("send_info_audio").innerHTML  = '';
		}
	}
		
	if(current_action_mail == 'repply'){


		if (document.getElementById("mail_list")) {
				document.getElementById("mail_member_login_1").style.display = 'none';
			}
		document.getElementById("send_mail_4").style.display='none';		
		document.getElementById("mail_tool").style.display='block';
		for(i=1; i<=3;i++){
			document.getElementById("send_mail_"+i).style.display='none';
			document.getElementById("btn_send_mail_"+i).firstChild.style.visibility='visible';
			if (document.getElementById("mail_list")) {
					document.getElementById("options_my_mail_"+i).style.display='none';
				}
				
		}
		document.getElementById("send_mail_"+nb_btn).style.display='block';
		document.getElementById("btn_send_mail_"+nb_btn).firstChild.style.visibility='hidden';
		//
		document.getElementById("mail_body").value = '';
		document.getElementById("mail_body_send_file").value = '';
		



	}
	
	if(nb_btn==1){
		if (document.getElementById("mail_body").value == ''){
			document.getElementById("mail_body").focus();
		}
		if (document.getElementById("mail_title").value == ''){
			document.getElementById("mail_title").focus();
		}
		//pour la page mail
		if(document.getElementById("options_my_mail_1")){
			if (document.getElementById("mail_member_login_1").style.display == 'block' && document.getElementById("mail_member_login").value == ''){
				document.getElementById("mail_member_login").focus();
			}
		}
	}
	
}



function read_mail(index){
	current_destinator_id = array_mail[index].author_id;
	current_destinator_login = array_mail[index].expd;
	current_index_mail=index;

	OpenCloseMail(4,'read');
	document.getElementById("send_info").innerHTML  = MISC_MAIL_SEND_INFO+array_mail[index].expd;
	document.getElementById("send_info_send_file").innerHTML  = MISC_MAIL_SEND_INFO+array_mail[index].expd;
	document.getElementById("send_info_audio").innerHTML  = MISC_MAIL_SEND_INFO+array_mail[index].expd;
	document.getElementById("read_picture").src  = array_mail[index].expd_picture;
	document.getElementById("read_exp").innerHTML  = array_mail[index].expd;
	document.getElementById("read_date").innerHTML = array_mail[index].date;
	document.getElementById("read_objet").innerHTML = array_mail[index].object;
	document.getElementById("read_txt").innerHTML  = array_mail[index].txt;
	document.getElementById("mail_title").value = 'RE :'+ array_mail[index].object;
	document.getElementById("mail_title_send_file").value = 'RE :'+ array_mail[index].object;
	document.getElementById("mail_title_audio").value = 'RE :'+ array_mail[index].object;
	document.getElementById("nb_mail").innerHTML   = Number(index)+'/'+Number(array_mail.length-1);
	//affichage d'un mail type picture

	

	if(array_mail[index].type =='picture'){
		document.getElementById("picture_mail").style.display='block';
		document.getElementById("picture_mail").src=array_mail[index].doc_url;
	}
	else{
		if(document.getElementById("picture_mail")){
		document.getElementById("picture_mail").style.display='none';
		}
	}
	
	if(index==0){
		document.getElementById("pagination_mail").style.display='none';
		mail_change_read_status(array_mail[index].id);
		}
		else{
			document.getElementById("pagination_mail").style.display='block';
			}
	//change le status du mail nonlu 
	
	
	if (index!=0 && document.getElementById("mail_"+index).parentNode.parentNode.className != 'read'){
		
			
		if(document.getElementById("mail_"+index).parentNode.parentNode.className=='new'){
			document.getElementById("mail_"+index).parentNode.parentNode.className='read';
			mail_change_read_status(array_mail[index].id);
		}
		if(document.getElementById("mail_"+index).parentNode.parentNode.className=='not_read_new'){
			document.getElementById("mail_"+index).parentNode.parentNode.className='not_read';
			mail_change_read_author_status(array_mail[index].id);
		}
	}
}
function next_mail(){
	if(current_index_mail<array_mail.length-1){
		read_mail(current_index_mail+1);
	}
	else{
		read_mail(1);
	}
	
}
function prev_mail(){
	if(current_index_mail>1){
		read_mail(current_index_mail-1);
	}
	else{
		read_mail(array_mail.length-1);
	}
}




/* ::::::::::::::::::::::::::::::::::::::::::
   ::::::::::::::::::::::::::::::::::::::::::
   SELECTION DE MAIL
   ::::::::::::::::::::::::::::::::::::::::::
   ::::::::::::::::::::::::::::::::::::::::::
*/
var array_mail_suppr_id= new Array();


function SelectMail(index){
	document.getElementById('mail[]').value='';
	//selection
	if(document.getElementById("mail_"+index).className==""){
		document.getElementById("mail_"+index).className="mail_selected";
		//on remplie un tableau
		array_mail_suppr_id.push(array_mail[index].id);
		}
	//deselection
	else{
		document.getElementById("mail_"+index).className="";
		var new_array_mail_suppr_id= new Array();
		for(i=0; i<array_mail_suppr_id.length;i++){
			if(array_mail_suppr_id[i]!=array_mail[index].id){
				new_array_mail_suppr_id.push(array_mail_suppr_id[i]);
				}
			}
		array_mail_suppr_id=new_array_mail_suppr_id;

		}
		for(i=0; i<=array_mail_suppr_id.length;i++){
			document.getElementById('mail[]').value=array_mail_suppr_id;
			}

}
function SelectAllMail(){
	//rinitialisation du tableau
	array_mail_suppr_id=new Array();
	document.getElementById('mail[]').value='';
	//on selectionne tout
	if(document.getElementById("select_all").className==""){
		document.getElementById("select_all").className="mail_selected";
		for(i=1; i<array_mail.length;i++){
			
			document.getElementById("mail_"+i).className="mail_selected";
			array_mail_suppr_id.push(array_mail[i].id);
			}
		
				
		for(i=0; i<=array_mail_suppr_id.length;i++){
			document.getElementById('mail[]').value=array_mail_suppr_id;
			}
	}
	//on deselectionne tout
	else{
		document.getElementById("select_all").className="";
		for(i=1; i<array_mail.length;i++){
			document.getElementById("mail_"+i).className="";
			
			}
		}

	}

function Submit_suppr_mail(){
	if(array_mail_suppr_id.length != 0){
		Confirm(MISC_TITLE_SUPPR_MAIL,MISC_CONFIRM_SUPPR_MAIL, 'mail_list_form');

	}else{		
		Alerte(MISC_TITLE_SUPPR_MAIL, MISC_ALERTE_SUPPR_MAIL);
	}
}


function Submit_restore_mail(){
	if(array_mail_suppr_id.length != 0){
		document.forms['mail_list_form'].elements['do_action'].value='mail_restore';
		Confirm(MISC_TITLE_RESTORE_MAIL,MISC_CONFIRM_RESTORE_MAIL, 'mail_list_form');
	}else{
		Alerte(MISC_TITLE_RESTORE_MAIL,MISC_ALERTE_RESTORE_MAIL );
	}
}

function setMailTitle(swf){
	if(swf=='audio'){	
		var monswf = window.document.audio_recorder;
		mail_title=document.getElementById("mail_title_audio").value;
		if(current_action_mail == 'repply'){
			monswf.SetVariable("ref",current_destinator_id);
		}
		}
	if(swf=='send_file'){	
		var monswf = window.document.send_file;
		mail_title=document.getElementById("mail_title_send_file").value;
		}
	monswf.SetVariable("mail_title",mail_title);

}

function setMailBody(swf){
	if(swf=='audio'){	
		var monswf = window.document.audio_recorder;
		mail_body=document.getElementById("mail_body_audio").value;

		}
	if(swf=='send_file'){	
		var monswf = window.document.send_file;
		mail_body=document.getElementById("mail_body_send_file").value;
		}
	
	monswf.SetVariable("mail_body",mail_body);
	//Passage de variable au swf
	if(current_action_mail == 'repply'){
		monswf.SetVariable("mail_destinator_id",current_destinator_id);
	}
}


function setMailPseudo(swf){
	if(swf=='no_swf'){
		document.getElementById("my_friend_list_1").options[0].selected=true;
		}
	else{
		if(swf=='audio'){	
		var monswf = window.document.audio_recorder;
		mail_destinator_login=document.getElementById("mail_member_login_3").value;
		document.getElementById("my_friend_list_3").options[0].selected=true;
		monswf.SetVariable("ref",mail_destinator_login);
		}
	if(swf=='send_file'){	
		var monswf = window.document.send_file;
		mail_destinator_login=document.getElementById("mail_member_login_2").value;
		document.getElementById("my_friend_list_2").options[0].selected=true;
		monswf.SetVariable("mail_destinator_login",mail_destinator_login);
		}
	
	}
	
	
}
function selectMailPseudo(index){
	
	document.getElementById("mail_member_login_"+index).value="";
	
	if(index==2){		
		var monswf = window.document.send_file;
		mail_destinator_login=document.getElementById("my_friend_list_2").value;
		monswf.SetVariable("mail_destinator_login",mail_destinator_login);
	}
	if(index==3){		
		var monswf = window.document.audio_recorder;
		mail_destinator_login=document.getElementById("my_friend_list_3").value;
		monswf.SetVariable("ref",mail_destinator_login);
	}
}
