var color_body_tmp='#FFFFFF';
var position_curseur_X;
var position_curseur_Y;
var ch_en_cours="";
var fen;
var ch_ajax;
var ch_css;

var timeoutID;
var timeoutID_vide_champ;

function detecte_nav() {
	var retour;
	if(navigator.userAgent.indexOf("MSIE 7") != -1) retour = "IE7";
	else if(navigator.userAgent.indexOf("MSIE 6") != -1) retour = "IE6";
	else if(navigator.userAgent.indexOf("Firefox/2") != -1) retour = "FIREFOX2";
	else if(navigator.userAgent.indexOf("Firefox") != -1) retour = "FIREFOX";
	else if(navigator.userAgent.indexOf("Netscape/7") != -1) retour = "NETSCAPE7";
	else if(navigator.userAgent.indexOf("Netscape") != -1) retour = "NETSCAPE";
	else if(navigator.userAgent.indexOf("Opera/9") != -1) retour = "OPERA9";
	else if(navigator.userAgent.indexOf("Opera") != -1) retour = "OPERA";
	else retour = "AUTRE";

	return retour;
}
// Fonction de rechargement du main

function click_societe(num_soc, num_site, type) {
    loading();
    timeoutID = setTimeout("click_societe_fct('"+num_soc+"', '"+num_site+"', '"+type+"')",200);
}
function click_societe_fct(num_soc, num_site, type) {
	clearTimeout(timeoutID);
	var inner = ajax_(ch_en_cours+ch_ajax+'/ajax.php?fen='+fen+'&fct=change_societe&param1='+num_soc+'&param2='+num_site+'&param3='+type);
	inner = ajax_(ch_en_cours+ch_ajax+'/ajax.php?fen='+fen+'&fct=load_entete');
	document.getElementById("div_principal").innerHTML = inner;
	timeoutID = setTimeout("donne_aide()",200);
	DS_all_tiny();
	fin_loading();
}
function click_menu(num) {
    loading();
    timeoutID = setTimeout("click_menu_fct('"+num+"')",200);
}
function click_menu_fct(num) {
	clearTimeout(timeoutID);
	var inner = ajax_(ch_en_cours+ch_ajax+'/ajax.php?fen='+fen+'&fct=change_menu&param1='+num);
	inner = ajax_(ch_en_cours+ch_ajax+'/ajax.php?fen='+fen+'&fct=load_main');
	document.getElementById("main").innerHTML = inner;
	timeoutID = setTimeout("donne_aide()",200);
	DS_all_tiny();
	fin_loading();
}
function click_sous_menu(num) {
    loading();
    timeoutID = setTimeout("click_sous_menu_fct('"+num+"')",500);
}
function click_sous_menu_fct(num) {
	clearTimeout(timeoutID);
	var inner = ajax_(ch_en_cours+ch_ajax+'/ajax.php?fen='+fen+'&fct=change_sous_menu&param1='+num);
	load_content_fct();
}
function change_content(num, idligne) {
	loading();
	timeoutID = setTimeout("change_content_fct('"+num+"', '"+idligne+"')",200);
}
function change_content_fct(num, idligne) {
	clearTimeout(timeoutID);
	var inner = ajax_(ch_en_cours+ch_ajax+'/ajax.php?fen='+fen+'&fct=change_content&param1='+num+'&param2='+idligne);
	//document.getElementById("td_main").innerHTML = inner;
	load_content_fct();
}

function load_content() {
	loading();
	timeoutID = setTimeout("load_content_fct()",20);
}
function load_content_fct() {
	clearTimeout(timeoutID);
	inner = ajax_(ch_en_cours+ch_ajax+'/ajax.php?fen='+fen+'&fct=load_content');
	document.getElementById("td_main").innerHTML = inner;
	timeoutID = setTimeout("donne_aide()",20);
	DS_all_tiny();
	fin_loading();
}

function ferme_content() {
    loading();
    timeoutID = setTimeout("ferme_content_fct()",20);
}
function ferme_content_fct() {
	clearTimeout(timeoutID);
	var inner = ajax_(ch_en_cours+ch_ajax+'/ajax.php?fen='+fen+'&fct=ferme_content');
	inner = ajax_(ch_en_cours+ch_ajax+'/ajax.php?fen='+fen+'&fct=load_content');
	document.getElementById("td_main").innerHTML = inner;
	timeoutID = setTimeout("donne_aide()",20);
	DS_all_tiny();
	fin_loading();
}
function vide_champ(champ) {
    clearTimeout(timeoutID_vide_champ);
    document.getElementById(champ).innerHTML = "";
}
function donne_aide() {
	clearTimeout(timeoutID);	
	//init_aide();
	//document.getElementById("div_aide").innerHTML=document.getElementById("content_aide").value;	
}

