//mudar paginas
function pagina1(){
  document.getElementById('cabecalho1').style.display = "block";
  document.getElementById('cabecalho2').style.display = "none";
}
function pagina2(){
  document.getElementById('cabecalho1').style.display = "none";
  document.getElementById('cabecalho2').style.display = "block";
}

//menusuperior em cascata....
function horizontal() {
 
   var navItems = document.getElementById("menu_dropdown").getElementsByTagName("li");
    
   for (var i=0; i< navItems.length; i++) {
      if(navItems[i].className == "submenu")
      {
         if(navItems[i].getElementsByTagName('ul')[0] != null)
         {
            navItems[i].onmouseover=function() {this.getElementsByTagName('ul')[0].style.display="block";}
            navItems[i].onmouseout=function() {this.getElementsByTagName('ul')[0].style.display="none";}
         }
      }
   }
 
}
//window.onload = horizontal;

//menu suoerior em casacata
var menu=function(){
	var t=15,z=50,s=6,a;
	function dd(n){this.n=n; this.h=[]; this.c=[]}
	dd.prototype.init=function(p,c){
		a=c; var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.st('+i+',true)');
			h.onmouseout=new Function(this.n+'.st('+i+')');
		}
	}
	dd.prototype.st=function(x,f){
		var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
		clearInterval(c.t); c.style.overflow='hidden';
		if(f){
			p.className+=' '+a;
			if(!c.mh){c.style.display='block'; c.style.height=''; c.mh=c.offsetHeight; c.style.height=0}
			if(c.mh==c.offsetHeight){c.style.overflow='visible'}
			else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
		}else{p.className=p.className.replace(a,''); c.t=setInterval(function(){sl(c,-1)},t)}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter=''; c.style.opacity=1; c.style.overflow='visible'}
			clearInterval(c.t); return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
		c.style.opacity=o; c.style.filter='alpha(opacity='+(o*100)+')';
		c.style.height=h+(d*f)+'px'
	}
	return{dd:dd}
}();
//acaba menu superiro

//login e senha
function validar(){
if(form.login.value == "" || form.senha.value == ""){
alert("Favor insira o LOGIN e a SENHA válida grato!");
return false;
}
}

// ir para login
function entrar(){
	  //parent.top.frames['myiframe'].location.href = 'login.php';
	  document.getElementById('myiframe').src = 'login.php';
	  //document.getElementById('myiframe').location.href ='sair.php';
	  }

//deslogar
function sair(){
	///parent.top.frames['myiframe'].location.href = 'sair.php';
	 //window.document.getElementById('myiframe').location.href ='sair.php';
	 if (document.getElementById('myiframe')){
       document.getElementById('myiframe').src = 'sair.php';
	 }else{
		window.location.href ='sair.php';
	 }
	 }

//funcao auto redimesiona iframe
function resizeFrame() {
//f.style.height = f.contentWindow.document.body.scrollHeight + "px";
if(document.getElementById('myiframe')){
var altura = document.getElementById('myiframe').contentWindow.document.body.scrollHeight;
document.getElementById('myiframe').height = altura;
//window.document.getElementById('myiframe').style.height = f.contentWindow.document.body.scrollHeight + "px";
}
}


//funcao auto redimesiona iframe (antiga)
function calcHeight(){
  //find the height of the internal page
  var the_height = window.document.getElementById('myiframe').contentWindow.document.body.scrollHeight;
  
  //change the height of the iframe
  window.document.getElementById('myiframe').height = the_height;
  window.document.getElementById('myiframe').height = the_height;
  //alert(the_height);
}
function calcHeight1(){
  //find the height of the internal page
  var the_height = window.parent.window.document.getElementById('myiframe').contentWindow.document.body.scrollHeight;
  //change the height of the iframe
  window.parent.document.getElementById('myiframe').height = the_height;
  window.parent.document.getElementById('myiframe').height = the_height;
  
  //alert(the_height);
//setTimeout("window.parent.document.getElementById('myiframe').height = the_height;",500);
}


 function showTimer() {
	 
  var time=new Date();
  var hour=time.getHours();
  var minute=time.getMinutes();
  var second=time.getSeconds();
  if(hour<10)   hour  ="0"+hour;
  if(minute<10) minute="0"+minute;
  if(second<10) second="0"+second;
  var st=hour+":"+minute+":"+second;
  if (document.getElementById("horaatual")){
  document.getElementById("horaatual").innerHTML=st; 
  }
  
 }
 function initTimer() {
  // O metodo nativo setInterval executa uma determinada funcao em um determinado tempo  
  setInterval(showTimer,1000);
 }
 
 function dataatual(){
datahora = new Date();
xhora = datahora.getHours();
if (xhora >= 0 && xhora < 12) {txtsaudacao = "Bom Dia!"}
if (xhora >= 12 && xhora < 18) {txtsaudacao = "Boa Tarde!"}
if (xhora >= 18 && xhora <= 23) {txtsaudacao = "Boa Noite!"}
xdia = datahora.getDay();
diasemana = new Array(7);
diasemana[0] = "Domingo";
diasemana[1] = "Segunda";
diasemana[2] = "Terça";
diasemana[3] = "Quarta";
diasemana[4] = "Quinta";
diasemana[5] = "Sexta";
diasemana[6] = "Sábado";
dia = datahora.getDate();
mes = datahora.getMonth();
mesdoano = new Array(12);
mesdoano[0] = "janeiro";
mesdoano[1] = "fevereiro";
mesdoano[2] = "março";
mesdoano[3] = "abril";
mesdoano[4] = "maio";
mesdoano[5] = "junho";
mesdoano[6] = "julho";
mesdoano[7] = "agosto";
mesdoano[8] = "setembro";
mesdoano[9] = "outubro";
mesdoano[10] = "novembro";
mesdoano[11] = "dezembro";
ano = datahora.getFullYear();

if(document.getElementById('dataatual')){
document.getElementById('dataatual').innerHTML = txtsaudacao + " " + diasemana[xdia] + ", " + dia + " de " + mesdoano[mes] + " de " + ano;
}
}


