$(document).ready(function()
{
	$('img').error(function(){
		$(this).attr('src', 'semfoto_ico.jpg');
	});
	
	$('div#BOX_ERRO, div#BOX_ALERTA, div#BOX_OK').animate({opacity: 1.0}, 5000)
		.fadeOut('slow');
});

$('input.TOGGLE').focus(function()
{
	if ($(this).val() == $(this).attr('title'))
		$(this).val(null);
});

$('input.TOGGLE').blur(function()
{
	if ($(this).val() === '')
		$(this).val($(this).attr('title'));
});

/*************************************************/
//	MOSTRAR ALERTA NA TELA
function ShwAlert(MSG, TIPO)
{
	var WIDTH	= 400;
	var ID		= 'BOX_'+TIPO;
	
	// REMOVER SE JÁ TIVER UM ALERTA ABERTO
	$('#COVER').remove();

	$('<div id="COVER"></div>')
		.prependTo('body')
		.html('<span id="'+ ID +'">'+MSG+'</span>');

	var LEFT 	= ($('div#COVER').width() / 2)	- 200;
	var TOP  	= ($('div#COVER').height() / 2)	- 100;

	$('span#'+ID)
		.css({
			'position':'fixed',
			'width': WIDTH,
			'left': LEFT,
			'top': TOP
		});
	
	remAlert();
}

function remAlert()
{
	$('div#COVER')
		.fadeIn('slow')
		.animate({opacity: 1.0}, 3000)
		.fadeOut('slow', function() {
//			$(this).remove();
		 })
		.click(function()
		{
			$(this).remove();
		});
}
/* ***************************************************************************** */
//	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;
	}
}

//	VALIDAR RADIO GROUP
function valRadios(NM_FORM, NOME)
{
	var RET = null;
	
	$('form#'+NM_FORM+' input[name='+NOME+']:radio').each(function()
	{
		if ( $(this).is(':checked') )
			RET = $(this).val();
	});
	
	return RET;
}

//	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;
	$('form#'+NM_FORM+' :input').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')
		{
			if($(this).attr('type') == 'radio')
			{
				VAL = valRadios(NM_FORM, NOME);
			}
			else if($(this).attr('type') == 'checkbox')
			{
				VAL = ( $(this).is(':checked') ) ?  $(this).val() : null;
			}

			if(VAL==='' || VAL===null || VAL==' ' || VAL=='0' || VAL===false || VAL=='Digite seu nome' || VAL=='Digite seu email')
			{
				switch ($(this).attr('type')) {
					case 'select-one'	: 
					case 'radio'		: 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, 'ERRO');
				$(this).focus();
				return false;
			}
		}
		

		REL 	= $(this).attr('rel');
		if (REL)
		{
			//CASO REL SEJA STRING
			if (isNaN(REL))
			{
				if(REL != 'shadowbox')
				{
					var SAME = $('#'+REL).attr('value');
					if(VAL != SAME)
					{
						MSG = 'Os campos '+REL+' e '+NID+' não conferem!';
						ShwAlert(MSG, 'ERRO');
						$(this).focus();
						return false;
					}
				}
			}
			else
			{
				if (VAL.length < REL)
				{
					MSG = 'O campo '+NID+' deve conter no mínimo '+REL+' caracteres';
					ShwAlert(MSG, 'ERRO');
					$(this).focus();
					return false;
				}
			}
		}
	});

	if(!MSG)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//	VALIDAÇÃO SIMPLES, APENAS ANALISA OS CAMPOS OBRIGATÓRIOS
function SendForm(NM_FORM)
{
	
	if(chkFields(NM_FORM) === true)
	{
		return true;
	}
	else
	{
		return false;
	}
}
