/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$(document).ready(function () {

    // $( ".form-group" ).removeClass( ".has-error" );
    var base_url = $(".navbar-nav").find('a').attr('href');
    console.log("base_url" + base_url);

    $('.venobox').venobox();


    $("#order_form").submit(function (e) {
        if ($('input:checkbox:checked', '#pricing-table tbody').length < 1) {
            //var error='Please select atleast one record to proceed.';
            $("#error").text('Please Select Atleast One Module.');
            $("#alert").show();
            e.preventDefault();
        }
        var user_error = '';
        $('input:checkbox:checked', '#pricing-table tbody').each(function (i, obj) {
            ioc = $(obj).attr('id').split('-');
            module = ioc[1];
            var module_name = $("#module_name-" + module).text();
            console.log("module_name" + module_name);
            if ($('#' + module + '-users').val() == '') {
                user_error += 'Add Users For ' + module_name + '<br>';

                $("#error").html(user_error);
                $("#alert").show();
                e.preventDefault();
            }
        });



    });


    // ajax call for monthly yearly rate //
    //$("#monthly").change(function(){
    $('input[type=radio][name=sample1]').value;
    $('input[type=radio][name=sample1]').on('change', function () {

        //'http://localhost/ciportal/pricingController/subscription_type'
        //alert(this.checked)
        // var base_url = '<?php echo base_url(); ?>';
        var subscription_type = this.value;
        if (subscription_type) {
            $.ajax({
                type: "post",
                dataType: 'json',
                url: base_url + 'pricingController/subscription_type',
                data: {
                    'subscription_type': subscription_type
                },

                success: function (result) {
                    //calculateTotal();

                    console.log(result);
                    // var data =JSON.stringify(result);  
                    $.each(result, function (key, value) {
                        //console.log(key + ": " + value.id);
                        // calculateTotal();
                        if (subscription_type == 'monthly') {
                            //$('#wf-per-user1').val('fhdsfkdsh');
                            $("#pricing-table").find("[data-mid='" + value.id + "']").val(value.monthly_rate);
                            if (!(value.module_code == "pm")) {
                                $("input[type=number][id=" + value.module_code + "-monthly]").val('1').attr('step', 1).attr('min', '1');
                            } else {
                                $("input[type=number][id=" + value.module_code + "-monthly]").val('12');
                                $("input[type=number][id=" + value.module_code + "-monthly]").val('12').attr('step', 12).attr('min', '12');
                            }
                        } else {
                            $("#pricing-table").find("[data-mid='" + value.id + "']").val(value.yearly_rate);
                            $("input[type=number][id=" + value.module_code + "-monthly]").val('12').attr('step', 12).attr('min', '12');
                            //$("input[type=number][id=" + value.module_code + "-monthly]").prop('step','12');

                        }
                    });
                    $(".nouser").trigger('input');
                    calculateTotal();

                }

            });
        }


    });






    $(".nouser").on('input', function () {

        var ioe = $(this).attr('id').split("-");
        var module = ioe[0];
        var nousers = $('#' + module + '-users').val();
        if (nousers > 0) {
            var peruser = $('#' + module + '-per-user').val();
            var nomonths = $('#' + module + '-monthly').val();
            var cost = nousers * peruser * nomonths;
            $('#' + module + '-yearly').val(cost);
        }

        if (nousers > 0) {

            $('#select-' + module).prop('checked', true).change();
        } else {
            $('#select-' + module).prop('checked', false).change();
        }

    });

    $('.nomonth').on('input', function () {
        $(".nouser").trigger('input');
    })

    $(".nouser").trigger('input');
    var ioc;

    $("input[type='checkbox']", '#pricing-table tbody').on('change', function () {


        if (this.checked == false) { //if this item is unchecked
            $("#select-all")[0].checked = false; //change "select all" checked status to false
        }
        if ($('input:checkbox:checked', '#pricing-table tbody').length == $('input:checkbox', '#pricing-table tbody').length) {
            $("#select-all")[0].checked = true; //change "select all" checked status to true
        }


        calculateTotal();





    });


    function calculateTotal() {

        mtotal = 0;
        ytotal = 0;
        $('input:checkbox:checked', '#pricing-table tbody').each(function (i, obj) {
            ioc = $(obj).attr('id').split('-');
            module = ioc[1];

            if (module != 'pm') {
                mtotal += parseInt($('#' + module + '-monthly').val());
            }
            ytotal += parseInt($('#' + module + '-yearly').val());
            console.log(module + ':' + mtotal);
        });
        let taxTotal = ytotal * (15 / 100);
        let grossTotal = ytotal + taxTotal;

        //$('#monthly-total').val(mtotal);
        $('#yearly-total').val(ytotal.toFixed(2));
        $('#total-tax').val(taxTotal.toFixed(2));
        $('#gross-total').val(grossTotal.toFixed(2));

    }


    $('#select-all').on('change', function () {

        var status = this.checked;
        var checkboxes = $("input[type='checkbox']", '#pricing-table tbody')
        $(checkboxes).each(function () { //iterate all listed checkbox items
            this.checked = status; //change ".checkbox" checked status
        });

        /* var checkboxes = $("input[type='checkbox']", '#pricing-table tbody');
         
         checkboxes.prop('checked', !checkboxes.prop("checked"));*/
        calculateTotal();
    });

    $('#country').on('change', function () {
        var countryID = $(this).val();
        if (countryID) {
            $.ajax({
                type: 'POST',
                url: 'ajaxData.php',
                data: 'country_id=' + countryID,
                success: function (html) {
                    $('#state').html(html);
                    $('#city').html('<option value="">Select state first</option>');
                }
            });
        } else {
            $('#state').html('<option value="">Select country first</option>');
            $('#city').html('<option value="">Select state first</option>');
        }
    });

    $('#state').on('change', function () {
        var stateID = $(this).val();
        if (stateID) {
            $.ajax({
                type: 'POST',
                url: 'ajaxData.php',
                data: 'state_id=' + stateID,
                success: function (html) {
                    $('#city').html(html);
                }
            });
        } else {
            $('#city').html('<option value="">Select state first</option>');
        }
    });






});
$(document).ready(function () {
    var valFooter = parseInt($("#slidesToShow").val());
    //console.log(valFooter);
    $.material.init();
    $('.feature-icons').slick({

        slidesToShow: valFooter,
        slidesToScroll: 1,
        infinite: false,
        nextArrow: '<i class="fa fa-arrow-right "></i>',
        prevArrow: '<i class="fa fa-arrow-left"></i>',
        responsive: [{
                breakpoint: 1024,
                settings: {
                    slidesToShow: 4,
                    slidesToScroll: 1,

                }
            }, {
                breakpoint: 600,
                settings: {
                    slidesToShow: 3,
                    slidesToScroll: 1
                }
            }, {
                breakpoint: 480,
                settings: {
                    slidesToShow: 2,
                    slidesToScroll: 1
                }
            }
        ]

    });

    $('.screenshot-carousel').slick({

        slidesToShow: 3,
        slidesToScroll: 1,
        infinite: false,
        responsive: [{
                breakpoint: 1024,
                settings: {
                    slidesToShow: 3,
                    slidesToScroll: 1,

                }
            }, {
                breakpoint: 600,
                settings: {
                    slidesToShow: 2,
                    slidesToScroll: 1
                }
            }, {
                breakpoint: 480,
                settings: {
                    slidesToShow: 2,
                    slidesToScroll: 1
                }
            }
        ]

    });
    $('[data-toggle=tab]').click(function () {

        $(this).parent().toggleClass('active').siblings().removeClass('active');

    })
});

