
$(document).ready(function() {
	$("#contato_telefone").mask("(99) 9999-9999");
});


function $E(id)
{
	if(document.getElementById(id))
		return document.getElementById(id);
	else 
		alert('Elemento ' + id + ' n&atilde;o definido.');	
}


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 validarContato(form)
{
	var mensagem    = "";
	var first_field = "";
	var valid       = true;
	var idsErro     = new Array();
	
	
	if($('#contato_nome').val() == '')
	{
		id = $('#contato_nome').attr('id');
		valid = false;
		if(first_field == "") 
			first_field = id;
		
		idsErro.push(id);
		
		
	}


	if($('#contato_email').val() == "" || !checkMail($('#contato_email').val()))
	{
		id = $('#contato_email').attr('id');
		valid = false;
		
		if(first_field == "") 
			first_field = id;	
		
		idsErro.push(id);
	}


	if($('#contato_telefone').val() == "" )
	{
		id = $('#contato_telefone').attr('id');
		valid = false;
		
		if(first_field == "") 
			first_field = id;	
		
		idsErro.push(id);
	}
	
	

	if($('#contato_mensagem').val() == '')
	{
		id = $('#contato_mensagem').attr('id');
		valid = false;
		if(first_field == "") 
			first_field = "contato_mensagem";
		
		idsErro.push(id);
	}
	
	
	if(valid) { 
		$('#contato_nome').attr('class', 'input');
		$('#contato_email').attr('class', 'input');
		$('#contato_telefone').attr('class', 'input');
		$('#contato_mensagem').attr('class', 'textarea');
		return true;
	}
	else {
		if(mensagem != '')
			$('.txt-mensagem').html(mensagem);
		
		
		for(var y = 0; y < $E('formulario').length; ++y)
		{
			$('#' + $E('formulario').elements[y].id).attr('class', 'input');	
			
			for(var x = 0; x < idsErro.length; ++x)
			{
				if($E('formulario').elements[y].id ==  idsErro[x])
				{
					if($E('formulario').elements[y].type == 'text')
						$('#' + idsErro[x]).attr('class', 'input-alerta');
					else if($E('formulario').elements[y].type == 'textarea')
						$('#' + idsErro[x]).attr('class', 'textarea-alerta');	
				}
			}
		}
		
		
		$('#alerta-ins').show('normal');
		$('#sucesso-ins').hide();
		
		$E(first_field).focus();
		first_field = "";
		idsErro	 	= new Array();
		return false;
	}
	
}

