if (!window.console){
	window.console = {
		log: Ext.emptyFn,
		debug: Ext.emptyFn
	};
}

Ext.ns("n.app");

n.app.commentForm = function(){
	var that, frm, itms, error;
	function config(){
		frm.on("submit", submit);
		frm.on("reset", function(){
			error.show();
			error.update("");
		});
	};
	function submit(evt){
		evt.preventDefault();
		var vals = that.getValues();
		var submitBtn = Ext.select("button[type=submit]").item(0);
		
		submitBtn.dom.disabled = true;
		if (that.isValid()) {
			Ext.Ajax.request({
				url: "com/ajax.cfc",
				params: {method: "addComment", json: Ext.encode(vals)},
				success: function(response, opts){
					var json = Ext.decode(response.responseText);
					frm.dom.reset();
					window.location.reload();
				},
				failure: function(response, opts){
					//alert("Failure: Try again later.");
					console.log(arguments);
				}
			});
		}
		else {
			error.update("Complete the orange fields.");
			error.show();
			submitBtn.dom.disabled = false;
		}
		
	};
	return {
		init: function(){
			that = this;
			frm = Ext.select("form");
			if (frm.getCount()){
				frm = frm.item(0);
				itms = frm.dom.elements;
				error = frm.select(".error").item(0);
				config();
			}
		},
		getValues: function(){
	        var flds = Ext.Ajax.serializeForm(frm.dom);
	        return Ext.urlDecode(flds);
		},
		findField: function(name){
			return Ext.get(that.el.select('[name="'+name+'"]').item(0).dom);
		},
		isValid: function(){
			var valid = true;
			Ext.each(itms, function(el){
				if (Ext.fly(el).hasClass("required") && el.value.length == 0)
					valid = false;
			});
			return valid;
		}
	}
};
