// Constructor
YAHOO.lib.Form = function(el, config) {
	this.page = null;
	this.proxy = null;
	this.upload = null;
	YAHOO.lib.Form.superclass.constructor.call(this, el, config);
}
YAHOO.lib.Form._SUBMIT_BUTTON 	= 'SubmitButton';
YAHOO.lib.Form._CANCEL_BUTTON 	= 'CancelButton';
YAHOO.lib.Form._MESSAGER 		= 'Messager';
YAHOO.lib.Form._TOOLTIP 		= 'Tooltip';
// Create
YAHOO.lib.Form.create = function(el, config) {
	if (YAHOO.lang.isUndefined(config)) config = {};
	return new YAHOO.lib.Form(el, config);
}
YAHOO.extend(YAHOO.lib.Form, YAHOO.widget.Dialog, {
	// Init
	init: function(el, config) {
		if (YAHOO.lang.isUndefined(config.draggable)) config.draggable = true;
		if (YAHOO.lang.isUndefined(config.fixedcenter)) config.fixedcenter = false;
		if (YAHOO.lang.isUndefined(config.constraintoviewport)) config.constraintoviewport = false;
		if (YAHOO.lang.isUndefined(config.underlay)) config.underlay = 'none';
		if (YAHOO.lang.isUndefined(config.modal)) config.modal = true;
		if (YAHOO.lang.isUndefined(config.visible)) config.visible = false;
		if (!YAHOO.lang.isUndefined(config.upload) && (config.upload)) {
			this.upload = true;
			config.postmethod = 'manual';
		}
		YAHOO.lib.Form.superclass.init.call(this, el, config);
		this.page = (!YAHOO.lang.isUndefined(config.page)) ? config.page : null;
		this.id = el;
		YAHOO.lib.util.Element.assignOnClick(el + YAHOO.lib.Form._SUBMIT_BUTTON, this, this.submitHandler);
		YAHOO.lib.util.Element.assignOnClick(el + YAHOO.lib.Form._CANCEL_BUTTON, this, this.cancelHandler);
		this.render();
		this.resetTooltips();
		if (!config.proxy) {
			this.proxy = this.form.getAttribute('action');
			if (!this.proxy) this.proxy = '?';
		} else this.proxy = config.proxy;
		if (this.form) {
			this.form.setAttribute('action', this.proxy);
			YAHOO.util.Event.removeListener(this.form, 'keydown');
			YAHOO.util.Event.addListener(this.form, 'keydown', this.onKeyPress, this, true);
		}
	}, 
	// On Key Press
	onKeyPress: function(e, obj) {
		var target = YAHOO.util.Event.getTarget(e);
		var code = YAHOO.util.Event.getCharCode(e);
		if ((code == 13) && (target.tagName) && (target.tagName.toUpperCase() == 'INPUT')) {
			var inputType = target.type;
			if ((inputType == 'text') || (inputType == 'password') || (inputType == 'checkbox') || 
				(inputType == 'radio') || (inputType == 'file')) obj.submitHandler();
		}
	}, 
	// Get query
	getQuery: function(aname, aphase, query) {
		return this.page.getQuery(aname, aphase, query);
	}, 
	// Get element
	getElement: function() {
		if (this.element && this.element.firstChild) return this.element.firstChild; else return null;
	}, 
	// Get height
	getHeight: function() {return YAHOO.lib.util.Element.getHeight(this.element);}, 
	// Get messager config
	getMessagerConfig: function() {
		var config = {page: this.page, parent: this};
		return config;
	}, 
	// Create messager
	createMessager: function() {
		if (!this.messager) {
			var el = this.id + YAHOO.lib.Form._MESSAGER;
			var config = this.getMessagerConfig();
			this.messager = YAHOO.lib.Messager.create(el, config);
		} else this.messager.hide();
	}, 
	// Show messages
	showMessages: function(responce) {
		this.createMessager();
		if (this.messager && responce && responce.messages) {
			this.messager.showMessages(responce.messages);
		}
	}, 
	// Destroy tooltips 
	destroyTooltips: function() {
		if (this.tooltips && (this.tooltips instanceof Array) && (this.tooltips.length > 0)) {
			var tooltip = null
			while (this.tooltips.length > 0) {
				tooltip = this.tooltips.pop();
				tooltip.destroy();
			}
		}
	}, 
	// Create tooltips
	createTooltips: function() {
		this.tooltips = [];
		var elements = YAHOO.lib.Tooltip.getContextElements(this.element);
		if (elements && (elements instanceof Array) && (elements.length > 0)) {
			var id = null;
			for (var i = 0; i < elements.length; i++) {
				id = this.id + YAHOO.lib.Form._TOOLTIP + i;
				this.tooltips[this.tooltips.length] = YAHOO.lib.Tooltip.create(id, {context: elements[i]});
			}
		}
	}, 
	// Reset tooltips
	resetTooltips: function() {
		this.destroyTooltips();
		this.createTooltips();
	}, 
	// Create simple text editors
	createSimpleTextEditors: function() {
		this.simpleTextEditors = [];
		var richTextAreas = YAHOO.lib.util.Element.getElementsByClassName(this.element, 'richtext');
		if (richTextAreas && (richTextAreas instanceof Array) && (richTextAreas.length > 0)) {
			var id = null;
			for (var i = 0; i < richTextAreas.length; i++) {
				id = this.id + 'SimpleTextEditor' + i;
				var config = {height: '300px', width: '530px', dompath: true, focusAtStart: true};
				richTextAreas[i].setAttribute('id', id);
				var editor = new YAHOO.widget.SimpleEditor(id, config);
				editor.render();
				this.simpleTextEditors[this.simpleTextEditors.length] = editor;
			}
		}
	}, 
	// Upload Handler
	uploadSuccess: function(o) {
		this.manualSubmitEvent.unsubscribeAll();
		this.submitSuccess(o);
	}, 
	// Upload
	uploadHandler: function() {
		var callback = {scope: this, upload: this.uploadSuccess};
		YAHOO.util.Connect.setForm(this.form, true);
		YAHOO.util.Connect.asyncRequest('POST', this.form.getAttribute('action'), callback);
	}, 
	// Submit handler
	submitHandler: function() {
		this.page.showWait();
		if (this.upload) {
			this.manualSubmitEvent.subscribe(this.uploadHandler, this, true);
		} else {
			this.callback = {scope: this, success: this.submitSuccess, failure: this.submitFailure};
		} 
		this.submit();
		this.show();
	}, 
	// Cancel handler
	cancelHandler: function() {
		this.cancel();
	}, 
	// Show
	show: function() {
		YAHOO.lib.Form.superclass.show.call(this);
		YAHOO.lib.util.Element.show(this.getElement());
		//this.createSimpleTextEditors();
	}, 
	// Hide
	hide: function() {
		YAHOO.lib.Form.superclass.hide.call(this);
		YAHOO.lib.util.Element.hide(this.getElement());
	}, 
	// Process success result
	processSuccessResult: function(responce) {
		this.showMessages(responce);
	}, 
	// Process failure result
	processFailureResult: function(responce) {
		this.showMessages(responce);
	}, 
	// Submit success
	submitSuccess: function(o) {
		this.page.hideWait();
		var responce = this.page.getJSObject(o.responseText);
		if (responce) {
			if (responce.result == 1) {
				this.processSuccessResult(responce);
			} else this.processFailureResult(responce);
			if (responce.redirect) this.page.redirect(responce.redirect);
		}
	}, 
	// Submit failure
	submitFailure: function(o) {
		this.page.hideWait();
		this.page.loadingFailure();
	}, 
	// Request Success
	requestSuccess: function(o) {this.page.hideWait();}, 
	// Request Failure
	requestFailure: function(o) {this.page.hideWait();}, 
	// Request
	request: function(handler, action, phase, proxy, query) {
		if (!handler) handler = this.requestSuccess;
		if (!action) action = 'edit';
		if (!phase) phase = 'enter';
		if (!proxy) proxy = this.proxy;
		var _query = this.getQuery(action, phase, query);
		var callback = {scope: this, success: handler, failure: this.requestFailure};
		YAHOO.util.Connect.asyncRequest('POST', proxy, callback, _query);
		this.page.showWait();
	}, 
	// Get Field
	getField: function(name) {
		var elem = null;
		if (this.form && this.form.elements) {
			for (var i in this.form.elements) {
				if ((this.form.elements[i]) && (this.form.elements[i].name) && (this.form.elements[i].name == name)) {
					elem = this.form.elements[i]; break;
				} 
			}
		}
		return elem;
	}, 
	// Get Field Value
	getFieldValue: function(name) {
		var data = this.getData();
		if (data && data[name]) return data[name];
		else return null;
	}, 
	// Set Options
	setOptions: function(elementName, data, valueKey, titleKey, defaultValue, constantOptionsCount) {
		if (!constantOptionsCount) constantOptionsCount = 1;
		var element = this.getField(elementName);
		element.options.length = constantOptionsCount;
		if (element && data && (data instanceof Array)) {
			var record = null;
			var option = null;
			for (var i = 0; i < data.length; i++) {
				record = data[i];
				option = new Option(String(record[titleKey]), String(record[valueKey]));
				if (record[valueKey] == defaultValue) option.selected = true;
				element.options.add(option);
			}
		}
	}, 
	// To string
	toString: function() {return 'Form Object';}
});
