var dom = (document.getElementById)? true:false;
var ns4 = (document.layers)? true:false;
var ie4 = (document.all)? true:false;
var dd_mode="";

function start() {			//Init des variables,des Divs-Layers, et du onmousedown
 	if (ie4) {
		wind.onmousedown=beginDrag;
	}else if (dom) {
		document.getElementById("wind").addEventListener("mousedown",beginDrag, false);
	}
}

function doDrag(e) {			// Déplacement des Divs-Layers
	if (ie4) {
		var difX=event.clientX-window.lastX;
	   var difY=event.clientY-window.lastY;
		var newX1 = parseInt(wind.style.left)+difX;
	   var newY1 = parseInt(wind.style.top)+difY;
	   wind.style.left=newX1+"px";
	   wind.style.top=newY1+"px";
	   if(parseInt($('#div_viewer').css('top')) > 0){
	   	div_viewer.style.left=newX1+"px";
	   	div_viewer.style.top=newY1+20+"px";
	   }
	   window.lastX=event.clientX;
	   window.lastY=event.clientY;
	}else if (dom) {
		var difX=e.clientX-window.lastX;
	   var difY=e.clientY-window.lastY;
		var newX1 = parseInt(document.getElementById("wind").style.left)+difX;
	   var newY1 = parseInt(document.getElementById("wind").style.top)+difY;
	   document.getElementById("wind").style.left=newX1+"px";
	   document.getElementById("wind").style.top=newY1+"px";
	   if(parseInt($('#div_viewer').css('top')) > 0){
		   document.getElementById("div_viewer").style.left=newX1+"px";
		   document.getElementById("div_viewer").style.top=newY1+20+"px";
		 }
	   window.lastX=e.clientX;
	   window.lastY=e.clientY;
	}
}

function beginDrag(e) {		// Paramétrage du déplacement des Divs-Layers et des onmousemove & onmouseup
	start_follower = false;
	if (ie4) {
		window.lastX=event.clientX;
	   window.lastY=event.clientY;
	   document.onmousemove=doDrag;
	   document.onmouseup=endDrag;
//		if (event.srcElement.id=="wind"){
//			Récup de l'Id d'un élément sous IE
//		}
	}else if (dom) {
		window.lastX=e.clientX;
	   window.lastY=e.clientY;
	   window.onmousemove=doDrag;
	   window.onmouseup=endDrag;
		myattr=e.target.getAttribute("ID");
//		if(myattr=="wind") {
//			Récup de l'ID d'un élément sous NS6
//		}
	}
}

function endDrag(e) {		// Stabilisation des Divs-Layers dans la fenetre
	start_follower = true;
	 if (ie4 || ns4) {
	 	document.onmousemove=null;
	 }else if (dom) {
		window.onmousemove=null;
	}
}



































function voir_faq(s){
	var i = new Array();
	var n = -1;
	i[++n] = "search_engine";
	i[++n] = "email1";
	i[++n] = "email2";
	i[++n] = "email3";
	i[++n] = "rss1";
	i[++n] = "rss2";
	i[++n] = "toolbox1";
	i[++n] = "toolbox2";
	i[++n] = "toolbox3";
	i[++n] = "toolbox4";
	
	for(var xn = 0;xn<n + 1;xn++){
		o(i[xn]).style.display = 'none';
	}
	o(i[s]).style.display = 'block';
}

var is_tool_menu_opened = false;

function addBordure(id){
	if(current_img != null){
		$('#'+id).css('border-bottom','1px solid black');
	}
}

function removeBordure(id){
	if(id != 'td_move_'+id_current){
		$('#'+id).css('border-bottom','1px solid white');
	}
}

function switchArboToolBox(go){
	if(go == 'toolbox'){
		is_tool_menu_opened = true;
		$('#tool_menu').click().toggle('slow');
	}
	else if(is_tool_menu_opened){
		$('#tool_menu').click().toggle('slow');
		is_tool_menu_opened = false;
	}
}


