// JavaScript Document



//************************************************************************
// HTTP REQUEST **********************************************************
//************************************************************************
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}
var http = getHTTPObject();
//************************************************************************
// HTTP REQUEST **********************************************************
//************************************************************************



function thumbs_images()
{
	document.getElementById("preload").style.visibility = 'visible';
	http.open("GET", "thumbs.php", true);
  	http.onreadystatechange = resposta;
  	http.send(null);
}
function resposta()
{
	if (http.readyState == 4) {
	document.getElementById("preload").style.visibility = 'hidden';
	document.getElementById("thumbs").innerHTML = '';
	document.getElementById("thumbs").innerHTML = http.responseText;
}
}



//************************************************************************
//FUNÇÃO PARA ALTERAÇÃO DE PÁGINAS *******************************************
//******************************************************************************
var v; // Página
var mur_id; // ID do Mural
var var_alt; // Variável Alt




function func(valor)
{
 v = valor;
 http.open("GET", "page.php?valor="+valor, true);
  http.onreadystatechange = handleHttpResponse;
  http.send(null);
}

function handleHttpResponse()
{
	if (http.readyState == 4) {
	document.getElementById("Conteudo_Meio").innerHTML = http.responseText;

// CRIAR BOTÕES
	if(v == 'mural'){
	document.getElementById("botao_criar").innerHTML = '<input name="Button" type="button" class="botao_criar"  onclick="cria_mural()" value="Criar Mural"/>';	
	}

	if(v == 'mensagens'){
	document.getElementById("prev_msg").innerHTML = '<b><span class="pequeno">Selecione uma <br>Mensagem ao lado.</span></b>';
	document.getElementById("bot_permitir").innerHTML = '<input name="Submit2" type="button" class="botao_criar" value="Permitir"   onclick="permitir_msg()"/>';
	document.getElementById("bot_excluir").innerHTML = '<input name="Submit" type="button" class="botao_alt" value="Excluir" onclick="excluir_msg(\'A\')"/>';
	}



	}
}



//************************************************************************

//////// M U R A L *******************************************************

//************************************************************************



//************************************************************************
//FUNÇÃO PARA CRIAR MURAL*************************************************
//************************************************************************


function cria_mural()
{
http.open("POST", "classes/cria_mural.php?mur_nome="+document.cad.mur_nome.value+"&mur_subt="+document.cad.mur_subt.value+"&mur_data="+document.cad.mur_data.value+"&mur_publicidade="+document.cad.mur_publicidade.value+"&mur_desc="+document.cad.mur_desc.value+"&mur_pasta="+document.cad.mur_pasta.value+"&mur_template="+document.cad.mur_template.value, true);
 
 http.onreadystatechange = resul_mural;
 http.send(null);
}
//RESULTADO
function resul_mural()
{
	  	if (http.readyState == 4) {
	  	
		results = http.responseText.split(",");

		if(results[1] != ""){ // SE DER ERRO
	  	alert(results[1]);
		}else{
		
		v = ' "'+document.cad.mur_nome.value+'" ';
		document.cad.mur_nome.value = '';
		document.cad.mur_subt.value = '';
		document.cad.mur_data.value = '';
		document.cad.mur_pasta.value = '';
		document.cad.mur_desc.value = '';
	  	alert('O Mural '+v+' foi criado com sucesso !');
		consulta_lista();
		}
		
		}
}


//************************************************************************
// FUNÇÃO PARA CONSULTAR NA LISTA
//************************************************************************

function consulta_lista()
{
http.open("POST", "classes/consulta_lista.php", true);
 
 http.onreadystatechange = consul_lista;
 http.send(null);
}

function consul_lista()
{
  campo_select = document.cad.lista;
  if (http.readyState == 4) {
    campo_select.options.length = 0;
    results = http.responseText.split(",");
   
	for( i = 0; i < results.length; i++ )
    { 
     document.getElementById("total_reg").innerHTML = i;
	 string = results[i].split( "|" );
     campo_select.options[i] = new Option( string[0], string[1] );
}

  }
}


//******************************************************************************
// FUNÇÃO EXCLUIR MURAL
//******************************************************************************

