/* $VERSION: phparray.js 3.1.032.1.2011.02.09.09.11	 */
function jsarray2php(obj)
{
	var string = '';
	var count = 0;

	if (typeof obj == 'function') return;
	if (typeof(obj) == 'object') {
		if (obj instanceof Array) {
			for (var key in obj) {
				if (typeof obj[key] == 'function') continue;
				string += jsarray2php(key);
				string += jsarray2php(obj[key]);
				count++;
			}
			string = 'a:' + count + ':{' + string + '}'
		}
		else if (obj instanceof Object) {
			classname = obj.toString();
			if (classname == '[object Object]') {
				classname = 'StdClass';
			}
			for (var key in obj) {
				string += jsarray2php(key);
				if (obj[key])
					string += jsarray2php(obj[key]);
				else
					string += jsarray2php('');
				count++;
			}
			string = 'O:' + classname.length + ':"' + classname + '":' + count + ':{' + string + '}';
		}
	}
	else {
		switch (typeof(obj)) {
			case 'number':
				string = ((obj - Math.floor(obj) != 0) ? 'd' : 'i') + obj + ';'; break;
			case 'string':
				string += 's:' + obj.length + ':"' + obj + '";'; break;
			case 'boolean':
				string = ((obj) ? 'b:1' : 'b:0') + ';';	break;
		}
	}

	return string;
}

function phparray2js (php)
{
	php = php.replace(/:&quot;/g, ':"');
	php = php.replace(/&quot;;/g, '";');
	this.php = php;
}

phparray2js.prototype = {
	transformer : function(dim) {
		var tab = new Array();
		for (var i=0;i<dim;i++) {
			var indice = this.extraireIndice();
			if (indice == -1) return;
			var valeur = this.extraireValeur();
			if (valeur == -1) 
				tab[indice] = undefined;
			else {
				switch (valeur[0]) {
					case "N" : tab[indice] = null; break;
					case "b" : tab[indice] = valeur[1] ? true : false; break;
					case "i" : tab[indice] = parseInt(valeur[1]); break;
					case "d" : tab[indice] = parseFloat(valeur[1]); break;
					case "s" : tab[indice] = valeur[1]; break;
					case "a" : tab[indice] = this.transformer(valeur[1]); break;
					default : tab[indice] = undefined;
				}
			}
		}
		this.php = this.php.substring(1);
		return tab;
	},
	
	extraireDimTab : function() {
		var reg = this.php.match(/^a:(\d+):\{/);
		if (reg != -1) {
			this.php = this.php.substring(reg[0].length);
			return reg[1];
		} else
			return -1;
	},
	
	extraireIndice :function() {
		var retour;
		var reg = this.php.match(/^((i):(\d+);|(s):\d+:"([^"]+)";)/);
		if (reg != -1) {
			this.php = this.php.substring(reg[0].length);
			if (reg[2] == "i") 
				retour = reg[3];
			else if (reg[4] == "s") 
				retour = reg[5];
			else 
				retour = -1;
		} else 
			retour = -1;
		return retour;
	},
	
	extraireValeur :function() {
		var retour;
		var reg = this.php.match(/^((N);|(b):([01]);|(i):(\d+);|(d):([\d\.]+);|(s):\d+:"([^"]*)";|(a):(\d+):\{)/);
		if (reg != -1) {
			this.php = this.php.substring(reg[0].length);
			if (reg[2] == "N") retour = new Array("N", null); // valeur nulle
			else if (reg[3] == "b")	retour = new Array("b", reg[4]); // booléen (true/false)
			else if (reg[5] == "i")	retour = new Array("i", reg[6]); // entier
			else if (reg[7] == "d")	retour = new Array("d", reg[8]); // entier double ou flottant
			else if (reg[9] == "s")	retour = new Array("s", reg[10]); // chaîne de caractères
			else if (reg[11] == "a") retour = new Array("a", reg[12]); // sous-tableau
			else retour = -1;
		}
		else
			retour = -1;
		return retour;
	},
	
	retour : function()	{
		var dim = this.extraireDimTab();
		this.tabjs = this.transformer(dim);
		return this.tabjs;
	}
}
