/**
 * Podstawowy obiekt formularzy w terminalu.
 *
 * @type	TDynamicObject
 */
TDynamicObject	= function()
{
	arguments = arguments[0];
	var p 			= _get(arguments[0]);
	this.obj		= _c('div');
	this.obj.parent	= p;
	this.obj.ol		= _get(arguments[1]);
	this.obj.parent.appendChild( this.obj );
	this.obj.id		= arguments[2];
	this.obj.auto_decrement	= 0;
	this.obj.form_name	= arguments[3];
	//this.obj.name_id	= 'niezdefiniowany_id';
	this.obj.name_id	= arguments[4];
	//-- event
	this.obj.event	= function( dane )
	{
		this.li = new TCheckListElement( this.form_name+'['+this.auto_decrement+']' , this.name_id , dane );
		this.auto_decrement--;
		if (this.li != null) this.ol.appendChild( this.li.uchwyt );
	}
	return this;		
}

/**
 * Funkcja dołączająca obiekt do elementu html;
 * @param [0]	parent (identyfiaktor string)
 * @param [1]	lista ol	(identyfikator string)
 * @param [2]	id obiekty	(identyfikator string)
 * @param [3]	name obiektu do formularza np. 'form[osoba_kontaktowa]' - obiekt doda automatyczny index od [ 0, -1,... -n]
 * @param [4]	unikalny id tworzonej pozycji na liście;
 * @type	TDynamicObject2
 */
TDynamicObject2	= function()
{
	var p 			= _get(arguments[0]);
	this.obj		= _c('div');
	this.obj.parent	= p;
	this.obj.ol		= _get(arguments[1]);
	this.obj.parent.appendChild( this.obj );
	this.obj.id		= arguments[2];
	this.obj.auto_decrement	= 0;
	this.obj.form_name	= arguments[3];
	this.obj.name_id	= arguments[4];
	//-- event
	this.obj.event	= function( dane )
	{
		this.li = new TCheckListElement( this.form_name+'['+this.auto_decrement+']' , this.name_id , dane );
		this.auto_decrement--;
		if (this.li != null)
		{
			this.ol.appendChild( this.li.uchwyt );
			this.li.uchwyt.parent = this.ol;
		}
	}
	return this;		
}

/**
 * Obiekt CheckBox z opisem
 * pierwsze 3 paramtery są BEZWZGLĘDNIE NIEZBĘDNE
 * Pozostałe argument funkcji stanowią opis łączony (spacją).
 * @type TCheckListElement	
 */
TCheckListElement	= function(name, name_id, dane)
{
	name			= arguments[0];
	name_id			= arguments[1];
	dane			= arguments[2];
	id				= dane[0];
	//--
	this.name		= 'Check_list_element';
	this.li			= _c('li');
	this.li.name	= 'element_li';
	this.cb 		= new TInput(this,'checkbox',name,name_id+'_'+id);
	this.cb.value	= id;
	this.li.apC( this.cb );
	this.tmp_value = '';
	this.i	= 1;
	for( this.i=1;this.i<dane.length;this.i++ ) this.tmp_value += dane[this.i]+ ' ';
	this.li.apC(_cTxt( this.tmp_value ));
	this.cb.checked	= true;
	//--
	this.span		= _c('span');
	this.span.name	= 'span';
	this.span.style.cursor	= 'pointer';
	this.span.obj	= this;
	this.span.apC( _cTxt('[usun]'));
	//--
	
	this.span.onclick	= function()
		{ this.obj.usun(); }
	this.li.apC( this.span );
	//-- metody;
	this.usun	= function()
		{this.uchwyt.parent.removeChild( this.uchwyt );}
	//--
	this.uchwyt	= this.li;
	//--
	return this;	
}

/**
 * Standardowy input;
 * 
 * @type TInput
 */
TInput	= function( p, typ, name, id )
{
	var defaultElement	= 'input';
	var defaultType		= 'text';
	var defaultName		= 'dynamicObject';
	//-- domyślny element i typ;
	var element	= defaultElement;
	if (typ 	== undefined)	typ 	= defaultType;
	if (typ 	== '')			typ 	= defaultType;
	if (name 	== undefined)	name 	= defaultName;
	if (name 	== '')			name 	= defaultName;
	//-- stworzenie obiektu
	this.obj 		= _c( element );
	if (this.obj == null) return null;
	//--
	this.obj.id		= id;
	this.obj.type	= typ;
	this.obj.name	= name;
	this.obj.parent	= p;
	//--
	return this.obj;
}

/**
 * Bazowy obiekt formularza z mechanizmem transmisji przez AJAX;
 * Kluczowym elementem jest:
 * 1. metoda: this.obj.rysuj()
 * 2. property: this.obj.AJAXsrc; adres danych AJAX
 * 
 * OBIEKT PRZESUNIĘTY DO PLIKU: TForm_object.js
 * 
 * @constructor
 * @author	Tomasz Łukawski
 * @type	TForm_object
 */
