function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

function addML(){
	var errore = 0;
	var email = jQuery('#mail').val();
	if (email.length==0) {  
            errore = 1;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        errore = 1;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        errore = 1;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        errore = 1;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        errore = 1;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
		errore = 1;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
		errore = 1;
    }
    
	
	if (errore == 0){
		jQuery.post("/act_penati.php?act=1", { 
			mail: jQuery('#mail').val()
			},function(data){
				if (data == 1){
					jQuery('#mail').val("");
					jQuery("#errorMsg").html("Iscrizione avvenuta con successo");
				}else{
					jQuery("#errorMsg").html("Il servizio non &egrave; momentaneamente attivo.<br />Riprovare in un secondo momento.");
				}
				
			});
	}else{
		jQuery("#errorMsg").html("Campo E-Mail non valido");
	}
	
}