function switchContact(obj,id,id2){
	if(obj.src.indexOf('images/down.png') > -1){
		with(o(id).style){display='none';}
		obj.src = './images/up.png';
	}
	else{
		with(o(id).style){display='';}
		obj.src = './images/down.png';
	}
	$("#"+id2).click().toggle("slow");
}

function sendMSGContact(){
	var data = "";
	data += "id_to="+o('id_to').value;
	data += "&sujet="+o('sujet').value;
	data += "&message="+o('message').value;
	process('sendMSGContact',data,Array('',true));
}

function checkNewMessage(){
	var data = '';
	if(message_box_dispo && is_logged){
		process('checkNewMessage',data,Array('',true));
	}
	else{
		VoirMessages();
	}
	setTimeout('checkNewMessage()',15000);
}

function toggleWidget(img){
	var css;
	var init_y = 137;
	var goto_y;
	if(top_widget < 0){
		img.src = './images/down.png';
		top_widget = 0;
		goto_y = parseInt($('#wind').css('top'))+20;
		$('#div_viewer').css('left',$('#wind').css('left'));
	}
	else{
		img.src = './images/up.png';
		top_widget = -1000;
		goto_y = top_widget;
	}
	$('#div_viewer').css('top',goto_y+'px');
}

function VoirMessages(){
	$('#cadreNewMessages').click().toggle('slow');
	with(o('newMessages').style){display='none';}
	message_box_dispo = true;
}

var message_box_dispo = true;

function affNewMessage(){
	if(message_box_dispo){
		with(o('newMessages').style){display='inline';}
		$('#cadreNewMessages').click().toggle('slow');
		message_box_dispo = false;
	}
}

function posSourceNext(id_tool,id_arbo){
	if(current_img != null){
		current_id_arbo = id_arbo;
		id_before = id_tool;
		if((id_arbo != null && id_before != id_current) || (id_arbo != null && id_before == null && id_current != null)){
			moveTool(id_before,id_current,current_id_arbo);
		}
	}
}

function follower () {
  var screen_y = 0;
  if (window.pageYOffset) {
    screen_y = window.pageYOffset;
  } else if (document.body && document.documentElement.scrollTop) {
    screen_y = document.documentElement.scrollTop;
  }
  if (screen_y > 119 && start_follower) {
   	with(document.getElementById('wind').style){position='absolute';top=(screen_y)+"px"}
   	if(top_widget == 0){
   		with(document.getElementById('div_viewer').style){position='absolute';top=(screen_y+18)+"px"}
   	}
  }
  /*else{
	  	with(document.getElementById('wind').style){position='absolute';top="119px";}
	  	if(top_widget == 0){
	  		with(document.getElementById('div_viewer').style){position='absolute';top="137px";}
	  	}
  }*/
  setTimeout("follower()", 25);
}

function moveSource(img,id_toolbox,start){
	if(start){
		document.onselectstart = new Function("return false;");
		id_current = id_toolbox;
		current_img = img;
		current_img_titre = document.getElementById('titre_volant_'+id_toolbox);
		$('#td_move_'+id_toolbox).css('border','1px solid black');
	}
	else{
		current_id_arbo = null;
		id_before = null;
		id_current = null;
		if(img != null){
			with(img.style){position="relative";top="0px;";left="0px;"}
		}
		current_img = null;
		current_img_titre = null;
		with(document.getElementById('titre_volant_'+id_toolbox).style){display='none';}
		$('#td_move_'+id_toolbox).css('border-bottom','1px solid white');
		//document.onselectstart = new Function("return true;");
	}
}

function deleteMessage(id_message){
	var data = "id_message="+id_message;
	process('deleteMessage',data,Array('',true));
}

function listMessages(){
	var data = "";
	process('listMessages',data,Array('',true));
}

function openMessage(id){
	$('#id_message_'+id).click().toggle('slow');
	var data = "id_message="+id;
	process('readMessage',data,Array('',true));
	with(o('no_read_'+id).style){display='none';}
	with(o('read_'+id).style){display='inline';}
}

var id_current_contact_switch = 0;
var position_contact_switch = "f";

