var xmlhttp,xmlhttp2,xmlhttpH;

var loader = '<div align="center" style="background-color:#000;width:100%;height:auto;margin:auto;padding:auto"><img src="backoffice/images/loading3.gif" vspace="40" align="center"/></div>';

var loader2 = '<div align="center" style="background-color:#fff;width:100%;height:auto;margin:auto;padding:auto"><img src="backoffice/images/loader.gif" vspace="40" align="center"/></div>';

var loader3 = '<img src="backoffice/images/loader.gif"/>';
//var loader2 = '<div align="center" style="background-image:URL(backoffice/images/loader.gif);background-repeat:no-repeat;background-position:center;width:100%;height:200px"></div>';

var Pload;

var pag, div, div2, obj, lista_body_aux, aux_seq, submenu;

var tamanhotext = 2;



function atribui_XMLHttpRequest()

{

	if (window.XMLHttpRequest) { 

    	xmlhttp = new XMLHttpRequest(); 

   	} else if (window.ActiveXObject) { 

     	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 

   	} else { 

       	alert("Seu navegador n&atilde;o suporta XMLHttpRequest."); 

    	return; 

   	}		

}

/*---------------------------------*/
function atribui_XMLHttpRequest2()

{

	if (window.XMLHttpRequest) { 

    	xmlhttp2 = new XMLHttpRequest(); 

   	} else if (window.ActiveXObject) { 

     	xmlhttp2 = new ActiveXObject("Microsoft.XMLHTTP"); 

   	} else { 

       	alert("Seu navegador n&atilde;o suporta XMLHttpRequest."); 

    	return; 

   	}		

}

/*---------------------------------*/

function fecha_loading()

{

	document.getElementById(div).innerHTML = '';	

}



/*---------------------------------*/



function loading()

{		

	if(Pload == undefined || Pload == 1) 
	{
		document.getElementById(div).innerHTML = loader;
	}else
	{
		if(Pload == 2) document.getElementById(div).innerHTML = loader2;
			else document.getElementById(div).innerHTML = loader3;
	}

}



/*---------------------------------*/

function openShadowbox(content, player, title){	

	Shadowbox.open({

        content:	content,

        player:     player,

        title:      title

    });

}

/*---------------------------------*/

function openShadowboxWH(content, player, title, w, h){

    Shadowbox.open({

        content:	content,

        player:     player,

        title:      title,

		width:      w,

		height:      h

    });

}

/*---------------------------------*/

function openShadowboxgallery(content, player, title, galeria){

    Shadowbox.open({

        content:	content,

        player:     player,

        title:      title,

		gallery: galeria

    });

}

/*---------------------------------*/



function logar()

{

	atribui_XMLHttpRequest();	

	pag = 'logar.php?email='+document.getElementById("email").value+'&senha='+document.getElementById("senha").value;

	xmlhttp.open("GET", pag, true); 	

    xmlhttp.onreadystatechange = processlogar; 

    xmlhttp.send(null); 

}

/*---------------------------------*/



function processlogar() 

{ 

     if (xmlhttp.readyState == 1) 

	 {          

		 //loading(1);

	 }

	if (xmlhttp.readyState == 4) 

	{ 

       if (xmlhttp.status == 200) 

	   {            		   		   

		   if(xmlhttp.responseText == '1')

		   {

			   //document.getElementById("barralogout").style.display = 'none';

			   //document.getElementById("login").style.display = 'none';

			  // Effect.Fade('barralogout');

			  // Effect.Fade('login');

			  // Effect.Appear('barralogin');

			  window.location = 'index.php';

		   }else

		   {

				alert("E-mail e/ou senha incorretos");   

		   }

		   //document.getElementById(div).style.display = 'none';

		   //Effect.Appear(div);

		   //Effect.Appear(div, { duration: 10.0 });

		   //document.getElementById(div).innerHTML = xmlhttp.responseText;			  

		   

       }else 

	   { 

	  		alert('Problemas ao carregar a p&aacute;gina, tente mais tarde.');

			//fecha_loading();

       } 

    } 

}


/*---------------------------------*/



function lembra_senha()

