window.onload = function() {
    var frm = document.getElementsByName('faleconosco')[0];
    frm.onsubmit = validate;

    frm.dia.onkeyup = getMaskNumeric;
    frm.dia.onblur = getMaskNumeric;

    frm.mes.onkeyup = getMaskNumeric;
    frm.mes.onblur = getMaskNumeric;

    frm.ano.onkeyup = getMaskNumeric;
    frm.ano.onblur = getMaskNumeric;
};

function validate() {
    var obj = null;
    var msg = '';
    
    if (this.nome.value == '') {
	obj = obj == null ? this.nome : obj;
	msg += "O 'nome' é obrigatório!\n";
    }
    
    if (this.email.value == '') {
	obj = obj == null ? this.email : obj;
	msg += "O 'e-mail' é obrigatório!\n";
    } else if (!isEmail(this.email.value)) {
	obj = obj == null ? this.email : obj;
	msg += "O 'e-mail' é inválido!\n";
    }
    
    if (this.dia.value == '') {
	obj = obj == null ? this.dia : obj;
	msg += "O 'dia' é obrigatório!\n";
    }
    
    if (this.mes.value == '') {
	obj = obj == null ? this.mes : obj;
	msg += "O 'mês' é obrigatório!\n";
    }
    
    if (this.ano.value == '') {
	obj = obj == null ? this.ano : obj;
	msg += "O 'ano' é obrigatório!\n";
    }
    
    var guestsName = document.getElementsByName('convidado_nome[]');
    var guestsEmail = document.getElementsByName('convidado_email[]');
    if (guestsName.length > 0){
	var nGuests = guestsName.length;
	for (var i = 0; i < nGuests; ++i){
	    var guestName = guestsName[i];
	    var guestEmail = guestsEmail[i];
	    if (guestName.value == '') {
		obj = obj == null ? guestName : obj;
		msg += "O 'nome' do "+(i+1)+"º convidado é obrigatório!\n";
	    }
	    
	    if (guestEmail.value == '') {
		obj = obj == null ? guestEmail : obj;
		msg += "O 'e-mail' do "+(i+1)+"º convidado é obrigatório!\n";
	    } else if (!isEmail(guestEmail.value)) {
		obj = obj == null ? guestEmail : obj;
		msg += "O 'e-mail' do "+(i+1)+"º convidado é inválido!\n";
	    }
	}
    } else {
	msg += "Insira ao 01 convidado!\n";
    }
    
    if (msg == ''){
	return true;
    } else {
	alert(msg);
	if (obj)
	    obj.focus();
	return false;
    }
}

function addGuest(name, email){
    var container = $('.submit');
    
    name = name == null ? '' : name;
    email = email == null ? '' : email;
    
    var tr = $(document.createElement('tr'));
    
    var td = $(document.createElement('td'));
    td.attr('valign', 'middle');
    td.addClass('hometopic2');
    td.append(document.createTextNode("Nome:"));
    td.append($(document.createElement('input')).attr('type', 'text').attr('name', 'convidado_nome[]').attr('maxlength', 100).attr('value', name));
    tr.append(td);
    
    var td = $(document.createElement('td'));
    td.attr('valign', 'middle');
    td.addClass('hometopic2');
    td.append(document.createTextNode("Email:"));
    td.append($(document.createElement('input')).attr('type', 'text').attr('name', 'convidado_email[]').attr('maxlength', 200).attr('value', email));
    tr.append(td);
    
    var td = $(document.createElement('td'));
    td.attr('valign', 'bottom');
    td.addClass('hometopic2');
    td.append($(document.createElement('a')).attr('href', 'javascript:void(0)').attr('title', 'remover convidado').addClass('btn').click(remGuest).append(document.createTextNode("remover")));
    tr.append(td);
    
    container.before(tr);
}

function remGuest(){
    $(this).parent().parent().remove();
}

function getMaskNumeric() {
    this.value = maskNumeric(this.value);
}
function maskNumeric(string) {
    string = string.replace(/\D/g, "");
    return string;
}
function isEmail(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" && er.test(mail))
	return true;
    else if (typeof (mail) == "object" && er.test(mail.value))
	return true;
    else
	return false;
}
