var cidades;
var cidades_destino;
var GLOBAL_id_cidade_origem;
var GLOBAL_id_cidade_destino;
var GLOBAL_id_cidade;
var $td = jQuery.noConflict();

$td(document).ready(function(){
	$td("#txData").datepicker();
	$td.datepicker.setDefaults($td.datepicker.regional['fr']);
	$td('#txData').datepicker('option',{
                                  dateFormat: "dd/mm/yy",
								    onSelect: function(date) { consultaViagem() }
                                 });
	 getCidades();
});

function getCidades(){
	try{
		href = document.location.href.split("?")[1];
		origemCode = href.split(",")[0].split("=")[1];
		$td.getJSON("../conteudo/cidades_destino.asp?InCdOrigem="+origemCode,
              function(json_data_destino){
                  cidades_destino = json_data_destino;
                  GLOBAL_id_cidade_origem = origemCode;
                  //GLOBAL_id_cidade = false;
                  $td("#cidade_destino2").autocomplete(cidades_destino, {
                  minChars: 0,
                  width: 310,
                  matchContains: true,
                  autoFill: false,
                  onSelectItem: function(){
                      document.getElementById('txData').focus();
                  },
                  formatItem: function(row, i, max){
                    $td("#cidade_destino2").attr("code",row.code);
                    return row.name;
                  },
                  formatMatch: function(row, i, max){
                    return row.name;
                  },
                  formatResult: function(row){
                    return row.name;
                  }
                });
                //document.getElementById('cidade_destino').value = "";
                //GLOBAL_id_cidade_destino = false;
              });
	}catch(e){
	}
	
  $td.getJSON("../conteudo/cidades.asp",
    function(json_data){
      cidades = json_data;
        $td("#cidade_origem2").autocomplete(cidades, {
        minChars: 0,
        width: 310,
        matchContains: true,
        autoFill: false,
        onSelectItem: function(){
			document.getElementById('cidade_destino2').style.backgroundImage = "";
            document.getElementById('cidade_destino2').value = "Carregando...";
            $td.getJSON("../conteudo/cidades_destino.asp?InCdOrigem="+GLOBAL_id_cidade,
              function(json_data_destino){
                  cidades_destino2 = json_data_destino;
                  GLOBAL_id_cidade_origem = GLOBAL_id_cidade;
                  GLOBAL_id_cidade = false;
					  $td("#cidade_destino2").autocomplete(cidades_destino, {
					  minChars: 0,
					  width: 310,
					  matchContains: true,
					  autoFill: false,
					  onSelectItem: function(){
						  //document.getElementById('txData').focus();
						  consultaViagem();
					  },
					  formatItem: function(row, i, max){
						$td("#cidade_destino2").attr("code",row.code);
						return row.name;
					  },
					  formatMatch: function(row, i, max){
						return row.name;
					  },
					  formatResult: function(row){
						return row.name;
					  }
					});
                document.getElementById('cidade_destino2').value = "";
                GLOBAL_id_cidade_destino = false;
              });
              setTimeout("document.getElementById('cidade_destino2').focus();",1000);
        },
        formatItem: function(row, i, max){
          //$td("#cidade_origem").attr("code",row.code);
          return row.name;
        },
        formatMatch: function(row, i, max){
          return row.name;
        },
        formatResult: function(row){
          return row.name;
        }
      });
    });
}

function formatItem(row){
  return row[0] + " (<strong>id: " + row[1] + "</strong>)";
}
function formatResult(row) {
  return row[0].replace(/(<.+?>)/gi, '');
}

function onSelectItem(row){
  
}