{

	atribui_XMLHttpRequest();	

	pag = 'lembrar_senha.php?email='+document.getElementById("email2").value;

	xmlhttp.open("GET", pag, true); 	

    xmlhttp.onreadystatechange = processlembra_senha; 

    xmlhttp.send(null); 

}

/*---------------------------------*/



function processlembra_senha() 

{ 

     if (xmlhttp.readyState == 1) 

	 {          

		 //loading(1);

	 }

	if (xmlhttp.readyState == 4) 

	{ 

       if (xmlhttp.status == 200) 

	   {            		   		   

		   if(xmlhttp.responseText == '1')

		   {

			  document.getElementById('divesqueci').style.display='none';
			  document.getElementById('divlogin').style.display='';
			  alert("A senha foi enviada para o e-mail indicado.");

		   }else

		   {

				alert("E-mail incorreto.");   

		   }
		   		  		   
       }else 

	   { 

	  		alert('Problemas ao carregar a p&aacute;gina, tente mais tarde.');

			//fecha_loading();

       } 

    } 

}

/*---------------------------------*/



function carrega(divi,pagina,obj)

{

	Pload = obj;

	atribui_XMLHttpRequest();	

	div = divi;

	xmlhttp.open("GET", pagina, true); 	

    xmlhttp.onreadystatechange = processcarrega; 

    xmlhttp.send(null); 

}

/*---------------------------------*/



function processcarrega() 

{ 

     if (xmlhttp.readyState == 1) 

	 {          

		 loading();

	 }

	if (xmlhttp.readyState == 4) 

	{ 

       if (xmlhttp.status == 200) 

	   {            		   		   

		   //document.getElementById(div).style.display = 'none';

		   //Effect.Appear(div);

		   //Effect.Appear(div, { duration: 10.0 });

		   document.getElementById(div).innerHTML = xmlhttp.responseText;			  

       }else 

	   { 

	  		alert('Problemas ao carregar a p&aacute;gina, tente mais tarde.');

			fecha_loading();

       } 

    } 

}



/*---------------------------------*/

function carrega2(divi,pagina,obj)

{

	Pload = obj;

	atribui_XMLHttpRequest2();	

	div2 = divi;

	xmlhttp2.open("GET", pagina, true); 	

    xmlhttp2.onreadystatechange = processcarrega2; 

    xmlhttp2.send(null); 

}

/*---------------------------------*/



function processcarrega2() 

{ 

     if (xmlhttp2.readyState == 1) 

	 {          

		 loading();

	 }

	if (xmlhttp2.readyState == 4) 

	{ 

       if (xmlhttp2.status == 200) 

	   {            		   		   

		   //document.getElementById(div).style.display = 'none';

		   //Effect.Appear(div);

		   //Effect.Appear(div, { duration: 10.0 });

		   document.getElementById(div2).innerHTML = xmlhttp2.responseText;			  

       }else 

	   { 

	  		alert('Problemas ao carregar a p&aacute;gina, tente mais tarde.');

			fecha_loading();

       } 

    } 

}



/*---------------------------------*/



function mascara(tecla,src,mask) {

    var _TXT = tecla.keyCode; 

	if(_TXT != 8 && _TXT !=46)

	{

	    var i = src.value.length; 

		var saida = mask.substring(0,1); 

		var texto = mask.substring(i);

 		if(texto.substring(0,1) != saida)

		{ 

			src.value += texto.substring(0,1); 

		} 

	}

    return true; 	

}

/*---------------------------------*/



function so_numeros(tecla) {

    var _TXT = tecla.keyCode; 

    if((_TXT > 47 && _TXT < 58) || (_TXT > 34 && _TXT < 41) || (_TXT > 95 && _TXT < 106) || _TXT==46 || _TXT==9) 

	{  		

    	return true; 

	}else 

	{ 

		if(_TXT != 8) return false; 

			else return true; 

    }

}



/*---------------------------------*/



function enviar()

