diff --git a/ChangeLog b/ChangeLog
index 2480a9d9393d696268d54bef6e727f193d297511..d5d6ea4e630000f166ba06678ea15db2a0e6568c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -36,7 +36,7 @@ For users:
 - Fix: Can use POS module with several concurrent users.
 
 For developers:
-- New: Can add a left menu (first level) into an existing top menu.
+- New: Can add a left menu into an existing top menu or left menu.
 - New: Add webservice to get or create a product or service.
 - New: Add webservice to get a user.
 - New: Add more "hooks" (like hooks to change way of showing/editing lines into dictionnaries).
diff --git a/dev/skeletons/modMyModule.class.php b/dev/skeletons/modMyModule.class.php
index d0c9daabeb56a301c9e7ec38335eeaa7a245efe4..715f01f43e0231c7cc6721eb579e6d5efd9e653f 100644
--- a/dev/skeletons/modMyModule.class.php
+++ b/dev/skeletons/modMyModule.class.php
@@ -200,11 +200,11 @@ class modMyModule extends DolibarrModules
 		// $this->menu[$r]=array(	'fk_menu'=>'fk_mainmenu=mainmenucode',		// Use 'fk_mainmenu=xxx' or 'fk_mainmenu=xxx,fk_leftmenu=yyy'
 		//							'type'=>'left',			// This is a Left menu entry
 		//							'titre'=>'MyModule left menu',
-		//							'mainmenu'=>'mymodule',
+		//							'leftmenu'=>'mymodule',
 		//							'url'=>'/mymodule/pagelevel2.php',
 		//							'langs'=>'mylangfile',	// Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
 		//							'position'=>100,
-		//							'enabled'=>'1',			// Define condition to show or hide menu entry. Use '$conf->mymodule->enabled' if entry must be visible if module is enabled.
+		//							'enabled'=>'1',			// Define condition to show or hide menu entry. Use '$conf->mymodule->enabled' if entry must be visible if module is enabled. Use '$leftmenu==\'system\'' to show if leftmenu system is selected.
 		//							'perms'=>'1',			// Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules
 		//							'target'=>'',
 		//							'user'=>2);				// 0=Menu for internal users, 1=external users, 2=both
