/**
 * Funkcje podstawowe
 */
function _c( ident, param )
{
    var obj = document.createElement( ident );
    
    obj.not_valid		= '';
    obj.valid_on_blur	= '';
    obj.AJAXsrc			= '';
	obj.AJAXsrc_set		= '';
    obj.min		= undefined;
    obj.max		= undefined;
    obj.automat		= '';
    obj.def_value	= '';
    obj.jquery	= false;
    obj.CammelCase	= false;
    obj.UpperCase	= false;

	//-- funkcja iterująca elementy i przetwarzająca f( dane[i] )
	//-- gdzie f jest nazwą funkcji będącą w obiekcie this;
	obj.each	= function( dane, f )
	{
		for (var i in dane)
		{
			if (typeof(dane[i]) == 'function') continue;
			this[f]( dane[i] );		
		}
	}

	obj.setParam	= function( param )
	{
		if (typeof(param) == 'object')
			{ for (var i in param) this[i]=param[i]; }
	}

    //-- własności z parametrów;
    if (typeof(param_object) == 'string')
	{
		param += ';';
		var t = param.split(';');
		for (var i in t)
		{
		    if (t[i] == '') continue;
		    var t2 = t[i].split('=');
		    if (t2.length>0)
			obj[t2[0]]=t2[1];
		}
	}
	obj.setParam(param);
    //--
    obj.apC 	= function(c){ this.appendChild(c); }
    if (!obj.jquery)
    {
	    obj.hide 	= function() {obj.style.display = 'none';};
	    obj.show 	= function() {obj.style.display = '';};
    }
    obj.change 	= function() {(obj.style.display == '')?this.hide():this.show();};
    obj.onkeyup	= function() {if (this.not_valid != '') this.value = this.value.replace(this.not_valid,'');}
    obj.exec	= function() {};
    obj.exe_automat = function()
    {
	    var t = this.automat.split('|');
	    this.value = this.value.replace(t[0],t[1]);    	
    }
	obj.getByAjax	= function(){ getByAjax( this.AJAXsrc, this, this.param ); }
	obj.aktywny	= function(l) { this.style.opacity = (l==1) ? 1 : 0.3 ; }
    obj.onblur	= function()
	{
		if (this.value == '') this.value = this.def_value;
		//--
		if (this.automat != '') this.exe_automat();
		if (this.UpperCase) this.value = this.value.toUpperCase();
		if (this.CamelCase)
			{
			this.value = this.value.toLowerCase();
			this.a = this.value.split(' ');
			for (this.i in this.a)
				if (this.a[this.i].length>=2)
					this.a[this.i] = this.a[this.i].charAt(0).toUpperCase() + this.a[this.i].substr(1,this.a[this.i].length-1);	
			this.value = this.a.join(' ');
			}
		if ((this.valid_on_blur != '') && (!this.valid_on_blur.test(this.value)))
		    {
		    alert('Nieprawidłowa wartość w polu!');
		    this.value = '';
		    }
		if (this.min != undefined)
		    if(eval(this.value)<eval(this.min))
			{
			alert('KOREKTA! Minimalna wartość pola to: '+this.min);
			this.value = this.min;
			}
		if (this.max != undefined)
		    if(eval(this.value)>eval(this.max))
			{
			alert('KOREKTA! Maksymalna wartość pola to: '+this.max);
			this.value = this.max;
			}
		//-- funkcja zewnętrzna do wykonanania onblur;
		obj.exec();
	}
	
	//-- jeżeli to 'select' to dodaj funkcję wypełniającą opcje;
	if (ident == 'select')
	{
		if (obj.ustaw_pierwszy==undefined) obj.ustaw_piwerszy = false;
		obj.fillOptions	= function( d, v, n )
		{
			//this.length	= 0;
			for ( var i in d )
			{
				if ( typeof(d[i]) == 'function' ) continue;
				//--
				var opt = _c('option',{value:d[i][v]});
				opt.apC( _cTxt( d[i][n]) );
				opt._name	= d[i][n];
				this.apC( opt );
				//--
				if (this.value == '')
					if (this.ustaw_pierwszy) this.value = d[i][v]; 
			}	
		}			
	}
	obj.onGetByAjaxEnd	= function(){}

    //-- spec funkcje;
    //- serial TD dla TR
    obj._c_td = function (count)
	{
		var td = new Array();
		td[0] = _c('td');
		obj.apC(td[0]);
	}
    return obj;
}

