function makeRequest(){
	var http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
            // See note below about this line
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!http_request) {
        return false;
    }
    //alert(http_request);
    return http_request;
}//fim da function

function ajaxRespost(DivID,URLparam,OptFlag){
   var xmlhttp=makeRequest();

    if(xmlhttp===false){
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    document.getElementById(DivID).innerHTML = "Carregando ...";
    xmlhttp.open('GET',URLparam, true);
    xmlhttp.onreadystatechange = function(){
	    if (xmlhttp.readyState == 4) {
	       document.getElementById(DivID).innerHTML = xmlhttp.responseText;
	       if(!OptFlag) calculaDivs();
	    }
	}
  	xmlhttp.send(null);
}//fim da function

function ajaxSendTo(DivID, URLparam, ValueInfo){
    var xmlhttp=makeRequest();

    if(xmlhttp===false){
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    document.getElementById(DivID).innerHTML = "<img src='images/loading.gif' alt='Loading...' hspace='0' vspace='0' border='0' />";
    xmlhttp.open("POST",URLparam,true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.onreadystatechange = function(){
	    if (xmlhttp.readyState == 4) {
        	if (xmlhttp.status == 200) {
            	document.getElementById(DivID).innerHTML = xmlhttp.responseText;
        	}else{
            	alert("Houve um problema ao obter os dados:\n" + xmlhttp.statusText);
        	}
    	}
	}//fim da function
    xmlhttp.send(ValueInfo);
}//fim da function

function limpar_div(DivID){
	document.getElementById(DivID).innerHTML='';
	calculaDivs();
	//document.getElementById(DivID).style.height='0px';
}

function evento(){
	if(document.getElementById('f_inicial').value !== null){
		var inc = document.getElementById('f_inicial').value;
		var fim = document.getElementById('f_final').value;
	    var local = document.getElementById('f_local').value;

		ajaxRespost('f_eventos','includes/carrega_combo.php?dtainc='+inc+'&dtafim='+fim+'&cid='+local,1);
	}
}

function buscaevento(opt){
	var cod = 0;
	var local = 0;
	var inc = null;
	var fim = null;

	if(document.getElementById('f_inicial').value !== null){
		inc = document.getElementById('f_inicial').value;
		fim = document.getElementById('f_final').value;
		if(!opt){ local = document.getElementById('f_local').value; }
	   	else{ cod = document.getElementById('f_event').value; }
		document.location='index.php?q=evento&dtainc='+inc+'&dtafim='+fim+'&cid='+local+'&id='+cod ;
	}
}



function getrota(){
		var cid = document.frm.cida.value;

		document.frm.action='index.php?q=destino&id=3&cod='+cid;
		document.frm.submit();
}

/*
 *
 *  ALTERAR O TAMANHO DAS DIVS
 *
 */

var DefMenuEsq = 0;
var marginTop = 260; 

function calculaDivs(DivSelect){
	if(!DivSelect)DivSelect = 'reload';
	DefMenuEsq = parseInt(document.getElementById('menuEsquerda').offsetHeight);
	var tamDiv = parseInt(document.getElementById(DivSelect).offsetHeight);
	tamDiv+= marginTop;
	if(tamDiv > DefMenuEsq){
		document.getElementById('corpodosite').style.height= tamDiv.toString()+'px';
	}else{
		document.getElementById('corpodosite').style.height= DefMenuEsq.toString()+'px';
	}
}//fim da function

/*
 *
 *  ALTERAR FONTES DE TAGS
 *
 */

var tamfonte = 0;
var notpageload = 0;

function initialFontSize(){
	var tam = readCookie("tamfonte");
	//alert(tam);
	if(tam != null){
		tamfonte = parseInt(tam);
		changeFontSize(tamfonte);
	}
}

function changeFontSize(inc){
	
  achei = 0;
	tags = new Array();
	tags[0] = "p";
	tags[1] = "div";
	tags[2] = "span";
	tags[3] = "h1";
	tags[4] = "h2";
	tags[5] = "h3";
	tags[6] = "h4";
	tags[7] = "select";
	tags[8] = "a";
	tags[9] = "h5";
	tags[10] = "li";
	tags[11] = "input";
	tags[12] = "textarea";
	tags[13] = "body";
	tags[14] = "td";
	tags[14] = "strong";
	for(i=0; i<tags.length; i++){
		var p = document.getElementsByTagName(tags[i]);
		for(n=0; n<p.length; n++) {
			if(p[n].style.fontSize)	{
				var size = parseInt(p[n].style.fontSize.replace("px", ""));
			} else {
				if(i==4) var size = 16;
				else if(i==5) var size = 14;
				else if(i==6) var size = 16;
				else if(i==3) var size = 16;
				else var size = 12;
			}
			p[n].style.fontSize = size+inc + 'px';
		}
	}
	if(notpageload)
	{
		createCookie("tamfonte", tamfonte+inc, 0);
		tamfonte = tamfonte + inc;
	}
	notpageload = 1;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');

  for(var i=0;i < ca.length;i++) {
    var c = ca[i];

    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name){
	createCookie(name,"",-1);
	history.go(0);
}

/*
 * troca o fundo da index
 */

function trocafundo(){
	var imgs = new Array("img_sup1.jpg","img_sup2.jpg","img_sup3.jpg","img_sup4.jpg","img_sup6.jpg","img_sup7.jpg",
                         "img_sup8.jpg","img_sup9.jpg","img_sup10.jpg","img_sup11.jpg","img_sup13.jpg","img_sup14.jpg",
                         "img_sup15.jpg","img_sup16.jpg","img_sup17.jpg","img_sup18.jpg","img_sup19.jpg","img_sup20.jpg",
                         "img_sup21.jpg","img_sup22.jpg","img_sup23.jpg","img_sup24.jpg","img_sup25.jpg","img_sup26.jpg",
                         "img_sup27.jpg","img_sup28.jpg","img_sup29.jpg","img_sup30.jpg");
    var y = Math.floor(Math.random()*imgs.length);

	//if (y==0) y=1;
	document.getElementById('cabec').style.background="#FFCC80 url('imagens/"+imgs[y]+"') no-repeat";
}

/*
 *	PEGA O ID DA LIST BOX CIDADE PORTAO E ENVIA PARA VIATERRESTRE.PHP
 */

function munio(){
    var local = document.getElementById('cidadeportao').value;
     ajaxRespost('munidiv','includes/carrega_viaterrestre.php?cid='+local+'');
}

/*
 *	devaneio
 */

function munid(){
    var local = document.getElementById('cidadedestino').value;

     ajaxRespost('munidiv','includes/carrega_viaterrestre.php?cid='+local);
}

/*
 *	FUNÇÃO REFERENTE A REDIRECIONAMENTO ATRAVES DE BOTOES
 */

function getmunicipio(){
   var origem = document.getElementById('cidadeportao').value;
   var destino = document.getElementById('cidadedestino').value;

    location.href='index.php?q=comochegar&muno='+origem+'&mund='+destino+'&cod=8&fg=2';
}



/*
 *	FUNÇÃO REFERENTE A REDIRECIONAMENTO ATRAVES DE BOTOES DA GUIA GUIAS
 */

function getguianome(){
   var nome = document.getElementById('nomeguia').value;

    location.href='index.php?q=guiaturista&cod=1&nom='+nome+'';
}

/*
 *	FUNÇÃO REFERENTE A REDIRECIONAMENTO ATRAVES DE BOTOES DA GUIA GUIAS
 */

function getguiaidioma_cidade(){
   var nome = document.getElementById('nomeguia').value;
   var cat = document.getElementById('catguia').value;
   var cidade = document.getElementById('cidadeguia').value;
   var idioma = document.getElementById('idiomaguia').value;

   location.href='index.php?q=guiaturista&cod=1&cid='+cidade+'&lang='+idioma+'&nom='+nome+'&cat='+cat+'';
}

/*
 *	FUNÇÃO REFERENTE A REDIRECIONAMENTO ATRAVES DE BOTÕES DA GUIA AGÊNCIA
 */

function getagencia_receptiva(){
   var pesquisa = document.getElementById('pesq').value;
   //var caminho = document.getElementById('cami').value;

    //if(pesquisa){
		location.href='index.php?q=guiaturista&cod=11&pgi=1&rota='+pesquisa+'';
	//}else if('cami'){
	  //  location.href='index.php?q=guiaturista&cod=11&cami='+caminho+'&pgi=3';
	//}
}

/*
 *FUNÇÃO REFETENTE A REDIRECIONAMENTO ATRAVES DE BOTÕES DA GUIA AGÊNCIA
 */

function getagencia(){
  var agencia = document.getElementById('agencia').value;

  location.href = 'index.php?q=estabelecimento&cod=32&opt=1&fg=5&te=&ts=&pgi=4&id='+agencia+'';
}
//limpar e reescrever nos campos  de textbox

function limpa_campo(mesmo,conteudo){
	if(mesmo.value == conteudo){mesmo.value = "";}
}

function recupera_campo(mesmo,conteudo)	{
  if(mesmo.value == ""){mesmo.value = conteudo;}
}

/*
 *FUNÇÃO PARA VERIFICAR SE HÁ ALGO NOS CAMPOS DA PAGINA GUIAS
 */

 function verifica_campoSelected(){

   var categoria = document.guiaturismo.catguia.selectedIndex == 0;
   var idioma = document.guiaturismo.idiomaguia.selectedIndex == 0;
   var cidade = document.guiaturismo.cidadeguia.selectedIndex == 0;
   var nomeguia = document.guiaturismo.nomeguia.selectedIndex == 0;

 if (!categoria || !idioma || !cidade || !nomeguia) {
  /*alert (categoria+'\n'+idioma+'\n'+cidade+'\n'+nomeguia);*/
 }else{
     alert('Por favor, selecione um dos campos.');
    return false;
 }
}
 
 /**libera_mes
  **função javascript que é utilizada pela common.php para popular o listbox com os messes disponiveis
  **ano - pega o valor do campo com id igual a ano
  **e redireciona para a pagina carrega_combo
  **/
 function libera_mes(){
	if(document.getElementById('data_ano').value !== null){
		var ano = document.getElementById('data_ano').value;
		
		ajaxRespost('mes','includes/carrega_combo.php?&ano='+ano+'&cod=1',1);
	}
}
 
function buscaEventoParam(){

var tcid= document.getElementById('busca_Cidade').value;
var tseg= document.getElementById('busca_Segmento').value;
var ttip= document.getElementById('busca_Evento').value;
var tdti= document.getElementById('f_busca_inicial').value;
var tdtf= document.getElementById('f_busca_final').value;
var tmes= document.getElementById('data_ano_mes').value;
var tano= document.getElementById('data_ano').value;

document.location.href='index.php?q=evento&mes='+tmes+'&ano='+tano+'&dtainc='+tdti+'&dtafim='+tdtf+'&id='+tcid+'&segm='+tseg+'&tipev='+ttip+'&cod=0';

}
