function addEvent(elm, evType, fn, useCapture) {

	if (elm.addEventListener) { 

		elm.addEventListener(evType, fn, useCapture); 

		return true; 

	}

	else if (elm.attachEvent) { 

		var r = elm.attachEvent('on' + evType, fn); 

		return r; 

	}

	else {

		elm['on' + evType] = fn;

	}

}

addEvent(window,'load',AfterLoadWindow,false);

//**** AFTER LOAD FUNCTION **********************************************************************************************************************************

function AfterLoadWindow(){
	
	//addEvent(document.getElementById('fstat_1'),'change',FStatChange,false);
	
}

//***** CESTOVKA FUNCTION *********************************************************************************************************************************

/*function FStatChange(){
	
	var idBlok = this.id.split('_');
	SelectBoxNacitam('foblast_'+idBlok[1]);
	AjaxFunctionCestovka('./ajax/stat_change.php?statValue='+this.value,'foblast_selectbox_'+idBlok[1], 1);
			
} */

var maxNcounter = 1;

function FChange(prvek){
	
	//odeslat vsechny prvky formulare
	//fstat_n, foblast_n - max n je urceno v global promene maxNcounter 
	var outputGET = '';
	var destinationDIV = '';
	for(i=1;i<=maxNcounter;i++){
	
		fstatN = document.getElementById('fstat_'+i);
		foblastN = document.getElementById('foblast_'+i);
	
		if(fstatN){
			outputGET = outputGET + '&fstat['+i+']=' + fstatN.value;
		}
		
		if(foblastN){
			outputGET = outputGET + '&foblast['+i+']=' + foblastN.value;
		}

	}
	      
	      
	//pokud se zmenil stat tak aktualizovat prislusnou oblast
	var idBlok = prvek.id.split('_');
	if(idBlok[0] == 'fstat'){
		SelectBoxNacitam('foblast_'+idBlok[1]);	 
		destinationDIV = 'foblast_selectbox_'+idBlok[1];
		outputGET = outputGET + '&refresh=1'; 
	}
	
	//kontrola zadanych datumu fdatum_odjezd - fdatum_prijezd
	var fdatum_odjezd = document.getElementById('fdatum_odjezd');
	var fdatum_prijezd = document.getElementById('fdatum_prijezd');
	if(fdatum_odjezd && fdatum_prijezd){
		if(fdatum_odjezd.value != '')
			outputGET = outputGET + '&fdatumO='+fdatum_odjezd.value;
		if(fdatum_prijezd.value != ''){
			outputGET = outputGET + '&fdatumP='+fdatum_prijezd.value;
		}	
	}
	
	//kontrola delky pobytu
	var fdelka = document.getElementById('fdelka');
	if(fdelka){
		outputGET = outputGET + '&fdelka='+fdelka.value;
	}
	
	//uroven hotelu, pocet hvezd
	var fhvezd = document.getElementById('fhvezd');
	if(fhvezd){
		outputGET = outputGET + '&fhvezd='+fhvezd.value;
		//destinationDIV = 'resajax';	
	}
	
  var rnd = Math.random()*1000000;
	rnd = parseInt(rnd);
	AjaxFunctionCestovka('./ajax/filtr_change.php?whatChanged=' + prvek.id + outputGET + '&rnd='+rnd, destinationDIV,'cestovkaResult', 1, 'ajax-loader.gif');
			
}


function SelectBoxNacitam(selectBoxID){
	var selectBox = document.getElementById(selectBoxID);
	var selectBoxHidden = document.getElementById(selectBoxID+"_hidden");
	
	if(selectBox && selectBoxHidden){
		selectBox.style.display = 'none'; 	
		selectBoxHidden.style.display = 'inline';
	}	
}


function ShowNext(blokID,cacheFileName,nextPageNum){
	var rnd = Math.random()*10000;
	rnd = parseInt(rnd);
	AjaxFunctionCestovka('./ajax/show_next_nabidka.php?cFN=' + cacheFileName + '&nPN=' + nextPageNum + '&rnd='+rnd, 'nnb_'+blokID,'placeForLoadingIcon', 1, 'ajax-loader-next.gif');
}