function _cTxt(tekst)
    { return document.createTextNode(tekst); }

/**
 * Celem funkcji jest pobranie informacji do obiektu;
 * @version	1.1
 */    
function getByAjax( url, id, param )
{
	advAJAX.get(
	{
		url			: url,
		parameters	: param,
		onLoading : function(obj)
		{
			var o=(typeof(id)=='object')? id:_get(id);
			if ( o != undefined )
				if ( o.onload != undefined )
					o.onload(); 
		},
		onError : function(obj){alert("Error: " + obj.status);},
		onSuccess : function(obj)
		{
			var o=(typeof(id)=='object')? id:_get(id);
			if (o != undefined )
				if ( o.load != undefined )
				{
					o.load(
						eval( obj.responseText.replace('<p>dane</p><!--','').replace('-->','').replace('<dane>','').replace('</dane>','') )					
						);
					if (typeof(o.onGetByAjaxEnd)=='function') o.onGetByAjaxEnd();
				}
		}
	}
   	);
}


/**
 * Celem funkcji jest wysłanie danych metodą POST i ewentualne
 * odebranie informacji zwrotnej;
 * Zwrócenie informacji o obiektach koniecznych do odświeżanie
 * metodą getByAjax(); 
 */
function setByAjax( url, id, param )
{
	advAJAX.post(
		{
		url			: url,
		parameters	: param,
		onSuccess	: function(obj){
			var o=(typeof(id)=='object')? id:_get(id);
			if ( o != undefined )
				if ( o.do_zmiany != undefined )
					o.do_zmiany( eval( obj.responseText ) );
           },
		onLoading : function(obj){
			var o=(typeof(id)=='object')? id:_get(id);
			if ( o != undefined )
				if ( o.onsend != undefined )
					o.onsend();
		},
		onError : function(obj){alert("Error: " + obj.status); }
   		}
   	);   	 
}

/**
 * Funkcja nadpisuje wskazany obiekt nowym o okreslonej klasie;
 * przykład: overload( 'id_warstwy' , TKalendarium, {parametr:1,param:2} );
 * 
 * Oryginalna warstwa jest ukrywana i dołączana do nowej;
 * 
 */
function overload( target , append_object, param )
{
	if (_get( target ) == undefined ) return false;
	var zastap = true;
	//--
	var obj = (param != undefined) ? new append_object( param ) : new append_object;
	if (obj == undefined) return false;
	if (param != undefined)
		if (param.zastap != undefined) zastap = param.zastap;  
	//--
	if (obj.uchwyt == undefined) obj.uchwyt = _c('div');
	//-- przepisanie stylu (class);
	obj.oryginal	= _get( target );
	obj.className	= obj.oryginal.className;
	//-- przepisanie id;
	this.oryginal_id	= obj.oryginal.id;
	obj.oryginal.id 	= '_'+this.oryginal_id;
	obj.id				= this.oryginal_id;
	//-- 
	if (!zastap) obj.oryginal.parentNode.appendChild( obj.uchwyt );
	else obj.oryginal.parentNode.replaceChild( obj.uchwyt, obj.oryginal );
	//-- ukrycie i przełączenie;
	obj.oryginal.style.display = 'none';
	obj.uchwyt.apC( obj.oryginal );
	//--
	return obj;
}
        
/**
 * Zamienia element HTML na obiekt danej klasy;
 * @param {Object} id
 * @param {Object} nazwa_klasy
 */
function convertToClass(id,nazwa_klasy)
{
	if (typeof(id)!= 'object') var w = _get(id); else w = id;
	//--
	var a = new nazwa_klasy({clone:true,clone_obj:_get(id)});
	w.parentNode.replaceChild(a,w);
	return a;
}