{

	loading(2);

	var form = document.forms[0];

	var i=0;

	for (var x = 0,y = form.length-1; x<y;x++) 

	{    

		form.elements[x].style.backgroundImage = "";

		if (form.elements[x].value == "" && form.elements[x].type != "hidden" && form.elements[x].type != "file" && form.elements[x].type != "checkbox" && form.elements[x].lang != "pt") 

		{    

			i++;				

			form.elements[x].style.backgroundImage = "url(images/alert.png)";

			form.elements[x].style.backgroundRepeat = "no-repeat";

			form.elements[x].style.backgroundPosition = "right top";

		}

	}

	if(i == 0){ 

	form.submit(); }else{escreve_msg('Preencha os campos obrigat&oacute;rios.');}

}

/*---------------------------------*/



function esconde(a)

{				

	document.getElementById(a).style.display = 'none';	

}

/*---------------------------------*/



function mostra(a)

{				

	if(document.getElementById(a).style.display == 'none') document.getElementById(a).style.display = '';

		else document.getElementById(a).style.display = 'none';

}

/*---------------------------------*/



function add_click(tabela,id,divi)

{

	atribui_XMLHttpRequest();	

	pag = 'add_click.php?tabela='+tabela+'&id='+id;

	div = divi;

	xmlhttp.open("GET", pag, true); 	

    xmlhttp.onreadystatechange = processadd_click; 

    xmlhttp.send(null); 

}

/*---------------------------------*/



function processadd_click() 

{ 

     if (xmlhttp.readyState == 1) 

	 {          

		 //loading(1);

	 }

	if (xmlhttp.readyState == 4) 

	{ 

       if (xmlhttp.status == 200) 

	   {            		   		   

		   document.getElementById(div).style.display = 'none';

		   Effect.Appear(div);

		   //Effect.Appear(div, { duration: 10.0 });

		   document.getElementById(div).innerHTML = xmlhttp.responseText;	

       }else 

	   { 

	  		alert('Problemas ao carregar a p&aacute;gina, tente mais tarde.');

			fecha_loading();

       } 

    } 

}



/*---------------------------------*/



function fontAdjust(operador){

	if(operador == 'mais' && tamanhotext<3)

	{

		tamanhotext++;

	}else{

		if(operador == 'menos' && tamanhotext>1)tamanhotext--;

	}

	if(tamanhotext == 3) document.getElementById('text').style.fontSize = "14px";

	if(tamanhotext == 2) document.getElementById('text').style.fontSize = "";

	if(tamanhotext == 1) document.getElementById('text').style.fontSize = "11px";

}

/*---------------------------------*/



function atualiza_select_todos(id_select,tabela,condicao,ordem,campo_mostra,campo_value,valor_seleciona)

{				

	atribui_XMLHttpRequest();

	pag = "gera_select_todos.php?id_select="+id_select+"&tabela="+tabela+"&condicao="+condicao+"&ordem="+ordem+"&campo_mostra="+campo_mostra+"&campo_value="+campo_value+"&valor_seleciona="+valor_seleciona;

	xmlhttp.open("GET", pag, true); 	

    xmlhttp.onreadystatechange = processatualiza_select; 

    xmlhttp.send(null); 

}

/*---------------------------------*/



function atualiza_select(id_select,tabela,condicao,ordem,campo_mostra,campo_value,valor_seleciona)

{				

	atribui_XMLHttpRequest();

	pag = "gera_select.php?id_select="+id_select+"&tabela="+tabela+"&condicao="+condicao+"&ordem="+ordem+"&campo_mostra="+campo_mostra+"&campo_value="+campo_value+"&valor_seleciona="+valor_seleciona;

	xmlhttp.open("GET", pag, true); 	

    xmlhttp.onreadystatechange = processatualiza_select; 

    xmlhttp.send(null); 

}

/*---------------------------------*/



function processatualiza_select() 

{ 

     if (xmlhttp.readyState == 1) 

	 {          

		 //loading(2);

	 }

	if (xmlhttp.readyState == 4) 

	{ 

       if (xmlhttp.status == 200) 

	   {            

			//alert(xmlhttp.responseText);

			document.getElementById("selectnovo").innerHTML = xmlhttp.responseText;

			//fecha_loading();

      }else 

	  { 

	  		alert(xmlhttp.status);

			//escreve_msg('Problemas ao carregar a p&aacute;gina, tente mais tarde.');

      } 

    } 

}

/*---------------------------------*/



function select_busca(valor)

