$(document).ready(function(){
  $(".pack-submit").click(function(){
    id = $(this).attr("id");
    console.log(id);
    url = $('#url').val();
    $.ajax({ 
      type: "post",
      url: ''+url+'add_package_cartdata',
      data: { 'id' : id , 'c_token' : $('#c_token').val() },
      json: true,
      success: function(res) {
        res = JSON.parse(res);
        window.location = ''+url+'view_cart/'+res.token+'';
      },
      error: function(jqXHR, textStatus, err) { alert('text status '+textStatus+', err '+err) }
    });
  });
});

$(document).on( 'click', 'button.plus, button.minus', function() {
  var qty = $( this ).parent( '.qty-no' ).find( '.qty' );
  var val = parseFloat(qty.val());
  var max = parseFloat(qty.attr( 'max' ));
  var min = parseFloat(qty.attr( 'min' ));
  var step = parseFloat(qty.attr( 'step' ));
  if ( $( this ).is( '.plus' ) ) {
       if ( max && ( max <= val ) ) {
      qty.val( max ).change();
      } else {
      qty.val( val + step ).change();
      } 
  } else {

       if ( min && ( min >= val ) ) {
      qty.val( min ).change();
      } else if ( val > 1 ) {
      qty.val( val - step ).change();
      } 
  } 
}); 

$(document).ready(function(){
$(".input-text").change(function(){
  id = $(this).attr("id");
  url = $('#url').val();
  var cost = $('#cost_'+id+'').val();
  val = $('#'+id+'').val() * cost;
  $('.'+id+'').html('$'+val.toFixed(2)+'');
  // console.log($('.0').text().replace("$", ""));
  subtotal = Number($('.0').text().replace("$", "")) + Number($('.1').text().replace("$", ""));
  $('.subtotal').html('$'+subtotal.toFixed(2)+'');
  if ($('#pst').val()) 
  {
    pst = subtotal * $('#pst').val() / 100;
    $('.pst').html('$'+pst.toFixed(2)+'');
  }
  else
  {
    pst = 0;
  }
  gst = subtotal * $('#gst').val() / 100;
  $('.gst').html('$'+gst.toFixed(2)+'');
  total = subtotal + pst + gst;
  $('.total').html('$'+total.toFixed(2)+'')


  $.ajax({ 
    type: "post",
    url: ''+url+'change_count_onclick',
    data: { 'id' : $('#cart_token_id_'+id+'').val() , 'count' : $('#'+id+'').val() },
    json: true,
    success: function(res) {
    },
    error: function(jqXHR, textStatus, err) { alert('text status '+textStatus+', err '+err) }
  });
});
});


$(document).ready(function(){
  $(".editAddress").click(function(){
    id = $(this).attr("id");
   alert(id);
    url = $('#url').val();
    $.ajax({ 
      type: "post",
      url: ''+url+'member_address_form',
      data: { 'id' : id  },
      json: true,
      success: function(res) {
       $('.address-form').css('display','block');
       $('.address-form').append(res);
       // window.location = ''+url+'view_cart/'+res.token+'';
      },
      error: function(jqXHR, textStatus, err) { alert('text status '+textStatus+', err '+err) }
    });
  });
});

$(document).ready(function(){
  $(".submit").click(function(){
    var formData = $('#member_address').serialize();
   alert(formData);
    url = $('#url').val();
    $.ajax({ 
      type: "post",
      url: ''+url+'member_address_edit_or_add',
      data: formData,
      json: true,
      success: function(res) {
       if(res == true){  
         $('.address-form').css('display','none'); 
      //  $("#address_list").load(window.location + "#address_list"); 
       }
      },
      error: function(jqXHR, textStatus, err) { alert('text status '+textStatus+', err '+err) }
    });
  });
});

$(document).ready(function(){
  $("#card-button").click(function(){
    // $('#loading-spinner-overlay').css('visibility', 'visible');
    
  });

  $(".select-address").click(function(){
   
    var value = $(this).attr("value");
    $('#address_id').val(value);
    
  });

  var val = $('.checked').val();
    $('#address_id').val(val);
});

