function send_request(obsluha, method, url, content, headers){
  var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
  if (!xmlhttp) {
      return false;
  }
  xmlhttp.open(method, url);
  xmlhttp.onreadystatechange = function() {
      obsluha(xmlhttp);
  };
  if (headers) {
      for (var key in headers) {
          xmlhttp.setRequestHeader(key, headers[key]);
      }
  }
  xmlhttp.send(content);
}
defaultLocation=0;
function parseSearch(httpRequest) {
  if (httpRequest.readyState == 4) {
    if(httpRequest.status == 200) {     
      var response = httpRequest.responseText;
      response = "<option value=\"0\">Všechny oblasti</option>"+response;
      $jq(".locationSearchSelect").html(response);
      if (defaultLocation>0){
        $jq(".locationSearchSelect").selectOptions(defaultLocation);
        defaultLocation=0;
      }
      //document.getElementById("pobytove-oblasti").innerHTML = response;
    }
  }
}

function handleCount(httpRequest) {
  if (httpRequest.readyState == 4) {
    if(httpRequest.status == 200) {
      var response = httpRequest.responseText;
      document.getElementById("total_count").innerHTML = response;
    }
  }
}



function getCount() {
	var el = document.getElementById("destinace").selectedIndex;
	var country = document.getElementById("destinace").options[el].value;
	
	var el2 = document.getElementById("pobytove-oblasti").selectedIndex;
	var location = document.getElementById("pobytove-oblasti").options[el2].value;
	
	var date_from = document.getElementById("calendar_odlet_input").value;
	var date_to = document.getElementById("calendar_prilet_input").value;
	
	send_request(handleCount, "GET", "/search/quick/country/"+country+"/location/"+location+"/date_from/"+date_from+"/date_to/"+date_to);

}

function getLocationList(item,searchbox) {
	//var el = document.getElementById("destinace").selectedIndex;
  var el = item.selectedIndex;
  var index=item.options[el].value;
  $jq('.destinaceSearchSelect').selectOptions(index);
  $jq('.locationSearchSelect').html("<option value='0'>Všechny oblasti</option>");
  send_request(parseSearch, "GET", "/search/locationlist/country/"+index);
  if (searchbox){
    getCount();
  }
}

function getDefaultLocationList(index) {
  send_request(parseSearch, "GET", "/search/locationlist/country/"+index);
}

function selectLocation(item){
  var el = item.selectedIndex;
  var index=item.options[el].value;
  $jq('.locationSearchSelect').selectOptions(index);
}

