$(document).ready(function() {
  new WOW().init();
  var homephoneplanprice = 32.95;
  var homephoneplanmincost = 819.80;
  var pendantplanprice = 0;
  var pendantmincost = 0;
  var intcallplanprice = 0;
  var totalplanprice = 0;
  var totalplanmincost = 0;

  function pendantchoiceprice(isselected) {
    if (isselected === 1) {
      pendantplanprice = 4;
      pendantmincost = 96;
      $('.field_is_pendant_selected').find('input').val("yes");
      $('.field_pendant_plan_cost').find('input').val("$" + pendantplanprice + " per month");
      $('.field_pendant_plan_duration').find('input').val("24 Months");
      $('.field_pendant_plan_minimum_cost').find('input').val("$" + pendantmincost);
    } else {
      pendantplanprice = 0;
      pendantmincost = 0;
      $('.field_is_pendant_selected').find('input').val("No");
      $('.field_pendant_plan_cost').find('input').val("NA");
      $('.field_pendant_plan_duration').find('input').val("NA");
      $('.field_pendant_plan_minimum_cost').find('input').val("NA");
    }
  }

  var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth() + 1; //January is 0!
  var yyyy = today.getFullYear();
  if (dd < 10) {
    dd = '0' + dd
  }
  if (mm < 10) {
    mm = '0' + mm
  }

  today = yyyy + '-' + mm + '-' + dd;
  document.getElementById("dob").setAttribute("max", today);

  function infosummary(id, formclass, value) {
    id.text(value);
    formclass.find('input').val(value);
  }

  function initialload() {
    $('.field_existing_telechoice_customer').find('input').val('No');
    $('.field_telechoice_account_number').find('input').val('NA');
  }


  function homephoneplanform(plancost, planduration, planmincost) {
    $('.field_homephone_plan_title').find('input').val(plancost + " " + planduration + " Months Plan");
    $('.field_homephone_plan_cost').find('input').val(plancost + " per month");
    $('.field_homephone_plan_duration').find('input').val(planduration + " Months");
    $('.field_homephone_minimum_cost').find('input').val(planmincost);
  }

  function isOnScreen(element) {
    var elementTop = element.offset().top();
    var elementBottom = elementTop + $(this).outerHeight();
    var viewportTop = $(window).scrollTop();
    var viewportBottom = viewportTop + $(window).height();
    return elementBottom > viewportTop && elementTop < viewportBottom;
  };
  window.onscroll = function() {
    if (isOnScreen($('.homephone-intro-section'))) {
      var scrolled = $(window).scrollTop();
      $('.homephone-intro-text-div').css("transform", "translateY(" + scrolled / 10 + "px)");
      $('.homephone-full-image-div').css("transform", "translateY(" + scrolled / 10 + "px)");
    }

  };

  function totalpricedisplay() {
    totalplanprice = homephoneplanprice + pendantplanprice + intcallplanprice;
    totalplanmincost = homephoneplanmincost + pendantmincost;
    $("#total-plan-cost").text(totalplanprice);
    $("#total-min-cost").text(totalplanmincost.toFixed(2));
    $('.field_total_plan_cost').find('input').val("$" + totalplanprice + " per month");
    $('.field_total_plan_duration').find('input').val("24 Months");
    $('.field_total_plan_minimum_cost').find('input').val("$" + totalplanmincost);
  }
  $("#confirm-plan-btn").click(function() {
    totalpricedisplay("$32.95", "24", "$819.80");
    $('#homefone-plan-div').addClass('d-none');
    $('#homephone-plan-summary-div').removeClass('d-none');
    $('#homephone-pendant-select-div').removeClass('d-none');
    $('#pendant-summary-div').addClass('visibility-hidden');
    $('#pendant-selected-sum-div').addClass('d-none');
    $('#pendant-not-selected-sum-div').addClass('d-none');
    $('#homephone-int-call-select-div').addClass('d-none');
    $('#int-call-summ-div').addClass('visibility-hidden');
    $('#int-call-not-selected-div').addClass('d-none');
    $('#int-call-selected-div').addClass('d-none');
    $('#homephone-customer-form-div').addClass('d-none');
    $('#homephone-customer-form-div').removeClass('d-flex');
    homephoneplanform("$32.95", "24", "$819.80");
  });
  $("#add-pendant-div-btn").click(function() {
    pendantchoiceprice(1);
    totalpricedisplay();
    $('#homefone-plan-div').addClass('d-none');
    $('#homephone-plan-summary-div').removeClass('d-none');
    $('#homephone-pendant-select-div').addClass('d-none');
    $('#pendant-summary-div').removeClass('visibility-hidden');
    $('#pendant-selected-sum-div').removeClass('d-none');
    $('#pendant-not-selected-sum-div').addClass('d-none');
    $('#homephone-int-call-select-div').removeClass('d-none');
    $('#int-call-summ-div').addClass('visibility-hidden');
    $('#int-call-not-selected-div').addClass('d-none');
    $('#int-call-selected-div').addClass('d-none');
    $('#homephone-customer-form-div').addClass('d-none');
    $('#homephone-customer-form-div').removeClass('d-flex');
  });
  $("#skip-pendant-div-btn").click(function() {
    pendantchoiceprice(0);
    totalpricedisplay();
    $('#homefone-plan-div').addClass('d-none');
    $('#homephone-plan-summary-div').removeClass('d-none');
    $('#homephone-pendant-select-div').addClass('d-none');
    $('#pendant-summary-div').removeClass('visibility-hidden');
    $('#pendant-selected-sum-div').addClass('d-none');
    $('#pendant-not-selected-sum-div').removeClass('d-none');
    $('#homephone-int-call-select-div').removeClass('d-none');
    $('#int-call-summ-div').addClass('visibility-hidden');
    $('#int-call-not-selected-div').addClass('d-none');
    $('#int-call-selected-div').addClass('d-none');
    $('#homephone-customer-form-div').addClass('d-none');
    $('#homephone-customer-form-div').removeClass('d-flex');
  });
  $("#int-call-next-btn").click(function() {
    $('#homefone-plan-div').addClass('d-none');
    $('#homephone-plan-summary-div').removeClass('d-none');
    $('#homephone-pendant-select-div').addClass('d-none');
    $('#homephone-int-call-select-div').addClass('d-none');
    $('#int-call-summ-div').removeClass('visibility-hidden');
    if ($(this).hasClass('int-call-plan-selected')) {
      $('#int-call-not-selected-div').addClass('d-none');
      $('#int-call-selected-div').removeClass('d-none');
    } else {
      $('#int-call-not-selected-div').removeClass('d-none');
      $('#int-call-selected-div').addClass('d-none');
      $('.field_is_international_call_plan_added').find('input').val("No");
      $('.field_international_call_plan_cost').find('input').val("NA");
      $('.field_international_call_plan_description').find('input').val("NA");
    }
    $('#homephone-customer-form-div').removeClass('d-none');
    $('#homephone-customer-form-div').addClass('d-flex');
  });
  $("#add-pendant-btn").click(function() {
    $('#pendant-selected-sum-div').removeClass('d-none');
    $('#pendant-not-selected-sum-div').addClass('d-none');
    pendantchoiceprice(1);
    totalpricedisplay();
  });
  $("#remove-pendant-btn").click(function() {
    $('#pendant-selected-sum-div').addClass('d-none');
    $('#pendant-not-selected-sum-div').removeClass('d-none');
    pendantchoiceprice(0);
    totalpricedisplay();
  });
  $("#add-int-plan-btn").click(function() {
    $('#homefone-plan-div').addClass('d-none');
    $('#homephone-pendant-select-div').addClass('d-none');
    $('#homephone-int-call-select-div').removeClass('d-none');
    $('#homephone-customer-form-div').addClass('d-none');
    $('#homephone-customer-form-div').removeClass('d-flex');
  });
  $("#change-int-plan-btn").click(function() {
    $('#homefone-plan-div').addClass('d-none');
    $('#homephone-pendant-select-div').addClass('d-none');
    $('#homephone-int-call-select-div').removeClass('d-none');
    $('#homephone-customer-form-div').addClass('d-none');
    $('#homephone-customer-form-div').removeClass('d-flex');
  });
  var isanyintcallselected = 0;

  function intcalldivselect(selecteddiv, planprice, plandesc) {
    if (selecteddiv.hasClass('plan-selected')) {
      intcallplanprice = 0;
      $('.field_is_international_call_plan_added').find('input').val("No");
      $('.field_international_call_plan_cost').find('input').val("NA");
      $('.field_international_call_plan_description').find('input').val("NA");
      totalpricedisplay();
      selecteddiv.removeClass('bg-primary');
      selecteddiv.removeClass('plan-selected');
      $("#int-call-next-btn").removeClass('int-call-plan-selected');
      isanyintcallselected = 0;
      $('#int-call-summ-div').removeClass('visibility-hidden');
      $('#int-call-not-selected-div').removeClass('d-none');
      $('#int-call-selected-div').addClass('d-none');
    } else {
      intcallplanprice = planprice;
      $('.field_is_international_call_plan_added').find('input').val("Yes");
      $('.field_international_call_plan_cost').find('input').val("$" + intcallplanprice + " per month");
      $('.field_international_call_plan_description').find('input').val(plandesc);
      totalpricedisplay();
      $("#int-call-plan-price-in-summ").text("Plan Cost - $" + planprice + " per month");
      $("#int-call-plan-desc-in-summ").text(plandesc);
      $('#int-call-summ-div').removeClass('visibility-hidden');
      $('#int-call-not-selected-div').addClass('d-none');
      $('#int-call-selected-div').removeClass('d-none');
      if (isanyintcallselected === 0) {
        selecteddiv.addClass('bg-primary');
        selecteddiv.addClass('plan-selected');
        $("#int-call-next-btn").addClass('int-call-plan-selected');
        isanyintcallselected = 1;
      } else {
        $("#int-call-unlim-15-plan").removeClass('plan-selected bg-primary');
        $("#int-call-unlim-5-plan").removeClass('plan-selected bg-primary');
        $("#int-call-lim-5-plan").removeClass('plan-selected bg-primary');
        $("#int-call-lim-9-plan").removeClass('plan-selected bg-primary');
        $("#int-call-lim-15-plan").removeClass('plan-selected bg-primary');
        selecteddiv.addClass('plan-selected bg-primary');
      }
    }
  }

  $("#int-call-unlim-15-plan").click(function() {
    intcalldivselect($(this), 12, "Unlimited calls to 15 Countries inlcuding Bangladesh, China, India, NewZealand, USA, Canada, Hong Kong, Malaysia, Thailand, UK, Greece, Pakistan, Egypt, Singapore, Vietnam.");
  });
  $("#int-call-unlim-5-plan").click(function() {
    intcalldivselect($(this), 5, "Unlimited calls to 10 Countries including Bangladesh, China, India, NewZealand, USA, Canada, Hong Kong, Malaysia, Thailand, UK.", 5);
  });
  $("#int-call-lim-5-plan").click(function() {
    intcalldivselect($(this), 5, "$150 of calls to any country in the world.");
  });
  $("#int-call-lim-9-plan").click(function() {
    intcalldivselect($(this), 9, "$300 of calls to any country in the world.");
  });
  $("#int-call-lim-15-plan").click(function() {
    intcalldivselect($(this), 15, "$600 of calls to any country in the world.");
  });

  $("#submit-btn").click(function() {$(".homephone-summary-div").addClass('bg-dark');});
  $("#exist-tele-customer").click(function() {
    $('#account-div').removeClass('d-none');
    $('#tele-account-number-output-div').removeClass('d-none');
    $('#tele-account-number').attr('required', '');
    $('.field_existing_telechoice_customer').find('input').val('Yes');
  });
  $("#new-tele-customer").click(function() {
    $('#account-div').addClass('d-none');
    $('#tele-account-number-output-div').addClass('d-none');
    $('#tele-account-number').removeAttr("required");
    $('.field_existing_telechoice_customer').find('input').val('No');
    $('.field_telechoice_account_number').find('input').val('NA');
  });
  $("#employment-type").change(function() {
    if ($(this).val() == "Unemployed") {
      $("#company-details-div").addClass('d-none');
      $("#company-address-div").addClass('d-none');
      $("#Employement-details-output-div").addClass('d-none');
      $("#company-name").removeAttr("required");
      $('#company-cont-number').removeAttr("required");
      $('#company-address').removeAttr("required");
      $('.field_company_name').find('input').val('NA');
      $('.field_company_contact_number').find('input').val('NA');
      $('.field_company_address').find('input').val('NA');
    } else {
      $("#company-details-div").removeClass('d-none');
      $("#company-address-div").removeClass('d-none');
      $("#Employement-details-output-div").removeClass('d-none');
      $("#company-name").attr('required', '');
      $('#company-cont-number').attr('required', '');
      $('#company-address').attr('required', '');
    }
  });
  $("#service-details").change(function() {
    if ($(this).val() == "New Number") {
      $("#new-num-text").removeClass('d-none');
      $("#port-num-div").addClass('d-none');
      $("#service-provider-div").addClass('d-none');
      $("#service-details-output-div").addClass('d-none');
      $("#port-number").removeAttr("required");
      $('#service-provider').removeAttr("required");
      $('#service-provider-num').removeAttr("required");
      $('.field_number_you_wish_to_transfer_across').find('input').val('NA');
      $('.field_current_service_provider').find('input').val('NA');
      $('.field_current_service_provider_account_number').find('input').val('NA');
    } else if ($(this).val() == "Port across a number") {
      $("#port-num-div").removeClass('d-none');
      $("#service-provider-div").removeClass('d-none');
      $("#service-details-output-div").removeClass('d-none');
      $("#new-num-text").addClass('d-none');
      $('#port-number').attr('required', '');
      $('#service-provider').attr('required', '');
      $('#service-provider-num').attr('required', '');
    } else {
      $("#port-num-div").addClass('d-none');
      $("#service-provider-div").addClass('d-none');
      $("#service-details-output-div").addClass('d-none');
      $("#new-num-text").addClass('d-none');
      $('#port-number').removeAttr("required");
      $('#service-provider').removeAttr("required");
      $('#service-provider-num').removeAttr("required");
    }
  });
  $("#next-btn").click(function() {
    if ($(this).hasClass('basic-info')) {
      var basicform = document.getElementById("basic-info-form");
      if (basicform.checkValidity() === false) {basicform.classList.add('was-validated');} else {
        $("#previous-btn-div").removeClass('d-none');
        $("#basic-info").addClass('d-none');
        $("#verification").removeClass('d-none');
        $("#information-summary").addClass('d-none');
        $("#basic-info-form").removeClass('was-validated');
        $("#verification-form").removeClass('was-validated');
        $("#next-btn").text("Next");
        $("#previous-btn").text("previous");
        $("#next-btn").addClass('verification');
        $("#next-btn").removeClass('summary basic-info');
        $("#previous-btn").addClass('verification');
        $("#previous-btn").removeClass('summary basic-info');
      }
    } else if ($(this).hasClass('verification')) {
      var verificationform = document.getElementById("verification-form");
      if (verificationform.checkValidity() === false) {verificationform.classList.add('was-validated');} else {
        $("#previous-btn-div").removeClass('d-none');
        $("#basic-info").addClass('d-none');
        $("#verification").addClass('d-none');
        $("#information-summary").removeClass('d-none');
        $("#basic-info-form").removeClass('was-validated');
        $("#verification-form").removeClass('was-validated');
        $("#next-btn").text("Submit");
        $("#previous-btn").text("Change Details");
        $("#next-btn").addClass('summary');
        $("#next-btn").removeClass('basic-info verification');
        $("#previous-btn").addClass('summary');
        $("#previous-btn").removeClass('basic-info verification');
        var summary = "";
        $('#basic-info-form input').each(
          function() {
            var input = $(this);

            summary = summary + input.attr('placeholder') + ' : ' + input.val() + '<br>';
          }
        );
        $("#summary").text(summary);
      }
    } else if ($(this).hasClass('summary')) {
      if (!$("#consent_box").is(":checked")) {
        $("#consent_error").fadeIn();
        return false;
      }

      $('#new_submission').submit();

    }

  });

  $("#previous-btn").click(function() {
    $("#previous-btn-div").addClass('d-none');
    $("#basic-info").removeClass('d-none');
    $("#verification").addClass('d-none');
    $("#information-summary").addClass('d-none');
    $("#basic-info-form").removeClass('was-validated');
    $("#verification-form").removeClass('was-validated');
    $("#next-btn").text("Next");
    $("#previous-btn").text("previous");
    $("#next-btn").addClass('basic-info');
    $("#next-btn").removeClass('summary verification');
    $("#previous-btn").addClass('');
    $("#previous-btn").removeClass('summary verification');
  });

  $('#tele-account-number').blur(function() {
    infosummary($('#tele-account-number-output'), $('.field_telechoice_account_number'), this.value);
  });
  $('#first-name').blur(function() {
    infosummary($('#first-name-output'), $('.field_first_name'), this.value);
  });
  $('#last-name').blur(function() {
    infosummary($('#last-name-output'), $('.field_last_name'), this.value);
  });
  $('#email-address').blur(function() {
    infosummary($('#email-address-output'), $('.field_email_address'), this.value);
  });
  $('#contact-number').blur(function() {
    infosummary($('#contact-number-output'), $('.field_contact_number'), this.value);
  });
  $('#dob').blur(function() {
    infosummary($('#dob-output'), $('.field_date_of_birth'), this.name);
  });
  $('#billing-address').blur(function() {
    infosummary($('#billing-address-output'), $('.field_billing_address'), this.value);
  });
  $('#service-details').blur(function() {
    infosummary($('#service-details-output'), $('.field_service_details'), this.value);
  });
  $('#port-number').blur(function() {
    infosummary($('#port-number-output'), $('.field_number_you_wish_to_transfer_across'), this.value);
  });
  $('#service-provider').blur(function() {
    infosummary($('#service-provider-output'), $('.field_current_service_provider'), this.value);
  });
  $('#service-provider-num').blur(function() {
    infosummary($('#service-provider-num-output'), $('.field_current_service_provider_account_number'), this.value);
  });
  $('#employment-type').blur(function() {
    infosummary($('#employment-type-output'), $('.field_employment_type'), this.value);
  });
  $('#company-name').blur(function() {
    infosummary($('#company-name-output'), $('.field_company_name'), this.value);
  });
  $('#company-cont-number').blur(function() {
    infosummary($('#company-cont-number-output'), $('.field_company_contact_number'), this.value);
  });
  $('#company-address').blur(function() {
    infosummary($('#company-address-output'), $('.field_company_address'), this.value);
  });
  $('#driver-licence-number').blur(function() {
    infosummary($('#driver-licence-number-output'), $('.field_drivers_licence_number'), this.value);
  });
  $('#driver-licence-state').blur(function() {
    infosummary($('#driver-licence-state-output'), $('.field_drivers_licence_state'), this.value);
  });
  $('#driver-licence-expiry').blur(function() {
    infosummary($('#driver-licence-expiry-output'), $('.field_drivers_licence_expiry'), this.value);
  });
  $('#medicare-number').blur(function() {
    infosummary($('#medicare-number-output'), $('.field_medicare_card_number'), this.value);
  });
  $("#consent_box").on("change", function() {
    $('.field_i_agree').find('input').prop("checked", $(this).is(":checked"));
    $("#consent_error").fadeOut();
  })

  initialload();

});