function excluir_mural(v)
{

if(document.cad.lista.value ==''){
	alert('  Você precisa SELECIONAR um mural para EXCLUIR   ');
	}else{
if (confirm (" Deseja mesmo excluir este Mural  ? \n\n   - Serão excluídas, caso haja, todas fotos e mensagens deste mural\n")){
	
http.open("POST", "classes/excluir_mural.php?mur_id="+document.cad.lista.value+"&v="+v, true);
 http.onreadystatechange = exclui_mural;
 http.send(null);
}
	}
}



//RESULTADO
function exclui_mural()
{
	  	if (http.readyState == 4) {
	  	alert(http.responseText);
		
		if(var_alt == 'yes'){
		document.cad.mur_nome.value = '';
		document.cad.mur_subt.value = '';
		document.cad.mur_data.value = '';
		document.cad.mur_pasta.value = '';
		document.cad.mur_pasta.disabled = "";
		document.cad.mur_desc.value = '';
     	document.cad.mur_template.options[0].selected="selected";
			document.getElementById("botao_criar").innerHTML = '<input name="Button" type="button" class="botao_criar"  onclick="cria_mural()" value="Criar Mural"/>';	
	document.getElementById("botao_cancel").innerHTML = '';	
		}
		document.cad.mur_pasta.disabled = "";
		consulta_lista();
		}
}


//******************************************************************************
// FUNÇÃO ALTERAR MURAL
//******************************************************************************


function consulta_mural()
{
if(document.cad.lista.value ==''){
	alert('  Você precisa SELECIONAR um mural para EDITAR   ');
	}else{
http.open("POST", "classes/consulta_mural.php?mur_id="+document.cad.lista.value, true);
 http.onreadystatechange = cons_mural;
 http.send(null);
	}
}
//RESULTADO
function cons_mural()
{
	  	if (http.readyState == 4) {
	  	
		results = http.responseText.split(","); // Separar pela vírgula
		
		mur_id = results[0];
		var_alt = 'yes';
		document.cad.mur_nome.value = results[1];
		document.cad.mur_subt.value = results[2];
		document.cad.mur_data.value = results[3];
		document.cad.mur_pasta.value = results[4];
		document.cad.mur_desc.value = results[5];
		document.cad.mur_pasta.disabled = "disabled";
		document.cad.mur_template.options.value = results[6];
		document.cad.mur_publicidade.value = results[7];

		document.getElementById("botao_criar").innerHTML = '<input name="Button" type="button" class="botao_alt"  onclick="alterar_mural()" value="Alterar Mural"/>';	
	document.getElementById("botao_cancel").innerHTML = '<input name="Button" type="button" class="botao_criar"  onclick="cancelar_alt()" value="Cancelar"/>';	
		}
}



//******************************************************************************
// FUNÇÃO ALTERAR MURAL
//******************************************************************************

function alterar_mural()
{

http.open("POST", "classes/alterar_mural.php?mur_id="+mur_id+"&mur_nome="+document.cad.mur_nome.value+"&mur_subt="+document.cad.mur_subt.value+"&mur_data="+document.cad.mur_data.value+"&mur_desc="+document.cad.mur_desc.value+"&mur_template="+document.cad.mur_template.value+"&mur_publicidade="+document.cad.mur_publicidade.value, true);
 http.onreadystatechange = alt_mural;
 http.send(null);
}
//RESULTADO
function alt_mural()
{
	  	if (http.readyState == 4) {
	  	alert(http.responseText);
		mur_id = '';

		document.cad.mur_nome.value = '';
		document.cad.mur_subt.value = '';
		document.cad.mur_data.value = '';
		document.cad.mur_pasta.value = '';
		document.cad.mur_pasta.disabled = "";
		document.cad.mur_desc.value = '';
		document.cad.mur_publicidade.value = '';
		document.cad.mur_template.options[0].selected="selected";

			document.getElementById("botao_criar").innerHTML = '<input name="Button" type="button" class="botao_criar"  onclick="cria_mural()" value="Criar Mural"/>';	
	document.getElementById("botao_cancel").innerHTML = '';	
		consulta_lista();
}
}


