if (!selector_width_default) {
  var selector_width_default = 140;
} 	   
var bRewrite = false;
var sForms = "search"; // lub searchDetails - podstawowe pola formularza do SUBMIT
var calendarFwMonths = 6; // ilosc miesiecy dozwolonych do przeszukiwania

function makeSearchTripMiner(clear) {
  if (!document.getElementById("country")) return false; // jeśli jeszcze nie przyleciał selectbox z ajaxa

  var temp = new Array();
  temp = document.location.toString().split(',');

  	if (clear != null && clear == true) {
		  document.getElementById("artFrom").value = '';
		  document.getElementById("banner_name").value = '';
		  document.getElementById("define").value = '';
		  document.getElementById("id_touroperator").value = '';
	  }		
    sLocation = "";
    if ( document.getElementById("country") && !document.getElementById("country").value ) {
      sLocation += "__,";
    } else {
        if ( document.getElementById("countrySelect") ) {
            sLocation += makeSearchRewriteAdd(  "countrySelect",     "country" ); //  1
        } else {
            sLocation += makeSearchRewriteAdd(  "country",            "country" ); //  1
        }
    }
    if (sLocation == '0,') {
    	sLocation = "__,";	
    }
    if ((clear == null || clear == false) && temp.length>1 && temp[1]=="") {
    	sLocation += ",";
    } else {
    	sLocation += makeSearchRewriteAdd(  "calendarInputFrom",    "dateFrom" ); // dateFrom 2
    }
    if ((clear == null || clear == false) && temp.length>2 && temp[2]=="") {
    	sLocation += ",";
    } else {
    	sLocation += makeSearchRewriteAdd(  "calendarInputTo",      "dateTo" ); // dateTo 3
    }
    // pozostałe dla MAPY
    if ( document.getElementById("transport_search") ) {
        sLocation += makeSearchRewriteAdd(  "transport_search",     "transport" ); //  5
    } else {
        sLocation += makeSearchRewriteAdd(  "transport",            "transport" ); //  5
    }
    sLocation += makeSearchRewriteAdd(  "period",               "period" ); //  6
    if (document.getElementById("hotel_standard").value == 0) {
    	sLocation += ","; //  7    	
    } else {
    	sLocation += makeSearchRewriteAdd(  "hotel_standard",       "hotel_standard" ); //  7
    }
    sLocation += makeSearchRewriteAdd(  "price_from",           "price_from" ); //  8
    sLocation += makeSearchRewriteAdd(  "price_to",             "price_to" ); //  9
    sLocation += makeSearchRewriteAdd(  "price_list",           "price_list" ); //  10
    sLocation += makeSearchRewriteAdd(  "maintenance",          "maintenance" ); //  11
    if (document.getElementById("hotel_room_adults") && document.getElementById("hotel_room_childs")) {
    	sLocation +=  document.getElementById("hotel_room_adults").value+'_'+document.getElementById("hotel_room_childs").value+','; //  12    	
    } else {
    	sLocation += makeSearchRewriteAdd(  "hotel_room",           "hotel_room" ); //  12
    }
    if ( document.getElementById("hotels_list") && document.getElementById("hotels_list").value != "dowolny" ) {
        sLocation += makeSearchRewriteAdd(  "hotels_list",          "hotel_code" ); //  13
    } else if ( document.getElementById("hotels_list_hid") ) {
        sLocation += makeSearchRewriteAdd(  "hotels_list_hid",          "hotel_code" ); //  13
    } else {
        sLocation += makeSearchRewriteAdd(  "hotel_code",           "hotel_code" ); //  13
    }
    sLocation += makeSearchRewriteAdd(  "hotel_city",           "hotel_city" ); //  14
    sLocation += makeSearchRewriteAdd(  "hotel_name",           "hotel_name" ); //  15
    if ( document.getElementById("departures_city_list") ) {
        sLocation += makeSearchRewriteAdd(  "departures_city_list", "departure_city" ); //  16
    } else {
        sLocation += makeSearchRewriteAdd(  "departure_city", "departure_city" ); //  16
    }
    if ( document.getElementById("region") ) {
        sLocation += makeSearchRewriteAdd(  "region",               "region_full" ); //  17
    } else {
        sLocation += makeSearchRewriteAdd(  "region_hid",           "region_full" ); //  17
    }
    
    sLocation += makeSearchRewriteAdd(  "catalog",              "catalog" ); //  18
    sLocation += makeSearchRewriteAdd(  "spec_offer",           "spec_offer" ); //  19
    sLocation += makeSearchRewriteAdd(  "nr_adult",             "nr_adult" ); //  20
    sLocation += makeSearchRewriteAdd(  "nr_child",             "nr_child" ); //  21
    sLocation += makeSearchRewriteAdd(  "iso_country_code",     "iso_country_code" ); //  22
    sLocation += makeSearchRewriteAdd(  "typ_oferty",           "typ_oferty" ); //  23
    sLocation += makeSearchRewriteAdd(  "lastminute",           "lastminute" ); //  24
    sLocation += makeSearchRewriteAdd(  "id_touroperator",      "id_touroperator" ); //  25
    sLocation += makeSearchRewriteAdd(  "banner_name",          "banner_name" ); //  25    
    if ( document.getElementById("udogodnienia") ) {
       sLocation += makeSearchRewriteAdd(  "udogodnienia",          "define" ); //  25 
    } else {
        sLocation += makeSearchRewriteAdd(  "define",               "define" ); //  25
    }
    sLocation += makeSearchRewriteAdd(  "artFrom",               "artFrom" ); //  26
    sLocation += makeSearchRewriteAdd(  "offset",                "offset" ); //  27
    if ($('orderBy1').value != "") {
    	sLocation += makeSearchRewriteAdd(  "orderBy1",              "orderBy1" ); //  28
    } else {
    	if ($('default_sort') && $('default_sort').value != "") {
    		sLocation += makeSearchRewriteAdd(  "default_sort", "orderBy1" ); //  28	
    	} else {
    		sLocation += ",";
    	}
    }
    sLocation += makeSearchRewriteAdd(  "orderBy2",              "orderBy2" ); //  29
    if (temp.length>1 && 
    		(temp[temp.length-1]=="searchd.html"
    	  || temp[temp.length-1]=="search.html2"
          || temp[temp.length-1]=="searchd.html2") ) {
	    last_string = temp[temp.length-1];
	    //alert(last_string);
	    sLocation += last_string;
    } else {
    	sLocation += "search.html";
    }
//    alert( sLocation );
    document.location = sLocation;
}