//carrega pagina na div CarregaPagina("test.html","divid")
function CarregaPagina(pagina,local)
{

var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState== 4 && xmlhttp.status==200)
    {
    document.getElementById(local).innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET",pagina,true);
xmlhttp.send();
//setTimeout("resizeFrame(window.parent.window.document.getElementById('myiframe'));",1000);
}

//funcao pra retoran url
function urlDecode(string, overwrite){
        if(!string || !string.length){
                return {};
        }
        var obj = {};
        var pairs = string.split('&');
        var pair, name, value;
        var lsRegExp = /\+/g;
        for(var i = 0, len = pairs.length; i < len; i++){
                pair = pairs[i].split('=');
                name = unescape(pair[0]);
                value = unescape(pair[1]).replace(lsRegExp, " ");
                //value = decodeURIComponent(pair[1]).replace(lsRegExp, " ");
                if(overwrite !== true){
                        if(typeof obj[name] == "undefined"){
                                obj[name] = value;
                        }else if(typeof obj[name] == "string"){
                                obj[name] = [obj[name]];
                                obj[name].push(value);
                        }else{
                                obj[name].push(value);
                        }
                }else{
                        obj[name] = value;
                }
        }
        return obj;
}


function PegaUrl(param){
        var wl = window.location.href;
        var params = urlDecode(wl.substring(wl.indexOf("?")+1));
        return(params[param]);
}


//funcoa muda modelo em ajaxa
function MontarModelo(pagina)
{

var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState== 4 && xmlhttp.status==200)
    {
    //document.getElementById(local).innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET",pagina,true);
xmlhttp.send();
}


function AbreConteudo(conteudo,loja){
$.ajaxSetup ({
// Disable caching of AJAX responses
cache: false
});

var conteudo = conteudo;
$('#conteudo').load(conteudo);

}


//redimensiona iframe a cada 1 seg
function resize_iframe()
{

	var height1=window.innerWidth;//Firefox
	if (document.body.clientHeight)
	{
		height1=document.body.clientHeight;//IE
	}
	//resize the iframe according to the size of the
	//window (all these should be on the same line)
	document.getElementById("myiframe").style.height=parseInt(height1-document.getElementById("myiframe").offsetTop-8)+"px";
	//alert (document.getElementById("myiframe").offsetTop);
}



//ajusta conteudo on timer
var c=0;
var t;
var timer_is_on=0;



function timedCount()
{
c=c+1;

t=setTimeout("timedCount()",1000);
//calcHeight();
//alert("aaa");

//resize_iframe();

if(window.parent.document.getElementById('myiframe')){
var alturaif = window.parent.document.getElementById('myiframe').contentWindow.document.body.scrollHeight;
window.parent.document.getElementById('myiframe').height = alturaif;
//resizeFrame(window.parent.document.getElementById('myiframe'));
}
}
  
 function redimensionaframe(){
if (!timer_is_on)
  {
  timer_is_on=1;
  timedCount();

  }
}
//funcao para monta pagina em cache
function MontaPG(loja,parte,arquivo){
	var codigo = document.getElementById(parte).innerHTML;
	$.post("indgera.php", { loja:loja, parte: parte, arquivo: arquivo, codigo: codigo } );
	
}
//ajustar conteudo a tela
function AjustarConteudo(){
	
var footer = parseInt(document.getElementById("footer").clientHeight);
var header = parseInt(document.getElementById("header").clientHeight);
var wrap = parseInt(document.getElementById("wrap").clientHeight);

document.getElementById("cont").style.height = wrap - header - footer ;
document.getElementById("conteudo").style.height = (wrap - header - footer-2) + "px" ;	
}


