var xmlhttp = false;
function criaobjetoXmlHttp(){

	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(e){
			xmlhttp = false;
		}
	}
	if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
		xmlhttp = new XMLHttpRequest();
	}

	if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
		xmlhttp = new XMLHttpRequest();
	}

	return xmlhttp;
}

function atualizaPaginaBuscaRegiao(url,div,idSubRegiao,nroPagina){
	var parametros = 'idSubRegiao='+idSubRegiao+'&nroPagina='+nroPagina;

	var divId = document.getElementById(div);
	var xmlhttp = criaobjetoXmlHttp();
	xmlhttp.open('POST', url, true);
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			divId.innerHTML = xmlhttp.responseText;
		}
	}
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", parametros.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(parametros);
}

/******************************************
*funcao que atualiza a div central do site
******************************************/
function atualizaPagina(serverPage,objID){
	var xmlhttp = criaobjetoXmlHttp();
	var obj = document.getElementById(objID);
    xmlhttp.open("GET",serverPage);
    xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			obj.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}
/******************************************
*fim atualizaPagina
******************************************/

/******************************************
* funcao valida e grava anuncio
******************************************/
function validaAnuncio() {
	var nomeAnuncio 	=  document.getElementById('nomeAnuncio').value;
	var dddAnuncio 		=  document.getElementById('dddAnuncio').value;
	var telAnuncio 		=  document.getElementById('telefoneAnuncio').value;
	var emailAnuncio 	=  document.getElementById('emailAnuncio').value;
	var tipoAnuncio 	=  document.getElementById('tipoAnuncio').value;

	if(!doString(nomeAnuncio				, 'nomeAnuncio'		, 'Nome')) 		return;
	if(!doDigits(dddAnuncio					, 'dddAnuncio'		, 'DDD')) 		return;
	if(!doDigits(telAnuncio.replace('-','')	, 'telefoneAnuncio'	, 'Telefone')) 	return;
	if(!doEmail(emailAnuncio				, 'emailAnuncio'	, 'Email')) 	return;

	var parametros = 'nomeAnuncio='+nomeAnuncio+'&telefoneAnuncio='+telAnuncio+'&emailAnuncio='+emailAnuncio+'&tipoAnuncio='+tipoAnuncio+'&dddAnuncio='+dddAnuncio;
	enviarFormularios('controles/gravaAnuncio.php',parametros);

	document.getElementById('nomeAnuncio').value 		= "";
	document.getElementById('dddAnuncio').value 		= "";
	document.getElementById('telefoneAnuncio').value 	= "";
	document.getElementById('emailAnuncio').value 		= "";
	document.getElementById('tipoAnuncio').value 		= "";
}
/******************************************
* fim funcao de anuncio
******************************************/

/******************************************
* funcao valida e grava cadastro
******************************************/
function validaCadastro() {

	var nomeCadastro 	=  document.getElementById('nomeCadastro').value;
	var dddCadastro 	=  document.getElementById('dddCadastro').value;
	var telCadastro 	=  document.getElementById('telefoneCadastro').value;
	var emailCadastro 	=  document.getElementById('emailCadastro').value;
	var aceitaCadastro  =  document.getElementById('aceitaCadastro').value;

	if(!doString(nomeCadastro					, 'nomeCadastro'		, 'Nome')) 		return;
	if(!doDigits(dddCadastro					, 'dddCadastro'			, 'DDD')) 		return;
	if(!doDigits(telCadastro.replace('-','')	, 'telefoneCadastro'	, 'Telefone')) 	return;
	if(!doEmail(emailCadastro					, 'emailCadastro'		, 'Email')) 	return;

	var parametros = 'nomeCadastro='+nomeCadastro+'&telefoneCadastro='+telCadastro+'&emailCadastro='+emailCadastro+'&aceitaCadastro='+aceitaCadastro+'&dddCadastro='+dddCadastro;
	enviarFormularios('controles/gravaCadastro.php',parametros);

	document.getElementById('nomeCadastro').value 		= "";
	document.getElementById('dddCadastro').value 		= "";
	document.getElementById('telefoneCadastro').value 	= "";
	document.getElementById('emailCadastro').value 		= "";
	document.getElementById('aceitaCadastro').value 	= "";
}
/******************************************
* fim funcao de cadastro
******************************************/

