var url = $('meta[name="base-url"]').attr('content');
$.ajaxSetup({
  headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
});


$(function(){
    $('nav-item').click(function(){
        $('nav-item.active').removeClass('active');
        $(this).addClass('active');
    });
});


$(document).ajaxStart(function() {
  $("#loading").fadeIn('200');
}).ajaxStop(function() {
  $("#loading").fadeOut('200');
});

// mobile navigation js start here???????????????????
function openNav() {
  document.getElementById("mySidenav").style.width = "250px";
}

function closeNav() {
  document.getElementById("mySidenav").style.width = "0";
}
// mobile navigation js end here???????????????????

// table popover js start here???????????????????
$(document).ready(function(){
  $('[data-toggle="popover"]').popover();   
});
// table popover js end here???????????????????



// poppp js end here???????????????????
    function closePopUp_popup_8666_retrieve ()  {
         $("#popup_8666_retrieve").modal('hide');
        $('.modal-backdrop.fade.in').remove();
        $('.modal-backdrop.fade.show').remove()

        }
var testEmail = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
//jQuery time
var current_fs, next_fs, previous_fs; //fieldsets
var left, opacity, scale; //fieldset properties which we will animate
var animating; //flag to prevent quick multi-click glitches

$(".next").click(function(){
  if($(this).attr('id')=='next1')
  {
    if($('#first_name').val()=="")
        { 
          errMessage('errFn','Please enter first name.');
          return false; 
        }

        if($('#last_name').val()=="")
        {
          errMessage('errLn','Please enter last name.');
          return false; 
        }

        var email = $('#email').val();
        if(email=="")
        { 
          errMessage('errMail','Please enter email.');
          return false; 
        }

        if(!testEmail.test(email))
        { 
          errMessage('errMail','Please enter valid email.');
          return false; 
        }

        var password = $('#password').val(); 
        if(password=='')
        { 
          errMessage('errPass','Please enter password.');
          return false; 
        }

        if(password.length<8)
        {
          errMessage('errPass','Your password must be at least 8 characters in length.');
          return false;
        }

        if($('input[name="remember"]').is(':checked')!=true)
        { 
          errMessage('errCh','Please accept the terms and conditions.');
          return false; 
        }
        $('.text-sign').html('<h3>Great job, '+$('#first_name').val()+'!</h3><p>You"re almost done.</p><p>What contact info would you like the credit bureaus to use?</p>');
      }
      else if($(this).attr('id')=='next2')
      {
        if($('#address').val()=="")
        { 
          errMessage('errAdd','Please enter address.');
          return false; 
        }

        if($('#state').val()=='')
        { 
          errMessage('errState','A state is required.');
          return false; 
        }

        if($('#city').val()=='')
        { 
          errMessage('errCity','A city is required.');
          return false;
        }

        if($('#phone').val()=="")
        { 
          errMessage('errPh','Phone number is required.');
          return false; 
        }

        var phone = $('#phone').val(); 
        if(phone.length<10)
        { 
          errMessage('errPh','Enter valid phone number.');
          return false; 
        }

        if($('#zip').val()=='')
        {
          errMessage('errZip','Zip number is required.'); 
          return false;
        }

        var zip = $('#zip').val();
        if(zip.length<5)
        {
          errMessage('errZip','Enter valid zip numer.'); 
          return false;
        }        

        $.ajax({
          type:'post',
          url:url+'/saveUser',
          data:$('#msform').serialize(),
          async:false,
          success:function(res)
          {
            // console.log(res);
            location.href = url+'/membership';
          },
          error:function(xhr,error)
          {
            var err = JSON.parse(xhr.responseText);
            err = err.errors;
              // console.log(err.phone);
            if(err!=null)
            {
              if(err.first_name!=null)
              {
                errMessage('errFn',err.first_name[0]);
                return false;
              }
              if(err.last_name!=null)
              {
                errMessage('errLn',err.last_name[0]);
                return false;
              }
              if(err.email!=null)
              {
                errMessage('errMail',err.email[0]);
                return false;
              }
              if(err.password!=null)
              {
                errMessage('errPass',err.password[0]);
                return false;
              }
              if(err.address!=null)
              {
                errMessage('errAdd',err.address[0]);
                return false;
              }
              if(err.state!=null)
              {
                errMessage('errState',err.state[0]);
                return false;
              }
              if(err.city!=null)
              {
                errMessage('errCity',err.city[0]);
                return false;
              }
              if(err.phone!=null)
              {
                errMessage('errPh',err.phone[0]);
                return false;
              }
              if(err.zip!=null)
              {
                errMessage('errZip',err.zip[0]);
                return false;
              }
            }
          }
        });
      }

  if(animating) return false;
  animating = true;
  
  current_fs = $(this).parent().parent();
  next_fs = $(this).parent().parent().next();
  // console.log(current_fs,next_fs);
  //activate next step on progressbar using the index of next_fs
  $("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");
  
  //show the next fieldset
  next_fs.show(); 
  //hide the current fieldset with style
  current_fs.animate({opacity: 0}, {
    step: function(now, mx) {
      //as the opacity of current_fs reduces to 0 - stored in "now"
      //1. scale current_fs down to 80%
      scale = 1 - (1 - now) * 0.2;
      //2. bring next_fs from the right(50%)
      left = (now * 50)+"%";
      //3. increase opacity of next_fs to 1 as it moves in
      opacity = 1 - now;
      current_fs.css({
        // 'transform': 'scale('+scale+')',
        // 'position': 'absolute'
      });
      next_fs.css({'left': left, 'opacity': opacity});
    }, 
    duration: 800, 
    complete: function(){
      current_fs.hide();
      animating = false;
    }, 
    //this comes from the custom easing plugin
    easing: 'easeInOutBack'
  });
});