//******************************************************************************
// CANCELAR ALTERAÇÃO
//******************************************************************************
function cancelar_alt(){
		
		mur_id = '';
		document.cad.mur_nome.value = '';
		document.cad.mur_subt.value = '';
		document.cad.mur_data.value = '';
		document.cad.mur_pasta.value = '';
		document.cad.mur_pasta.disabled = "";
		document.cad.mur_publicidade.value = '';
		document.cad.mur_template.options[0].selected="selected";
		
		document.cad.mur_desc.value = '';
			document.getElementById("botao_criar").innerHTML = '<input name="Button" type="button" class="botao_criar"  onclick="cria_mural()" value="Criar Mural"/>';	
	document.getElementById("botao_cancel").innerHTML = '';	

}










//************************************************************************

//////// F O T O S ******************************************************

//************************************************************************



//************************************************************************
//FUNÇÃO PARA ADICIONAR FOTO *********************************************
//************************************************************************


function add_foto()
{
http.open("POST", "classes/add_foto.php?foto_foto="+document.cad.foto_foto.value+"&mur_id="+document.cad.murais.value, true);
 
 http.onreadystatechange = resul_foto;
 http.send(null);
}
//RESULTADO
function resul_foto()
{
	  	if (http.readyState == 4) {
	  	
		results = http.responseText.split(",");

		if(results[1] != ""){ // SE DER ERRO
	  	alert(results[1]);
		}else{
		
 		document.getElementById("add_fot").innerHTML = '<input name="foto_foto" type="file"  class="form_mural_menu" id="foto_foto" size="20"/>';
	  	alert('A Foto foi adicionada com sucesso !');
		
		//consulta_lista();
		}
		
		}
}



//************************************************************************
// FUNÇÃO PARA CONSULTAR FOTO NA LISTA -----------------------------------
//************************************************************************

function consulta_foto()
{
http.open("POST", "classes/consulta_fotos.php?mur_id="+document.cad.murais.value, true);
 
http.onreadystatechange = consul_foto;
http.send(null);
}

function consul_foto()
{
  campo_select = document.cad.lista;
  if (http.readyState == 4) {
    campo_select.options.length = 0;
    results = http.responseText.split(",");
   	results.reverse();
   
	for( i = 0; i < results.length; i++ )
    { 
     document.getElementById("total_reg").innerHTML = i;
	 string = results[i].split( "|" );
     campo_select.options[i] = new Option( string[0], string[1] );
}

  }
}





//******************************************************************************
// FUNÇÃO EXCLUIR FOTOS
//******************************************************************************

function excluir_foto()
{

if(document.cad.lista.value ==''){
	alert('  Você precisa SELECIONAR uma foto para EXCLUIR   ');
	}else{
if (confirm (" Deseja mesmo excluir esta Foto  ? \n\n   - Serão excluídas também, caso haja, todas as mensagens desta foto\n")){
	
http.open("POST", "classes/excluir_foto.php?foto_id="+document.cad.lista.value+"&mur_id="+document.cad.murais.value, true);
 http.onreadystatechange = exclui_foto;
 http.send(null);
}
	}
}

//RESULTADO
function exclui_foto()
{
	  	if (http.readyState == 4) {
	  	alert(http.responseText);
		consulta_foto();
		document.getElementById("prev_img").innerHTML = '';

		}
}







//************************************************************************

//////// M E N S A G E N S ***********************************************

//************************************************************************




//************************************************************************
// FUNÇÃO PARA CONSULTAR MSG E JOGAR NA LISTA
//************************************************************************
/*
function consulta_msg()
{
http.open("POST", "classes/consulta_msg.php?msg_id="+document.cad.lista.value, true);
 
 http.onreadystatechange = consul_msg;
 http.send(null);
}

function consul_msg()
{

	if (http.readyState == 4) {
    results = http.responseText.split("|");
	foto_thumb = results[0].split(".");
	
	
	document.getElementById("prev_img").innerHTML = '<img src="galerias/'+results[4]+'/'+foto_thumb[0]+'_thumb.jpg" width="80" height="60" hspace="3" vspace="3" border="2"  style="border-color:#FFFFFF;"/><br /><span class="pequeno">asdasdasda</span>';
//'+foto_thumb[0]+'.jpg --> nome da imagem

	document.getElementById("prev_msg").innerHTML = '<strong><span class="pequeno">De:</span><span class="style8"><br /><strong>'+results[1]+'</strong></span><br /><span class="pequeno"><br />Mensagem:</span><br /></strong><span class="style8">'+results[2]+'</span>';
	
	}

}
*/