/******************************************
* funcao valida e grava Contato
******************************************/
function validaContato() {

	var nomeContato 	=  document.getElementById('nomeContato').value;
	var dddContato 		=  document.getElementById('dddContato').value;
	var telContato 		=  document.getElementById('telefoneContato').value;
	var emailContato 	=  document.getElementById('emailContato').value;
	var mensagemContato =  document.getElementById('mensagemContato').value;

	if(!doString(nomeContato					, 'nomeContato'		, 'Nome')) 		return;
	if(!doDigits(dddContato						, 'dddContato'		, 'DDD')) 		return;
	if(!doDigits(telContato.replace('-','')		, 'telefoneContato'	, 'Telefone')) 	return;
	if(!doEmail(emailContato					, 'emailContato'	, 'Email')) 	return;
	if(!doString(mensagemContato				, 'mensagemContato'	, 'Mensagem'))	return;

	var parametros = 'nomeContato='+nomeContato+'&telefoneContato='+telContato+'&emailContato='+emailContato+'&dddContato='+dddContato+'&mensagemContato='+mensagemContato;
	enviarFormularios('controles/gravaContato.php',parametros);

	document.getElementById('nomeContato').value		= "";
	document.getElementById('dddContato').value			= "";
	document.getElementById('telefoneContato').value	= "";
	document.getElementById('emailContato').value		= "";
	document.getElementById('mensagemContato').value	= "";
}
/******************************************
* fim funcao de Contato
******************************************/

/******************************************
* funcao valida e grava Duvida
******************************************/
function validaDuvida() {
	var nomeDuvida 		=  document.getElementById('nomeDuvida').value;
	var emailDuvida 	=  document.getElementById('emailDuvida').value;
	var mensagemDuvida 	=  document.getElementById('mensagemDuvida').value;

	if(!doString(nomeDuvida					, 'nomeDuvida'		, 'Nome'))		return;
	if(!doEmail(emailDuvida					, 'emailDuvida'		, 'Email')) 	return;
	if(!doString(mensagemDuvida				, 'mensagemDuvida'	, 'Mensagem'))	return;

	var parametros = 'nomeDuvida='+nomeDuvida+'&emailDuvida='+emailDuvida+'&mensagemDuvida='+mensagemDuvida;
	enviarFormularios('controles/gravaDuvida.php',parametros);

	document.getElementById('nomeDuvida').value			= "";
	document.getElementById('emailDuvida').value		= "";
	document.getElementById('mensagemDuvida').value		= "";
}
/******************************************
* fim funcao de Duvida
******************************************/