$(".previous").click(function(){
  if(animating) return false;
  animating = true;
  
  current_fs = $(this).parent().parent();
  previous_fs = $(this).parent().parent().prev();
  
  //de-activate current step on progressbar
  $("#progressbar li").eq($("fieldset").index(current_fs)).removeClass("active");
  
  //show the previous fieldset
  previous_fs.show(); 
  //hide the current fieldset with style
  current_fs.animate({opacity: 0}, {
    step: function(now, mx) {
      //as the opacity of current_fs reduces to 0 - stored in "now"
      //1. scale previous_fs from 80% to 100%
      scale = 0.8 + (1 - now) * 0.2;
      //2. take current_fs to the right(50%) - from 0%
      left = ((1-now) * 50)+"%";
      //3. increase opacity of previous_fs to 1 as it moves in
      opacity = 1 - now;
      current_fs.css({'left': left});
      previous_fs.css({ 'opacity': opacity});
    }, 
    duration: 800, 
    complete: function(){
      current_fs.hide();
      animating = false;
    }, 
    //this comes from the custom easing plugin
    easing: 'easeInOutBack'
  });
});

$(".submit").click(function(){
  return false;
})


$(document).ready(function() {
    $('.collapse').on('show.bs.collapse', function() {
        var id = $(this).attr('id');
        $('a[href="#' + id + '"]').closest('.panel-heading').addClass('active-faq');
        $('a[href="#' + id + '"] .panel-title span').html('<i class="glyphicon glyphicon-minus"></i>');
    });
    $('.collapse').on('hide.bs.collapse', function() {
        var id = $(this).attr('id');
        $('a[href="#' + id + '"]').closest('.panel-heading').removeClass('active-faq');
        $('a[href="#' + id + '"] .panel-title span').html('<i class="glyphicon glyphicon-plus"></i>');
    });
});

// SMart Credit SignIn Functionality

  $(document).on('click','.editPer', function(){
    $(this).text('Save');
    $(this).attr('class','pull-right savePer');
    $('.perForm').find('input').attr('disabled',false);
  });

  $(document).on('click','.savePer', function(){ 
    updateUser();  
  });

function saveSmartCredit()
{
    if($('#security_question').val()=='')
    {
      errMessage('errSQ','The Security Question field is required.');
      return false;
    }
    if($('#security_answer').val()=='')
    {
      errMessage('errSA','The Security Answer field is required.');
      return false; 
    }
    if($('#social_security').val()=='')
    {
      errMessage('errSS','The Social Security # field is required.');
      return false; 
    }
    if($('#dob').val()=='')
    {
      errMessage('errDob','The Date of Birth field is required.');
      return false;
    }

    var frm = new FormData();
    frm.append('plan',$('#credit_plan').val());
    frm.append('security_question',$('#security_question').val());
    frm.append('security_answer',$('#security_answer').val());
    frm.append('social_security',$('#social_security').val());
    frm.append('dob',$('#dob').val());
    $.ajax({
      type:'post',
      url:url+'/saveCreditData',
      data:frm,
      contentType: false,
      processData: false,
      success:function(res)
      {
        // console.log(res);
        if(res)
        {
          location.href = url+'/myaccount';
        }
      },
      error:function(xhr,error)
      {
        var err = JSON.parse(xhr.responseText);
        err = err.errors;
        if(err!=null)
        {
          
        }
      }
    });
}

// User Profile

$(document).on('click','.editMyAcc', function(){
    $(this).text('Save');
    $(this).attr('class','pull-right saveMyAcc');
    $('.MyAccform').find('input').attr('disabled',false);
    $('.MyAccform').find('select').attr('disabled',false);
});

  $(document).on('click','.saveMyAcc', function(){ 
      updateUser();    
  });

