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(jQuery("#dati").attr("checked") == false){
		errore = 2;
	}
	
	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;
    }
    
	
	switch (errore){
		case 0:
			jQuery.post("act.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.");
				}
			});
			break;
		case 1:
			jQuery("#errorMsg").html("Campo E-Mail non valido");
			break;
		case 2:
			jQuery("#errorMsg").html("&Egrave; necessario dare il consenso al trattamento dei dati");
			break;
	}
	
	
}


function addView(idVideo){
	
	jQuery.post("/video/act.php", { 
	act: 1,
	idVideo: idVideo
	});

	
	
}
function countVideoView(idVideo){
	
	jQuery.post("/video/act.php", { 
	act: 2,
	idVideo: idVideo
	},function(data){
		jQuery('#visite'+idVideo).html("Visualizzazioni: "+data);
	});

	
	
}