/******************************************
* funcao valida e grava Sugestao
******************************************/
function validaSugestao() {

	var nomeSugestao 				=  document.getElementById('nomeSugestao').value;
	var dddSugestao 				=  document.getElementById('dddSugestao').value;
	var telSugestao 				=  document.getElementById('telefoneSugestao').value;
	var emailSugestao 				=  document.getElementById('emailSugestao').value;
	var cidadeSusgestaoSaida		=  document.getElementById('cidadeSusgestaoSaida').value;
	var bairroSusgestaoSaida		=  document.getElementById('bairroSusgestaoSaida').value;
	var cidadeSusgestaoDestino		=  document.getElementById('cidadeSusgestaoDestino').value;
	var bairroSusgestaoDestino		=  document.getElementById('bairroSusgestaoDestino').value;
	var horarioChegadaDestino		=  document.getElementById('horarioChegadaDestino').value;
	var horarioRetorno				=  document.getElementById('horarioRetorno').value;
	var horarioRetorno				=  document.getElementById('horarioRetorno').value;
	var horarioRetorno				=  document.getElementById('horarioRetorno').value;
	var referenciaSusgestaoDestino	=  document.getElementById('referenciaSusgestaoDestino').value;
	var referenciaSusgestaoSaida	=  document.getElementById('referenciaSusgestaoSaida').value;

	if(!doString(nomeSugestao					, 'nomeSugestao'			, 'Nome')) 				return;
	if(!doDigits(dddSugestao					, 'dddSugestao'				, 'DDD')) 				return;
	if(!doDigits(telSugestao.replace('-','')	, 'telefoneSugestao'		, 'Telefone')) 			return;
	if(!doEmail(emailSugestao					, 'emailSugestao'			, 'Email')) 			return;
	if(!doString(cidadeSusgestaoSaida			, 'cidadeSusgestaoSaida'	, 'Cidade de Saída')) 	return;
	if(!doString(bairroSusgestaoSaida			, 'bairroSusgestaoSaida'	, 'Bairro de Saída')) 	return;
	if(!doString(cidadeSusgestaoDestino			, 'cidadeSusgestaoDestino'	, 'Cidade de Destino')) return;
	if(!doString(bairroSusgestaoDestino			, 'bairroSusgestaoDestino'	, 'Bairro de Destino'))	return;
	if(!validaHora(horarioChegadaDestino		, 'horarioChegadaDestino'	, 'Horário de Chegada'))return;
	if(!validaHora(horarioRetorno				, 'horarioRetorno'			, 'Horário de Saída'))	return;

	var parametros = 'nomeSugestao='+nomeSugestao+'&telefoneSugestao='+telSugestao+'&emailSugestao='+emailSugestao+'&dddSugestao='+dddSugestao+'&cidadeSusgestaoSaida='+cidadeSusgestaoSaida+'&bairroSusgestaoSaida='+bairroSusgestaoSaida+'&cidadeSusgestaoDestino='+cidadeSusgestaoDestino+'&bairroSusgestaoDestino='+bairroSusgestaoDestino+'&horarioChegadaDestino='+horarioChegadaDestino+'&horarioRetorno='+horarioRetorno+'&referenciaSusgestaoDestino='+referenciaSusgestaoDestino+'&referenciaSusgestaoSaida='+referenciaSusgestaoSaida;
	enviarFormularios('controles/gravaSugestao.php',parametros);

	document.getElementById('nomeSugestao').value 				= "";
	document.getElementById('dddSugestao').value 				= "";
	document.getElementById('telefoneSugestao').value 			= "";
	document.getElementById('emailSugestao').value 				= "";
	document.getElementById('cidadeSusgestaoSaida').value 		= "";
	document.getElementById('bairroSusgestaoSaida').value 		= "";
	document.getElementById('cidadeSusgestaoDestino').value 	= "";
	document.getElementById('bairroSusgestaoDestino').value 	= "";
	document.getElementById('horarioChegadaDestino').value 		= "";
	document.getElementById('horarioRetorno').value 			= "";
	document.getElementById('horarioRetorno').value 			= "";
	document.getElementById('horarioRetorno').value 			= "";
	document.getElementById('referenciaSusgestaoDestino').value = "";
	document.getElementById('referenciaSusgestaoSaida').value 	= "";
}
/******************************************
* fim funcao de Sugestao
******************************************/

function validaIndique(){
	
	var inputNomeRemetente 		=  document.getElementById('inputNomeRemetente').value;
	var inputNomeDestinatario	=  document.getElementById('inputNomeDestinatario').value;
	var inputEmailDestinatario 	=  document.getElementById('inputEmailDestinatario').value;
	var mensagemIndique 		=  document.getElementById('mensagemIndique').value;

	if(!doString(inputNomeRemetente		, 'inputNomeRemetente'		, 'Nome Remetente'))	return;
	if(!doString(inputNomeDestinatario	, 'inputNomeDestinatario'	, 'Nome Destinatário'))	return;	
	if(!doEmail(inputEmailDestinatario	, 'inputEmailDestinatario'	, 'Email Destinatário'))return;
	if(!doString(mensagemIndique		, 'mensagemIndique'			, 'Mensagem')) 			return;

	var parametros = 'nomeRemetente='+inputNomeRemetente+'&nomeDestinatario='+inputNomeDestinatario+'&emailDestinatario='+inputEmailDestinatario+'&mensagem='+mensagemIndique;
	enviarFormularios('../controles/enviaIndicacao.php',parametros);

	document.getElementById('inputNomeRemetente').value 	= "";
	document.getElementById('inputNomeDestinatario').value 	= "";
	document.getElementById('inputEmailDestinatario').value = "";
	document.getElementById('mensagemIndique').value 		= "";
}

