// BaseFormControl Client Script Library
// Alfa-XP, Michael Isipciuc, 2007.
//
Type.registerNamespace("FM");
Type.registerNamespace("FM.UI");

FM.UI.IObservable= function() {}
FM.UI.IObservable.prototype = {
    AddObserver: function(){},
    NotifyObservers: function(){}
}
FM.UI.IObservable.registerInterface('FM.UI.IObservable');

FM.UI.IObserver= function() {}
FM.UI.IObserver.prototype = {
    Controller_HandleEvent: function(){}
}
FM.UI.IObserver.registerInterface('FM.UI.IObserver');


FM.UI.BaseFormControl= function(element) { // inherited from IObservable
		FM.UI.BaseFormControl.initializeBase(this);//, [element]
		this._element= element;
		this._control= element; // store control attributes// store data (text control), but can be only for attributes (picklist)
		this._focusElement= null;
		this._ui= element.nextSibling; // ui container (visibility)
		if (this._ui.nodeType == 3) this._ui= this._ui.nextSibling;
		this.Name= element.getAttribute('field');
		this.IsRequired= element.getAttribute('req') == '1';
		this._isChanged= element.getAttribute('changed') == '1';
		this.Enabled= element.getAttribute("enabled") != "0";
		this.Readonly= !this.Enabled;
		this.Visibled= true;
		this.tabID= element.getAttribute('tabID');
		this.onchange_script= element.getAttribute("onchange_script");
		this.onfocus_script= element.getAttribute("onfocus_script");
		this._encodexml= false;
		this._updateIsRequired= false; // now for boolean fields
		this.Title= element.getAttribute('title');
		
		var bs_json= element.getAttribute('bindings');
		this._bindings= (bs_json != null && bs_json != undefined) ? eval(bs_json) : null;
		//alert(this.Name + "\n"+ this._bindings);
		//eval("{[{'from':'aa','to':'bb'},{'from':'ff','to':'44'}]}");
		this._hostForm= null;
		this.notifyObserversDlg= null;
		
		this.smartTagFocus= true;
		this._valid= false;
		this.titleCell= null;
		this._eventsAttached= false;
}

