﻿function iniciar() {
	var r = new Restrict("frmContato");
    r.field.codigo = "\\d.";
    r.mask.codigo = "###.###.###.####";
    
    r.field.email = "\\w-.@";

	r.onKeyRefuse = function(o, k){
        o.style.backgroundColor = "#fdc";
    }
    r.onKeyAccept = function(o, k){
        if(k > 30)
            o.style.backgroundColor = "#FFF";
    }
    r.start();

	var f = document.forms[0];
	addEvent( f, 'submit', function() { return enviar() } )
	
	var inputs = document.getElementsByTagName("input");
	for(var i in inputs) {
 		if(inputs[i].type == "text") {
			addEvent( inputs[i], 'blur', function() { this.style.backgroundColor = "#FFF"; } );
			addEvent( inputs[i], 'keyup', function() { tabAutom(this) } );
		}
	}
}

function enviar() {
	var erros = "";
	with(document.forms[0]) {
		if (nome.value == "") {
			erros += " - Você não digitou o seu NOME \n";
		}
	
		var eml = email.value;
		if (email.value == "") {
			erros += " - Você não digitou o E-MAIL para resposta \n";
		} else if (eml.length != 0) {
			if (eml.indexOf("@") == -1) {
				if (eml.indexOf(".") == -1) {
					erros += " - E-MAIL inválido ou digitado incorretamente \n";
				}
			}
		}

		if (assunto.value == "") {
			erros += " - Você não digitou o ASSUNTO da mensagem \n";
		}

		if (mensagem.value == "") {
			erros += " - Você não digitou o texto da MENSAGEM \n";
		}

		if (erros != "") {
			alert("Para enviar, corrija o(s) erro(s) abaixo: \n\n" + erros + "\n");
			return false;
		}
	}
}

addEvent(window, 'load', iniciar);