/*function setSelectors()
{
    insertCountryList();
    zmien_dojazd(false);
    if (document.getElementById("transport") && document.getElementById("transport_hid")) {
        ajaxGetDeparturesList('select_departures', document.getElementById('transport').value, document.getElementById("transport_hid").value );
    }
    window.setTimeout("setDepartureCity()",  500);
    setCountrySelector();
    getWaitRegion();
//    setDepartureCity();
}*/

function setCountryFromHidden() {
      country_hid = document.getElementById('country_hid');
      country = document.getElementById('country');
      if (country && country_hid && country_hid.value != '') {
        tab = country_hid.value.split('__');
        new_value = tab[0]+'_#_'+tab[1];
        
        country.value = new_value;
      }
}

function setCountrySelector()
{
   if (!document.getElementById('country')) {
      window.setTimeout("setCountrySelector()",  500);
   } else {
       setCountryFromHidden();
   }
}

function setDepartureCity()
{
   if (!document.getElementById('departures_city_list')) {
      window.setTimeout("setDepartureCity()",  500);
   } else {
       if (document.getElementById('departure_city_get')) {
           document.getElementById('departures_city_list').value = document.getElementById('departure_city_get').value;
       }
   }
}

function zmien_dojazd(typ, refresh)
{
    if (typ==null) {
        typ= document.getElementById("transport_hid");
        if (!typ) return false;
        if (!typ.value) return false;
    }
    
 if (refresh==null) refresh=true;
  var transportS = document.getElementById("transport");
  var selectDeparture = document.getElementById('select_departures');

 if (typ=='b' || typ=='B') {
	transportS.value = "B";
  if (refresh) {
    ajaxGetDeparturesList('select_departures', transportS.value, null); 
    document.getElementById('select_departures').innerHTML = throbberImg;
  }
 } else if (typ=='h' || typ=='H') {
	transportS.value = ('H');
	var dcl = document.getElementById('departures_city_list');
	if (dcl) dcl.disabled = true;
 } else if (typ=='f' || typ=='F') {
	transportS.value = "F";
  if (refresh) { 
	  ajaxGetDeparturesList('select_departures', transportS.value, null);
	  document.getElementById('select_departures').innerHTML = throbberImg;
  }
 }
}

