// A reusable error reader class for XML forms
Ext.form.XmlErrorReader = function(){
	Ext.form.XmlErrorReader.superclass.constructor.call(this, {
		record : 'field',
		success: '@success'
	}, [
	'id', 'msg'
	]
	);
};
Ext.extend(Ext.form.XmlErrorReader, Ext.data.XmlReader);

var EditLineMain = function() {

	var th = this;
	this.form = null;

	this.add = function() {
		this._do(0);
	}

	this.edit = function(id) {
		this._do(id);
	}

	this._do = function(id) {
		if (!this.form) {
			this.form = new Ext.form.BasicForm(app.cour_main.panel.body, {
				errorReader: new Ext.form.XmlErrorReader(),
				waitMsgTarget: true
			});
		}
		var default_text = '';
		if (id) {
			default_text = app.cour_main.getIdText(id);
			if(default_text == ''){
				msg_error('Cannot find requested node (' + id + ')');
				return;
			}
		}
		
		Ext.Msg.prompt('Course Editor', 'Новое название:', function(btn, text) {
			if (btn == 'ok' && text.trim()) {
				this.form.submit({
					params: {
						id: id,
						title: text
					},
					method: 'POST',
					waitMsg: 'Сохранение...',
					url: 'actions/panelmain.save.xml.php',

					failure: function(form, action) {
						msg_error('Line was not saved (server error). Please, reload the page and try again');
					},
					success: function(form, action) {
						if (id) {
							app.cour_main.reload();
						} else {
							//TODO: а что тут вааще? =)
							//msg_ok('Курс успешно добавлен.');
							app.cour_main.reload();
						}
					},
					scope: th
				});
			};
			if (btn == 'cancel') {
				
			};
		}, this, false, default_text);
	}
}