function gravaFormularioFretado(idFretado){
	var fretado	 		=  idFretado;
	var inputNomeCadastroLinha		=  document.getElementById('inputNomeCadastroLinha').value;
	var inputDDDCadastroLinha 		=  document.getElementById('inputDDDCadastroLinha').value;	
	var inputTelefoneCadastroLinha 	=  document.getElementById('inputTelefoneCadastroLinha').value;
	var inputEmailCadastroLinha 	=  document.getElementById('inputEmailCadastroLinha').value;
	
	if(!doString(inputNomeCadastroLinha						, 'inputNomeCadastroLinha'		, 'Nome de Cadastro'))	return;
	if(!doDigits(inputDDDCadastroLinha						, 'inputDDDCadastroLinha'		, 'DDD')) 				return;	
	if(!doDigits(inputTelefoneCadastroLinha.replace('-','')	, 'inputTelefoneCadastroLinha'	, 'Telefone')) 			return;	
	if(!doEmail(inputEmailCadastroLinha						, 'inputEmailCadastroLinha'		, 'Email'))				return;
	
	var parametros = 'nome='+inputNomeCadastroLinha+'&ddd='+inputDDDCadastroLinha+'&telefone='+inputTelefoneCadastroLinha+'&email='+inputEmailCadastroLinha+'&idFretado='+fretado;
	enviarFormularios('../controles/cadastraUsuarioFretado.php',parametros);
	
	document.getElementById('inputNomeCadastroLinha').value 	= "";
	document.getElementById('inputDDDCadastroLinha').value 		= "";
	document.getElementById('inputTelefoneCadastroLinha').value = "";
	document.getElementById('inputEmailCadastroLinha').value 	= "";
	
	mostraEscondeForm("divFormularioDeCadastro");
}

/******************************************
* funcão utilizada para enviar formulários
******************************************/
function enviarFormularios(url,parametros){	
	var xmlhttp = criaobjetoXmlHttp();
	xmlhttp.open('POST', url, true);
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			alert(xmlhttp.responseText);
		}
	}
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", parametros.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(parametros);
}
/******************************************
* fim funcoes de enviar formularios
******************************************/

/******************************************
* funcoes de validacao de formulario
******************************************/
var reDigits = /^\d+$/;
function doDigits(pStr, campo, nomeCampo){
	if(pStr.length > 0){
		if (reDigits.test(pStr)) {
 			return true;
 		} else if (pStr != null && pStr != "") {
 			limpaCampo(campo,nomeCampo);
			return false;
 		}
	}else{
		limpaCampo(campo,nomeCampo);
		return false;
	}
}


var reEmail = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
function doEmail(pStr, campo, nomeCampo){
 	if(pStr.length > 0){
		if (reEmail.test(pStr)) {
 			return true;
 		} else if (pStr != null && pStr != "") {
 			limpaCampo(campo,nomeCampo);
 			return false;
 		}
	}else{
		limpaCampo(campo,nomeCampo);
		return false;
	}
}

function doString(pStr, campo, nomeCampo){
	if(pStr.length == 0 || pStr == ""){
		limpaCampo(campo,nomeCampo);
		return false;
	}else{
		return true;
	}
}

function validaHora(valor,campo, nomeCampo){
	if(doDigits(valor.replace(':',''),campo,nomeCampo)){
		hrs = (valor.substring(0,2));
		min = (valor.substring(3,5));

		if ((hrs < 00 ) || (hrs > 23) || (min < 00) ||( min > 59)){
			limpaCampo(campo,nomeCampo);
			return false;
		}
		return true;
	}else{
		return false;
	}
}
/******************************************
* fim das funcoes de validacao
******************************************/

function limpaCampo(campo, nomeCampo){
	document.getElementById(campo).value = '';
	document.getElementById(campo).focus();
	alert('O valor do campo : '+ nomeCampo +' é inválido !!!');
}


function validaTamanhoDDD(valor,campo){
	if(valor.length == 2){
		document.getElementById(campo).focus();
	}
}

function mascaraHora(valor,campo){
	if(valor.length == 2){
		document.getElementById(campo).value = valor+':';
	}
}


function abrePopup(){
	window.open('./includes/indique.php','indique','top=80,left=80,width=350,height=350,resizable=no,toolbar=no,statusbar=no,scrollbars=no');
}

function abrePopupItinerario(idItnerario){
	window.open('./includes/buscaItinerarioFretado.php?buscaFretado='+idItnerario,'itinerario'+idItnerario, 'top=80, left=80, width=625, height=600, resizable=no, toolbar=no, statusbar=no, scrollbars=yes');
}