function switchMsgContact(i,id_to){
	if(id_current_contact_switch == i || position_contact_switch == 'f'){
		$("#id_box").click().toggle("slow");
		if(position_contact_switch == 'f'){
			position_contact_switch = 'o';
		}
		else{
			position_contact_switch = 'f';
			$("#id_box").click().toggle("slow");
		}
	}
	o('msg_nom').innerHTML = o('nom_'+i).innerHTML;
	o('conteneur').setAttribute("style","padding-top:"+(y-428)+"px;");
	o('id_to').setAttribute("value",id_to);
	id_current_contact_switch = i;
}

function rechercher(){
	if(o('mot_clef').value.length > 0){
		o('env_recherche').disabled = true;
		
		var data = sendForm('f_rechercher');
		if(document.getElementById('nb_par_page')){
			data += 'nb_par_page='+document.getElementById('nb_par_page').value;
		}
		if(o('tn').checked){
			data += '&tn=1';
		}
		else{
			data += '&tn=0';
		}
		if(document.getElementById('id_page')){
			data += "&p="+document.getElementById('id_page').value;
		}
		else{
			data += "&p=1";
		}
		process('rechercher',data,Array('',true));
		o('env_recherche').disabled = false;
	}
	else{
		alert('Votre recherche est vide !');
	}
}

function moveTool(id_to,id_from,id_arbo){
	var data = "id_from="+id_from+"&id_to="+id_to+"&id_arbo="+id_arbo;
	current_id_arbo = null;
	id_before = null;
	id_current = null;
	current_img = null;
	current_img_titre = null;
	process('moveTool',data,Array('',true));
}

function moveFav(f,t){
	var data = "f="+f+"&t="+t;
	process('moveFav',data,Array('',true));
}

function RetforgotPW(){
	o('btn_cnx').style.display = 'block';
	o('btn_forgot').style.display = 'none';
}

function sendPW(){
	if(o('my_email').value != ''){
		var data = 'mail='+o('my_email').value;
		process('sendPW',data,Array('',true));
	}
	else{
		alert("Merci de donner votre adresse e-mail");
	}
}

function forgotPW(){
	o('btn_cnx').style.display = 'none';
	o('btn_forgot').style.display = 'block';
}

function html_exec(){
	document.getElementById('code').value;
	alert(document.getElementById('viewer').location.href);
}

function readRSS(){
	if(o('url').value.length > 0){
		o('btn_rss').disabled = true;
		
		var data = 'url='+o('url').value;
		process('readRSS',data,Array('',true));
		o('btn_rss').disabled = false;
	}
	else{
		alert('Merci d\'entrer l\'URL du flux RSS !');
	}
}

function renameSource(id){
	var nom = o('txt_'+id).value;
	if(nom == ''){
		alert('Vous devez donner un nom à votre Widget');
		return false;
	}
	var data = "id_toolbox="+id+"&nom="+nom;
	process('renameSource',data,Array('',true));
}

function renameArbo(id){
	var nom = o('txt_arbo_'+id).value;
	if(nom == ''){
		alert('Vous devez donner un nom à votre rubrique');
		return false;
	}
	var data = "id_arbo="+id+"&intitule="+nom;
	process('renameArbo',data,Array('',true));
}

function deleteArbo(id){
	if(confirm("Etes-vous sûr de vouloir supprimer cette rubrique ainsi que tout ce qu'elle contient ?")){
		var data = 'id_arbo='+id;
		process('deleteArbo',data,Array('',true));
	}
}

function addArbo(id){
	var data = 'id_parent='+id;
	process('addArbo',data,Array('',true));
}

function saveSource(){
	if(o('code').value.length > 0 && o('code').value != 'Insérez ici votre code source'){
		var code = o('code').value;
		var new_code = '';
		var c = '';
		for(var i = 0;i<code.length;i++){
			c = code.substr(i,1);
			if(c == '&'){
				c = '---';
			}
			new_code += c;
		}
		var data = 'intitule='+o('intitule').value+'&id_arbo='+o('id_arbo').value+'&code='+new_code;
		process('saveSource',data,Array('',true));
	}
	else{
		alert('Merci d\'entrer un code pour cette source !');
	}
}

