// JavaScript Document
/*author Thomas Ricci*/

function validaEmail(email)
{
	var emailPattern=/(.)+\@(.)+\.(.)+$/;
	if(emailPattern.test(email.value))
	{
			document.getElementById("error").innerHTML="";
			//alert("Email Registrata con Successo! \n \nShakymakaky ti ringrazia per il tuo interesse!");
			document.getElementById("thanks").innerHTML="<font color='#FFFFFF'>Invio in Corso ...</font>"
			
			inviaEmailAjax(email,"NewsLetter");
			
		
	}
	else 
	{
		document.getElementById("error").innerHTML="<font color='red'>Attenzione: email non valida!</font>";
		document.getElementById("thanks").innerHTML="";
				}
	return false;
}

var xmlHttp;


	function inviaEmailAjax(data,dest)
	{
		try
		{
			xmlHttp= new XMLHttpRequest(); //Firefox,Chrome...
		}catch(e)
			{
				try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); //IE...
			}catch(e)
		{try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP")}catch(e){alert("Il tuo browser non supporta contenuto dinamico: Aggiorna il Browser!");}}}
		

		xmlHttp.onreadystatechange = function (){ callback(); }
		xmlHttp.open("get",dest+"?email="+data.value,true);
		xmlHttp.send(null);
			
	}

	function callback()
	{
		if ( xmlHttp.readyState === 2 ) {
	    		document.getElementById("error").innerHTML = "Richiesta inviata...";
			}
		else if ( xmlHttp.readyState === 3 ) {
    			document.getElementById("error").innerHTML = "Ricezione della risposta...";
			}
		else if ( xmlHttp.readyState === 4 )
			 {
			        if ( xmlHttp.status === 200 ) 
				{
					    var res=xmlHttp.responseText;
					    if(res=="Email già presente nei nostri archivi!")
							{
								document.getElementById("error").innerHTML="<font color='red'>"+res+"</font>";
								document.getElementById("thanks").innerHTML="";
							}
						else 
							{
								document.getElementById("thanks").innerHTML="<font color='green'>"+res+"</font>";
								document.getElementById("error").innerHTML="";
							}
	        		}

			
	       			 else {
	        			// errore di caricamento
	        			document.getElementById("error").innerHTML = "<font style='color:red;'>Errore nell'invio dei dati...</font><br/>";
	        		}

			}
		
	}