function formManager(form, cookie){
	this.form = form;
	(this.cookie = cookie) && this.init();
}
formManager.prototype = {
	setField :  function(key, val){
		var field = this.form.elements[key], add, tag, ret = [], rev;
		if(this.cookie){
			var c = Cookie(this.cookie), s = [], i, p;
			if(c){
				c = c.split('&');
				for(i = 0; i < c.length; i++){
					p = c[i].split('=');
					if(key != decodeURIComponent(p[0]))s.push(c[i]);
				}
			}
			if(val){
				c = encodeURIComponent(key) + '=';
				if(val.push){
					p = [];
					for(i = 0; i < val.length; i++)p[i] = encodeURIComponent(val[i]);
					s.push(c + p.join('&' + c));
				}else{
					s.push(c + encodeURIComponent(val));
				}
			}
			Cookie(this.cookie, s.join('&'));
		}
		if(field){
			field.length || (field = [field]);
			tag = field[0].tagName.toUpperCase();
			if(tag.substr(0, 6) == 'OPTION'){
				for(i = 0; i < field.length; i++){
					if(val){
						if(!rev){
							rev = val.legnth ? val : [val];
							field.selectedIndex = -1;
						}
						if(in_array(rev, field[i].value)){
							field.selectedIndex = i;
							field[i].selected = true;
						}else{
							field[i].selected = false;
						}
					}else{
						field[i].selected = false;
						i == field.length && (field.selectedIndex = -1);
					}
				}
				if(field.selectedIndex == -1){
					field.options[i] = new Option(val[0], val[0]);
					field.selectedIndex = i;
				}
			}else{
				switch(field[0].type.toUpperCase){
				case 'RADIO':
					p = 0;
					for(i = 0; i < field.length; i++)(field[i].checked = val && field[i].value == val) && (p = 1);
					!p && val && (add = 'radio');
					break;
				case 'CHECKBOX':
					if(val){
						p = 0;
						for(i = 0; i < field.length; i++){
							if(!rev)rev = val.legnth ? val : [val];
							(field[i].checked = in_array(rev, field[i].value)) && (p = 1);
						}
						p || (add = 'radio');
					}else{
						for(i = 0; i < field.length; i++)field[i].checked = false;
					}
					break;
				default :
					for(i = 0; i < field.length; i++)field[i].value = val || '';
					return;
				}
			}
		}
		if((!field && val) || add){
			this.form.elements[key] = field = document.createElement('input');
			field.name = key;
			field.type = add || 'hidden';
			field.value = val;
			if(add){
				field.style.display = 'none';
				field.checked = true;
			}
			this.form.appendChild(field);
		}
	},
	getField :  function(key){
		var field = this.form.elements[key], tag, ret = [];
		if(field){
			field.length || (field = [field]);
			for(i = 0; i < field.length; i++){
				tag = field[i].tagName.toUpperCase();
				if(tag.substr(0, 6) == 'OPTION'){
					if(field[i].selected)ret.push(field[i].value);
				}else{
					switch(field[i].type.toUpperCase){
					case 'RADIO':
						if(field[i].checked)return field[i].value;
						break;
					case 'CHECKBOX':
						if(field[i].checked)ret.push(field[i].value);
						break;
					default :
						return field[i].value;
					}
				}
			}
			return ret.length ? ret : null;
		}
	},
	delField : function(key){
		var field = this.form[key],i;
		if(field){
			field.length || (field = [field]);
			for(i = 0; i < field.length; i++)
				this.form.removeChild(field[i]);
			if(this.cookie && (c = Cookie(this.cookie))){
				c = c.split('&');
				var s = [], j, p;
				for(j = 0; j < c.length; j++){
					p = c[j].split('=');
					if(key != decodeURIComponent(p[0]))s.push(c[j]);
				}
				Cookie(this.cookie, s.join('&'))
			}
		}
	},
	clear : function(fields){
		if(!arguments.length){
			fields = this.form.elements;
			for(var i = 0; i < fields.length; i++)
				this.setField(fields[i].name);
		}
		typeof fields == 'string' && (fields = fields.split(','));
		for(var i = 0; i < fields.length; i++)
			this.setField(fields[i]);
	},
	submit : function(){
		(!this.onsubmit || this.onsubmit() !== false) && this.form.submit();
	},
	init : function(){
		if(!this.cookie)return;
		var c = Cookie(this.cookie), o = {}, i, p;
		if(!c)return;
		c = c.split('&');
		for(i = 0; i < c.length; i++){
			p = c[i].split('=');
			p[0] = decodeURIComponent(p[0]);
			p[1] = decodeURIComponent(p[1]);
			if(o[p[0]]){
				o[p[0]].push || (o[p[0]] = [o[p[0]]]);
				o[p[0]].push(p[1]);
			}else{
				o[p[0]] = p[1];
			}
		}
		for(i in o)this.setField(i, o[i]);
	}
}

function in_array(array, need){
	for(var i =0; i < array.length; i++)
		if(array[i] == need)
			return true;
	return false;
}

function Cookie(key, value, expires, path, domain, secure){
	key = encodeURIComponent(key);
	var t = expires, r = (new RegExp('(?:;\s*)?' + key + '=(.+?)(?:;|$)')).exec(document.cookie), e, f;
	if(value !== undefined){
		if(t && !(t instanceof Date)){
			e = t;t = new Date();
			e = /^([+-]?)(\d+)([YMWDHIS]?)$/i.exec(e) || [,,0];
			e[3] && (e[3] = e[3].toUpperCase());
			e[3] == 'W' && (e[3] = 'D') && (e[2] *= 7);
			f = {Y : 'FullYear', M : 'Month', D : 'Date', H : 'Hours', I : 'Minutes', S : 'Seconds'}[e[3] || 'I'];
			eval('t.set' + f + '(t.get' + f + '()' + (e[1] || '+') + e[2] + ')')
		}
		document.cookie = key + '=' + encodeURIComponent(value)
						+ (t ? ';expires=' + t.toGMTString() : '')
						+ '; path=' + (typeof path != 'string' ? '/' : path)
						+ (domain ? '; domain=' + domain : '')
						+ (secure ? '; secure' : '')
	}
	return r ? decodeURIComponent(r[1]) : r
}