/* code added on 29/03/2017  for contactus and scedule page select all modules checkbox */
$(document).ready(function () {
    $('.datepicker').datepicker({

    });
    $("#select-all-module").on('change', function () {

        var status = this.checked;
        //console.log("status"+status);
        var checkboxes = $("input[type='checkbox']");
        $(checkboxes).each(function () { //iterate all listed checkbox items
            this.checked = status; //change ".checkbox" checked status
        });



    });
    $(".checkbox-module").on('change', function () {
        // alert("hi");
        // var k=$('checkbox-module:checked').length;
        //console.log(k);

        if (this.checked == false) { //if this item is unchecked
            $("#select-all-module")[0].checked = false; //change "select all" checked status to false
        }
        if ($('.checkbox-module:checkbox:checked').length == $('.checkbox-module:checkbox').length) {
            $("#select-all-module")[0].checked = true; //change "select all" checked status to true
        }

    });

});
/* ends here */

/* For Carousel starts here */
$('.carousel .item').each(function(){
  var next = $(this).next();
  if (!next.length) {
    next = $(this).siblings(':first');
  }
  next.children(':first-child').clone().appendTo($(this));
  
  if (next.next().length>0) {
    next.next().children(':first-child').clone().appendTo($(this));
  }
  else {
  	$(this).siblings(':first').children(':first-child').clone().appendTo($(this));
  }
});