function changeOrder(ord) {
    var orderBy1 = document.getElementById('orderBy1');
    var orderBy2 = document.getElementById('orderBy2');
    
      if (orderBy1.value != ord) {
        var ord2 = '0'
      } else if (orderBy2.value == '0') {
        var ord2 = '1';
      } else { 
        var ord2 = '0';
      }
      orderBy1.value = ord;
      orderBy2.value = ord2;
      var artFrom = document.getElementById('artFrom');
      artFrom.value = 0;
      makeSearchTripMiner();
}
function changePaginator(from) {
    var artFrom = document.getElementById('artFrom');
    artFrom.value = from;
    makeSearchTripMiner();
}

function makeSearchRewriteAdd( pole, argument_url )
{
    if ( document.getElementById(pole) && document.getElementById(pole).type == "checkbox" && document.getElementById(pole).checked ) {
     return "1,"; // lastminute
    } else if ( document.getElementById(pole) && document.getElementById(pole).type == "checkbox" ) {
      return ","; // lastminute
    }
    if ( document.getElementById(pole) ) {
    	//alert(pole + " : " + document.getElementById(pole).value);
	    if (document.getElementById(pole).value == 0) {
		    if (pole == "departure_city" ||
		    	pole == "spec_offer") {
		    	return ",";
		    }
	    }
        return addSearchArgumentModRewrite(  pole, argument_url )+ ",";
    } else {
        return ",";
    }
}

function getWaitRegion()
{
     if (!document.getElementById('td_region')) return false;
     if (!document.getElementById('country')) {
        window.setTimeout("getWaitRegion('',140)",  100);
     } else {
        insertRegionsList(document.getElementById('country'),140);
     }
}

function getWaitHotels()
{
     if (!document.getElementById('td_hotel_name')) return false;
     if (!document.getElementById('region')) {
        window.setTimeout("getWaitHotels(140)",  200);
     } else {
        insertHotelsList(140);
     }
}

function clearForm()
{
	 clearInput('transport'); 
	 clearInput('date');
	 clearInput('price');
	 clearInput('period');
	 
	 clearInput('price_from');
	 clearInput('hotel_standard_min');
	 clearInput('hotel_standard_max');
	 clearInput('departure_city');
	 clearInput('hotel_room');
	 clearInput('hotel_standard');
	 clearInput('price_to');
	 clearInput('hotel_city');
	 clearInput('hotel_name');
	 clearInput('idhd');
	 clearInput('catalog');
	 clearInput('maintenance');
	 clearInput('typ_oferty');
	 clearInput('udogodnienia');
	 clearInput('hotel_code');
	 clearInput('udogodnienia');
	 clearInput('region_full');
	 clearInput('price_list');
	 clearInput('kontynenty_hid');
	 
	 clearInput('artFrom');
	 clearInput('orderBy1');
	 clearInput('orderBy2');
	 
	 clearInput('country_hid');
	 clearInput('region_hid');
	 clearInput('departure_city_hid');
	 clearInput('region_hid');
	 clearInput('hotels_list_hid');
	 clearInput('departure_city_get');
	 
	 clearInput('lastminute');
	 clearInput('country');
	 
	 selectZeroInSelector("country");
	 selectZeroInSelector("region");
	 selectZeroInSelector("departure_city");
	 selectZeroInSelector("hotels_list");
	 selectZeroInSelector("hotel_standard");
	 selectZeroInSelector("hotel_room_adults",true);
	 selectZeroInSelector("hotel_room_childs",true);
}

