
	// funzione per prendere un elemento con id univoco
		function prendiElementoDaId(id_elemento) {
			var elemento;
			if(document.getElementById)
				elemento = document.getElementById(id_elemento);
			else
				elemento = document.all[id_elemento];
			return elemento;
		};
	
	// funzione per assegnare un oggetto XMLHttpRequest
		function assegnaXMLHttpRequest() {
			var
				XHR = null,
				browserUtente = navigator.userAgent.toUpperCase();
			if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
				XHR = new XMLHttpRequest();
			else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) {
				if(browserUtente.indexOf("MSIE 5") < 0)
					XHR = new ActiveXObject("Msxml2.XMLHTTP");
				else
					XHR = new ActiveXObject("Microsoft.XMLHTTP");
			}
			return XHR;
		};



/** OGGETTI / ARRAY */

	// oggetto di verifica stato
		var readyState = {
			INATTIVO:	0,
			INIZIALIZZATO:	1,
			RICHIESTA:	2,
			RISPOSTA:	3,
			COMPLETATO:	4
		};

	// array descrittivo dei codici restituiti dal server
	// [la scelta dell' array è per evitare problemi con vecchi browsers]
		var statusText = new Array();
		statusText[100] = "Continue";
		statusText[101] = "Switching Protocols";
		statusText[200] = "OK";
		statusText[201] = "Created";
		statusText[202] = "Accepted";
		statusText[203] = "Non-Authoritative Information";
		statusText[204] = "No Content";
		statusText[205] = "Reset Content";
		statusText[206] = "Partial Content";
		statusText[300] = "Multiple Choices";
		statusText[301] = "Moved Permanently";
		statusText[302] = "Found";
		statusText[303] = "See Other";
		statusText[304] = "Not Modified";
		statusText[305] = "Use Proxy";
		statusText[306] = "(unused, but reserved)";
		statusText[307] = "Temporary Redirect";
		statusText[400] = "Bad Request";
		statusText[401] = "Unauthorized";
		statusText[402] = "Payment Required";
		statusText[403] = "Forbidden";
		statusText[404] = "Not Found";
		statusText[405] = "Method Not Allowed";
		statusText[406] = "Not Acceptable";
		statusText[407] = "Proxy Authentication Required";
		statusText[408] = "Request Timeout";
		statusText[409] = "Conflict";
		statusText[410] = "Gone";
		statusText[411] = "Length Required";
		statusText[412] = "Precondition Failed";
		statusText[413] = "Request Entity Too Large";
		statusText[414] = "Request-URI Too Long";
		statusText[415] = "Unsupported Media Type";
		statusText[416] = "Requested Range Not Satisfiable";
		statusText[417] = "Expectation Failed";
		statusText[500] = "Internal Server Error";
		statusText[501] = "Not Implemented";
		statusText[502] = "Bad Gateway";
		statusText[503] = "Service Unavailable";
		statusText[504] = "Gateway Timeout";
		statusText[505] = "HTTP Version Not Supported";
		statusText[509] = "Bandwidth Limit Exceeded";
		
		
	function isLegal(input, legalchars) {
	var ok = true;
	var chr = null;
	var i = 0;
	
	for (i=0; (i<input.length) && (ok); i++) {
		chr = input.charAt(i);
		ok = (legalchars.indexOf(chr,0) != -1);
	}
	return ok;
}

function isNotLegal(input, illegalchars) {
	var ok = false;
	var chr = null;
	var i = 0;

	for (i=0; (i<input.length) && (!ok); i++) {
		chr = input.charAt(i);
		ok = (illegalchars.indexOf(chr,0) != -1);
		   
	}
	return  ok;
}


function isblank(s) {
	for ( var i=0; i<s.length; i++ ) {
   	var c = s.charAt(i)	;
   	if ( (c != ' ') && (c != '\n') && (c != '\t') )  return false;
	}
	return true;

}

function testIntero(formfield) {
	var ok = true;
	if (!isblank(formfield.value)) {
		if (!(isLegal(formfield.value, "0123456789"))) {
			ok = false;
			formfield.focus();
		}
	}
	return(ok);
}

function checkEmail(input)
{
   var ok=false;
   var i=0;
   var charToSearch = '@';
   for (i=0; (i<input.length) && (ok==false); i++) 
	{
		chr = input.charAt(i);
		if ( chr == charToSearch ) 
		     if( charToSearch == '@') 
		         charToSearch = '.';
		     else
		         ok=true;
	}
	
   return(ok);
}