//************************************************************************
// FUNÇÃO PARA CONSULTAR MSG E EXIBIR
//************************************************************************

function consulta_msg()
{
if(document.cad.lista.value == ''){
alert("Não existe nenhuma mensagem nesta seleção."); }else{
http.open("POST", "classes/consulta_msg.php?msg_id="+document.cad.lista.value, true);
 http.onreadystatechange = consul_msg;
 http.send(null);
}
}

function consul_msg()
{

	if (http.readyState == 4) {
    results = http.responseText.split("|");
	foto_thumb = results[0].split(".");
	
	
	document.getElementById("prev_img").innerHTML = '<img src="galerias/'+results[4]+'/'+foto_thumb[0]+'_thumb.jpg" width="80" height="60" hspace="3" vspace="3" border="2"  style="border-color:#FFFFFF;"/><br /><span class="pequeno">'+results[4]+'</span>';

	document.getElementById("prev_msg").innerHTML = '<strong><span class="pequeno">De:</span><span class="style8"><br /><strong>'+results[1]+'</strong></span><br /><span class="pequeno"><br />Mensagem:</span><br /></strong><span class="style8">'+results[2]+'</span>';
	
	}

}


//************************************************************************
// FUNÇÃO PARA CONSULTAR FOTOS NA LISTA
//************************************************************************

function consulta_fotos_lista()
{
http.open("POST", "classes/consulta_fotos.php?mur_id="+document.cad.murais.value, true);
 
 http.onreadystatechange = consul_foto_lis;
 http.send(null);
}

function consul_foto_lis()
{
 
  campo_select = document.cad.fotos;
  if (http.readyState == 4) {
    campo_select.options.length = 0;
    results = http.responseText.split(",");
   
	for( i = 0; i < results.length; i++ )
    { 
     document.getElementById("tot_fotos").innerHTML = i;
	 string = results[i].split( "|" );
     campo_select.options[i] = new Option( string[0], string[1] );
	 document.getElementById("bot_permitir").innerHTML = '';
	 document.getElementById("bot_excluir").innerHTML = '';
	 document.getElementById("prev_img").innerHTML = '';
	 document.getElementById("prev_msg").innerHTML = '';
	 document.getElementById("ver_tudo").innerHTML = '<img src="image/voltar.gif" alt="VER RECENTES" width="20" height="22" border="0" style="cursor: pointer;"/>';
	}
	
   	// Apagar lista ------------------------------------------
    lista = document.cad.lista.options.length = 0;
       
	for( i = 0; i < lista; i++ )
    { 
    document.cad.lista.options[i] = null;
	}
	//Apagar imagem

	// ----------------------------------------------------------
  }
}


//************************************************************************
// FUNÇÃO PARA CONSULTAR MSG NA LISTA
//************************************************************************

function consulta_msg_lista()
{
http.open("POST", "classes/consulta_msg_lista.php?foto_id="+document.cad.fotos.value, true);
http.onreadystatechange = consul_msg_lis;
http.send(null);
}

function consul_msg_lis()
{

  campo_select = document.cad.lista;
  if (http.readyState == 4) {
    campo_select.options.length = 0;
    results = http.responseText.split(",");

	// Se existir valores adicione o botão excluir.
	if(results[0] != ''){
	document.getElementById("bot_excluir").innerHTML = '<input name="Submit" type="button" class="botao_alt" value="Excluir" onclick="excluir_msg(\'B\')"/>';
	}

for( i = 0; i < results.length; i++ )
    { 
     document.getElementById("total_reg").innerHTML = i;
	 string = results[i].split( "?" );
     campo_select.options[i] = new Option( string[0], string[1] );
}



  }

}




