var cntr, rn, to, w, m;

	
	function seek_init(){
		cntr = $('i_kraj');
		rn = $('i_rodzajNieruchomosci');
		to = $('i_typOferty');
		w = $('i_wojewodztwo');
		m = $('i_miasto');
	}

	function getW(){
		var req1 = mint.Request().Set("getJSON", true).Set("evalScripts", true).Set("async", false); //typy ofert

		req1.OnSuccess = function(){
			var resp = this.responseJSON;
			var o_sel = w.options[w.selectedIndex].value;
			var new_sel = 0;

			if (resp.length>0){
				w.options.length = 0;
				w.options[0] = new Option(langComm['select_all'], "0");
				for (i=0; i<resp.length; i++){
					w.options[1+i] = new Option(resp[i].name, resp[i].id);
					if(resp[i].id == o_sel) new_sel = 1+i;
				}
				w.options[new_sel].selected="1";
			}
		}

		req1.AddParam("mod", "ajax");
		req1.AddParam("action", "getW");
		req1.AddParam("kraj", cntr.options[cntr.selectedIndex].value);
		req1.Send("/index.php");
	}
	
	function getM(){
		var req1 = mint.Request().Set("getJSON", true).Set("evalScripts", true).Set("async", false); //typy ofert

		req1.OnSuccess = function(){
			var resp = this.responseJSON;
			var o_sel = m.options[m.selectedIndex].value;
			var new_sel = 0;

			if (resp.length>0){
				m.options.length = 0;
				m.options[0] = new Option(langComm['select_all'], "1");
				for (i=0; i<resp.length; i++){
					m.options[1+i] = new Option(resp[i].name, resp[i].id);
					if(resp[i].id == o_sel) new_sel = 1+i;
				}
				m.options[new_sel].selected="1";
			}
		}

		req1.AddParam("mod", "ajax");
		req1.AddParam("action", "getM");
		req1.AddParam("kraj", cntr.options[cntr.selectedIndex].value);
		req1.AddParam("w", w.options[w.selectedIndex].value);
		req1.Send("/index.php");
	}
	
	function getRN(){
		var req1 = mint.Request().Set("getJSON", true).Set("evalScripts", true).Set("async", false); //typy ofert

		req1.OnSuccess = function(){
			var resp = this.responseJSON;
			var o_sel = rn.options[rn.selectedIndex].value;
			var new_sel = 0;

			if (resp.length>0){
				rn.options.length = 0;
				rn.options[0] = new Option(langComm['select_all'], "0");
				for (i=0; i<resp.length; i++){
					rn.options[1+i] = new Option(resp[i].name, resp[i].id);
					if(resp[i].id == o_sel) new_sel = 1+i;
				}
				rn.options[new_sel].selected="1";
			}
		}

		req1.AddParam("mod", "ajax");
		req1.AddParam("action", "getRN");
		req1.AddParam("kraj", cntr.options[cntr.selectedIndex].value);
		req1.AddParam("w", w.options[w.selectedIndex].value);
		req1.AddParam("m", m.options[m.selectedIndex].value);
		req1.AddParam("to", to.options[to.selectedIndex].value);
		req1.Send("/index.php");
	}
	
	function getTO(){
		var req1 = mint.Request().Set("getJSON", true).Set("evalScripts", true).Set("async", false); //typy ofert

		req1.OnSuccess = function(){
			var resp = this.responseJSON;
			var o_sel = to.options[to.selectedIndex].value;
			var new_sel = 0;

			if (resp.length>0){
				to.options.length = 0;
				to.options[0] = new Option(langComm['select_all'], "1");
				for (i=0; i<resp.length; i++){
					to.options[1+i] = new Option(resp[i].name, resp[i].id);
					if(resp[i].id == o_sel) new_sel = 1+i;
				}
				to.options[new_sel].selected="1";
			}
		}

		req1.AddParam("mod", "ajax");
		req1.AddParam("action", "getTO");
		req1.AddParam("kraj", cntr.options[cntr.selectedIndex].value);
		req1.AddParam("w", w.options[w.selectedIndex].value);
		req1.AddParam("m", m.options[m.selectedIndex].value);
		req1.AddParam("rn", rn.options[rn.selectedIndex].value);
		req1.Send("/index.php");
	}

	

	function reloadByCNTR(){
		cntr.disabled = true;
		w.disabled = true;
		rn.disabled = true;
		to.disabled = true;
		m.disabled = true;

		getW();
		w.disabled = false;
		getM();
		m.disabled = false;
		getRN();
		rn.disabled = false;
		getTO();
		to.disabled = false;
		cntr.disabled = false;
	}


	function reloadByW(){
		cntr.disabled = true;
		w.disabled = true;
		rn.disabled = true;
		to.disabled = true;
		m.disabled = true;

		getM();
		m.disabled = false;
		getRN();
		rn.disabled = false;
		getTO();
		to.disabled = false;
		cntr.disabled = false;
		w.disabled = false;
	}


	function reloadByM(){
		cntr.disabled = true;
		w.disabled = true;
		rn.disabled = true;
		to.disabled = true;
		m.disabled = true;

		getRN();
		rn.disabled = false;
		getTO();
		to.disabled = false;
		cntr.disabled = false;
		w.disabled = false;
		m.disabled = false;
	}


	function reloadByRN(){
		cntr.disabled = true;
		w.disabled = true;
		rn.disabled = true;
		to.disabled = true;
		m.disabled = true;

		getTO();
		to.disabled = false;
		cntr.disabled = false;
		w.disabled = false;
		m.disabled = false;
		rn.disabled = false;
	}


	function reloadByTO(){
		cntr.disabled = true;
		w.disabled = true;
		rn.disabled = true;
		to.disabled = true;
		m.disabled = true;

		getRN();
		cntr.disabled = false;
		w.disabled = false;
		m.disabled = false;
		to.disabled = false;
		rn.disabled = false;
	}

	function toggleForm(on_off){
		if ( on_off == 'off'){
			$('i_szukaj').disabled = true;
			$('i_wtorny').disabled = true;
			$('i_pierwotny').disabled = true;
			$('i_of_luksusowa').disabled = true;
			$('i_of_inwestycje').disabled = true;
			$('i_of_spec').disabled = true;
			$('i_pow_od').disabled = true;
			$('i_pow_do').disabled = true;
			$('i_cena_od').disabled = true;
			$('i_cena_do').disabled = true;
		}else{
			$('i_szukaj').disabled = false;
			$('i_wtorny').disabled = false;
			$('i_pierwotny').disabled = false;
			$('i_of_luksusowa').disabled = false;
			$('i_of_inwestycje').disabled = false;
			$('i_of_spec').disabled = false;
			$('i_pow_od').disabled = false;
			$('i_pow_do').disabled = false;
			$('i_cena_od').disabled = false;
			$('i_cena_do').disabled = false;
		}
	}

	function isNumeric(strString){
		var strValidChars = "0123456789.,-";
		var strChar;
		var blnResult = true;

		if (strString.length == 0) return false;

		//  test strString consists of valid characters listed above
		for (i = 0; i < strString.length && blnResult == true; i++){
			strChar = strString.charAt(i);
			if (strValidChars.indexOf(strChar) == -1){
				blnResult = false;
			}
		}
		return blnResult;
	}

	function verifyForm(frm){
		if (frm.kraj.options[frm.kraj.selectedIndex].value=='off'){
			alert( "Wybierz \"Kraj\"." );
			frm.kraj.focus();
			return false;
		}
		if (frm.rodzajNieruchomosci.options[frm.rodzajNieruchomosci.selectedIndex].value=='off'){
			alert( "Wybierz \"Rodzaj nieruchomości\"." );
			frm.rodzajNieruchomosci.focus();
			return false;
		}
		if (frm.typOferty.options[frm.typOferty.selectedIndex].value=='0'){
			alert( "Wybierz \"Typ oferty\"." );
			frm.typOferty.focus();
			return false;
		}
		if (frm.wojewodztwo.options[frm.wojewodztwo.selectedIndex].value=='off'){
			alert( "Wybierz \"Region\"." );
			frm.wojewodztwo.focus();
			return false;
		}
		if (frm.cena_od.value!='' && isNumeric(frm.cena_od.value)==false ){
			alert( "Wartość w polu \"Cena nieruchomości od\" jest nieprawidłowa." );
			frm.cena_od.focus();
			return false;
		}
		if (frm.cena_do.value!='' && isNumeric(frm.cena_do.value)==false ){
			alert( "Wartość w polu \"Cena nieruchomości do\" jest nieprawidłowa." );
			frm.cena_do.focus();
			return false;
		}
		if (frm.pow_od.value!='' && isNumeric(frm.pow_od.value)==false ){
			alert( "Wartość w polu \"Powierzchnia nieruchomości od\" jest nieprawidłowa." );
			frm.pow_od.focus();
			return false;
		}
		if (frm.pow_do.value!='' && isNumeric(frm.pow_do.value)==false ){
			alert( "Wartość w polu \"Powierzchnia nieruchomości do\" jest nieprawidłowa." );
			frm.pow_do.focus();
			return false;
		}

		frm.submit();
	}

