function NumberFormat(b) {
  ; (b = b?.toString()?.replace(/[,]+/gi, '')),
    (b = b?.toString()?.replace(/₹/gi, ''))
  var c = ''
  b?.indexOf('.') > 0 && (c = b?.substring(b?.indexOf('.'), b?.length)),
    (b = Math.floor(b)),
    (b = b?.toString())
  var f = b?.substring(b?.length - 3),
    d = b?.substring(0, b?.length - 3)
  '' != d && (f = ',' + f)
  var a = $.trim(d.replace(/\B(?=(\d{2})+(?!\d))/g, ',') + f + c)
  return a
}
function isNumberKey(a, b) {
  var c = a.which ? a.which : a.keyCode
  return (
    (46 !== c || ('loanAmount' !== b && 'loanYears' !== b)) &&
    (8 == a.which ||
      0 == a.which ||
      !(a.which < 48 || 57 < a.which) ||
      46 == c)
  )
}
function validate(f, h) {
  var k = f,
    c = k.attr('min'),
    j = k.attr('max'),
    b = h,
    g = k.data('index'),
    d = $('div.errorMsg[data-index="' + g + '"]')
  return (
    '1' == g && $('#loanAmount_1').val(b),
    isNaN(b) ||
      (('1' == g || '3' == g) && -1 < b.toString().indexOf('.')) ||
      b < parseFloat(c) ||
      b > parseFloat(j)
      ? (d.addClass('tooltip'), k.addClass('error'), 0)
      : (d.removeClass('tooltip'), k.removeClass('error'), 1)
  )
}
function EMI(v, k, f) {
  var q,
    c,
    h,
    b,
    j,
    p,
    s = validateIntrestRate(),
    g = validateYears()
  formatter('loanAmount'),
    validateLoanAmount() &&
    s &&
    g &&
    ((q =
      document
        .getElementById('loanAmount_1')
        .value.toString()
        .replace(/[,|\s|₹]+/gi, '') || 0),
      (c = document.getElementById('interestRate').value),
      (h = 12 * document.getElementById('loanYears').value),
      0 < q.toString().length &&
      (document.getElementById('principal').innerHTML =
        '₹ ' + NumberFormat(Math.round(q)).toString()),
      0 < q.toString().length &&
      0 < c.toString().length &&
      0 < h.toString().length &&
      0 != h.toString() &&
      ((p = (j = (b = CalculateEMI(q, c, h)) * h) - q),
        (document.getElementById('monthlyRepayment').innerHTML =
          '₹ ' + NumberFormat(b.toFixed(2)).toString()),
        (tI = NumberFormat(p).toString()),
        (tI1 =
          -1 < tI.indexOf('.') ? tI.slice(0, tI.indexOf('.') + 5) : tI),
        (tAP = NumberFormat(j).toString()),
        (tAP1 =
          -1 < tAP.indexOf('.')
            ? tAP.slice(0, tAP.indexOf('.') + 5)
            : tAP),
        (document.getElementById('totalInterest').innerHTML =
          '₹ ' + tI.substr(0, tI.indexOf('.'))),
        (document.getElementById('totalAmountPayable').innerHTML =
          '₹ ' + tAP.substr(0, tI.indexOf('.') + 2)),
        (document.getElementById('hdtotalInterest').innerHTML =
          tI1.replace(/,/gi, '')),
        (document.getElementById('hdtotalAmountPayable').innerHTML =
          tAP1.replace(/,/gi, '')),
        ChartRefresh(Math.round(p), Math.round(q))))
}
function validateIntrestRate() {
  var a = parseFloat(document.getElementById('interestRate').value),
    b = a.toString().split('.')
  return (
    !isNaN(a) &&
    a < 10 &&
    1 < b.length &&
    2 < b[1].length &&
    (document.getElementById('interestRate').value = parseFloat(
      a.toFixed(2)
    )),
    !!(R_loan = rangeValidator(
      document.getElementById('interestRate'),
      6,
      15
    ))
  )
}
function rangeValidator(c, d, f) {
  if (isNaN($.trim(c.value))) {
    return !1
  }

  if (0 === $.trim(c.value).length) {
    return !0
  }

  var b = parseFloat($.trim(c.value))
  return !isNaN(b) && !(b < parseFloat(d) || b > parseFloat(f))
}
function validateYears() {
  return !!(N_loan = rangeValidator(
    document.getElementById('loanYears'),
    1,
    30
  ))
}
function formatter(a) {
  if (
    isNaN($.trim(document.getElementById(a).value)) &&
    !validateLoanAmount()
  ) {
    return rangeValidator(document.getElementById(a), 0, 50000000)
  }

  null != document.getElementById(a).value
    ? ((document.getElementById(a).value =
      '₹' + NumberFormat($.trim(document.getElementById(a).value))),
      (x = $.trim(document.getElementById(a).value.toString()).replace(
        /[,]+/gi,
        ''
      )),
      (x = x.toString().replace(/₹/gi, '')),
      (document.getElementById(a + '_1').value = x))
    : (document.getElementById(a + '_1').value = 0)
}
function validateLoanAmount() {
  return !!(P_loan = rangeValidator(
    document.getElementById('loanAmount_1'),
    0,
    50000000
  ))
}
function CalculateEMI(b, c, f) {
  var d = Math.pow(1 + c / 100 / 12, f),
    a = (b * (c / 100 / 12) * d) / (d - 1)
  return a
}
function ChartRefresh(a, b) {
  // chart.load({
  //   columns: [
  //     ['Interest Payable', a],
  //     ['Principal', b],
  //   ],
  // })
}
$(function () {
  $('.calc-tabs a').on('click', function () {
    $(this).addClass('active').siblings().removeClass('active')
    $('.calc-content .' + $(this).attr('data-rel'))
      .addClass('active')
      .siblings()
      .removeClass('active')
  })
  $('.glossary-tab').on('click', function () {
    $('.glossary-tab').removeClass('selected')
    $(this).addClass('selected')
    selectedClass = $(this).attr('data-rel')
    $('#glossary-content').fadeTo(100, 0.1)
    $('#glossary-content li')
      .not('.' + selectedClass)
      .fadeOut()
      .removeClass('scale-anm')
    setTimeout(function () {
      $('.' + selectedClass)
        .fadeIn()
        .addClass('scale-anm')
      $('#glossary-content').fadeTo(300, 1)
    }, 300)
  })

  /* popup */
  $('.popupLink').on('click', function () {
    $(this).addClass('active')
    currentID = $(this).attr('id')
    $('#imodal-' + currentID).show()
    $('body').addClass('no-scroll')
  })

  $('.imodal-close').on('click', function () {
    $('.imodal-popup').hide()
    $('body').removeClass('no-scroll')
  })

  $('.scroll-to-section').on('click', function (e) {
    e.preventDefault();   
      var hash = this.hash;
      // console.log('🚀 ~ file: homepage.js ~ line 45 ~ e', e)
      var elem = $(this).attr('href'), elems = '#' + elem;
      var stickyElems = 180;
      
      var elemOffset = $(elems).offset().top - stickyElems || 0
      // alert(elemOffset);
      // $('html,body').animate({ scrollTop: elemOffset }, 500)
      // window.location.hash = hash;
      $('html, body').animate({scrollTop: elemOffset}, 500, function() {
        // Add hash (#) to URL when done scrolling (default click behavior)
        window.location.hash = hash;
      });
   // End if
  })
  $('.select-box__current').on('click', function () {
    if ($('.select-box__current').hasClass('active')) {
      $('.select-box__current').removeClass('active')
    } else {
      $('.select-box__current').addClass('active')
    }
  })

  $('.sliderRangeEligibility').each(function () {
    var a = $(this)
    a.rangeslider({
      polyfill: !1,
      onSlide: function (c, d) {
        // console.log(a.attr('data-index'))
        // var b = $('input.slider_text_eligibility[data-index="' + a.data('index') + '"]')
        // b.val(d), validate(b, f);
        var f = 0
        1 == a.data('index') || 12 == a.data('index') || 9 == a.data('index') || 13 == a.data('index') || 15 == a.data('index')
          ? ($(this).val(d), (f = d), (d = '₹ ' + NumberFormat(d)))
          : (f = a.val())
        var b = $('input.slider_text_eligibility[data-index="' + a.data('index') + '"]')
        b.val(d), validate(b, f)
      },
      onSlideEnd: function onSlideEnd(b, c) {
        calculateEligibility()
      },
    })
  })
  $('.slider_text_eligibility').each(function () {
    $(this).on('change', function () {
      var a,
        b,
        c = $(this)
      if (c.val() == '') {
        c.val(0)
      }
      0 != validate(c, c.val()) && ((a = c.data('index')), (b = c.val()), $('input.sliderRangeEligibility[data-index="' + a + '"]').val(b).change(),
        calculateEligibility())
    })
  })
  $('.sliderRangeAffordability').each(function () {
    var a = $(this)
    a.rangeslider({
      polyfill: !1,
      onSlide: function (c, d) {
        var f = 0
        1 == a.data('index') || 12 == a.data('index') || 9 == a.data('index')
          ? ($(this).val(d), (f = d), (d = '₹ ' + NumberFormat(d)))
          : (f = a.val())
        var b = $('input.sliderTextAffordability[data-index="' + a.data('index') + '"]')
        b.val(d), validate(b, f)
      },
      onSlideEnd: function onSlideEnd(b, c) {
        calculateAffordibility()
      },
    })
  })
  $('.sliderTextAffordability').each(function () {
    $(this).on('change', function () {
      var a,
        b,
        c = $(this)
      if (c.val() == '') {
        c.val(0)
      }
      0 != validate(c, c.val()) &&
        ((a = c.data('index')),
          (b = c.val()),
          $('input.sliderRangeAffordability[data-index="' + a + '"]')
            .val(b)
            .change(),
          calculateAffordibility()
        )
    })
  })

  // OurHomeLoan Slider

  $('.ourhomeslider').slick('destroy').slick({
    slidesToShow: 4.5,
    slidesToScroll: 1,
    speed: 900,
    rows: 0,
    infinite: false,
    arrows: true,
    dots: false,
    prevArrow: $('.ourhome--arrows .arrow-left'),
    nextArrow: $('.ourhome--arrows .arrow-right'),
    responsive: [
      {
        breakpoint: 769,
        settings: {
          slidesToShow: 3.5,
          slidesToScroll: 1,
          rows: 0,
        },
      },
      {
        breakpoint: 550,
        settings: {
          slidesToShow: 1.5,
          slidesToScroll: 1,
          rows: 0,
        },
      },
    ],
  })
  $('.sticky-scroll').slick({
    // slidesToShow: 5.5,
    slidesToShow: 3,
    slidesToScroll: 1,
    speed: 900,
    rows: 0,
    infinite: false,
    arrows: true,
    dots: false,
    variableWidth: true,
    prevArrow: $('.sticky-arrows .arrow-left'),
    nextArrow: $('.sticky-arrows .arrow-right'),
    responsive: [
      {
        breakpoint: 769,
        settings: "unslick",
      },
    ],
  })

  // Header Slider
  // $('#gmi_Elegibility').on('focus', function () {
  //   $(this), $(this).val($.trim($('#gmi_ElegibilityVal').val()) || '0')
  // });
  // $('#gmi_Elegibility').on('blur', function () {
  //   var a = $(this)
  //   isNaN(a.val())
  //     ? ($('#gmi_ElegibilityVal').val(a.val().replace(/[,|\s|₹]+/gi, '')))
  //     : (a.val('₹' + NumberFormat(a.val())))
  // })
  // if ($('#gmi_Elegibility').length) {
  //   $('#gmi_Elegibility').val('₹' + NumberFormat($('#gmi_Elegibility').val()))
  // }

  // $('#otherEMI_Elegibility').on('focus', function () {
  //   $(this), $(this).val($.trim($('#otherEMI_ElegibilityVal').val()) || '0')
  // });
  // if ($('#otherEMI_Elegibility').length) {
  //   $('#otherEMI_Elegibility').val('₹' + NumberFormat($('#otherEMI_Elegibility').val()));
  // }
  // $('#otherEMI_Elegibility').on('blur', function () {
  //   var a = $(this)
  //   isNaN(a.val())
  //     ? $('#otherEMI_ElegibilityVal').val(a.val().replace(/[,|\s|₹]+/gi, ''))
  //     : a.val('₹' + NumberFormat(a.val()))
  // })
  // $('#downPayment').on('focus', function () {
  //   $(this), $(this).val($.trim($('#downPaymentVal').val()) || '0')
  // });
  // if ($('#otherEMI_Elegibility').length) {
  //   $('#downPayment').val('₹' + NumberFormat($('#downPayment').val()));
  // }
  // $('#downPayment').on('blur', function () {
  //   var a = $(this)
  //   isNaN(a.val())
  //     ? $('#downPaymentVal').val(a.val().replace(/[,|\s|₹]+/gi, ''))
  //     : a.val('₹' + NumberFormat(a.val()))
  // });
  // $('#otherEMI').on('focus', function () {
  //   $(this), $(this).val($.trim($('#otherEMIVal').val()) || '0')
  // });
  // if ($('#otherEMI_Elegibility').length) {
  //   $('#otherEMI').val('₹' + NumberFormat($('#otherEMI').val()));
  // }
  // $('#otherEMI').on('blur', function () {
  //   var a = $(this)
  //   isNaN(a.val())
  //     ? $('#otherEMIVal').val(a.val().replace(/[,|\s|₹]+/gi, ''))
  //     : a.val('₹' + NumberFormat(a.val()))
  // })
  // $('#gmi').on('focus', function () {
  //   $(this), $(this).val($.trim($('#gmiVal').val()) || '0')
  // });
  // if ($('#gmi').length) {
  //   $('#gmi').val('₹' + NumberFormat($('#gmi').val()));
  // }
  // $('#gmi').on('blur', function () {
  //   var a = $(this)
  //   isNaN(a.val())
  //     ? $('#gmiVal').val(a.val().replace(/[,|\s|₹]+/gi, ''))
  //     : a.val('₹' + NumberFormat(a.val()))
  // })

  //filter tab

  var selectedClass = ''
  $('.fiter-tab-btn-v1').on('click', function () {
    $(this).addClass('selected').siblings().removeClass('selected')
    selectedClass = $(this).attr('data-rel')
    $('#exploreFilter').fadeTo(100, 0.1);
    $('#exploreFilter li').attr('data-index', '');
    $('.' + selectedClass).each(function (index) {
      $(this).attr('data-index', index + 1)
    });
    $('#exploreFilter li')
      .not('.' + selectedClass)
      .fadeOut()
      .removeClass('scale-anm');
    setTimeout(function () {

      $('.' + selectedClass)
        .fadeIn()
        .addClass('scale-anm')
      $('#exploreFilter').fadeTo(300, 1)

    }, 300)
  });

  $('#exploreFilter li').each(function (index) {
    $(this).attr('data-index', index + 1)
  });
  $(document).mouseup(function (e) {
    var container = $(".select-box__current");
    // if the target of the click isn't the container nor a descendant of the container
    if (!container.is(e.target) && container.has(e.target).length === 0) {
      $('.select-box__current').removeClass('active')
    }
  });
})

$(window).on('scroll', function () {
  if ($(this).scrollTop() > 818 && !$('#bannerscrollBar').hasClass('sticky')) {
    $('#bannerscrollBar').addClass('sticky');
  }
  if ($(this).scrollTop() < 819 && $('#bannerscrollBar').hasClass('sticky')) {
    $('#bannerscrollBar').removeClass('sticky');
  }
}).on('load', function () {
  if (window.location.hash) {
    var elem = window.location.hash;
    var stickyElems = 180;
    var elemOffset = $(elem).offset().top - stickyElems || 0
    // alert(elemOffset);
    $('html,body').animate({ scrollTop: elemOffset }, 500)
  }
});
