function checkEmail(valor){
	if(valor==''){return false;}
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){return true;}
	return false;
}

function valida_formulario_rapido(obj) {
	if (checkEmail(obj.email.value)==false) {
		alert('E-mail inválido.');
		pinta2(obj.email);
		obj.email.focus();
		return false;
	}
	envia_formulario_news(obj);
}

function envia_formulario_news(obj){
	try {
		ajax = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	catch(e) {
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(ex) {
			try {
				ajax = new XMLHttpRequest();
			}
			catch(exc) {
				alert("Esse navegador de internet não tem recursos para funcionamento correto deste site!");
				ajax = null;
			}
		}
	}
	
	if(ajax) {
		var file = "post-form-news.php";
		idDiv = document.getElementById('formulario-news');
		var email = obj.email.value;

		ajax.open("POST", file, true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 1) {
				idDiv.innerHTML = "<div class='msg-form'>Enviando...</div>";   
			}
			if(ajax.readyState == 4 ) {
				if(ajax.responseXML) {
					processXML(ajax.responseXML);
				}
				else {
					idDiv.innerHTML = "<div class='msg-form'>Erro: não foi possível o envio.</div>";
				}
			}
		}
		var params = 'email='+email;
		ajax.send(params);
	}
}
function processXML(obj){
	var dataArray = obj.getElementsByTagName("retorno");
	if(dataArray.length > 0) {
		for(var i = 0 ; i < dataArray.length ; i++) {
			var retorno = dataArray[0].firstChild.nodeValue;
			if(retorno == 'tem'){
				idDiv.innerHTML = "<div class='msg-form'>Esse e-mail já existe<br><br><a href='javascript:formvolta();'>Voltar</a></div>";
				break;
			}
			else if(retorno == 'ok'){
				idDiv.innerHTML = "<div class='msg-form'>Seu e-mail foi cadastrado<br>com sucesso!<br><br><a href='javascript:formvolta();'>Voltar</a></div>";
			}
		}
	}
	else {
		idDiv.innerHTML = "<div class='msg-form'>Erro! Tente novamente.</div>";
	}	  
}
function formvolta(){
	idDiv = document.getElementById('formulario-news');
	idDiv.innerHTML = '<h5>Insira seu e-mail:</h5><input name="email" type="text" class="text" onFocus="javascript:pinta2(this);" onBlur="javascript:apaga2(this);"><input name="enviar" type="image" src="img/bt-incluir.png" class="button">';
}
