
function tohtml(editeurId,divHtmlId,txtHtmlId) {
    editeurhtml=document.getElementById(editeurId+'_html');
    editeurtxt=document.getElementById(editeurId+'_txt');
    divHtml=document.getElementById(divHtmlId);
    txtHtml=document.getElementById(txtHtmlId);
    
    editeurhtml.style.display="none";
    editeurtxt.style.display="block";
    txtHtml.value=divHtml.innerHTML;
    return false;
}

function towysiwyg(editeurId,divHtmlId,txtHtmlId) {
    editeurhtml=document.getElementById(editeurId+'_html');
    editeurtxt=document.getElementById(editeurId+'_txt');
    divHtml=document.getElementById(divHtmlId);
    txtHtml=document.getElementById(txtHtmlId);
    
    editeurhtml.style.display="block";
    editeurtxt.style.display="none";
    divHtml.innerHTML=txtHtml.value;
    return false;
}

function convertLayer(editeurId,divHtmlId,txtHtmlId) {
    editeurhtml=document.getElementById(editeurId+'_html');
    editeurtxt=document.getElementById(editeurId+'_txt');
    divHtml=document.getElementById(divHtmlId);
    txtHtml=document.getElementById(txtHtmlId);

    if (editeurhtml.style.display=="none")     divHtml.innerHTML=txtHtml.value;
    txtHtml.value=divHtml.innerHTML;
}


function switchDiv(noCat) {
    var imgsec=document.getElementById('img_'+noCat);
    var divsec=document.getElementById(noCat);
    if (divsec.style.display=="none"){
        divsec.style.display="block";
        
        if (imgsec){
            imgsec.src='themes/default/images/deplierbas.gif';
           }
    } else {
        if ( (divsec.style.display=="block") || (divsec.style.display=="") ){
               divsec.style.display="none";
            if (imgsec){
                imgsec.src='themes/default/images/deplierhaut.gif';
            }
        }
    }
    return false;
    
}

function switchTreeDiv(div,imagepath) {
    imgsec=document.getElementById('img'+div);
    divsec=document.getElementById(div);
    if (divsec.style.display=="none") {
        divsec.style.display="";
        if (imgsec)
        {
            imgsec.src=imagepath+'/tree_collapse.gif';
        }
       }else {
           divsec.style.display="none";
        if (imgsec)
        {
            imgsec.src=imagepath+'/tree_expand.gif';
        }
       }
    return false;
}

function is_mozilla()
{
    var ua=navigator.userAgent.toLowerCase();
    return (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);
}

/* /!\ This one won't work in a XHTML+XML application
 * as getElementsByTagName and ByName doesn't exist in
 * the specs.
 */
function getElementsByTagAndName(tagname, name)
{
    if (is_mozilla())
    {
        return document.getElementsByName(name);
    }
    var elems = document.getElementsByTagName(tagname);
    var result=new Array();
    for (i=0; i<elems.length; i++)
    {
        var elem = elems[i];
        if (elem.name == name) result.push (elem);
    }
    return result;
}

function menu_admin_switchAction(cat)
{
    for(var i=0; i< list_menu_admin_id.length;i++)
    {
        divsec=document.getElementById("action_menu_admin_" +list_menu_admin_id[i]);
        if(cat == "action_menu_admin_" +list_menu_admin_id[i])
        {
            if (divsec.style.display=="none"){
                divsec.style.display="block";
            } else {
                if ( (divsec.style.display=="block") || (divsec.style.display=="") ){
                       divsec.style.display="none";
                }
            }
        } else {
            divsec.style.display="none";
        }
    }
    return false;
}

function menu_admin_switch(id, url_root)
{
  var tr_list = getElementsByTagAndName('tr', id);
  var show = (tr_list[0].style.display=='none');
  do_menu_admin_switch(id, url_root, tr_list, show);  
}

function do_menu_admin_switch(id, url_root, tr_list, show) 
{
  var img = document.getElementById(id+"_img");
  var i;
  for (i=0; i<tr_list.length; i++)
  {
    tr = tr_list[i];
    if (show) {
      tr.style.display = '';
      img.src = url_root+'images/treemenu2/tree_collapse.gif';
    } else {
      tr.style.display = 'none';
      img.src = url_root+'images/treemenu2/tree_expand.gif';
    }

    var children = getElementsByTagAndName('tr', tr.id);
    if (!show && (0<children.length)) do_menu_admin_switch(tr.id, url_root, children, show)
  }
}

