<!--start overlay-->
	  <div class="overlay toggle-menu"></div>
	<!--end overlay-->
    </div>
    <!-- End container-fluid-->
    
    </div><!--End content-wrapper-->
   <!--Start Back To Top Button-->
    <a href="javaScript:void();" class="back-to-top"><i class="fa fa-angle-double-up"></i> </a>
    <!--End Back To Top Button-->
	
	<!--Start footer-->
	<footer class="footer">
      <div class="container">
        <div class="text-center">
          Copyright © Copyright 2021 BuyNxt Private Limited - All Rights Reserved
        </div>
      </div>
    </footer>
	<!--End footer-->
  
  </div><!--End wrapper-->
  <?= $js; ?>
  <?php echo @$galleryscript; ?>
  <!--Page Js -->
  <script><?php echo @$pagejs; ?></script>
  <!---->
  <script>
  jQuery('#watype').on('change', function() {
 
  if(this.value != 'TEXT'){
  	jQuery('#mediashow').show();
  }else{
  	jQuery('#mediashow').hide();
  }
  
});
  </script>
  
   <script>
  jQuery('#merchanttemplates').on('change', function() {
 	var values={};
	values['templateId']=this.value;
	$.ajax({
					type: 'POST',
					url: '<?= base_url('merchants/gettemplatesdata');?>',
					data: values,
					dataType: 'html',
					success: function(data){						
							jQuery('#templatesdata').html(data);
					}
				});
	});
  </script>
  
<script>
jQuery("#contactscsv").on("change", function (e) {
	 var file = $(this)[0].files[0];
    var upload = new Upload(file);

    // maby check size or type here with upload.getSize() and upload.getType()

    // execute upload
    upload.doUpload();
});

var Upload = function (file) {
    this.file = file;
};

Upload.prototype.getType = function() {
    return this.file.type;
};
Upload.prototype.getSize = function() {
    return this.file.size;
};
Upload.prototype.getName = function() {
    return this.file.name;
};
Upload.prototype.doUpload = function () {
    var that = this;
    var formData = new FormData();

    // add assoc key values, this will be posts values
    formData.append("file", this.file, this.getName());
    formData.append("upload_file", true);

    $.ajax({
        type: "POST",
        url: "<?= base_url('merchants/csvfilecheck'); ?>",
        xhr: function () {
            var myXhr = $.ajaxSettings.xhr();
            if (myXhr.upload) {
                myXhr.upload.addEventListener('progress', that.progressHandling, false);
            }
            return myXhr;
        },
        success: function (data) {
            // your callback here
        },
        error: function (error) {
            // handle error
        },
        async: true,
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        timeout: 60000
    });
};

Upload.prototype.progressHandling = function (event) {
    var percent = 0;
    var position = event.loaded || event.position;
    var total = event.total;
    var progress_bar_id = "#progress-wrp";
    if (event.lengthComputable) {
        percent = Math.ceil(position / total * 100);
    }
    // update progressbars classes so it fits your code
    $(progress_bar_id + " .progress-bar").css("width", +percent + "%");
    $(progress_bar_id + " .status").text(percent + "%");
};
 </script> 
 

 
 <script>
function validate(file,type) {
    $('#sendmediaerr').hide();
	$('#sendmediaerr').removeClass('alert alert-danger');
    $('#sendmediaerr').html("");
	var ext = (file.value).split(".");
    ext = ext[ext.length-1].toLowerCase();      
    
	var size = file.files[0].size;
	
	if(type == 'image'){ var arrayExtensions = ["jpg" , "jpeg", "png"]; var filesize=6000000; }
	
	if(type == 'video'){ var arrayExtensions = ["mp4"]; var filesize=15000000;}
	
	if(type == 'doc'){ var arrayExtensions = ["doc" , "pdf"]; var filesize=14000000;}
	
	//if(size>12){
	if(size>filesize){
		$('#sendmediaerr').show();
		$('#sendmediaerr').addClass('alert alert-danger');
		var sizeInMB = (filesize / (1024*1024)).toFixed(2);
        $('#sendmediaerr').html("Use File Size less than "+ sizeInMB+"MB");
		$('.sendmedia').val("");
	}
	
    if (arrayExtensions.lastIndexOf(ext) == -1) {
		$('#sendmediaerr').show();
		$('#sendmediaerr').addClass('alert alert-danger');
        $('#sendmediaerr').html("Wrong extension type.");
		$('.sendmedia').val("");
    }
}
 </script>
 
  <script>
 function setInputFilter(textbox, inputFilter) {
  ["input", "keydown", "keyup", "mousedown", "mouseup", "select", "contextmenu", "drop"].forEach(function(event) {
    textbox.addEventListener(event, function() {
      if (inputFilter(this.value)) {
        this.oldValue = this.value;
        this.oldSelectionStart = this.selectionStart;
        this.oldSelectionEnd = this.selectionEnd;
      } else if (this.hasOwnProperty("oldValue")) {
        this.value = this.oldValue;
        this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
      } else {
        this.value = "";
      }
    });
  });
}

