   var check_reg_fields = function ( ){
   
       if ( $('#signup_username').val() == '' ){
           alert('Devi riempire il campo username!');
           return false;
       }else if ( String($('#signup_username').val()).length < 4 ){
           alert('L\'username deve contenere almeno quattro caratteri!');
           return false;
       }else if ( $('#signup_password').val() == '' ){
           alert('Devi riempire il campo password!');
           return false;
       }else if ( String($('#signup_password').val()).length < 4 ){
           alert('La password deve contenere almeno quattro caratteri') ;
           return false;
       }else if ( $('#signup_email').val() == '' ){
           alert('Devi riempire il campo Email');
           return false;
       }else if ( ! echeck($('#signup_email').val()) ){
           alert('L\'email inserita non e\' corretta!');
           return false;
       }else if ( $('#signup_email').val() != $('#signup_email2').val() ){
           alert('Le due email non coincidono!');
           return false;
       }
       
       return true ;
   }
   
   var check_login = function ( ){
       var username = $('#login-usr').val();
       var password = $('#login-pwd').val();
       
       if ( logged ){
           return true;
       }
       if ( username == '' || username == 'Username' ){
           alert('Devi inserire un username!');
           return false ;
       }else if ( password == '' || password == 'Password' ){
           alert('Devi inserire una password');
           return false;
       }else{
           $.post(fm_base_url+'login.htm', {username:username,password:password},function(data){
               if ( data == 'username' ){
                   alert('Questa userame non esiste!');
               }else if( data == 'password' ){
                   alert('La password non e\' corretta');
               }else{
                   logged=true;
                   $('#login-form').submit();
               }
           });
       }
       
       return false ;
   }
   
   var vote = function ( id,squadra,target ){
       if ( ! is_logged ){
           return alert('Per votare devi essere loggato!\nSe non sei registrato puoi\' farlo gratuitamente usando il modulo a destra !');
       }else if(voted[id]!=undefined&&voted[id]){
           return alert('Hai gia\'a votato questo pronostico!');
       }
       $(target).animate({backgroundColor:'#8cbb47'});
       voted[id]=true;
       
       $.post(fm_base_url+'vote.htm', {id:id,squadra:squadra},function(data){
           $('#user-points').html(data.points);
           $('#user-spoints').html(data.spoints);
           $('#user-rank').html(data.rank+'&deg;');
           $('#user-points').css('color','red');
           $('#user-points').animate({color:'#000'},800);
           $('#user-spoints').css('color','red');
           $('#user-spoints').animate({color:'#000'},800);
           $('#user-rank').css('color','red');
           $('#user-rank').animate({color:'#000'},800);
           
           
           //FIXED
           
           $('#user-points-fixed').html(data.points);
           $('#user-spoints-fixed').html(data.spoints);
           $('#user-rank-fixed').html(data.rank+'&deg;');
           $('#user-points-fixed').css('color','red');
           $('#user-points-fixed').animate({color:'#000'},800);
           $('#user-spoints-fixed').css('color','red');
           $('#user-spoints-fixed').animate({color:'#000'},800);
           $('#user-rank-fixed').css('color','red');
           $('#user-rank-fixed').animate({color:'#000'},800);
           
       },'json');
   }
   
   var live_refresh_start = function ( ){
       $.post(fm_base_url+'live.htm', {items:items},function(data){
           for ( var i in data ){
               var cur = data[i];
               var total_votes = parseInt(cur.voti_squadra_a)+parseInt(cur.voti_squadra_b)+parseInt(cur.voti_squadra_x);
               var percent_a = parseInt(Math.round(cur.voti_squadra_a/total_votes*100));
               var percent_b = parseInt(Math.round(cur.voti_squadra_b/total_votes*100));
               var percent_x = parseInt(Math.round(cur.voti_squadra_x/total_votes*100));
               
               if ( ! isNaN(percent_a) ){
                  $('#prono-progress-a-'+cur.id).animate({width:Math.max(2,percent_a)+'%'});
                  $('#prono-percent-a-'+cur.id).html(percent_a+'%');
               }
               if ( ! isNaN(percent_b) ){
                  $('#prono-progress-b-'+cur.id).animate({width:Math.max(2,percent_b)+'%'});
                  $('#prono-percent-b-'+cur.id).html(percent_b+'%');
               }
               if ( ! isNaN(percent_x) ){
                  $('#prono-progress-x-'+cur.id).animate({width:Math.max(2,percent_x)+'%'});
                  $('#prono-percent-x-'+cur.id).html(percent_x+'%');
               }
               
               //console.log(percent_a);
               //console.log(percent_b)
               //console.log(percent_x);
               
           }
           setTimeout ( live_refresh_start, 800);
       },'json');
   }
   
   $(function(){
       $('#login-usr').focus(function(){
           var me = $(this);
           if ( me.val() == 'Username' ){
               me.val('');
               me.removeClass('tip-field');
           }
       });
       
       $('#login-usr').blur(function(){
           var me = $(this);
           if ( me.val() == '' ){
               me.val('Username');
               me.addClass('tip-field');
           }
       });
       
       $('#login-pwd').focus(function(){
           var me = $(this);
           if ( me.val() == 'Password' ){
               me.val('');
               me.removeClass('tip-field');
           }
       });
       
       $('#login-pwd').blur(function(){
           var me = $(this);
           if ( me.val() == '' ){
               me.val('Password');
               me.addClass('tip-field');
           }
       });
       
       $('#login-usr').val('Username').addClass('tip-field');
       $('#login-pwd').val('Password').addClass('tip-field');
       
   })
   
   $(function(){
    var hold = $('#user-box') ;
    var fixed = $('#user-fixed-box');
    
    var holdHeight = hold.height();
    var holdMargin = parseInt(hold.css('margin-bottom'));
    
    fixed.hide();
    //fixed.html(hold.html());
    
    $(window).scroll(function(){
        var scrollTop = $(window).scrollTop();
        if ( scrollTop > hold.position().top ){
            fixed.show();
        }else{
            fixed.hide();
        }
    })
});
