function validateForm(){
	var isWrong = false;
	$('#errorContainer').html(''); //reset error container
	
	$('.required').each(function(){
		
		//check email
		if($(this).attr('id') == "email"){
			var x=$(this).attr('value');
			var atpos=x.indexOf("@");
			var dotpos=x.lastIndexOf(".");
			if(x==""){
				$('#errorContainer').append('<p>E-mail obbligatoria</p>');
				$(this).addClass('errorFound');
				isWrong=true;
			}	else if( atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length){
				$('#errorContainer').append('<p>E-mail non valida</p>');
				$(this).addClass('errorFound');
				isWrong=true;
			};
		};
		
		//check password
		if($(this).attr('id') == "password"){
			var x=$(this).attr('value');
			if (x == ''){
				$('#errorContainer').append('<p>Password non valida</p>');
				$(this).addClass('errorFound');
				isWrong=true;
			};
		};
    
    //check prefisso
    if($(this).attr('id') == 'prefix'){
        var x = $(this).attr('value');
        if(x == ''){
          $('#errorContainer').append('<p>Prefisso obbligatorio</p>');
          $(this).addClass('errorFound');
          isWrong=true;
        } else if ( !isInt(x)  || x.length != 3){
          $('#errorContainer').append('<p>Prefisso non valido</p>');
          $(this).addClass('errorFound');
          isWrong=true;
        }
        else{
            jQuery.ajax({
                url:  '../ajax/check_prefix.php?pr='+x,
                success:  function(ris) {
                              if(!ris){
                                  $('#errorContainer').append('<p>Prefisso non valido</p>');
                                  $(this).addClass('errorFound');
                                  isWrong=true;
                              }
                          },
                async:   false,
                dataType:'json'
            });
        }
    }
		
		//check telefono
		if($(this).attr('id') == "telefono"){
      var numericExpression = /^[0-9]+$/;
			var x=$(this).attr('value');
			if (x == ''){
				$('#errorContainer').append('<p>Numero di telefono obbligatorio</p>');
				$(this).addClass('errorFound');
				isWrong=true;
			} else if ( !x.match(numericExpression) || x.length<6){
				$('#errorContainer').append('<p>Numero di telefono non valido</p>');
				$(this).addClass('errorFound');
				isWrong=true;
			};
		};
		
		//check privacy
		if($(this).attr('id') == "privacy"){
			if($(this).prop('checked') == false){
				$('#errorContainer').append('<p>Consenso al trattamento dati obbligatorio</p>');
				$(this).addClass('errorFound');
				isWrong=true;
			}
		};
		
    
    //check codice
    if($(this).attr('id') == 'codice'){
      var x = $(this).val();
      
			if (x == ''){
				$('#errorContainer').append('<p>Codice obbligatorio</p>');
				$(this).addClass('errorFound');
				isWrong=true;
			} else if((x.length != 10) && (x.length != 6) && (x.length != 9)){
        $('#errorContainer').append('<p>Il codice non è corretto</p>');
				$(this).addClass('errorFound');
				isWrong=true;
      }
    }
    
		//check captcha
		if($(this).attr('id') == "id_captcha"){
			var x=$(this).attr('value');
			if(!x){
				$('#errorContainer').append('<p>Non hai selezionato un simbolo per la convalida</p>');
				$(this).addClass('errorFound');
				isWrong=true;				
			} else if(x!=$('#play-captcha-original img').attr('rel')){
				$('#errorContainer').append('<p>Il simbolo selezionato non è corretto</p>');
				$(this).addClass('errorFound');
				isWrong=true;
			}
		};
    
    //check codice verifica numero
		if($(this).attr('id') == "codiceVerifica"){
			var x=$(this).val();
      
			if (x == ''){
				$('#errorContainer').append('<p>Codice di verifica obbligatorio</p>');
				$(this).addClass('errorFound');
				isWrong=true;
			}
		};
		
	});
	
  if(isWrong){
		$('.linkErrorContainer').colorbox({width:350, inline:true, href:"#errorContainer", opacity: 0.7, fixed: true, initialWidth:0, initialHeight:0});
		$('.linkErrorContainer').click();
	};
	
  return !isWrong;
}

function isInt(value){
  if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
      return true;
  } else {
      return false;
  }
}

function clearError(field){
	$(field).removeClass('errorFound');
}

$(document).ready(function() {
    /**
      * nuovi snippet
      */
        
    //verifica numero
    $('#sendpsw').click(function() {
        $.post('../ajax/op_sendpsw.php',{},function(send) {
            switch(send){
                case 'true':   var msg = 'La password per la richiesta dei premi ti è stata inviata al tuo numero di cellulare.'; break;
                case 'false':  var msg = 'Errore, non è stato possibile inviare l\'SMS, riprova più tardi.'; break;
                case 'null':   var msg = 'Errore, utenza non trovata. Inserire un numero valido.'; break;
                case 'expire': var msg = 'Hai già richiesto la password per richiedere i premi, controlla sul tuo cellulare.'; break;
                case 'kosession': var msg = 'Errore, sessione non valida. Ripetere login'; break;
            }
                
            alert(msg);
        },'json');
    });
});
