// **************************************************************** //
// Serializer (utilisable avec la fonction 'unserialize' de PHP	 	//
// Auteur: 	RODIER Samuel (2006)								 	//
// Date: 	19/07/2006												//
// Tests:	Tester uniquement pour les valeurs (non-object)			//
// 			Pour Champamedia										//
// Remarque : Linux ajoute des anti-slashs, utilisez la fonction    //
//            PHP 'stripSlashes', si c'est le cas ...				//
// **************************************************************** //


// Type serialization ...
	function SerializeString (MyString) {
		return ( "s:" + MyString.length + ":\"" + MyString + "\";" );
	}
	function SerializeInt (MyInt) {
		return( "i:" + MyInt + ";" );
	}
	function SerializeBool (MyBool) {
		if (MyBool) {
			return( "b:1;" );
		} else {
			return( "b:0;" );
		}
	}
	function SerializeNull () {
		return( "N;" );
	}
	function SerializeFloat (MyFloat) {
		return( "d:" + MyFloat + ";" );
	}
	function SerializeArray (MyArray) {
		var sSer = "";
		var key;
		var iCount = 0;
		for (key in MyArray) {
			if (!isNaN(key)) {
				key = Number(key);
			}
			sSer += SerializeVariant (key);
			var value = MyArray[key];
			sSer += SerializeVariant (value);
			iCount++;
		}
		return( "a:" + iCount + ":{" + sSer + "}" );
	}

// Serialisez votre objet JavaScript avec cette fonction ...
	function SerializeVariant (MyVariant) {
		if ( MyVariant == null ) {
			return ( SerializeNull() );
		} else if ( typeof(MyVariant) == "boolean" ) {
			return ( SerializeBool (MyVariant) );
		}  else if ( typeof(MyVariant) == "number" ) {
			if (parseInt(MyVariant) == MyVariant) {
				return ( SerializeInt (MyVariant) );
			} else {
				return ( SerializeFloat (MyVariant) );
			}
		} else if ( typeof(MyVariant) == "string" ) {
			return ( SerializeString (MyVariant) );
		}else if ( typeof(MyVariant) == "object" ) {
			if (MyVariant instanceof Array) {
				return ( SerializeArray (MyVariant) );
			} else {
				return ( SerializeString ("SerializationError!") );
			}
		} else {
				return ( SerializeString ("SerializationError!") );
		}
	}