
function serialize(incoming) {
    var nkeys = 0
    var outgoing = ''

    for(var key in incoming) {
        if(key.length == 0 || key == '') continue
        nkeys++
        
        outgoing = outgoing + 's:' + key.length + ':"' + key + '";'
        outgoing = outgoing + 's:' + incoming[key].length + ':"' + incoming[key] + '";'
    }
    var regex = /\n/g;
    outgoing = 'a:' + nkeys + ':{' + outgoing.replace(regex,",") + '}'
    return outgoing
}

function trimnl(input) {
    if(navigator.appVersion.indexOf('MSIE') != -1) return input;
    snl = /^[\n\s]*/g;
    enl = /[\n\s]*$/g;
    input = input.replace(snl,'')
    input = input.replace(enl,'')
    return input;
}

/**
* This file is part of the unserialize package (http://www.phpguru.org/)
*
* unserialize is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* unserialize is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with unserialize; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* © Copyright 2005 Richard Heyes
*   http://www.phpguru.org/
*/

    /**
    * Unserializes a PHP serialized data type. Currently handles:
    *  o Strings
    *  o Integers
    *  o Doubles
    *  o Arrays
    *  o Booleans
    *  o NULL
    *  o Objects
    * 
    * alert()s will be thrown if the function is passed something it
    * can't handle or incorrect data.
    *
    * @param  string input The serialized PHP data
    * @return mixed        The resulting datatype
    */

function unserialize(input) {
    var result = unserialize_(trimnl(input));
    return result[0];
}


    /**
    * Function which performs the actual unserializing
    *
    * @param string input Input to parse
    */
function unserialize_(input) {
    var length = 0;

    switch (input.charAt(0)) {
    /**
    * Array
    */
        case 'a':
            length = unserialize_length(input);
            input  = input.substr(String(length).length + 4);

            var arr   = new Array();
            var key   = null;
            var value = null;

            for (var i=0; i<length; ++i) {
                key   = unserialize_(input);
                input = key[1];

                value = unserialize_(input);
                input = value[1];

                arr[key[0]] = value[0];
            }

            input = input.substr(1);
            return [arr, input];
            break;
            
        /**
        * Objects
        */
        case 'O':
            length = unserialize_length(input);
            var classname = String(input.substr(String(length).length + 4, length));

            input  = input.substr(String(length).length + 6 + length);
            var numProperties = Number(input.substring(0, input.indexOf(':')))
            input = input.substr(String(numProperties).length + 2);

            var obj      = new Object();
            var property = null;
            var value    = null;

            for (var i=0; i<numProperties; ++i) {
                key   = unserialize_(input);
                input = key[1];
                    
                // Handle private/protected
                key[0] = key[0].replace(new RegExp('^\x00' + classname + '\x00'), '');
                key[0] = key[0].replace(new RegExp('^\x00\\*\x00'), '');

                value = unserialize_(input);
                input = value[1];

                obj[key[0]] = value[0];
            }

            input = input.substr(1);
            return [obj, input];
            break;

        /**
        * Strings
        */
        case 's':
            length = unserialize_length(input);
            return [String(input.substr(String(length).length + 4, length)), input.substr(String(length).length + 6 + length)];
            break;

        /**
        * Integers and doubles
        */
        case 'i':
        case 'd':
            var num = Number(input.substring(2, input.indexOf(';')));
            return [num, input.substr(String(num).length + 3)];
            break;
            
        /**
        * Booleans
        */
        case 'b':
            var bool = (input.substr(2, 1) == 1);
            return [bool, input.substr(4)];
            break;
            
        /**
        * Null
        */
        case 'N':
            return [null, input.substr(2)];
            break;

        /**
        * Unsupported
        */
        case 'o':
        case 'r':
        case 'C':
        case 'R':
        case 'U':
            alert('Error: Unsupported PHP data type found!');

        /**
        * Error
        */
        default:
            return [null, null];
            break;
    }
}
    

/**
* Returns length of strings/arrays etc
*
* @param string input Input to parse
*/
function unserialize_length(input) {
    input = input.substring(2);
    var length = Number(input.substr(0, input.indexOf(':')));
    return length;
}

function unserialize_delete_key(key_in, array_in) {
    var array_out = Array()
    
    for(var key in array_in) {
        if(key != key_in)
            array_out[key] = array_in[key]
    }
    return array_out
}