function chg_site_mag_lang(id_lang, session, param) {
    loading();
    timeoutID = setTimeout("chg_site_mag_lang_fct('"+id_lang+"', '"+session+"', '"+param+"')",20);
}
function chg_site_mag_lang_fct(id_lang, session, param) {
	clearTimeout(timeoutID);
	save_param_pref(id_lang, "PREF_SITE_MAG_LANG_EN_COURS");
	if (!isDefined(param)) param="";
	change_content_fct(session, param);
	fin_loading();
}

// -------------------- Fin fonction rechargement main ------------------------------- //

// ------------------- Tiny mce --------------------------------------------------------- //
function DS_all_tiny() {
	try {
		try {
			var noeud = document.getElementById("page")
			var fils = noeud.childNodes; 
			var i=fils.length-1;
			while (fils[i].id!="conteneur") {
				noeud.removeChild(fils[i]);
				i--;
			}
			
		}
		catch(ex) {}
		var i=0;
		while (document.getElementById('textarea_mce_'+i)) {
			var bc = "#FFFFFF";
			if (color_body_tmp!="") bc = color_body_tmp;
			DS_tiny_mce('textarea_mce_'+i, bc);
			i++;
		}
	}
	catch(ex) {}
}

function DS_tiny_mce(name_, bc) {
tinyMCE.init({
	// General options
	language : "fr",
	mode : "exact",
	color_body : bc,
	elements : name_,
	theme : "advanced",
	plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

	// Theme options
	theme_advanced_buttons1 : "newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
	theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,advhr,|,print,|,ltr,rtl,|,fullscreen",
	theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : true,

	// Example content CSS (should be your site CSS)
	content_css : ch_css,

	// Drop lists for link/image/media/template dialogs
	template_external_list_url : "lists/template_list.js",
	external_link_list_url : "lists/link_list.js",
	external_image_list_url : "lists/image_list.js",
	media_external_list_url : "lists/media_list.js"

	// Replace values for the template plugin
	/*template_replace_values : {
		username : "Some User",
		staffid : "991234"
	}*/
});
}

//------------------------------ Fin tiny mce ------------------------------- //

function isDefined(variable)
{
return (!(!(document.getElementById(variable))))
}

function LimiterTextArea(nom_controletexte, nbcar, nom_controledecompte) {
    var moncontroletexte = document.getElementById(nom_controletexte);
    var moncontroledecompte = document.getElementById(nom_controledecompte);
    
    if (moncontroletexte && moncontroledecompte) {
        moncontroletexte.onclick = function(){TextAreaEstRempli(moncontroletexte, nbcar, moncontroledecompte)};
        moncontroletexte.onblur = function(){TextAreaEstRempli(moncontroletexte, nbcar, moncontroledecompte)};
        moncontroletexte.onkeyup = function(){TextAreaEstRempli(moncontroletexte, nbcar, moncontroledecompte)};
        moncontroletexte.onkeypress = function(){TextAreaEstRempli(moncontroletexte, nbcar, moncontroledecompte)};

        // *** Affichage du nombre de caractères restant
        if(moncontroledecompte.type)
            moncontroledecompte.value = NbCarRestant(moncontroletexte, nbcar); // Pour un input de formulaire
        else moncontroledecompte.innerHTML = NbCarRestant(moncontroletexte, nbcar); // Pour un élément HTML
    }
}

// TextAreaEstRempli
// args : textarea moncontroletexte, int nbcar, element_HTML moncontroledecompte
// return : bool
// Renvoie vrai si le nombre de caractères maximum du textarea n'est pas atteint
function TextAreaEstRempli(moncontroletexte, nbcar, moncontroledecompte) {
    if (moncontroletexte) {
        if (moncontroletexte.value.length <= nbcar) {
            //alert("pas rempli");
            // mes actions ...

            // *** Affichage du nombre de caractères restant
            if(moncontroledecompte.type) moncontroledecompte.value = NbCarRestant(moncontroletexte, nbcar);
            else moncontroledecompte.innerHTML = NbCarRestant(moncontroletexte, nbcar);

            return true;
        }
        else {
            //alert("rempli");
            // mes actions ...
            
            // Affichage du nombre de caractères restant
            moncontroletexte.value = moncontroletexte.value.substr(0, nbcar);
            
            // *** Affichage du nombre de caractères restant
            if(moncontroledecompte.type) moncontroledecompte.value = NbCarRestant(moncontroletexte, nbcar);
            else moncontroledecompte.innerHTML = NbCarRestant(moncontroletexte, nbcar);

            return false;
        }
    }
}

