function enviarPagina( pagina ){
    if( pagina != null && pagina != "" ){
        document.getElementById("frm").action = pagina;
        document.getElementById("frm").method = "POST";
        document.getElementById("frm").submit();
    }
}

function isPlataformaMovel(){
    var strVersao = navigator.appVersion;
    if(strVersao.indexOf("Mobile", 0) > -1){        
        return true;
    }
    return false; 
    
}
function xmlhttpPostBairro(strURL) {
    var xmlHttpReq = false;
    var self = this;
    
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }    

    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if ( self.xmlHttpReq.readyState == 4 ){
            //setTimeout( 'atualizarPaginaBairro( self.xmlHttpReq.responseText )', 500 );            
            atualizarPaginaBairro( self.xmlHttpReq.responseText );
        }
    }
    var form = document.frm;
    var qstr = "";
    qstr += 'dispositivoMovel=true';
    if( escape(form.lst_cidade.value) != "" ){
        qstr+= '&cidade=' + escape(form.lst_cidade.value);
    }
    if( document.forms['frm'].bairro != null){
        qstr += "&bairro=" + document.forms['frm'].bairro.value;
    }
    self.xmlHttpReq.send( qstr );
}
function atualizarPaginaBairro( lst ){    
    document.getElementById("quadroMultiSelecaoBairro").innerHTML = lst;

}
function abrirPagina( pagina, titulo ){
    if( pagina != null && pagina != "" ){
        var y = screen.height;
        var x = screen.width;
        var opcoes = 'left=0 ,top=0 , toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+ x +',height='+ y +'';
        window.open(pagina, titulo ,opcoes);
        

 }
}
function habilitarDesabilitar( id ){
    if( document.getElementById( id ).style.display == '' ){
        document.getElementById( id ).style.display = 'none';
    }
    else{
        document.getElementById( id ).style.display = '';
    }
}
function preencherInputHiddenBairro(){
    var bairros = "";
    //var arr = document.all.ifrBairro.contentWindow.document.getElementsByName("bairros");
    var arr;
    if( isPlataformaMovel() ){
        arr = document.getElementsByName("bairros");
    }else{
        arr = document.getElementById("ifrBairro").contentWindow.document.getElementsByName("bairros");
    }    
    for( var a = 0; a < arr.length; a++ ){
        if( arr[a].checked ){
            if( bairros == "" ){
                bairros = arr[a].value;
            }
            else{
                bairros += "," + arr[a].value;
            }
        }
    }
    document.getElementById("hdnBairro").value = bairros;
}
function acrescentarCampoAnexo( idDiv ){
    var html = "<input type=\"file\" class=\"input\" name=\"fotos\" id=\"fotos\" style=\"width:100%\" value=\"\">";
    document.getElementById( idDiv ).insertAdjacentHTML("beforeEnd", html);
}
function xmlhttpPostImovelFavorito(strURL) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if ( self.xmlHttpReq.readyState == 4 ){
            alert("Imovel adicionado com sucesso!");
        }
    }
    var form = document.forms['frm'];
    var qstr = 'idImovelAgenciado=' + escape(form.idImovelAgenciado.value);
    self.xmlHttpReq.send( qstr );
}
function abrirDetalhes( idEmpreendimento, idImovelAgenciado ){
    abrirPagina("detalhes.jsp?hdnIDEmpreendimento=" + idEmpreendimento + "&hdnIDImovelAgenciado=" + idImovelAgenciado, "");
}
function removerImovelFavorito( idImovelAgenciado ){
    var y = screen.height;
    var x = screen.width;
    var opcoes = 'left=0 ,top=0 , toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+ x +',height='+ y +'';
    var janela = window.open("removerImovelFavorito.jsp?idImovelAgenciado=" + idImovelAgenciado, "" ,opcoes);
    alert("Imovel removido com sucesso!");
    window.location.reload();
    janela.close();
 }
function abrirPopup(){
    var y = 275;
    var x = 325;
    var opcoes = 'left=350 ,top=300,modal=yes,toolbar=no,location=yes,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width='+ x +',height='+ y +'';  
    var janela = window.open("popup_feirao.jsp", "" ,opcoes);
    janela.focus();
    
}

function aoCarregar(){    
    var dispositivoMovel = isPlataformaMovel();

    if( dispositivoMovel ){        
        //document.getElementById("quadroMultiSelecaoBairro").style.display = '';
        document.getElementById("btnFiltrarBairros").style.display = '';
        xmlhttpPostBairro('filtroBairroMultiSelecaoLocalizar.jsp');        

    }else{
        
        var parametros = "?";
        parametros += "dispositivoMovel=" + dispositivoMovel;
        if( document.forms['frm'].lst_cidade != null ){
            parametros += "&cidade=" + document.forms['frm'].lst_cidade.value;
        }
        if( document.forms['frm'].bairro != null){
            parametros += "&bairro=" + document.forms['frm'].bairro.value;
        }
        document.getElementById("ifrBairro").style.display = '';        
        document.getElementById("ifrBairro").src = "filtroBairroMultiSelecaoLocalizar.jsp" + parametros ;        
        if( document.forms['frm'].bairro != null ){
            document.forms['frm'].bairro.value = "";
        }
        
    }
}

