diff --git a/htdocs/core/ajax/constantonoff.php b/htdocs/core/ajax/constantonoff.php
index 1231b4ff9ebf283dd278f6e8a1689e6a45304944..277d62191c347aee1e3d7a385eaefccd40d25042 100644
--- a/htdocs/core/ajax/constantonoff.php
+++ b/htdocs/core/ajax/constantonoff.php
@@ -45,17 +45,21 @@ top_httphead();
 print '<!-- Ajax page called with url '.$_SERVER["PHP_SELF"].'?'.$_SERVER["QUERY_STRING"].' -->'."\n";
 
 // Registering the location of boxes
-if((isset($_GET['action']) && ! empty($_GET['action'])) && (isset($_GET['name']) && ! empty($_GET['name'])) )
+if ((isset($_GET['action']) && ! empty($_GET['action'])) && (isset($_GET['name']) && ! empty($_GET['name'])) )
 {
+	$entity = (GETPOST('entity','int') ? GETPOST('entity','int') : $conf->entity);
+	$action = GETPOST('action', 'alpha');
+	$name = GETPOST('name', 'alpha');
+	
 	if ($user->admin)
 	{
-		if ($_GET['action'] == 'set')
+		if ($action == 'set')
 		{
-			dolibarr_set_const($db, GETPOST('name','alpha'), 1, 'chaine', 0, '', GETPOST('entity','int'));
+			dolibarr_set_const($db, $name, 1, 'chaine', 0, '', $entity);
 		}
-		else if ($_GET['action'] == 'del')
+		else if ($action == 'del')
 		{
-			dolibarr_del_const($db, GETPOST('name','alpha'), GETPOST('entity','int'));
+			dolibarr_del_const($db, $name, $entity);
 		}
 	}
 }
diff --git a/htdocs/core/ajax/row.php b/htdocs/core/ajax/row.php
index 882a28f9c42143fc140af95142a7bf14c89ca59f..c7761cdfd27cbe36aa8b2464e0302ed340f95fa4 100644
--- a/htdocs/core/ajax/row.php
+++ b/htdocs/core/ajax/row.php
@@ -1,5 +1,5 @@
 <?php
-/* Copyright (C) 2010-2011 Regis Houssin  <regis@dolibarr.fr>
+/* Copyright (C) 2010-2012 Regis Houssin  <regis@dolibarr.fr>
  *
  * 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
@@ -35,11 +35,6 @@ require_once(DOL_DOCUMENT_ROOT."/core/class/genericobject.class.php");
  * View
  */
 
-// Ajout directives pour resoudre bug IE
-//header('Cache-Control: Public, must-revalidate');
-//header('Pragma: public');
-
-//top_htmlhead("", "", 1);  // Replaced with top_httphead. An ajax page does not need html header.
 top_httphead();
 
 print '<!-- Ajax page called with url '.$_SERVER["PHP_SELF"].'?'.$_SERVER["QUERY_STRING"].' -->'."\n";