//************************************************************************
// FUNÇÃO PARA VER TODAS AS MENSAGENS
//************************************************************************

function consulta_msg_todas()
{
http.open("POST", "classes/ver_todas_msg.php", true);
http.onreadystatechange = consul_msg_tod;
http.send(null);
}

function consulta_msg_todas2()
{
http.open("POST", "classes/ver_todas_msg2.php", true);
http.onreadystatechange = consul_msg_tod2;
http.send(null);
}

function consul_msg_tod()
{

  campo_select = document.cad.lista;
  
  if (http.readyState == 4) {


	
	campo_select.options.length = 0;

	results_msgs = http.responseText.split(",");


	// Se existir valores adicione o botão excluir.
	if(results[0] == ''){
	alert('Não existe mensagens novas !');	
	}
	if(results[0] != ''){
	document.getElementById("prev_msg").innerHTML = '<b><span class="pequeno">Selecione uma <br>Mensagem ao lado.</span></b>';
	
	
	
	document.getElementById("bot_permitir").innerHTML = '<input name="Submit2" type="button" class="botao_criar" value="Permitir"  onclick="permitir_msg()"/>';
	document.getElementById("bot_excluir").innerHTML = '<input name="Submit" type="button" class="botao_alt" value="Excluir" onclick="excluir_msg(\'A\')"/>';
	document.getElementById("ver_tudo").innerHTML = '';
	document.getElementById("tot_fotos").innerHTML = '';
	document.getElementById("prev_img").innerHTML = '';

	}


if(results_msgs.length > 0){


campo_select.options[0] = new Option( 'Teste', 'Valor' );
for( i = 0; i < results_msgs.length; i++ )
	{   
	 document.getElementById("total_reg").innerHTML = i;
	 string = results_msgs[i].split( "?" );
     campo_select.options[i] = new Option( string[0], string[1] ); 
	}
	
}



// Apagar lista da Fotos ------------------------------------------
   lista = document.cad.fotos.options.length = 0;
	for( i = 0; i < lista; i++ )
    { 
    document.cad.fotos.options[i] = null;
	}
//-----------------------------------------------------------------

  }

}






//************************************************************************
// FUNÇÃO PARA EXCLUIR MENSAGENS
//************************************************************************

function excluir_msg(V)
{
if (document.cad.lista.value ==''){
alert('Por favor, selecione uma mensagem para excluir'); }else{

if (confirm (" Deseja mesmo excluir esta Mensagem ?")){
http.open("POST", "classes/excluir_msg.php?msg_id="+document.cad.lista.value+"&v="+V, true);
http.onreadystatechange = excluir_msg_;
http.send(null);
}

}
}

function excluir_msg2(V)
{
if (document.cad.lista.value ==''){
alert('Por favor, selecione uma mensagem para excluir'); }else{

if (confirm (" Deseja mesmo excluir esta Mensagem ?")){
http.open("POST", "classes/excluir_msg2.php?msg_id="+document.cad.lista.value+"&v="+V, true);
http.onreadystatechange = excluir_msg_2;
http.send(null);
}

}
}


function excluir_msg_()
{
  if (http.readyState == 4) {
	// Se existir valores adicione o botão excluir.
	if(results[0] != ''){
	document.getElementById("bot_excluir").innerHTML = '<input name="Submit" type="button" class="botao_alt" value="Excluir" onclick="excluir_msg()"/>';
	}
	
	alert(http.responseText);
	document.getElementById("total_reg").innerHTML = '';
	if(document.cad.murais.value == ''){
	consulta_msg_todas();	
	}else{
	consulta_msg_lista();
	}
	document.getElementById("prev_msg").innerHTML = '';
	document.getElementById("prev_img").innerHTML = '';


  }

}