function SubmitReg()
{
	if( isblank(prendiElementoDaId("numerotessera").value) ) 
	{
	alert("Devi indicare il numero della tua Ipercard");
	prendiElementoDaId("numerotessera").focus();
	return false;
	}
	if( isblank(prendiElementoDaId("nome").value))  
	{
	prendiElementoDaId("nome").focus();
	alert("Devi indicare il tuo nome");
	return false;
	}
	if( isblank(prendiElementoDaId("cognome").value))
	{
	prendiElementoDaId("cognome").focus();
	alert("Devi indicare il tuo cognome");
	return false;
	}
	if( isblank(prendiElementoDaId("address").value) ) 
	{
	prendiElementoDaId("address").focus();	
	alert("Indicare l'indirizzo");
	return false;
	}	
	if( isblank(prendiElementoDaId("citta").value) ) 
	{
	prendiElementoDaId("citta").focus();
	alert("Indicare la città");
	return false;
	}
	if( isblank(prendiElementoDaId("pr").value) ) 
	{
	prendiElementoDaId("pr").focus();	
	alert("Indicare la provincia");
	return false;
	}		
	if( isblank(prendiElementoDaId("cap").value) ) 
	{
	prendiElementoDaId("cap").focus();	
	alert("Indicare il CAP");
	return false;
	}
	if( !isblank(prendiElementoDaId("cap").value) && !testIntero(prendiElementoDaId("cap")) ) 
	{
	prendiElementoDaId("cap").focus();	
	alert("Il cap non è corretto");
	return false;
	}
	if( !isblank(prendiElementoDaId("cap").value) && prendiElementoDaId("cap").value.length<5 ) 
	{
	prendiElementoDaId("cap").focus();	
	alert("Il cap non è corretto");
	return false;
	}
	if( isblank(prendiElementoDaId("email").value) ) 
	{
	prendiElementoDaId("email").focus();	
	alert("Devi indicare la tua email");
	return false;
	}
	var re = new RegExp(prendiElementoDaId("expreg").value);
	var valoremail=prendiElementoDaId("email").value.replace(" ","");
	prendiElementoDaId("email").value=valoremail;
	if (!valoremail.match(re)) 
	{
	    prendiElementoDaId("email").focus();
	    alert("Indirizzo e-mail non valido");
	    return false;
	}
	if( isblank(prendiElementoDaId("aacli").value) ) 
	{
	prendiElementoDaId("aacli").focus();
	alert("Indicare l'anno ");
	return false;
	}
	if( !testIntero(prendiElementoDaId("aacli")) ) 
	{
	prendiElementoDaId("aacli").focus();
	alert("L'anno indicato non è corretto");
	return false;
	}
	if( !isblank(prendiElementoDaId("aacli").value) && prendiElementoDaId("aacli").value.length<4 ) 
	{
	prendiElementoDaId("aacli").focus();	
	alert("L'anno indicato non è corretto");
	return false;
	}
	if( isblank(prendiElementoDaId("aa").value) ) 
	{
	prendiElementoDaId("aa").focus();
	alert("Indicare il tuo anno di nascita di nascita ");
	return false;
	}
	if( !testIntero(prendiElementoDaId("aa")) ) 
	{
	prendiElementoDaId("aa").focus();
	alert("L'anno di nascita non è corretto");
	return false;
	}
	if( !isblank(prendiElementoDaId("aa").value) && prendiElementoDaId("aa").value.length<4 ) 
	{
	prendiElementoDaId("aa").focus();	
	alert("L'anno di nascita non è corretto");
	return false;
	}
	
	if( isblank(prendiElementoDaId("pwssd").value) ) 
	{
	prendiElementoDaId("pwssd").focus();
	alert("Devi indicare la password");
	return false;
	}
	if( prendiElementoDaId("pwssd").value.length < 6  ) 
	{
	prendiElementoDaId("pwssd").focus();
	alert("La password deve essere almeno di 6 caratteri");
	return false;
	}
	if( isNotLegal(prendiElementoDaId("pwssd").value, "' ")) 
	{
	prendiElementoDaId("pwssd").focus();
	alert("La password non puo contenere il carattere ' o spazi");
	return false;
	}
	
	if( prendiElementoDaId("pwssd").value != prendiElementoDaId("pwssd2").value ) 
	{
	prendiElementoDaId("pwssd").focus();
	alert("Le due password devono essere uguali");
	return false;
	}
	if ( !prendiElementoDaId("lex6754").checked)
	{
	prendiElementoDaId("lex6754").focus();
	alert("Attenzione per registrarsi è necessario esprimere il consenso al trattamento dei dati");
	return false;
	}
	
	doVerificaEsistenzaEmail( prendiElementoDaId("numerotessera").value, prendiElementoDaId("email").value );
	
}


	
function doVerificaEsistenzaEmail(username, email) {
  var ajax; 
  

   if( isblank(email)) 
     return false;
   
   // sto ancora in attesa della risposta, quindi non rifaccio la chiamata
   if ( richiestaInCorso  ) return false;
 
    ajax = assegnaXMLHttpRequest();
 
  // se tutti gli elementi appena assegnati sono validi
  if(ajax) {
  
    richiestaInCorso = true;
    ajax.open("get", "checkEmail.asp" + "?w2eemail=" + email + "&w2eidutente=" + prendiElementoDaId("hid").value+"&w2enomeutente="+username);
    ajax.setRequestHeader("connection", "close");
    
    // funzione assegnata ad onreadystatechange
    ajax.onreadystatechange = function() {
  
		      // se le operazioni sono state effettuate
		      if(ajax.readyState === readyState.COMPLETATO) {
		      	//alert("tornato");
		   	richiestaInCorso = false;
		        // ... ed il server non ha dato errori
		        if(statusText[ajax.status]  === "OK") {
		          // stampiamo il risultato
		         
		          if( ajax.responseText.substring(0,2) == "YE" ) {
		              alert("L'indirizzo e-mail o il numero di Ipercard sono gia' presenti nel sistema, impossibile continuare con la registrazione");
			      prendiElementoDaId("numerotessera").focus();
			      return;		             
		             } 
		          if( ajax.responseText.substring(0,2) == "NO" ) {
		              prendiElementoDaId("submitter").action=prendiElementoDaId("hpag").value+'.asp';
		              prendiElementoDaId("submitter").submit(true);
			      return;		             
		             }
		          if( ajax.responseText.substring(0,2) == "ER" ) {
		              alert("In questo momento non è possibile completare l'operazione");
			      return;		             
		             }
		      	}
		        // ... altrimenti mandiamo un avviso
		        else {
		          alert("In questo momento non è possibile completare l'operazione");
			}
					     
		        // riabiliamo il bottone per permettere un'altra richiesta
		      	}
    		};	
    // invio richiesta
    ajax.send(null);
    return( false );
  }
   
}
		
	
	
	
function SubmitRegNewsletter()
{
	if( isblank(prendiElementoDaId("nome").value))  
	{
	prendiElementoDaId("nome").focus();
	alert("Devi indicare il tuo nome");
	return false;
	}
	if( isblank(prendiElementoDaId("cognome").value))
	{
	prendiElementoDaId("cognome").focus();
	alert("Devi indicare il tuo cognome");
	return false;
	}
	if( isblank(prendiElementoDaId("cellulare").value) ) 
	{
	prendiElementoDaId("cellulare").focus();	
	alert("Indicare il cellulare");
	return false;
	}	
	if( !isLegal(prendiElementoDaId("cellulare").value, " 0123456789/-") ) 
	{
	prendiElementoDaId("cellulare").focus();	
	alert("Il numero di cellulare non è corretto (e'' possibile indicare numeri, spazi, -, /)");
	return false;
	}		
	if( isblank(prendiElementoDaId("cap").value) ) 
	{
	prendiElementoDaId("cap").focus();	
	alert("Indicare il CAP");
	return false;
	}
	if( !isblank(prendiElementoDaId("cap").value) && !testIntero(prendiElementoDaId("cap")) ) 
	{
	prendiElementoDaId("cap").focus();	
	alert("Il cap non è corretto");
	return false;
	}
	if( !isblank(prendiElementoDaId("cap").value) && prendiElementoDaId("cap").value.length<5 ) 
	{
	prendiElementoDaId("cap").focus();	
	alert("Il cap non è corretto");
	return false;
	}
	if( isblank(prendiElementoDaId("email").value) ) 
	{
	prendiElementoDaId("email").focus();	
	alert("Devi indicare la tua email");
	return false;
	}
	var valore=prendiElementoDaId("email").value.replace(" ","");
	prendiElementoDaId("email").value=valore;
	var re = new RegExp(prendiElementoDaId("expreg").value);
	if (!prendiElementoDaId("email").value.match(re)) 
	{
	    prendiElementoDaId("email").focus();
	    alert("Indirizzo e-mail non valido");
	    return false;
	}
	if ( !prendiElementoDaId("lex6754").checked)
	{
	prendiElementoDaId("lex6754").focus();
	alert("Attenzione per registrarsi è necessario esprimere il consenso al trattamento dei dati");
	return false;
	}
	
	doVerificaEsistenzaEmail( "", prendiElementoDaId("email").value );
	
}


function getTmpstmp()
{
	var aaa= new Date();
	return( aaa.getTime());	
}
	