function advAJAXwrapper() {
	this.$$ = function(x) {
		return document.getElementById(x);
	}
	this.formDEFAULT = "index_ajax.php";
	this.pokaz = function(id) {
		$(id).style.display="";
	}
	
	this.ukryj = function(id) {
		$(id).style.display="none";
	}
	
	this.setSelected = function(pole,wartosc) {
		var pole = $(pole);
		if (pole && pole.options) {
			for (var i=0;i<pole.options.length;i++) {
				var opcja = pole.options[i];
				if (opcja.value == wartosc) {
					pole.options[i].selected = true;
				}
			}
		}
	}
	this.createURL = function(mod) {
		return "moduly/"+mod+"/"+mod+"_ajax.php";
	}
	this.UtworzID = function(pole1,pole2) {
		var str = $(pole1).value.replace(/ /g,'_');
		str = str.replace(/ą/g,'a');
		str = str.replace(/Ą/g,'A');
		str = str.replace(/ć/g,'c');
		str = str.replace(/Ć/g,'C');
		str = str.replace(/ę/g,'e');
		str = str.replace(/Ę/g,'E');		
		str = str.replace(/ł/g,'l');
		str = str.replace(/Ł/g,'L');
		str = str.replace(/ń/g,'n');
		str = str.replace(/Ń/g,'N');
		str = str.replace(/ó/g,'o');
		str = str.replace(/Ó/g,'O');
		str = str.replace(/ź/g,'z');
		str = str.replace(/Ź/g,'Z');
		str = str.replace(/Ż/g,'Z');
		str = str.replace(/ż/g,'z');
		str = str.replace(/Ś/g,'S');
		str = str.replace(/ś/g,'s');
		str = str.replace(/[\?\&\(\)\<\>\$\%\^\*\!\@\#\[\]\+\-\.\=\"\']+/g,'');
		$(pole2).value = str;
	}
	
	this.SUBMIT = function(funkcja) {
	  if (this.className != null) {
			this.parametry.className = this.className;
		}
		id = this.id;
		advAJAX.submit(
			this.$$(this.form),
			{
				parameters: this.parametry,
				onSuccess: function(obj) {
					//alert(obj.responseText);
					//alert(obj.responseJSON.rezultat.blad);
					eval(id+"."+funkcja+"(obj.responseJSON);");

				},
				onError: function(obj) {
					alert(obj.statusText);
				}
			}
		);
	}
	
	this.GETPOST = function(funkcja) {
	  if (this.className != null) {
			this.parametry.className = this.className;
		}
		if (this.form == null || $(this.form)) {
			this.form = this.formDEFAULT;
		}		
	id = this.id;
		parametry: this.parameters;
		advAJAX.post(
			{
				url: this.form,
				parameters: this.parametry,
				onSuccess: function(obj) {
					//alert(obj.responseText);
					//alert(obj.responseJSON.rezultat.blad);
					eval(id+"."+funkcja+"(obj.responseJSON);");
				},
				onError: function(obj) {
					alert(obj.statusText);
				}
			}
		);
	}
	
	this.startProgress = function(tresc,tytul)
	{
		if (tresc == null)
			tresc = '<img src="/layout/gonito/img/progress_bar.gif" />';
		if (tytul == null)
			tytul = 'Proszę czekać';
		$("#komunikat").html('<h1>'+tytul+'</h1>'+tresc);
		$('#komunikat').slideToggle('fast');		
	}
	
	this.stopProgress = function()
	{
		$("#komunikat").text("");
		$('#komunikat').slideToggle('fast');	
	}

    this.utworzRadioButton = function(nazwa,wartosc,i)
    {
	    id = nazwa+'_'+(i+1);
	    return '<input type="radio" name="'+nazwa+'" value="'+wartosc+'" id="'+id+'" />';
    }

    this.utworzCheckBoxa = function(nazwa,wartosc,i)
    {
	    id = nazwa+'[]_'+(i+1);
	    return '<input type="checkbox" name="'+nazwa+'[]" value="'+wartosc+'" id="'+id+'" />';
    }
    
    this.wypelnijSelecta = function(id,arr,value,display,selected) {
		if (selected == null) {
			selected = '';
		}
    //value == w tym polu znajduje sie wartosc option
    //display == a w tym to, co sie wyswietli na ekranie
		if ($(id).options) {
			for (i=0;i<$(id).options.length;i++) {
				$(id).options[i] = null;
			}
			for (i=0;i<arr.length;i++) {
				tekst = eval('arr[i].'+display);
				wartosc = eval('arr[i].'+value);
				$(id).options[i] = new Option(tekst,wartosc);
				if (wartosc == selected) {
					$(id).options[i].selected = true;
				}
			}
		}
	}
	
	this.clearLB = function(el) {
		if ($(el).options) {
			for (i=0;i<$(el).options.length;i++) {
				$(el).options[i] = null;
			}
		}
	}
	
	this.wyczyscTabele = function(el)
	{
		while (el.rows.length)
		{
			el.deleteRow(el.rows.length-1);
		}
	}
	
	this.tworzTR = function(tabela)
	{
		return document.createElement('tr');
	}
	
	this.tworzTD = function(rzad)
	{
		return document.createElement('td');
	}
}

function createTD() {
	return document.createElement('td');
}

function addTD(rz,txt,cent) {
	var el = createTD();
	if (cent != null) {
		if (cent == 1) {
			el.setAttribute('align','center');
		}
		if (cent == 2) {
			el.setAttribute('align','right');
		}	
	}
	el.innerHTML = txt;
	rz.appendChild(el);
}

function addslashes(str) {
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\0/g,'\\0');
return str;
}
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\\"/g,'"');
str=str.replace(/\\\\/g,'\\');
str=str.replace(/\\0/g,'\0');
return str;
}
function setV(el,val)
{
	el.innerHTML = val;
}

