//////////////////////////////////////////////////////////////////////////////////


/// APARENCIA


//////////////////////////////////////////////////////////////////////////////////
function ExpandirBloco(num){
	var bloco = document.getElementById("blocoexp"+num);
	if (bloco.style.display == "none"){ // nao e retrair
		var divs = document.getElementById("conteudo").getElementsByTagName("div");
		for(var d=0; d<divs.length ;d++){
			if (divs[d].className=="amostrabloco"){
				if (divs[d].style.display == "none"){
					var idb = divs[d].id.substr(12);
					ExpandirBlocoIndiv(idb);
				}
			}
		}
	}
	ExpandirBlocoIndiv(num);
}
function Toggle(elem,blockinline){
	if (document.getElementById(elem).style.display=="none"){
		if (blockinline=="inline"){
			document.getElementById(elem).style.display = "inline";
		} else {
			document.getElementById(elem).style.display = "block";
		}
	} else {
		document.getElementById(elem).style.display = "none";
	}
}
function ExpandirBlocoIndiv(num){
	var img = document.getElementById("conteudobloco"+num).getElementsByTagName("a")[0].getElementsByTagName("img")[0];
	var bloco = document.getElementById("blocoexp"+num);
	if (bloco.style.display == "block"){ //retrair
		img.src = img.src.replace("retrair","expandir");
	} else {
		img.src = img.src.replace("expandir","retrair");
	}
	Toggle("blocoexp"+num);
	Toggle("amostrabloco"+num);
}
function LinkInterno(uri){
	document.location.href = "/mestri/mestri/principal.php?lang=2&item="+uri;
}
function TipValor(elem,foco,tip) {
	if (foco == 1 && elem.value==tip){//entrafoco
		elem.value='';
	} else {//saifoco
		if (elem.value=='') elem.value=tip;
	}
}
function mudaOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}
function Fade(id, inout, milliseg) {
    var veloc = Math.round(milliseg / 100);
    var timer = 0;
    if (inout=="out") {
		//mudaOpac(100,id);
        for (i = 100; i >= 0; i--) {
            setTimeout("mudaOpac(" + i + ",'" + id + "')",(timer * veloc));
            timer++;
        }
		document.getElementById(id).display = "none";
    } else {
		mudaOpac(0,id);
		document.getElementById(id).display = "block";
        for (i = 0; i <= 100; i++) {
            setTimeout("mudaOpac(" + i + ",'" + id + "')",(timer * veloc));
            timer++;
        }
    }
} 
function SubMenu(num){
	var divs = document.getElementById("menu").getElementsByTagName("div");
	var x = false;
	
	for (var d=0; d<divs.length ;d++){
		if (divs[d].id.substr(5,1)==num){
			var dd = divs[d].style.display;
			if (dd=="none" || !dd){
				if (!x) x="block"; 
				divs[d].style.display=x;
			} else {
				if (!x) x="none"; 
				divs[d].style.display=x;
			}
		}
	}
}

