//konstruktor klasy Wyszukiwanie;
//parametrem jest id miejsca, w ktorym maja sie wyswietlic elementy wyszukiwania i sortowania
function Wyszukiwanie(thisStr, showPlaceId)
{
	//miejsce do wyswietlania elementow wyszukiwania i sortowania
	var showPlace = document.getElementById(showPlaceId);
	
	var kryteria = new Array();
	var wartosciPatt = new Array();		//wzorce wartosci dla kryteriow
	var wartosciShow = new Array();		//wartosci pokazujace sie na stronie
	var warSort = new Array();
	
	
	this.getThisStr = function()
	{ return thisStr; };
	
	
	this.setKryterium = function(i, val)
	{ kryteria[i] = val; };
	
	this.getKryterium = function(i)
	{ return kryteria[i]; };
	
	this.getKryteria = function()
	{ return kryteria; };
	
	
	
	this.setWartoscPatt = function(i, val)
	{ wartosciPatt[i] = val; };
	
	this.getWartoscPatt = function(i)
	{ return wartosciPatt[i]; };
	
	this.getWartosciPatt = function()
	{ return wartosciPatt; };
	
	
	
	this.setWartoscShow = function(i, val)
	{ wartosciShow[i] = val; };
	
	this.getWartoscShow = function(i)
	{ return wartosciShow[i]; };
	
	this.getWartosciShow = function()
	{ return wartosciShow; };
	
	
	
	this.setWarSort = function(i, val)
	{ warSort[i] = val; };
	
	this.getWarSort = function(i)
	{ return warSort[i]; };
	
	this.getAllWarSort = function()
	{ return warSort; };
	
	
	this.getShowPlace = function()
	{ return showPlace; };
}

Wyszukiwanie.prototype.ustawKryteria= function(elNamesArr)
{
	for(j=0; j<elNamesArr.length; j++)
	{
		this.setKryterium(j, new KryteriumWysz(this.getThisStr(), elNamesArr, 'kryterium_'+j));
	}
}

Wyszukiwanie.prototype.ustawPolaDlaKryteriow = function(elValuesArr)
{
	for(j=0; j<elValuesArr.length; j++)
	{
		this.setWartoscPatt(j, new WartoscWysz(elValuesArr[j], 'wartosc_'+j, true));
		this.setWartoscShow(j, new WartoscWysz(elValuesArr[j], 'wartosc_'+j, false));
	}
}

Wyszukiwanie.prototype.ustawWartosciSortowania = function(sortValuesArr)
{
	for(j=0; j<this.getKryteria().length; j++)
	{
		this.setWarSort(j, new WartoscSort(sortValuesArr, 'sort_'+j));
	}
}

//funkcja inicjujaca pola wyszukiwania, wartosci oraz opcje sortowania
Wyszukiwanie.prototype.init = function(searchObject)
{
	if(typeof(this.getShowPlace()) == 'undefined')
		return;
	
	for(j=0; j<this.getKryteria().length; j++)
	{
		this.getShowPlace().innerHTML += '<div class="divBox">'+
					this.getKryterium(j).getKontener()+
					this.getWartoscShow(j).getKontener()+
					this.getWarSort(j).getKontener()+
				'</div><div style="padding-top: 1px; margin: 4px 0px; border-top: 1px #777777 solid"></div>';
				
		this.getKryterium(j).print();
		this.getWartoscShow(j).print();
		this.getWarSort(j).print();
	}
	
	this.getShowPlace().innerHTML += '<input type="hidden" id="liczbaPol" name="liczbaPol" value="'+this.getKryteria().length+'">';
	
	//ustawiamy kolejne wartosci, jezeli byly zaznaczone przed przeladowaniem strony
	if(typeof(searchObject) != 'undefined')
	{
		for(j=0; j<searchObject.getLength(); j++)
		{
			var kryt = searchObject.getKryterium(j);	//nazwa kryterium
			var wart = searchObject.getWartosc(j);		//wartosc
			var sort = searchObject.getSortWart(j);		//kryterium sortowania

			this.getKryterium(j).setZaznaczone(kryt);
			this.getKryterium(j).print();
			
			var zaznaczoneNr = this.getKryterium(j).getZaznaczoneNr();
			this.getWartoscShow(j).setTyp(this.getWartoscPatt(zaznaczoneNr).getTyp());
			this.getWartoscShow(j).setWartosc(typeof(wart)!='undefined'?wart:"");
			this.getWartoscShow(j).setAktywny(true);
			this.getWarSort(j).setZaznaczone(sort);
			this.getWarSort(j).setAktywny(true);
			
			this.getWartoscShow(j).print();
			this.getWarSort(j).print();
		}
	}
}


Wyszukiwanie.prototype.reset = function()
{
	
	if(typeof(this.getShowPlace()) == 'undefined')
		return;
	
	this.getShowPlace().innerHTML = "";	
}


//funkcja aktywuje pola wartosci wyszukiwania i kryteriow sortowania,
//gdy zostanie wybrane okreslone kryterium wyszukiwania, podane poprzez argument el
Wyszukiwanie.prototype.aktywujPola = function(el)
{
	//obliczamy numer pola, bedacy cyfra po znaku podkreslenia w wartosci pola
	var nrPola = el.value.substr(el.value.indexOf('_') + 1);
	//numer i nazwie id pola
	var id = el.id.substr(el.id.indexOf('_') + 1);
	
	if(el.value != "")	//jezeli wybrano kryterium	
	{
		if(nrPola < this.getKryteria().length)
		{
			this.getWartoscShow(id).setTyp(this.getWartoscPatt(nrPola).getTyp());
			this.getWartoscShow(id).setAktywny(true);
			this.getWarSort(id).setAktywny(true);
			
			this.getWartoscShow(id).print();
			this.getWarSort(id).print();
		}
	}
	else
	{
		this.getWartoscShow(id).setAktywny(false);
		this.getWarSort(id).setAktywny(false);
		
		this.getWartoscShow(id).print();
		this.getWarSort(id).print();
	}
}


Wyszukiwanie.prototype.txtToHtml = function(str)
{
	str = str.toLowerCase();
	str = str.replace(/±/g, "a");
	str = str.replace(/æ/g, "c");
	str = str.replace(/ê/g, "e");
	str = str.replace(/³/g, "l");
	str = str.replace(/ñ/g, "n");
	str = str.replace(/ó/g, "o");
	str = str.replace(/¶/g, "s");
	str = str.replace(/¼/g, "z");
	str = str.replace(/¿/g, "z");
	
	return str;
}