function centTD(el)
{
	el.setAttribute('align','center');
}

function createTDC() {
	el = document.createElement('td');
	centTD(el);
	return el;
}

function tPaginator() {
	
	this.setElement = function(id) {
		this.display = id;
	}
	
	this.setPerPage = function(count) {
		this.perPage = count;
	}
	
	this.Paginate = function(page) {
		this.startPage = page;
		this.doPaginate();
	}
	
	this.doPaginate = function() {
		if (this.outMode == "tr") {
		this.wyczyscTabele($(this.display));
		}
		else {
			$(this.display).innerHTML = '';
		}
		if (!this.startPage) {
			this.startPage = 0;
		}
		eval(this.grabDatafun);
	}
	
	this.displayOutput = function(out) {
		//out musi byc tablica JSON. obowiazkowe dwa pola:
		//out => znow tablica JSON lub zwykly html
		//count => count(*) tego co chcemy wyswietlac
		if (this.mode == "html") {
			$(this.display).innerHTML = out.out;
		}
		else {
			this.totalCount = out.count;
			eval(this.doWithDatafun+"(out)");
		}
		this.createNavBar();
	}
	
	this.setMode = function(mode) {
		//dla modu html tresc musi byc pobrana jako html
		this.mode = mode;
	}
	
	this.createNavBar = function() {
		//tworzy pasek nawigacyjny
		txt='Strona: ';
		for (i=0;i<Math.ceil(this.totalCount/this.perPage);i++)
		{
			if (i == this.startPage)
			{
				txt+=(i+1)+'&nbsp;';
			}
			else {
			txt+='<a href="javascript:'+this.ID+'.Paginate('+i+');">'+(i+1)+'</a>&nbsp;';
			}
		}
		if (this.outMode == "tr")
		{
			rzad = document.createElement('tr');
			el = document.createElement('td');
			el.setAttribute('colspan',6);
			el.innerHTML = txt;
			rzad.appendChild(el);
		}
		else
		{
			rzad = document.createElement('div');
			el = document.createElement('span');
			el.innerHTML = txt;
			rzad.appendChild(el);			
		}
		$(this.display).appendChild(rzad);
		this.createPerPageSetup();
	}
	this.createPerPageSetup = function()
	{
		txt='Rekordów na stronę: <select name="PerPageSetup" onChange="javascript:'+this.ID+'.setupPerPage(this.value);">';
			for (i=0;i<this.perPageOptions.length;i++)
			{
				txt+='<option value="'+this.perPageOptions[i]+'">'+this.perPageOptions[i]+'</option>';
			}
		txt+='</select>';
		if (this.outMode == "tr")
		{
			rzad = document.createElement('tr');
			el = document.createElement('td');
			el.colSpan = "6";
			el.innerHTML = txt;
			rzad.appendChild(el);
		}
		else
		{
			rzad = document.createElement('div');
			el = document.createElement('span');
			el.innerHTML = txt;
			rzad.appendChild(el);
		}
		$(this.display).appendChild(rzad);
		//alert($(this.display).innerHTML);
	}
	
	this.setupPerPage = function(val)
	{
		this.setPerPage(val);
		this.doPaginate();
	}
}

tPaginator.prototype = new advAJAXwrapper();

function lista_cb(el)
{
	var id = $(el+'[]_0');
	if (id)
	{
		x=0;
		dodane = new Array();
		dodane_string = '';
		while ($(el+'[]_'+x))
		{
			temp = $(el+'[]_'+x);
			if (temp.checked)
			{
				dodane[dodane.length]=temp.value;
			}
			x++;
		}
		if (dodane.length)
		{
			x=0;
			while(x<=dodane.length-2)
			{
				dodane_string+=dodane[x]+',';
				x++;
			}
			dodane_string+=dodane[dodane.length-1];
			return new Array(dodane,dodane_string);
		}
		else return -1;
	}
	else return -1;
}

function radio(el) {
	if (!el)
		el = 'radio';
	var id = $('id_'+el+'_0');
	if (id)
	{
		x = 0;
		wybrany = -1;
		while ($('id_'+el+'_'+x))
		{
			temp = $('id_'+el+'_'+x);
			if (temp.checked)
			{
				return temp.value;
			}
			x++;
		}
		return -1;
	}
	else return -1;
}

function generateUL(arr)
{
	count = 0;
	out = '';
	while(count < arr.length)
	{
		out+= '<b>'+arr[count]+'</b><br />';
		count++;
	}
	//out+= '</ul>';
	return out;
}

function genUL(arr)
{
	count = 0;
	out = '<ul>';
	while(count < arr.length)
	{
		out+= '<li>'+arr[count]+'</li>';
		count++;
	}
	out+= '</ul>';
	return out;	
}

function chkBox(val,data,cell,row)
{
	return '<input type="checkbox" name="id[]" id="id[]_'+row+'" value="'+val+'" />';
}

function rdBtn(val,data,cell,row)
{
	return '<input type="radio" name="id_radio" id="id_radio_'+row+'" value="'+val+'" />';
}
