(function ($) {
	"use strict";

	/*-----------------------------------------------------
        	Fix PreLoader
        -----------------------------------------------------*/
	$(window).on('load', function () {
		$(".status").fadeOut();
		$(".preloader").delay(350).fadeOut("slow");
	});
	
	
	/* Fix Accordation */
	    $(".acordation_content").slideUp(200);
	    $(".accordation_section > a.accordation_title").on("click", function() {
            if ($(this).hasClass("active")) {
              $(this).removeClass("active");
              $(this)
                .siblings(".acordation_content")
                .slideUp(200);
              $(".accordation_section > a.accordation_title i")
                .removeClass("fa-minus")
                .addClass("fa-plus");
            } else {
              $(".accordation_section > a.accordation_title i")
                .removeClass("fa-minus")
                .addClass("fa-plus");
              $(this)
                .find("i")
                .removeClass("fa-plus")
                .addClass("fa-minus");
              $(".accordation_section > a.accordation_title").removeClass("active");
              $(this).addClass("active");
              $(".acordation_content").slideUp(200);
              $(this)
                .siblings(".acordation_content")
                .slideDown(200);
            }
          });
	    /* Fix Accordation */

	$(document).ready(function () {

		var base_url;
		base_url = $('#base_url').val();

		function printDiv() {

			//Get the HTML of div
			var divElements = document.getElementById('purchase_report_modal').innerHTML;
			//Get the HTML of whole page
			var oldPage = document.body.innerHTML;
			//Reset the page's HTML with div's HTML only
			document.body.innerHTML =
				'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title></title></head><body>' +
				divElements + '</body>';
			//Print Page
			window.print();
			//Restore orignal HTML
			document.body.innerHTML = oldPage;
			setTimeout(function () {
				location.reload();
			}, 500);

		}
		/** add active class and stay opened when selected */
		var url = window.location;

		// for sidebar menu entirely but not cover treeview
		$('ul.sidebar-menu a').filter(function () {
			return this.href == url;
		}).parent().addClass('active');

		// for treeview
		$('ul.treeview-menu a').filter(function () {
			return this.href == url;
		}).parentsUntil(".sidebar-menu > .treeview-menu").addClass('active');

		//Date picker
		$('#mfw_PurchaseBillDate').datepicker({
			format: 'dd/mm/yyyy',
			autoclose: true
		});
		$('#mfw_PurchaseBillDate').datepicker('setDate', new Date());
		$('#mfw_TransferGTSDate').datepicker({
			format: 'dd/mm/yyyy',
			autoclose: true
		});
		$('#mfw_TransferGTSDate').datepicker('setDate', new Date());
		$('.hasDatepicker').datepicker({

			format: 'dd/mm/yyyy',
			autoclose: true
		});

		$('.sidebar-menu').tree();
		$('#mfw_CompanyTable').DataTable({
			'paging': true,
			'lengthChange': false,
			'searching': true,
			'ordering': false,
			'info': true,
			'autoWidth': true

		});
		$('#hdlOrderPending').DataTable({
			'paging': true,
			'lengthChange': true,
			'searching': true,
			'ordering': false,
			'info': true,
			'autoWidth': true,
			'language': {
				'sLengthMenu': "Show _MENU_"
			}
		});

		$('#mfw_ItemsTable').DataTable({
			'paging': true,
			'lengthChange': true,
			'searching': true,
			'ordering': false,
			'info': true,
			'autoWidth': true,
			'language': {
				'sLengthMenu': 'Show _MENU_'
			},
			'responsive': true,
			'processing': true

		});


		$('#hdlwholesaler_table').DataTable({
			'paging': true,
			'lengthChange': true,
			'searching': true,
			'ordering': false,
			'info': true,
			'autoWidth': true,
			'responsive': true,
			'bFilter': true,
			'language': {
				'sLengthMenu': 'Show _MENU_'
			}
		});


		$('#hdlOrderDelivery').DataTable({
			'paging': true,
			'lengthChange': true,
			'searching': true,
			'ordering': false,
			'info': true,
			'autoWidth': true,
			'language': {
				'sLengthMenu': "Show _MENU_"
			}

		});
		$('#hdlOrderCancel').DataTable({
			'paging': true,
			'lengthChange': true,
			'searching': true,
			'ordering': false,
			'info': true,
			'autoWidth': true,
			'language': {
				'sLengthMenu': "Show _MENU_"
			}
		});

		$('.add_datatable').DataTable({
			'paging': true,
			'lengthChange': false,
			'searching': true,
			'ordering': false,
			'info': true,
			'autoWidth': true,
			'language': {
				'sLengthMenu': "Show _MENU_"
			}
		});
		
		$('.hda_langTable').DataTable({
			'paging': true,
			'lengthChange': true,
			'searching': true,
			'ordering': false,
			'info': true,
			'autoWidth': true,
			'responsive': true,
			'bFilter': true,
			'language': {
				'sLengthMenu': 'Show _MENU_'
			}
		});

		$('.select2').select2();

		$('.select2_wholesaler').select2({
			'placeholder': "Select Wholesaler",
			'allowClear': true
		});


		var _URL = window.URL || window.webkitURL;

		var imagval = 0;
		$(document).on('change', '#ItemImage', function (e) {
			var file, img;


			if ((file = this.files[0])) {
				img = new Image();
				img.onload = function () {
				

					if (this.width > 400 || this.height > 400) {
						imagval = 1;
						toastr.error("Please upload image size 400px*400px");
						$("#ItemImage").val('');
					} else {
						var imagval = 0;
					}

				};
				img.onerror = function () {
					alert("not a valid file: " + file.type);
					return false;
				};
				img.src = _URL.createObjectURL(file);


			}

		});

		var imaglogo = 0;
		$(document).on('change', '#logo_Image', function (e) {
			var file, img;

			if ((file = this.files[0])) {
				img = new Image();
				img.onload = function () {
					

					if (this.width > 512 || this.height > 512) {
						imaglogo = 1;
						toastr.error("Please upload logo image size 512px*512px");
						$("#logo_Image").val('');
						$("#priLogoImage").attr('src', '');
					} else {
						var imaglogo = 0;
					}

				};
				img.onerror = function () {
					alert("not a valid file: " + file.type);
					return false;
				};
				img.src = _URL.createObjectURL(file);


			}

		});

		var headerLogo = 0;
		$(document).on('change', '#HeaderLogo', function (e) {
			var file, img;

			if ((file = this.files[0])) {
				img = new Image();
				img.onload = function () {

					if (this.width > 51 || this.height > 51) {
						var headerLogo = 1;
						toastr.error("Please upload header logo image size 50px*50px");
						$("#HeaderLogo").val('');
						$("#priHeaderLogo").val('src', '#');
					} else {
						var headerLogo = 0;

					}

				};
				img.onerror = function () {
					alert("not a valid file: " + file.type);
					return false;
				};
				img.src = _URL.createObjectURL(file);


			}

		});

		var LoginBoxImage = 0;
		$(document).on('change', '#LoginBoxImage', function (e) {
			var file, img;

			if ((file = this.files[0])) {
				img = new Image();
				img.onload = function () {
					if (this.height < 615) {
						LoginBoxImage = 1;
						toastr.error("Please upload login box image size 600px*620");
						$("#LoginBoxImage").val('');
						$("#blah").attr('src', '');
					} else {
						var LoginBoxImage = 0;
					}

				};
				img.onerror = function () {
					alert("not a valid file: " + file.type);
					return false;
				};
				img.src = _URL.createObjectURL(file);


			}

		});

		var LoginBgImage = 0;
		$(document).on('change', '#LoginBgImage', function (e) {
			var file, img;

			if ((file = this.files[0])) {
				img = new Image();
				img.onload = function () {
					if (this.width > 1920 || this.height > 1080) {
						LoginBoxImage = 1;
						toastr.error("Please upload login background image size 1920px*1080px");
						$("#LoginBgImage").val('');
						$("#blahImg").attr('src', '');
					} else {
						var LoginBgImage = 0;
					}

				};
				img.onerror = function () {
					alert("not a valid file: " + file.type);
					return false;
				};
				img.src = _URL.createObjectURL(file);


			}

		});


		var whoimg = 0;
		$(document).on('change', '#imgInp', function (e) {
			var file, img;


			if ((file = this.files[0])) {
				img = new Image();
				img.onload = function () {
					if (this.width > 400 || this.height > 400) {
						whoimg = 1;
						toastr.error("Please upload Wholesaler image size 400px*400px");
						$("#imgInp").val('');
						$("#blah").attr('src', '');
					} else {
						var whoimg = 0;
					}

				};
				img.onerror = function () {
					alert("not a valid file: " + file.type);
					return false;
				};
				img.src = _URL.createObjectURL(file);


			}

		});


		/*================== Item Section Start======================*/
		$("#mfwItemFormId").validate({
			rules: {
				ItemName: "required",
				ItemMinStock: {
					required: true,
					number: true
				},
				ItemWeight: {
					required: true,
					number: true
				},
				stock_unit: "required",
				retaiMrplRate: {
					required: true,
					number: true
				},


			},
			submitHandler: function (form) {
				var _this = $('#CreateItem');
				var btnText = _this.text();
				var targetForm = _this.closest('form');
				var formAction = $('#farm_action').val();
				var ItemWeight = $('#ItemWeight').val();
				var ItemMinStock = $('#ItemMinStock').val();
				var retailRate = $('#retailRate').val();
				var retaiMrplRate = $('#retaiMrplRate').val();

				if (ItemWeight <= 0) {
					toastr.error("Please enter valid weight");
					return false;
				}

				if (imagval > 400) {
					toastr.error("Please upload image size 400px*400px");
					return false;
				}

				if (imaglogo == 1) {
					toastr.error("Please upload image size 512px*512px");
					return false;
				}

				if (!_this.hasClass('adding')) {
					var formdata = new FormData(form);
					_this.html(btnText + ' <i class="fa fa-spinner fa-pulse"></i>'); //.addClass('adding');
					$.ajax({
						method: 'post',
						url: base_url + formAction,
						data: formdata,
						processData: false,
						contentType: false,
						success: function (resp) {
							resp = $.parseJSON(resp);
							if (resp['status'] == 1) {
								if (resp['redirect'] && resp['redirect'] != '') {
									_this.html(btnText).removeClass('adding');
									toastr.success(resp['msg']);
									setTimeout(function () {
										location.reload();
									}, 800);
								} else {
									location.reload();
								}
							} else if (resp['status'] == 2) {
								_this.html(btnText).removeClass('adding');
								toastr.error(resp['error']);
							} else {
								_this.html(btnText).removeClass('adding');
								toastr.error(resp['error']);
							}
						},
						error: function () {
							_this.html(btnText).removeClass('adding');
							toastr.error('Something went wrong, try again.');
						}
					});
				}
			}
		});


		/*================== General Setting page Start======================*/
		$("#hdlGeneralForm").validate({
			rules: {
				logoTitle: "required"

			},
			submitHandler: function (form) {
				var _this = $('#GeneralCreate');
				var btnText = _this.text();
				var targetForm = _this.closest('form');
				var formAction = $('#farm_action').val();


				if (imaglogo == 1) {
					toastr.error("Please upload logo image size 512px*512px");
					return false;
				}

				if (headerLogo == 1) {
					toastr.error("Please upload header logo image size 50px*50px");
					return false;
				}
				if (LoginBoxImage == 1) {
					toastr.error("Please upload login box image size 600px*700px");
					return false;
				}

				if (LoginBgImage == 1) {
					toastr.error("Please upload login background image size 1920px*1080px");
					return false;
				}

				var formdata = new FormData(form);
				$.ajax({
					method: 'post',
					url: base_url + formAction,
					data: formdata,
					processData: false,
					contentType: false,
					success: function (resp) {
						resp = $.parseJSON(resp);
						if (resp['status'] == 1) {

							toastr.success(resp['msg']);

						} else {

							toastr.error(resp['error']);
						}
					},
					error: function () {

						toastr.error('Something went wrong, try again.');
					}
				});

			}
		});

		/*================== General Setting Api page Start======================*/
		$("#hdlApiForm").validate({
			rules: {
				smsApi: "required",

			},
			submitHandler: function (form) {
				var _this = $('#GeneralApiCreate');
				var btnText = _this.text();
				var formAction = $('#farm_action_api').val();
				var smsApi = $('#smsApi').val();

				var formdata = new FormData(form);
				$.ajax({
					method: 'post',
					url: base_url + formAction,
					data: formdata,
					processData: false,
					contentType: false,
					success: function (resp) {
						resp = $.parseJSON(resp);
						if (resp['status'] == 1) {

							toastr.success(resp['msg']);

						} else {

							toastr.error(resp['error']);
						}
					},
					error: function () {

						toastr.error('Something went wrong, try again.');
					}
				});

			}
		});

		$(document).on('click', '.unit_edit_btn', function () {
			var _this = $(this);
			var itemEditId = _this.attr('data-id');
			var btnText = _this.text();
			if (itemEditId != '') {
				_this.html(btnText + ' <i class="fa fa-spinner fa-pulse"></i>'); //.addClass('adding');
				$.ajax({
					method: 'post',
					url: base_url + 'dashboard/edit_unit',
					data: {
						'itemId': itemEditId
					},
					success: function (resp) {
						var respData = JSON.parse(resp);

						if (respData['status'] == 1) {
							$('#UnitName').val(respData['itemdata']['unitName']);

							$('#ItemUnitId').val(respData['itemdata']['unitId']);
							setTimeout(function () {
								$('#CreateItem').text('Update');
								$("#ItemCreateItemBtn").trigger("click");
								_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							}, 800);

						} else if (respData['status'] == 0) {
							_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							toastr.error(respData['error']);
						}
					},
					error: function () {
						_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i></i>');
						toastr.error('Something went wrong, please try.');
					}
				});
			}
		});

		/*================== item unit  page==================*/
		$("#hdlItemUnitForm").validate({
			rules: {
				UnitName: "required",

			},
			submitHandler: function (form) {
				var _this = $('#CreateItemUnit');
				var btnText = _this.text();
				var formAction = $('#farm_action').val();

				var formdata = new FormData(form);
				$.ajax({
					method: 'post',
					url: base_url + formAction,
					data: formdata,
					processData: false,
					contentType: false,
					success: function (resp) {
						resp = $.parseJSON(resp);
						if (resp['status'] == 1) {

							toastr.success(resp['msg']);
							setTimeout(function () {
								location.reload();
							}, 800);

						} else {

							toastr.error(resp['error']);
						}
					},
					error: function () {

						toastr.error('Something went wrong, try again.');
					}
				});

			}
		});

		$(document).on('keypress', 'input', function (event) {
			var character = String.fromCharCode(event.keyCode);
			return isValid(character);
		});

		function isValid(str) {
			return !/[-_~`!#$%\^&*()+=\[\]\\';,{}|\\":<>\?]/g.test(str);
		}

		$(document).on('click', '.item_edit_btn', function () {
			var _this = $(this);
			var itemEditId = _this.attr('data-id');
			var btnText = _this.text();
			if (itemEditId != '') {
				_this.html(btnText + ' <i class="fa fa-spinner fa-pulse"></i>'); //.addClass('adding');
				$.ajax({
					method: 'post',
					url: base_url + 'dashboard/edit_item',
					data: {
						'itemId': itemEditId
					},
					success: function (resp) {
						var respData = JSON.parse(resp);

						if (respData['status'] == 1) {
							$('#ItemName').val(respData['itemdata']['subCatName']);
							$('#ItemMinStock').val(respData['itemdata']['stockQnt']);
							$('#ItemWeight').val(respData['itemdata']['itemStockWeight']);
							$('#stock_unit').val(respData['itemdata']['qntUnit']);
							$("#stock_unit").trigger("change");
							$('#categoryId').val(respData['itemdata']['mainCatId']);
							$("#categoryId").trigger("change");
							$('#wholesalerId').val(respData['itemdata']['partyId']);
							$("#wholesalerId").trigger("change");
							$('#retailRate').val(respData['itemdata']['rate']);
							$('#retaiMrplRate').val(respData['itemdata']['itemMrp']);
							if (respData['itemdata']['image'] != '') {
								var itemSrc = base_url + 'assets/uploads/' + respData['itemdata']['image'];
								var imgName = respData['itemdata']['image'];
								$('#ItemImageDiv').html("<img src='" + itemSrc + "' class='img_thumb'><input type='hidden' value='" + imgName + "' name='mfwItemOldImage'>");
							}
							$('#ItemUpdateId').val(respData['itemdata']['subCatId']);
							setTimeout(function () {
								$('#CreateItem').text('Submit');
								$("#ItemCreateItemBtn").trigger("click");
								_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							}, 800);

						} else if (respData['status'] == 0) {
							_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							toastr.error(respData['error']);
						}
					},
					error: function () {
						_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i></i>');
						toastr.error('Something went wrong, please try.');
					}
				});
			}
		});
		$(document).on('click', '.item_delete_btn', function () {
			if (confirm("Are you sure you want to delete this item?")) {
			    $(this).remove();
				var _this = $(this);
				var itemDeleteId = _this.attr('data-id');
				var btnText = _this.text();

				if (itemDeleteId != '') {
					_this.html(btnText + ' <i class="fa fa-spinner fa-pulse"></i>'); //.addClass('adding');
					$.ajax({
						method: 'post',
						url: base_url + 'dashboard/delete_item',
						data: {
							'itemId': itemDeleteId
						},
						success: function (resp) {
							var respData = JSON.parse(resp);
							if (respData['status'] == 1) {
								_this.closest('tr').remove();
							} else if (respData['status'] == 0) {
								_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
								toastr.error(respData['error']);
							}
						},
						error: function () {
							_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i></i>');
							toastr.error('Something went wrong, please try.');
						}
					});
				}
			} else {
				return false;
			}
		});

		$(document).on('click', '.category_edit_btn', function () {
			var _this = $(this);
			var itemEditId = _this.attr('data-id');
			var btnText = _this.text();
			if (itemEditId != '') {
				_this.html(btnText + ' <i class="fa fa-spinner fa-pulse"></i>'); //.addClass('adding');
				$.ajax({
					method: 'post',
					url: base_url + 'dashboard/edit_catigory',
					data: {
						'itemId': itemEditId
					},
					success: function (resp) {
						var respData = JSON.parse(resp);

						if (respData['status'] == 1) {
							$('#ItemName').val(respData['itemdata']['catName']);

							if (respData['itemdata']['catImage'] != '') {
								var itemSrc = base_url + 'assets/uploads/' + respData['itemdata']['catImage'];
								var imgName = respData['itemdata']['catImage'];
								if (itemSrc != "") {
									$('#ItemImagepr').attr('src', itemSrc);
								} else {
									$('#ItemImagepr').attr('src', "#");
								}
							}
							$('#ItemUpdateId').val(respData['itemdata']['catId']);
							setTimeout(function () {
								$('#CreateItem').text('Update');
								$("#ItemCreateItemBtn").trigger("click");
								_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							}, 800);

						} else if (respData['status'] == 0) {
							_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							toastr.error(respData['error']);
						}
					},
					error: function () {
						_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i></i>');
						toastr.error('Something went wrong, please try.');
					}
				});
			}
		});

		$(document).on('click', '.category_delete_btn', function () {
			if (confirm("Are you sure you want to delete this item categories?")) {
			    $(this).remove();
				var _this = $(this);
				var itemDeleteId = _this.attr('data-id');
				var btnText = _this.text();

				if (itemDeleteId != '') {
					_this.html(btnText + ' <i class="fa fa-spinner fa-pulse"></i>'); //.addClass('adding');
					$.ajax({
						method: 'post',
						url: base_url + 'dashboard/delete_categories',
						data: {
							'itemId': itemDeleteId
						},
						success: function (resp) {
							var respData = JSON.parse(resp);
							if (respData['status'] == 1) {
								_this.closest('tr').remove();
							} else if (respData['status'] == 0) {
								_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
								toastr.error(respData['error']);
							}
						},
						error: function () {
							_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i></i>');
							toastr.error('Something went wrong, please try.');
						}
					});
				}
			} else {
				return false;
			}
		});

		$(document).on('click', '.logo_edit_btn', function () {
			var _this = $(this);
			var itemEditId = _this.attr('data-id');
			var btnText = _this.text();
			if (itemEditId != '') {
				_this.html(btnText + ' <i class="fa fa-spinner fa-pulse"></i>'); //.addClass('adding');
				$.ajax({
					method: 'post',
					url: base_url + 'admin/edit_logo',
					data: {
						'itemId': itemEditId
					},
					success: function (resp) {
						var respData = JSON.parse(resp);
						if (respData['status'] == 1) {
							$('#ItemName').val(respData['itemdata']['logo_title']);

							if (respData['itemdata']['logo_image'] != '') {
								var itemSrc = base_url + 'assets/uploads/' + respData['itemdata']['logo_image'];
								var imgName = respData['itemdata']['logo_image'];
								if (itemSrc != "") {
									$('#ItemImageDiv').html("<img src='" + itemSrc + "' class='img_thumb'><input type='hidden' value='" + imgName + "' name='mfwItemOldImage'>");

								}
							}
							$('#ItemUpdateId').val(respData['itemdata']['logo_id']);
							setTimeout(function () {
								$('#CreateItem').text('Update');
								$("#ItemCreateItemBtn").trigger("click");
								_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							}, 800);

						} else if (respData['status'] == 0) {
							_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							toastr.error(respData['error']);
						}
					},
					error: function () {
						_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i></i>');
						toastr.error('Something went wrong, please try.');
					}
				});
			}
		});

		$(document).on('click', '.api_edit_btn', function () {
			var _this = $(this);
			var itemEditId = _this.attr('data-id');
			var btnText = _this.text();
			if (itemEditId != '') {
				_this.html(btnText + ' <i class="fa fa-spinner fa-pulse"></i>'); //.addClass('adding');
				$.ajax({
					method: 'post',
					url: base_url + 'admin/api_edit',
					data: {
						'itemId': itemEditId
					},
					success: function (resp) {
						var respData = JSON.parse(resp);
						if (respData['status'] == 1) {
							$('#ItemName').val(respData['itemdata']['value_text']);

							$('#ItemUpdateId').val(respData['itemdata']['uniq_id']);
							setTimeout(function () {
								$('#CreateItem').text('Update');
								$("#ItemCreateItemBtn").trigger("click");
								_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							}, 800);

						} else if (respData['status'] == 0) {
							_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							toastr.error(respData['error']);
						}
					},
					error: function () {
						_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i></i>');
						toastr.error('Something went wrong, please try.');
					}
				});
			}
		});

		$(document).on('click', '.general_edit_btn', function () {
			var _this = $(this);
			var itemEditId = _this.attr('data-id');
			var btnText = _this.text();
			if (itemEditId != '') {
				_this.html(btnText + ' <i class="fa fa-spinner fa-pulse"></i>'); //.addClass('adding');
				$.ajax({
					method: 'post',
					url: base_url + 'admin/general_edit',
					data: {
						'itemId': itemEditId
					},
					success: function (resp) {
						var respData = JSON.parse(resp);
						if (respData['status'] == 1) {
							$('#ThemeColor').val(respData['itemdata']['theme_color']);

							if (respData['itemdata']['login_box_img'] != '') {
								var itemSrc = base_url + 'assets/uploads/' + respData['itemdata']['login_box_img'];
								var imgName = respData['itemdata']['login_box_img'];
								if (itemSrc != "") {
									$('#LoginBoxImageDiv').attr("src", itemSrc);

								}
							}

							$('#LoginBgColor').val(respData['itemdata']['login_box_color']);

							if (respData['itemdata']['login_img'] != '') {
								var itemSrc = base_url + 'assets/uploads/' + respData['itemdata']['login_img'];
								var imgName = respData['itemdata']['login_img'];
								if (itemSrc != "") {
									$('#LoginBgImageDiv').attr("src", itemSrc);

								}
							}
							$('#ItemUpdateId').val(respData['itemdata']['logo_id']);
							setTimeout(function () {
								$('#CreateItem').text('Update');
								$("#ItemCreateItemBtn").trigger("click");
								_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							}, 800);

						} else if (respData['status'] == 0) {
							_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							toastr.error(respData['error']);
						}
					},
					error: function () {
						_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i></i>');
						toastr.error('Something went wrong, please try.');
					}
				});
			}
		});


		// admin upadate 
		$("#adminFormId").validate({
			rules: {
				admin_name: "required",
				admin_email: {
					required: true,
					email: true
				},
				admin_mobile: {
					required: true,
					number: true
				},
				admin_wn: {
					required: true
				},
				admin_hn: {
					required: true
				},
				admin_town: "required",
				admin_dis: "required",

			},
			submitHandler: function (form) {

				var _this = $('#adminFormId');
				var btnText = _this.text();
				var targetForm = _this.closest('form');
				var UserMobile = $('#admin_mobile').val();
				var UserPassword = $('#admin_pwd').val();
				var AadharNumber = $('#AadharNumber').val();
				var PanNumber = $('#PanNumber').val();
				var GSTNumber = $('#GSTNumber').val();
				var userRole = $('.userType').val();
				var admin_town = $('#admin_town').val();
				var admin_dis = $('#admin_dis').val();


				if (UserPassword != "") {
					if (UserPassword.length < 6) {
						toastr.error('Minimum password length is 6 characters.');
						return false;
					}
				}

				if (UserMobile.length >= 11 || UserMobile.length <= 9) {
					toastr.error('Please enter a valid mobile number.');
					return false;
				}

				if (imagval == 1) {
					toastr.error("Please upload image size 400px*400px");
					return false;
				}

				if (isNaN(admin_town) != 1) {
					toastr.error("Please enter a valid town name");
					return false;
				}

				if (isNaN(admin_dis) != 1) {
					toastr.error("Please enter a valid district name");
					return false;
				}

				if (userRole != 1) {
					if (AadharNumber.length >= 13 || AadharNumber.length <= 11) {
						toastr.error('Please enter a valid aadhar number.');
						return false;
					}
					if (PanNumber.length >= 11 || PanNumber.length <= 9) {
						toastr.error('Please enter a valid pan number.');
						return false;
					}
					if (GSTNumber.length >= 16 || GSTNumber.length <= 14) {
						toastr.error('Please enter a valid GST number.');
						return false;
					}
				}

				var formdata = new FormData(form);

				$.ajax({
					method: 'post',
					url: base_url + 'admin/admin_edit',
					data: formdata,
					processData: false,
					contentType: false,
					success: function (resp) {
						resp = $.parseJSON(resp);
						if (resp['status'] == 1) {
							if (resp['redirect'] && resp['redirect'] != '') {

								toastr.success(resp['msg']);
								setTimeout(function () {
									window.location.href = base_url + resp['redirect'];
								}, 800);
							} else {
								location.reload();
							}
						} else if (resp['status'] == 2) {

							toastr.error(resp['error']);
						} else {

							toastr.error('Something went wrong, try again.');
						}
					}
				});

			}
		});


		///   email validation ///

		function validateEmail($email) {
			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
			return emailReg.test($email);
		}

		/*================== Add User From Backend Start======================*/
		$("#mfwUserFormId").validate({
			rules: {
				UserName: "required",
				UserEmail: {
					required: true,
					email: true
				},
				UserMobile: {
					required: true,
					number: true
				},
				WordNumber: {
					required: true
				},
				HouseNumber: {
					required: true
				},
				Town: "required",
				District: "required",

			},
			submitHandler: function (form) {

				var _this = $('#hdaCreateUser');
				var UserPassword = $('#UserPassword').val();
				var District = $('#District').val();
				var HouseNumber = $('#HouseNumber').val();
				var WordNumber = $('#WordNumber').val();
				var UserMobile = $('#UserMobile').val();
				var UserId = $('#UserId').val();
				var admin_town = $('#Town').val();
				var admin_dis = $('#District').val();
				var btnText = _this.text();
				var targetForm = _this.closest('form');
				if (UserId == "") {
					if (UserPassword == "") {
						toastr.error('Required password field.');
						return false;
					}
				}
				if (UserPassword != "") {
					if (UserPassword.length < 6) {
						toastr.error('Minimum password length is 6 characters.');
						return false;
					}
				}

				if (UserMobile.length >= 11 || UserMobile.length <= 9) {
					toastr.error('Please enter a valid mobile number.');
					return false;
				}

				if (isNaN(admin_town) != 1) {
					toastr.error("Please enter a valid town name");
					return false;
				}

				if (isNaN(admin_dis) != 1) {
					toastr.error("Please enter a valid district name");
					return false;
				}


				if (!_this.hasClass('adding')) {
					var formdata = new FormData(form);
					_this.html(btnText + ' <i class="fa fa-spinner fa-pulse"></i>'); //.addClass('adding');
					$.ajax({
						method: 'post',
						url: base_url + 'user/create_user',
						data: formdata,
						processData: false,
						contentType: false,
						success: function (resp) {
							resp = $.parseJSON(resp);
							if (resp['status'] == 1) {
								if (resp['redirect'] && resp['redirect'] != '') {
									_this.html(btnText).removeClass('adding');
									toastr.success(resp['msg']);
									setTimeout(function () {
										window.location.href = base_url + resp['redirect'];
									}, 800);
								} else {
									location.reload();
								}
							} else if (resp['status'] == 2) {
								_this.html(btnText).removeClass('adding');
								toastr.error(resp['error']);
							} else {
								_this.html(btnText).removeClass('adding');
								toastr.error(resp['error']);
							}
						},
						error: function () {
							_this.html(btnText).removeClass('adding');
							toastr.error('Something went wrong, try again.');
						}
					});
				}

			}
		});

		$(document).on('click', '.user_edit_btn', function () {
			var _this = $(this);
			var UsrerEditId = _this.attr('data-id');
			var btnText = _this.text();
			if (UsrerEditId != '') {
				_this.html(btnText + ' <i class="fa fa-spinner fa-pulse"></i>'); //.addClass('adding');
				$.ajax({
					method: 'post',
					url: base_url + 'user/edit_user',
					data: {
						'UsrerEditId': UsrerEditId
					},
					success: function (resp) {
						var respData = JSON.parse(resp);

						if (respData['status'] == 1) {
							$('#UserName').val(respData['userData']['userName']);
							$('#UserEmail').val(respData['userData']['userEmail']);
							$('#UserMobile').val(respData['userData']['userMobile']);
							var address = respData['userData']['address'];
							var arr = address.split("|");
							$('#WordNumber').val(arr[1]);
							$('#HouseNumber').val(arr[3]);
							$('#Town').val(arr[2]);
							$('#District').val(arr[0]);
							$('#UserId').val(respData['userData']['userId']);
							setTimeout(function () {
								$('#hdaCreateUser').text('Update');
								$("#ItemCreateItemBtn").trigger("click");
								_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							}, 800);

						} else if (respData['status'] == 0) {
							_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							toastr.error(respData['error']);
						}
					},
					error: function () {
						_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i></i>');
						toastr.error('Something went wrong, please try.');
					}
				});
			}
		});

		/*  user status  on/off */
		$(document).on('click', '.UserStatus', function () {

			var _this = $(this);
			var UserId = _this.attr('data-UserStatusId');
			var status = _this.attr('data-statusValue');
			var btnText = _this.text();
			
			if (UserId != '') {
				var alertmsg = confirm("Are you sure you want to change status ?");
				if (alertmsg == true) {
					$(this).remove();
					$.ajax({
						method: 'post',
						url: base_url + 'user/userStatus',
						data: {
							'UserId': UserId,
							'status': status
						},
						success: function (resp) {
							var respData = JSON.parse(resp);
							if (respData['status'] == 1) {
								toastr.success(respData['msg']);
								setTimeout(function () {
									window.location.reload();
								}, 500);
							} else {
								_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i></i>');
								toastr.error('Something went wrong, please try.');
							}

						},
						error: function () {
							_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i></i>');
							toastr.error('Something went wrong, please try.');
						}
					});
				} else {
					return false;
				}
			} else {
				return false;
			}
		});


		/*  user status  on/off */
		$(document).on('click', '.CatStatus', function () {

			var _this = $(this);
			var UserId = _this.attr('data-UserStatusId');
			var status = _this.attr('data-statusValue');
			var btnText = _this.text();
			
			if (UserId != '') {
				var alertmsg = confirm("Are you sure status update?");
				if (alertmsg == true) {
				    $(this).remove();
					$.ajax({
						method: 'post',
						url: base_url + 'dashboard/catStatus',
						data: {
							'UserId': UserId,
							'status': status
						},
						success: function (resp) {
							var respData = JSON.parse(resp);
							if (respData['status'] == 1) {
								toastr.success(respData['msg']);
								setTimeout(function () {
									window.location.reload();
								}, 500);
							} else {
								_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i></i>');
								toastr.error('Something went wrong, please try.');
							}

						},
						error: function () {
							_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i></i>');
							toastr.error('Something went wrong, please try.');
						}
					});
				} else {
					return false;
				}
			} else {
				return false;
			}
		});

		/*  user status  on/off */
		$(document).on('click', '.refundStatus', function () {

			var _this = $(this);
			var refundId = _this.attr('data-refundId');
			var userEmail = _this.attr('data-userEmail');
			var btnText = _this.text();
			
			if (refundId != '') {
                
                var alertmsg = confirm("Are you sure payment refund ?");
				if (alertmsg == true) {
				    $(this).remove();
					$.ajax({
						method: 'post',
						url: base_url+'admin/refundStatus',
						data: {
							'refundId': refundId,'userEmail': userEmail
						},
						success: function (resp) {
						    console.log(resp);
							var respData = JSON.parse(resp);
							if (respData['status'] == 1) {
								toastr.success(respData['msg']);
								setTimeout(function () {
									window.location.reload();
								}, 500);
							} else {
								toastr.error('Something went wrong, please try3.');
							}

						},
						error: function () {
							toastr.error('Something went wrong, please try4.');
						}
					});
				} else {
					return false;
				}
			} else {
				return false;
			}
		});

		function readURL(input) {
			if (input.files && input.files[0]) {
				var reader = new FileReader();

				reader.onload = function (e) {
					$('#blah').attr('src', e.target.result);
				}

				reader.readAsDataURL(input.files[0]); // convert to base64 string
			}
		}

		$(document).on('change', '#imgInp', function () {
			readURL(this);
		});


		function readURLs(input) {
			if (input.files && input.files[0]) {
				var reader = new FileReader();

				reader.onload = function (e) {
					$('#shopblah').attr('src', e.target.result);
				}

				reader.readAsDataURL(input.files[0]); // convert to base64 string
			}
		}

		$(document).on('change', '#shopimgInp', function () {
			readURLs(this);
		});


		function readURLa(input) {
			if (input.files && input.files[0]) {
				var reader = new FileReader();

				reader.onload = function (e) {
					$('#aadharblah').attr('src', e.target.result);
				}

				reader.readAsDataURL(input.files[0]); // convert to base64 string
			}
		}

		$(document).on('change', '#aadharimgInp', function () {
			readURLa(this);
		});


		function readURLp(input) {
			if (input.files && input.files[0]) {
				var reader = new FileReader();

				reader.onload = function (e) {
					$('#panblah').attr('src', e.target.result);
				}

				reader.readAsDataURL(input.files[0]); // convert to base64 string
			}
		}

		$(document).on('change', '#panimgInp', function () {
			readURLp(this);
		});

		function readURLloi(input) {
			if (input.files && input.files[0]) {
				var reader = new FileReader();

				reader.onload = function (e) {
					$('#priLogoImage').attr('src', e.target.result);
				}

				reader.readAsDataURL(input.files[0]); // convert to base64 string
			}
		}

		$(document).on('change', '#logo_Image', function () {
			readURLloi(this);
		});

		function readURLhl(input) {
			if (input.files && input.files[0]) {
				var reader = new FileReader();

				reader.onload = function (e) {
					$('#priHeaderLogo').attr('src', e.target.result);
				}

				reader.readAsDataURL(input.files[0]); // convert to base64 string
			}
		}

		$(document).on('change', '#HeaderLogo', function () {
			readURLhl(this);

		});

		function readURLlbg(input) {
			if (input.files && input.files[0]) {
				var reader = new FileReader();

				reader.onload = function (e) {
					$('#blah').attr('src', e.target.result);
				}

				reader.readAsDataURL(input.files[0]); // convert to base64 string
			}
		}

		$(document).on('change', '#LoginBoxImage', function () {
			readURLlbg(this);
		});

		function readURLlbimg(input) {
			if (input.files && input.files[0]) {
				var reader = new FileReader();

				reader.onload = function (e) {
					$('#blahImg').attr('src', e.target.result);
				}

				reader.readAsDataURL(input.files[0]); // convert to base64 string
			}
		}

		$(document).on('change', '#LoginBgImage', function () {
			readURLlbimg(this);
		});
		$(document).on('click', '#panblah,#shopblah,#blah,#aadharblah', function () {
			src = $(this).attr("src");
			$(".big_img").attr("src", src);
		});

		$(".big_img").attr('src', "#");

		/*================== Add wholesaler From Backend Start======================*/
		$("#hdaWholesalerFormId").validate({
			rules: {
				UserName: "required",
				UserEmail: {
					required: true,
					email: true
				},
				UserMobile: {
					required: true,
					number: true
				},
				WordNumber: {
					required: true
				},
				HouseNumber: {
					required: true
				},
				Town: "required",
				District: "required",
				AadharNumber: {
					required: true,
					number: true
				},
				PanNumber: "required",
				GSTNumber: "required",

			},
			submitHandler: function (form) {

				var _this = $('#hdaWholesalerUser');
				var UserPassword = $('#UserPassword').val();
				var UserName = $('#UserName').val();
				var District = $('#District').val();
				var HouseNumber = $('#HouseNumber').val();
				var WordNumber = $('#WordNumber').val();
				var UserMobile = $('#UserMobile').val();
				var AadharNumber = $('#AadharNumber').val();
				var PanNumber = $('#PanNumber').val();
				var GSTNumber = $('#GSTNumber').val();
				var UserId = $('#UserId').val();
				var admin_town = $('#Town').val();
				var admin_dis = $('#District').val();
				var btnText = _this.text();
				var targetForm = _this.closest('form');

				if (UserName == 0) {
					toastr.error('Please enter a valid user name.');
					return false;
				}
				if (UserMobile.length >= 11 || UserMobile.length <= 9 || UserMobile == 0) {
					toastr.error('Please enter a valid mobile number.');
					return false;
				}
				if (UserPassword != "") {
					if (UserPassword.length < 6 || UserPassword == 0) {
						toastr.error('Minimum password length is 6 characters.');
						return false;
					}
				}

				if (UserId != "") {
				    if (isNaN(admin_town) != 1) {
					toastr.error("Please enter a valid town name");
					return false;
    				}
    
    				if (isNaN(admin_dis) != 1) {
    					toastr.error("Please enter a valid district name");
    					return false;
    				}
					if (AadharNumber.length >= 13 || AadharNumber.length <= 11 || AadharNumber == 0) {
						toastr.error('Please enter a valid aadhar number.');
						return false;
					}
					if (PanNumber.length >= 11 || PanNumber.length <= 9 || PanNumber == 0) {
						toastr.error('Please enter a valid pan number.');
						return false;
					}
					if (GSTNumber.length >= 16 || GSTNumber.length <= 14 || GSTNumber == 0) {
						toastr.error('Please enter a valid GST number.');
						return false;
					}

					if (whoimg == 1) {
						toastr.error("Please upload Wholesaler image size 400px*400px");
						return false;
					}
				}

				if (!_this.hasClass('adding')) {
					var formdata = new FormData(form);
					_this.html(btnText + ' <i class="fa fa-spinner fa-pulse"></i>'); //.addClass('adding');
					$.ajax({
						method: 'post',
						url: base_url + 'user/create_wholesaler',
						data: formdata,
						processData: false,
						contentType: false,
						success: function (resp) {
							resp = $.parseJSON(resp);
							if (resp['status'] == 1) {
								if (resp['redirect'] && resp['redirect'] != '') {
									_this.html(btnText).removeClass('adding');
									toastr.success(resp['msg']);
									setTimeout(function () {
										location.reload();
									}, 800);
								} else {
									location.reload();
								}
							} else if (resp['status'] == 2) {
								_this.html(btnText).removeClass('adding');
								toastr.error(resp['error']);
							} else {
								_this.html(btnText).removeClass('adding');
								toastr.error(resp['error']);
							}
						},
						error: function () {
							_this.html(btnText).removeClass('adding');
							toastr.error('Something went wrong, try again.222');
						}
					});
				}

			}
		});

		$(document).on('click', '.wholesaler_edit_btn', function () {
			var _this = $(this);
			$(".section_wh").removeClass("wholesalerinfo");

			var UsrerEditId = _this.attr('data-id');
			var btnText = _this.text();
			if (UsrerEditId != '') {
				_this.html(btnText + ' <i class="fa fa-spinner fa-pulse"></i>'); //.addClass('adding');
				$.ajax({
					method: 'post',
					url: base_url + 'user/edit_wholesaler',
					data: {
						'UsrerEditId': UsrerEditId
					},
					success: function (resp) {
						var respData = JSON.parse(resp);

						if (respData['status'] == 1) {
							$('#UserName').val(respData['userData']['userName']);
							$('#UserEmail').val(respData['userData']['userEmail']);
							$('#UserMobile').val(respData['userData']['userMobile']);
							var address = respData['userData']['address'];
							var arr = address.split("|");
							$('#WordNumber').val(arr[1]);
							$('#HouseNumber').val(arr[3]);
							$('#Town').val(arr[2]);
							$('#District').val(arr[0]);

							$('#AadharNumber').val(respData['userData']['aadharNumber']);
							$('#PanNumber').val(respData['userData']['panNumber']);
							$('#GSTNumber').val(respData['userData']['GSTNumber']);
							$('#UserId').val(respData['userData']['userId']);

							if (respData['userData']['userImage'] != '') {
								var itemSrc = base_url + 'assets/uploads/users/' + respData['userData']['userImage'];
								$('#blah').attr("src", itemSrc);
							}

							if (respData['userData']['shopImage'] != '') {
								var itemSrc = base_url + 'assets/uploads/users/' + respData['userData']['shopImage'];
								$('#shopblah').attr("src", itemSrc);
							}

							if (respData['userData']['aadharImage'] != '') {
								var itemSrc = base_url + 'assets/uploads/users/' + respData['userData']['aadharImage'];
								$('#aadharblah').attr("src", itemSrc);
							}

							if (respData['userData']['panImage'] != '') {
								var itemSrc = base_url + 'assets/uploads/users/' + respData['userData']['panImage'];
								$('#panblah').attr("src", itemSrc);
							}

							setTimeout(function () {
								$('#hdaWholesalerUser').text('Update');
								$("#ItemCreateItemBtn").trigger("click");
								_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							}, 800);

						} else if (respData['status'] == 0) {
							_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							toastr.error(respData['error']);
						}
					},
					error: function () {
						_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i></i>');
						toastr.error('Something went wrong, please try.');
					}
				});
			}
		});

		// select active user and inactive


		$(document).on('change', '#cancelCustomer', function () {
	
			var value = $(this).val();
			if (value != "") {

				if (value == "Customers") {
					var furl = "directsales/cancelled_orders/customer";
					window.location.href = base_url + furl;
				}
			}

		});

		$(document).on('change', '#coptionStatus', function () {
			var value = $(this).val();
			if (value != "") {

				if (value == "Active") {
					var furl = "user";
					window.location.href = base_url + furl;
				} else {
					var furl = "user/incustomers";
					window.location.href = base_url + furl;
				}
			}

		});

		$(document).on('change', '#optionStatus', function () {
			var value = $(this).val();
			if (value != "") {

				if (value == "Active") {
					var furl = "user/wholesaler";
					window.location.href = base_url + furl;
				} else {
					var furl = "user/inwholesaler";
					window.location.href = base_url + furl;
				}
			}

		});


		// popup value reset all form

		$(document).on('click', '.mfwPopUpClose', function () {
			$(".section_wh").addClass("wholesalerinfo");
			$('#hdaWholesalerUser').text('Submit');
			reset_usermodalpop();
			$('#UserEmail').attr('readonly', false);
			$('#UserEmail').val('');
			$('#UserMobile').val('');
			$('#UserMobile').attr('readonly', false);
			$("#stock_unit").data('unselecting');

		});

		function reset_usermodalpop() {

			$('#mfwUserFormId,#hdaWholesalerFormId,#mfwItemFormId').find('input[type=text],input[type=hidden], input[type=email]').val('');
			$('#mfw_UserUpdateId,#hdaWholesalerFormId,#mfwItemFormId').val('');
			$('#ItemImageDiv').find('input,img').remove();
			$('#mfwUserFormId,#hdaWholesalerFormId,#mfwItemFormId').trigger("reset");
			$('#blah,#shopblah,#aadharblah,#panblah').attr("src", "#");
		}


		$(document).on('click', '#Generate_barcode_btn', function () {

			// window.print();
			var barcode_itemcode = $('#Barcode_item_code').val();
			var item_name = $('#Barcode_item_name').val();
			var barcode_qty = $('#Barcode_qty').val();
			var error = 0;

			if (barcode_itemcode == '') {
				toastr.error('Please enter item code');
				error++;
			}
			if (error == 0) {
				
				JsBarcode("#barcode", barcode_itemcode, {
					format: "CODE39",
					width: 1,
					height: 60,
					fontSize: 5,
					displayValue: true,
					text: item_name,
				});
				JsBarcode("#barcode1", barcode_itemcode, {
					format: "CODE39",
					width: 1,
					height: 60,
					fontSize: 5,
					displayValue: true,
					text: item_name,
				});
				printDiv();



			}


		});


		// search value form


		$(document).on('submit', '#searchItem', function (e) {

			e.preventDefault(); // avoid to execute the actual submit of the form.

			var _this = $(this);
			var furl = _this.attr('data-furl');
			var search_item = $('.search_item').val();
			var searchOption = $('#searchOption').val();
			if (search_item != "") {
				var item = "/" + search_item;
				if (searchOption != 0) {
					if (searchOption == "items") {
						var furl = "dashboard/items_master";
						window.location.href = base_url + furl + item;
					} else {
						var cat = "/" + searchOption;
						window.location.href = base_url + furl + item + cat;
					}

				} else {
					var furl = "dashboard/items_master";

					window.location.href = base_url + furl + item;
				}
			}

		});


		$(document).on('change', '#colorpicker', function () {
			$('#hexcolor').val(this.value);
		});
		$(document).on('change', '#hexcolor', function () {
			$('#colorpicker').val(this.value);
		});

		$(document).on('change', '#LoginBgColor', function () {
			$('#hexcolor').val(this.value);
		});

		$(document).on('change', '#ThemeColor', function () {
			$('#thexcolor').val(this.value);
		});


		var $myTable = $('#mfw_ItemsTable');
		var t = $myTable.DataTable();

		$(document).on('click', '#hdTestUser', function () {
			var UserName = $('input[name="UserName"]').val();
			var UserEmail = $('input[name="UserEmail"]').val();
			var UserMobile = $('input[name="UserMobile"]').val();
			var base_url = $('#base_url').val();
			var trow = (t.column(0).data().length) + 1;
			addRow(UserName, UserEmail, UserMobile, base_url, trow);

		});

		function addRow(UserName, UserEmail, UserMobile, base_url, trow) {

			var email = "<span class='cus_email'>" + UserEmail + "</span>";
			var Items = '<a class="btn-primary icon_btn" href=' + base_url + ' target="_blank"><i class="fa fa-eye" aria-hidden="true"></i></a>';
			var Action = "<button class='btn-primary wholesaler_edit_btn icon_btn' data-id='1' title='Edit'><i class='fa fa-pencil-square-o'></i></button>";
			var Status = "<label class='switch UserStatus' data-UserStatusId='1' data-statusValue='0'><input type='checkbox' name='u_status' checked ><span class='slider round'></span></label>";
			$myTable.find('tr').each(function () {
				if ($(this).find('td:first').html() == UserName) {
					$(this).addClass('remove');
				}
			});
			t.row('.remove').remove().draw(false);
			t.row.add([
				trow,
				'',
				UserName,
				email,
				UserMobile,
				'',
				'',
				Items,
				Action,
				Status
			]).draw(false);
		}


		$(document).on('change', '#aadharimgInp', function () {
			var fileExtension = ['jpeg', 'jpg', 'png'];
			if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) {
				alert("Only formats are allowed : " + fileExtension.join(', '));
				$('#aadharimgInp').val('');
			}
		});

		$(document).on('change', '#imgInp', function () {
			var fileExtension = ['jpeg', 'jpg', 'png'];
			if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) {
				alert("Only formats are allowed : " + fileExtension.join(', '));
				$('#imgInp').val('');
			}
		});

		$(document).on('change', '#shopimgInp', function () {
			var fileExtension = ['jpeg', 'jpg', 'png'];
			if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) {
				alert("Only formats are allowed : " + fileExtension.join(', '));
				$('#shopimgInp').val('');
			}
		});

		$(document).on('change', '#panimgInp', function () {
			var fileExtension = ['jpeg', 'jpg', 'png'];
			if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) {
				alert("Only formats are allowed : " + fileExtension.join(', '));
				$('#panimgInp').val('');
			}
		});

		$(document).on('change', '#ItemImage', function () {
			var fileExtension = ['jpeg', 'jpg', 'png'];
			if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) {
				alert("Only formats are allowed : " + fileExtension.join(', '));
				$('#ItemImage').val('');
			}
		});
		$(document).on('change', '#logo_Image', function () {
			var fileExtension = ['jpeg', 'jpg', 'png'];
			if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) {
				alert("Only formats are allowed : " + fileExtension.join(', '));
				$('#logo_Image').val('');
			}
		});
		$(document).on('change', '#HeaderLogo', function () {
			var fileExtension = ['jpeg', 'jpg', 'png'];
			if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) {
				alert("Only formats are allowed : " + fileExtension.join(', '));
				$('#HeaderLogo').val('');
			}
		});
		$(document).on('change', '#LoginBoxImage', function () {
			var fileExtension = ['jpeg', 'jpg', 'png'];
			if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) {
				alert("Only formats are allowed : " + fileExtension.join(', '));
				$('#LoginBoxImage').val('');
			}
		});
		$(document).on('change', '#LoginBgImage', function () {
			var fileExtension = ['jpeg', 'jpg', 'png'];
			if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) {
				alert("Only formats are allowed : " + fileExtension.join(', '));
				$('#LoginBgImage').val('');
			}
		});
		
		/********** Update Settings *************/
        $(document).on('click', '.hdl_add_language', function () {
			var _this = $(this);
              var addnewlanguage = ($.trim($('#addnewlanguage').val())).toLowerCase();
                var existinglanguage = $('#existinglanguage').val();
                if( addnewlanguage != '' ) {
                    if( existinglanguage.search(addnewlanguage) != '-1' ) {
                       toastr.error(addnewlanguage+' is already added.');
                        return false;
                    }
                    else if( existinglanguage.search(' ') != '-1' ) {
                        // Space cannot be allowed
                        toastr.error('Space is not allowed.');
                        return false;
                    }
                }
                $('#addnewlanguage').val(addnewlanguage);
                $('#addlanguageForm').submit();
			
		});
		
		/********** Update Settings *************/
        $(document).on('click', '.hdl_delete_language', function () {
			var _this = $(this);
		var basepath = $('#base_url').val();
		
		var t = confirm("Do you really want to delete the language ?");
		if ( t == true ) {
		    $(this).remove();
			window.location= basepath+"admin/delete_selected_language/"+$('#lan_to_delete').val();
		}
		return false;
	
			
		});
        
                
        function updateSettings(commonclass) {
            if( commonclass == 'logoform' ) {
                // image upload
                $('#logoform').submit();
            }else if( commonclass == 'languageSettings' ) {
                // language settings
                var addnewlanguage = ($.trim($('#addnewlanguage').val())).toLowerCase();
                var existinglanguage = $('#existinglanguage').val();
                if( addnewlanguage != '' ) {
                    if( existinglanguage.search(addnewlanguage) != '-1' ) {
                        // already exists
                       toastr.error(addnewlanguage+' is already added.');
                        return false;
                    }
                    else if( existinglanguage.search(' ') != '-1' ) {
                        // Space cannot be allowed
                        toastr.error('Space is not allowed.');
                        return false;
                    }
                }
                $('#addnewlanguage').val(addnewlanguage);
                $('#addlanguageForm').submit();
            }
     
        }
        
        $(document).on('click', '.dblclicklang123', function () {
			var _this = $(this);
			var langKey = _this.attr('data-id');
			var btnText = _this.text();
			
			if (langKey != '') {
				$.ajax({
					method: 'post',
					url: base_url + 'admin/language_msg_update',
					data: {
						'langKey': langKey
					},
					success: function (resp) {
						var respData = JSON.parse(resp);
                        
						if (respData['status'] == 1) {
							$('#msgName').val(respData['langData']);
							$('#langKey').val(respData['langId']);
							$('.sub_title').html('<span>Language type '+ respData['langType']+'</span>');
							
							setTimeout(function () {
								$('#hdaLangMsg').text('Update');
								$("#ItemCreateItemBtn").trigger("click");
								_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							}, 800);

						} else if (respData['status'] == 0) {
							_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							toastr.error(respData['error']);
						}
					},
					error: function () {
						_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i></i>');
						toastr.error('Something went wrong, please try.');
					}
				});
			}
		});
		
		$(document).on('submit', '#hdalangForm', function () {
			var _this = $(this);
			var langKey = $('#langKey').val();
			var farm_action = $('#farm_action').val();
			var msgName = $('#msgName').val();
	        
			if (langKey != '') {
				$.ajax({
					method: 'post',
					url: base_url + farm_action,
					data: {
						'langKey': langKey ,'msgName': msgName
					},
					success: function (resp) {
						var respData = JSON.parse(resp);
                        return false;
						if (respData['status'] == 1) {
							
						toastr.success(respData['msg']);
						setTimeout(function () {
						//	location.reload();
						}, 800);

						} else if (respData['status'] == 0) {
							_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i>');
							toastr.error(respData['error']);
						}
					},
					error: function () {
						_this.html(btnText + ' <i class="fa fa-pencil-square-o"></i></i>');
						toastr.error('Something went wrong, please try.');
					}
				});
			}
		});
		
	});

})(jQuery);