Form = Class.create();
Form.extend({
	ele:null,
	ajaxForm:function(id,type){
		var type = type;
		var form = this.ele = $(id);
		$$('input.file').each(
		function(v,i){
			$(v).setProperty('disabled','disabled');
		});
		var option = {
			url:form.action,
			beforeSubmit:formCheck,
			onSuccess: My.loadSuccess,
			resetForm: true
		};
		this.ajaxSubmit(option,type);
		function formCheck(formArray, jqForm){
			switch(type){
				case 'comment':
				if(!$('form_name').get('value')){
					alert('Please Enter Data!','alert');
					return false;
				}
				break;
				case 'blog':
				if(!$('form_subject').get('value')){
					alert('Please Enter Data!','alert');
					return false;
				}
				break;
				default:
				return true;
			}
			return true;
		};

		return false;
	},
	ajaxSubmit:function(options,type) {
		options = $extend({
			url:  this.ele.get('action') || window.location,
			type: this.ele.get('method') || 'GET'
		}, options || {});

		Fck.update();
		var a = this.formToArray();
		if (options.data) {
			for (var n in options.data)
			a.push( { name: n, value: options.data[n] } );
		}
		// give pre-submit callback an opportunity to abort the submit
		if (options.beforeSubmit && options.beforeSubmit(a, this, options) === false) return this;

		var q = this.param(a);//.replace(/%20/g,'+');
		if (options.type.toUpperCase() == 'GET') {
			options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q;
			options.data = null;  // data is null for 'get'
		}
		else{
			options.data = q;
		}
		var $form = this.ele, callbacks = [];
		if (options.resetForm) callbacks.push(function() { 
			$form.reset(); 
			Fck.clear();
		});

		if (options.onSuccess)
			callbacks.push(options.onSuccess);
		options.onSuccess = function(data, status) {
			for (var i=0, max=callbacks.length; i < max; i++)
			callbacks[i](data, status, $form);
		};
		var Reqeust = new Request.JSON(options).send();

		return this;
	},
	formToArray:function() {
		var a = [];
		var form = this.ele;
		var els = form.elements;
		if (!els) return a;
		for(var i=0, max=els.length; i < max; i++) {
			var el = els[i];
			var n = el.name;
			if (!n) continue;
			
			var v = this.fieldValue(el);
			if (v && v.constructor == Array) {
				for(var j=0, jmax=v.length; j < jmax; j++)
				a.push({name: n, value: v[j]});
			}
			else if (v !== null && typeof v != 'undefined')
			a.push({name: n, value: v});
		}
		return a;
	},
	fieldValue:function(el, successful) {
		var n = el.name, t = el.type, tag = el.tagName.toLowerCase();
		if (typeof successful == 'undefined') successful = true;

		if (successful && (!n || el.disabled || t == 'reset' || t == 'button' ||
		(t == 'checkbox' || t == 'radio') && !el.checked ||
		(t == 'submit' || t == 'image') && el.form && el.form.clk != el ||
		tag == 'select' && el.selectedIndex == -1))
		return null;

		if (tag == 'select') {
			var index = el.selectedIndex;
			if (index < 0) return null;
			var a = [], ops = el.options;
			var one = (t == 'select-one');
			var max = (one ? index+1 : ops.length);
			for(var i=(one ? index : 0); i < max; i++) {
				var op = ops[i];
				if (op.selected) {
					// extra pain for IE...
					var v = $.browser.msie && !(op.attributes['value'].specified) ? op.text : op.value;
					if (one) return v;
					a.push(v);
				}
			}
			return a;
		}
		return el.value;
	},
	param:function( a ) {
		var s = [];
		if ( a.constructor == Array)
			$each( a, function(v,i){
				s.push( encodeURIComponent(v.name) + "=" + encodeURIComponent( v.value ) );
			});
		else
			for ( var j in a )
				if ( a[j] && a[j].constructor == Array )
					$each( a[j], function(){
						s.push( encodeURIComponent(j) + "=" + encodeURIComponent( this ) );
					});
				else
					s.push( encodeURIComponent(j) + "=" + encodeURIComponent( a[j] ) );
		return s.join("&").replace(/%20/g, "+");
	}
})

