
function rakeback_calculate(){
    /*
            full table hands per hour is aprox 60, 6max aprox 80
    */
    
    //levels for calculation
    var level=new Array();
    level[60] = new Array(0.05, 0.08, 0.12, 0.16, 0.23, 0.26, 0.28, 0.31, 0.04, 0.06, 0.08, 0.13, 0.18, 0.22, 0.26);
    level[80] = new Array(0.06, 0.07, 0.13, 0.17, 0.24, 0.27, 0.29, 0.32, 0.05, 0.07, 0.09, 0.14, 0.19, 0.23, 0.27);
    level[100] = new Array(0.05, 0.08, 0.12, 0.16, 0.23, 0.26, 0.28, 0.31, 0.04, 0.06, 0.08, 0.13, 0.18, 0.22, 0.26);
    var tabletype = $('#rake_calculator :radio').fieldValue()[0];
    var selectedLevel = level[tabletype][$('#limit').val()] / 100;
    if(
        ($('#week').val() != 0)&&($('#hands').val() != 0) ||
        ($('#week').val() == 0)&&($('#hands').val() == 0)
        )
    {   // Both or None Hands or HPR
        $('#hourserror').show('slow');
        return false;
    } else $('#hourserror').hide('slow');
    
    if($('#week').val() != 0) var hands = ( $('#week').val() * 7) * tabletype;
    if($('#hands').val() != 0) var hands = ( $('#hands').val() * 7);
    
    //var hands = ( $('#week').val() * 7) * tabletype;
    var tables = $('#tables').val();
    var rakepercent = $('#rakepercent').val();
    
    var totalDaily      = hands*selectedLevel*tables*100/7;
    if(tables == 0){
        $('#tableserror').show('slow');
        return false;
    } else $('#tableserror').hide('slow');
    if(("."+totalDaily+".") == ".NaN.") {
        $('#limiterror').show('slow');
        //alert('Please select a limit');
        return false;
    } else $('#limiterror').hide('slow');
    
    var totalWeekly     = totalDaily*7;
    var totalMonthly    = totalWeekly*((365 / 12) / 7);
    var totalYearly     = totalMonthly*12;
    var rakebackDaily   = totalDaily*rakepercent/100;
    var rakebackWeekly  = totalWeekly*rakepercent/100;
    var rakebackMonthly = totalMonthly*rakepercent/100;
    var rakebackYearly  = totalYearly*rakepercent/100;
    

    
    $('#rake_daily').val('$'+Math.round(totalDaily));
    $('#rake_weekly').val('$'+Math.round(totalWeekly));
    $('#rake_monthly').val('$'+Math.round(totalMonthly));
    $('#rake_yearly').val('$'+Math.round(totalYearly));
    $('#rakeback_daily').val('$'+Math.round(rakebackDaily));
    $('#rakeback_weekly').val('$'+Math.round(rakebackWeekly));
    $('#rakeback_monthly').val('$'+Math.round(rakebackMonthly));
    $('#rakeback_yearly').val('$'+Math.round(rakebackYearly));
    return true;
}

$(document).ready(function() {
    
    //var for animation state
    rake_calc_closed = true;
    
    //build the nice forms
    $.NiceJForms.build();
    
    $('#calculate').click(function(){
        //do rake calculations        
        if(rakeback_calculate()){
            //do nice reveal animation :)
            if(rake_calc_closed){
                $("div.rake_results").animate({
                    height: 'show', opacity: 'show'
                }, "slow",rake_calc_closed = false);
                //change button to say RE calculate
                $('#calculate').val('Re-Calculate');
            }
        }
        //make sure the form doesnt submit
        return false;
    });
});