function excluir_msg_2()
{
  if (http.readyState == 4) {
	// Se existir valores adicione o botão excluir.
	if(results[0] != ''){
	document.getElementById("bot_excluir").innerHTML = '<input name="Submit" type="button" class="botao_alt" value="Excluir" onclick="excluir_msg2()"/>';
	}
	
	alert(http.responseText);
	document.getElementById("total_reg").innerHTML = '';
	if(document.cad.murais.value == ''){
	consulta_msg_todas2();	
	}else{
	consulta_msg_lista();
	}
	document.getElementById("prev_msg").innerHTML = '';
	document.getElementById("prev_img").innerHTML = '';


  }

}



//************************************************************************
// FUNÇÃO PARA PERMITIR MENSAGENS
//************************************************************************

function permitir_msg()
{
if (document.cad.lista.value ==''){
alert('Por favor, selecione uma mensagem para dar permissão'); 

}else{

http.open("POST", "classes/permitir_msg.php?msg_id="+document.cad.lista.value, true);
http.onreadystatechange = permitir_msg_;
http.send(null);
}

}

function permitir_msg_()
{
  if (http.readyState == 4) {
	// Se existir valores adicione o botão excluir.
	
	
	alert(http.responseText);

	document.getElementById("prev_msg").innerHTML = '';
	document.getElementById("prev_img").innerHTML = '';
	
	document.cad.lista.focus();
	
	consulta_msg_todas();
	
	
  }

}



//************************************************************************
// FUNÇÃO SESSÕES
//************************************************************************

function criar_sessoes()
{
upload.document.add.mur_pasta.value = document.cad.murais.value;
upload.document.add.marca_dagua.value = document.carimbo.marca_dagua.value;
upload.document.add.transparencia.value = document.carimbo.transparencia.value;
upload.document.add.posicao.value = document.carimbo.posicao.value;


/*http.open("POST", "classes/criar_sessoes.php?posicao="+document.carimbo.posicao.value+"&transparencia="+document.carimbo.transparencia.value+"&marca_dagua="+document.carimbo.marca_dagua.value+"&mur_pasta="+document.cad.murais.value, true);
http.onreadystatechange = criar_sess;
http.send(null);*/
}

/*function criar_sess()
{
  if (http.readyState == 4) {
	// Se existir valores adicione o botão excluir.
	}

}*/





//************************************************************************
// FUNÇÕES GERAIS
//************************************************************************



// PREVIEW DO THUMB DA IMAGEM
function ver_img()
{
	/*foto = document.cad.lista.value.split("?");
	t = foto[1].split(".");
	alert(t[1]);*/
	
	if(document.cad.lista.value == ''){
	alert('É necessário selecionar uma foto');
	}else{
	pasta = document.cad.murais.value.split("?");
	foto = document.cad.lista.value.split("?");
	foto_thumb = foto[1].split(".");
	
document.getElementById("prev_img").innerHTML = '<img src="galerias/'+pasta[1]+'/'+foto_thumb[0]+'_thumb.jpg" width="80" height="60" hspace="3" vspace="3" border="2"  style="border-color:#FFFFFF;"/ alt="'+pasta[1]+'/'+foto_thumb[0]+'.jpg"><br />';
	}

}


function abrir(id, template, largura, altura) {
  var url;
  url = "templates/"+template+"/index.php?mur_id="+id;

  var esquerda = (screen.width - largura)/2;
  var topo = (screen.height - altura)/2;
  window.open(url,'win','height=' + altura + ', width=' + largura + ', top=' + topo + ', left=' + esquerda); 
}


function page(url, largura, altura) {
  var url;
  
  var esquerda = (screen.width - largura)/2;
  var topo = (screen.height - altura)/2;
  window.open(url,'win','height=' + altura + ', width=' + largura + ', top=' + topo + ', left=' + esquerda+',toolbar=no, statusbar=no, location=no, scrollbars=1'); 
}

function page_less(url, largura, altura) {
  var url;
  
  var esquerda = (screen.width - largura)/2;
  var topo = (screen.height - altura)/2;
  window.open(url,'win','height=' + altura + ', width=' + largura + ', top=' + topo + ', left=' + esquerda); 
}

function iframe() {
	document.getElementById("murais_thumbs").src = 'thumbs_central.php?mur_nome='+document.	getElementById("mur_nome_id").value+'&mur_pasta='+document.getElementById("mur_pasta").value;
	
	
}
	