// JavaScript Document

function get(elemento){
	return document.getElementById(elemento);
}

var mail = false;
var nome = false;
var send = false;

function splash_alert(alerta){
	get(alerta).style.display='block';
	get('fade').style.display='block';
}

function hid_splash_alert(alerta){
	get(alerta).style.display='none';
	get('fade').style.display='none';
}

function habilita(){
	var bt = get('news_submit');
	if(mail && nome){	
		send = true; 
	}else{
		send = false;
	}
}

function validaNome(alerta,campo) {
	var cam = get(campo);
	if (cam.value.length < 3) {
		nome = false;
		splash_alert(alerta);
		habilita();
	} else {
		nome = true;
		habilita();
	}
}

function validaMail(alerta,campo){
	var cam = get(campo);
	var email = cam.value;
	var reg = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	//alert(email);
	if ( ! ( reg.test(email) ) ) {
		//cam.style.border = "1px solid #FF0000";
		mail = false;
		splash_alert(alerta);
		habilita();
	}else{
		mail = true;
		habilita();
	}
}

function semNada(s1,s2){ 
	if( ( (s1.value == null) && (s2.value == null) ) ||( ( s1.value == "") && (s2.value == "") ) ){
		return  true;
	}else{
		return false;
	}
}

function ajax()
{
    try {
        return new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
        try {
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch(ex) {
            try {
                return new XMLHttpRequest();
            } catch(exc) {
                alert("Esse browser não tem recursos para uso do AJAX");
                return false;
            }
        }
    }
}

var ajax1 = new ajax();

function vaiNews(camp1,camp2,alerta)
{
	
		var cam1 = get(camp1);
		var cam2 = get(camp2);
		
		var campVazio = semNada(cam1,cam2);
		
		if(!campVazio && send ){
			
		   	// abro o arquivo que vai calcular nosso frete
    		ajax1.open("GET", "res_news.php?nome="+cam1.value+"&email="+cam2.value, true );
    		// seto o header de resposta
    		ajax1.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    		// envia os dados para o php
			//alert("nome="+cam1.value+"&email="+cam2.value);
    		ajax1.send("nome="+cam1.value+"&email="+cam2.value);
    		// aqui vamos testar o estado da nossa requisição no nosso caso:
    		// status = 4 concluido
    		// != de 4 em andamento.
		    ajax1.onreadystatechange = function() {
    		    if (ajax1.readyState == 4) {
    	    	    // jogo o resultado encontrado no DIV em nosso html
    	        	//get("teste").innerHTML = 
					cam1.value ='';
					cam2.value ='';
					//alert(ajax1.responseText);
					//alert("quant="+ajax1.responseText);
				}
    		}
		}else{
			splash_alert(alerta);
		}
}

 
