$(document).ready(function () {

function getOffsetdata() {
  var getOffset = $('#agency_form').offset().top;
  var body = $("html, body");
  body.stop().animate({ scrollTop: getOffset - 20 }, 500, 'swing', function () {
    console.log("Finished animating");
  });
}

if ($("#agency_form").length) {
  $("#agency_form").validate({
    onkeyup: true,
    onblur: true,
    submitHandler: function (form) {
      console.log("Submitted!", form);
      form.submit();
    }
  });
}

$("#onBoardingbtn, #form1Btn, #form2Btn, #form3Btn, #form4Btn, #form5Btn, #form6Btn, #form7Btn").click(function (e) {
  e.preventDefault();
  if ($("#agency_form").valid()) {
    $(this).parents('.step_c').hide().next('.step_c').show();
    getOffsetdata()
  }
})

$("#form1Back, #form2Back, #form3Back, #form4Back, #form5Back, #form6Back, #form7Back, #form8Back").click(function () {
  $(this).parents('.step_c').hide().prev('.step_c').show();
  getOffsetdata();
})

$("#otherAddress").change(function () {
  if ($(this).val() === "No") {

    $(".otherAddWrap").removeClass('hideData');
  } else {
    $(".otherAddWrap").addClass('hideData');
  }
});

// Custom Input
var inputs = document.querySelectorAll('.ci-file-input')
for (var i = 0, len = inputs.length; i < len; i++) {
  customInput(inputs[i])
}
function customInput(el) {
  const fileInput = el.querySelector('[type="file"]')
  const label = el.querySelector('[data-js-label]')
  fileInput.onchange =
    fileInput.onmouseout = function () {
      if (!fileInput.value) return
      var value = fileInput.value.replace(/^.*[\\\/]/, '')
      // el.className += ' -chosen'
      label.innerText = value
    }
}

});



// choose file  
// $('#chooseFile').bind('change', function () {
//   var filename = $("#chooseFile").val();
//   if (/^\s*$/.test(filename)) {
//     $(".file-upload").removeClass('active');
//     $("#noFile").text("No file chosen..."); 
//   }
//   else {
//     $(".file-upload").addClass('active');
//     $("#noFile").text(filename.replace("C:\\fakepath\\", "")); 
//   }
// });
// satyendra js
$(document).ready(function() {
  $(document).on('change', '.file-upload input[type="file"]', function() {
      var filename = $(this).val();
      if (/^\s*$/.test(filename)) {
          $(this).parents(".file-upload").find(".file-select-name").text("No file chosen...");
      } else {
          $(this).parents(".file-upload").find(".file-select-name").text(filename.substring(filename.lastIndexOf("\\") + 1, filename.length));
      }

      var uploadFile = $(this);
      var files = !!this.files ? this.files : [];
      if (!files.length || !window.FileReader) return; // no file selected, or no FileReader support

      if (/^image/.test(files[0].type)) { // only image file
          var reader = new FileReader(); // instance of the FileReader
          reader.readAsDataURL(files[0]); // read the local file

          reader.onloadend = function() { // set image data as background of div
              // alert(uploadFile.closest(".file-upload").find('.imagePreview').length);
              uploadFile.closest(".file-upload").find('.imagePreview').css("background-image", "url(" + this.result + ")");
          }
      }
  });
  $(document).on('click', '.file-remove', function(e) {
      e.preventDefault();
      $(this).closest('.file-upload').remove();
      return false;
  });
  $(document).on('click', '.file-add', function(e) {
      e.preventDefault();
      $("#upload-card1").prepend('<div class="file-upload">' +
          '<div class="file-select">' +
              '<div class="imagePreview"></div>' +
              '<button class="btn btn-secondary">Choose File</button>' +
              '<div class="file-select-name">No File Chosen</div>' +
              '<input type="file" name="files[]" class="profileimg">' +
          '</div>' +
          '<button class="btn btn-danger file-remove">Remove</button>' +
     '</div>');
      return false;
      
  });

  $(document).on('click', '.file-add1', function(e) {
    e.preventDefault();
    $("#upload-card2").prepend('<div class="file-upload" id="file2">' +
        '<div class="file-select">' +
            '<div class="imagePreview"></div>' +
            '<button class="btn btn-secondary">Choose File</button>' +
            '<div class="file-select-name">No File Chosen</div>' +
            '<input type="file" name="files[]" class="profileimg">' +
        '</div>' +
        '<button class="btn btn-danger file-remove">Remove</button>' +
   '</div>');
    return false;
    
});



});






var MAX_SIZE = 1024 * 1024 * 5; // 5 MB
var VALID_TYPES = ['image/jpg', 'image/jpeg', 'image/png', 'image/gif'];

function validateFile() {
	if (this.files && this.files.length) {
		var file = this.files[0];
		
		//console.log(file);
		var type = file.type;
		var size = file.size;
		var fileNameSplit = file.name.split('.');
		var extension = fileNameSplit[fileNameSplit.length - 1];
		
		//console.log(type);
		//console.log(extension);
		//console.log(size);
		
		if (size > MAX_SIZE) {
			$('#error-file-too-large').addClass('state-visible');
		} else {
			$('#error-file-too-large').removeClass('state-visible');
		}
		
		if (VALID_TYPES.indexOf(type) < 0) {
			$('#error-invalid-type').addClass('state-visible');
		} else {
			$('#error-invalid-type').removeClass('state-visible');
		}
	}
}

$(document).ready(function() {
	$('#image').on('change', validateFile);
});