function AreaTotal(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // todos menos Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...funcionaria tb no Explorer 6 modo Strict, Mozilla e Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// todos menos Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 modo Strict 
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // outros Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	if (yScroll < windowHeight){
		AlturaTotal = windowHeight;
	} else {
		AlturaTotal = yScroll;
	}
	if (xScroll < windowWidth){
		LarguraTotal = windowWidth;
	} else {
		LarguraTotal = xScroll;
	}

	return [LarguraTotal,AlturaTotal,windowWidth,windowHeight];
}
function Posicao(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function Overlay(modo,uri,w,h) {
	var objBody = document.getElementsByTagName('body').item(0);
	var Area = AreaTotal();
	var Largura = Area[0];
	var Altura = Area[1];
	var LarguraTela = Area[2];
	var AlturaTela = Area[3];
	var bodyOverlay = document.createElement("div");
	bodyOverlay.setAttribute('id','bodyOverlay');
	bodyOverlay.style.height = Altura + 'px'; // fundo com o tamanho total da página.
	if (!document.getElementById('bodyOverlay')) {
		objBody.insertBefore(bodyOverlay, objBody.firstChild);
	}
	if (modo=="img"){
		var bed = document.createElement("div");
		bed.setAttribute('id','bedOverlay');
		bed.style.width = '100%';
		bed.style.height = Altura + 'px';
		bed.style.top = ((AlturaTela/2)-(h/2)) + "px";
		bed.style.left = ((LarguraTela/2)-(w/2)) + "px";
		objBody.insertBefore(bed, objBody.firstChild);
		
		var fechar = document.createElement("a");
		fechar.setAttribute('id','fechar');
		fechar.innerHTML = "fechar";
		fechar.setAttribute('href','javascript:removerOverlay("img")');
		fechar.style.width = (w-28) + "px";
		bed.appendChild(fechar);
		var imagem = document.createElement("img");
		imagem.setAttribute('id','imgOverlay');
		imagem.setAttribute('src',uri);
		bed.appendChild(imagem);
		
		
		
	} else if (modo=="total"){
		document.getElementById('total').style.display="block";
		NovoConteudoT(uri);
	} else if (modo=="g"){
		document.getElementById('form_grande').style.display="block";
		NovoConteudoG(uri);
	} else if (modo=="p"){
		document.getElementById('form_peq').style.display="block";
		NovoConteudoP(uri);
	} else if (modo=="b"){
		document.getElementById('place_muda_bloco').style.display="block";
		NovoConteudoB(uri);
	} else if (modo=="nada"){
	} else {
		document.getElementById('alterar_'+uri).style.display="block";
	}
}
function removerOverlay(modo,campo) {
	var bodyOverlay = document.getElementById('bodyOverlay');
	if (bodyOverlay) {
		bodyOverlay.parentNode.removeChild(bodyOverlay);
	}
	if (modo=="img"){
		document.getElementById('bedOverlay').parentNode.removeChild(document.getElementById('bedOverlay'));
	} else if (modo=="total"){
		document.getElementById('total').style.display="none";
	} else if (modo=="g"){
		document.getElementById('form_grande').style.display="none";
	} else if (modo=="p"){
		document.getElementById('form_peq').style.display="none";
	} else if (modo=="b"){
		document.getElementById('place_muda_bloco').style.display="none";
	} else {
		document.getElementById('alterar_'+campo).style.display="none";
	}
}
function Barra(acao){
	if (acao == "fecha"){
		document.getElementById("barra1").style.display = "none";
		document.getElementById("barra_sub").style.display = "none";
		document.getElementById("nao_barra").style.display = "inline";
		if (document.getElementById("interna")) document.getElementById("interna").style.backgroundPosition = "0px 0px";
		var estilo ="none";
	} else {
		document.getElementById("barra1").style.display = "block";
		document.getElementById("barra_sub").style.display = "block";
		document.getElementById("nao_barra").style.display = "none";
		if (document.getElementById("interna")) document.getElementById("interna").style.backgroundPosition = "0px 300px";
		var estilo = "block";
	}
	if (document.getElementById("interna")) var icones = document.getElementById("interna").getElementsByTagName("div");
	else if (document.getElementById("blog")) var icones = document.getElementById("blog").getElementsByTagName("div");
	
	for (var i in icones){
		if (icones[i].className == "icomenu" || icones[i].className == "icosimples") icones[i].style.display = estilo;
	}

}
function BarraPropriedades(acao){
	if (acao=="recolher"){
		document.getElementById("propriedades_expandir").style.display = "inline";
		document.getElementById("propriedades_recolher").style.display = "none";
		document.getElementById("sub_2").style.display = "none";	
	} else {
		document.getElementById("propriedades_expandir").style.display = "none";
		document.getElementById("propriedades_recolher").style.display = "inline";	
		document.getElementById("sub_2").style.display = "block";	
	}
}
function Trocavis(elemento,acao){
	alert("trocavis");
	if (acao == "alterar"){
		document.getElementById(elemento+"_mais").style.display = "none";
		document.getElementById("alterar_"+elemento).style.display = "block";
	} else {
		document.getElementById(elemento+"_mais").style.display = "inline";
		document.getElementById("alterar_"+elemento).style.display = "none";
	}
}

function TrocaConteudo(lugar,conteudo){
	document.getElementById(lugar).innerHTML = conteudo;
}

function Legenda(estado,elem,msg,hilite){
	var leg = document.getElementById("legendacentral");
	if (hilite != "") var hilite_elem = document.getElementById(hilite+"_elem");
	if (estado == "in"){
		var x = Posicao(elem)[0];
		var y = Posicao(elem)[1];
		leg.innerHTML = msg;
		leg.style.display="block";
		leg.style.top = (y-5)+"px";
		leg.style.left = (x+16)+"px";
		//alert(hilite)
		if (hilite != "") hilite_elem.style.backgroundColor = "#DDD";
	} else {
		leg.style.display="none";
		if (hilite != "") hilite_elem.style.backgroundColor = "transparent";
	}
}
function Reposicionar(de,para){
	var lde = document.getElementById('formreposlista_label'+de).innerHTML;
	var lpara = document.getElementById('formreposlista_label'+para).innerHTML;
	document.getElementById('formreposlista_label'+para).innerHTML = lde;
	document.getElementById('formreposlista_label'+de).innerHTML = lpara;
	
	var vde = document.getElementById('formreposlista_valor'+de).value;
	var vpara = document.getElementById('formreposlista_valor'+para).value;
	document.getElementById('formreposlista_valor'+para).value = vde;
	document.getElementById('formreposlista_valor'+de).value = vpara;
}
function MudaContato(valor){
	if (valor==1 || valor==2){
		document.getElementById('formmc_vp').style.display = 'block';
		document.getElementById('formmc_sel').style.display = 'none';
	} else if (valor == 3){
		document.getElementById('formmc_vp').style.display = 'none';
		document.getElementById('formmc_sel').style.display = 'none';
	} else if (valor == 4){
		document.getElementById('formmc_vp').style.display = 'none';
		document.getElementById('formmc_sel').style.display = 'block';
	}
}

function AdicionaCategoria(valor,elem){
	elemento = document.getElementById("cat-"+elem);
	if (valor != "---"){
		if (elemento.value==""){
			elemento.value = valor;
		} else {
			elemento.value += ","+valor;
		}
	}
}
//////////////////////////////////////////////////////////////////////////////////


/// DATA


//////////////////////////////////////////////////////////////////////////////////

function MudaData(){
	document.getElementById('sel_valor').value = document.getElementById('sel_dia').value+"/"+document.getElementById('sel_mes').value+"/"+document.getElementById('sel_ano').value;
}

function GeraData(quando){
	if (quando=="hoje"){
		hoje = new Date();	
		var dia = hoje.getDate();
		var mes = hoje.getMonth()+1;
		var ano = hoje.getFullYear();
	}
	var tudo = "<select name='diaf' id='diaf'>";
	for(var d=1; d<32; d++){
		tudo += "<option value='"+d+"'";
		if (dia==d) tudo += " selected";
		tudo += ">"+d+"</option>";
	} tudo += "</select>";
	tudo += "<select name='mesf' id='mesf'>";
	for(var m=1; m<13; m++){
		tudo += "<option value='"+m+"'";
		if (mes==m) tudo += " selected";
		tudo += ">"+m+"</option>";
	} tudo += "</select>";
	tudo += "<select name='anof' id='anof'>";
	for(var a=1990; a<2021; a++){
		tudo += "<option value='"+a+"'";
		if (ano==a) tudo += " selected";
		tudo += ">"+a+"</option>";
	} tudo += "</select>";

	return tudo;
}

//////////////////////////////////////////////////////////////////////////////////


/// BLOCO


//////////////////////////////////////////////////////////////////////////////////

function BlocoAbas(aba,modulo){
	document.getElementById('formmb_aba'+modulo+'-1').style.display='none';
	document.getElementById('formmb_aba'+modulo+'-2').style.display='none';
	document.getElementById('formmb_aba'+modulo+'-3').style.display='none';
	document.getElementById('formmb_aba'+modulo+'-'+aba).style.display='block';
	document.getElementById('formmb_abatit'+modulo+'-1').className='abatit';
	document.getElementById('formmb_abatit'+modulo+'-2').className='abatit';
	document.getElementById('formmb_abatit'+modulo+'-3').className='abatit';
	document.getElementById('formmb_abatit'+modulo+'-'+aba).className='abatitesc';
	
}
function BlocoAjax(c){
	setTimeout('MostraVar("bloco.php?a=imagens&c='+c+'","formmb_aba'+c+'-1")',100);
	setTimeout('MostraVar("bloco.php?a=arquivos&c='+c+'","formmb_aba'+c+'-2")',1000);
}
function Bloco(acao,c,x,uri){ // c = qual bloco(id) // x = qual imagem/arq.
	if (acao=="excluirarquivo" && confirm("tem certeza que deseja exluir este arquivo?")){
		MostraVar("bloco.php?a=excluirarquivo&x="+x+"&c="+c,"formmb_aba"+c+"-2");
	}
	if (acao=="excluirimagem" && confirm("tem certeza que deseja exluir esta imagem?")){
		MostraVar("bloco.php?a=excluirimagem&x="+x+"&c="+c,"formmb_aba"+c+"-1");
	}
	if (acao=="imagem"){
		document.getElementById("place_muda_bloco").style.display="none";
		MostraVar("bloco.php?a=altimagem&u="+uri+"&x="+x+"&c="+c,"formmb_aba"+c+"-1");	
	}
	if (acao=="arquivo"){
		document.getElementById("place_muda_bloco").style.display="none";
		MostraVar("bloco.php?a=altarquivo&u="+uri+"&x="+x+"&c="+c,"formmb_aba"+c+"-2");	
	}
	if (acao=="novaimagem"){
		document.getElementById("place_muda_bloco").style.display="none";
		MostraVar("bloco.php?a=novaimagem&u="+uri+"&c="+c,"formmb_aba"+c+"-1");	
	}
	if (acao=="novoarquivo"){
		document.getElementById("place_muda_bloco").style.display="none";
		MostraVar("bloco.php?a=novoarquivo&u="+uri+"&c="+c,"formmb_aba"+c+"-2");	
	}
}

//////////////////////////////////////////////////////////////////////////////////


/// EXCLUIR


//////////////////////////////////////////////////////////////////////////////////


function Excluir(caso,unidade,msg){
	if (confirm("Deseja excluir "+msg)){
		document.location.href = "alteracaobd.php?acao="+caso+"&unidade="+unidade;
	}
}


//////////////////////////////////////////////////////////////////////////////////
//
//
/// AJAX / NOVO CONTEUDO
//
//
//////////////////////////////////////////////////////////////////////////////////

function NovoConteudo(uri){
    if (window.XMLHttpRequest) req = new XMLHttpRequest();
	else if (window.ActiveXObject) req = new ActiveXObject("Microsoft.XMLHTTP");
	
    req.onreadystatechange= function() { 
        if (req.readyState==4) if (req.status==200){
			document.getElementById("barra_escolha").style.display = "block";
			document.getElementById("barra_escolha").innerHTML = req.responseText;
		}
		if (req.readyState == 1) { //carregando
			document.getElementById("barra_escolha").style.display = "block";
			document.getElementById("barra_escolha").innerHTML = "<span id='carregando'>carregando</span>";
		}
    }
    req.open("GET",uri,true);
	
    if (window.XMLHttpRequest) req.send(null);
	else if (window.ActiveXObject) req.send();
}

function Barra2Fechar(){
	document.getElementById("barra_escolha").innerHTML = "";
}


////////////////////////////////////////////////////////////////////////////// total
function NovoConteudoT(uri){
    if (window.XMLHttpRequest) req = new XMLHttpRequest();
	else if (window.ActiveXObject) req = new ActiveXObject("Microsoft.XMLHTTP");
	
    req.onreadystatechange= function() { 
        if (req.readyState==4) if (req.status==200){
			document.getElementById("total").style.display = "block";
			document.getElementById("total").innerHTML = req.responseText;
		}
		if (req.readyState == 1) { //carregando
			document.getElementById("total").style.display = "block";
			document.getElementById("total").innerHTML = "<span id='carregando'>carregando</span>";
		}
    }
    req.open("GET",uri,true);
	
    if (window.XMLHttpRequest) req.send(null);
	else if (window.ActiveXObject) req.send();
}
//////////////////////////////////////////////////////////////////////////////// grande
function NovoConteudoG(uri){
    if (window.XMLHttpRequest) req = new XMLHttpRequest();
	else if (window.ActiveXObject) req = new ActiveXObject("Microsoft.XMLHTTP");

    req.onreadystatechange= function() { 
        if (req.readyState==4) if (req.status==200){
			document.getElementById("form_grande").style.display = "block";
			document.getElementById("form_grande").innerHTML = req.responseText;
		}
		if (req.readyState == 1) { //carregando
			document.getElementById("form_grande").style.display = "block";
			document.getElementById("form_grande").innerHTML = "<span id='carregando'>carregando</span>";
		}
    }
    req.open("GET",uri,true);

    if (window.XMLHttpRequest) req.send(null);
	else if (window.ActiveXObject) req.send();
}
//////////////////////////////////////////////////////////////////////////////// peq
function NovoConteudoP(uri){
    if (window.XMLHttpRequest) req = new XMLHttpRequest();
	else if (window.ActiveXObject) req = new ActiveXObject("Microsoft.XMLHTTP");
	
    req.onreadystatechange= function() { 
        if (req.readyState==4) if (req.status==200){
			document.getElementById("form_peq").style.display = "block";
			document.getElementById("form_peq").innerHTML = req.responseText;
		}
		if (req.readyState == 1) { //carregando
			document.getElementById("form_peq").style.display = "block";
			document.getElementById("form_peq").innerHTML = "<span id='carregando'>carregando</span>";
		}
    }
    req.open("GET",uri,true);
	
    if (window.XMLHttpRequest) req.send(null);
	else if (window.ActiveXObject) req.send();
}
//////////////////////////////////////////////////////////////////////////////// bloco (img-arq)
function NovoConteudoB(uri){
    if (window.XMLHttpRequest) req = new XMLHttpRequest();
	else if (window.ActiveXObject) req = new ActiveXObject("Microsoft.XMLHTTP");
	
    req.onreadystatechange = function() { 
        if (req.readyState == 4) 
			if (req.status == 200){
				document.getElementById("place_muda_bloco").style.display = "block";
				document.getElementById("place_muda_bloco").innerHTML = req.responseText;
			}
		if (req.readyState == 1) { //carregando
			document.getElementById("place_muda_bloco").style.display = "block";
			document.getElementById("place_muda_bloco").innerHTML = "<span id='carregando'>carregando</span>";
		}
    }
    req.open("GET",uri,true);
	
    if (window.XMLHttpRequest) req.send(null);
	else if (window.ActiveXObject) req.send();
}

////////////////////////////////////////////////////////////////////////////// variavel
function MostraVar(uri, elem){
    if (window.XMLHttpRequest) reqvar = new XMLHttpRequest();
	else if (window.ActiveXObject) reqvar = new ActiveXObject("Microsoft.XMLHTTP");
	
    reqvar.onreadystatechange= function() { 
        if (reqvar.readyState==4) {
			if (reqvar.status==200) {
				document.getElementById(elem).innerHTML = reqvar.responseText;
			} else {//if (reqvar.status == 404){
				document.getElementById(elem).innerHTML = "erro: "+reqvar.statusText;
			}
		} else if (reqvar.readyState == 1) { //carregando
			document.getElementById(elem).innerHTML = "<span id='carregando' style='width:260px;height:110px;top:0px'>carregando</span>";
		}
    }
	
    reqvar.open("GET",uri,true);
	
    if (window.XMLHttpRequest) reqvar.send(null);
	else if (window.ActiveXObject) reqvar.send();
}

//////////////////////////////////////////////////////////////////////////////////
//
//
/// VALIDAÇAO
//
//
//////////////////////////////////////////////////////////////////////////////////
function ValidaNovo(modulo){
	if (document.formnovo.nome.value==""){
		alert("Preencha o nome do item");
		document.formnovo.nome.focus();
		return false
	}
	if (modulo == "L"){
		if (document.formnovo.arquivo.value==""){
			alert("Escolha um arquivo");
			document.formnovo.arquivo.focus()
			return false
		}
	}
}
function checaUpload(form){
	if (form.valor.value == ""){
		alert ("Escolha um arquivo");
		return false;
	}
}
function checaUsuario(nome,senha1,senha2){
	if (nome == ""){
		alert("Preencha o nome do usuario");
		return false
	}
	if (senha != senha2){
		alert("A senha não coincide com a confirmacao.");
	}
		return false
}