function closeWidget(){
	$('#wind').hide();
	o('titre_wid').innerHTML = '';
	o('div_viewer').innerHTML = '';
	$('#div_viewer').click().hide();
}

function readSource(id){
	var data = 'id_toolbox='+id;
	o('img_reader').src = './images/down.png';
	top_widget = 0;
	var goto_y = 137;
	$('#div_viewer').click().show();
	with(o('div_viewer').style){top=goto_y+"px";}
	process('readSource',data,Array('',true));
	process('readSourceName',data,Array('',true));
}

function listSource(id){
	process('listSource');
}

function saveRSS(){
	if(o('url').value.length > 0){
		if(o('main').innerHTML.indexOf('readRSS();">'+o('url').value) > 0){
			return alert("L'URL du flux RSS existe dans votre liste !");
		}
		o('btn_save_rss').disabled = true;
		var data = 'url='+o('url').value;
		process('saveRSS',data,Array('',true));
		o('url').value = '';
		o('btn_save_rss').disabled = false;
	}
	else{
		alert('Merci d\'entrer une URL de flux RSS !');
	}
}

function deleteRSS(id){
	if(confirm("Voulez vous vraiment supprimer ce flux RSS de votre liste ?")){
		var data = 'id_rss='+id;
		process('deleteRSS',data,Array('',true));
	}
	return;
}

function addSource(id_arbo){
	$('#id_add_source').css('top',(y-100));
	$('#id_add_source').show();
	o('id_arbo').value = id_arbo;
	o('code').value = '';
}

function closeAddSource(id_arbo){
	$('#id_add_source').hide();
	o('id_arbo').value = '';
}

function sendSource(id){ // affiche le cadre
	//var css = "display:inline;position:absolute;padding-left:10px;top:"+(y-70)+"px;left:300px;background-color:white;width:325px;height:310px;border:1px dotted red;";
	$('#id_sendSource').css('position','absolute');
	$('#id_sendSource').css('top',(y-70)+'px');
	$('#id_sendSource').css('left','300px');
	o('id_toolbox').value = id;
	$('#id_sendSource').show();
}

function closeSendSource(){ // ferme le cadre
	//var css = "display:none;position:absolute;padding-left:10px;top:0px;left:300px;background-color:white;width:325px;height:310px;border:1px dotted red;";
	o('id_toolbox').value = '';
	o('id_contact').selectedIndex = 0;
	o('id_message').value = '';
	$('#id_sendSource').hide();
}

function envoyerSource(){ // procède à l'envoi
	var id_contact = '0';
	var selectBox = o('id_contact');
	for (var i=0; i<selectBox.options.length; i++) {
	  if (selectBox.options[i].selected) {
	    id_contact += ","+selectBox.options[i].value;
	  }
	}
	var id_toolbox = o('id_toolbox').value;
	var message = o('id_message').value;
	var data = "id_toolbox="+id_toolbox+"&id_contact="+id_contact+"&message="+message;
	process('envoyerSource',data,Array('',true));
}

function renommerSource(id){
	$('#lien_'+id).click().hide();
	o('txt_'+id).value = o('from_txt_'+id).innerHTML;
	$('#rename_'+id).click().show();
}

function renommerArbo(id){
	$('#lien_arbo_'+id).click().hide();
	o('txt_arbo_'+id).value = o('lien_arbo_'+id).innerHTML;
	$('#rename_arbo_'+id).click().show();
}

function deleteSource(id){
	if(confirm("Voulez vous vraiment supprimer cette source HTML de votre liste ?")){
		var data = 'id_toolbox='+id;
		process('deleteSource',data,Array('',true));
	}
	return;
}

function listRSS(){
	var data  = '';
	process('listRSS',data,Array('',true));
}

function deleteFav(id){
	if(confirm("Voulez vous vraiment supprimer ce favoris de votre liste ?")){
		var data = 'id_favoris='+id;
		process('deleteFav',data,Array('',true));
	}
	return;
}