function CarregaFrame(pagina){

$.ajaxSetup ({
// Disable caching of AJAX responses
cache: false
});

	if(document.getElementById("myiframe")){
		document.getElementById('myiframe').src = ""+pagina;	
	}else{
		$("#conteudo2").load("index_frame.php");
	}
	pagina2();
}


// funcoes para ocultar paineies
//coluna_esquerda
//coluna_diteita
function OcultaColuna(coluna){
	$('body').ready(function() {
//executa funcao
document.getElementById(coluna).style.display = "none";

});

}
function MostraColuna(coluna){
	$('body').ready(function() {
//executa funcao
document.getElementById(coluna).style.display = "";

});

}

//funcao ativar u desativar usa assim

// ativar("id","tabela",campo_modificado);
function ativar(valor,tabela,campo){

$.post("/admin/servicos/ativar.php", {
valor: valor,
tabela: tabela,
campo: campo
 });

}

//funcao para excluir registro ajax
function excluir(tabela,codigo){
	if (confirm("Deseja realmente excluir?")){
	$.post("/admin/servicos/excluir.php", {
	codigo: valor,
	tabela: tabela });
	window.location.reload()
	}
}

function ajustafundo(){
	 document.getElementById("cabecalho").style.marginTop = document.getElementById("editortema").offsetHeight+ "px";
	 document.getElementById("index_fundo").style.marginTop = document.getElementById("editortema").offsetHeight+ "px";
	//alert(document.getElementById("editortema").offsetHeight+ "px");
	//alert(document.getElementById("editortema").offsetHeight);
}


//##Funcao dependent list box ## //
function Dependent(mestre,escravo,tabela,texto,dependencia,ordem,padrao,pagina,alternativo){
 $.ajaxSetup ({
// Disable caching of AJAX responses
cache: false
});

function LimpaCampo(Campo){
	var selectList = document.getElementById(Campo);
selectList.options.length = 0;
  
}
//fucnao que adiciona item slecionado
function AddItem(Campo,Text,Value)
    {
        // Create an Option object        
        var opt = document.createElement("option");
        // Add an Option object to Drop Down/List Box
        document.getElementById(Campo).options.add(opt);

        // Assign text and value to Option object
        opt.text = Text;
        opt.value = Value;
		opt.selected = true;
		
    }

var mestrevalor = $("#"+escravo+"").val();
var coddependencia = $("#"+mestre+"").val();

	if($("#"+alternativo).val()){
		var valorantigo = $("#"+alternativo).val();
	}else{
		var valorantigo = $("#"+escravo).val();
	}

//zera list box
LimpaCampo(escravo);
AddItem(escravo,padrao,'0');
jQuery(function($){
	
    $.getJSON('/admin/servicos/dependentlistbox'+pagina+'.php?&tabela='+tabela+"&texto="+texto+"&ordem="+ordem+"&coddependencia="+coddependencia+"&dependencia="+dependencia+"&codpai="+coddependencia, function(data) {
		var select = $('#'+escravo+'');
        $.each(data, function(key, val){
            $('<option/>').attr('value', val.id)
                  .html(val.campo)
                  .appendTo(select);
				 
        });
		//determina valor padrao
		document.getElementById(escravo).value = valorantigo;
		
    });
});
}

function DependentListbox(mestre,escravo,tabela,texto,dependencia,ordem,padrao,pagina,alternativo){

//atribui evento onload 
$('document').ready(function() {
//executa funcao
Dependent(mestre,escravo,tabela,texto,dependencia,ordem,padrao,pagina,alternativo);

$("#"+mestre).change(function() {
//executa funcao
Dependent(mestre,escravo,tabela,texto,dependencia,ordem,padrao,pagina,alternativo)
});

});

}
//## Acaba Funcao dependent list box ##// 

//## funcoa depente 

//funcao ajusta cabelho e demais itens
 function ajustaalturaitem(item){
	
document.getElementById(item).style.height = document.getElementById(item).scrollHeight+"px";

}
//funcao ajusta cabelho e demais itens

//funcoa ajusta  frame editor
function ajustapropriedade(frame){
  //find the height of the internal page<script language="javascript" type="text/javascript">

  var the_height = window.parent.window.document.getElementById(frame).contentWindow.document.body.scrollHeight;

  //change the height of the iframe
  window.parent.document.getElementById(frame).height = the_height;
  window.parent.document.getElementById(frame).height = the_height;
  //alert(the_height);
}
