diff --git a/htdocs/core/lib/ajax.lib.php b/htdocs/core/lib/ajax.lib.php
index 3b3eec1eb4815e1dc4a9edb1e04b96f44329b526..9b4a18609dd0c42986941ce4c3ebdcead4698c24 100644
--- a/htdocs/core/lib/ajax.lib.php
+++ b/htdocs/core/lib/ajax.lib.php
@@ -92,14 +92,10 @@ function ajax_autocompleter($selected='',$htmlname,$url,$option='',$minLength=2,
  */
 function ajax_multiautocompleter($htmlname,$fields,$url,$option='',$minLength=2,$autoselect=0)
 {
-	$script='';
-
-	$fields = php2js($fields);
-
-	$script.= '<!-- Autocomplete -->'."\n";
+	$script = '<!-- Autocomplete -->'."\n";
 	$script.= '<script type="text/javascript">';
 	$script.= 'jQuery(document).ready(function() {
-					var fields = '.$fields.';
+					var fields = '.json_encode($fields).';
 					var length = fields.length;
 					var autoselect = '.$autoselect.';
 					//alert(fields + " " + length);
@@ -284,33 +280,4 @@ function ajax_constantonoff($code,$input=array())
 	return $out;
 }
 
-/**
- * Convert a PHP array into a js array
- * @param       $var
- * @return      String with js array or false if error
- */
-function php2js($var)
-{
-    if (is_array($var)) {
-        $res = "[";
-        $array = array();
-        foreach ($var as $a_var) {
-            $array[] = php2js($a_var);
-        }
-        return "[" . join(",", $array) . "]";
-    }
-    elseif (is_bool($var)) {
-        return $var ? "true" : "false";
-    }
-    elseif (is_int($var) || is_integer($var) || is_double($var) || is_float($var)) {
-        return $var;
-    }
-    elseif (is_string($var)) {
-        return "\"" . addslashes(stripslashes($var)) . "\"";
-    }
-    // autres cas: objets, on ne les gère pas
-    return false;
-}
-
-
 ?>
\ No newline at end of file
diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php
index 11e5ce3d68c862afb08a5d1552608e388aefbf71..272b106faf0af93263ff8fbf0e1f40cf4959ec14 100644
--- a/htdocs/core/lib/functions.lib.php
+++ b/htdocs/core/lib/functions.lib.php
@@ -41,11 +41,103 @@ if (! function_exists('json_encode'))
      */
     function json_encode($elements)
     {
-        if (is_array($elements)) return '["' . join('","', $elements) . '"]';
-        else return '"'.$elements.'"';
+    	$num = count($elements);
+    	
+    	// determine type
+    	if (is_numeric(key($elements)))
+    	{
+    		// indexed (list)
+    		$output = '[';
+    		for ($i = 0, $last = ($num - 1); isset($elements[$i]); ++$i)
+    		{
+    			if (is_array($elements[$i])) $output.= json_encode($elements[$i]);
+    			else $output .= _val($elements[$i]);
+    			if($i !== $last) $output.= ',';
+    		}
+    		$output.= ']';
+    	}
+    	else
+    	{
+    		// associative (object)
+    		$output = '{';
+    		$last = $num - 1;
+    		$i = 0;
+    		foreach($elements as $key => $value)
+    		{
+    			$output .= '"'.$key.'":';
+    			if (is_array($value)) $output.= json_encode($value);
+    			else $output .= _val($value);
+    			if ($i !== $last) $output.= ',';
+    			++$i;
+    		}
+    		$output.= '}';
+    	}
+    	
+    	// return
+    	return $output;
+    }
+    
+    function _val($val)
+    {
+    	if (is_string($val)) return '"'.rawurlencode($val).'"';
+    	elseif (is_int($val)) return sprintf('%d', $val);
+    	elseif (is_float($val)) return sprintf('%F', $val);
+    	elseif (is_bool($val)) return ($val ? 'true' : 'false');
+    	else  return 'null';
     }
 }
 
+if (! function_exists('json_decode'))
+{
+	/**
+	 * Implement json_decode for PHP that does not support it
+	 *
+	 * @param	string	$json		Json encoded to PHP Object or Array
+	 * @param	bool	$assoc		False return an object, true return an array
+	 * @return 	mixed				Object or Array
+	 */
+	function json_decode($json, $assoc=false)
+	{
+		$comment = false;
+		
+		for ($i=0; $i<strlen($json); $i++)
+		{
+			if (! $comment)
+			{
+				if (($json[$i] == '{') || ($json[$i] == '[')) $out.= 'array(';
+				else if (($json[$i] == '}') || ($json[$i] == ']')) $out.= ')';
+				else if ($json[$i] == ':') $out.= ' => ';
+				else $out.= $json[$i];
+			}
+			else $out.= $json[$i];
+			if ($json[$i] == '"' && $json[($i-1)]!="\\") $comment = !$comment;
+		}
+		
+		// Return an array
+		eval('$array = '.$out.';');
+		
+		// Return an object
+		if (! $assoc)
+		{
+			if (! empty($array))
+			{
+				$object = false;
+				
+				foreach ($array as $key => $value)
+				{
+					$object->{$key} = $value;
+				}
+				
+				return $object;
+			}
+			
+			return false;
+		}
+		
+		return $array;
+	}
+}
+
 
 /**
  * Return a DoliDB instance (database handler).