function validaBuscaitinerario(id){

	document.getElementById(id).checked = true;

	if(id == 'idavolta'){
		document.getElementById('volta').checked = false;
		document.getElementById('ida').checked = false;
	}else if(id == 'volta'){
		document.getElementById('idavolta').checked = false;
		document.getElementById('ida').checked = false;
	}else{
		document.getElementById('idavolta').checked = false;
		document.getElementById('volta').checked = false;
	}
}

/******************************************
* funcoes de busca (Select)
******************************************/

function atualizaBuscaSelectBairroSaida(url,idSelect,idSelectPreencher){

    if( limparOptions('selectBairroSaida'	, 'Selecione o Bairro ou Local') &&
        limparOptions('selectCidadeChegada'	, 'Selecione a Cidade')			 &&
		limparOptions('selectBairroChegada'	, 'Selecione o Bairro ou Local')
     ){
		var tipoBuscaBairroSaida =retornaTipoBusca();
		var parametros = 'idBusca='+document.getElementById(idSelect).value+'&tipoBusca='+tipoBuscaBairroSaida;

        if(document.getElementById(idSelect).value > 0){
            var xmlhttp = criaobjetoXmlHttp();
			xmlhttp.open('POST', url, true);
			xmlhttp.onreadystatechange = function(){
			    if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
				    xmlTree = xmlhttp.responseXML;
				    var bairros = xmlTree.getElementsByTagName('opcao');
				    for(i = 0; i < bairros.length; i++){
				        valueBairro = bairros[i].getAttribute('value')
					    nomeBairro =  bairros[i].firstChild.nodeValue;
					    document.getElementById(idSelectPreencher).options[i] =  new Option(nomeBairro,valueBairro)
				    }
			    }
            }
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlhttp.setRequestHeader("Content-length", parametros.length);
			xmlhttp.setRequestHeader("Connection", "close");
			xmlhttp.send(parametros);
		}
	}
}

function atualizaBuscaSelectCidadeChegada(url,idSelectCidadeSaida,idSelectBairroSaida,idSelectPreencher){

	var	tipoBuscaCidadeChegada =retornaTipoBusca();
	var idCidade   =  document.getElementById(idSelectCidadeSaida).value;
	var idBairro   =  document.getElementById(idSelectBairroSaida).value;
	var parametros = 'idCidade='+idCidade+'&idBairro='+idBairro+'&tipoBusca='+tipoBuscaCidadeChegada;
	
	if( limparOptions('selectCidadeChegada'	, 'Selecione a Cidade')			 &&
		limparOptions('selectBairroChegada'	, 'Selecione o Bairro ou Local')
     ){
	
		if(document.getElementById(idSelectCidadeSaida).value > 0){
			var xmlhttp = criaobjetoXmlHttp();
			xmlhttp.open('POST', url, true);
			xmlhttp.onreadystatechange = function(){
				if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
					xmlTree = xmlhttp.responseXML;
					var bairros = xmlTree.getElementsByTagName('opcao');
					for(i = 0; i < bairros.length; i++){
						valueBairro = bairros[i].getAttribute('value');
						nomeBairro =  bairros[i].firstChild.nodeValue;
						document.getElementById(idSelectPreencher).options[i] =  new Option(nomeBairro,valueBairro)
					}
				}
			}
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.setRequestHeader("Content-length", parametros.length);
			xmlhttp.setRequestHeader("Connection", "close");
			xmlhttp.send(parametros);
		}
	}
}