diff --git a/htdocs/core/class/menu.class.php b/htdocs/core/class/menu.class.php
index b89a8b2dcded96e66502b47712812328e994621a..d31be260e3133ecc03beef47fe05e3b117497ecd 100644
--- a/htdocs/core/class/menu.class.php
+++ b/htdocs/core/class/menu.class.php
@@ -1,6 +1,6 @@
 <?php
 /* Copyright (C) 2002-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
- * Copyright (C) 2005-2008 Laurent Destailleur  <eldy@users.sourceforge.net>
+ * Copyright (C) 2005-2012 Laurent Destailleur  <eldy@users.sourceforge.net>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -50,7 +50,7 @@ class Menu
     }
 
     /**
-     * Add a menu entry into this->liste
+     * Add a menu entry into this->liste (at end)
      *
      * @param	string	$url        Url to follow on click
      * @param   string	$titre      Label of menu to add
@@ -63,14 +63,28 @@ class Menu
      */
     function add($url, $titre, $level=0, $enabled=1, $target='',$mainmenu='',$leftmenu='')
     {
-        $i = count($this->liste);
-        $this->liste[$i]['url'] = $url;
-        $this->liste[$i]['titre'] = $titre;
-        $this->liste[$i]['level'] = $level;
-        $this->liste[$i]['enabled'] = $enabled;
-        $this->liste[$i]['target'] = $target;
-        $this->liste[$i]['mainmenu'] = $mainmenu;
-        $this->liste[$i]['leftmenu'] = $leftmenu;
+        $this->liste[]=array('url'=>$url,'titre'=>$titre,'level'=>$level,'enabled'=>$enabled,'target'=>$target,'mainmenu'=>$mainmenu,'leftmenu'=>$leftmenu);
+    }
+
+    /**
+     * Insert a menu entry into this->liste
+     *
+     * @param	int		$idafter	Array key after which inserting new entry
+     * @param	string	$url        Url to follow on click
+     * @param   string	$titre      Label of menu to add
+     * @param   string	$level      Level of menu to add
+     * @param   int		$enabled    Menu active or not
+     * @param   string	$target		Target lien
+     * @param	string	$mainmenu	Main menu ('home', 'companies', 'products', ...)
+     * @param	string	$leftmenu	Left menu ('setup', 'system', 'admintools', ...)
+     * @return	void
+     */
+    function insert($idafter, $url, $titre, $level=0, $enabled=1, $target='',$mainmenu='',$leftmenu='')
+    {
+        $array_start = array_slice($this->liste,0,($idafter+1));
+        $array_new   = array(0=>array('url'=>$url,'titre'=>$titre,'level'=>$level,'enabled'=>$enabled,'target'=>$target,'mainmenu'=>$mainmenu,'leftmenu'=>$leftmenu));
+        $array_end   = array_slice($this->liste,($idafter+1));
+        $this->liste=array_merge($array_start,$array_new,$array_end);
     }
 
     /**
diff --git a/htdocs/core/class/menubase.class.php b/htdocs/core/class/menubase.class.php
index 980cc22b453e426bd8e71aef1c6e9d71196cbcff..70bb2737757a76472b5dfea51ddb64403845591c 100644
--- a/htdocs/core/class/menubase.class.php
+++ b/htdocs/core/class/menubase.class.php
@@ -85,7 +85,7 @@ class Menubase
         $this->menu_handler=trim($this->menu_handler);
         $this->module=trim($this->module);
         $this->type=trim($this->type);
-        $this->mainmenu=trim($this->mainmenu);        // If type='top'
+        $this->mainmenu=trim($this->mainmenu);
         $this->leftmenu=trim($this->leftmenu);
         $this->fk_menu=trim($this->fk_menu);          // If -1, fk_mainmenu and fk_leftmenu must be defined
         $this->fk_mainmenu=trim($this->fk_mainmenu);
@@ -152,12 +152,12 @@ class Menubase
         $sql.= " ".($this->fk_mainmenu?"'".$this->fk_mainmenu."'":"null").",";
         $sql.= " ".($this->fk_leftmenu?"'".$this->fk_leftmenu."'":"null").",";
         $sql.= " '".$this->position."',";
-        $sql.= " '".$this->url."',";
-        $sql.= " '".$this->target."',";
-        $sql.= " '".$this->titre."',";
-        $sql.= " '".$this->langs."',";
-        $sql.= " '".$this->perms."',";
-        $sql.= " '".$this->enabled."',";
+        $sql.= " '".$this->db->escape($this->url)."',";
+        $sql.= " '".$this->db->escape($this->target)."',";
+        $sql.= " '".$this->db->escape($this->titre)."',";
+        $sql.= " '".$this->db->escape($this->langs)."',";
+        $sql.= " '".$this->db->escape($this->perms)."',";
+        $sql.= " '".$this->db->escape($this->enabled)."',";
         $sql.= " '".$this->user."'";
         $sql.= ")";
 
@@ -244,11 +244,12 @@ class Menubase
     }
 
 
-    /*
-     *    \brief      Load object in memory from database
-     *    \param      id          id object
-     *    \param      user        User that load
-     *    \return     int         <0 if KO, >0 if OK
+    /**
+     *   Load object in memory from database
+     *
+     *   @param		int		$id         Id object
+     *   @param		User    $user       User that load
+     *   @return	int         		<0 if KO, >0 if OK
      */
     function fetch($id, $user=0)
     {
@@ -328,7 +329,7 @@ class Menubase
         $sql = "DELETE FROM ".MAIN_DB_PREFIX."menu";
         $sql.= " WHERE rowid=".$this->id;
 
-        dol_syslog("Menubase::delete sql=".$sql);
+        dol_syslog(get_class($this)."::delete sql=".$sql);
         $resql = $this->db->query($sql);
         if (! $resql)
         {
@@ -396,27 +397,11 @@ class Menubase
         }
 
         $newTabMenu=array();
-        $i=0;
         if (is_array($tabMenu))
         {
             foreach($tabMenu as $val)
             {
-                if ($val['type']=='top')
-                {
-                    $newTabMenu[$i]['rowid']=$val['rowid'];
-                    $newTabMenu[$i]['fk_menu']=$val['fk_menu'];
-                    $newTabMenu[$i]['url']=$val['url'];
-                    $newTabMenu[$i]['titre']=$val['titre'];
-                    $newTabMenu[$i]['right']=$val['perms'];
-                    $newTabMenu[$i]['target']=$val['target'];
-                    $newTabMenu[$i]['mainmenu']=$val['mainmenu'];
-                    $newTabMenu[$i]['leftmenu']=$val['leftmenu'];
-                    $newTabMenu[$i]['enabled']=$val['enabled'];
-                    $newTabMenu[$i]['type']=$val['type'];
-                    $newTabMenu[$i]['langs']=$val['langs'];
-
-                    $i++;
-                }
+                if ($val['type']=='top') $newTabMenu[]=$val;
             }
         }
 
@@ -472,31 +457,44 @@ class Menubase
             // Update fk_menu when value is -1 (left menu added by modules with no top menu)
             foreach($tabMenu as $key => $val)
             {
+                //var_dump($tabMenu);
                 if ($val['fk_menu'] == -1 && $val['fk_mainmenu'] == $mainmenu)    // We found a menu entry not linked to parent with good mainmenu
                 {
+                    //print 'Try to add menu (current is mainmenu='.$mainmenu.' leftmenu='.$leftmenu.') for '.join(',',$val).' fk_mainmenu='.$val['fk_mainmenu'].' fk_leftmenu='.$val['fk_leftmenu'].'<br>';
+                    //var_dump($this->newmenu->liste);exit;
+
                     if (empty($val['fk_leftmenu']))
                     {
-                        //print 'Try to add menu with '.$mainmenu.' for '.join(',',$val);
-                        //var_dump($this->newmenu->liste);exit;
                         $this->newmenu->add($val['url'], $val['titre'], 0, $val['perms'], $val['target'], $val['mainmenu'], $val['leftmenu']);
+                        //var_dump($this->newmenu->liste);
                     }
-                    else if ($val['fk_leftmenu'] == $leftmenu)
+                    else
                     {
-                        //print 'Try to add menu with '.$mainmenu.' for '.join(',',$val);
-                        //var_dump($this->newmenu->liste);exit;
-
-                        // Search higher menu level with this couple (mainmenu,leftmenu)=(fk_mainmenu,fk_leftmenu)
+                        // Search first menu with this couple (mainmenu,leftmenu)=(fk_mainmenu,fk_leftmenu)
+                        $searchlastsub=0;$lastid=0;$nextid=0;$found=0;
                         foreach($this->newmenu->liste as $keyparent => $valparent)
                         {
                             //var_dump($valparent);
+                            if ($searchlastsub)    // If we started to search for last submenu
+                            {
+                                if ($valparent['level'] >= $searchlastsub) $lastid=$keyparent;
+                                if ($valparent['level'] < $searchlastsub)
+                                {
+                                    $nextid=$keyparent;
+                                    break;
+                                }
+                            }
                             if ($valparent['mainmenu'] == $val['fk_mainmenu'] && $valparent['leftmenu'] == $val['fk_leftmenu'])
                             {
-                                //print "We found parent: level=".$valparent['level'];
-                                // TODO
-                                // We add menu entry
-                                break;
+                                //print "We found parent: keyparent='.$keyparent.' - level=".$valparent['level'].' - '.join(',',$valparent).'<br>';
+                                // Now we look to find last subelement of this parent (we add at end)
+                                $searchlastsub=($valparent['level']+1);
+                                $lastid=$keyparent;
+                                $found=1;
                             }
                         }
+                        //print 'We must insert menu entry between entry '.$lastid.' and '.$nextid.'<br>';
+                        if ($found) $this->newmenu->insert($lastid, $val['url'], $val['titre'], $searchlastsub, $val['perms'], $val['target'], $val['mainmenu'], $val['leftmenu']);
                     }
                 }
             }
@@ -553,7 +551,7 @@ class Menubase
                 if ($menu['perms'])
                 {
                     $perms = verifCond($menu['perms']);
-                    //print "verifCond rowid=".$menu['rowid']." ".$menu['right'].":".$perms."<br>\n";
+                    //print "verifCond rowid=".$menu['rowid']." ".$menu['perms'].":".$perms."<br>\n";
                 }
 
                 // Define $enabled
@@ -600,10 +598,6 @@ class Menubase
                     $tabMenu[$b]['target']      = $menu['target'];
                     $tabMenu[$b]['mainmenu']    = $menu['mainmenu'];
                     $tabMenu[$b]['leftmenu']    = $menu['leftmenu'];
-                    /*if (! isset($tabMenu[$b]['perms'])) $tabMenu[$b]['perms'] = $perms;
-                    else $tabMenu[$b]['perms']  = ($tabMenu[$b]['perms'] && $perms);
-                    if (! isset($tabMenu[$b]['enabled'])) $tabMenu[$b]['enabled'] = $enabled;
-                    else $tabMenu[$b]['enabled'] = ($tabMenu[$b]['enabled'] && $enabled);*/
                     $tabMenu[$b]['perms']       = $perms;
                     $tabMenu[$b]['enabled']     = $enabled;
                     $tabMenu[$b]['type']        = $menu['type'];
@@ -645,8 +639,8 @@ class Menubase
             if ($tab[$x]['fk_menu'] == $pere && $tab[$x]['enabled'])
             {
                 //print 'mainmenu='.$tab[$x]['mainmenu'];
-                $this->newmenu->add($tab[$x]['url'], $tab[$x]['titre'], $level - 1, $tab[$x]['perms'], $tab[$x]['target'], $tab[$x]['mainmenu']);
-                $this->recur($tab, $tab[$x]['rowid'], $level + 1);
+                $this->newmenu->add($tab[$x]['url'], $tab[$x]['titre'], ($level-1), $tab[$x]['perms'], $tab[$x]['target'], $tab[$x]['mainmenu'], $tab[$x]['leftmenu']);
+                $this->recur($tab, $tab[$x]['rowid'], ($level+1));
             }
         }
    }