/* setInputFilter(document.getElementById("inputNum"), function(value) {
  return /^(?: *\d+ *(?:\n|$))+$/.test(value); // Allow digits and '.' only, using a RegExp
}); */
 </script>
 
 <script>
 

 $('#sendwhatsapp').on('change paste', 'input, select, textarea', function(){
	$('.wamsg-prev').html('');
	var myform = document.getElementById("sendwhatsapp");
	var fd = new FormData(myform);
	
	var fileInput = document.getElementById('uploadmedia');
	//console.log(fileInput);
   if(fileInput){
	 if(fileInput.files[0] != null){  var fileUrl = window.URL.createObjectURL(fileInput.files[0]);}
   else{ var fileUrl = '';}
	   fd.append('mediaurl', fileUrl);
 }
	
	
	
	fd.delete('video');
	fd.delete('image');
	fd.delete('document');
    $.ajax({
        url: "<?= base_url('merchants/gettemplateprv');?>",
        data: fd,
        cache: false,
        processData: false,
        contentType: false,
        type: 'POST',
        success: function (dataofconfirm) {
            //alert(dataofconfirm);
			console.log(dataofconfirm);
			$('.wamsg-prev').html(dataofconfirm);
			// do something with the result
        },
		  error: function (xhr, desc, err)
        {
			console.log(desc);			
        }
    });
});
 </script>

<script>
$('#checkdublicate').keyup(function () {
            if (/\D/g.test(this.value)) {
                this.value = this.value.replace(/\D/g, '');
            }
            this.value = this.value
                    .replace(/[\n\r]+/g, "")
                    .replace(/(.{10})/g, "$1\n");
            validateNumbers();
        });
        function validateNumbers() {
            var value = $("#checkdublicate").val();
            var numbersArray = value.split('\n');
            var validNumbers = [];
            var duplicateNumbers = [];
            var inValidNumbers = [];

            // remove empty elements
            var index = numbersArray.indexOf("");
            while(index !== -1)
            {
                numbersArray.splice(index,1);
                index = numbersArray.indexOf("");
            }

            for (var $i = 0; $i < numbersArray.length; $i++) {
                var number = numbersArray[$i];
                if (validNumbers.indexOf(number) !== -1 || inValidNumbers.indexOf(number) !== -1) {
                    duplicateNumbers.push(number);
                } else if (number.match(/[6789]\d{9}/)) {
                    validNumbers.push(number);
                } else {
                    inValidNumbers.push(number);
                }
            }
            $("#totalno").text(numbersArray.length);
            $("#duplicateno").text(duplicateNumbers.length);
            $("#validno").text(validNumbers.length);
            $("#invalidno").text(inValidNumbers.length);
        }
</script>

<script>
	$('#sendwhatsapp').submit(function () {
	var availablecredits = <?= round(LEFTCREDITS); ?>;
	
	 var value = $("#checkdublicate").val();
     var numbersArray = value.split('\n');
     var validNumbers = [];
     var duplicateNumbers = [];
     var index = numbersArray.indexOf("");
     while(index !== -1)
      {
         numbersArray.splice(index,1);
         index = numbersArray.indexOf("");
       }
	   var sendnumbers = numbersArray.length;		
   
	if (sendnumbers>availablecredits) {
        var sortcredit = sendnumbers-availablecredits;
		jQuery('#errmsg').html('<div class="alert alert-danger p-4 text-center">Opps! Need '+sortcredit+' Credits to send whatsapp or reduce some contacts.</div>');
        return false;
    }
});
</script>	
</body>
</html>