function atualizaBuscaSelectBairroChegada(url,idSelectCidadeSaida,idSelectBairroSaida,idSelectCidadeChegada,idSelectPreencher){

	var	tipoBuscaCidadeChegada 	=  retornaTipoBusca();
	var idCidadeSaida   		=  document.getElementById(idSelectCidadeSaida).value;
	var idBairro   				=  document.getElementById(idSelectBairroSaida).value;
	var idCidadeChegada   		=  document.getElementById(idSelectCidadeChegada).value;

	var parametros = 'idCidadeSaida='+idCidadeSaida+'&idBairro='+idBairro+'&tipoBusca='+tipoBuscaCidadeChegada+'&idCidadeChegada='+idCidadeChegada;
	
	if(limparOptions('selectBairroChegada'	, 'Selecione o Bairro ou Local')){	
		if(document.getElementById(idSelectCidadeChegada).value > 0){
			var xmlhttp = criaobjetoXmlHttp();
			xmlhttp.open('POST', url, true);
			xmlhttp.onreadystatechange = function(){
				if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
					xmlTree = xmlhttp.responseXML;
					var bairros = xmlTree.getElementsByTagName('opcao');
					for(i = 0; i < bairros.length; i++){
						valueBairro = bairros[i].getAttribute('value');
						nomeBairro =  bairros[i].firstChild.nodeValue;
						document.getElementById(idSelectPreencher).options[i] =  new Option(nomeBairro,valueBairro)
					}
				}
			}
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.setRequestHeader("Content-length", parametros.length);
			xmlhttp.setRequestHeader("Connection", "close");
			xmlhttp.send(parametros);
		}
	}
}


function buscaItnerarioPorItinerario(url,nroPagina,divCentro,tipoBusca,cidadeSaida,bairroSaida,cidadeChegada,bairroChegada){

	if(tipoBusca && cidadeSaida){
		var	tipoBuscaCidadeChegada 	= tipoBusca;
		var idCidadeSaida   		= cidadeSaida;
		var idBairroSaida   		= bairroSaida;
		var idCidadeChegada   		= cidadeChegada;
		var idBairroChegada   		= bairroChegada;

	}else{
		var	tipoBuscaCidadeChegada 	= retornaTipoBusca();
		var idCidadeSaida   		=  document.getElementById('selectCidadeSaida').value;
		var idBairroSaida   		=  document.getElementById('selectBairroSaida').value;
		var idCidadeChegada   		=  document.getElementById('selectCidadeChegada').value;
		var idBairroChegada   		=  document.getElementById('selectBairroChegada').value;
	}

	var parametros = 'idCidadeSaida='+idCidadeSaida+'&idBairroSaida='+idBairroSaida+'&tipoBusca='+tipoBuscaCidadeChegada+'&idCidadeChegada='+idCidadeChegada+'&idBairroChegada='+idBairroChegada;

	var xmlhttp = criaobjetoXmlHttp();
	xmlhttp.open('POST', url, true);
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			document.getElementById(divCentro).innerHTML = xmlhttp.responseText;
		}
	}
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", parametros.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(parametros);

}


/******************************************
* fim funcoes de busca (Select)
******************************************/


function limparOptions(objselect,mensagem){

    i = 0;
    qtd = 0;
    while(document.getElementById(objselect).options.length > 0){
       if(document.getElementById(objselect).options[i]){
          if(document.getElementById(objselect).options[i].value != ""){
               document.getElementById(objselect).options[i] = null;
          }
       }else{
          if(document.getElementById(objselect).options[qtd]){
              document.getElementById(objselect).options[qtd] = null;
          }
       }
       i++;
    }
    document.getElementById(objselect).options[0] = new Option(mensagem,'');
    return true;
}

function retornaTipoBusca(){

	if(document.getElementById('idavolta').checked){
		tipoBuscaBairroSaida = 'idavolta';
	}else if(document.getElementById('ida').checked){
		tipoBuscaBairroSaida = 'ida';
	}else {
		tipoBuscaBairroSaida = 'volta';
	}

	return tipoBuscaBairroSaida;
}

function pintaListaRuasAzul(obj){
	document.getElementById(obj.id).className = 'pintaListaAzul';
}

function pintaListaRuasBranco(obj){
	document.getElementById(obj.id).className = 'pintaListaBranco';
}

function validaCampo(obj){
	document.getElementById('inputBuscaEndereco').value = document.getElementById(obj.id).innerHTML;
	fechaAutoComplete();
}

function fechaAutoComplete(){
	if(document.getElementById("ulListaRuas").style.display == 'block'){
		document.getElementById("ulListaRuas").style.display = "none"
	}
}

