//	JavaScript Document
/*************************************************/
//	MOSTRAR ALERTA NA TELA
function ShwAlert(MSG, OBJ, TIPO)
{
	var WIDTH	= 400;
/*	var LEFT 	= ((screen.width) / 2) - 200;
	var TOP  	= ((screen.height) / 2) - 160;*/
	var ID		= (TIPO == 'ERRO') ? 'ERRO' : 'ALERTA';
	
	// REMOVER SE JÁ TIVER UM ALERTA ABERTO
	$('#COVER').remove();

	$('<div id="COVER"></div>')
		.prependTo('body')
		.html('<span id="'+ ID +'">'+MSG+'</span>');
//		.click(function () { 
//			$(this).remove(); 
//	    })

	var LEFT 	= ($('div#COVER').width() / 2)	- 200;
	var TOP  	= ($('div#COVER').height() / 2)	- 160;

	$('span#'+ID)
		.css({
			'position':'fixed',
			'width': WIDTH,
			'left': LEFT,
			'top': TOP
		});
	
	remAlert();
}

function remAlert()
{
	$('div#COVER')
		.fadeIn('slow')
		.animate({opacity: 1.0}, 2500)
		.fadeOut('slow', function() {
			$(this).remove();
		 });
}

/***********************/

function txtCounter(OBJ)
{
	$(OBJ).keydown(function (event)
	{
		var MAX = $(this).attr('maxlength');
		var VAL = $(this).val();
		var numChar = VAL.length;

		if(numChar == MAX)
		{
			ShwAlert('Esse campo permite no máximo '+MAX+' caracteres!', '', 'ERRO');
			var RET = VAL.substring(0, MAX);
			$(this).val(RET);
		}
	});
}


function chgValue (OBJ, VALUE)
{
	if (OBJ.value === '')
	{
		OBJ.value = VALUE;
	}
	else if (OBJ.value == VALUE)
	{
		OBJ.value = '';
	}
}

/* ***************************************************************************** */

//	JS QUE INSERE UM CAMPO tipo COM NOME nome E VALUE obj DENTRO DE doc	
function insField (NM_FORM, TIPO, NOME, OBJ)
{
	var DOC				= document.forms[NM_FORM];
	var newACT			= document.createElement('INPUT');

	if(newACT)
	{
		newACT.type		= TIPO;
		newACT.name 	= NOME;
		newACT.value	= OBJ;

		DOC.appendChild( newACT );
		return true;
	}
	else
	{
		return false;
	}
}

//	JS QUE ANALISA O FORM E VERIFICA SE OS CAMPOS COM title='Obrigatório' ESTÃO PREENCHIDOS	
function chkFields (NM_FORM)
{
	var NOME, ID, NID, TITLE, TIPO, VAL, REL, MSG;

	$(':text, :select, :textarea, :radio, :checkbox, :file, :password', 'form#'+NM_FORM).each( function()
	{
		NOME	= $(this).attr('name');
		ID		= $(this).attr('id');
		NID 	= ID.replace(/_/g, ' ');
		TITLE	= $(this).attr('title');
		VAL		= $(this).val();

		if (TITLE=='Obrigatório' && (VAL==='' || VAL===null || VAL==' ' || VAL=='0' || VAL===false || VAL=='Nome ou Apelido' || VAL=='Digite seu nome' || VAL=='Digite seu email'))
		{
			switch ($(this).attr('type')) {
				case 'select-one'	: MSG = 'Selecione uma opção para '+NID; break;
				case 'checkbox'		: MSG = 'Você precisa ler e aceitar os termos de uso do site'; break;
				default				: MSG = 'Preencha o campo '+NID; break;
			}
			
			ShwAlert(MSG, '#'+ID, 'ERRO');
			$(this).focus();
			return false;
			

		}

		if (NOME=='Login' || NOME=='Senha')
		{
			if(VAL.length < 5)
				MSG = 'O campo '+NOME+' deve conter no mínimo 5 caracteres';
			
			if(NOME == VAL)
				MSG = 'Preencha o campo '+NOME;

			if (MSG)
			{
				ShwAlert(MSG, '#'+ID, 'ERRO');
				$(this).focus();
				return false;
			}
		}
		
		REL 	= $(this).attr('rel');
//alert('NOME: '+NOME+' - ID: '+ID+' - TITLE: '+TITLE+' - VAL: '+VAL+' - REF: '+REL);
		if(REL && REL != 'shadowbox')
		{
			var SAME = $('#'+REL).attr('value');
			if(VAL != SAME)
			{
				MSG = 'Os campos '+REL+' e '+NID+' não conferem!';
				ShwAlert(MSG, '#'+ID, 'ERRO');
				$(this).focus();
				return false;
			}
		}
	});

	if(!MSG)
		return true;
}

//	VALIDAÇÃO SIMPLES, APENAS ANALISA OS CAMPOS OBRIGATÓRIOS
function SendForm(NM_FORM)
{
	if(chkFields(NM_FORM) === true)
	{
		$('form#'+NM_FORM).submit();
	}
}

function ExcForm(NM_FORM)
{
	if(confirm('Deseja realmente excluir esse conteúdo?') === true)
	{
		insField(NM_FORM, 'hidden', 'EXC', 'EXC');
		$('form#'+NM_FORM).submit();
	}
}

// CADASTRO DE USUARIOS DO SITE
function cad_User (NM_FORM, ACT)
{
	ShwAlert('Aguarde ...', 'input#BT_ENVIAR', 'ALERTA');
	
	var DOC		= document.forms[NM_FORM];
	var LIST	= DOC.elements;
	
	if(chkFields (NM_FORM) === true)
	{
		var SEND = 'lg='+DOC.Login.value+'&em='+DOC.Email.value;
	
		$.ajax({
			type		: "POST",
			data		: 'lg='+DOC.Login.value+'&em='+DOC.Email.value,
			url			: "ajx_cad_source.php",
			success		: function(msg)
			{
				if(msg=='ok')
				{
					if(insField (NM_FORM, 'hidden', 'ACT', 'REC'))
					{
						DOC.submit();
					}
					else
					{
						ShwAlert('Ocorreu um erro, tente novamente', 'input#BT_ENVIAR', 'ERRO');
					}
				}
				else
				{
					ShwAlert(msg, 'input#BT_ENVIAR', 'ERRO');
				}
			},
			error		: function(txt)
			{
				ShwAlert(txt, 'input#BT_ENVIAR', 'ERRO');
			}
		});
	}
}


//	CADASTRO DE DENÚNCIAS DE CONTEUDOS
function cad_Den (NM_FORM, TIPO, USR, ID)
{
	if(chkFields (NM_FORM) === false)
	{
		return false;
	}
	
	ajx_SEND_Denuncia (NM_FORM, TIPO, USR, ID);
}