$(document).ready(function() 
{ 
    var tax;
    
    //for all drop down select list
    $(".select_dropdown").select2();
    
    // Initialize datepicker
    $('.input_date').datepicker();

    
    //form validation for market place and source field
    $("#report_entry_form").submit(function()
    {
       if($("#market_place").val() == 0)
       {
           alert('Please Select a Market Place.');
           return false;
       }
       else
       {
            if($("#source").val() == 0)
            {
                alert('Please Select a Source.');
                return false;
            }   
       }
    });    
    
    
    //for adding commission rate
    if($('#market_place').val() == 0)
    {
       $('#commission_rate').html('');
    }
    else
    {
       $('#commission_rate').html(market_places[$('#market_place').val()]+'%');
    }
    
    
    
    //for adding tax field
    $('#market_place').change(function()
    {    
        if($(this).val() != 0)
        {
            $('#commission_rate').html(market_places[$(this).val()]+'%');
            $("#commission").val((((parseFloat($('#selling_price').val()))* (parseFloat($("#commission_rate").html())))/100).toFixed(2));        
            $("#tax").val(tax); 
        }             
        if($(this).val() == 1 || $(this).val() == 7 || $(this).val() == 8 || $(this).val() == 9)
        {
            if($("#tax_text").length == 0)
            {
               $('#tax_div').hide().append('<label class="col-sm-2 control-label"><a id="tax_text" href="">Tax</a></label>').slideDown('slow');
            }
        }
        else
        {
           $('#tax_div').slideUp('slow').html('');  
        }
    });    
   
   
   
   //adding tax field
   $( "body" ).on( "click", "#tax_text", function() 
   {
        if($("#tax").length == 0)
        {
            tax = (parseFloat($("#selling_price").val())*8.75/100).toFixed(2);
            $("#tax_div").append('<div id="tax_html"><div class="col-sm-4"><input type="number" id="tax" readonly="readonly" value="'+tax+'" class="form-control" name="tax" required="required" step ="any" min=0/></div><div>8.75%</div><div class="col-sm-4"></div></div>');    
            return false;
        }
        else
        {
            $("#tax_html").remove();
            return false;
        }
   });   
   
   
   //$('#bar_chart').hide(); 
   
   
   //calculate tax and place tax in tax field
   $("#selling_price").keyup(function()
   {
       $("#tax").val((parseFloat($(this).val())*8.75/100).toFixed(2));  
       $("#commission").val((((parseFloat($(this).val()))* (parseFloat($("#commission_rate").html())))/100).toFixed(2));        
       tax = (parseFloat($(this).val())*8.75/100).toFixed(2);
   });      
   
   
   $('.report_table_div').hide(); 
   
   $('.tr_class').hide(); 
   
   $('.total_report_head').click(function(event)
   {
        var current_id = $(this).attr('id');
        
        if($('#report_table_'+current_id).is(":hidden"))
        {
            $('#tr_class_id_'+current_id).show();
            $('#report_table_'+current_id).slideDown("fast");  
            event.preventDefault();
        }
        else
        {
            $('#report_table_'+current_id).slideUp("slow",function()
            {
                $('#tr_class_id_'+current_id).hide();
            }); 
            event.preventDefault();            
        }
   });   
});