function autoCompleteRuas(obj,e){
	var tecla = e.keyCode;
	var div = document.getElementById("ulListaRuas");

	if(tecla != 40 && tecla != 38 && tecla != 13 && tecla != 27){
		if(obj.value != "" && 	obj.value[(obj.value.length)-1] != ","){

			var url		   = './includes/buscaItinerarioPorRua.php';
			var parametros = 'idCidade='+document.getElementById('selectCidadeEndereco').value+'&nomeRua='+obj.value;

			var xmlhttp = criaobjetoXmlHttp();
			xmlhttp.open('POST', url, true);
			xmlhttp.onreadystatechange = function(){
				if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
					div.innerHTML = xmlhttp.responseText;
					div.style.display = "block"
				}
			}
		    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		    xmlhttp.setRequestHeader("Content-length", parametros.length);
		    xmlhttp.setRequestHeader("Connection", "close");
		    xmlhttp.send(parametros);

		}else{
			fechaAutoComplete();
		}
	}else{
		if(div.style.display == "block"){

			var lista = document.getElementById("ulListaRuas");
			if(tecla == 40){//seta para baixo
				var divAzul = false;
				for(i = 1; i <= 5; i++){
					if(document.getElementById("local"+i)){
            			if(document.getElementById("local"+i).className == 'pintaListaAzul'){
							document.getElementById("local"+i).className = 'pintaListaBranco';
							divAzul = i;
            			}
					}
				}

				if(document.getElementById("local"+(divAzul+1))){
					document.getElementById("local"+(divAzul+1)).className = 'pintaListaAzul';
				}else{
                 	if(!divAzul){
                 		document.getElementById("local1").className = 'pintaListaAzul';
                 	}else{
                 		document.getElementById("local"+divAzul).className = 'pintaListaAzul';
                 	}
				}
			}

			if(tecla == 38){//seta para cima
				var divAzul = false;
				for(i = 1; i <= 5; i++){
					if(document.getElementById("local"+i)){
            			if(document.getElementById("local"+i).className == 'pintaListaAzul' && i > 1){
							document.getElementById("local"+i).className = 'pintaListaBranco';
							divAzul = i;
            			}
					}
				}

                if(document.getElementById("local"+(divAzul-1))){
                    document.getElementById("local"+(divAzul-1)).className = 'pintaListaAzul';
                }else{
                    document.getElementById("local1").className = 'pintaListaAzul';
                }
			}

			if(tecla == 13){//enter
                var valor = false;
				for(i = 1; i <= 5; i++){
					if(document.getElementById("local"+i)){
            			if(document.getElementById("local"+i).className == 'pintaListaAzul'){
							valor = document.getElementById("local"+i).innerHTML;
							document.getElementById("inputBuscaEndereco").value = valor;
            			}
					}
				}

				if(valor){
					fechaAutoComplete();
				}
			}

			if(tecla == 27) fechaAutoComplete();;//esc
		}
	}
}

function buscaItnerarioPorEndereco(valorCidade, valorRuas ,numeroPagina){

	if(!valorCidade){
		valorCid = document.getElementById("selectCidadeEndereco").value;
		valorRua = document.getElementById("inputBuscaEndereco").value;
		pagina   = 0;
	}else{
		valorCid = valorCidade 	;
		valorRua = valorRuas 	;
		pagina   = numeroPagina	;
	}

	if(!doString(valorRua, 'inputBuscaEndereco' , 'Rua')) return;
	var parametros = 'idCidade='+valorCid+'&nomeRua='+valorRua+'&pagina='+pagina;

	var xmlhttp = criaobjetoXmlHttp();
	xmlhttp.open('POST', './includes/buscaLinhasPorRua.php', true);
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			document.getElementById('divConteudoCentro').innerHTML = xmlhttp.responseText;
		}
	}
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", parametros.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(parametros);


}

function abrirModal(idFretado,tituloFretado){		
	var win = new Window({	className: "mac_os_x",  
							width:630, 
							height:410, 
							zIndex: 100, 
							maximizable: false, 
                      		minimizable: false,
                      		resizable:   false, 
							title: tituloFretado, 
							showEffect:Effect.BlindDown,
							hideEffect: Effect.BlindUp,   								
							draggable:true, 
							wiredDrag: true})
  								
	win.setURL("includes/buscaItinerarioFretado.php?buscaFretado="+idFretado);
	win.setStatusBar(tituloFretado);
	win.showCenter();   		
}

function mostraEscondeForm(div){
	if(document.getElementById(div).style.display == 'block'){
		document.getElementById(div).style.display = 'none'
	}else{
		document.getElementById(div).style.display = 'block'
	}
}


