var http = criaObjeto();
var nomeDiv;
var conteudoPagina;

function tiraEspaco(texto){
	while(texto.indexOf(" ") != -1){
	  texto = texto.replace(" ","");
	}
    return texto;
}

function criaObjeto(){
    var obj;
	try{		
		// XmlHttpRequest para Firefox,	Opera, Safari e derivados.		
		obj = new XMLHttpRequest();	
	}	catch (e){		
		// XmlHttpRequest para Internet Explorer.		
		try{			
		// Internet Explorer 6.0+
			obj = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			// Internet Explorer.			
			obj = new ActiveXObject("Microsoft.XMLHTTP");		
		}	
	}	
	return obj;
}



function enviaRequisicao(campo,acao,mensagem){
	if (mensagem != undefined){
		document.getElementById(campo).innerHTML = '<div align=center><br/><img src="http://www.mixnews.net/imagens/diversos/loading.gif"><br/>' + mensagem + '</div>';
	}
	nomeDiv = campo;
    http.open('get',acao);
    http.onreadystatechange = processaResposta;
    http.send(null);
}

function processaResposta(){
    if(http.readyState == 4){
        if(http.status == 200){
            var resposta = http.responseText;
            document.getElementById(nomeDiv).innerHTML = resposta;
            if (resposta.indexOf('erroChave')>0){
            	document.getElementById(nomeDiv).innerHTML = conteudoPagina;
            	alert("Lista inexistente ou chave inválida.");
            }            
            if (resposta.indexOf('cadastrouEmail')>0){
            	document.getElementById(nomeDiv).innerHTML = conteudoPagina;
            	alert("Cadastro realizado com sucesso.");
            }            
            if (resposta.indexOf('erroCadastroEmail')>0){
            	document.getElementById(nomeDiv).innerHTML = conteudoPagina;
            	alert("E-mail inválido.Tente novamente.");
            }
        }else{
            document.getElementById(nomeDiv).innerHTML = "Erro ao cadastrar.";
        }
    }
}
function cadastrar(divOrigem) {
	if (document.getElementById(divOrigem)==null){
		alert('Div inexistente ou não informado.');
		return;
	}
	if (document.getElementById('listaId')==null){
		alert('Campo listaId inexistente');
		return;
	}
	if (document.getElementById('chaveLista')==null){
		alert('Chave da lista inexistente');
		return;
	}
	if (document.getElementById('nomeNews')==null){
		alert('Campo nomeNews inexistente');
		return;
	}
	if (document.getElementById('emailNews')==null){
		alert('Campo emailNews inexistente');
		return;
	}
	
	if (tiraEspaco(document.getElementById('listaId').value) == ''){
		alert("Lista não informada.");
		document.getElementById('listaId').focus();
		return;
	} 
	if (tiraEspaco(document.getElementById('nomeNews').value) == ''){
		alert("Digite seu nome.");
		document.getElementById('nomeNews').focus();
		return;
	} 
	if (tiraEspaco(document.getElementById('emailNews').value) == ''){
		alert("Digite seu email.");
		document.getElementById('emailNews').focus();
		return;
	} else{
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('emailNews').value))){ 
	       	alert("Email inválido.");
	        document.getElementById('emailNews').focus();
	   	    return;
	    }  
	}
	conteudoPagina = document.getElementById(divOrigem).innerHTML;
	campos  = '&email=' + encodeURI(document.getElementById('emailNews').value) 
	campos += '&nome=' + encodeURI(document.getElementById('nomeNews').value);
	campos += '&listaId=' + encodeURI(document.getElementById('listaId').value);
	campos += '&chaveLista=' + encodeURI(document.getElementById('chaveLista').value);
	enviaRequisicao(divOrigem,'conector.php?pagina=1'+campos,'Cadastrando...');
	
}