// NbCarRestant
// args : textarea moncontroletexte, int nbcar
// return : int
// Renvoie le nombre de caractère à saisir
function NbCarRestant(moncontroletexte, nbcar) {
    if (moncontroletexte.value.length) return new Number(nbcar - moncontroletexte.value.length);
    else return new Number(nbcar);
}

function donne_position(element) {
         var Obj=element.offsetParent;
         var X = 0;
         var Y = 0;
         while (Obj) {
               try {
                 X += Obj.offsetLeft;
                 Y += Obj.offsetTop;
               }
               catch(ex){}
               Obj = Obj.offsetParent;

         }                    //- marge_X
   var position = Array();

   position["x"] = X;
   position["y"] = Y;
   return position;
}

function donne_position_curseur(e) {
        alert('vrr');
      	position_curseur_X = (navigator.appName.substring(0,3) == "Net") ? e.pageX : event.x+document.body.scrollLeft;
      	alert(position_curseur_X);
      	position_curseur_Y = (navigator.appName.substring(0,3) == "Net") ? e.pageY : event.y+document.body.scrollTop;
      	window.status = "Souris X:"+x+" / Y:"+y;
}

function loading() {

  /*if(navigator.appName.substring(0,3) == "Net")
	document.captureEvents(Event.MOUSEMOVE);
  document.onmousemove = donne_position_curseur(); */
  var myHeight = 0;
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
  }
  else  {
       if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
              //IE 6+ in 'standards compliant mode'
              myWidth = document.documentElement.clientWidth;
              myHeight = document.documentElement.clientHeight;
       }
       else {
             if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
                 //IE 4 compatible
                 myWidth = document.body.clientWidth;
                 myHeight = document.body.clientHeight;
             }
       }
  }
  myWidth = (myWidth / 2) - 75;
  myHeight = (myHeight / 2) - 75;
  try {
    myWidth += document.body.scrollLeft;
    myHeight += document.body.scrollTop;
  }catch (ex) {};
  document.getElementById('div_loading').style.display = 'block';
  document.getElementById('div_loading').style.left = parseInt(myWidth)+'px';// position_curseur_X;
  document.getElementById('div_loading').style.top = parseInt(myHeight)+'px';//position_curseur_Y;
}

function fin_loading() {
    document.getElementById('div_loading').style.display = 'none';
}

function change_lang(id_lang) {           //alert(id_lang);
    var val = ajax_(ch_en_cours+'ajax/ajax.php?fen='+fen+'&fct=change_lang&param1='+id_lang);
    //alert(val);
    window.location.href="";
}

function choose_color(compo) {
    var h = 200;
    var w = 300;
    var topPosition = (screen.height - h) / 2;
    var leftPosition = (screen.width - w) / 2;
    window.open("include/color.php?compo="+compo, "filemanager", "width="+w+",height="+h+",top="+topPosition+",left="+leftPosition+",location=no,menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=no");
}

function choose_image(compo) {
    var h = 500;
    var w = 800;
    var topPosition = (screen.height - h) / 2;
    var leftPosition = (screen.width - w) / 2;
    window.open("include/module/filemanager/filemanager.php?compo="+compo+"&imageDest=public&fen="+fen, "filemanager", "width="+w+",height="+h+",top="+topPosition+",left="+leftPosition+",location=no,menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=no");
}

function affiche_aide(anchor_) {
    var h = screen.height-200;
    var w = screen.width-200;
    var topPosition = (screen.height - h) / 2;
    var leftPosition = (screen.width - w) / 2;
    window.open("aide.php?fen="+fen+"#label_titre_aide_"+anchor_, "Aide", "width="+w+",height="+h+",top="+topPosition+",left="+leftPosition+",location=no,menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=no");
  
}

function save_param_pref(val, key_session) {
	var inner = ajax_(ch_en_cours+ch_ajax+'/ajax.php?fen='+fen+'&fct=save_param_pref&param1='+val+'&param2='+key_session);
}

function tout_cocher(prefixe, val) {
	var i=0;
	try {
		while (document.getElementById(prefixe+i)) {
			document.getElementById(prefixe+i).checked=val;
			try {
				tout_cocher(prefixe+i+'_', val);
			}
			catch(ex) {}
			i++;
		}
	}
	catch(ex) {}
}
