/**
 * NS Generic form
 * 
 * @version   1.00.090604
 * @author    LBI Lost Boys
 */
NS(function($){
	
	function Forms() {
		this.form = $('#form-application');
		NS.subscribe('change', this.handleChange.bind(this));
	}

	Forms.prototype = {
		Defaults: {
			validateRequired: true,
			validateServer: false,
			replaceInputs: true,
			simulateClick: true
		},

		owns: function(form) {
			return this.form[0] == form;
		},

		prefers: function(setting) {
			return this.Defaults[setting];
		},

		handleChange:function(e) {
			var input = e.target;
			if(/form-subject/.test(input.id)) {
				var form = input.form, post = NS.getFormValues(form);
				NS.XHR.sendAndLoad(post, NS.getProperty('POST_VEA_FORM'), this.handleResponse.bind(this));
			}
		},
		
		handleResponse:function(xml) {
			var form = $("form", xml).text();
			var target = $("#form-application");
			NS.DOM.replace($(form), target);
		}
	};

	NS.subscribe('initialize', function() {
		NS.addApplication('forms', new Forms());
	});
});