// Catarinense Spa
// Acídio Alan
// Produzido por A2C - Internet para Negócios
// www.a2c.com.br

function removeSpaces(string) {
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}

function addFriend() {
	var nome = document.getElementById("nome_amigo");
	var email = document.getElementById("email_amigo");
	var lista_nomes = document.getElementById("lista_nomes");
	var lista_emails = document.getElementById("lista_emails");
	var quantidade = document.getElementById("quantidade");
	var lista = document.getElementById("lista");
	
	// Regra para a validação de e-mail.
	var reEmail = /^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;

	if(removeSpaces(nome.value) == "") {
		nome.focus();
		return
	}
	if(email.value == "") {
		email.focus();
		return;
	} else {
		valid_email = reEmail.exec(email.value)
		if(valid_email == null) {
			email.focus();
			return
		}
	}
	if(lista_nomes.value == "") {
		lista_nomes.value = nome.value;
	} else {
		lista_nomes.value = lista_nomes.value+";"+nome.value;
	}
	if(lista_emails.value == "") {
		lista_emails.value = email.value;
	} else {
		lista_emails.value = lista_emails.value+";"+email.value;
	}
	var total = lista_nomes.value.split(";");
	quantidade.innerHTML = total.length;
	
	//Limpa a lista
	lista.innerHTML = "";
	
	//Cria a lista novamente
	for(var x=0;x<total.length;x++) {
		tag = document.createElement("span");
		tag.innerHTML = Number(x+1)+" - "+total[x];
		lista.appendChild(tag);
	}
	nome.value = "";
	email.value = "";
}