//Globais
var opened_soc = new Array();
var opened_desc = new Array();
var aux = "0";
var guarda_id = "-1";

//funcoes
meuAjax = function (){  //inicia Ajax
     try{  
         xmlhttp = new XMLHttpRequest();  
     }catch(ee){  
         try{  
             xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");  
         }catch(e){  
             try{  
                 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");  
             }catch(E){  
                 xmlhttp = false;  
             }  
         }  
     }  
}  
 
ajaxGet = function( nomeUrl, divRetorno ,id_empresa){  // trata os socios
	var alredy=0;
    divInsereResposta = document.getElementById( divRetorno );  
       
    meuAjax();  
    xmlhttp.abort();  
	if( opened_soc[id_empresa]=="1"){ alredy=1; }
	if(alredy==0){
    	xmlhttp.open("GET", nomeUrl ,true);    
		opened_soc[id_empresa]="1";
	}   
	else{
		xmlhttp.open("GET", "pagina_branca.php" ,true);    
		opened_soc[id_empresa]="0";
	}  
    xmlhttp.setRequestHeader("Content-Type", "text/html; charset=UTF-8");  
   
    xmlhttp.onreadystatechange = function() {  
             if( xmlhttp.readyState == 4 ){    
                var valorRetorno            = xmlhttp.responseText;  
                divInsereResposta.innerHTML = valorRetorno;           
            }  
     } 
	 var imagem_id = "img"+id_empresa;
     xmlhttp.send( null );  
}

ajaxDesc = function( nomeUrl, divRetorno ,id_empresa){  // trata as descricoes
	var alredy=0;
    divInsereResposta = document.getElementById( divRetorno );  
	if(guarda_id==id_empresa){opened_desc[guarda_id]="1"; aux++;}
	else { opened_desc[guarda_id]="0";  aux="0"; }
	if(aux >= 2){opened_desc[guarda_id]="0"; aux="0";}
    meuAjax();  
    xmlhttp.abort();  
	if( opened_desc[id_empresa]=="1"){ alredy=1; }
	if(alredy==0){
    	xmlhttp.open("GET", nomeUrl ,true);    
		opened_desc[id_empresa]="1";
	}   
	else{
		xmlhttp.open("GET", "pagina_branca.php" ,true);    
		opened_desc[id_empresa]="0";
	}  
    xmlhttp.setRequestHeader("Content-Type", "text/html; charset=UTF-8");  
   
    xmlhttp.onreadystatechange = function() {  
             if( xmlhttp.readyState == 4 ){    
                var valorRetorno            = xmlhttp.responseText;  
                divInsereResposta.innerHTML = valorRetorno;           
            }  
     } 
	 guarda_id = id_empresa;
     xmlhttp.send( null );  
}  

function change(x,id){
	if( opened_soc[id]=="1"){ 
		x.src='../imagens/sociofechado.gif';
	}
	else {
		x.src='../imagens/socioaberto.gif';
	}

}

function changePos(obj,id) { // div empresa filha de descricao
	ajaxDesc('desc_pag.php?empresa='+id, 'desc_div',id );
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	div = document.getElementById("desc_div");
	div.style.backgroundColor = '#FFFFFF';
	div.style.bordercolor = '#ff6666';
	div.style.left = curleft-170+'px';
	div.style.top = curtop+30 +'px';
	div.style.display = "";
	if(opened_desc[guarda_id]=="1"  ){ div.style.display = ""; }
	else if(opened_desc[guarda_id]=="0" ){ div.style.display = "none"; }
	
}