$(document).ready(function(e){
  $("#address-btn").click(function(){
    // var formData = $('#member-address-save').serialize();
    url = $('#url').val();
    if($('#country').val() == 'Canada'){ state = $('#state2').val();  }else{  state = $('#state').val(); }
    $.ajax({ 
      type: "post",
      url: ''+url+'member_address_edit',
      data: {
        id : $('#id').val(),
        name : $('#first_name').val(),
        last_name : $('#last_name').val(),
        address : $('#street_address').val(),
        gender : $("input[name='gender']:checked").val(),
        city : $('#city').val(),
        mobile : $('#mobile').val(),
        state : state,
        pincode : $('#pincode').val(),
        country : $('#country').val(),
        country_code : $('#country_code_post').val(),
      },
      json: true,
      success: function(res) {
       if(res == true){  
        // $("#member-address").load(location.href + " #member-address");
         console.log((res));
         $('.success-message').show();
         $('.success-message').delay(3000).fadeOut('slow');
       }
      },
      error: function(jqXHR, textStatus, err) { alert('text status '+textStatus+', err '+err) }
    });
  });
  
  var country = $("#country").val();
  if(country == 'Canada')
  {
    canada_array = ["Alberta (AB)","British Columbia (BC)","Manitoba (MB)","New Brunswick (NB)","Newfoundland and Labrador (NL)","Northwest Territories (NT)","Nova Scotia (NS)","Nunavut (NU)","Ontario (ON)","Prince Edward Island (PE)","Quebec (QC)","Saskatchewan (SK)","Yukon (YT)"];
    let result = '';
    if($('#state').val() == ''){ result += ' <option value="">Select Province</option>'; }else{  result += ' <option value="'+$('#state').val()+'">'+$('#state').val()+'</option>'; }
  
    canada_array.forEach((element) => {
      result += ' <option value="'+element+'">'+element+'</option>';
    });
    $("#state2").html(result);
      $("#state2").show();
      $("#state").hide();
  }else{
      $("#state").show();
      $("#state2").hide();
     
  }

  $("#country").change(function(){
    var country = $("#country").val();
    if(country == 'Canada')
    {
      canada_array = ["Alberta (AB)","British Columbia (BC)","Manitoba (MB)","New Brunswick (NB)","Newfoundland and Labrador (NL)","Northwest Territories (NT)","Nova Scotia (NS)","Nunavut (NU)","Ontario (ON)","Prince Edward Island (PE)","Quebec (QC)","Saskatchewan (SK)","Yukon (YT)"];
        let result = ' <option value="">Select Province</option>';
        canada_array.forEach((element) => {
          result += ' <option value="'+element+'">'+element+'</option>';
        });
        $("#state2").html(result);
        $("#state2").show();
        $("#state").hide();
    }else{
        $("#state2").hide();
        $("#state").show();
    }
  });

});


$(document).ready(function(e){
  $("#pass-btn").click(function(){
    const passwordPattern = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,}/;
    if ($('#old_password').val() != '' && $('#new_password').val() != "" && $('#confirm_password').val() != "" ) 
    {
      if ($('#old_password').val() != $('#new_password').val()) 
      {
        $('#error_op_after_check').html("");
        if (passwordPattern.test($('#new_password').val())) 
        {
          $('#error_np').html("");
          old_password = $('#old_password').val();
          new_password = $('#new_password').val();
          confirm_password = $('#confirm_password').val();
          var formData = $('#member-pass-save').serialize();
          url = $('#url').val();
          if (new_password == confirm_password) 
          {
            $.ajax({ 
              type: "post",
              url: ''+url+'member_password_edit',
              data: formData,
              json: true,
              success: function(res) {
               if(res == 1){  
                 window.location.href = url+"member_logout";
               }
               else{
                $('#error_op_after_check').html("Please check your old password").css('color' , 'red');
                $('#old_password').val(old_password);
                $('#new_password').val(new_password);
                $('#old_password').val(old_password);
                $('#confirm_password').val('');
               }
              },
              error: function(jqXHR, textStatus, err) { alert('text status '+textStatus+', err '+err) }
            });
          }
          else
          {
            $('#error_op_after_check').html("Please check your old password").css('color' , 'red');
          }
        }
      else
      {
        $('#error_np').html("Must contain at least one number &#013;and one uppercase &#013;and one special character like !@#$%^&*() &#013;and lowercase letter,&#013;and at least 8 or more characters").css('color' , 'red');    
      }
      } 
      else 
      {
        $('#error_np').html("Old password and new password is same").css('color' , 'red');
      }
    }
    else
    {
      if ($('#old_password').val() == "" )
      {
        $('#error_op_after_check').html("Input required").css('color' , 'red');
      }
      if ($('#new_password').val() == "")
      {
        $('#error_np').html("Input required").css('color' , 'red');
      }
      if ($('#confirm_password').val() == "" )
      {
        $('#error_cp').html("Input required").css('color' , 'red');
      }
    }
   
  });
});

