function valida() {
	var form1 = document.getElementById("mailfriend");
	var msg = "";
	var erro = "";
	var identifica = "";
	
	document.getElementById("mailfriend").seuNome.style.background="";
	document.getElementById("mailfriend").sendmailto.style.background="";
	document.getElementById("mailfriend").sendmailfrom.style.background="";
	document.getElementById("mailfriend").email2.style.background="";
	document.getElementById("mailfriend").email3.style.background="";
	document.getElementById("mailfriend").email4.style.background="";
	document.getElementById("mailfriend").email5.style.background="";
	document.getElementById("mailfriend").email6.style.background="";
	
	if(form1.seuNome.value.length<=0){
		document.getElementById("mailfriend").seuNome.style.background="#fffdca";
		erro = "Nome";
		msg=1;
	}
	if(!isEmail(document.getElementById("mailfriend").sendmailto)){
		document.getElementById("mailfriend").sendmailto.style.background="#fffdca";
		if(erro.length>0){
			erro = erro + " e E-mail ";
		}else{
			erro = "E-mail ";	
		}
		msg=1;
	}

	if(document.getElementById("mailfriend").sendmailto.value == (document.getElementById("mailfriend").sendmailfrom.value || document.getElementById("mailfriend").email2.value || document.getElementById("mailfriend").email3.value || document.getElementById("mailfriend").email4.value || document.getElementById("mailfriend").email5.value || document.getElementById("mailfriend").email6.value) && document.getElementById("mailfriend").copia.checked){
		document.getElementById("mailfriend").sendmailfrom.style.background="#fffdca";
		msg=1;
	}
	
	if((!isEmail(document.getElementById("mailfriend").sendmailfrom)) && (!isEmail(document.getElementById("mailfriend").email2)) && (!isEmail(document.getElementById("mailfriend").email3)) && (!isEmail(document.getElementById("mailfriend").email4)) && (!isEmail(document.getElementById("mailfriend").email5)) && (!isEmail(document.getElementById("mailfriend").email6))){
		

		document.getElementById("mailfriend").sendmailfrom.style.background="#fffdca";
		msg=1;
	}
	
	if(msg==1){
		document.getElementById("ok").style.display="none";
		document.getElementById("msgErro").innerHTML=erro;
		document.getElementById("erro").style.display="block";	
		return false;
	}
	
	if(msg==0){
	
		document.getElementById("mailfriend").seuNome.style.background="";
		document.getElementById("mailfriend").sendmailto.style.background="";
		document.getElementById("mailfriend").sendmailfrom.style.background="";
		document.getElementById("mailfriend").email2.style.background="";
		document.getElementById("mailfriend").email3.style.background="";
		document.getElementById("mailfriend").email4.style.background="";
		document.getElementById("mailfriend").email5.style.background="";
		document.getElementById("mailfriend").email6.style.background="";
		
		document.getElementById("erro").style.display="none";
		document.getElementById("ok").style.display="block";
		
		// Captura os valores dos campos do formulário 
		var nome = document.getElementById("mailfriend").seuNome.value;
		nome = escape(nome);
		var mailfrom = document.getElementById("mailfriend").sendmailfrom.value;
		var mailto = document.getElementById("mailfriend").sendmailto.value;
		var send = document.getElementById("mailfriend").send.value;
		var sendmailsourcetemplate = document.getElementById("mailfriend").sendmailsourcetemplate.value;
		var resultpage = document.getElementById("mailfriend").resultpage.value;
		var mailForm = document.getElementById("mailfriend");
		
		// inclui o restante dos e-mails na url caso existam
		$('#mailfriend input[type="text"]').each(function(i)
		{
			if ((this.value != '') && (this.name.search(/email/) > -1))
			{
				mailto = mailto + ","+ this.value;
			};
		});
		
		// Se o checkbox "Enviar cópia para meu e-mail" estiver marcado, adiciona o meu e-mail no mailto também.
		if(document.getElementById("mailfriend").copia.checked == true)
		{
			mailto = mailto + "," + mailfrom;
		}
		
		// http request 
		var baseUrl = _URL;
		
		url = baseUrl+"?tpl=mailfriend&oldtpl="+_OLD_TPL+"&nome="+nome+"&sendmailto="+mailto+"&sendmailfrom="+mailfrom+"&send="+send+"&sendmailsourcetemplate="+sendmailsourcetemplate+"&resultpage="+resultpage;
		
		// Monta a url do http request passando os parametros sid e infoid que serão utilizados pelo mailview.htm caso existam.
		if(_SID != "" &&  _INFOID != "")
		{
			url = baseUrl+"?tpl=mailfriend&oldtpl="+_OLD_TPL+"&sid="+_SID+"&infoid="+_INFOID+"&nome="+nome+"&sendmailto="+mailto+"&sendmailfrom="+mailfrom+"&send="+send+"&sendmailsourcetemplate="+sendmailsourcetemplate+"&resultpage="+resultpage;
		} 
		else if(_SID != "") 
		{
			url = baseUrl+"?tpl=mailfriend&oldtpl="+_OLD_TPL+"&sid="+_SID+"&nome="+nome+"&sendmailto="+mailto+"&sendmailfrom="+mailfrom+"&send="+send+"&sendmailsourcetemplate="+sendmailsourcetemplate+"&resultpage="+resultpage;
		}
		
		if (document.getElementById("mailfriend").mypage_infoid.value != '' && document.getElementById("mailfriend").mypage_sid.value != ''){
		  url = url+'&mypageinfoid='+document.getElementById("mailfriend").mypage_infoid.value;
		  url = url+'&mypagesid='+document.getElementById("mailfriend").mypage_sid.value;
    }
		
		// reseta os campos para quando o usuário abrir novamente o ligthbox
		document.getElementById("mailfriend").sendmailfrom.value="";
		document.getElementById("mailfriend").sendmailto.value="";
		
		document.getElementById("mailfriend").seuNome.value="";
		
		document.getElementById("mailfriend").email2.value="";
		document.getElementById("mailfriend").email3.value="";
		document.getElementById("mailfriend").email4.value="";
		document.getElementById("mailfriend").email5.value="";
		document.getElementById("mailfriend").email6.value="";
		 //alert(url);
		$('#ok').load(url);

		return false;
	}
	return false;
}

function isEmail(email){
	var goodEmail = email.value.match(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/gi);
	if (!goodEmail || email.value ==""){
		return false;
	}else{
		return true;
	}
}