diff --git a/htdocs/core/menus/smartphone/smartphone.lib.php b/htdocs/core/menus/smartphone/smartphone.lib.php
index a4821a0ab6fde9863ea7654cf90f4c66cd8dc580..5750f883c9d2f870c092db7a1e4bb9235d829c5d 100755
--- a/htdocs/core/menus/smartphone/smartphone.lib.php
+++ b/htdocs/core/menus/smartphone/smartphone.lib.php
@@ -51,7 +51,7 @@ function print_smartphone_menu($db,$atarget,$type_user,$limitmenuto)
 	{
 		if ($newTabMenu[$i]['enabled'] == true)
 		{
-			if ($newTabMenu[$i]['right'] == true)	// Is allowed
+			if ($newTabMenu[$i]['perms'] == true)	// Is allowed
 			{
 				// Define url
 				if (preg_match("/^(http:\/\/|https:\/\/)/i",$newTabMenu[$i]['url']))
diff --git a/htdocs/core/menus/standard/auguria.lib.php b/htdocs/core/menus/standard/auguria.lib.php
index eb365b3256130bed08d62e2e9296a181e09a4994..8377ff4fc4fa90a561153ac3c3d40327c6ea47f4 100644
--- a/htdocs/core/menus/standard/auguria.lib.php
+++ b/htdocs/core/menus/standard/auguria.lib.php
@@ -54,7 +54,7 @@ function print_auguria_menu($db,$atarget,$type_user)
 		if ($newTabMenu[$i]['enabled'] == true)
 		{
 			$idsel=(empty($newTabMenu[$i]['mainmenu'])?'none':$newTabMenu[$i]['mainmenu']);
-			if ($newTabMenu[$i]['right'] == true)	// Is allowed
+			if ($newTabMenu[$i]['perms'] == true)	// Is allowed
 			{
 				// Define url
 				if (preg_match("/^(http:\/\/|https:\/\/)/i",$newTabMenu[$i]['url']))
@@ -281,6 +281,8 @@ function print_left_auguria_menu($db,$menu_array_before,$menu_array_after)
                 $url.='mainmenu='.$mainmenu;
             }
 
+            print '<!-- Add menu entry with mainmenu='.$menu_array[$i]['mainmenu'].', leftmenu='.$menu_array[$i]['leftmenu'].', level='.$menu_array[$i]['mainmenu'].' -->'."\n";
+
             // Menu niveau 0
             if ($menu_array[$i]['level'] == 0)
             {
diff --git a/htdocs/core/menus/standard/eldy.lib.php b/htdocs/core/menus/standard/eldy.lib.php
index 207035d2736db080c6bfae9d9b5bb0a2729a6cf8..cfddbdda8a39eb758af81798145625456be83c78 100644
--- a/htdocs/core/menus/standard/eldy.lib.php
+++ b/htdocs/core/menus/standard/eldy.lib.php
@@ -450,7 +450,7 @@ function print_eldy_menu($db,$atarget,$type_user)
 			//var_dump($newTabMenu[$i]);
 
 			$idsel=(empty($newTabMenu[$i]['mainmenu'])?'none':$newTabMenu[$i]['mainmenu']);
-			if ($newTabMenu[$i]['right'] == true)	// Is allowed
+			if ($newTabMenu[$i]['perms'] == true)	// Is allowed
 			{
 				if (preg_match("/^(http:\/\/|https:\/\/)/i",$newTabMenu[$i]['url']))
 				{
@@ -1357,6 +1357,8 @@ function print_left_eldy_menu($db,$menu_array_before,$menu_array_after)
             // For external modules
             $url = dol_buildpath($menu_array[$i]['url'], 1);
 
+            print '<!-- Add menu entry with mainmenu='.$menu_array[$i]['mainmenu'].', leftmenu='.$menu_array[$i]['leftmenu'].', level='.$menu_array[$i]['level'].' -->'."\n";
+
             // Menu niveau 0
             if ($menu_array[$i]['level'] == 0)
             {