$(document).ready(function(e){
$('#old_password').on('keyup', function () {
  $('#error_op_after_check').html("");
  url = $('#url').val();
  $.ajax({ 
    type: "post",
    url: ''+url+'check_old_password',
    data: { id : $('#id').val() ,old_password : $('#old_password').val() },
    json: true,
    success: function(res) {
      console.log(res);
     if(res == 1){  
      $('#error_op').html("Matching").css('color' , 'green')
     }
     else{
      if ($('#old_password').val().length == 0) {
        $('#error_op').html("");
      } else {
        $('#error_op').html("Not matching").css('color' , 'red');
      }

     }
    },
    error: function(jqXHR, textStatus, err) { alert('text status '+textStatus+', err '+err) }
  });
});
}); 


$(document).ready(function(){
$('#new_password, #confirm_password').on('keyup', function () {
  if ($('#old_password').val() != $('#new_password').val()) 
  {
    $('#error_np').html("");
  }
  if ($('#old_password').val() == $('#new_password').val()) 
  {
    $('#error_np').html("Old password and new password is same");
  }
  if ($('#confirm_password').val() == "" )
  {
    $('#error_cp').html('');
  }
  if ($('#new_password').val() == "" )
  {
    $('#error_np').html("");
  }
  if($('#confirm_password').val() != $('#new_password').val())
  {
    $('#error_cp').html("Password and confirm password doesn't match").css('color', 'red');
  }
  else
  {
    $('#error_cp').html("");
  }
  if ($('#new_password').val() == $('#confirm_password').val() && $('#confirm_password').val().length > 0 ) {
    // $('#error_cp').html('Matching').css('color', 'green');
  }
  else if($('#new_password').val().length == 0 || $('#confirm_password').val().length == 0){
    if ($('#confirm_password').val() == "" )
    $('#error_cp').html('');
    if ($('#new_password').val() != "" )
    $('#error_np').html("");
  } 

});
});

// $(document).ready(function(e){
//   $("#pincode").change(function(){
//     zip_id = $("#pincode").val();
//     url = $('#url').val();
//     $.ajax({ 
//       type: "post",
//       url: ''+url+'zipcode_data',
//       data: { 'id' : zip_id },
//       json: true,
//       success: function(res) {
//         res = JSON.parse(res);
//         if(res != 0){  
//           $('#city').val(res[0].city);
//           $('#state').val(res[0].state);
//         }
//       },
//       error: function(jqXHR, textStatus, err) { alert('text status '+textStatus+', err '+err) }
//     });
//   });
// });



// $(document).ready(function(){
//     $(".pack-submit").click(function(){
//       id = $(this).attr("id");
//       url = $('#url').val();
//       console.log($('#c_token').val())
//       $.ajax({ 
//         type: "post",
//         url: ''+url+'add_package_cartdata',
//         data: { 'id' : id , 'c_token' : $('#c_token').val() },
//         json: true,
//         success: function(res) {
//           res = JSON.parse(res);
//           console.log(res);
//           $('#c_token').val(res.token);
//           $('#'+id+'').html('View Cart');
//           $('#'+id+'').attr('href', ''+url+'view_cart/'+res.token+'')
//         },
//         error: function(jqXHR, textStatus, err) { alert('text status '+textStatus+', err '+err) }
//       });
//     });
// });