TForm_object	= function(param)
{
	this.obj			= _c('div',{AJAXsrc:''});
		//-- domyślne
		this.obj.send_ajax_on_select	= true;
		this.obj.image_src_on_load = '/images/loading5.gif';
		this.obj.show_loading_progress	= true;
		this.obj.id	= 'Form_object_'+parseInt(Math.random(0,1)*10000000);
		//--
	this.obj.setParam( param );
	/**
	 * Funkcja wywoływana przez mechanizm AJAX.
	 * Domyślna funkcja obsługująca dane zwrotne przesłane do obiektu.
	 */
	this.obj.load	= function( dane )
	{
		this.innerHTML = '';
		if (this.rysuj != undefined) this.rysuj( dane );
	}
	/**
	 * Funkcja wywoływana w trakcie pobierania danych z AJAX
	 */
	this.obj.onload	= function()
		{
			if (this.show_loading_progress)
				this.innerHTML = '<img src="'+this.image_src_on_load+'" style="align:center;">';
		}
	/**
	 * Funkcja wywoływana na zakończenie setByAjax();
	 */
	this.obj.do_zmiany	= function( dane )
	{
		for (this.i in dane)
			if (typeof(dane[this.i]) != 'object')
			{					
				//-- jeżeli 'string' to wykonaj uaktualnienie obiektu
				if (dane[this.i] != '')
					{
						var id	= dane[this.i];
						var adr = id.match(/\([a-z_]*\)/g);
						//--
						if (adr != null) //--adresowanie względne;
							{
							adr = String(adr);
							adr = adr.replace(/[\(\)]/g,'');
							var o = this[adr];
							}
						else //-- adresowanie bezwzględne
							var o = _get(id);
						//-- 		
						if ( o != null)
							if ( o.getByAjax != undefined)
								o.getByAjax();
					}
			}
			else
			//-- jeśli dane to "obiekt" to przypisz wartości z danych do obiektu
			{
				for (this.j in dane[this.i]) obj[this.j]=dane[this.i][this.j];
			}
	}
	this.obj.uchwyt	= this.obj;
	//--
	return this.obj;		
}

/**
 * Obiekt Hint na obiektach SELECT / AJAX
 * 
 * Podłączany do obiektu za pomoca funkcji: join( target )
 * 
 * Obiekt wymaga dopracowania celem większej uniwersalności;
 * 
 * OBIEKT PRZESUNIĘTY DO PLIKU: THintList.js
 * 
 * @type THintList
 */
THintList = function(param)
{
	this.obj			= new TForm_object(param);
	//--
	this.obj.AJAXsrc	= '';
	this.obj.hide();
	this.obj.min_length_hint	= 3;
	this.obj.id			= 'hint_'+ Math.floor( Math.random() * 10000000 );
	this.obj.className	= 'HintList';
	this.obj.dane_id	= 'dane';
	//---
	if (typeof(param) == 'object') { for (var i in param) this.obj[i]=param[i]; }
	this.obj.param		= new Array();
	this.obj.rysuj = function( dane )
	{
		if (dane == undefined) return false;
		//--
		if (dane[this.dane_id] == undefined)
			{
			this.returnControl();
			return false;
			}
		if (dane[this.dane_id].length == 0)
			{
			this.returnControl();
			return false;
			}
		//--
		this.lista	= _c('select',{parent:this});
		this.lista.setAttribute( 'size', 15 );
		this.lista.setAttribute( 'tabIndex', -1 );
		this.lista.onclick = function() {this.onselect(); }
		this.lista.onfocus = function() {this.selectedIndex = 0;}
		this.lista.onkeyup = function( e )
			{
				e = ( e)? e : (window.event);
				if (e.keyCode == 13) {this.onselect(); return false;}
				if (e.keyCode == 27) {this.parent.returnControl();}
			}
		this.lista.onselect	= function()
		{
			this.parent.set( this.value );
			this.parent.returnControl();
		}
		for (this.i in dane[this.dane_id])
		{
			this.itm	= _c('option',{parent:this});
			this.itm.apC( _cTxt( dane[this.dane_id][this.i]['d'] ) );
			this.itm.value	= dane[this.dane_id][this.i]['d'];
			this.lista.apC( this.itm );
		}
		this.apC( this.lista );
		this.show();
	}
	this.obj.set	= function( value )
	{
		this.parent.value = value;
	}
	this.obj.returnControl	= function()
		{
		this.hide();
		this.parent.focus();
		}
	/**
	 * Funkcja dołącza Hint Ajax do pola INPUT;
	 */
	this.obj.join	= function( target )
	{
		if (target.onkeyup != undefined) target._onkeyup	= target.onkeyup;
		this.parent	= target;
		//--
		target.hintlist	= this;
		target.onkeyup	= function( e )
			{
			e = (e)? e : (window.event) ;
			if (e.keyCode == 9)		{ this.hintlist.hide(); return false; }
			if (e.keyCode == 27)	{ this.hintlist.hide(); return false; }
			if (this.value.length >= this.hintlist.min_length_hint)
				{
				this.hintlist.param	= {'szukana':encodeURI(this.value)};
				if (e.keyCode == 40)
					{
					if (this.hintlist.style.display == 'none') this.hintlist.getByAjax();
					this.hintlist.lista.focus();
					}
				else
					this.hintlist.getByAjax();
				}
			if (this.value.length < this.hintlist.min_length_hint)	this.hintlist.hide();
			//--
			if(this._onkeyup != undefined) this._onkeyup(e);
			}
		//--
		target.onkeydown = function( e )
			{
			e = (e)? e : (window.event) ;
			if (e.keyCode == 9) { this.hintlist.hide(); }		
			}
		//--
		//target.onblur	= function() {}
		if (this.kotwica != undefined) this.kotwica.apC( target.hintlist );
		else target.parent.apC( target.hintlist );
	}
	this.obj.onblur	= function() {this.hide();}
	return this.obj;
}