// ----------  AJAX FUNCTIONS ------------------------------------------------------------------------------------------------------------------------------
//AjaxFunction('./_admin/ajax_filter_result.php?'+url4ajax+'&ctg='+categoriesSQL,'ajaxFilterResult',1);

function AjaxFunctionCestovka(urlAddress,destinationDiv,iconDestination,showWorkingGif,gifName)
{
	var url = urlAddress;

 //add AjaxLoadingIcon
 if(showWorkingGif == 1){
 	var mistoZobrazeniGif = document.getElementById(iconDestination);
 	if(mistoZobrazeniGif)
 		mistoZobrazeniGif.innerHTML = '<img id="ajaxLoadingIcon"  src="./img/'+gifName+'" title="loading..." alt="loading..." />';
 }
 
 if (url != 0)
 {
    if (window.ActiveXObject)
    {
      httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
      httpRequest = new XMLHttpRequest();
    }
    httpRequest.open("GET", url, true);
    httpRequest.onreadystatechange= function () {processRequestCestovka(destinationDiv); } ;
    httpRequest.send(null);
  }
  else
  {
		var destDiv = document.getElementById(destinationDiv);
		if(destDiv)
    	destDiv.innerHTML = "";
    
  }
}

function processRequestCestovka(destinationDiv)
{
  if (httpRequest.readyState == 4)
  {
    if(httpRequest.status == 200)
    {
      var mistoZobrazeni = document.getElementById(destinationDiv);
      var pocetVysledku = document.getElementById('cestovkaResult');
      var vystup = httpRequest.responseText.split('~');
      
      var vystupText = vystup[0];
      
      
      if(mistoZobrazeni)
      	mistoZobrazeni.innerHTML = vystupText;
      
			// ~celkem_zajezdu^celkem_hotelu
      if(vystup[1]){
      	var vystupCount = vystup[1].split('^');	
      	if(pocetVysledku)
      		pocetVysledku.innerHTML = '<span class="result-zajezdu">'+'Nalezeno '+'<strong>'+vystupCount[0]+'</strong>'+' zájezdů'+'</span>'+'<span class="result-hotelu">'+'Nalezeno '+'<strong>'+vystupCount[1]+'</strong>'+' hotelů'+'</span>';	
      }                                                                  
				
      return true;
    }
    else
    {
        //alert("Chyba pri nacitani stanky"+ httpRequest.status +":"+ httpRequest.statusText);
    }
  }
}


//*************************************************************************************************************************************************************************

function ValidateDotazForm(){

  var dotaz_jmeno = document.getElementById('dotaz_jmeno');
  var dotaz_prijmeni = document.getElementById('dotaz_prijmeni');
  var dotaz_mail = document.getElementById('dotaz_mail');
  var dotaz_tel = document.getElementById('dotaz_tel');
  var dotaz_dotaz = document.getElementById('dotaz_dotaz');
	
	if(dotaz_jmeno && dotaz_prijmeni && dotaz_mail && dotaz_tel && dotaz_dotaz){
			
	}else{
		return false;
	}
  
}

//******************************************* JQUERY UI ***********************************************
$(function() {
	$( "#fdatum_odjezd" ).datepicker({
			showOtherMonths: false,
			selectOtherMonths: false,
			numberOfMonths: 2,
			showButtonPanel: false,
			showOn: "both",
			buttonImage: "js/jqueryui/css/ui-lightness/images/calendar.gif",
			buttonImageOnly: true
	});
	
	$( "#fdatum_prijezd" ).datepicker({
			showOtherMonths: false,
			selectOtherMonths: false,
			numberOfMonths: 2,
			showButtonPanel: false,
			showOn: "both",
			buttonImage: "js/jqueryui/css/ui-lightness/images/calendar.gif",
			buttonImageOnly: true
	});
	
});