function writeEmailingField(modid, tabfield, selectionObj){
    // Zone d'affichage de la liste des champs personnalisables disponible
    var field = document.getElementById(modid+"_listfield");    

    // Si la zone existe, on remplit avec la liste des champs personnalisables
    if( field != undefined ){
        selectionIndex=selectionObj.selectedIndex;
        field.innerHTML = tabfield[selectionIndex];
    }
}

function dummy_tri_up(idlist,id) {
    var ligne=document.getElementById(id);
    var p = ligne.parentNode;
    avant = ligne.previousSibling;
    while (avant.nodeType!=1 && avant!=null) {
        avant=avant.previousSibling;
    }
    if (avant!=null) {
        var nl=ligne.cloneNode(true);
        p.insertBefore(nl,avant);
        p.removeChild(ligne);
        s = "idobject="+idlist+"&id1="+id+"&id2="+avant.id;
        envoyer(s , "ajax.php?mod=metabase&action=tri", "Tri");
    }
    return false;
}
function dummy_tri_down(idlist,id) {

    var ligne=document.getElementById(id);
    var p = ligne.parentNode;
    next = ligne.nextSibling;
    while (next.nodeType!=1 && next!=null) {
        next=next.nextSibling;
    }
    if (next!=null) {
        var nl=next.cloneNode(true);
        p.insertBefore(nl,ligne);
        p.removeChild(next);
        s = "idobject="+idlist+"&id1="+id+"&id2="+next.id;
        envoyer(s , "ajax.php?mod=metabase&action=tri", "Tri");

    }
    return false;
}

function ajaxupdate_spin(idlist, idobject,field,iddom,down) {
    var val=parseInt(document.getElementById(iddom).value);
    if (down && val > 0) val=val-1; 
    else if(!down) val=val+1;
	if (isNaN(val)) val=0;
    document.getElementById(iddom).value=val;
    ajaxupdate(idlist,idobject,field,val);
}

function ajaxupdate_checkbox(idlist, idobject,field,iddom,fd,div_ret) {
    var imgcb=document.getElementById(iddom);
    var val=parseInt(imgcb.title);
    if (val==0) {
	val=1; 
        imgcb.src='images/on.png';
    }
    else {
        val=0;
        imgcb.src='images/off.png';
    }
    imgcb.title=val;
    ajaxupdate(idlist,idobject,field,val,fd,div_ret);
	if (div_ret!="" && val==0) {
			var div=document.getElementById(div_ret);
			if (div!=null) div.innerHTML="";
		}

}
function ajaxupdate_text(idlist, idobject,field,iddom) {
    var alink=document.getElementById(iddom);
  
	var val=(alink.innerHTML);
//    alink.onclick='';

//    alink.class='';
   if (alink.title!='open') { 
           alink.innerHTML='<input type="text" value="'+val+'" id="text_'+idobject+'"><img src="images/mini_ok.png" onclick=\"ajaxupdate_text_close(\''+idobject+'\',\''+iddom+'\',\''+idlist+'\',\''+field+'\');\">';
           alink.title='open';
    }
    return false;
}
function ajaxupdate_textarea(idlist, idobject,field,iddom) {
    var alink=document.getElementById(iddom);
  
	var val=(alink.innerHTML);
//    alink.onclick='';

//    alink.class='';
   if (alink.title!='open') { 
           alink.innerHTML='<textarea cols="80" id="text_'+idobject+'">'+val+'</textarea><img src="images/mini_ok.png" onclick=\"ajaxupdate_text_close(\''+idobject+'\',\''+iddom+'\',\''+idlist+'\',\''+field+'\');\">';
           alink.title='open';
    }
    return false;
}

function ajaxupdate_text_close(f,iddom,idlist,field) {
        var alink=document.getElementById(iddom);
        var inputn=document.getElementById('text_'+f);
        alink.innerHTML=inputn.value;
        alink.title='Cliquez pour editer';
        ajaxupdate(idlist,f,field,inputn.value);
}
function ajaxupdate_state(idlist,idobject,field,val,iddom,idtransaction) {
	
		val=new String(val);

		s = "idobject="+idlist+"&id="+idobject+"&field="+field;		
		s+='&val='+base64Encode(val);
		s+='&idtransaction='+idtransaction;
		// on utilise json 
		var ret=json_form(s,"ajax.php?mod=metabase&action=update_state");
		dom=document.getElementById(iddom);
		if (dom!=null) dom.innerHTML=ret;
        if (fctstate) fctstate(field,val,idtransaction,idlist);


}

