// Thought Junction Javascript Components.
// Based on Extjs 2.2
//
// Components Included
//
//  tjtext
//  tjhidden
//  tjpassword
//	tjtextarea
//  tjcheckbox
//  tjmulticheck		= Ext	checkboxgroup
//  tjradio				= Ext	radiogroup

// All components must have these functions:
// GetName:  returning the valid name for error messaging.
// focus:  With the ability to select all.


Ext.namespace('TJ.comp');


// tjtext = TJ.comp.Text;

TJ.comp.Text = Ext.extend(Ext.form.TextField,{
	width: 300,
	readOnly: false,
	 initComponent:function() {
		if(typeof(this.maskRe) =='string')
			this.maskRe=new RegExp(this.maskRe);
		 this.hiddenName = this.name;
         // call parent initComponent
        TJ.comp.Text.superclass.initComponent.call(this);
		this.on('specialkey',
			function(field,e){ if (e.getKey() == e.ENTER) {  field.fireEvent('dosubmit', field); }});
 
    }, // end of function initCompon
	setReadOnly:function(r){
		this.readOnly=r;
//		return;
		if(r) {

            this.el.dom.setAttribute('readOnly', true);
            this.el.addClass('x-item-disabled');
		} else {
            this.el.dom.removeAttribute('readOnly');
            this.el.removeClass('x-item-disabled');
		}
	},
    onRender : function(ct, position){
        TJ.comp.Text.superclass.onRender.call(this, ct, position);
		this.setReadOnly(this.readOnly);
	},
    validateValue : function(value){
		if(! TJ.comp.OneLangRequired.call(this)){
			return false;
		}
		return TJ.comp.Text.superclass.validateValue.call(this, value);
		
    }


});
Ext.reg('tjtext',TJ.comp.Text);
TJ.comp.ChangePassword = Ext.extend(TJ.comp.Text,{
	width: 200,
	inputType :'password'
});
Ext.reg('tjchangepassword',TJ.comp.ChangePassword);



TJ.comp.Email = Ext.extend(TJ.comp.Text, {
	maxLength: 72
});
Ext.reg('tjemail',TJ.comp.Email);

// tjhidden = Ext.form.Hidden;
Ext.reg('tjhidden',Ext.form.Hidden);

// tjpassword = TJ.comp.Password

TJ.comp.Password = Ext.extend(TJ.comp.Text, {
	inputType :'password'
});
Ext.reg('tjpassword',TJ.comp.Password);

Ext.reg('tjfloat',Ext.form.NumberField);

TJ.comp.IntegerField = Ext.extend(Ext.form.NumberField,{
	allowDecimals: false,
	readOnly: false,
	setReadOnly:function(r){
		this.readOnly=r;
		if(r) {
            this.el.dom.setAttribute('readOnly', true);
            this.el.addClass('x-item-disabled');
		} else {
            this.el.dom.removeAttribute('readOnly');
            this.el.removeClass('x-item-disabled');
		}
	},
    onRender : function(ct, position){
        TJ.comp.IntegerField.superclass.onRender.call(this, ct, position);
		this.setReadOnly(this.readOnly);
	}

});
Ext.reg('tjinteger',TJ.comp.IntegerField);



// tjtextarea = TJ.comp.TextArea
TJ.comp.TextArea = Ext.extend(Ext.form.TextArea,{
	width: 400,
	height: 64	,
    validateValue : function(value){
		if(! TJ.comp.OneLangRequired.call(this)){
			return false;
		}
		return TJ.comp.TextArea.superclass.validateValue.call(this, value);
		
    }
});
Ext.reg('tjtextarea',TJ.comp.TextArea);


// Checkbox

// TJ.comp.checkbox:  tjcheckbox
// Use a CheckboxGroup and a single checkbox.

TJ.comp.Checkbox = Ext.extend(Ext.form.CheckboxGroup,{
	blankText: "This field is required",
	width: 40,
	initComponent: function() {
		this.items = [{name: this.name, checked: this.checked}];
        TJ.comp.Checkbox.superclass.initComponent.call(this);
		this.on('specialkey',
			function(field,e){ if (e.getKey() == e.ENTER) {  field.fireEvent('dosubmit', field); }});
	},
    focus : function(selectText, delay){
		if (this.rendered)
		{
			if(this.items[0])
				this.items[0].focus(selectText,delay);

		}
	},
	getValue: function(){
		if(this.rendered){
			return this.items.items[0].checked;
		}
		return this.checked;

	},

	setValue: function(value){
		if(this.rendered){
			this.items.items[0].setValue(value);
		}
		else
			this.checked=value;
	}

});
Ext.reg('tjcheckbox',TJ.comp.Checkbox);
	
TJ.comp.MultiCheck = Ext.extend(Ext.form.CheckboxGroup,{
	width:  600,
	columns: 1,
	allowBlank: false,
	initComponent: function() {
		this.items=[];
		var initvalue= (this.value || '').split(',');


		for(var i = 0 ; i < this.values.length; i++){
			this.items.push({	boxLabel: this.options[i] || this.values[i], 
								name: this.name+'[]', 
								inputValue: this.values[i],
								checked: (initvalue.indexOf(this.values[i])) >=0
							});
		}
        TJ.comp.MultiCheck.superclass.initComponent.call(this);
		this.on('specialkey',
			function(field,e){ if (e.getKey() == e.ENTER) {  field.fireEvent('dosubmit', field); }});
	},
	focus: function(selectText,delay){
		TJ.comp.MultiCheck.superclass.focus(false,delay);
	},
	getName: function(){
		return this.name;
	},
	setValue: function(value){
		if(this.rendered){
			val=value.split(',');
			this.items.each(function(item) {
				item.setValue(val.indexOf(this.inputValue)>=0);
		    });
		}
		else
			this.value=value;
	}

});
Ext.reg('tjmulticheck',TJ.comp.MultiCheck);

TJ.comp.Radio = Ext.extend(Ext.form.RadioGroup,{
	width:  400,
	columns: 1,
	allowBlank: false,
//	autoWidth: true,
	initComponent: function() {
		this.items=[];
		for(var i = 0 ; i < this.values.length; i++){
			this.items.push({	boxLabel: this.options[i] || this.values[i], 
								name: this.name, 
								inputValue: this.values[i],
								checked: this.values[i]==this.value
							});
		}
        TJ.comp.Radio.superclass.initComponent.call(this);
		this.on('specialkey',
			function(field,e){ if (e.getKey() == e.ENTER) {  field.fireEvent('dosubmit', field); }});
	},
	focus: function(selectText,delay){
		TJ.comp.Radio.superclass.focus(false,delay);
	},
	getName: function(){
		return this.name;
	},
setValue: function(v) {
     if(this.rendered)
    this.items.each(function(item) {
         item.setValue(item.getRawValue() == v);
       });
   else {
	    for(k in this.items) this.items[k].checked = this.items[k].inputValue == v;
   }
   }
});
Ext.reg("tjradio", TJ.comp.Radio);

TJ.comp.UserFile = Ext.extend(Ext.form.FileUploadField,{
	width: 400,
	allowBlank: false,
	buttonCfg : { iconCls : 'icon_folder_magnify',
				  text :''},
	getName: function(){
		return this.name;
	}

});


Ext.reg("tjuserfile",TJ.comp.UserFile);