FM.UI.BaseFormControl.prototype= {

  initialize : function() {
		FM.UI.BaseFormControl.callBaseMethod(this, 'initialize');

		if (!this._isChanged) {
			this.defaultValue= this.getValue();
		}
		if (this.onchange_script) this.AddObserver(Function.createDelegate(this, this.evalOnChangeScript));
		if (this.Enabled) this.attachEvents();
		if (!this._focusElement) this._focusElement= this._control;
  },
  
  get_element : function() { return this._element; },
  
  dispose : function() {
		if (this.notifyObserversDlg && this._control) { this.RegisterOnChangeHandler(this.notifyObserversDlg, false);	this.notifyObserversDlg= null; }
		this.detachEvents();
		
    FM.UI.BaseFormControl.callBaseMethod(this, 'dispose');
  },

  canFocus : function() {
		return (this._focusElement && this.Enabled && this.Visibled);
  },
  
  setValue : function(val) { this._control.value= val; },
  getValue : function() { return this._control.value; },
  getDisplayValue : function() { return this._control.value; },
  
  focus : function() { if (this._hostForm) this._hostForm.showTab(this.tabID); if (this.canFocus()) this._focusElement.focus(); },
  
  show : function(b) {
		if (b == false) { this.Visibled= false; this._ui.style.display= 'none';} 
		else { this.Visibled= true; this._ui.style.display= ''; }
  },
  
  IsChanged : function() {
		if (this._isChanged) return true;
		this._isChanged= (this.getValue() != this.defaultValue);
		return this._isChanged;
  },
  
  resetValue : function() { this.setValue(this.defaultValue, false); this._IsChanged= false; },
  
  DisplayMissingValue : function() {
		Form__YouMustProvide= "Please enter";
		alert(window.Form__YouMustProvide + " " + this.Title + ".");
		this.focus();
  },
  
  defineTitleCell : function() {
		if (this.titleCell != null) return;
  },
  
  IsBindable : function() { return this._bindings != null; },
  
  prepareProgress : function() { window.status= "Loading...";},
  hideProgress : function() { window.status= "";},
  onServiceFailed : function(res, element) { element.hideProgress(); alert(res.get_message()); },
  
  // XML
  ConvertToXml : function(raiseMessage, isNew) {
		var val= this.getValue();
		if (val == "" && this.IsRequired) {
			if (raiseMessage) this.DisplayMissingValue();
			return null;
		}
		//if (!isNew && val == this.defaultValue) return ""; // removed checking !IsNew // 2007-06-26
		if (!this._updateIsRequired && val == this.defaultValue) return "";
		
		return this.buildXmlNode(val);
  },
  buildXmlNode : function(val) { 
		return "<" + this.Name + ">" + (this._encodexml ? XmlEncode(val) : val) + "</" + this.Name + ">";
  },
  
  evalOnChangeScript : function(_event_) {
		if (!this.onchange_script) return;
		var _Form_= this._hostForm;
		try { eval(this.onchange_script); } catch (e) { alert("The custom event handler for the field '" + this.Name + "' has been failed.\n\n\n" + e.description + "\nError Type: " + e.name); }
		//var res= false;
		//try { eval(this.onchange_script); } catch (e) { res= confirm("The custom event handler for the field '" + this.Name + "' has been failed.\n\n\n" + e.description + "\nError Type: " + e.name + "\n\n\nDo you want to try to debug?"); } if (res) eval(this.onchange_script);
  },
  
  // Events
  add_afterChange: function(h) { this.get_events().addHandler("afterChange", h); },
  remove_afterChange: function(h) { this.get_events().removeHandler("afterChange", h); },
  raiseAfterChange: function(e) { 
		var h= this.get_events().getHandler("afterChange"); if (h) h(e); 
	},
	
	RaiseEvent : function() { this.evalOnChangeScript() },

	// IObservable	
  NotifyObservers : function(e) { this.raiseAfterChange(e); },
  AddObserver : function(handler) {
		this.add_afterChange(handler);
		if (!this.notifyObserversDlg && this._control) {
			this.notifyObserversDlg= Function.createDelegate(this, this.NotifyObservers);
			this.RegisterOnChangeHandler(this.notifyObserversDlg, true);
		}
  },
  RegisterOnChangeHandler : function(h, reg) { 
		if (reg) $addHandler(this._control, 'change', h); else $removeHandler(this._control, 'change', h);
	},
  
  //Properties
  get_enabled: function() { return this.Enabled; },
  set_enabled: function(val) {
		if (val !== this.Enabled) {
			this.Enabled= val;
			this._control.disabled= !this.Enabled;
			this.redraw();
			if (this.Enabled) this.attachEvents(); else this.detachEvents();			
		}
  },
  
  attachEvents : function() { },
  detachEvents : function() { },
	redraw : function() { },
	
	validate : function() {
		this._valid= true;
		var val= this.getValue();
		if (val == "" && this.IsRequired) { this.DisplayMissingValue(); this._valid= false; }
		return this._valid;
	},
	
	alert : function(msg) {
		window.alert(msg);
		if (this._hostForm) this._hostForm.focusElement();
		this.focus();
	},
  
	getInfo : function() { 
		return this.Name + "\n----------------------\nIsRequired: " + this.IsRequired + "\nEnabled: " + this.Enabled + "\nVisibled: " + this.Visibled + "\n----------------------\nOnChangeScript:\n" + this.onchange_script;
	}
}

FM.UI.BaseFormControl.inheritsFrom(Sys.Component);
FM.UI.BaseFormControl.registerClass('FM.UI.BaseFormControl', Sys.Component, Sys.IDisposable, FM.UI.IObservable);


Sys.Application.notifyScriptLoaded();
