function autentica(arquivo,idForm) {

var ajaxPost = XMLHTTPRequest();
ajaxPost.open("POST", ("./functions/"+arquivo+".php"), true);
ajaxPost.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajaxPost.send(BuscaElementosForm(idForm));

  ajaxPost.onreadystatechange = function() {
  if (ajaxPost.readyState == 4) {
  	gebi('msglog').innerHTML = 'Carregando...';
  		if(ajaxPost.responseText == false) {
	    	gebi('conteudo').innerHTML = 'Bem Vindo ao Painel de Administração';
	    	go('menu','menu');
	    	var texto = ajaxPost.responseText;
	    	extraiScript(texto);
  }else{
  	gebi('msglog').innerHTML = ajaxPost.responseText;
    }
  }
  };
};

function go(div,arquivo,action) {

if(div == 'conteudo') gebi('hist').value = arquivo;
	
var ajax = XMLHTTPRequest();

ajax.open("GET", ("http://www.guiasaopauloimoveis.com.br/painel/functions/" + arquivo + ".php?op=" + action), true);

  ajax.onreadystatechange = function() {
  if (ajax.readyState == 1) {
    gebi(div).innerHTML = "<img src='../imagens/carrega.gif' align='absmiddle'> Carregando...";
  } else if (ajax.readyState == 4) {
  if(ajax.responseText == false){
    gebi(div).innerHTML = "Erro ao carregar arquivo";
  }else{
    gebi(div).innerHTML = ajax.responseText;
    texto = ajax.responseText;
    extraiScript(texto);
    }
  }
  }
ajax.send(null);
};

function salvar() {

var ajaxPost = XMLHTTPRequest();
ajaxPost.open("POST", ("./functions/inclui.php"), true);
ajaxPost.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajaxPost.send(BuscaElementosForm('editform'));

  ajaxPost.onreadystatechange = function() {
  if (ajaxPost.readyState == 4) {
  if(ajaxPost.responseText == false){
    alert('Erro');
  }else{
  	alert(ajaxPost.responseText);
  	editclose();
  	go('conteudo',gebi('hist').value);
    }
  }
  }
;
}

function edit(tabela,id) {
	
	var ml = screen.width/2 - 300;
	
	gebi('editarea').style.display = 'block';
	gebi('oculta').style.display = 'block';
	
	$("#editarea").animate({ 
        width: 600,
        left: ml
      }, 500 );
      
      $("#oculta").animate({ 
        opacity: 0.5
      }, 500 );

      go('editarea','edit','&tabela='+tabela+'&id='+id);
}

function editImoveis(arquivo,id) {
	
	var ml = screen.width/2 - 380;
	
	gebi('editarea').style.display = 'block';
	gebi('oculta').style.display = 'block';
	
	if(arquivo == "cad_construtoras") {
      	ml = screen.width/2 - 300;
      	$("#editarea").animate({ 
        width: 600,
        left: ml
      }, 500 );
      } else if(arquivo == "cad_contatos") {
      	ml = screen.width/2 - 300;
      	$("#editarea").animate({ 
        width: 600,
        left: ml
      }, 500 );
      } else $("#editarea").animate({ 
        width: 760,
        left: ml
      }, 500 );
      
      $("#oculta").animate({ 
        opacity: 0.5
      }, 500 );
      
      

      go('editarea',arquivo,'&id='+id);
}

function editI(tipo,id) {
	
	var ml = screen.width/2 - 300;
	
	gebi('editarea').style.display = 'block';
	gebi('oculta').style.display = 'block';
	
	$("#editarea").animate({ 
        width: 600,
        left: ml
      }, 500 );
      
      $("#oculta").animate({ 
        opacity: 0.5
      }, 500 );

      go('editarea','cad_imovel','&tipo='+tipo+'&id='+id);
}

function exclui(tabela,campo,id) {
      if (confirm("Você realmente deseja deletar este registro?")) {
             go('exec','exclui','&tabela='+tabela+'&campo='+campo+'&id='+id);
         }
}

function liberar(id,valor) {
	go('exec','libera','&valor='+valor+'&id='+id);
}

function exImovel(id_imovel,id_endereco) {
	if (confirm("Você realmente deseja deletar este registro?")) {
             go('exec','exclui','&tabela=tb_imoveis&campo=id_imovel&id='+id_imovel);
             go('exec','exclui','&tabela=tb_configs&campo=ide_imovel&id='+id_imovel);
             go('exec','exclui','&tabela=tb_fotos&campo=ide_imovel&id='+id_imovel);
             go('exec','exclui','&tabela=tb_enderecos&campo=id_endereco&id='+id_endereco);
         }
}
function editclose() {
	gebi('editarea').innerHTML = '';
	
	gebi('editarea').style.display = 'none';
	gebi('oculta').style.display = 'none';
  
      $("#oculta").animate({ 
        opacity: 0
      }, 500 );
	
}
