/**
 * jQuery Deserialize plugin
 * @author: Dawid Fatyga
 *
 * Forked from: http://github.com/jakubpawlowicz/jquery.deserialize
 *
**/

/* Builtin Array class extension, which converts itself to map */
Array.prototype.toHash = function(){
    var map = {};
    for(var i = 0;i < this.length; i++)
        map[this[i]] = '';
    return map;
};

$.fn.deserialize = function(s, options) {
    var data = s.split("&");

    options = options || {};
    attr = options.attribute || "name";
    var names = (options.except || []).toHash();
    var except = true;
    if(options.only){
        names = options.only.toHash();
        except = false;
    }

    for (var i = 0; i < data.length; i++) {
        var pair = decodeURIComponent(data[i]).split("=");
        var _name = pair[0];
        var value = pair[1];
        if(except != _name in names){
            if(value == "true" || value == "false"){
                if(value == "false"){
                    $("[" + attr + "='" + _name + "']", this).removeAttr('checked');
                }else{
                    $("[" + attr + "='" + _name + "']", this).attr('checked',value);
                }
            }else{
                $("[" + attr + "='" + _name + "']", this).val(value);
            }
        }
    }
}


