PPForm = function(config) {
	Ext.apply(this, config);
	this.form = Ext.get('pp-form-'+this.id);
	Ext.EventManager.addListener(this.form.dom, 'submit', this.submit, this);
}
PPForm.prototype = {
	getField: function(name, returnEl) {
		var fn = returnEl !== false ? 'get' : 'getDom';
		return Ext[fn]('pp-form-field-'+this.id+'-'+name);
	},
	getFieldContainer: function(name) {
		var field = this.getField(name);
		if (!field) return null;
		var ct = field.findParent('.pp-form-field-ct', 10, true);
		return ct;
	},
	getFieldInvalidMsgEl: function(name) {
		var ct = this.getFieldContainer(name);
		if (!ct) return null;
		var el = ct.child('.pp-form-field-invalid-msg');
		return el;
	},
	fieldSetDisplayed: function(name, displayed) {
		var ct = this.getFieldContainer(name);
		if (ct) ct.setDisplayed(displayed);
	},
	registerStateField: function(state, country) {
		var countryField = this.getField(country, false);
		this.fieldSetDisplayed(state, countryField.value == 'us');
		Ext.EventManager.addListener(countryField, 'change', function() {
			this.fieldSetDisplayed(state, countryField.value == 'us');
		}, this);
	},
	mask: function(msg) {
		this.form.mask(msg, 'x-mask-loading');
	},
	unmask: function() {
		this.form.unmask();
	},
	getValues: function() {
		var params = {}, item, key, value;
		for (var i = 0; i < this.form.dom.elements.length; i++) {
			item = this.form.dom.elements[i];
			key = item.name;
			if (!key) continue;
			if (item.tagName == 'INPUT' && item.type == 'checkbox') {
				value = item.checked ? '1' : '';
			} else if (item.tagName == 'INPUT' && item.type == 'radio') {
				if (!item.checked) continue;
				value = item.value;
			} else {
				value = item.value;
			}
			params[key] = value;
		}
		return params;
	},
	submit: function(e) {
		e.stopEvent();
		this.hideMsg();
		this.clearInvalid();
		this.mask('Vent venligst...');
		var params = this.getValues();
		if (typeof this.post_params == 'object') Ext.apply(params, this.post_params);
		Ext.Ajax.request({
			url: this.post_url,
			params: params,
			callback: this.submitCallback,
			scope: this
		})
	},
	submitCallback: function(o, s, r) {
		var result = handleJSONResponse(s, r);
		if (!result) {
			this.unmask();
			this.showMsg('Der kunne ikke oprettes forbindelse til serveren. Prøv venligst igen.', true)
			return;
		}
		if (result.success == false) {
			this.unmask();
			if (result.msg) {
				this.showMsg(result.msg, true);
			}
			if (result.errors instanceof Array) {
				Ext.each(result.errors, function(error) {
					this.markInvalid(error.id, error.msg);
				}, this);
				if (!result.msg) {
					this.showMsg('Et eller flere af felterne er ikke udfyldt korrekt. Ret venligst disse, og forsøg igen.', true);
				}
			}
			return;
		}
		if (this.reset_on_success) {
			this.reset();
		}
		if (this.success_msg) {
			this.showMsg(this.success_msg);
			this.unmask();
		} else if (this.success_url) {
			document.location.href = this.success_url;
		} else if (result.success_url) {
			document.location.href = result.success_url;
		} else if (result.success_msg) {
			this.showMsg(result.success_msg);
			this.unmask();
		} else {
			document.location.href = document.location.href;
		}
	},
	markInvalid: function(name, msg) {
		var el = this.getFieldInvalidMsgEl(name);
		if (!el) return;
		el.update(msg);
		el.show();
	},
	clearInvalid: function(name) {
		var els = this.form.select('.pp-form-field-invalid-msg');
		els.each(function(el) {
			el.setDisplayed(false);
		}, this);
	},
	showMsg: function(msg, error) {
		if (this.msg_as_alert) {
			alert(msg);
		} else {
			if (!this.msgEl) this.msgEl = Ext.get('pp-form-msg-'+this.id);
			if (error) this.msgEl.addClass('pp-form-msg-error');
			else this.msgEl.removeClass('pp-form-msg-error');
			this.msgEl.update(msg);
			this.msgEl.show();
			this.msgEl.scrollIntoView();
		}
	},
	hideMsg: function() {
		if (!this.msgEl) return;
		this.msgEl.setDisplayed(false);
	},
	reset: function() {
		this.form.dom.reset();
	}
};