function ajaxupdate(idlist,idobject,field,val,fd,div_ret) {
   //mode encode base 64 
		val=new String(val);
		
        s = "idobject="+idlist+"&id="+idobject+"&field="+field;
		if (fd!=undefined)s+='&field_date='+fd;
		s+='&val='+base64Encode(val);
		// on utilise json 
		var ret=json_form(s,"ajax.php?mod=metabase&action=update");
		if (div_ret!="") {
			var div=document.getElementById(div_ret);
			if (div!=null) div.innerHTML=ret;
		}
 }
function envoyer(chaine, urlservice, message){

  var xmlhttp = false;

  /* Compilation conditionnelle d'IE */
  /*@cc_on 
  @if (@_jscript_version >= 5)
     try
     {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     }
     catch (e)
     {
        try
        {
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
           xmlhttp = false;
        }
     }
  @else
     xmlhttp = false;
  @end 
@*/

  /* on essaie de créer l'objet si ce n'est pas déjà fait */
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
  {
     try
     {
        xmlhttp = new XMLHttpRequest();
     }
     catch (e)
     {
        xmlhttp = false;
     }
  }

    xmlhttp.open("POST",urlservice, false);

        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send(chaine);


  if (xmlhttp)
  {
     /* on définit ce qui doit se passer quand la page répondra */
     xmlhttp.onreadystatechange=function()
     {
        if (xmlhttp.readyState == 4) /* 4 : état "complete" */
        {
           if (xmlhttp.status == 200) /* 200 : code HTTP pour OK */
           {
              /*
              Traitement de la réponse.
              Ici on affiche la réponse dans une boîte de dialogue.
              */
              //alert(xmlhttp.responseText);  
				if(xmlhttp.responseText.substr(0,2) != 'ok'){
                        alert("Il y a eu une erreur :\n" + xmlhttp.responseText);

                }
			
           }
			else {
				alert("Je comprends rien !!");
			}
        }
     }
  }
  return xmlhttp;
}
/*
function envoyer(chaine, urlservice, message){

    	if(window.XMLHttpRequest) // Firefox 
				p= new XMLHttpRequest(); 
		else if(window.ActiveXObject) // Internet Explorer 
				p = new ActiveXObject("Microsoft.XMLHTTP"); 
		else { // XMLHttpRequest non supporté par le navigateur 
				alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
				return; 
		} 
       // p.onload = null;

        p.open("POST",urlservice, false);

        p.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        p.send(chaine);

        status = p.status;
        if ( status != "200" ) {
                alert("arg ! mauvaise r�ponse http ("+ status +")");
        } else {
                if(p.responseText.substr(0,2) != 'ok'){
                        alert("Il y a eu une erreur :\n" + p.responseText);
                }
        }
}
*/
function popup(url,nom,w,h,s){
    fen=open(url,nom,'width='+w+',height='+h+',toolbar=no,location=no,status=no,menubar=no,scrollbars='+s+',resizable=no');
    fen.focus();
}

function getCookieVal(offset) {
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function LireCookie(nom) {
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen)
	{
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;

	}
	return null;
}

function EcrireCookie(nom, valeur) {
	var argv=EcrireCookie.arguments;
	var argc=EcrireCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=nom+"="+escape(valeur)+
		((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
		((path==null) ? "" : ("; path="+path))+
		((domain==null) ? "" : ("; domain="+domain))+
		((secure==true) ? "; secure" : "");
}
function getFieldEmailing(noSelection,iddom) {
	
		s='&noSelection='+noSelection;
		// on utilise json 
		var ret=json_form(s,"ajax.php?mod=emailing&action=getField");
		dom=document.getElementById(iddom);
        field="";
        for (var i=0;i<ret.length;i++) {
        field+="{"+ret[i]+"} ";
        }
		if (dom!=null) dom.innerHTML=field;
}

function getPort(poids,iddom) {
	
		s='&poids='+poids;
		// on utilise json 
		var ret=json_form(s,"ajax.php?mod=processus_commande&action=getPort");
		dom=document.getElementById(iddom);
		if (dom!=null) dom.innerHTML=ret[0];
}