function deleteContact(id){
	if(confirm("Voulez vous vraiment supprimer ce contact de votre liste ?")){
		var data = 'id_contact='+id;
		process('deleteContact',data,Array('',true));
	}
	return;
}

function editFav(id){
	var data = "id_favoris="+id;
	process('getFavToEdit',data,Array('',true));
}

function editContact(id){
	var data = "id_contact="+id;
	process('getContactToEdit',data,Array('',true));
}


function abortEditFav(){
	o('add_fav_btn').value = 'Sauvegarder';
	with(o('abort_fav_btn').style){display='none';}
	o('intitule').value = '';
	o('url').value = '';
	o('tags').value = '';
	o('description').value = '';
	o('id_favoris').value = '';
}

function abortEditContact(){
	o('add_fav_btn').value = 'Sauvegarder';
	with(o('abort_fav_btn').style){display='none';}
	o('nom').value = '';
	o('email').value = '';
	o('id_contact').value = '';
}

function inputEditFav(data){
	o('add_fav_btn').value = 'Modifier';
	with(o('abort_fav_btn').style){display='inline';}
	data = data.split('|');
	o('intitule').value = data[0];
	o('url').value = data[1];
	o('tags').value = data[2];
	o('description').value = data[3];
	o('id_favoris').value = data[4];
}

function inputEditContact(data){
	o('add_fav_btn').value = 'Modifier';
	with(o('abort_fav_btn').style){display='inline';}
	data = data.split('|');
	o('nom').value = data[0];
	o('email').value = data[1];
	o('id_contact').value = data[2];
}

function saveFav(){
	if(o('intitule').value.length > 0 && o('url').value.length > 0){
		var data = 'intitule='+o('intitule').value+'&url='+o('url').value+'&tags='+o('tags').value+'&description='+o('description').value+'&id_favoris='+o('id_favoris').value;
		process('saveFav',data,Array('',true));
		abortEditFav();
	}
	else{
		alert("Merci de remplir au moins le nom et l'url.");
	}
}

function saveContact(){
	if(o('nom').value.length > 0 && o('email').value.length > 0){
		if(!c_email(o('email').value)){
			alert("L'adresse email n'est pas valide !");
			return;
		}
		var data = 'nom='+o('nom').value+'&email='+o('email').value+'&id_contact='+o('id_contact').value;
		process('saveContact',data,Array('',true));
		abortEditContact();
	}
	else{
		alert("Merci de remplir tous les champs");
	}
}

function listFav(){
	var data  = '';
	process('listFav',data,Array('',true));
}

function listContacts(){
	var data  = '';
	process('listContacts',data,Array('',true));
}

function listMails(){
	var data  = '';
	process('lireMails',data,Array('',true));
}

function showMail(ob){
	if(o(ob).style.display == 'inline'){
		o(ob).style.display = 'none';
	}
	else{
		o(ob).style.display = 'inline';
	}
}

var tmp_read_email_data = '';
var tmp_read_email_type = 1;

function readEmail(){
	if((o('pop').value != '' && o('email').value != '' && o('login').value != '' && o('pw').value != '') || arguments.length > 0){
		o('btn_email').disabled = true;
		if(arguments.length > 0){
			var data = 'pop='+arguments[0]+'&email='+arguments[1]+'&login='+arguments[2]+'&pw='+arguments[3]
		}
		else{
			var data = 'pop='+o('pop').value+'&email='+o('email').value+'&login='+o('login').value+'&pw='+o('pw').value;
		}
		tmp_read_email_data = data;
		process('lireMails',data,Array('',true));
		o('btn_email').disabled = false;
	}
	else{
		alert('Vous devez entrer toutes les informations requises');
	}
}

function classerEmail(sort){
	if(tmp_read_email_data != ''){
		o('btn_email').disabled = true;
		tmp_read_email_type = !tmp_read_email_type + 0;
		var data = tmp_read_email_data + "&sort="+sort+"&type="+tmp_read_email_type;
		process('lireMails',data,Array('',true));
		o('btn_email').disabled = false;
	}
}