function selectZeroInSelector(nazwa,pustyString)
{
    el = gEl( nazwa );
    if (el) {
        if (!pustyString) el.value=0;
        else el.value= "";
    }
}

function clearInput(name)
{
     var input1;
     if (eval("document.forms['searchDetails']")) {
   		 input1 = eval("document.forms['searchDetails']."+name);
     }
	   var input2 = eval("document.forms['search']."+name);
	   if(input1) {
			if(input1.type=='checkbox') {
	 			 input1.checked = false;
			} else {
			  	 input1.value = '';
			}
	 }
	 if(input2) {
	 		if(input2.type=='checkbox') {
	 			 input2.checked = false;
			} else {
			  	 input2.value = '';
			}
	 }
}

function tidAndSubmit(tid)
{
  var ah = document.getElementById("ahref_offer_"+tid);
    if (ah) {
      if ( ah.href ) {
        document.location = ah.href;
        return;
      }
    } else {
   		document.forms[sForms].idhd.value=tid;
		if (bRewrite) rewriteSearchForm();
		document.forms[sForms].submit();
    }
}

function setJsSearch( a_bRewrite, a_sForms ) {
   	bRewrite = a_bRewrite;
   	sForms = a_sForms;
}

function setCalendarMonths(a_months) {
   	calendarFwMonths = a_months; 
}

function tmSetCalendar( combo )
{
	   if (!combo) return;
	   	var miesiac = (combo.value);
	   	
	   	if (!miesiac) fillCalendar(0,0);
	   	miesiac = parseInt(miesiac);
	   	var ileDni = dlugosciMiesiaca[ miesiac ];
	   	var ostatniDzien = koniecMiesiaca[ miesiac ];
	   	var tabelaCalend = document.getElementById('calendar_table');
	   	var dzienTygodnia = 1;
	   	var dzienMiesiaca = 1;
	   	var tydzien = 1;
	   	
	   	var row;
	   	var cell;
	   	var len = tabelaCalend.rows.length;
	   	for(i = 0; i < len; i++)
		{
			tabelaCalend.deleteRow(0);
		}
	   		for(index = 0; index < ileDni+(7-ostatniDzien); index++)
			{
				if (dzienTygodnia==1) row = tabelaCalend.insertRow(0);

				dzienMiesiaca=ileDni-index+(7-ostatniDzien);
				cell = row.insertCell(0);
				if ( (tydzien==1 && (7-index)<=ostatniDzien) || tydzien>1 ) {
					if (dzienMiesiaca==aktualnyDzien && miesiac==aktualnyMiesiac)
						style = 'calendarToday';
					else if (dzienTygodnia==1)
						style = 'calendarSunday';							
					else
						style = 'calendarWhite';
						
					cell.innerHTML = '<div style="width:100%;" class="'+style+'" onclick="fillCalendar('+dzienMiesiaca+','+miesiac+')">'+dzienMiesiaca+'</div>';
						
				} else {
					cell.innerHTML = '&nbsp';
				}
				
				if (dzienTygodnia==7 && index<ileDni) tydzien++;	
				
				if (dzienTygodnia<7) dzienTygodnia++;	
				else dzienTygodnia=1;
			}
			if (dzienTygodnia!=1)
				dzienTygodnia--;
			else
				dzienTygodnia=7;
			for(index = 0; index < (7-dzienTygodnia); index++)
			{
				cell=row.insertCell(0);
				cell.innerHTML = '&nbsp';
			}
   }

