
	
/*** FORMS ***/
	
// Object for binding an ajax object to an html tag by ajaxkey
var AjaxForm = Class.create();
AjaxForm.prototype = {
	// Default constructor.  All arguments are optional.		
	initialize: function(id, data) {
		this.id = id;
		if (data != null) this.bind(data);			
	},
	// Binds all fields with ajaxkey defined to the datasource.
	bind: function(data) {
		if (data != null) this.data = data;
		AjaxFormBinder.setForm(this.id, this.data);
	},
	// Copies all fields with ajaxkey defined to the datasource.
	update: function() {
		return AjaxFormBinder.copyForm(this.data, this.id);
	},
	dataSource: null
}

// Object that automatically calls Ajax.Pro to load itself.
var AjaxAutoForm = Class.create();
Object.extend(Object.extend(AjaxAutoForm.prototype, AjaxForm.prototype), {
	// Default constructor.  All arguments are optional.		
	initialize: function() {
		// id for select tag
		this.id = null;
		// method to call to retrieve data
		this.loadMethod = null;
		// arguments to send to method
		this.loadMethodArgs = null;
		// method to call to retrieve data
		this.saveMethod = null;
		// ajax object this method exists in
		this.dataSource = null;
	},
	dataSource: null,
	// Override these methods to receive ajax notification events.
	onLoadSuccess: function(value) {},
	onLoadError: function(error) {alert(error);},
	onSaveSuccess: function(value) {},
	onSaveError: function(error) {},
	// Loads form data from ajax.net pro.
	// I think the serialization is going to be flaky here.  need to call invoke directly.
	load: function() {
		if (this.loadMethod != null && this.dataSource != null) {
		    var method = this.dataSource + '.' + this.loadMethod;
			var js = (this.loadMethodArgs == null)
				? method + '(this.loadCallback.bind(this))'
				: method + '(\'' + escape(this.loadMethodArgs) + '\', this.loadCallback.bind(this))';					
			try { eval(js);} catch (e) { this.onLoadError(e); }
		}			
		else {
			this.onLoadError('AjaxAutoForm.loadMethod not defined');
		}
	},
	// Saves the contents of this form via ajax.net pro.
	save: function() {
		if (this.saveMethod != null && this.dataSource != null) {
		    this.update();
			var js = this.dataSource + '.' + this.saveMethod + '(this.data, this.saveCallback.bind(this))';					
			try { eval(js); } catch (e) { this.onSaveError(e); }
		}						
		else {
			this.onSaveError('AjaxAutoForm.saveMethod not defined.');
		}
	},
	// Binds data to the form upon ajax callback.
	loadCallback: function(res) {
		if (res.error) {
			this.onLoadError(res.error);
		}
		else {
			this.bind(res.value);
			this.onLoadSuccess(res.value);				
		}
	},
	// Binds data to the form upon ajax callback.
	saveCallback: function(res) {
		if (res.error) {
			this.onSaveError(res.error);
		}
		else {
			this.onSaveSuccess(res.value);				
		}
	}	
});

// Creates a new instance of AjaxAutoForm using named arguments.
AjaxAutoForm.createInstance = function(args) {
	var ret = Object.extend(new AjaxAutoForm(), args);
	ret.load();
	return ret;
}	
	
