var divSummary;

// Limpa e esconde o sumário

var $error = false;

function ClearSummary()
{
	$error = false;
	if (divSummary != null)
	{
		divSummary.style.display = 'none';
		divSummary.innerHTML = '';
	}
}
// Seta as classes nos campos e adiciona a mensagem no sumário
function Highlight(idLabel, idField, msg, msgComp)
{
	$error = true;
	var lbl = document.getElementById(idLabel);
	var field = document.getElementById(idField);
	
	if(lbl == null || field == null)
	{
		alert([idLabel, idField]);
		return;
	}
	
	// Seta as classes de erro nos labels e fields
	lbl.className = 'highlight';
	field.className = 'texto formErro';
	
	// exibe o sumário e as mensagens de erro
	if (divSummary != null)
	{
		divSummary.style.display = '';
		if (msgComp)
			divSummary.innerHTML += msg;
		else
			divSummary.innerHTML += '<p>Preencha o campo <span class="highlight">' + msg + '</span> corretamente.</p>';
	}
}
// Recupera o Defaul do Label e Field
function ClearHighlight(idLabel, idField)
{
	var lbl = document.getElementById(idLabel);
	var field = document.getElementById(idField);
	
	
	if(lbl == null || field == null)
	{
		alert([idLabel, idField]);
		return;
	}
	
	lbl.className = '';
	field.className = 'texto';
}
// novo: valida se o campo foi preenchido e ja marca a label
function ValidateRequired(idLabel, field, msg, msgComp)
{
	if(field == null)
	{
		alert('Field null: ' + idLabel);
		return false;
	}

	field.value = field.value.replace(/^\s+|\s+$/g, "");
	if(field.value.length == 0)
	{
		Highlight(idLabel, field.id, msg, msgComp);
		return false;
	}
	return true;
	
}
function ValidateCPFFormat(idLabel, field)
{
	var success = field.value.search(/^(\d{3}\.){2}\d{3}-\d{2}$/)!=-1;
	
	if(!success)
		Highlight(idLabel, field.id, '<p>Preencha o campo <span class=\"highlight\">CPF</span> corretamente.', true);
	return success;
}

function ValidateEmail(idLabel, field)
{
	var success = field.value.search(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/)>-1;
	if(!success)
		Highlight(idLabel, field.id, '<p>Formato de <span class=\"highlight\">e-mail</span> inválido.', true);
	return success;
}