{

	//atualiza_select(id_select,tabela,condicao,ordem,campo_mostra,campo_value,valor_seleciona)

	if(valor == 'materias') 

	{

		atualiza_select('id_categoria','materias_categorias',"status='s'",'titulo','titulo','id','');

		document.getElementById("formbusca").action = 'busca.php';

	}else

	{

		document.getElementById("selectnovo").innerHTML = '';

		document.getElementById("formbusca").action = 'fotosVideos.php';

	}

}

/*---------------------------------*/



function coloca_focus()

{

	document.getElementById("senha").focus();	

}

function muda_input(objeto,a)

{

	if(a=='1')

	{

		if(objeto.value=='Senha:')

		{

			document.getElementById("mudainput").innerHTML = '<input name="senha" type="password" id="senha" tabindex="1" value="" style="width:130px;" onfocus="muda_input(this,\'1\')" onblur="muda_input(this,\'2\')" />';

			window.setTimeout(coloca_focus, 100);

		}

	}else

	{

		if(objeto.value=='')document.getElementById("mudainput").innerHTML = '<input name="senha" type="type" id="senha" tabindex="1" value="Senha:" style="width:130px;" onfocus="muda_input(this,\'1\')" onblur="muda_input(this,\'2\')" />';

	}

}

function muda_input_esp(objeto,a)

{

	if(a=='1')

	{
		var aux = objeto.value.split("r");
		if(aux[0]=='Cont')

		{

			document.getElementById("mudainput").innerHTML = '<input name="senha" type="password" id="senha" tabindex="1" value="" style="width:130px;" onfocus="muda_input_esp(this,\'1\')" onblur="muda_input_esp(this,\'2\')" />';

			window.setTimeout(coloca_focus, 100);

		}

	}else

	{

		if(objeto.value=='')document.getElementById("mudainput").innerHTML = '<input name="senha" type="type" id="senha" tabindex="1" value="'+unescape("Contrase&ntilde;a")+':" style="width:130px;" onfocus="muda_input_esp(this,\'1\')" onblur="muda_input_esp(this,\'2\')" />';

	}

}

/*---------------------------------*/

function envia_contato()

{

	if(document.getElementById('formcontato').nome.value != '')

	{

		if(document.getElementById('formcontato').email.value != '')

		{

			if(document.getElementById('formcontato').ddd.value != '')

			{

				if(document.getElementById('formcontato').telefone.value != '')

				{

					if(document.getElementById('formcontato').mensagem.value != '')

					{

						document.getElementById('formcontato').submit();	

					}else

					{

						alert('Preencha o campo Mensagem');	

					}

				}else

				{

					alert('Preencha o campo Telefone');	

				}

			}else

			{

				alert('Preencha o campo DDD');	

			}

		}else

		{

			alert('Preencha o campo E-mail');	

		}

	}else

	{

		alert('Preencha o campo Nome');	

	}

}



/*---------------------------------*/



function enviar_cadastra(formenvia)

{

	var form = document.getElementById(formenvia);

	var i=0;

	for (var x = 0,y = form.length-1; x<y;x++) 

	{    

		if (form.elements[x].value == "" && form.elements[x].type != "hidden" && form.elements[x].type != "file" && form.elements[x].type != "checkbox" && form.elements[x].lang != "pt") 

		{    

			i++;				

		}

	}

	if(i == 0)

	{ 

		if(document.getElementById(formenvia).senha.value == document.getElementById(formenvia).senha2.value)

		{

			if(checkMail(document.getElementById(formenvia).email.value))

			{

				form.submit();

			}else

			{

				alert('Formato de e-mail errado.');

			}

		}else

		{

			alert('Confirme sua senha.');

		}

	}else{alert('Preencha os campos obrigatorios.');}

}



/*---------------------------------*/

function salva(tabela,id)

{
	alert('Salvo com sucesso.');
	atribui_XMLHttpRequest();
	pag = "salva.php?id="+id+"&tabela="+tabela;
	xmlhttp.open("GET", pag, true); 	
    xmlhttp.onreadystatechange = processsalva; 
    xmlhttp.send(null); 
}