function updateUser()
{
  if($('#first_name').val()=='')
    {
      errMessage('errFn','The First Name field is required.');
      return false; 
    }
    if($('#last_name').val()=='')
    {
      errMessage('errLn','The Last Name field is required.');
      return false; 
    }
    var email = $('#email').val();
    if(email=="")
    { 
      errMessage('errMail','The Email field is required.');
      return false; 
    }

    if(!testEmail.test(email))
    { 
      errMessage('errMail','Please enter valid email.');
      return false; 
    }
    if($('#phone_number').val()=='')
    {
      errMessage('errPh','Phone Number is required.');
      return false;   
    }
    if($('#address').val()=='')
    {
      errMessage('errAdd','The Address (First Line) field is required.');
      return false;    
    }
    if($('#zip_code').val()=='')
    {
      errMessage('errZip','Zip is required.');
      return false;
    }
    if($('#state').val()=='')
    {
      errMessage('errState','The State field is required.');
      return false;
    }
    if($('#city').val()=='')
    {
      errMessage('errCity','The City field is required.');
      return false;
    }

    var frm = new FormData();
    frm.append('first_name',$('#first_name').val());
    frm.append('last_name',$('#last_name').val());
    frm.append('email',$('#email').val());
    frm.append('mobile',$('#phone_number').val());
    frm.append('address',$('#address').val());
    frm.append('pincode',$('#zip_code').val());
    frm.append('state',$('#state').val());
    frm.append('city',$('#city').val());
    $.ajax({
          type:'post',
          url:url+'/updateUserProfile',
          data:frm,
          contentType: false,
          processData: false,
          success:function(res)
          {
            // console.log(res);
            if(res)
            {
              location.reload(true);
            }
          },
          error:function(xhr,error)
          {
            var err = JSON.parse(xhr.responseText);
            err = err.errors;
            if(err!=null)
            {
              if(err.first_name!=null)
              {
                errMessage('errFn',err.first_name[0]);
                return false;
              }
              if(err.last_name!=null)
              {
                errMessage('errLn',err.last_name[0]);
                return false;
              }
              if(err.email!=null)
              {
                errMessage('errMail',err.email[0]);
                return false;
              }
              if(err.address!=null)
              {
                errMessage('errAdd',err.address[0]);
                return false;
              }
              if(err.phone!=null)
              {
                errMessage('errPh',err.phone[0]);
                return false;
              }
              if(err.zip!=null)
              {
                errMessage('errZip',err.zip[0]);
                return false;
              }
              if(err.state!=null)
              {
                errMessage('errState',err.state[0]);
                return false;
              }
              if(err.city!=null)
              {
                errMessage('errCity',err.city[0]);
                return false;
              }
            }
          }
        });
}

function updatePassword()
{
  if($('#current_password').val()=='')
  {
    errMessage('errCP','The Current password field is required.');
    return false;
  }

  if($('#new_password').val()=='')
  {
    errMessage('errNP','The New password field is required.');
    return false;
  }

  $.ajax({
    type:'post',
    url:url+'/updateProfilePassword',
    data:{'password':$('#current_password').val(),'new_pass':$('#new_password').val()},
    success:function(res)
    {
      // console.log(res);
      if(res=='true')
      {
        errMessage('errMsg','Your password has been changed.');
        location.reload(true);
        return false;
        
      }
      if(res=='wrong')
      {
        errMessage('errCP','Incorrect password.');
        return false;
      }
    }
  });
}

function getCity(id)
{
  $.ajax({
    type:"get",
    url:url+"/getCity/"+id,
    success:function(res)
    {
      var city = '';
      var ci = $('input[name="city_id"]').val();
      if(res.length>0)
      {
        city += '<option value="">Select</option>';
        for(var i=0;i<res.length;i++)
        {
          var selected = (ci==res[i].city_id) ? 'selected':'';
          city += '<option '+selected+' value='+res[i].city_id+'>'+res[i].city+'</option>';
        }
      }
      $('#city').html(city);
    }
  });
}

$(document).ready(function(){
  var state = $('#state').val();
  if(state)
  {
    getCity(state);
  }
});

function addName(name)
{
  var frm = new FormData();
  if(name=='firstname')
  {
    if($('#add_first_name').val()=='')
    {
      errMessage('errAfn','Enter the name!.');
      return false;
    }

    frm.append('firstname',$('#add_first_name').val());
  }else
  {
    if($('#add_last_name').val()=='')
    {
      errMessage('errAln','Enter the name!.');
      return false;
    }

    frm.append('lastname',$('#add_last_name').val());
  }
  
  $.ajax({
    type:'post',
    url:url+'/postAddName',
    data:frm,
    contentType: false,
    processData: false,
    success:function(res)
    {
      // console.log(res);
      location.reload(true);
    }
  });
}

function deleteAddName(val,name)
{
  $.ajax({
    type:'post',
    url:url+'/deleteName',
    data:{'name':val,'col':name},
    success:function(res)
    {
      // console.log(res);
      location.reload(true);
    }
  });
}


