var contatoAux;
var contatoFrm = new Array(3);
function validaContato(frm){
		if(erroContato(frm.nome) && erroContato(frm.email) && erroContato(frm.texto))
		{ 
			contatoAux = frm.parentNode.innerHTML;
			contatoFrm[0] =frm.nome.value;
			contatoFrm[1] =frm.email.value;
			contatoFrm[2] =frm.texto.value;
			envia(frm);	
			return false;
		}
		else
			return false;
}
function erroContato(obj)
{
	obj.value = obj.value.replace(/[ ]{2,}/gi, ' ').replace(/(^ | $)/g, '').replace(/^[^ a-z0-9]{1,}$/ig,'');
	if(obj.value.length == 0)
		{
			obj.style.backgroundColor="red";
			obj.focus();
			alert('O '+obj.id+' é obrigatorio!');
			return false;
		}
		else
		{
			obj.style.backgroundColor='';
			return true;
		}
}
function envia(frm)
{
	try
	{
		var http = new XMLHttpRequest()
	}
	catch(ee)
	{
		try
		{
		 	var http = new ActiveXObject("Msxml2.XMLHTTP")
		}
		catch(e)
		{
			try
			{
				var http = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch(E)
			{
				var http = false
			}
		}
	}

	var aux="nome="+frm.nome.value+"&email="+frm.email.value+"&texto="+frm.texto.value;
	http.open("POST", "manda_email.php" ,true)
	
	var div  = frm.parentNode;
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	http.setRequestHeader("Content-length", aux.length);
    	http.setRequestHeader("Connection", "close");
	div.innerHTML='Enviando contato...'
	http.onreadystatechange=function() {
		if (http.readyState==4)
		{ 
			if (http.status == 200) {
				var retorno=http.responseXML;
				retorno = retorno.getElementsByTagName('retorno').item(0).firstChild.nodeValue;
				if(eval(retorno))
				{
					div.innerHTML= "Contato Enviado com sucesso!";
					div.className="msgOk";
				}
				else
				{
					div.innerHTML= "Erro ao enviar Contato! <br/><a href=\"#\"  class=\"login\" onclick=\"javascript: volta(this)\">Voltar</a>";
					div.className="msgErro";
				}
			}
		}
	}
	http.send(aux);
}
function volta(a)
{
	a.parentNode.className="contato";	
	a.parentNode.innerHTML = contatoAux;
	var frm = document.getElementById('contatoFrm');
	frm.nome.value = contatoFrm[0];
	frm.email.value = contatoFrm[1] ;
	frm.texto.value = contatoFrm[2];
}