@@ -52,21 +47,16 @@ if((isset($_GET['roworder']) && !empty($_GET['roworder'])) && (isset($_GET['tabl
 
 	foreach($roworder as $value)
 	{
-		if (!empty($value))
-		{
-			$newroworder[] = $value;
-		}
+		if (! empty($value)) $newroworder[] = $value;
 	}
 
-	$roworder = implode(',',$newroworder);
-
-	dol_syslog("AjaxRow roworder=".$_GET['roworder']." neworder=".$roworder." element=".$_GET['element'], LOG_DEBUG);
+	dol_syslog("AjaxRow roworder=".$_GET['roworder']." fk_element=".$_GET['fk_element'], LOG_DEBUG);
 
 	$row=new GenericObject($db);
 	$row->table_element_line = $_GET['table_element_line'];
 	$row->fk_element = $_GET['fk_element'];
 	$row->id = $_GET['element_id'];
-	$result=$row->line_ajaxorder($roworder);
+	$result=$row->line_ajaxorder($newroworder);
 	$result=$row->line_order(true);
 }
 
diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php
index 076cd861ddbef9a709c09048d2cbd231170dcfce..9549eaf371d02a0da48434aad7939f26c460e142 100644
--- a/htdocs/core/class/commonobject.class.php
+++ b/htdocs/core/class/commonobject.class.php
@@ -890,24 +890,74 @@ abstract class CommonObject
 		}
 		if ($nl > 0)
 		{
+			$rows=array();
+			
 			$sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.$this->table_element_line;
 			$sql.= ' WHERE '.$this->fk_element.' = '.$this->id;
+			$sql.= ' AND fk_parent_line IS NULL';
 			$sql.= ' ORDER BY rang ASC, rowid '.$rowidorder;
 
 			dol_syslog(get_class($this)."::line_order sql=".$sql, LOG_DEBUG);
 			$resql = $this->db->query($sql);
 			if ($resql)
 			{
+				$i=0;
 				$num = $this->db->num_rows($resql);
-				$i = 0;
 				while ($i < $num)
 				{
 					$row = $this->db->fetch_row($resql);
-					$this->updateRangOfLine($row[0], ($i+1));
+					$rows[] = $row[0];
+					$childrens = $this->getChildrensOfLine($row[0]);
+					if (! empty($childrens))
+					{
+						foreach($childrens as $child)
+						{
+							array_push($rows, $child);
+						}
+					}
 					$i++;
 				}
+
+				if (! empty($rows))
+				{
+					foreach($rows as $key => $row)
+					{
+						$this->updateRangOfLine($row, ($key+1));
+					}
+				}
+			}
+		}
+	}
+	
+	/**
+	 * 	Get childrens of line
+	 * 
+	 * 	@param	int		$id		Id of parent line
+	 */
+	function getChildrensOfLine($id)
+	{
+		$rows=array();
+		
+		$sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.$this->table_element_line;
+		$sql.= ' WHERE '.$this->fk_element.' = '.$this->id;
+		$sql.= ' AND fk_parent_line = '.$id;
+		$sql.= ' ORDER BY rang ASC';
+		
+		dol_syslog(get_class($this)."::getChildrenOfLines sql=".$sql, LOG_DEBUG);
+		$resql = $this->db->query($sql);
+		if ($resql)
+		{
+			$i=0;
+			$num = $this->db->num_rows($resql);
+			while ($i < $num)
+			{
+				$row = $this->db->fetch_row($resql);
+				$rows[$i] = $row[0];
+				$i++;
 			}
 		}
+		
+		return $rows;
 	}
 
     /**
@@ -966,13 +1016,11 @@ abstract class CommonObject
     /**
      * 	Update position of line with ajax (rang)
      *
-     * 	@param		int		$roworder
+     * 	@param	array	$rows	Array of rows
      */
-    function line_ajaxorder($roworder)
+    function line_ajaxorder($rows)
     {
-        $rows = explode(',',$roworder);
         $num = count($rows);
-
         for ($i = 0 ; $i < $num ; $i++)
         {
             $this->updateRangOfLine($rows[$i], ($i+1));
diff --git a/htdocs/core/tpl/ajaxrow.tpl.php b/htdocs/core/tpl/ajaxrow.tpl.php
index ca7dee493b2e141474eac44d8fcf7b93f0133047..59dc7b8ed7885b9b321ddd70ec447666ec81ed59 100644
--- a/htdocs/core/tpl/ajaxrow.tpl.php
+++ b/htdocs/core/tpl/ajaxrow.tpl.php
@@ -1,5 +1,5 @@
 <?php
-/* Copyright (C) 2010-2011 Regis Houssin       <regis@dolibarr.fr>
+/* Copyright (C) 2010-2012 Regis Houssin       <regis@dolibarr.fr>
  * Copyright (C) 2010-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  *
  * This program is free software; you can redistribute it and/or modify
@@ -21,22 +21,22 @@
 <!-- BEGIN PHP TEMPLATE FOR JQUERY -->
 <?php if (count($object->lines) > 1 && $_GET['action'] != 'editline') { ?>
 <script>
-jQuery(document).ready(function(){
-	jQuery(".imgup").hide();
-	jQuery(".imgdown").hide();
-    jQuery(".lineupdown").removeAttr('href');
-    jQuery(".tdlineupdown").css("background-image",'url(<?php echo DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/grip.png'; ?>)');
-    jQuery(".tdlineupdown").css("background-repeat","no-repeat");
-    jQuery(".tdlineupdown").css("background-position","center center");
+$(document).ready(function(){
+	$(".imgup").hide();
+	$(".imgdown").hide();
+    $(".lineupdown").removeAttr('href');
+    $(".tdlineupdown").css("background-image",'url(<?php echo DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/grip.png'; ?>)');
+    $(".tdlineupdown").css("background-repeat","no-repeat");
+    $(".tdlineupdown").css("background-position","center center");
 
-    jQuery("#tablelines").tableDnD({
+    $("#tablelines").tableDnD({
 		onDrop: function(table, row) {
 			var reloadpage = "<?php echo $conf->global->MAIN_FORCE_RELOAD_PAGE; ?>";
-			var roworder = cleanSerialize(jQuery("#tablelines").tableDnDSerialize());
+			var roworder = cleanSerialize($("#tablelines").tableDnDSerialize());
 			var table_element_line = "<?php echo $object->table_element_line; ?>";
 			var fk_element = "<?php echo $object->fk_element; ?>";
 			var element_id = "<?php echo $object->id; ?>";
-			jQuery.get("<?php echo DOL_URL_ROOT; ?>/core/ajax/row.php",
+			$.get("<?php echo DOL_URL_ROOT; ?>/core/ajax/row.php",
 					{
 						roworder: roworder,
 						table_element_line: table_element_line,
@@ -47,11 +47,11 @@ jQuery(document).ready(function(){
 						if (reloadpage == 1) {
 							location.href = '<?php echo $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; ?>';
 						} else {
-							jQuery("#tablelines .drag").each(
+							$("#tablelines .drag").each(
 									function( intIndex ) {
-										jQuery(this).removeClass("pair impair");
-										if (intIndex % 2 == 0) jQuery(this).addClass('impair');
-										if (intIndex % 2 == 1) jQuery(this).addClass('pair');
+										$(this).removeClass("pair impair");
+										if (intIndex % 2 == 0) $(this).addClass('impair');
+										if (intIndex % 2 == 1) $(this).addClass('pair');
 									});
 						}
 					});
@@ -59,17 +59,17 @@ jQuery(document).ready(function(){
 		onDragClass: "dragClass",
 		dragHandle: "tdlineupdown"
 	});
-    jQuery(".tdlineupdown").hover(    	function() { jQuery(this).addClass('showDragHandle'); },
-    	function() { jQuery(this).removeClass('showDragHandle'); }
+    $(".tdlineupdown").hover( function() { $(this).addClass('showDragHandle'); },
+    	function() { $(this).removeClass('showDragHandle'); }
     );
 });
 </script>
 <?php } else { ?>
 <script>
-jQuery(document).ready(function(){
-	jQuery(".imgup").hide();
-	jQuery(".imgdown").hide();
-    jQuery(".lineupdown").removeAttr('href');
+$(document).ready(function(){
+	$(".imgup").hide();
+	$(".imgdown").hide();
+    $(".lineupdown").removeAttr('href');
 });
 </script>
 <?php } ?>