var resultado;
function valida(obj){
    valor=obj.value;
    nome=obj.name;
    if(nome=='email'){
        resultado = validaEmail(valor);
    }
    if(resultado){
        document.getElementById(nome).style.backgroundColor  = "#FFFFFF";
    }else{
        document.getElementById(nome).style.backgroundColor  = "red";
    }
}
function validaEmail(valor){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(er.test(valor)){
        return true;
    }else{
        return false;
    }
}
function envianews(){
    if(resultado){
        nome=document.getElementById("nome").value;
        email=document.getElementById("email").value;
        display_data(nome, email);
    }else{
        Ext.MessageBox.alert('Alerta', 'Erro ao cadastrar o e-mail, por favor <BR/>verifique se digitou o e-mail corretamente');
    }
}
function display_data(nome, email) {
    xmlhttp=GetXmlHttpObject();
    var resposta= null;
    if (xmlhttp==null) {
        alert ("Your browser does not support AJAX!");
        return false;
    }
    var url="/func/cadnews/?nome="+nome+"&email="+email;
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==1 || xmlhttp.readyState==2 || xmlhttp.readyState==3){
            document.getElementById("status").style.display="inline";
        }
        if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") {
            document.getElementById("status").style.display="none";
            resp=xmlhttp.responseText;
            if(resp!=null){
                if(resp=="cadastrado"){
                    Ext.MessageBox.alert('Alerta', 'E-mail ja cadastrado.');
                }else{
                    if(resp=="erro"){
                        Ext.MessageBox.alert('Alerta', 'Erro ao cadastrar.');
                    }else{
                        if(resp=="sucess"){
                            Ext.MessageBox.alert('Alerta', 'Cadastrado com sucesso.');
                            document.getElementById("nome").value="";
                            document.getElementById("email").value="";
                            document.getElementById("email").style.backgroundColor  = "#FFFFFF";
                        }
                    }
                }
            }else{
                Ext.MessageBox.alert('Alerta', 'Erro ao cadastrar.');
            }
            return resposta;
        }
    }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    return false;
}
function GetXmlHttpObject() {
    var xmlhttp=null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlhttp=new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlhttp;
}
function OnEnter(evt){
    resultado=validaEmail(document.getElementById("email").value);
    var key_code = evt.keyCode  ? evt.keyCode  : evt.charCode ? evt.charCode : evt.which    ? evt.which    : void 0;
    if (key_code == 13){
        envianews();
    }
}