function gotoWebmail(u){
	var s = u.substr(u.indexOf('@')+1,u.length);
	window.open('http://www.'+s);
}

function saveEmail(){
	if(o('pop').value != '' && o('email').value != '' && o('login').value != '' && o('pw').value != ''){
		o('btn_save_email').disabled = true;
		var data = 'pop='+o('pop').value+'&email='+o('email').value+'&login='+o('login').value+'&pw='+o('pw').value+'&webmail='+o('webmail').value;
		process('saveMails',data,Array('',true));
		o('pop').value = '';
		o('email').value = '';
		o('login').value = '';
		o('pw').value = '';
		o('webmail').value = '';
		o('btn_save_email').disabled = false;
	}
	else{
		alert('Vous devez entrer toutes les informations requises');
	}
}

function deleteMails(id){
	if(confirm("Voulez vous vraiment supprimer ce compte e-mail de votre liste ?")){
		var data = 'id='+id;
		process('deleteMails',data,Array('',true));
	}
	return;
}

function mes_emails(){
	var data  = '';
	process('mes_emails',data,Array('',true));
}

function setSearchEngine(se){
	if(!ie){
		o('se_plopi').setAttribute('class','recherche_moteur');
		o('se_yahoo').setAttribute('class','recherche_moteur');
		o('se_msn').setAttribute('class','recherche_moteur');
		o('se_'+se).setAttribute('class','recherche_moteur_sel');
	}
	else{
		var css = "cursor:hand;height:19px;	width:90px;	padding-left:5px; padding-top:3px;	font-weight:bold;	font-size:12px;	color:black;	float:left;	margin-top:5px;	margin-left:5px;	border-bottom:1px solid #CCCCCC;	border-right:1px solid #CCCCCC;	border-top:1px solid #EEEEEE;	border-left:1px solid #EEEEEE;	text-align:left;";
		var css_sel = "cursor:hand;height:19px;	width:92px;	background: #EEEEEE url(./images/recherche_moteur_sel.gif) no-repeat top right;	padding-top:4px;	padding-left:5px;	font-weight:bold;	font-size:12px;	color:black;	float:left;	margin-top:5px;	margin-left:5px;	border-bottom:1px solid #CCCCCC;text-align:left;";
		o('se_plopi').style.cssText = css;
		o('se_yahoo').style.cssText = css;
		o('se_msn').style.cssText = css;
		o('se_'+se).style.cssText = css_sel;
	}
	var data = 'se='+se;
	process('selectSE',data,Array('',true));
}

function logout(){
	if(confirm("Voulez vous vraiment quitter PlopiBox.com ?")){
		process("logout");
	}
}

function switch_visible(){
	var data = '';
	if(is_visible){
		process("set_invisible",data,Array('',true));
	}
	else{
		process("set_visible",data,Array('',true));
	}
}

function p_login(){
	var login = o('login').value;
	var pw =  o('pw').value;
	if(login != '' && pw != ''){
		var data = "login="+login+"&pw="+pw;
		process("login",data,Array('',true));
	}
	else{
		alert("Veuillez entrez vos identifiants de connexion");
	}
	return false;
}

function switch_position_arbo(is_opened,id_arbo){
	var data = 'is_opened='+is_opened+'&id_arbo='+id_arbo;
	process("switch_position_arbo",data,Array('',true));
}

function check_login(){
	process("is_logged","",Array('',true));
}

function subscribe(){
	var login = o('s_login').value;
	var pw =  o('s_pw').value;
	if(login != '' && pw != ''){
		if(!c_email(login)){
			alert("Votre adresse email n'est pas correcte !");
			return false;
		}
		var data = "login="+login+"&pw="+pw;
		process("subscribe",data,Array('',true));
	}
	else{
		alert("Veuillez entrez vos identifiants de connexion");
	}
	return false;
}

function remettre_in(o,v){
	if(o.value == ''){
		o.value = v;
	}
}

function retirer_in(o,v){
	if(o.value == v){
		o.value = '';
	}
}