function setCalendar( combo )
{
	   if (!combo) return;
	   	var miesiac = (combo.value);
	   	
	   	if (!miesiac) fillCalendar(0,0);
	   	miesiac = parseInt(miesiac);
	   	var ileDni = dlugosciMiesiaca[ miesiac ];
	   	var ostatniDzien = koniecMiesiaca[ miesiac ];
	   	var tabelaCalend = document.getElementById('calendar_table');
	   	var dzienTygodnia = 1;
	   	var dzienMiesiaca = 1;
	   	var tydzien = 1;
	   	
	   	var row;
	   	var cell;
	   	var len = tabelaCalend.rows.length;
	   	for(i = 0; i < len; i++)
		{
			tabelaCalend.deleteRow(0);
		}
	   		for(index = 0; index < ileDni+(7-ostatniDzien); index++)
			{
				if (dzienTygodnia==1) row = tabelaCalend.insertRow(0);

				dzienMiesiaca=ileDni-index+(7-ostatniDzien);
				cell = row.insertCell(0);
				if ( (tydzien==1 && (7-index)<=ostatniDzien) || tydzien>1 ) {
					if (dzienMiesiaca==aktualnyDzien && miesiac==aktualnyMiesiac)
						style = 'calendarToday';
					else if (dzienTygodnia==1)
						style = 'calendarSunday';							
					else
						style = 'calendarWhite';
						
					cell.innerHTML = '<div style="width:100%;" class="'+style+'" onclick="fillCalendar('+dzienMiesiaca+','+miesiac+')">'+dzienMiesiaca+'</div>';
						
				} else {
					cell.innerHTML = '&nbsp';
				}
				
				if (dzienTygodnia==7 && index<ileDni) tydzien++;	
				
				if (dzienTygodnia<7) dzienTygodnia++;	
				else dzienTygodnia=1;
			}
			if (dzienTygodnia!=1)
				dzienTygodnia--;
			else
				dzienTygodnia=7;
			for(index = 0; index < (7-dzienTygodnia); index++)
			{
				cell=row.insertCell(0);
				cell.innerHTML = '&nbsp';
			}
   }
   
   function fillCalendar( dzien, miesiac )
   {
	   	if (!document.getElementById('calendarInput'+aktualnaData)) {
	   		blinkDiv('calendar_search','off');
	   		return;
   		}
   	
   		if (dzien==0 && miesiac==0) document.getElementById('calendarInput'+aktualnaData).value = '';
   		else {
	   		if (dzien<=9) dzien = '0'+dzien;
	   		if (miesiac<=9) miesiac = '0'+miesiac;
	   		var rok = aktualnyRok;
	   		
	   		//if ( miesiac < aktualnyMiesiac ) rok++;
		   	if (aktualnaData=='To') {
		   		dFrom = document.getElementById('calendarInputFrom').value;
		   		if (dFrom.substr(3,2) >= aktualnyMiesiac) {
			   		if (parseFloat(miesiac) < parseFloat(dFrom.substr(3,2)) ) {
			   			if ( (parseFloat(miesiac) - parseFloat(dFrom.substr(3,2)) ) < 0 ) {
			   				if (((miesiac - dFrom.substr(3,2))+12) > calendarFwMonths){
			   					alert('Okres nie może być dłuższy niż '+calendarFwMonths+' miesięcy');
			   					//blinkDiv('calendar_search','off');
			   					return;
			   				}
			   			}
			   		}
				   	else if (parseFloat(miesiac) == parseFloat(dFrom.substr(3,2))) {
			   			if (dzien < dFrom.substr(0,2) ) {
			   				alert('Data nie może być wcześniejsza od daty \"Termin od:\"');
			   			}
			   		}
			   		else if ( (parseFloat(miesiac) - parseFloat(dFrom.substr(3,2))) > calendarFwMonths ) {
			   				alert('Okres nie może być dłuższy niż '+calendarFwMonths+' miesięcy');
			   				return;
		   			}
			   	}
			   	else{
			   		if (parseFloat(miesiac) > parseFloat(dFrom.substr(3,2))){
			   			if ( (miesiac - dFrom.substr(3,2)) > calendarFwMonths ) {
			   					alert('Okres nie może być dłuższy niż '+calendarFwMonths+' miesięcy');
			   					return;
		   				}
			   		}
			   		else if (parseFloat(miesiac) == parseFloat(dFrom.substr(3,2))){
			   			if (dzien < dFrom.substr(0,2) ) {
			   				alert('Data nie może być wcześniejsza od daty \"Termin od:\"');
			   			}
			   		}
			   		else if (miesiac < dFrom.substr(3,2)){
			   			alert('Data nie może być wcześniejsza od daty \"Termin od:\"');
			   		}
			   	}
	   		}
	   		document.getElementById('calendarInput'+aktualnaData).value = dzien+'-'+miesiac;
	   		if (aktualnaData=='From'){
	   			if (miesiac == '12')
	   				document.getElementById('calendarInputTo').value = dzien+'-01';
	   			else{
	   				miesiac = parseFloat(miesiac)+1;
	   				if (miesiac<=9) miesiac = '0'+miesiac;
	   				document.getElementById('calendarInputTo').value = dzien+'-'+miesiac;
	   			}
	   		}
   		}
   		blinkDiv('calendar_search','off');
   }
		   
	function setPeriod(selector)
    {
   		document.getElementById('td_period').innerHTML = selector.value + ' dni';
   		if (document.getElementById('order_period')) document.getElementById('order_period').innerHTML = selector.value + ' dni';
   		var dateTo = document.getElementById('dateToTd');
   		dateTo.innerHTML = addDays(dateFromYear,dateFromMonth,dateFromDay, selector.value);
    }
	
    function addDays(year,month,day, numDays) {
    	
    	var myDate=new Date();
			//alert(year+','+(month-1)+','+day);
  		//myDate.setFullYear(year,month-1,day);
			myDate.setFullYear(year);
			myDate.setMonth(month-1);
			myDate.setDate(day);
		
	    var resDate = new Date(myDate.getTime() + numDays*24*60*60*1000);
	    var newDay = resDate.getDate();
	    var newMonth = (resDate.getMonth()+1);
	    if (newDay<=9) newDay = '0'+newDay;
	    if (newMonth<=9) newMonth = '0'+newMonth;
	    return newDay +'.'+ newMonth +'.'+resDate.getFullYear();
	}

var iCountSearchArguments = 0;
function addSearchArgument( aSearchList, name, argument, noEscape ) {
  if ( document.getElementById(name) && document.getElementById(name).value.length>0 ) {
    aSearchList[iCountSearchArguments] = Array();
    aSearchList[iCountSearchArguments]["argument"] = argument;
    if (!noEscape) aSearchList[iCountSearchArguments]["value"] = escape(document.getElementById(name).value);
    else aSearchList[iCountSearchArguments]["value"] = (document.getElementById(name).value);
    iCountSearchArguments++;
  }
  return aSearchList;
}

function addSearchArgumentModRewrite( name, argument ) {
  if ( document.getElementById(name) && document.getElementById(name).value.length>0 ) {
    return escape(document.getElementById(name).value.replace("#",""));
  } else {
    return '';
  }
}

function changeSearchType(t) {
	var url = document.location.toString();
	var newurl = '';
	if (t == 'all') {
		newurl = url.replace('.html2', '.html');
    } else {
    	newurl = url.replace('.html', '.html2');
    }
	if (newurl != url) document.location = newurl;
}