$(document).ready(function(){    
 
    $('.select-tags').select2();

    // Menu Class Active Start
    $('.submenu li').each(function () {        
        var location = window.location.href;
        location = location.split('?')[0];
        var newLocation = $(this).find('a:first').attr('href');      
        var modifiedLocation = location.substr(location.length - newLocation.length);
    
        if (newLocation == modifiedLocation) {
        
            $(this).addClass('mm-active').siblings().removeClass('mm-active');
            $(this).find('a').addClass('mm-active').siblings().removeClass('mm-active');
            $(this).parent().parent().addClass("mm-active").siblings().removeClass('mm-active');
            return false;
        }
        return true;
    });
    // Menu Class Active End

    
$('#tour_request_expenseType').change(function () {
    var expenseTypeId = $(this);    
   
    // Request the neighborhoods of the selected city.
    $.ajax({
        url: $('#path_tour_request_expense_type').data("href"),
        type: "GET",
        dataType: "JSON",  
        data: {
            expenseTypeId: expenseTypeId.val()
        },         
        success: function (expensePurposes) {

            var expensePurposesSelect = $("#tour_request_expensePurpose");

            // Remove current options
            expensePurposesSelect.html('');
            
            // Empty value ...
            expensePurposesSelect.append('<option value> Select a Purpose of ' + expenseTypeId.find("option:selected").text() + ' ...</option>');
                        
            $.each(expensePurposes, function (key, expensePurpose) {
                expensePurposesSelect.append('<option value="' + expensePurpose.id + '">' + expensePurpose.name + '</option>');
            });
                     
        },
        error: function (error) {
            alert(error);
        }
    });

});


$('#addRow').click(function () {

    $("#claimHeader").show();

    var row_add = '<tr>' +
    '<td>' + $('#divCategory').html() + '</td>' +
    '<td>' + $('#divFromPlace').html() + '</td>' +
    '<td>' + $('#divStartingDate').html() + '</td>' +
    '<td> ' + $('#divToPlace').html() + ' </td>' +   
    '<td>' + $('#divEndingDate').html() + '</td>' +
    '<td>' + $('#divTravelMode').html() + '</td>' +
    '<td>' + $('#divAmountCurrency').html() + '</td>' +
    '<td> ' + $('#divAmount').html() + ' </td>' +      
    '<td> ' + $('#divRemarks').html() + ' </td>' +  
    '<td>  <input type="button" value="X" class="delete btn btn-danger btn-mini"></td></tr>';
  
    $('#claimDetails tbody').append(row_add);

});

$(document).on('click', '.delete', function () {

    if ($(this).parents('table tbody').find('tr').length > 1) {
        $(this).closest('tr').remove();

    } else {
        alert('Must have at least one row !');
    }

});


$('#save_data, #voucher_createNew').click(function () {
    $("#save_data, #voucher_createNew").prop('disabled', true);

    // Creating Voucher Json Object
    var claimObj = { "StartingDate": "", "EndingDate": "", "TourDetails": "" };

    // Set Voucher Value
    claimObj.StartingDate = $("#tour_claim_startingDate").val();
    claimObj.EndingDate = $("#tour_claim_endingDate").val();
    claimObj.TourDetails = $("#tour_claim_tourDetails").val();
    //voucherObj.CompanyId = $("#voucher_voucherdate").val();

    // var array_data_file = {};
    var category = new Array();
    var fromPlace = new Array();
    var startingDate = new Array();
    var toPlace = new Array();
    var endingDate = new Array();
    var travelMode = new Array();
    var amountCurrency = new Array();
    var amount = new Array();
    var remarks = new Array();

    // TODO: maybe you want to assign an unique id to your table element
    $('#claim table tbody tr').each(function () {
        var td = $('td', this);
        // alert($('#purchase_imeino', td).val());                       
        // Set SalesSub individual Value 
        category.push($('#tour_claim_details_category', td).val());
        fromPlace.push($('#tour_claim_details_fromPlace', td).val()); 
        startingDate.push($('#tour_claim_details_startingDate', td).val());
        toPlace.push($('#tour_claim_details_toPlace', td).val()); 
        endingDate.push($('#tour_claim_details_endingDate', td).val());
        travelMode.push($('#tour_claim_details_travelMode', td).val()); 
        amountCurrency.push($('#tour_claim_details_amountCurrency', td).val());
        amount.push($('#tour_claim_details_amount', td).val()); 
        remarks.push($('#tour_claim_details_remarks', td).val());

        // var debitAmount =  $('#voucher_details_debit', td).val();
        // if(debitAmount == '')
        //     debitAmount = 0;                                     
        // debit.push(debitAmount);
        // var creditAmount = $('#voucher_details_credit', td).val();            
        // if(creditAmount == '')
        // {
        //     creditAmount = 0;
        // }
        // credit.push(creditAmount);
       
    });
   
    var params = { 'claimObj':JSON.stringify(claimObj), 'category':JSON.stringify(category), 'fromPlace':JSON.stringify(fromPlace), 'startingDate':JSON.stringify(startingDate), 'toPlace':JSON.stringify(toPlace), 'endingDate':JSON.stringify(endingDate), 'travelMode':JSON.stringify(travelMode), 'amountCurrency':JSON.stringify(amountCurrency), 'amount':JSON.stringify(amount), 'remarks':JSON.stringify(remarks) }

    // Set 2: Ajax Post
    //var json = $.toJSON(voucherdetail);
    // Here i have used ajax post for saving/updating information
    
    $.ajax({
       url: $('#path_tour_claim_new_save').data("href"),
       data: params,
       type: "POST",
       dataType: 'json',           
       success: function (data) {
            if(data.success)
            {
                window.location.href = $('#path_tour_claim_index').data("href");
            }
            else
            {
                $("#save_data, #voucher_createNew").prop('disabled', false);
                alert(data.responseText);
            }
        
          // alert(data.redirectTo);
                     
       },
       error: function (xhr, error) {
         // alert('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
         $("#save_data, #voucher_createNew").prop('disabled', false);
         alert(error);            
       }
      

      });


});




});