/* Ajax form wrapper */
function rdAjaxPost(args) {
	// Private vars;
	var cFormID = args.cFormID;
	var oForm = $('#' + cFormID);
	var cAction = args.cAction ? args.cAction : oForm.attr('action');
	var cLogID = args.cLogID ? args.cLogID : cFormID + '_log';
	var oLog = cLogID != 'nolog' ? $('#' + cLogID) : $("<div />");  
	var cClassLoader = args.cClassLoader ? args.cClassLoader : 'loader';
	var cClassError = args.cClassError ? args.cClassError : 'error';
	var cClassSuccess = args.cClassSuccess ? args.cClassSuccess : 'success';
	var cSuccessMsg = args.cSuccessMsg ? args.cSuccessMsg : 'Information saved successfully';
	var oData = args.oData ? args.oData : oForm.serialize();
	var oRequest;
	var json = '';
	
	return {
		send: function() {
			// Submit;
			var self = this;
			 
			// Empty Log, Remove Error Class, Add Loader Class;
			this.get_oLog().empty().removeClass(this.get_cClassError()).addClass(this.get_cClassLoader());
			
			// Run custom function;
			this.submit_custom(self);
			
			// Submit Data;
			var ro = $.ajax({
				type: "POST",
				url: this.get_cAction(),
				data: this.get_oData(),
				success: function(json, status, xml){
					self.receive(json);
				}
			});

			this.set_oRequest(ro);
		},
		receive: function(json){
			var self = this;
		// Receive and process data
			this.set_json(json);

		// Empty Log, Remove Error Class, Add Loader Class;
			this.get_oLog().removeClass(this.get_cClassLoader());
		
		// Load JSON;
			eval('result = ' + this.json);
		
		// If Success;
			if(result.success == true){
				this.get_oLog().empty().addClass(this.get_cClassSuccess()).html(this.get_cSuccessMsg());
				this.receive_custom();
			    
		// If Failure;
			} else {
				var errorMsg = '';
				
				if(typeof(result.errors) != 'object'){
					errorMsg = result.errors;
				}else{
					for(iVal in result.errors){
						errorMsg += result.errors[iVal]+"<br />";
					}
				}
				this.get_oLog().empty().addClass(this.get_cClassError()).removeClass(this.get_cClassSuccess()).html(errorMsg);
			};
		},
		set_json: function(_json) {
			this.json = _json;
		},
		set_oRequest: function(_ro){
			this.oRequest = _ro;
		},
		get_oRequest: function(){
			return this.oRequest;
		},
		get_json: function() {
			return json;
		},
		get_cFormID: function() {
			return cFormID;
		},
		get_iFormType: function() {
			return iFormType;
		},
		get_oForm: function() {
			return oForm;
		},
		get_cAction: function() {
			return cAction;
		},
		get_cLogID: function() {
			return cLogID;
		},
		get_oLog: function() {
			return oLog;
		},
		get_cClassLoader: function() {
			return cClassLoader;
		},
		get_cClassError: function() {
			return cClassError;
		},
		get_cClassSuccess: function() {
			return cClassSuccess;
		},
		get_cSuccessMsg: function() {
			return cSuccessMsg;
		},
		get_oData: function() {
			return oData;
		},
		abort: function(){
			this.oRequest.abort();
		},
		reset_oData: function() {
			oData = args.oData ? args.oData : oForm.serialize();
		},
		submit_custom: typeof(args.submit_custom) == 'function' ? args.submit_custom : function () {},
		receive_custom: typeof(args.receive_custom) == 'function' ? args.receive_custom : function () {}
	};
};