/**
 * jQuery Deserialize plugin
 * @author: Dawid Fatyga
 *
 * Forked from: http://github.com/jakubpawlowicz/jquery.deserialize
 *
**/

(function($){
	function toHash(){
		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"
	
		if(options.only && options.except)
			throw "You cannot pass both 'only' and 'except' options"
	
		var names = (options.except || []).toHash()
		var except = true
		if(options.only){
			names = options.only.toHash()
			except = false
		}
	
		callback = options.callback
		callback_on = options.callback_on || false
		if(callback_on)
			callback_on = callback_on.toHash()
	
	
		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){
				$("[" + attr + "='" + _name + "']", this).val(value)
				if(callback && ((!callback_on) || (_name in callback_on))){
					callback(_name, value)
				}
			}
		}
	}	
})(jQuery);

$.deserialize = function(s) {
			  var data = s.split("&");
			  var returnValue = {};
			  for (var i = 0; i < data.length; i++) {
			    var pair = decodeURIComponent(data[i]).split("=");
				//NOTE: I think you have to decode each 'component' separately, but I could be wrong - this will work 99.9% of the time.
				returnValue[pair[0]] = pair[1];
			    //$("[name='" + pair[0] + "']", this).val(pair[1]);
			  }
			  return returnValue;
			};

