function carregaXmlHttp() {
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP")
    } else {
        alert('Seu browser não suporta os recursos utilizados por este site.');
    }
    return xmlhttp
}

function doBuscaCidade(uf){

    xmlhttp = carregaXmlHttp();

    //limpa o select
    var cobobox = document.getElementById("txtCidade")
    while(cobobox.options.length>0)cobobox.options[0]=null
    cobobox.options[0] = new Option("Aguarde...","Aguarde...")

    //Monta a url com a uf
    xmlhttp.open("GET", "/topsearch/pyscripts/xmlCidades?id_uf=" + uf,true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
    xmlhttp.setRequestHeader("Pragma", "no-cache");

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState == 4) {

            if (xmlhttp.status == 200){
                //limpa o select
                var cobobox = document.getElementById("txtCidade")
                while(cobobox.options.length>0)cobobox.options[0]=null
            
                //Pega resultado do xml
                var resp = xmlhttp.responseXML;
                var raiz = resp.getElementsByTagName("cidades").item(0);
                var cidades = raiz.getElementsByTagName("cidade");

                //preenche a caixa de opções com os valores recebidos
                for(var i = 0; i < cidades.length; i++) {
        
                    idCidade = cidades.item(i).getAttribute("id");
                    nomeCidade = cidades.item(i).firstChild.data;
                    cobobox.options[cobobox.options.length]=new Option(nomeCidade, idCidade)
                }
            } else {
                alert("Não foi possível carregar as cidades deste estado.");
            }
        }
    }
    xmlhttp.send(null);
}

function doBuscaSubcategoria(id_pai_cat){

    var cobobox = document.getElementById("txt2");

    xmlhttp = carregaXmlHttp();

    //limpa o select

    while(cobobox.options.length>0)cobobox.options[0]=null
    cobobox.options[0] = new Option("Aguarde...","Aguarde...")

    //Monta a url com a uf
    xmlhttp.open("GET", "/topsearch/pyscripts/xmlSubcategorias?id_pai_cat=" + id_pai_cat,true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
    xmlhttp.setRequestHeader("Pragma", "no-cache");

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState == 4) {

            if (xmlhttp.status == 200){
                //limpa o select
                var cobobox = document.getElementById("txt2")
                while(cobobox.options.length>0)cobobox.options[0]=null
            
                //Pega resultado do xml
                var resp = xmlhttp.responseXML;
                var raiz = resp.getElementsByTagName("subcategorias").item(0);
                var subcategorias= raiz.getElementsByTagName("subcategoria");

                //preenche a caixa de opções com os valores recebidos
                for(var i = 0; i < subcategorias.length; i++) {
        
                    idCategoria = subcategorias.item(i).getAttribute("id");
                    nomeCategoria = subcategorias.item(i).firstChild.data;
                    cobobox.options[cobobox.options.length]=new Option(nomeCategoria, idCategoria)
                }
            } else {
                alert("Não foi possível carregar as subcategorias desta categoria.");
            }
        }
    }
    xmlhttp.send(null);
}

function IsVazio( sConteudo ) {
	var iPrimeiro_caracter = sConteudo.charAt(0);
	
	if ( ( sConteudo != null ) && ( sConteudo != "" ) && ( iPrimeiro_caracter != " " ) ) {
		
		var sTamanho_conteudo = sConteudo.length;
		for ( var iCount = 0; iCount < sTamanho_conteudo; iCount++ ) {
			var sCaracter = sConteudo.charAt( iCount );
			if ( ( sCaracter != '\n' ) && ( sCaracter != '\t' ) ) {
				return false;
			}
		}
	} else {
		return true;
	}
}

function IsVazio( sConteudo ){
	var iPrimeiro_caracter = sConteudo.charAt(0);
	if ( ( sConteudo != null ) && ( sConteudo != "" ) && ( iPrimeiro_caracter != " " ) ) {
		var sTamanho_conteudo = sConteudo.length;
		for ( var iCount = 0; iCount < sTamanho_conteudo; iCount++ ) {
			var sCaracter = sConteudo.charAt( iCount );
			if ( ( sCaracter != '\n' ) && ( sCaracter != '\t' ) ) {
				return false;
			}
		}
	} else {
		return true;
	}
}

function validaForm( oForm ){
    
	searchterm  = oForm.elements['searchTerm'];
	idcat = oForm.elements['id_cat'];
	idsubcat = oForm.elements['id_subcat'];
	seluf = oForm.elements['sel_uf'];
	selcidade = oForm.elements['sel_cidade'];
       	
	if ( IsVazio( searchterm.value ) || ( searchterm.value == 'Digite...' ) ) {


		if ( idcat.value == '-1' ) {
			alert('Por favor, selecione a categoria.');
			idcat.focus();
			return false;
		}


		if ( idsubcat.value == 'Selecione...' ) {
			alert('Por favor, selecione a subcategoria.');
			idsubcat.focus();
			return false;
		}

		if ( seluf.value == '-1' ) {
			alert('Por favor, selecione um estado.');
			seluf.focus();
			return false;
		}


		if ( selcidade.value == 'Selecione...' ) {
			selcidade.value = '';
		}


	}

}