/*---------------------------------*/
function processsalva() 
{ 
     if (xmlhttp.readyState == 1) 
	 {   
		 div = 'divloading';		 
		 Pload = 3;
		 loading();
	 }
	if (xmlhttp.readyState == 4) 
	{ 
       if (xmlhttp.status == 200) 
	   {            
			if(xmlhttp.responseText == '1')
			{

			}else
			{
				alert("Erro ao salvar, tente mais tarde por favor.");	
			}
			fecha_loading();
      }else 
	  { 
		alert("Erro ao salvar, tente mais tarde por favor.");	
		fecha_loading();
	  } 
    } 
}

/*---------------------------------*/

function exclui(tabela,id)

{
	//alert('tabela: '+tabela+' id: '+id);
	if(confirm("Deseja excluir realmente?"))
	{
		atribui_XMLHttpRequest();
		pag = "exclui.php?id="+id+"&tabela="+tabela;
		xmlhttp.open("GET", pag, true); 	
		xmlhttp.onreadystatechange = processexclui; 
		xmlhttp.send(null); 
	}
}
/*---------------------------------*/
function seleciona(valor,elemento)
{
	var sel = document.getElementById(elemento);
	var i;
	for(i = 0; i < sel.options.length; i++)
	{
		if(sel.options[i].value == valor) sel.selectedIndex = i;		
	}
}
/*---------------------------------*/
function processexclui() 
{ 
     if (xmlhttp.readyState == 1) 
	 {   
		/* div = 'divloading';		 
		 Pload = 3;
		 loading();*/
	 }
	if (xmlhttp.readyState == 4) 
	{ 
       if (xmlhttp.status == 200) 
	   {            
			//fecha_loading();
			if(xmlhttp.responseText == '1')
			{
				/*carrega('galeriamain','fotoElegendaVip.php?pos=0',2);
				carrega2('galeriafotos','foto_navega_vip.php?posstart=0',2);				*/
				auxtree = 1;
				refreshpasta(ultimoid);				
			}else
			{
				if(xmlhttp.responseText == '2')
				{
					carrega('galeriavideos','video_navega_vip.php?start=0',1);
				}else
				{
					var aux = xmlhttp.responseText.split("-");
					if(aux[0] == '3')
					{
						div2 = 'materia'+aux[1];
						esconde(div2);
					}else
					{
						if(aux[0] == '4')
						{
							div2 = 'lavagem'+aux[1];
							esconde(div2);
						}else
						{
							alert("Erro ao excluir, tente mais tarde por favor."+xmlhttp.responseText);	
						}
					}
				}
			}			
      }else 
	  { 
		alert("Erro ao excluir, tente mais tarde por favor.");	
		//fecha_loading();
	  } 
    } 
}

/*---------------------------------*/

function imprimir(pag,id)

{	
	document.getElementById("iprint").src = 'print_'+pag+'.php?id='+id;
}

/*---------------------------------*/

function imprimir_foto(foto,legenda)

{
	document.getElementById("iprint").src = 'print_foto.php?foto='+foto+'&legenda='+legenda;
}

/*---------------------------------*/

function checkMail(mail)

{        

	var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);        

	if(typeof(mail) == "string")

	{                

		if(er.test(mail))

		{ 

			return true; 

		}        

	}else if(typeof(mail) == "object")

	{                

		if(er.test(mail.value))

		{                                         

			return true;                                 

		}        

	}else

	{                

		return false;                

	}

}

/*---------------------------------*/

function getPageSize()
{
	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)
	{ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else 
	{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) 
	{	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) 
	{ // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) 
	{ // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight)
	{
		pageHeight = windowHeight;
	} else 
	{
		pageHeight = yScroll;
	}
	return pageHeight;
}
/*---------------------------------*/
function maxchar(maximo,texto,divmax,e)
{
	var whichCode = e.keyCode;
    if(whichCode==8 || whichCode==46 || (whichCode>=37 && whichCode<=40))
	{
		if((whichCode==8 || whichCode==46) && texto.length>0)document.getElementById(divmax).innerHTML = maximo-texto.length+1;
			else document.getElementById(divmax).innerHTML = maximo-texto.length;
		return true;
	}else
	{
		if(texto.length < maximo)
		{
			document.getElementById(divmax).innerHTML = maximo-texto.length-1;
			return true;
		}else
		{
			return false;
		}
	}
}