<?php 
//include "k_files/k_config.php";
$k_head_keywords = "";
$k_head_desc = "Admin Template";
$k_head_author = "KreonSolutions.com";
$k_head_login_check = 1;
$k_page_title = $k_head_title; 
include "k_files/k_header.php";
if($_SESSION['user_id']){
	db::getInstance()->enableLog = 1;
}

$k_table_title = $k_head_title = $k_table_log_button;
$FormID = isset($FormID) ? $FormID : (isset($_GET['form']) ? $_GET['form'] : 0);
$k_debug = isset($k_debug) ? $k_debug : 0;
$radio = array();
//if($_SESSION["user_id"] == 1)    
$bypassUserRoles = 1; //else $bypassUserRoles = 0;
// if($FormID == 5217)   $k_debug=1;
$k_user_id = $_SESSION["user_id"];
// if($k_user_id == 1020)   $k_debug=1;
//include_once('dbClass.php');
// echo "hhh".$_SESSION['dbName'];
// echo "hhh".$_SESSION['user_id'];
// echo "HHHH".$_SESSION['dbHost'];

function checkVariable($str){ //CHECK IF THERE IS A VARIABLE AND CONVERT IT
	if(strpos($str,"$") !== false){
		//echo "<br/>c".$cc = substr($str, strpos($str,"$"));	
		//echo "<br/>p".$pp = substr($cc,0,strpos($cc," "));
		$cc = substr($str, strpos($str,"$"));
		$pp = substr($cc,0,strpos($cc," "));
		$pp = " ". $pp;
		$ini = strpos($pp, "[");
		$ini += 1;
		$len = strpos($pp, "]", $ini) - $ini;
		$dd = substr($pp, $ini, $len);
		//replace Session for this below value
		return $_SESSION[$dd];
	}else{
		return $str;
	}
}

?>

<script>
	window.onload = function(e) { // Focus On first Input field
		// $('#form').find('input[type=text]').eq(0).focus();
		var url = window.location.href;
		if(url.indexOf("preview") != -1){
			console.log($("#form select.select2-hidden-accessible").length);
			$("#form :input").prop("disabled", true);


			// Disable all Select2 UI interactions (dropdowns, styling)
			// setTimeout(function () {
			// 	const $selects = $("#form select.select2-hidden-accessible");
			// 	console.log("Matched selects: ", $selects.length);

			// 	$selects.each(function () {
			// 		const $select = $(this);
			// 		$select.on("select2:opening select2:open", function (e) {
			// 			e.preventDefault();
			// 		});

			// 		$select.next(".select2").find(".select2-selection").css({
			// 			"pointer-events": "none",
			// 			"background-color": "#eee",
			// 			"cursor": "not-allowed"
			// 		});
			// 	});
			// }, 200); // Give time for Select2 to render

			$(document).on('select2:open', function (e) {
				$('select.select2-hidden-accessible').each(function () {
					// Prevent it from opening on focus (e.g. via tabindex)
					$(this).select2('close');

					$(this).on('select2:opening select2:open', function (e) {
						e.preventDefault();
					});

					$(this).next('.select2').find('.select2-selection').css({
						'pointer-events': 'none',
						'background-color': '#eee',
						'cursor': 'not-allowed'
					});
				});
			});


			$("input[name='saveButton']").remove();
			$(".switch").css("pointer-events","none");
			$('#form a').each(function(index) {
				const $a = $(this);
				const $parentDiv = $a.parent();

				if ($a.has('img').length) {
					// Prevent click on image
					$a.on('click', function(e) {
						if ($(e.target).is('img')) {
							e.preventDefault();
						}
					});

					// Hide the span sibling (e.g., the delete "X" button)
					$parentDiv.find('span').hide();
				} else {
					// Hide the <a> if no image
					$a.hide();
				}
			});
			$("#backButton").show();
			// $("input[id='previewEditButton']").show();
			$("#previewEditButton").show();
			$("#previewDeleteButton").show();
			if ($("#previewBtnEinvoice").length) $("#previewBtnEinvoice").show();
			if ($("#previewBtnEinvoice").length) $("#previewBtnEinvoice").attr("disabled", false);
			if ($("#previewBtnEway").length) $("#previewBtnEway").show();
			if ($("#previewBtnEway").length) $("#previewBtnEway").attr("disabled", false);
			$("#formAddNewBtn").show();
			$("#adjamount").removeAttr('disabled');
			$("#adjamount1").removeAttr('disabled');

			//voucher print modal open in preview page. enabled some button & field here
			$("#isVoucherPrintBtn").show();
			$("#reportTemplateSelect").removeAttr('disabled');
			$("#printModalPrintBtn").removeAttr('disabled');
			$("#printModalCloseBtn").removeAttr('disabled');

			//Adjustment form button code
			$("#prevButton").attr("disabled",false);
			
		}
		
		if(url.indexOf("display") != -1){
			
			window.validateOnLostFocusSp = true;
			$("#form :input").prop("disabled", true);
			$(':file').parent().css('pointer-events','').css("cursor","not-allowed").css("background-color","#eee");
			$("input[name='saveButton']").hide();
			// $('#form select').css("cursor","not-allowed");
			$(".switch").css("pointer-events","none");

			$('#form a').each(function(index) {
				$(this).hide();
			});
			$("#backButton").show();
			$("#form :input").each(function() {
    			var formInputId= $(this).attr('id');
				if(formInputId.indexOf('-text') != -1){
					$('#'+formInputId).css('cursor','').css("cursor","not-allowed").css("background-color","#eee");

				}

				// if(formInputId.indexOf('MultyDDModal') != -1){
				// 	if($('#'+formInputId).prop('type') !="button"){
				// 		$('#'+formInputId).css('cursor','').css("cursor","not-allowed").css("background-color","#eee");
				// 	}
				// }
			});

			
			// $("input[id='previewEditButton']").show();
			// $("#previewEditButton").show();
			// $("#previewDeleteButton").show();

		}
	}

	function openWindowWithPost(url, params, windowOptions) {

		if(url.length > 2){
			var form = document.createElement("form");
			form.setAttribute("method", "post");
			form.setAttribute("action", url);
			form.setAttribute("target", windowOptions);
			// form.setAttribute("target", newWindow.name);  // Set the form's target to the newly opened window


			for (var key in params) {
				if (params.hasOwnProperty(key)) {
					var hiddenField = document.createElement("input");
					hiddenField.setAttribute("type", "hidden");
					hiddenField.setAttribute("name", key);
					hiddenField.setAttribute("value", params[key]);

					form.appendChild(hiddenField);
				}
			}

			document.body.appendChild(form);
			form.submit();
			document.body.removeChild(form);
		}
	}


	// function PreviewForm(FormID,FieldName,EditId){
	// 	console.log(FormID,FieldName,EditId);
	// 	// Set window options (width, height)
	// 	var windowOptions = "width=800,height=600,resizable=yes,scrollbars=yes";

	// 	if(FieldName == 'adjamount1'){
	// 		openWindowWithPost("list-data.php?form=" + FormID, { prevFormid: <?php echo $FormID; ?>, partyid: $("#partyid").val(), editID: EditId, entryid:$("#editID").val()  },windowOptions); //editID: $("#editID").val(), 
	// 	}else{
	// 		openWindowWithPost("list-data.php?form=" + FormID, { prevFormid: <?php echo $FormID; ?>, partyid: $("#partyid").val(), entryid: $("#editID").val() },windowOptions);
	// 	}
	// }

	function load_data(is_variable,is_value){	
		var dataTable = $('#datatable-table').DataTable({
			"processing":true,
			"serverSide":true,
			"order":[],
			"ajax":{
				url:"fetch.php",
				type:"POST",
				data:{FormID:FormID,is_variable:is_variable,is_value:is_value}
			},
			// "columnDefs":[
			// 	{
			// 	"targets":[0],
			// 	"orderable":false,
			// 	},
			// ],
			orderCellsTop: true,
		});
	}

	//Bank reco on date change function
	// function handleBankRecoDateChange(thiss) {
	// 	// Get closest row
	// 	var $row = $(thiss).closest("tr");

	// 	var clearDt = $(thiss).val();
	// 	var entrydt = $row.find('input[name="entrydt"]').val();


	// 	// Convert to Date objects
	// 	var clearDate = new Date(clearDt);
	// 	var entrydt = new Date(entrydt);

	// 	// Normalize to only date part (removes time zone/offset issues)
	// 	clearDate.setHours(0, 0, 0, 0);
	// 	entryDate.setHours(0, 0, 0, 0);


    // 	// Check if date is between range
    // 	if (clearDate >= entrydt) {
	// 		console.log("✅ Date is within range");

	// 		var drAmount = parseFloat($row.find('input[name="dr_amount"]').val()) || 0;
	// 		var crAmount = parseFloat($row.find('input[name="cr_amount"]').val()) || 0;
	// 		var $adjInput = $('#PopupAdjamount');
	// 		var adjAmount = parseFloat($adjInput.val()) || 0;

	// 		// Prevent duplicate adjustment: use data attribute as a flag
	// 		if (!$row.data('adjusted')) {
	// 			if (crAmount === 0) {
	// 				// Subtract drAmount from adjAmount
	// 				adjAmount -= drAmount;
	// 			} else if (drAmount === 0) {
	// 				// Add crAmount to adjAmount
	// 				adjAmount += crAmount;
	// 			}

	// 			$adjInput.val(adjAmount.toFixed(2)); // update with two decimals
	// 			$row.data('adjusted', true); // mark row as adjusted
	// 		}

	// 	} else {
	// 		console.log("❌ Date is out of range");
	// 		alert("Date is outside the allowed range!");
	// 		$(thiss).val(''); // Reset the value
	// 		$row.data('adjusted', false); // allow adjustment again
	// 		return;
	// 	}
	// }

	function handleBankRecoDateChange(thiss) {
		var $row = $(thiss).closest("tr");

		var clearDt = $(thiss).val();
		var entryDtVal = $row.find('input[name="entrydt"]').val();

		// Convert to Date objects (ensure only date part is compared)
		var clearDate = new Date(clearDt);
		var entryDate = new Date(entryDtVal);

		// Normalize to only date part (removes time zone/offset issues)
		clearDate.setHours(0, 0, 0, 0);
		entryDate.setHours(0, 0, 0, 0);

		// Check if cleardt >= entrydt
		if (clearDate >= entryDate) {
			console.log("✅ Clear date is valid");

			var drAmount = parseFloat($row.find('input[name="dr_amount"]').val()) || 0;
			var crAmount = parseFloat($row.find('input[name="cr_amount"]').val()) || 0;
			var $adjInput = $('#PopupAdjamount'); // <-- fix here
			var adjAmount = parseFloat($adjInput.val()) || 0;

			// Prevent duplicate adjustment
			if (!$row.data('adjusted')) {
				if (crAmount === 0) {
					adjAmount -= drAmount;
				} else if (drAmount === 0) {
					adjAmount += crAmount;
				}

				$adjInput.val(adjAmount.toFixed(2));
				$row.data('adjusted', true);
			}

		} else {
			console.log("❌ Clear date is before entry date");
			alert("Clear Date cannot be earlier than Entry Date!");
			$(thiss).val('');
			$row.data('adjusted', false); // allow retry
		}
	}


	function saveBankRecoDetails(){
		var HeaderData = [];
		var bodyData = [];
		HeaderData.push('id');
		$('#ddTable').find('th').each(function(){
			var headerText = $(this).text().replace(/[\t\n]/g, '').trim()
			if (headerText != "") {
				HeaderData.push(headerText);
			}
		});
		console.log(HeaderData);

		$('#ddTable').find('tbody tr').each(function(){
			var rowData = []; 
			$(this).find('td').each(function(){
				if($(this).text() == "" || $(this).text() == null || $(this).text() == undefined){
					if($(this).find('input').val() == undefined){
						rowData.push('');
					}else{
						rowData.push($(this).find('input').val());
					}
				}else{
					rowData.push($(this).text());
				}
			});
			bodyData.push(rowData);

		});
		console.log(bodyData);

		// Create JSON object as key-value pairs using HeaderData and bodyData
		var jsonResult = [];

		for (var i = 0; i < bodyData.length; i++) {
			var rowObject = {};
			for (var j = 0; j < HeaderData.length; j++) {
				rowObject[HeaderData[j]] = bodyData[i][j];
				rowObject['CompanyId'] = <?php echo $_SESSION['dbCompany']; ?>;
				rowObject['DivisionId'] = <?php echo $_SESSION['dbDivision']; ?>;
				rowObject['YearCode'] = <?php echo $_SESSION['dbYear']; ?>;
				rowObject['FormID'] = <?php echo $FormID; ?>;
				rowObject['UserID'] = <?php echo $_SESSION['user_id']; ?>;
				rowObject['ssdt'] = $('#ssdt').val();
				rowObject['lldt'] = $('#lldt').val();
				rowObject['bname'] = $('#bname').val();
				
			}
			jsonResult.push(rowObject);
		}
		console.log(jsonResult);

		$.ajax({
			type : 'POST',
			dataType: 'json',
			data : { jsonData : jsonResult, FormID:<?php echo $FormID; ?>, generatedUniqueId: $("#kreon-grid-agenerateduniqueid").val() },
			url : "storeAdjAmount.php",
			success : function(result){
			}
		})
	}


	//In adjustmentamount click on ok button pending value get into adjamt text field & do calculation
	function autoFill(thiss,gridFieldName){
		var pendingVal = $(thiss).closest("tr").find('input[name="pending"]').val();
		var adjamount = $(thiss).closest("tr").find('input[name="adjamount"], input[name="adjamt"]').val();
		var balanceVal = $('#PopupBalance').val();

		if(adjamount == 0){
			if(parseInt(pendingVal) < parseInt(balanceVal)){
				$(thiss).closest("tr").find('input[name="adjamount"], input[name="adjamt"]').val(pendingVal);
			}else{
				$(thiss).closest("tr").find('input[name="adjamount"], input[name="adjamt"]').val(balanceVal);
			}
			getBillCalculation($(thiss),'click',gridFieldName);	
		}
		// console.log($(thiss).closest("tr").next());
		$(thiss).closest("tr").next().find('input[name="adjamount"], input[name="adjamt"]').focus();
		
	};

	//On click saveAdjAmount button call ajax & save data
	function saveAdjAmtData(){
		var HeaderData = [];
		var bodyData = [];

		// $('#ddTable').find('th').each(function(){
		// 	if($(this).text() != "" && $(this).text() != null && $(this).text() != undefined){
		// 		HeaderData.push($(this).text());
		// 	}
		// });

		HeaderData.push('id');
		$('#ddTable').find('th').each(function(){
			var headerText = $(this).text().replace(/[\t\n]/g, '').trim()
			if (headerText != "") {
				HeaderData.push(headerText);
			}
		});
		// console.log(HeaderData);

		$('#ddTable').find('tbody tr').each(function(){
			var rowData = []; 
			$(this).find('td').each(function(){
				if($(this).text() == "" || $(this).text() == null || $(this).text() == undefined){
					if($(this).find('input').val() == undefined){
						rowData.push('');
					}else{
						rowData.push($(this).find('input').val());
					}
				}else{
					rowData.push($(this).text());
				}
			});
			bodyData.push(rowData);

		});
		// console.log(bodyData);

		// Create JSON object as key-value pairs using HeaderData and bodyData
		var jsonResult = [];

		timestamp = 1293683278;
		var date = new Date;

		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		var day = date.getDate();
		var hours = date.getHours();
		var minutes = date.getMinutes();
		var seconds = date.getSeconds();

		var CreatedDate = "'"+year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds+"'";
	
		var GridId = $('#PopupGridId').val();
		var gridIdentifier = $('#grid-id-'+GridId).attr('class');		
		var gridSerialNo = gridIdentifier.slice(4);

		var prevCnt = $("#cnt"+gridSerialNo).val();	
		var prevCnt = parseInt(prevCnt) + 1;

		for (var i = 0; i < bodyData.length; i++) {
			var rowObject = {};
			for (var j = 0; j < HeaderData.length; j++) {
				rowObject[HeaderData[j]] = bodyData[i][j];
				rowObject['srno'] = prevCnt;
				rowObject['CreatedAt'] = CreatedDate;
				rowObject['CompanyId'] = <?php echo $_SESSION['dbCompany']; ?>;
				rowObject['DivisionId'] = <?php echo $_SESSION['dbDivision']; ?>;
				rowObject['YearCode'] = <?php echo $_SESSION['dbYear']; ?>;
				rowObject['FormID'] = <?php echo $FormID; ?>;
				rowObject['UserID'] = <?php echo $_SESSION['user_id']; ?>;
				rowObject['generateduniqueid'] = $("#kreon-grid-"+gridSerialNo+"generateduniqueid").val();//data1.generatedUniqueId;
			}
			jsonResult.push(rowObject);
		}
		// console.log(jsonResult);
		//disabled button
		$('#saveAdjAmtButton').attr('disabled', true);
		
		$.ajax({
			type : 'POST',
			dataType: 'json',
			data : { jsonData : jsonResult, FormID:<?php echo $FormID; ?>, generatedUniqueId: $("#kreon-grid-agenerateduniqueid").val() },
			url : "storeAdjAmount.php",
			success : function(result){
				
				if(result['data']['Success'] == 1){
					if(result['data']['GridResponse']){
						var gridResponseFields = result['data']['GridResponse'].split('|');

						for(var k=0; k < gridResponseFields.length; k++){
							var gridResponseFieldsArray = gridResponseFields[k].split(':');

							var type = $('#'+gridResponseFieldsArray[0]).prop('type');
							if(type == 'text' || type == 'date' || type == 'number' || type == 'email' || type == 'textarea' || type == 'time'){
								$('#'+gridResponseFieldsArray[0]).val(gridResponseFieldsArray[1]);
							}
						}
					}

					var valueToSet = $('#PopupAdjamount').val();
					var prefix = '#kreon-grid-' + gridSerialNo;
					var input1 = $(prefix + 'adjamount-text');
					var input2 = $(prefix + 'adjamt-text');
					// $('#kreon-grid-'+gridSerialNo+'adjamount-text').val($('#PopupAdjamount').val());
					if (input1.length) {
						input1.val(valueToSet);
					} else if (input2.length) {
						input2.val(valueToSet);
					}

					// $('#kreon-grid-'+gridSerialNo+'adjamount').val($('#PopupAdjamount').val());
					var mainInput1 = $(prefix + 'adjamount');
					var mainInput2 = $(prefix + 'adjamt');

					if (mainInput1.length) {
						mainInput1.val(valueToSet);
					} else if (mainInput2.length) {
						mainInput2.val(valueToSet);
					}

					$("#DropDownModal").modal("hide"); 
					$('#saveAdjAmtButton').attr('disabled', false);
				}
			}
		});

	}

	//On keydown event calculate balance
	function getBillCalculation(thiss,type,gridFieldName){
		
		// alert($(thiss).closest("tr").find('input[name="PENDING"]').val());

		var currentRow = $(thiss).attr('id');
		var max = $(thiss).closest("tr").find('input[name="adjamount"], input[name="adjamt"]').attr("max");
        var min = $(thiss).closest("tr").find('input[name="adjamount"], input[name="adjamt"]').attr("min");

		var currentAdjAmount = $(thiss).closest("tr").find('input[name="adjamount"], input[name="adjamt"]').val();
		var pendingAmt = $(thiss).closest("tr").find('input[name="pending"]').val();

		if(type='click'){
			var currentAmount = $(thiss).closest("tr").find('input[name="pending"]').val();
		}else{
			var currentAmount = $(thiss).val();
		}

		
		if(parseInt(currentAmount) > min && parseInt(currentAmount) <= max){
			var adjAmount = 0;
			$("table > tbody > tr").each(function() {
				// if($(this).find("input").is('#adjamount')){
				// 	var currentFieldValue = parseInt($(this).find('#adjamount').val());
				// 	console.log("currentFieldValue",currentFieldValue);
				// 	adjAmount += parseInt(currentFieldValue);
				// }

				var input = $(this).find("input#adjamount, input#adjamt");

				if (input.length) {
					var currentFieldValue = parseInt(input.val()) || 0;
					console.log("currentFieldValue", currentFieldValue);
					adjAmount += currentFieldValue;
				}
			});

			var amount = $('#PopupAmount').val();
			var balance = parseInt(amount)-parseInt(adjAmount);

			if(balance != NaN){
				if(balance >= 0){
					$('#PopupBalance').val(balance);
					$(thiss).closest("tr").find('input[name="balance"]').val(parseInt(pendingAmt) - parseInt(currentAdjAmount))

				}else{
					if(event.keyCode != 8 && event.keyCode != 46 && event.keyCode != 9 && event.keyCode != 01){
						event.preventDefault();

						adjAmount -= parseInt($(thiss).closest("tr").find('input[name="adjamount"],input[name="adjamt"]').val());
						
						$(thiss).closest("tr").find('input[name="adjamount"],input[name="adjamt"]').val(0);
						// $(thiss).closest("tr").find('input[name="adjamount-text"]').val(currentAmount.slice(0,-1));
						// console.log($(thiss).closest("tr").find('input[name="adjamount"]') +" == "+ $('#PopupAdjamount').val());
						
						alert('Balance amount is not less than 0.');
					}
					
				}
			}

			if(isNaN(adjAmount)){
				$('#PopupAdjamount').val(0);
			}else{
				$('#PopupAdjamount').val(adjAmount);
			}

			var valueToSet = $('#PopupAdjamount').val();
			var prefix = '#kreon-grid-a' //+ gridSerialNo;
			var input1 = $(prefix + 'adjamount-text');
			var input2 = $(prefix + 'adjamt-text');

			// $('#kreon-grid-'+gridSerialNo+'adjamount-text').val($('#PopupAdjamount').val());
			if (input1.length) {
				input1.val(valueToSet);
			} else if (input2.length) {
				input2.val(valueToSet);
			}

			// $('#kreon-grid-'+gridSerialNo+'adjamount').val($('#PopupAdjamount').val());
			var mainInput1 = $(prefix + 'adjamount');
			var mainInput2 = $(prefix + 'adjamt');

			if (mainInput1.length) {
				mainInput1.val(valueToSet);
			} else if (mainInput2.length) {
				mainInput2.val(valueToSet);
			}

			// $("#kreon-grid-aadjamount-text").val($('#PopupAdjamount').val());
			// $("#kreon-grid-aadjamount").val($('#PopupAdjamount').val());

		}else{
		
			if(event.keyCode != 8 && event.keyCode != 46 && event.keyCode != 9 && event.keyCode != 01){
				event.preventDefault();
			
				$(thiss).closest("tr").find('input[name="adjamount"],input[name="adjamt"]').val(currentAmount.slice(0,-1));
				alert('Adjustment amount is greater than balance amount.');
			}
		}

	}

	//Deselect row on cross button
	function deselectRow(deselectRowId,deselectRowLabel,DBFieldName){
		$("#ddTable tr td:nth-child(1)").filter(function() {
			return $(this).text().trim() == deselectRowId;
		}).parent('tr').removeClass('DTTT_selected selected ');

		$("#pill-"+deselectRowId).remove();

		MDDModalHiddenValue = $('#MultyDDModal-hidden-'+DBFieldName).val();
		MDDModalHiddenValueArray = MDDModalHiddenValue.split(",");

		MDDModalLabel = $('#MultyDDModal-'+DBFieldName).val();
		MDDModalLabelArray = MDDModalLabel.split(",");

		for (let i = 0; i < MDDModalHiddenValueArray.length; i++) {
			if (MDDModalHiddenValueArray[i] == deselectRowId) {
				MDDModalHiddenValueArray.splice(i, 1);
				MDDModalLabelArray.splice(i, 1);

				break;
			}
		}

		MDDModalHiddenValueString = MDDModalHiddenValueArray.toString();
		$('#MultyDDModal-hidden-'+DBFieldName).val()
		$('#MultyDDModal-hidden-'+DBFieldName).val(MDDModalHiddenValueString);
		
		MDDModalLabelString = MDDModalLabelArray.toString();
		$('#MultyDDModal-'+DBFieldName).val();
		$('#MultyDDModal-'+DBFieldName).val(MDDModalLabelString);

	}

	function deselectAllRow(){
		var currentField = $('#DropDownModalID').val();
		var splitCurrentField = currentField.split('-');
		var currentFieldValue = splitCurrentField[0]+"-hidden-"+splitCurrentField[1];
		var currentFieldValues = $('#'+currentFieldValue).val();
		var currentFieldValuesArray = currentFieldValues.split(',');

		// var rows = $('#ddTable').rows('.DTTT_selected selected').remove().draw();

		for(var i=0; i<currentFieldValuesArray.length; i++){
			$("#ddTable tr td:nth-child(1)").filter(function() {
				return $(this).text().trim() == currentFieldValuesArray[i];
			}).parent('tr').removeClass('DTTT_selected selected ');
	
			$("#pill-"+currentFieldValuesArray[i]).remove();
		}

		$('#'+currentFieldValue).val('');
		$('#MultyDDModal-'+splitCurrentField[1]).val('');

		$("#pillContainer").empty();
		$('#pillContainerClear').css("display","none");
	}

	$("#sub_registerid").on("select2-opening", function() { 
       $("#sub_registerid").on("select2-open", function() { 
         $("#sub_registerid").select2("search","BBB");
       });
    });


	function getSheetData(sheetsMap, sheetName, XLSX, shDt){
		const sheetData = sheetsMap[sheetName]?.data || [];
		console.log(sheetName, shDt, sheetData, "<=");
		sheetData.forEach((row, i) => {
			const values = Object.values(row);
			XLSX.utils.sheet_add_aoa(shDt, [values], { origin: { r: 4 + i, c: 0 } }); // Start from row 6 (index 5)
		});
	}

	async function CallSpForFieldType24(requestParams,GridResponse,SpName,PopulateGridID,editid,userid,gridid,PopulateField,FType = null){
		try {
			var result = await $.ajax({
				type: 'POST',
				dataType: 'json',
				data: {
					requestParams:requestParams, 
					SpName:SpName, 
					editid:editid, 
					userid:userid, 
					gridid:gridid, 
					PopulateField:PopulateField, 
					FType:FType, 
					FormID:<?php echo $FormID; ?>,
					FormData: $('form').serialize()
				},
				url: 'getFieldType24SpResponse.php'
			});

			console.log(result);
			if(FType == 'GSTR1'){
				if(result['status']){
					var response = result;
					// const jsonData = await response.json();
					jsonData= response;
					console.log('jsonData',jsonData);
					// Check if the response is valid
					if (jsonData.status === "true" && jsonData.sheets) {
						let wb = XLSX.utils.book_new(); // Create a new workbook
						var cnt = 1;
						// Extract API sheets as a dictionary for easy access
						const sheetsMap = {};
						jsonData.sheets.forEach(s => {
							sheetsMap[s.sheetName] = s;
						});
						var b2bSezDeData = [
							['Summary For B2B, SEZ, DE (4A, 4B, 6B, 6C)', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							['No. of Recipients','', 'No. of Invoices', '','Total Invoice Value', '', '', '', '', '', '','Total Taxable Value', 'Total Cess'],
							[null, '', null, '', '', '', null, '', '', '', '', null, null],
							['GSTIN/UIN of Recipient', 'Receiver Name', 'Invoice Number', 'Invoice Date', 'Invoice Value', 'Place Of Supply', 'Reverse Charge', 'Applicable % of Tax Rate', 'Invoice Type', 'E-Commerce GSTIN', 'Rate', 'Taxable Value', 'Cess Amount']
						];
						let b2bSezDeSheet = XLSX.utils.aoa_to_sheet(b2bSezDeData);
						b2bSezDeSheet['A3'] = { t: 'n', f: 'COUNTA(A5:A2000)' }; // Count recipients
						b2bSezDeSheet['C3'] = { t: 'n', f: 'COUNTA(C5:C2000)' }; // Count invoices
						b2bSezDeSheet['E3'] = { t: 'n', f: 'SUM(E5:E2000)' }; // Count invoices
						b2bSezDeSheet['L3'] = { t: 'n', f: 'SUM(L5:L2000)' }; // Total Taxable Value
						b2bSezDeSheet['M3'] = { t: 'n', f: 'SUM(M5:M2000)' }; // Total Cess
						getSheetData(sheetsMap, 'b2b,sez,de', XLSX, b2bSezDeSheet)

						var b2bData = [
							['Summary For B2B', 'HELP', '', '', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							['No. of Recipients', 'No. of Invoices', 'Total Invoice Value', '', '', '', '', '', '', '', 'Total Taxable Value', 'Total Cess', '', ''],
							[null, '', null, '', '', '', '', '', '', '', null, null, null, null],
							['GSTIN/UIN of Recipient', 'Receiver Name', 'Invoice Number', 'Invoice Date', 'Invoice Value', 'Place Of Supply', 'Reverse Charge', 'Applicable % of Tax Rate', 'Invoice Type', 'E-Commerce GSTIN', 'Rate', 'Taxable Value', 'Cess Amount']
						];
						let b2bSheet = XLSX.utils.aoa_to_sheet(b2bData);
						b2bSheet['A3'] = { t: 'n', f: 'SUMPRODUCT((A5:A20004<>"")/COUNTIF(A5:A20004,A5:A20004&""))' }; // Formula for cell A3
						b2bSheet['C3'] = { t: 'n', f: 'COUNTA(C5:C2000)' }; // Formula for cell C3
						b2bSheet['G3'] = { t: 'n', f: 'SUM(G5:G2000)' }; // Formula for cell G3
						b2bSheet['N3'] = { t: 'n', f: 'SUM(N5:N2000)' }; // Formula for cell N3
						b2bSheet['O3'] = { t: 'n', f: 'SUM(O5:O2000)' }; // Formula for cell O3
						getSheetData(sheetsMap, 'b2ba', XLSX, b2bSheet);

						// b2cl Sheet
						var b2clData = [
							['Summary For B2CL', 'HELP', '', '', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							['No. of Invoices', 'Invoice Number', 'Invoice Date', 'Invoice Value', 'Place Of Supply', 'Applicable % of Tax Rate', 'Rate', 'Taxable Value', 'Cess Amount'],
							[null, '', null, '', '', '', null, null, null, null],
							['Invoice Number', 'Invoice Date', 'Invoice Value', 'Place Of Supply', 'Rate', 'Taxable Value', 'Cess Amount']
						];
						let b2clSheet = XLSX.utils.aoa_to_sheet(b2clData);
						b2clSheet['B3'] = { t: 'n', f: 'COUNTA(B5:B2000)' }; // Count invoices
						getSheetData(sheetsMap, 'b2cl', XLSX, b2clSheet);

						var b2claData = [
							['Summary For B2CLA (Amended B2C Large)', 'HELP', '', '', '', '', '', '', '', '', '', 'HELP'],
							['No. of Invoices', 'Total Inv Value', 'Total Taxable Value', 'Total Cess', '', '', '', '', '', '', '', '', 'HELP'],
							[null, '', null, '', '', '', null, '', '', '', '', null, null],
							['Original Invoice Number', 'Original Invoice Date', 'Original Place Of Supply', 'Revised Invoice Number', 'Revised Invoice Date', 'Invoice Value', 'Applicable % of Tax Rate', 'Rate', 'Taxable Value', 'Cess Amount', 'E-Commerce GSTIN']
						];

						let b2claSheet = XLSX.utils.aoa_to_sheet(b2claData);
						b2claSheet['B3'] = { t: 'n', f: 'COUNTA(B5:B2000)' }; // Count invoices
						b2claSheet['C3'] = { t: 'n', f: 'SUM(C5:C2000)' }; // Total Taxable Value
						b2claSheet['D3'] = { t: 'n', f: 'SUM(D5:D2000)' }; // Total Cess
						getSheetData(sheetsMap, 'b2cla', XLSX, b2claSheet);

						var b2csData = [
							['Summary For B2CS', 'HELP', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							['Total Taxable Value', 'Total Cess', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							[null, '', null, '', '', '', null, '', '', '', '', null, null],
							['Type', 'Place Of Supply', 'Applicable % of Tax Rate', 'Rate', 'Taxable Value', 'Cess Amount', 'E-Commerce GSTIN']
						];
						let b2csSheet = XLSX.utils.aoa_to_sheet(b2csData);
						b2csSheet['A3'] = { t: 'n', f: 'SUM(E5:E2000)' }; // Total Taxable Value
						b2csSheet['B3'] = { t: 'n', f: 'SUM(F5:F2000)' }; // Total Cess
						getSheetData(sheetsMap, 'b2cs', XLSX, b2csSheet);

						var b2csaData = [
							['Summary For B2CSA', 'HELP', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							['Total Taxable Value', 'Total Cess', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							[null, '', null, '', '', '', null, '', '', '', '', null, null],
							['Financial Year', 'Original Month', 'Place Of Supply', 'Type', 'Applicable % of Tax Rate', 'Rate', 'Taxable Value', 'Cess Amount', 'E-Commerce GSTIN']
						];
						let b2csaSheet = XLSX.utils.aoa_to_sheet(b2csaData);
						b2csaSheet['A3'] = { t: 'n', f: 'SUM(E5:E2000)' }; // Total Taxable Value
						b2csaSheet['B3'] = { t: 'n', f: 'SUM(F5:F2000)' }; // Total Cess
						getSheetData(sheetsMap, 'b2csa', XLSX, b2csaSheet);

						// cdnr Sheet
						var cdnrData = [
							['Summary For CDNR', 'HELP', '', '', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							['No. of Recipients', 'No. of Notes', 'Total Note Value', 'Total Taxable Value', 'Total Cess', 'GSTIN/UIN of Recipient', 'Receiver Name', 'Note Number', 'Note Date', 'Note Type', 'Place Of Supply', 'Reverse Charge', 'Rate', 'Taxable Value', 'Cess Amount'],
							[null, '', null, '', '', '', '', '', null, '', '', '', '', null, null],
							['GSTIN/UIN of Recipient', 'Receiver Name', 'Note Number', 'Note Date', 'Rate', 'Taxable Value', 'Cess Amount']
						];
						let cdnrSheet = XLSX.utils.aoa_to_sheet(cdnrData);
						cdnrSheet['C3'] = { t: 'n', f: 'COUNTA(C5:C2000)' }; // Example Formula for cdnr
						getSheetData(sheetsMap, 'cdnr', XLSX, cdnrSheet);

						var cdnraData = [
							['Summary For CDNRA (Amended CDNR)', 'HELP', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							['No. of Recipients', 'No. of Notes', 'Total Note Value', 'Total Taxable Value', 'Total Cess', '', '', '', '', '', '', '', 'HELP'],
							[null, '', null, '', '', '', null, '', '', '', '', null, null],
							['Original Note Number', 'Original Note Date', 'Revised Note Number', 'Revised Note Date', 'Note Type', 'Place Of Supply', 'Reverse Charge', 'Note Supply Type', 'Note Value', 'Applicable % of Tax Rate', 'Rate', 'Taxable Value', 'Cess Amount']
						];
						let cdnraSheet = XLSX.utils.aoa_to_sheet(cdnraData);
						cdnraSheet['C3'] = { t: 'n', f: 'SUM(C5:C2000)' }; // Total Note Value
						cdnraSheet['D3'] = { t: 'n', f: 'SUM(D5:D2000)' }; // Total Taxable Value
						cdnraSheet['E3'] = { t: 'n', f: 'SUM(E5:E2000)' }; // Total Cess
						getSheetData(sheetsMap, 'cdnra', XLSX, cdnraSheet);

						var cdnurData = [
							['Summary For CDNUR', 'HELP', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							['No. of Notes/Vouchers', 'Total Note Value', 'Total Taxable Value', 'Total Cess', '', '', '', '', '', '', '', '', 'HELP'],
							[null, '', null, '', '', '', null, '', '', '', '', null, null],
							['UR Type', 'Note Number', 'Note Date', 'Note Type', 'Place Of Supply', 'Note Value', 'Applicable % of Tax Rate', 'Rate', 'Taxable Value', 'Cess Amount']
						];
						let cdnurSheet = XLSX.utils.aoa_to_sheet(cdnurData);
						cdnurSheet['C3'] = { t: 'n', f: 'COUNTA(C5:C2000)' }; // Example Formula for cdnr
						getSheetData(sheetsMap, 'cdnur', XLSX, cdnurSheet);

						var cdnuraData = [
							['Summary For CDNURA', 'HELP', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							['No. of Notes/Vouchers', 'Total Note Value', 'Total Taxable Value', 'Total Cess', '', '', '', '', '', '', '', '', 'HELP'],
							[null, '', null, '', '', '', null, '', '', '', '', null, null],
							['UR Type', 'Original Note Number', 'Original Note Date', 'Revised Note Number', 'Revised Note Date', 'Note Type', 'Place Of Supply', 'Note Value', 'Applicable % of Tax Rate', 'Rate', 'Taxable Value', 'Cess Amount']
						];
						let cdnuraSheet = XLSX.utils.aoa_to_sheet(cdnuraData);
						cdnuraSheet['C3'] = { t: 'n', f: 'COUNTA(C5:C2000)' }; // Example Formula for cdnr
						getSheetData(sheetsMap, 'cdnura', XLSX, cdnuraSheet);

						var expData = [
							['Summary For EXP', 'HELP', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							['No. of Invoices', 'Total Invoice Value', 'No. of Shipping Bills', 'Total Taxable Value', 'Total Cess', '', '', '', '', '', '', '', 'HELP'],
							[null, '', null, '', '', '', null, '', '', '', '', null, null],
							['Export Type', 'Invoice Number', 'Invoice Date', 'Invoice Value', 'Port Code', 'Shipping Bill Number', 'Shipping Bill Date', 'Rate', 'Taxable Value', 'Cess Amount']
						];
						let expSheet = XLSX.utils.aoa_to_sheet(expData);
						expSheet['B3'] = { t: 'n', f: 'SUM(B5:B2000)' }; // Total Invoice Value
						expSheet['C3'] = { t: 'n', f: 'COUNT(C5:C2000)' }; // Number of Shipping Bills
						expSheet['D3'] = { t: 'n', f: 'SUM(D5:D2000)' }; // Total Taxable Value
						expSheet['E3'] = { t: 'n', f: 'SUM(E5:E2000)' }; // Total Cess
						getSheetData(sheetsMap, 'exp', XLSX, expSheet);

						var expaData = [
							['Summary For EXPA', 'HELP', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							['No. of Invoices', 'Total Invoice Value', 'No. of Shipping Bills', 'Total Taxable Value', 'Total Cess', '', '', '', '', '', '', '', 'HELP'],
							[null, '', null, '', '', '', null, '', '', '', '', null, null],
							['Export Type', 'Original Invoice Number', 'Original Invoice Date', 'Revised Invoice Number', 'Revised Invoice Date', 'Invoice Value', 'Port Code', 'Shipping Bill Number', 'Shipping Bill Date', 'Rate', 'Taxable Value', 'Cess Amount']
						];
						let expaSheet = XLSX.utils.aoa_to_sheet(expaData);
						expaSheet['B3'] = { t: 'n', f: 'SUM(B5:B2000)' }; // Total Invoice Value
						expaSheet['C3'] = { t: 'n', f: 'COUNT(C5:C2000)' }; // Number of Shipping Bills
						expaSheet['D3'] = { t: 'n', f: 'SUM(D5:D2000)' }; // Total Taxable Value
						expaSheet['E3'] = { t: 'n', f: 'SUM(E5:E2000)' }; // Total Cess
						getSheetData(sheetsMap, 'expa', XLSX, expaSheet);

						var atData = [
							['Summary For Advance Received', 'HELP', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							['Total Advance Received', 'Total Cess', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							[null, '', null, '', '', '', null, '', '', '', '', null, null],
							['Place Of Supply', 'Rate', 'Gross Advance Received', 'Cess Amount']
						];
						let atSheet = XLSX.utils.aoa_to_sheet(atData);
						atSheet['A3'] = { t: 'n', f: 'SUM(A5:A2000)' }; // Total Advance Received
						atSheet['B3'] = { t: 'n', f: 'SUM(B5:B2000)' }; // Total Cess
						getSheetData(sheetsMap, 'at', XLSX, atSheet);

						var ataData = [
							['Summary For Amended Tax Liability (Advance Received)', 'HELP', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							['Total Advance Received', 'Total Cess', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							[null, '', null, '', '', '', null, '', '', '', '', null, null],
							['Financial Year', 'Original Month', 'Original Place Of Supply', 'Rate', 'Gross Advance Received', 'Cess Amount']
						];
						let ataSheet = XLSX.utils.aoa_to_sheet(ataData);
						ataSheet['A3'] = { t: 'n', f: 'SUM(A5:A2000)' }; // Total Advance Received
						ataSheet['B3'] = { t: 'n', f: 'SUM(B5:B2000)' }; // Total Cess
						getSheetData(sheetsMap, 'ata', XLSX, ataSheet);

						var atadjData = [
							['Summary For Advance Adjusted', 'HELP', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							['Total Advance Adjusted', 'Total Cess', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							[null, '', null, '', '', '', null, '', '', '', '', null, null],
							['Place Of Supply', 'Rate', 'Gross Advance Adjusted', 'Cess Amount']
						];
						let atadjSheet = XLSX.utils.aoa_to_sheet(atadjData);
						atadjSheet['A3'] = { t: 'n', f: 'SUM(A5:A2000)' }; // Total Advance Adjusted
						atadjSheet['B3'] = { t: 'n', f: 'SUM(B5:B2000)' }; // Total Cess
						getSheetData(sheetsMap, 'atadj', XLSX, atadjSheet);

						var atadjaData = [
							['Summary For Amendment of Adjustment Advances', 'HELP', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							['Total Advance Adjusted', 'Total Cess', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							[null, '', null, '', '', '', null, '', '', '', '', null, null],
							['Financial Year', 'Original Month', 'Original Place Of Supply', 'Rate', 'Gross Advance Adjusted', 'Cess Amount']
						];
						let atadjaSheet = XLSX.utils.aoa_to_sheet(atadjaData);
						atadjaSheet['A3'] = { t: 'n', f: 'SUM(A5:A2000)' }; // Total Advance Adjusted
						atadjaSheet['B3'] = { t: 'n', f: 'SUM(B5:B2000)' }; // Total Cess
						getSheetData(sheetsMap, 'atadja', XLSX, atadjaSheet);
						
						var exempData = [
							['Summary For Nil Rated, Exempted, and Non-GST Supplies', 'HELP', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							['Total Nil Rated Supplies', 'Total Exempted Supplies', 'Total Non-GST Supplies', '', '', '', '', '', '', '', '', '', 'HELP'],
							[null, '', null, '', '', '', null, '', '', '', '', null, null],
							['Description', 'Nil Rated Supplies', 'Exempted Supplies', 'Non-GST Supplies']
						];
						let exempSheet = XLSX.utils.aoa_to_sheet(exempData);
						exempSheet['B3'] = { t: 'n', f: 'SUM(B5:B2000)' }; // Total Nil Rated Supplies
						exempSheet['C3'] = { t: 'n', f: 'SUM(C5:C2000)' }; // Total Exempted Supplies
						exempSheet['D3'] = { t: 'n', f: 'SUM(D5:D2000)' }; // Total Non-GST Supplies
						getSheetData(sheetsMap, 'exemp', XLSX, exempSheet);

						var hsnData = [
							['Summary For HSN', 'HELP', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							['No. of HSN', '','','','Total Value','', 'Total Taxable Value', 'Total Integrated Tax', 'Total Central Tax', 'Total State/UT Tax', 'Total Cess'],
							[null, '', null, '', '', '', null, '', '', '', '', null, null],
							['HSN', 'Description', 'UQC', 'Total Quantity', 'Total Value', 'Rate', 'Taxable Value', 'Integrated Tax Amount', 'Central Tax Amount', 'State/UT Tax Amount', 'Cess Amount']
						];
						let hsnSheet = XLSX.utils.aoa_to_sheet(hsnData);
						hsnSheet['A3'] = { t: 'n', f: 'COUNTA(A5:A2000)' }; // Total Value
						hsnSheet['E3'] = { t: 'n', f: 'SUM(E5:E2000)' }; // Total Value
						hsnSheet['G3'] = { t: 'n', f: 'SUM(G5:G2000)' }; // Total Taxable Value
						hsnSheet['H3'] = { t: 'n', f: 'SUM(H5:H2000)' }; // Total Integrated Tax
						hsnSheet['I3'] = { t: 'n', f: 'SUM(I5:I2000)' }; // Total Central Tax
						hsnSheet['J3'] = { t: 'n', f: 'SUM(J5:J2000)' }; // Total State/UT Tax
						hsnSheet['K3'] = { t: 'n', f: 'SUM(K5:K2000)' }; // Total Cess
						getSheetData(sheetsMap, 'hsn', XLSX, hsnSheet);

						var docsData = [
							['Summary of Documents Issued During the Tax Period', 'HELP', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							['Total Number of Documents', 'Total Cancelled Documents', '', '', '', '', '', '', '', '', '', '', 'HELP'],
							[null, '', null, '', '', '', null, '', '', '', '', null, null],
							['Nature of Document', 'Sr. No. From', 'Sr. No. To', 'Total Number of Documents', 'Cancelled']
						];
						let docsSheet = XLSX.utils.aoa_to_sheet(docsData);
						docsSheet['B3'] = { t: 'n', f: 'SUM(D5:D2000)' }; // Total Number of Documents
						docsSheet['C3'] = { t: 'n', f: 'SUM(E5:E2000)' }; // Total Cancelled Documents
						getSheetData(sheetsMap, 'docs', XLSX, docsSheet);

						// Step 3: Add these sheets to a workbook
						wb = XLSX.utils.book_new();
						XLSX.utils.book_append_sheet(wb, b2bSezDeSheet, 'b2b,sez,de');
						XLSX.utils.book_append_sheet(wb, b2bSheet, 'b2ba');
						XLSX.utils.book_append_sheet(wb, b2clSheet, 'b2cl');
						XLSX.utils.book_append_sheet(wb, b2claSheet, 'b2cla');
						XLSX.utils.book_append_sheet(wb, b2csSheet, 'b2cs');
						XLSX.utils.book_append_sheet(wb, b2csaSheet, 'b2csa');
						XLSX.utils.book_append_sheet(wb, cdnrSheet, 'cdnr');
						XLSX.utils.book_append_sheet(wb, cdnraSheet, 'cdnra');
						XLSX.utils.book_append_sheet(wb, cdnurSheet, 'cdnur');
						XLSX.utils.book_append_sheet(wb, cdnuraSheet, 'cdnura');
						XLSX.utils.book_append_sheet(wb, expSheet, 'exp');
						XLSX.utils.book_append_sheet(wb, expaSheet, 'expa');
						XLSX.utils.book_append_sheet(wb, atSheet, 'at');
						XLSX.utils.book_append_sheet(wb, ataSheet, 'ata');
						XLSX.utils.book_append_sheet(wb, atadjSheet, 'atadj');
						XLSX.utils.book_append_sheet(wb, atadjaSheet, 'atadja');
						XLSX.utils.book_append_sheet(wb, exempSheet, 'exemp');
						XLSX.utils.book_append_sheet(wb, hsnSheet, 'hsn');
						XLSX.utils.book_append_sheet(wb, docsSheet, 'docs');

						// Step 4: Write the workbook to an Excel file
						XLSX.writeFile(wb, 'GSTR1.xlsx');

					} else {
						alert('No data found or error fetching data.');
					}
				}
			}else{
				if(result['status']){
					if(result['success'] == 1){
						if(result['msg'] != null && result['msg'] != ''){
							alert(result['msg']);
							// return;
						}
						var gridIdentifier = $('#grid-id-'+gridid).attr('class');
						var gridSerialNo = gridIdentifier.slice(4);
						var GridResponseArray=[];
						var gridRes = [];
						var gridCalculation = [];	var gridCalCnt = 0;
						var gridCalculationFieldName = [];
						var gridCalculationType = [];

						for(var l=0; l<result['gridData'].length; l++){
							if(result['gridData'][l]['gridcalculation'] == 1){
								gridCalculationFieldName.push(result['gridData'][l]['dbfieldname']);
								gridCalculationType.push(result['gridData'][l]['gridcalculation']);
							}
						}


						var promises = [];
						for(var l=0; l<result['data'].length; l++){
							GridResponseArray = GridResponse.split("|");

							for(var k=0; k<GridResponseArray.length; k++){
								gridRes = GridResponseArray[k].split(":");

								if(gridRes[2] == '1'){

									if(gridRes[3] == '1'){
										if(result['data'][l][gridRes[0]] == ""){
											$('#kreon-grid-'+gridSerialNo+''+gridRes[0].trim()).css("border","1px solid red");
											$('#kreon-grid-'+gridSerialNo+''+gridRes[0].trim()).focus();
											return false;
										}
									}
									
									$('#kreon-grid-'+gridSerialNo+''+gridRes[0].trim()).val(result['data'][l][gridRes[1].trim()]);

									// mfld += "<td><input readonly type=\"text\" value=\'"+result['data'][l][gridRes[1].trim()]+"\' class=\'form-control\'   name=\''+gridSerialNo+'"+gridRes[0].trim()+"[]\'   id=\'"+gridRes[0].trim()+"\'></td>";
								}else if(gridRes[2] == '5' || gridRes[2] == '19'){

									if(gridRes[4] == '1'){
										if(result['data'][l][gridRes[0]] == ""){
											$('#kreon-grid-'+gridSerialNo+''+gridRes[0].trim()).css("border","1px solid red");
											$('#kreon-grid-'+gridSerialNo+''+gridRes[0].trim()).focus();
											return false;
										}
									}
									var $newOption = $("<option selected=\"selected\"></option>").val(result['data'][l][gridRes[3].trim()]).text(result['data'][l][gridRes[1].trim()])
									if($('#kreon-grid-'+gridSerialNo+''+gridRes[0].trim()).data('select2')) {
										$('#kreon-grid-'+gridSerialNo+''+gridRes[0].trim()).select2('open');
										$('#kreon-grid-'+gridSerialNo+''+gridRes[0].trim()).append($newOption);
										$('#kreon-grid-'+gridSerialNo+''+gridRes[0].trim()).select2('close');
									}
								
								}else if(gridRes[2].trim() == '17'){
									//Check grid field required
									if(gridRes[4] == '1'){
										if(result['data'][l][gridRes[0]] == ""){
											$('#kreon-grid-'+gridSerialNo+''+gridRes[0].trim()).css("border","1px solid red");
											$('#kreon-grid-'+gridSerialNo+''+gridRes[0].trim()).focus();
											return false;
										}
									}
									$('#kreon-grid-'+gridSerialNo+gridRes[0].trim()+'-text').val(result['data'][l][gridRes[1].trim()]);
									$('#kreon-grid-'+gridSerialNo+gridRes[0].trim()).val(result['data'][l][gridRes[3]]);
							
								}		
							}

							DynamicGridInsertFirstRowEdit = ddResponseData['DynamicGridInsertFirstRowEdit'];
							// Add new row and add the promise to the array
							promises.push(AddNewRow($(this),result['gridData'],gridSerialNo,result['data'][l][result['selecedRowIdName']], DynamicGridInsertFirstRowEdit));	
						}

						await Promise.all(promises);
						await CreateOnChangeScriptGridCalculation(gridCalculationFieldName, gridCalculationType, gridSerialNo);

						//Populate field
						if(PopulateField != null){
							PopulateFieldArray = PopulateField.split('|');
							var PopulateFieldResponseArray = [];
							for(var j=0; j<PopulateFieldArray.length; j++){
								SplitPopulateFieldArray = PopulateFieldArray[j].split(':');
								
								var PopulateFieldResponse = result['PopulateFieldResponse'][0][SplitPopulateFieldArray[0]];
								PopulateFieldResponseArray = PopulateFieldResponse.split(':');
								
								if(PopulateFieldResponseArray[0] == 1){
									if(PopulateFieldResponseArray[1].trim() == "NULL"){
									
										$("#"+SplitPopulateFieldArray[0].trim()).val('');
									}else{
										// if(SplitPopulateFieldArray[0] == 'cut_mtrs'){
										// 	var cutMtrsValue = parseInt($('#cut_mtrs').val()) + parseInt(PopulateFieldResponseArray[1]);
										// 	$("#"+SplitPopulateFieldArray[0].trim()).val(cutMtrsValue);

										// }else{

											$("#"+SplitPopulateFieldArray[0].trim()).val(PopulateFieldResponseArray[1]);
										// }
									}

								}else if(PopulateFieldResponse[0] == 5){
									var $newOption = $("<option selected=\"selected\"></option>").val(PopulateFieldResponseArray[1]).text(PopulateFieldResponseArray[2])
									if($('#'+SplitPopulateFieldArray[0].trim()).data('select2')) {
										$('#'+SplitPopulateFieldArray[0].trim()).select2('open');
										$('#'+SplitPopulateFieldArray[0].trim()).append($newOption).trigger('change');
										$('#'+SplitPopulateFieldArray[0].trim()).select2('close');
									}

								}
							}

						}
												
						if(result['cursorPosition'] !="" && result['cursorPosition'] != undefined){
							var cursorPositionSplit = result['cursorPosition'].split(':');
							console.log("cursor "+cursorPositionSplit);
							if(cursorPositionSplit[1] == 5){
								$('#'+cursorPositionSplit[0]).next('span').find('.select2-selection--single').focus();
							}else if(cursorPositionSplit[1] == 17){
								$('#'+cursorPositionSplit[0]+'-text').focus();
							}else if(cursorPositionSplit[1] == 16){
								$('#'+cursorPositionSplit[0]+'-text').focus();
							}
						}

						if(result['NonEditableFields'] != null && result['NonEditableFields'][0]['NonEditableFields'] != undefined){
							var NonEditableFields = result['NonEditableFields'][0]['NonEditableFields'].split('|');
							for(var k=0; k < NonEditableFields.length; k++){
								var NonEditableFieldArray = NonEditableFields[k].split(':');
								if(NonEditableFieldArray[1] == 'lock'){
									$('#'+NonEditableFieldArray[0]).css('pointer-events','none').css('background-color','#eee');
								}else{
									$('#'+NonEditableFieldArray[0]).css('pointer-events','').css('background-color','white');
								}
							}
						}
						// return;
						
					}else{
						alert(result['msg']);
						// if(CursorPosition !="" && CursorPosition != undefined){
						// 	$('#'+CursorPosition).focus();
						// }
					}
					
				}
			}
		} catch (error) {
			console.error('Error:', error);
		}
	}

	// async function AddResultInGrid(ddRowSelectType, selectedItemId, DBFieldType, DBFieldName, isGrid, requestParams, formReeponseString) {
	// 	var gridid = ddResponseData['PopulateGrid'].split(":");
	// 	var flag = 0;
	// 	console.log("formReeponseString "+formReeponseString);
	// 	try {
	// 		// Making AJAX call with async/await
	// 		const result = await $.ajax({
	// 			type: 'POST',
	// 			dataType: 'json',
	// 			data: {
	// 				selectedRowId: selectedItemId,
	// 				GridId: gridid[1],
	// 				FormId: <?php echo $FormID; ?>,
	// 				FieldName: DBFieldName,
	// 				DBFieldType: DBFieldType,
	// 				isGrid: isGrid,
	// 				requestParams: requestParams,
	// 				formReeponse: formReeponseString
	// 			},
	// 			url: 'getGridData.php'
	// 		});

	// 		// console.log(gridid);
	// 		var gridIdentifier = $('#grid-id-' + gridid[1].trim()).attr('class');
	// 		var gridSerialNo = gridIdentifier.slice(4);

	// 		var gridRes = [];
	// 		var gridCalculationFieldName = [];
	// 		var gridCalculationType = [];
	// 		var gridResponseParams = ddResponseData['gridResponseParams'].split("|");
	// 		var OnkeyupParameterCalculation = [];

	// 		// Process grid data for calculation and input field population
	// 		for (var t = 0; t < result['gridData'].length; t++) {
	// 			if (result['gridData'][t]['GridCalculation'] > 0) {
	// 				gridCalculationFieldName.push(result['gridData'][t]['DbFieldName']);
	// 				gridCalculationType.push(result['gridData'][t]['GridCalculation']);
	// 			}

	// 			if (result['gridData'][t]['OnkeyupParameters'].length > 2) {
	// 				if (result['gridData'][t]['FieldType'] != 5) {
	// 					OnkeyupParameterCalculation.push([result['gridData'][t]['DbFieldName'], result['gridData'][t]['FieldType'], result['gridData'][t]['OnkeyupParameters']]);
	// 				}
	// 			}
	// 		}

	// 		if (Array.isArray(result['data'])) {

	// 			var promises = [];
	// 			// Loop through the data and process each row
	// 			for (var l = 0; l < result['data'].length; l++) {
	// 				for (var t = 0; t < result['gridData'].length; t++) {
	// 					if (result['gridData'][t]['Required'] == 1) {
	// 						var fldvalue = result['data'][l][result['gridData'][t]['DbFieldName']];
	// 						if (fldvalue == "" || fldvalue == 0 || fldvalue == null) {
	// 							flag = 1;
	// 							alert(result['gridData'][t]['DisplayName'] + " is required");
	// 							if (DBFieldType == 17) {
	// 								$('#' + DBFieldName +'-text').val("");
	// 								$('#' + DBFieldName).val("");
	// 							} else if (DBFieldType == 16) {
	// 								$('#' + DBFieldName+'-text').val("");
	// 								$('#' + DBFieldName).val("");
	// 							}
	// 							$('#DropDownModal').modal('hide');
	// 							return;
	// 						}
	// 					}
	// 				}

	// 				if (flag == 0) {
	// 					for (var k = 0; k < gridResponseParams.length; k++) {
	// 						gridRes = gridResponseParams[k].split(":");
	// 						if (gridRes[2].trim() == '1' || gridRes[2].trim() == '3' || gridRes[2].trim() == '7' || gridRes[2].trim() == '8') {
	// 							$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).val(result['data'][l][gridRes[1].trim()]);
	// 						} else if (gridRes[2].trim() == '6') {
	// 							var dateObj = result['data'][l][gridRes[1].trim()];
	// 							const date = new Date(dateObj.date);
	// 							const options = {
	// 								timeZone: 'Asia/Calcutta',
	// 								year: 'numeric',
	// 								month: '2-digit',
	// 								day: '2-digit'
	// 							};
	// 							const formattedDate = new Intl.DateTimeFormat('en-GB', options).format(date);
	// 							const [day, month, year] = formattedDate.split('/');
	// 							const inputDateValue = `${year}-${month}-${day}`;
	// 							$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).val(inputDateValue);
	// 						} else if (gridRes[2].trim() == '5' || gridRes[2].trim() == '19') {
	// 							if (result['data'][l][gridRes[1]] != undefined) {
	// 								var newOption = $("<option selected=\"selected\"></option>").val(result['data'][l][gridRes[3]]).text(result['data'][l][gridRes[1]]);
	// 								if ($('#kreon-grid-' + gridSerialNo + '' + gridRes[0]).data('select2')) {
	// 									$('#kreon-grid-' + gridSerialNo + '' + gridRes[0]).select2('open');
	// 									$('#kreon-grid-' + gridSerialNo + '' + gridRes[0]).append(newOption).trigger("change");
	// 									$('#kreon-grid-' + gridSerialNo + '' + gridRes[0]).select2('close');
	// 								} else {
	// 									$('#kreon-grid-' + gridSerialNo + '' + gridRes[0]).append(newOption).trigger("change");
	// 								}
	// 							}
	// 						} else if (gridRes[2].trim() == '17') {
	// 							$('#kreon-grid-' + gridSerialNo + gridRes[0].trim() + '-text').val(result['data'][l][gridRes[1]]);
	// 							$('#kreon-grid-' + gridSerialNo + gridRes[0].trim()).val(result['data'][l][gridRes[3]]);
	// 						}
	// 					}

	// 					if (result['formResponseArray'] != null && result['formResponseArray'].length > 0) {
	// 						for (var n = 0; n < result['formResponseArray'].length; n++) {
	// 							formRes = result['formResponseArray'][n].split(":");
	// 							if (formRes[2].trim() == '1' || formRes[2].trim() == '3' || formRes[2].trim() == '6' || formRes[2].trim() == '7' || formRes[2].trim() == '8') {
	// 								$('#kreon-grid-' + gridSerialNo + '' + formRes[0].trim()).val(formRes[1].trim());
	// 							} else if (formRes[2].trim() == '5' || formRes[2].trim() == '19') {
	// 								var $newOption = $("<option selected=\"selected\"></option>").val(formRes[1]).text(formRes[3]);
	// 								$('#kreon-grid-' + gridSerialNo + '' + formRes[0]).select2('open');
	// 								$('#kreon-grid-' + gridSerialNo + '' + formRes[0]).append($newOption).trigger("change");
	// 								$('#kreon-grid-' + gridSerialNo + '' + formRes[0]).select2('close');
	// 							} else if (formRes[2].trim() == '17') {
	// 								$('#kreon-grid-' + gridSerialNo + formRes[0].trim() + '-text').val(formRes[3].trim());
	// 								$('#kreon-grid-' + gridSerialNo + formRes[0].trim()).val(formRes[1]);
	// 							}
	// 						}
	// 					}
	// 					DynamicGridInsertFirstRowEdit = ddResponseData['DynamicGridInsertFirstRowEdit'];
	// 					// Add row for each data item
	// 					promises.push(AddNewRow($(this), result['gridData'], gridSerialNo, selectedItemId, DynamicGridInsertFirstRowEdit));

	// 				}
	// 			}
	// 			// Wait for all promises to finish before moving forward
	// 			await Promise.all(promises);
				
	// 			// Wait for calculations after rows are added
	// 			await CreateOnChangeScriptGridCalculation(gridCalculationFieldName, gridCalculationType, gridSerialNo);

	// 			$('#DropDownModal').modal('hide');
	// 		} else {
	// 			console.log('result["data"] is not an array', result['data']);
	// 		}

			

	// 	} catch (error) {
	// 		console.error('Error:', error);
	// 	}
	// }

	async function AddResultInGrid(ddRowSelectType, selectedItemId, DBFieldType, DBFieldName, isGrid, requestParams, formReeponseString) {
		
		var gridid = ddResponseData['PopulateGrid'].split(":");
		var flag = 0;
		console.log("formReeponseString "+formReeponseString);
		try {
			// Making AJAX call with async/await
			const result = await $.ajax({
				type: 'POST',
				dataType: 'json',
				data: {
					selectedRowId: selectedItemId,
					GridId: gridid[1],
					FormId: <?php echo $FormID; ?>,
					FieldName: DBFieldName,
					DBFieldType: DBFieldType,
					isGrid: isGrid,
					requestParams: requestParams,
					formResponse: formReeponseString
				},
				url: 'getGridData.php'
			});

			
			var gridIdentifier = $('#grid-id-' + gridid[1].trim()).attr('class');
			var gridSerialNo = gridIdentifier.slice(4);

			var gridRes = [];
			var gridCalculationFieldName = [];
			var gridCalculationType = [];
			var gridResponseParams = ddResponseData['gridResponseParams'].split("|");
			var OnkeyupParameterCalculation = [];

			// Process grid data for calculation and input field population
			for (var t = 0; t < result['gridData'].length; t++) {
				if (result['gridData'][t]['gridcalculation'] > 0) {
					gridCalculationFieldName.push(result['gridData'][t]['dbfieldname']);
					gridCalculationType.push(result['gridData'][t]['gridcalculation']);
				}
				CreateOnkeyupScriptGrid
				if (result['gridData'][t]['onkeyupparameters'].length > 2) {
					if (result['gridData'][t]['fieldtype'] != 5) {
						OnkeyupParameterCalculation.push([result['gridData'][t]['dbfieldname'], result['gridData'][t]['fieldtype'], result['gridData'][t]['onkeyupparameters']]);
					}
				}
			}

			if (Array.isArray(result['data'])) {

				var promises = [];
				// Loop through the data and process each row
				for (var l = 0; l < result['data'].length; l++) {
					for (var t = 0; t < result['gridData'].length; t++) {
						if (result['gridData'][t]['required'] == 1) {
							var fldvalue = result['data'][l][result['gridData'][t]['dbfieldname']];
							if (fldvalue == "" || fldvalue == 0 || fldvalue == null) {
								flag = 1;
								alert(result['gridData'][t]['displayname'] + " is required");
								if (DBFieldType == 17) {
									$('#' + DBFieldName +'-text').val("");
									$('#' + DBFieldName).val("");
								} else if (DBFieldType == 16) {
									$('#' + DBFieldName+'-text').val("");
									$('#' + DBFieldName).val("");
								}
								$('#DropDownModal').modal('hide');
								return;
							}
						}
					}

					if (flag == 0) {
						for (var k = 0; k < gridResponseParams.length; k++) {
							gridRes = gridResponseParams[k].split(":");
							if (gridRes[2].trim() == '1' || gridRes[2].trim() == '3' || gridRes[2].trim() == '7' || gridRes[2].trim() == '8') {
								$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).val(result['data'][l][gridRes[1].trim()]);
								// Call function for grid calculation
								for (var m = 0; m < OnkeyupParameterCalculation.length; m++) {
									if (OnkeyupParameterCalculation[m] != undefined) {
										if (OnkeyupParameterCalculation[m].includes(gridRes[0].trim()) == true) {
											promises.push(CreateOnkeyupScriptGrid("kreon-grid-" + gridSerialNo + "" + gridRes[0].trim(), 'grid', 'dynamic'));
										}
									}
								}
							} else if (gridRes[2].trim() == '6') {
								console.log("hi");
								var dateObj = result['data'][l][gridRes[1].trim()];
								console.log("dateObj "+dateObj);
								const date = new Date(dateObj.date);
								const options = {
									timeZone: 'Asia/Calcutta',
									year: 'numeric',
									month: '2-digit',
									day: '2-digit'
								};
								const formattedDate = new Intl.DateTimeFormat('en-GB', options).format(date);
								const [day, month, year] = formattedDate.split('/');
								const inputDateValue = `${year}-${month}-${day}`;
								$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).val(inputDateValue);
							} else if (gridRes[2].trim() == '5' || gridRes[2].trim() == '19') {
								if (result['data'][l][gridRes[1]] != undefined) {
									var newOption = $("<option selected=\"selected\"></option>").val(result['data'][l][gridRes[3]]).text(result['data'][l][gridRes[1]]);
									if ($('#kreon-grid-' + gridSerialNo + '' + gridRes[0]).data('select2')) {
										$('#kreon-grid-' + gridSerialNo + '' + gridRes[0]).select2('open');
										$('#kreon-grid-' + gridSerialNo + '' + gridRes[0]).append(newOption).trigger("change");
										$('#kreon-grid-' + gridSerialNo + '' + gridRes[0]).select2('close');
									} else {
										$('#kreon-grid-' + gridSerialNo + '' + gridRes[0]).append(newOption).trigger("change");
									}
								}
							} else if (gridRes[2].trim() == '17' || gridRes[2].trim() == '16') {
								$('#kreon-grid-' + gridSerialNo + gridRes[0].trim() + '-text').val(result['data'][l][gridRes[1]]);
								$('#kreon-grid-' + gridSerialNo + gridRes[0].trim()).val(result['data'][l][gridRes[3]]);
							}
						}

						if (result['formResponseArray'] != null && result['formResponseArray'].length > 0) {
							for (var n = 0; n < result['formResponseArray'].length; n++) {
								formRes = result['formResponseArray'][n].split(":");
								if (formRes[2].trim() == '1' || formRes[2].trim() == '3' || formRes[2].trim() == '6' || formRes[2].trim() == '7' || formRes[2].trim() == '8') {
									$('#kreon-grid-' + gridSerialNo + '' + formRes[0].trim()).val(formRes[1].trim());
								} else if (formRes[2].trim() == '5' || formRes[2].trim() == '19') {
									// var value1 = formRes[1] ? formRes[1].trim() : 0;
									// var value3 = formRes[3] ? formRes[3].trim() : '';
									var $newOption = $("<option selected=\"selected\"></option>").val(formRes[1].trim()).text(formRes[3].trim());
									$('#kreon-grid-' + gridSerialNo + '' + formRes[0].trim()).select2('open');
									$('#kreon-grid-' + gridSerialNo + '' + formRes[0].trim()).append($newOption).trigger("change");
									$('#kreon-grid-' + gridSerialNo + '' + formRes[0].trim()).select2('close');
								} else if (formRes[2].trim() == '17' || formRes[2].trim() == '17') {
									$('#kreon-grid-' + gridSerialNo + formRes[0].trim() + '-text').val(formRes[3].trim());
									$('#kreon-grid-' + gridSerialNo + formRes[0].trim()).val(formRes[1]);
								}
							}
						}
						DynamicGridInsertFirstRowEdit = ddResponseData['DynamicGridInsertFirstRowEdit'];
						// Add row for each data item
						promises.push(AddNewRow($(this), result['gridData'], gridSerialNo, selectedItemId, DynamicGridInsertFirstRowEdit));

					}
				}
				// Wait for all promises to finish before moving forward
				await Promise.all(promises);
				
				// Wait for calculations after rows are added
				await CreateOnChangeScriptGridCalculation(gridCalculationFieldName, gridCalculationType, gridSerialNo);

				$('#DropDownModal').modal('hide');
			} else {
				console.log('result["data"] is not an array', result['data']);
			}

			

		} catch (error) {
			console.error('Error:', error);
		}
	}

	// async function AddNewRow(onCLickVariable, mArray, gridSerialNo, selectedRowId, DynamicGridInsertFirstRowEdit) {
	// 	var fieldArray = [];
	// 	var mfld = "";
	// 	var headerGridStart = '<tr class="grp' + gridSerialNo + '" data-rowid="' + selectedRowId + '" id="' + gridSerialNo;
	// 	var headerGridEnd = '">';
	// 	var cuurrentRowData = "";
	// 	var cuurrentRowDataWithComma = "";
	// 	var thClass = "";

	// 	for (var t = 0; t < mArray.length; t++) {
	// 		var fldvalue = "";
	// 		var fldvalue1 = "";

	// 		if (mArray[t]['FieldType'] == 17) {
	// 			fldvalue = $('#kreon-grid-' + gridSerialNo + mArray[t]['DbFieldName']+'-text').val();
	// 			fldvalue1 = $('#kreon-grid-' + gridSerialNo + '' + mArray[t]['DbFieldName']).val();
	// 		} else {
	// 			fldvalue = $('#kreon-grid-' + gridSerialNo + '' + mArray[t]['DbFieldName']).val();
	// 		}

	// 		fieldArray.push([mArray[t]['FieldType'], 'kreon-grid-' + gridSerialNo + '' + mArray[t]['DbFieldName'], mArray[t]['CarryOn'], mArray[t]['DefaultValue'], mArray[t]['GridCalculation']]);

	// 		if (mArray[t][26] == 0) {
	// 			thClass = " thHidden ";
	// 		}

	// 		// Building the field HTML (input types)
	// 		if (mArray[t]['FieldType'] == 1) {
	// 			mfld += "<td class='" + thClass + "'><input readonly type='text' value='" + fldvalue + "' class='form-control' name='" + gridSerialNo + "" + mArray[t]['DbFieldName'] + "[]' id='" + gridSerialNo + "" + mArray[t]['DbFieldName'] + "' ></td>";
	// 		}
	// 		if (mArray[t]['FieldType'] == 3) {
	// 			mfld += "<td class='" + thClass + "'><textarea readonly class='form-control' name='" + gridSerialNo + "" + mArray[t]['DbFieldName'] + "[]' id='" + gridSerialNo + "" + mArray[t]['DbFieldName'] + "'>" + fldvalue + "</textarea></td>";
	// 		}
	// 		if (mArray[t]['FieldType'] == 7) {
	// 			mfld += "<td class='" + thClass + "'><input readonly type='number' value='" + fldvalue + "' class='form-control' name='" + gridSerialNo + "" + mArray[t]['DbFieldName'] + "[]' id='" + gridSerialNo + "" + mArray[t]['DbFieldName'] + "'></td>";
	// 		}
	// 		if (mArray[t]['FieldType'] == 6) {
	// 			mfld += "<td class='" + thClass + "'><input readonly type='date' value='" + fldvalue + "' class='form-control' name='" + gridSerialNo + "" + mArray[t]['DbFieldName'] + "[]' id='" + gridSerialNo + "" + mArray[t]['DbFieldName'] + "'></td>";
	// 		}
	// 		if (mArray[t]['FieldType'] == 5 || mArray[t]['FieldType'] == 19) {
	// 			var selectedOption = $("#kreon-grid-" + gridSerialNo + "" + mArray[t]['DbFieldName'] + " :selected").text();
	// 			mfld += "<td class='" + thClass + "'><select readonly style='pointer-events: none;' name='" + gridSerialNo + "" + mArray[t]['DbFieldName'] + "[]' id='" + gridSerialNo + "" + mArray[t]['DbFieldName'] + "' class='form-control populate'>";
	// 			mfld += "<option value='" + fldvalue + "'>" + selectedOption + "</option>";
	// 			mfld += "</select></td>";
	// 		}
	// 		if (mArray[t]['FieldType'] == 17) {
	// 			mfld += "<td class='" + thClass + "'><input disabled type='text' value='" + fldvalue + "' class='form-control' name='" + gridSerialNo + mArray[t]['DbFieldName'] + "-text[]' id='" + gridSerialNo + mArray[t]['DbFieldName'] + "-text'><input type='hidden' id='" + gridSerialNo + mArray[t]['DbFieldName'] + "' name='" + gridSerialNo + "" + mArray[t]['DbFieldName'] + "[]' value='" + fldvalue1 + "'></td>";
	// 		}

	// 		if(mArray[t]['FieldType'] == 18){
	// 			mfld += "<td class='"+thClass+"'><input readonly type='time' value='"+fldvalue+"' class='form-control'   name='"+gridSerialNo+""+mArray[t]['DbFieldName']+"[]\'   id='"+gridSerialNo+""+mArray[t]['DbFieldName']+"'></td>";
	// 		}	

	// 		var decimal=  /^-?\d*\.?\d*$/;

	// 		//pass value to sp at adding row
	// 		cuurrentRowData += mArray[t]['DbFieldName']+"=";
	// 		if(mArray[t]['FieldType'] == 1 || mArray[t]['FieldType'] == 3 || mArray[t]['FieldType'] == 6 || mArray[t]['FieldType'] == 8 || mArray[t]['FieldType'] == 12 || mArray[t]['FieldType'] == 13 || mArray[t]['FieldType'] == 18){
	// 			cuurrentRowData += fldvalue != "" ? fldvalue : "";
	// 		}else if(mArray[t]['FieldType'] == 2 || mArray[t]['FieldType'] == 5 || mArray[t]['FieldType'] == 7 || mArray[t]['FieldType'] == 9 || mArray[t]['FieldType'] == 10 || mArray[t]['FieldType'] == 11 || mArray[t]['FieldType'] == 15 || mArray[t]['FieldType'] == 19){
	// 			cuurrentRowData += fldvalue != "" ? fldvalue : 0;
	// 		}else if(mArray[t]['FieldType'] == 17){
	// 			cuurrentRowData += fldvalue1 != "" ? fldvalue1 : 0;
	// 		}
	// 		if(t != mArray.length-1)
	// 			cuurrentRowData += "|";

	// 		cuurrentRowDataWithComma += mArray[t]['DbFieldName']+"=";
	// 		if(mArray[t]['FieldType'] == 0 || mArray[t]['FieldType'] == 1 || mArray[t]['FieldType'] == 3 || mArray[t]['FieldType'] == 6 || mArray[t]['FieldType'] == 8 || mArray[t]['FieldType'] == 12 || mArray[t]['FieldType'] == 13 || mArray[t]['FieldType'] == 18){
	// 			if(fldvalue == undefined || fldvalue == "" || fldvalue == null){
	// 				cuurrentRowDataWithComma += fldvalue != "" ? "\'\'"+fldvalue+"\'\'" : "\'\'\'\'";
	// 			}else{
	// 				if(fldvalue.match(decimal)) {
	// 					cuurrentRowDataWithComma += (fldvalue != "" && fldvalue != null) ? fldvalue : 0;
	// 				}else{
	// 					cuurrentRowDataWithComma += fldvalue != "" ? "\'\'"+fldvalue+"\'\'" : "\'\'\'\'";
	// 				}
	// 			}
	// 		}else if(mArray[t]['FieldType'] == 2 || mArray[t]['FieldType'] == 5 || mArray[t]['FieldType'] == 7 || mArray[t]['FieldType'] == 9 || mArray[t]['FieldType'] == 10 || mArray[t]['FieldType'] == 11 || mArray[t]['FieldType'] == 15 || mArray[t]['FieldType'] == 19){
	// 			cuurrentRowDataWithComma += fldvalue != "" ? fldvalue : 0;
	// 		}else if(mArray[t]['FieldType'] == 17){
	// 			cuurrentRowDataWithComma += fldvalue1 != "" ? fldvalue1 : 0;
	// 		}
	// 		if(t != mArray.length-1)
	// 			cuurrentRowDataWithComma += ",";

	// 	}

	// 	var cnt = $('#cnt' + gridSerialNo).val();
	// 	cnt = +cnt + 1;
	// 	mfld += "<td><a style='padding:3px;margin:2px;' href='javascript:void(0)' class='btn btn-info edits" + gridSerialNo + "'><span class='glyphicon glyphicon-pencil' aria-hidden='true'></span></a><a style='padding:2px 3px;' href='javascript:void(0)' class='btn btn-danger removes" + gridSerialNo + "'><span class='glyphicon glyphicon-remove' aria-hidden='true'></span></a><input type='hidden' name='" + gridSerialNo + "GridEditID[]' id='" + gridSerialNo + "GridEditID' value='0'><input type='hidden' name='" + gridSerialNo + "GridTempID[]' id='" + gridSerialNo + "GridTempID' value='" + cnt + "'></td>";
	// 	var headofGrid = headerGridStart + '' + cnt + '' + headerGridEnd;

	// 	document.getElementById('cnt' + gridSerialNo).value = cnt;

	// 	// Awaiting the validation AJAX call before proceeding
	// 	try {
	// 		const response = await $.ajax({
	// 			type: "POST",
	// 			dataType: "json",
	// 			data: {
	// 				ValidationOn: "gridsave",
	// 				formData: cuurrentRowData,
	// 				formDataWithComma: cuurrentRowDataWithComma,
	// 				mainFormData: $("form").serialize(),
	// 				FormID: <?php echo $FormID?>,
	// 				GridID: $('#GridId' + gridSerialNo).val(),
	// 				GridRowTempId: cnt
	// 			},
	// 			url: "validateForm.php"
	// 		});

	// 		if (response["status"] == true) {
	// 			if (response["data"]["Success"] == 1) {
	// 				$('#panel' + gridSerialNo + ' table tbody').append(headofGrid + mfld);
	// 				$(".add" + gridSerialNo).attr("disabled", false);
	// 			} else if (response["data"]["Success"] == 2) {
	// 				if (confirm(response["data"]["Msg"] + ". Are you sure you want to submit?")) {
	// 					$('#panel' + gridSerialNo + ' table tbody').append(headofGrid + mfld);
	// 					$(".add" + gridSerialNo).attr("disabled", false);
	// 				} else {
	// 					$(".add" + gridSerialNo).attr("disabled", false);
	// 				}
	// 			} else {
	// 				$(".add" + gridSerialNo).attr("disabled", false);
	// 				alert(response["data"]["Msg"]);
	// 			}

	// 			// Clear input values and focus on grid input
	// 			for (var s = 0; s < fieldArray.length; s++) {
	// 				if (fieldArray[s][0] == 5 || fieldArray[s][0] == 19) {
	// 					$("#" + fieldArray[s][1]).select2('open').val(null).select2('close');
	// 				} else {
	// 					document.getElementById(fieldArray[s][1]).value = "";
	// 				}
	// 			}

	// 			// Focus the cursor on the first field
	// 			if (mArray[0]['FieldType'] == 17) {
	// 				$("#" + gridSerialNo + mArray[0][1] + "-text").focus();
	// 			} else {
	// 				$("#" + gridSerialNo + "" + mArray[0][1]).focus();
	// 			}
				
	// 			// if(ddResponseData['DynamicGridInsertFirstRowEdit'] == 'yes'){
	// 			// 	//if(<?php echo $editID; ?> == 0){
	// 			// 		if(cnt == 1){
	// 			// 			setTimeout(
	// 			// 			function() {
	// 			// 				EditFirstRow(gridSerialNo,mArray);
	// 			// 			},
	// 			// 			300);
	// 			// 		}
	// 			// 	//}
	// 			// }

	// 		}else{
	// 			$('#panel'+gridSerialNo+' table tbody').append(headofGrid + mfld);
	// 		}

	// 		if (DynamicGridInsertFirstRowEdit.includes('yes')) {
	// 			var crsr = DynamicGridInsertFirstRowEdit.split('|')[1]; //cursor position
	// 			if(cnt == 1){
	// 				setTimeout(
	// 				function() {
	// 					EditFirstRow(gridSerialNo,mArray, crsr);
	// 				},
	// 				300);
	// 			}
	// 		} 
	// 	} catch (error) {
	// 		console.error('Error:', error);
	// 		$(".add" + gridSerialNo).attr("disabled", false);
	// 	}
	// }

	async function AddNewRow(onCLickVariable, mArray, gridSerialNo, selectedRowId, DynamicGridInsertFirstRowEdit) {
		var fieldArray = [];
		var mfld = "";
		var headerGridStart = '<tr class="grp' + gridSerialNo + '" data-rowid="' + selectedRowId + '" id="' + gridSerialNo;
		var headerGridEnd = '">';
		var cuurrentRowData = "";
		var cuurrentRowDataWithComma = "";
		var thClass = "";

		for (var t = 0; t < mArray.length; t++) {
			var fldvalue = "";
			var fldvalue1 = "";

			if (mArray[t]['fieldtype'] == 17 || mArray[t]['fieldtype'] == 16) {
				fldvalue = $('#kreon-grid-' + gridSerialNo + mArray[t]['dbfieldname']+'-text').val();
				fldvalue1 = $('#kreon-grid-' + gridSerialNo + '' + mArray[t]['dbfieldname']).val();
			} else {
				fldvalue = $('#kreon-grid-' + gridSerialNo + '' + mArray[t]['dbfieldname']).val();
			}

			fieldArray.push([mArray[t]['fieldtype'], 'kreon-grid-' + gridSerialNo + '' + mArray[t]['dbfieldname'], mArray[t]['carryon'], mArray[t]['defaultvalue'], mArray[t]['gridcalculation'], mArray[t]['readonly']]);

			if (mArray[t][26] == 0) {
				thClass = " thHidden ";
			}

			// Building the field HTML (input types)
			if (mArray[t]['fieldtype'] == 1) {
				mfld += "<td class='" + thClass + "'><input readonly type='text' value='" + fldvalue + "' class='form-control' name='" + gridSerialNo + "" + mArray[t]['dbfieldname'] + "[]' id='" + gridSerialNo + "" + mArray[t]['dbfieldname'] + "' ></td>";
			}
			if (mArray[t]['fieldtype'] == 3) {
				mfld += "<td class='" + thClass + "'><textarea readonly class='form-control' name='" + gridSerialNo + "" + mArray[t]['dbfieldname'] + "[]' id='" + gridSerialNo + "" + mArray[t]['dbfieldname'] + "'>" + fldvalue + "</textarea></td>";
			}
			if (mArray[t]['fieldtype'] == 7) {
				mfld += "<td class='" + thClass + "'><input readonly type='number' value='" + fldvalue + "' class='form-control' name='" + gridSerialNo + "" + mArray[t]['dbfieldname'] + "[]' id='" + gridSerialNo + "" + mArray[t]['dbfieldname'] + "'></td>";
			}
			if (mArray[t]['fieldtype'] == 6) {
				mfld += "<td class='" + thClass + "'><input readonly type='date' value='" + fldvalue + "' class='form-control' name='" + gridSerialNo + "" + mArray[t]['dbfieldname'] + "[]' id='" + gridSerialNo + "" + mArray[t]['dbfieldname'] + "'></td>";
			}
			if (mArray[t]['fieldtype'] == 5 || mArray[t]['fieldtype'] == 19) {
				var selectedOption = $("#kreon-grid-" + gridSerialNo + "" + mArray[t]['dbfieldname'] + " :selected").text();
				mfld += "<td class='" + thClass + "'><select readonly style='pointer-events: none;' name='" + gridSerialNo + "" + mArray[t]['dbfieldname'] + "[]' id='" + gridSerialNo + "" + mArray[t]['dbfieldname'] + "' class='form-control populate'>";
				fldvalue = fldvalue === null ? 0 : fldvalue;
				mfld += "<option value='" + fldvalue + "'>" + selectedOption + "</option>";
				mfld += "</select></td>";
			}

			if (mArray[t]['fieldtype'] == 16) {
				mfld += "<td class='" + thClass + "'><input disabled type='text' value='" + fldvalue + "' class='form-control' name='" + gridSerialNo + mArray[t]['dbfieldname'] + "-text[]' id='" + gridSerialNo + mArray[t]['dbfieldname'] + "-text'><input type='hidden' id='" + gridSerialNo + mArray[t]['dbfieldname'] + "' name='" + gridSerialNo + "" + mArray[t]['dbfieldname'] + "[]' value='" + fldvalue1 + "'></td>";
			}

			if (mArray[t]['fieldtype'] == 17) {
				mfld += "<td class='" + thClass + "'><input disabled type='text' value='" + fldvalue + "' class='form-control' name='" + gridSerialNo + mArray[t]['dbfieldname'] + "-text[]' id='" + gridSerialNo + mArray[t]['dbfieldname'] + "-text'><input type='hidden' id='" + gridSerialNo + mArray[t]['dbfieldname'] + "' name='" + gridSerialNo + "" + mArray[t]['dbfieldname'] + "[]' value='" + fldvalue1 + "'></td>";
			}

			if(mArray[t]['fieldtype'] == 18){
				mfld += "<td class='"+thClass+"'><input readonly type='time' value='"+fldvalue+"' class='form-control'   name='"+gridSerialNo+""+mArray[t]['dbfieldname']+"[]\'   id='"+gridSerialNo+""+mArray[t]['dbfieldname']+"'></td>";
			}	

			var decimal=  /^-?\d*\.?\d*$/;

			//pass value to sp at adding row
			cuurrentRowData += mArray[t]['dbfieldname']+"=";
			if(mArray[t]['fieldtype'] == 1 || mArray[t]['fieldtype'] == 3 || mArray[t]['fieldtype'] == 6 || mArray[t]['fieldtype'] == 8 || mArray[t]['fieldtype'] == 12 || mArray[t]['fieldtype'] == 13 || mArray[t]['fieldtype'] == 18){
				cuurrentRowData += fldvalue != "" ? fldvalue : "";
			}else if(mArray[t]['fieldtype'] == 2 || mArray[t]['fieldtype'] == 5 || mArray[t]['fieldtype'] == 7 || mArray[t]['fieldtype'] == 9 || mArray[t]['fieldtype'] == 10 || mArray[t]['fieldtype'] == 11 || mArray[t]['fieldtype'] == 15 || mArray[t]['fieldtype'] == 19){
				cuurrentRowData += fldvalue != "" ? fldvalue : 0;
			}else if(mArray[t]['fieldtype'] == 17 || mArray[t]['fieldtype'] == 16){
				cuurrentRowData += fldvalue1 != "" ? fldvalue1 : 0;
			}
			if(t != mArray.length-1)
				cuurrentRowData += "|";

			cuurrentRowDataWithComma += mArray[t]['dbfieldname']+"=";
			if(mArray[t]['fieldtype'] == 0 || mArray[t]['fieldtype'] == 1 || mArray[t]['fieldtype'] == 3 || mArray[t]['fieldtype'] == 6 || mArray[t]['fieldtype'] == 8 || mArray[t]['fieldtype'] == 12 || mArray[t]['fieldtype'] == 13 || mArray[t]['fieldtype'] == 18){
				console.log("pornima",mArray[t]['fieldtype'],fldvalue);
				if(fldvalue == undefined || fldvalue == "" || fldvalue == null){
					cuurrentRowDataWithComma += fldvalue != "" ? "\'\'"+fldvalue+"\'\'" : "\'\'\'\'";
				}else{
					if(fldvalue.match(decimal)) {
						cuurrentRowDataWithComma += (fldvalue != "" && fldvalue != null) ? fldvalue : 0;
					}else{
						cuurrentRowDataWithComma += fldvalue != "" ? "\'\'"+fldvalue+"\'\'" : "\'\'\'\'";
					}
				}
			}else if(mArray[t]['fieldtype'] == 2 || mArray[t]['fieldtype'] == 5 || mArray[t]['fieldtype'] == 7 || mArray[t]['fieldtype'] == 9 || mArray[t]['fieldtype'] == 10 || mArray[t]['fieldtype'] == 11 || mArray[t]['fieldtype'] == 15 || mArray[t]['fieldtype'] == 19){
				cuurrentRowDataWithComma += fldvalue != "" ? fldvalue : 0;
			}else if(mArray[t]['fieldtype'] == 17 || mArray[t]['fieldtype'] == 16){
				cuurrentRowDataWithComma += fldvalue1 != "" ? fldvalue1 : 0;
			}
			if(t != mArray.length-1)
				cuurrentRowDataWithComma += ",";

		}

		var cnt = $('#cnt' + gridSerialNo).val();
		cnt = +cnt + 1;
		mfld += "<td><a style='padding:3px;margin:2px;' href='javascript:void(0)' class='btn btn-info edits" + gridSerialNo + "'><span class='glyphicon glyphicon-pencil' aria-hidden='true'></span></a><a style='padding:2px 3px;' href='javascript:void(0)' class='btn btn-danger removes" + gridSerialNo + "'><span class='glyphicon glyphicon-remove' aria-hidden='true'></span></a><input type='hidden' name='" + gridSerialNo + "GridEditID[]' id='" + gridSerialNo + "GridEditID' value='0'><input type='hidden' name='" + gridSerialNo + "GridTempID[]' id='" + gridSerialNo + "GridTempID' value='" + cnt + "'></td>";
		var headofGrid = headerGridStart + '' + cnt + '' + headerGridEnd;

		document.getElementById('cnt' + gridSerialNo).value = cnt;

		// Awaiting the validation AJAX call before proceeding
		try {
			const response = await $.ajax({
				type: "POST",
				dataType: "json",
				data: {
					ValidationOn: "gridsave",
					formData: cuurrentRowData,
					formDataWithComma: cuurrentRowDataWithComma,
					mainFormData: $("form").serialize(),
					FormID: <?php echo $FormID?>,
					GridID: $('#GridId' + gridSerialNo).val(),
					GridRowTempId: cnt
				},
				url: "validateForm.php"
			});

			if (response["status"] == true) {
				if (response["data"]["Success"] == 1) {
					// $('#panel' + gridSerialNo + ' table tbody').append(headofGrid + mfld);
					$('#panel'+gridSerialNo+' table tbody tr:first').after(headofGrid + mfld);
					$(".add" + gridSerialNo).attr("disabled", false);
				} else if (response["data"]["Success"] == 2) {
					if (confirm(response["data"]["Msg"] + ". Are you sure you want to submit?")) {
						// $('#panel' + gridSerialNo + ' table tbody').append(headofGrid + mfld);
						$('#panel'+gridSerialNo+' table tbody tr:first').after(headofGrid + mfld);
						$(".add" + gridSerialNo).attr("disabled", false);
					} else {
						$(".add" + gridSerialNo).attr("disabled", false);
					}
				} else {
					$(".add" + gridSerialNo).attr("disabled", false);
					alert(response["data"]["Msg"]);
				}

				
				
				// if(ddResponseData['DynamicGridInsertFirstRowEdit'] == 'yes'){
				// 	//if(<?php echo $editID; ?> == 0){
				// 		if(cnt == 1){
				// 			setTimeout(
				// 			function() {
				// 				EditFirstRow(gridSerialNo,mArray);
				// 			},
				// 			300);
				// 		}
				// 	//}
				// }

			}else{
				// $('#panel'+gridSerialNo+' table tbody').append(headofGrid + mfld);
				$('#panel'+gridSerialNo+' table tbody tr:first').after(headofGrid + mfld);
			}


			// Clear input values and focus on grid input
			// for (var s = 0; s < fieldArray.length; s++) {
			// 	console.log("in field clear loop");
			// 	if (fieldArray[s][0] == 5 || fieldArray[s][0] == 19) {
			// 		$("#" + fieldArray[s][1]).select2('open').val(null).select2('close');
			// 	} else {
			// 		document.getElementById(fieldArray[s][1]).value = "";
			// 	}
			// }

			
			for(var s=0; s < fieldArray.length; s++){
				if(fieldArray[s][2] != 1 && fieldArray[s][2] != 2){
					
					if(fieldArray[s][0] == 5 || fieldArray[s][0] == 19){
						
						if(fieldArray[s][3] == null || fieldArray[s][3].length == 0){

							// Step 1: Get the current onchange function as a string. onchange function remove & then adding because after adding row onchange function triggering so
							let onchangeFunction = $("#" + fieldArray[s][1]).attr("onchange");

							// Step 2: If onchange function exists, store it, remove it temporarily
							if (onchangeFunction) {
								// Step 2: Remove the onchange attribute temporarily
								$("#" + fieldArray[s][1]).removeAttr("onchange");

								$("#"+fieldArray[s][1]).select2("open");
								$("#"+fieldArray[s][1]).val(0).trigger("change");
								$("#"+fieldArray[s][1]).select2("close");

								// Step 4: Re-add the original onchange function with the stored value
								$("#" + fieldArray[s][1]).attr("onchange", onchangeFunction);
							}else{
								$("#"+fieldArray[s][1]).select2("open");
								$("#"+fieldArray[s][1]).val(0).trigger("change");
								$("#"+fieldArray[s][1]).select2("close");
							}
							
						} 
						//after add if not readonly then remove readonly
						if(fieldArray[s][5] != '1'){
							$("#"+fieldArray[s][1]).next("span").find(".select2-selection--single").prop("readonly", false).css("pointer-events","").css("background-color","white");
						}
					}
					if(fieldArray[s][0] == 17){
						if(fieldArray[s][3] == null || fieldArray[s][3].length == 0){
							fieldName = fieldArray[s][1].split("-");
							console.log("fieldName "+fieldName);
							$("#"+fieldName[0]+"-"+fieldName[1]+"-"+gridSerialNo+fieldName[2].slice(1)+"-text").val("");
							$("#"+fieldName[0]+"-"+fieldName[1]+"-"+gridSerialNo+fieldName[2].slice(1)).val("");
						}
						
						//after add if not readonly then remove readonly
						if(fieldArray[s][5] != '1'){
							$("#"+fieldName[0]+"-"+fieldName[1]+"-"+gridSerialNo+fieldName[2].slice(1)+"-text").prop("readonly", false).css("pointer-events","").css("background-color","white");
						}
					}else{
						if(fieldArray[s][3] == null){
							$("#"+fieldArray[s][1].trim()).val("");
							$("#"+gridSerialNo+cnt).val(0);
						}else{
							$("#"+fieldArray[s][1]).val(fieldArray[s][3]);	
						}

						//after add if not readonly then remove readonly
						if(fieldArray[s][5] != '1'){
							console.log('hi');
							$("#"+fieldArray[s][1]).prop("readonly", false).css("pointer-events","").css("background-color","white");
						}		
					}

					$("#"+fieldArray[s][1]).css("border","1px solid #aa8a");
				}

				// if(fieldArray[s][2] == 2){
				// 	var fieldValueStr = $("#"+fieldArray[s][1]).val();
				// 	var splitIndex = fieldValueStr.lastIndexOf("-");
				// 	var part1 = fieldValueStr.substring(0, splitIndex); // Before the last hyphen
				// 	var part2 = fieldValueStr.substring(splitIndex + 1); // After the last hyphen

				// 	var increementValue = parseInt(part2)+1;
				// 	if(part1.length > 0){
				// 		var changeValue = part1+"-"+increementValue;
				// 	}else{
				// 		var changeValue = increementValue;
				// 	}
				// 	$("#"+fieldArray[s][1]).val(changeValue);
				// }

				//Cursor focus on grid perticular field that was mention in AfterGridAddCursorPosition field
				// if("'.$d[9].'".length > 1){
					
				// 	if(fieldArray[s][3] == 17){
				// 		if(fieldArray[s][1] == "kreon-grid-'.$d[3].''.$d[9].'-text"){
				// 			$("#"+fieldArray[s][1]).focus();
				// 		}
				// 	}else if(fieldArray[s][3] == 16){
				// 		if(fieldArray[s][1] == "kreon-grid-'.$d[3].''.$d[9].'-text"){
				// 			$("#"+fieldArray[s][1]).focus();
				// 		}
				// 	}else if(fieldArray[s][3] == 5){
				// 		console.log("in");
				// 		if(fieldArray[s][1] == "kreon-grid-'.$d[3].''.$d[9].'"){
				// 			$("#"+fieldArray[s][1]).select2("open");
				// 			$("#"+fieldArray[s][1]).val(0).trigger("change");
				// 			// $("#"+fieldArray[s][1]).select2("close");
				// 		}
				// 	}else{
				// 		if(fieldArray[s][1] == "kreon-grid-'.$d[3].''.$d[9].'"){
				// 			$("#"+fieldArray[s][1]).focus();
				// 		}
				// 	}
				// }
			}

			// Focus the cursor on the first field
			// if (mArray[0]['fieldtype'] == 17) {
			// 	$("#" + gridSerialNo + mArray[0][1] + "-text").focus();
			// } else {
			// 	$("#" + gridSerialNo + "" + mArray[0][1]).focus();
			// }

			if(DynamicGridInsertFirstRowEdit){
				if (DynamicGridInsertFirstRowEdit.includes('yes')) {
					var crsr = DynamicGridInsertFirstRowEdit.split('|')[1]; //cursor position
					if(cnt == 1){
						setTimeout(
						function() {
							EditFirstRow(gridSerialNo,mArray, crsr);
						},
						300);
					}
				} 
			}
		} catch (error) {
			console.error('Error:', error);
			$(".add" + gridSerialNo).attr("disabled", false);
		}
	}

	//First Row editable on grid insert dynamically
	function EditFirstRow(gridSerialNo,mArray, cursorPos = null){
		// alert("hi");
		var row = $("#a1");
		// console.log(row);
		$(row).addClass("being-edited");

		var GridEditId = $(row).find("#"+gridSerialNo+"GridEditID").val();
		var GridTempId = $(row).find("#"+gridSerialNo+"GridTempID").val();

	
		$(".AddEdit a:nth-child(2)").attr("id", 'a1');
		$(".AddEdit a:nth-child(3)").attr("id", 'a1');
		$(".AddEdit").find("#"+gridSerialNo+"GridEditID").val(GridEditId);
		$(".AddEdit").find("#"+gridSerialNo+"GridTempID").val(GridTempId);

		$(".AddEdit a:nth-child(1)").css("display","none");
		$(".AddEdit a:nth-child(2)").show();
		$(".AddEdit a:nth-child(3)").show();

		for(var t=0; t < mArray.length; t++){
			if(mArray[t]['fieldtype'] == 17 || mArray[t]['fieldtype'] == 17){
				var EditFldvalue = $(row).find("#"+gridSerialNo + mArray[t]['dbfieldname'] + "-text").val();
				var EditFldvalue1 = $(row).find("#"+gridSerialNo + mArray[t]['dbfieldname']).val();
			}else if(mArray[t]['fieldtype'] == 12){
				var EditFldvalue = $(row).find("a").attr("href");
				var EditFldvalue1 = $(row).find("#"+gridSerialNo+mArray[t]['dbfieldname']).val();
				
			}else{
				var EditFldvalue = $(row).find("#"+gridSerialNo+mArray[t]['dbfieldname']).val();
			}

			if(mArray[t]['fieldtype'] == 5 || mArray[t]['fieldtype'] == 19){
				var selectedOption = $(row).find("#"+gridSerialNo+mArray[t]['dbfieldname'] + " :selected").text();
				var $newOption = $("<option selected=\"selected\"></option>").val(EditFldvalue).text(selectedOption);

				$("#kreon-grid-"+gridSerialNo+mArray[t]['dbfieldname']).select2('open');
				$("#kreon-grid-"+gridSerialNo+mArray[t]['dbfieldname']).append($newOption);
				$("#kreon-grid-"+gridSerialNo+mArray[t]['dbfieldname']).select2('close');
			}else if(mArray[t]['fieldtype'] == 17 || mArray[t]['fieldtype'] == 17){
				$("#kreon-grid-"+gridSerialNo + mArray[t]['dbfieldname'] +"-text").val(EditFldvalue);
				$("#kreon-grid-"+gridSerialNo + mArray[t]['dbfieldname']).val(EditFldvalue1);
			}else if(mArray[t]['fieldtype'] == 12){
				if(EditFldvalue1 != 0){
					$("#kreon-grid-"+gridSerialNo+mArray[t]['dbfieldname']).parent("label").after("<input readonly type='hidden' value='"+EditFldvalue1+"' name='kreon-grid-aimage[]' id='kreon-grid-aimage'><a target=\"_blank\" href='"+EditFldvalue+"'><img src='"+EditFldvalue+"' width='60'></a>");
				}
			}else{
				$("#kreon-grid-"+gridSerialNo+mArray[t]['dbfieldname']).val(EditFldvalue);

				//Grid calculation on edit button
				// if(mArray[t]['GridCalculation'] > 0){
				// 	CreateOnChangeScriptGridCalculation("kreon-grid-"+gridSerialNo+mArray[t]['DbFieldName'], mArray[t]['GridCalculation'],"grid",0);
				// }
			}

			if(mArray[t]['isnoteditable'] == 1){
				$("#kreon-grid-"+gridSerialNo+mArray[t]['dbfieldname']).prop("readonly", true); 
			}	
			if(cursorPos && cursorPos.length > 0){
				$("#kreon-grid-"+gridSerialNo + cursorPos).focus();	
			}
		}
	}

	function RemoveGridRow(gridSerialNo,selectedRowIds,gridRemoveType){
		var cnt = document.getElementById("cnt"+gridSerialNo).value;

		if(gridRemoveType == 'single'){
			for(var i=0; i<=cnt; i++){
				if(i != 0){
					$("#"+gridSerialNo+""+i).remove();
					var rowcount = cnt-i;
					$("#cnt"+gridSerialNo).val(rowcount);
				}
			}
		}else{
			for(var j=0; j<selectedRowIds.length; j++){
				selectedRowIds[j].remove();
				var rowcount = cnt-selectedRowIds.length;
				$("#cnt"+gridSerialNo).val(rowcount);
			}
		}

	}

	function checkMDDField(DBFieldName,DBFieldType){
		var MDDModalValue = [];
		var MDDModalLabel = [];
		var itemvalue = [];
		var itemlabel = [];
		var MDDModalHiddenValue = '';
		var MDDModalHidden = '';
		if(DBFieldType == 16){
			var MDDModalHiddenValue = $('#MultyDDModal-hidden-'+DBFieldName).val();
			var MDDModalHidden = $('#MultyDDModal-'+DBFieldName).val();
			MDDModalLabel = MDDModalHidden.split(',');
		}else if(DBFieldType == 23 || DBFieldType == 14){
			var MDDModalHiddenValue = $('#GridMultyDDModal-hidden-'+DBFieldName).val();
		}
		
		if(MDDModalHiddenValue){
			MDDModalValue = MDDModalHiddenValue.split(',');
			console.log(MDDModalValue);
			if(MDDModalValue.length > 0){
				for(var s=0; s<MDDModalValue.length; s++){
					itemvalue.push(parseInt(MDDModalValue[s]));
				}
	
				if(DBFieldType == 16){
					for(var t=0; t<MDDModalLabel.length; t++){
						itemlabel.push(MDDModalLabel[t]);
					}
				}
				
				// alert(itemlabel);
				return [itemlabel, itemvalue];
			}
		}else{
			return [itemlabel, itemvalue];
		}
	}

	// function SearchFunction(event,DBFieldName,DBFieldType,FormID,GridID,isGrid,gridResponseParams) {
	// 	console.log("search function");
	// 	var x = event.code;
	// 	if(x == 'ArrowDown'){
	// 		$("#ddTable tbody tr:nth-child(1) .select-checkbox").addClass('focus');
	// 		// $(".dataTables_wrapper .dataTables_filter input").focus();
	// 		$("#ddTable tbody tr:nth-child(1) td.select-checkbox:before").focus();
	// 		// $("td.select-checkbox.checkbox_align.focus").click();
	// 	}
	// 	if(x == 'Enter' || x == 'NumpadEnter') {
	// 		//On enter redirect to first page so that search starts from offset 0
	// 		// setTimeout(
	// 			// function() {
	// 				table.page( 0 ).draw( false );
	// 			// }, 100);

	// 		var viewcodeArray = data1['columns'];
	// 		console.log(viewcodeArray);
	// 		var variable = "";
	// 		var value = "";
	// 		for(var m=0; m<viewcodeArray.length; m++){
	// 			if(viewcodeArray[m]['data'] != ""){
	// 				if($("#"+viewcodeArray[m]['data']).val() != undefined && $("#"+viewcodeArray[m]['data']).val() != ""){
	// 					value += $("#"+viewcodeArray[m]['data']).val();
	// 					value += ",";
	// 					variable += viewcodeArray[m]['data']+",";
	// 				}
	// 			}
	// 		}
			
	// 		if (value.charAt(value.length - 1) === ",") {
	// 			value = value.slice(0, -1);
	// 		}
	// 		if (variable.charAt(variable.length - 1) === ",") {
	// 			variable = variable.slice(0, -1);
	// 		}
			
	// 		$("#ddTable").DataTable().destroy();
	// 		if(value != undefined && value != ""){
	// 			console.log("calling load_dd from Search_if");
	// 			load_dd(DBFieldName,DBFieldType,FormID,GridID,isGrid,gridResponseParams,variable,value);
	// 		}else{
	// 			variable = "";
	// 			value = "";
	// 			console.log("calling load_dd from Search_else");
	// 			load_dd(DBFieldName,DBFieldType,FormID,GridID,isGrid,gridResponseParams,variable,value);
	// 		}

	// 		// var value = event.target.value;
	// 		// var variable = event.target.id;
	// 		// console.log(DBFieldName);
	// 		// load_dd(DBFieldName,variable,value);
	// 	}    
	// }

	function validateOnButton(thiss){
		var btnVal = $(thiss).val();
		var editID = $("input[name=editID]").val();
		event.preventDefault();
		validateFormOnSave(event, btnVal, editID);
	}

	//Check validation on submit
	function validateFormOnSave(event,btnVal,editID){
		/* TO CHECK THE ROWS REQUIRED FOR THE GRIDS */
		var gridSerials = ["a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "aa", "ab", "ac", "ad", "ae", "af", "ag", "ah", "aj", "ak", "al", "am", "an", "ap", "aq", "ar", "au", "av", "aw", "ax", "ay", "az"];
		var gridCount = $('#finalGridCount').val();
		console.log("VALIDATING GRID 1 : ", gridCount)
		for (var i = 0; i < +gridCount; i++){
			var tmpcnt = $("#cnt" + gridSerials[i]).val();
			var reqcnt = $("#cnt" + gridSerials[i]).data('minrowsreq');
			console.log("VALIDATING GRID 2: ", tmpcnt, reqcnt)
			if(+tmpcnt < +reqcnt){
				alert("Please add rows to the grid.");
				return false;
			}
		}

		$('input,textarea,select').css("border","1px solid #aa8a");
		var required = $('input,textarea,select').filter('[required]:visible');
        var allRequired = true;
        required.each(function(){
            // alert($(this).val());
            if($(this).val() == ''){    
                allRequired = false;
				var elem = $(this).attr('id');
				$("#"+ elem).css("border","1px solid red");
				$("#"+ elem).focus();
				console.log("validateFormOnSave Function - error in validating reqd field");
				var tmp = $("." + elem + " div label").text()
                alert("Missing data in the field - " + tmp.split("*")[0]);
                return false;
            }
        });

        if(!allRequired){
            return;
        }
		
		$('#kreonClickButton').val(btnVal);
		event.preventDefault();

		if(!validateSelect2(event)){
			return false;
		}else{
			$('#SaveUpdate').attr('disabled', true);
			$.ajax({
				type : 'POST',
				dataType: 'json',
				data : {ValidationOn:'save', FormID: <?php echo $FormID; ?>, formData: $('form').serialize(),editID:editID},
				url : 'validateForm.php',
				success : function(result){
					//Success = 0 - show message & stop submit
					//Success = 1 - submit form
					//Success = 2 - show message. If press ok then submit form or if press close then stop submit
					//Success = 3 - show message & stop submit(Error in exicution of query)

					if(result['status'] == true){
						if(result['data']['Success'] == 1){
							$('form').removeAttr("onSubmit");
							$("#callScriptBeforeUnload").val(0);
							$('form').unbind('submit').submit();
						}else{
							if(result['data']['Success'] == 2){
								if (confirm(result['data']['Msg']+'. Are you sure you want to submit?') == true) {
									$('form').removeAttr("onSubmit");
									$("#callScriptBeforeUnload").val(0);
									$('form').unbind('submit').submit();
								} else {
									$('#SaveUpdate').attr('disabled', false);
								}
							}else if(result['data']['Success'] == 0){
								alert(result['data']['Msg']);
								$('#SaveUpdate').attr('disabled', false);
							}else{
								alert("Internal Server Error");
								$('#SaveUpdate').attr('disabled', false);
							}
						}
					}else{
						$('form').removeAttr("onSubmit");
						$("#callScriptBeforeUnload").val(0);
						$('form').unbind('submit').submit();
					}
				}
			});
		}	
	}

	function validateSelect2(event){
		// var tmp = $('form select').each(function () { return $(this).attr('id')});	
		console.log("validateSelect2", event);
		var tmp = $('form select').each(function () { return $(this); });
		var returnFlag = true;
		for(var i =0; i < tmp.length; i++){
			var input = $(tmp[i]).attr('id');
			if($('#'+input).hasClass('select2-hidden-accessible')){		
					
				if ($('#'+input).val() == 0 && $('#'+input).prop('required')) {	
					$('#'+input).next('span').find('.select2-selection--single').css('border-color','red')
					$('#'+input).next('span').find('.select2-selection--multiple').css('border-color','red')
					alert('please select '+input);
					returnFlag = false;
					break;
				}
			}
		}
		return returnFlag;
	}

	//Check validation on edit
	function validateFormOnEdit(editID){
		console.log( "validateFormOnEdit", editID)
		$.ajax({
			type : 'POST',	
			dataType: 'json',
			data : {ValidationOn:'edit', FormID: <?php echo $FormID; ?>,EditID: editID, formData: $('form').serialize()},
			url : 'validateForm.php',
			success : function(result){
				// console.log(result);
				if(result['status'] == true){
					if(result['data']['NonEditable'] != undefined && result['data']['NonEditable'].length > 0){
						var NonEditableFields = result['data']['NonEditable'].split(",");
						for(var i =0; i < NonEditableFields.length; i++){
							var type = $('#'+NonEditableFields[i]).prop('type');
							// console.log(type +" "+ NonEditableFields[i]);
							if(type == 'text' || type == 'date' || type == 'number' || type == 'email' || type == 'textarea' || type == 'time'){
								// $('#'+NonEditableFields[i]).attr('readonly','readonly');
								$('#'+NonEditableFields[i]).css('pointer-events','none').css('background','#eee');
							}else if(type == 'select-one'){
								
								// console.log($('#'+NonEditableFields[i]).next('span > span').find('.select2-selection--single'));
								// $("#"+NonEditableFields[i]).select2({disabled:'readonly'});
								// $('#'+NonEditableFields[i]).select2("container").addClass("select2_dropdown_readonly");
								// $('#'+NonEditableFields[i]).next('span').find('.select2-selection--single').addClass("select2_dropdown_readonly");
								
								$('#'+NonEditableFields[i]).next('span').find('.select2-selection--single').css('pointer-events','none').css('background','#eee');
							}else if(type == 'select-multiple'){
								$('#'+NonEditableFields[i]).next('span').find('.select2-selection--multiple').css('pointer-events','none').css('background','#eee');
							}else if(type == 'checkbox'){
								// $('#'+NonEditableFields[i]).on("click", function(){ return false; });
								if($('#'+NonEditableFields[i]).parent().hasClass('switch-primary') == true){
									$('#'+NonEditableFields[i]).parent().css('pointer-events','none');
								}else{
									// $('#'+NonEditableFields[i]).click(function(){return false;});
									$('*[id*='+NonEditableFields[i]+']').each(function() {
										$(this).css('pointer-events','none');
									});
								}
							}else if(type == 'radio'){
								$('*[id*='+NonEditableFields[i]+']').each(function() {
										$(this).css('pointer-events','none');
									});
								// $("input[type='radio']").click(function(){return false;});
							}else if(type == 'file'){
								if($('#'+NonEditableFields[i]).prop('multiple') == true){

									$('#'+NonEditableFields[i]).css('pointer-events','none');
									// console.log($('#'+NonEditableFields[i]).parent());
									$('#'+NonEditableFields[i]).nextAll().find('span').remove();
									
								}else{
									// $('#'+NonEditableFields[i]).parent().css('pointer-events','none');
								}
							}
						}
					}

					if(result["tempData"]){
						// console.log(result["tempData"][0]);
						$("#"+Object.keys(result["tempData"][0])).empty();
						for(var i=0; i<result["tempData"].length; i++){
							$("#"+Object.keys(result["tempData"][i])).append(Object.values(result["tempData"][i]));
						}
					}
				}
			}
		});
	}

	function ConfirmDialog(message) {
		$('<div></div>').appendTo('body')
			.html('<div><h6>' + message + '?</h6></div>')
			.dialog({
				modal: true,
				title: 'Delete message',
				zIndex: 10000,
				autoOpen: true,
				width: 'auto',
				resizable: false,
				buttons: {
					Yes: function() {
					// $(obj).removeAttr('onclick');                                
					// $(obj).parents('.Parent').remove();

					$('body').append('<h1>Are you sure: <i>Yes</i></h1>');

					$(this).dialog("close");
					},
					No: function() {
					$('body').append('<h1>Confirm Dialog Result: <i>No</i></h1>');

					$(this).dialog("close");
					}
				},
				close: function(event, ui) {
					$(this).remove();
				}
			});
	};

	//Check validation on edit
	function validateFormOnDelete(delID){
		// var d = ConfirmDialog("ARE YOU DELETING?");
		console.log("deleted");
		// return;
		$.ajax({
			type : 'POST',	
			dataType: 'json',
			data : {ValidationOn:'delete', FormID: <?php echo $FormID; ?>,DeleteID: delID},
			url : 'validateForm.php',
			success : function(result){
				//Success = 0 - show message & stop delete
				//Success = 1 - delete record
				//Success = 3 - show message & stop submit(Error in exicution of query)

				if(result['status'] == true){
					if(result['data']['Success'] == 1){
						alert('ENTRY DELETED!');
					}else if(result['data']['Success'] == 0){
						alert(result['data']['Msg']);
					}else{
						alert("Internal Server Error");
						$('#SaveUpdate').attr('disabled', false);
					}
				}
			}
		});
	}


	//Check validation on Lost focus
	function validateOnLostFocusSp(thiss,GridId){

		var lostFocusSpFieldID = $(thiss).attr('id');
		if(lostFocusSpFieldID == undefined){
			lostFocusSpFieldID = thiss;
		}
		// console.log("==>>validateOnLostFocusSp "+lostFocusSpFieldID+" == "+GridId);
	
		var fieldValue = $(thiss).val();
		if(GridId > 0){
			var gridIdentifier = $('#grid-id-'+GridId).attr('class');		
			var gridSerialNo = gridIdentifier.slice(4);

			var lostFocusSpFieldIDArray = lostFocusSpFieldID.split("-");

			if(lostFocusSpFieldIDArray[2].includes("SingleDDModal")){
				var replaceString = gridSerialNo+"SingleDDModal";
				var lostFocusSpField = lostFocusSpFieldIDArray[2].replace(new RegExp("^" + replaceString), "");
			}else{
				var lostFocusSpField = lostFocusSpFieldIDArray[2].substring(1);
			}

			
		}else{
			var lostFocusSpField = lostFocusSpFieldID;
		}

		var cuurrentRowData = '';
		var row = $('#'+lostFocusSpFieldID).closest('tr').find('input,select').each(function(){
			var tdid = $(this).attr('id');
			var fieldid = tdid.replace("kreon-grid-"+gridSerialNo, "");
			cuurrentRowData += fieldid.trim()+"="+$('#'+tdid).val();

			cuurrentRowData += "|";
		});
		cuurrentRowData = cuurrentRowData.slice(0, -1)

		$.ajax({
			type : 'POST',
			dataType: 'json',
			data : {ValidationOn:'lostfocus', FormID: <?php echo $FormID; ?>, GridID:GridId, FieldID: lostFocusSpField, fieldValue:fieldValue, formData: $('form').serialize(), cuurrentRowData:cuurrentRowData},
			url : 'validateForm.php',
			success : function(result){
				console.log(result);
				//Success = 0 - show message & stop submit
				//Success = 1 - submit form
				//Success = 2 - show message. If press ok then submit form or if press close then stop submit
				//Success = 3 - show message & stop submit(Error in exicution of query)
				if(result['status'] == true){

					if(result['data']['Success'] == 0){
						alert(result['data']['Msg']);
					}

					if(result['data']['Success'] == 3){
						alert("Internal Server Error");
					}

					if(result['data']['GridResponse']){
					
						if(result['data']['GridResponse'].length > 4){

							GridResponse = result['data']['GridResponse'].split("|");
							
							for(var i=0; i < GridResponse.length; i++){
								GridResponseArray = GridResponse[i].split(":");
								
								if(GridResponseArray[2].trim() == 1 || GridResponseArray[2].trim() == 2 || GridResponseArray[2].trim() == 3 || GridResponseArray[2].trim() == 7 || GridResponseArray[2].trim() == 8){
									if(GridId != 0){
										
										if(result['data']['Success'] == 0){
											$('#kreon-grid-'+gridSerialNo+''+GridResponseArray[0].trim()).val('');
										}else{
											$('#kreon-grid-'+gridSerialNo+''+GridResponseArray[0].trim()).val(GridResponseArray[1].trim());
										}
									}else{
										
										if(result['data']['Success'] == 0){
											$('#'+GridResponseArray[0].trim()).val();
										}else{
											$('#'+GridResponseArray[0].trim()).val(GridResponseArray[1].trim());
										}
									}
	
								}else if(GridResponseArray[2].trim() == 5 || GridResponseArray[2].trim() == 19){
									if(GridId != 0){
										if(result['data']['Success'] == 0){
											$('#kreon-grid-'+gridSerialNo+''+GridResponseArray[0].trim()).empty().trigger('change');
										}else{
											var $newOption = $("<option selected=\"selected\"></option>").val(GridResponseArray[3].trim()).text(GridResponseArray[1].trim())
											$('#kreon-grid-'+gridSerialNo+''+GridResponseArray[0].trim()).select2('open');
											$('#kreon-grid-'+gridSerialNo+''+GridResponseArray[0].trim()).append($newOption).trigger("change");
											$('#kreon-grid-'+gridSerialNo+''+GridResponseArray[0].trim()).select2('close');
										}
									}else{
										if(result['data']['Success'] == 0){
											$('#'+GridResponseArray[0].trim()).empty().trigger('change');
										}else{
											var $newOption = $("<option selected=\"selected\"></option>").val(GridResponseArray[3].trim()).text(GridResponseArray[1].trim());
											$('#'+GridResponseArray[0].trim()).select2('open');
											$('#'+GridResponseArray[0].trim()).append($newOption).trigger("change");
											$('#'+GridResponseArray[0].trim()).select2('close');
										}
									}
						
								}else if(GridResponseArray[2].trim() == 17){
									if(GridId != 0){
										if(result['data']['Success'] == 0){
											$('#kreon-grid-'+gridSerialNo + GridResponseArray[0].trim()+'-text').val();
											$('#kreon-grid-'+gridSerialNo + GridResponseArray[0].trim()).val();
										}else{
											$('#kreon-grid-'+gridSerialNo + GridResponseArray[0].trim() + '-text').val(GridResponseArray[1]);
											$('#kreon-grid-'+gridSerialNo + GridResponseArray[0].trim()).val(GridResponseArray[3]);
										}
									}else{
										if(result['data']['Success'] == 0){
											$('#'+GridResponseArray[0].trim()+'-text').val();
											$('#'+GridResponseArray[0].trim()).val();
										}else{
											$('#'+GridResponseArray[0].trim()+'-text').val(GridResponseArray[1]);
											$('#'+GridResponseArray[0].trim()).val(GridResponseArray[3]);
										}
									}
								}else if(GridResponseArray[2].trim() == 6){
									if(GridResponseArray[1] != ''){
										var dateObj =  GridResponseArray[1].trim();
										console.log(dateObj);
										// var [day, month, year] = dateObj.split("-");
										// var date = new Date(`${year}-${month}-${day}`);
										// var inputDateValue = date.toISOString().split('T')[0];
										var [year, month, day] = dateObj.split("-");  // Notice year comes first!
										var date = new Date(`${year}-${month}-${day}`);  // Use backticks

										var inputDateValue = date.toISOString().split('T')[0]; 
										console.log(inputDateValue);  // Optional: check the result
									}

									if(GridId != 0){
										
										if(result['data']['Success'] == 0){
											$('#kreon-grid-'+gridSerialNo+''+GridResponseArray[0].trim()).val('');
										}else{
											$('#kreon-grid-'+gridSerialNo+''+GridResponseArray[0].trim()).val(inputDateValue);
										}
									}else{
										
										if(result['data']['Success'] == 0){
											$('#'+GridResponseArray[0].trim()).val();
										}else{
											$('#'+GridResponseArray[0].trim()).val(inputDateValue);
										}
									}
	
								}
							}
						}	
					}

					if(result['data']['NonEditableFields']){
						var NonEditableFields =result['data']['NonEditableFields'].split('|');
						
						for(var k=0; k < NonEditableFields.length; k++){
							var NonEditableFieldArray = NonEditableFields[k].split(':');
							if(NonEditableFieldArray[1].trim() == 'lock'){
								$('#'+NonEditableFieldArray[0].trim()).prop('readonly', true).css('pointer-events','none').css('background-color','#eee');
								$('#'+NonEditableFieldArray[0].trim()).next('span').find('.select2-selection--single').prop('readonly', true).css('pointer-events','none').css('background-color','#eee');

							}else{
								$('#'+NonEditableFieldArray[0].trim()).prop('readonly', false).css('pointer-events','').css('background-color','white');
								$('#'+NonEditableFieldArray[0].trim()).next('span').find('.select2-selection--single').prop('readonly', false).css('pointer-events','').css('background-color','white');

							}
						}
					}

					if(result["tempData"]){
						$("#"+Object.keys(result["tempData"][0])).empty();
						for(var i=0; i<result["tempData"].length; i++){
							console.log(Object.values(result["tempData"][i]));
							$("#"+Object.keys(result["tempData"][i])).append(Object.values(result["tempData"][i]));
						}
					}
				}

			}
		});
	}

	//Check validation on change
	function validateOnChangeSp(thiss,GridId){
		var onChangeSpFieldID = $(thiss).attr('id');
		if(GridId > 0){
			var onChangeSpFieldIDArray = onChangeSpFieldID.split("-");
			var onChangeSpField = onChangeSpFieldIDArray[2].substring(1);
		}else{
			var onChangeSpField = onChangeSpFieldID;
		}

		$.ajax({
			type : 'POST',
			dataType: 'json',
			data : {ValidationOn:'onchange', FormID: <?php echo $FormID; ?>, FieldID: onChangeSpField, formData: $('form').serialize()},
			url : 'validateForm.php',
			success : function(result){
				//Success = 0 - show message & stop submit
				//Success = 1 - submit form
				//Success = 2 - show message. If press ok then submit form or if press close then stop submit
				//Success = 3 - show message & stop submit(Error in exicution of query)
				
				if(result['status'] == true){
					if(result['data']['Success'] == 0){
						alert(result['data']['Msg']);
					}
					if(result['data']['Success'] == 0){
						alert("Internal Server Error");
					}
					
				}
			}
		});
	}

	//Check validation on change
	function validateOnClick(DBFieldName,validateOnClickSelectedValue,validateOnClickSelectedLabel){
		
		// var onChangeSpFieldID = $(thiss).attr('id');
		// if(GridId > 0){
		// 	var onChangeSpFieldIDArray = onChangeSpFieldID.split("-");
		// 	var onChangeSpField = onChangeSpFieldIDArray[2].substring(1);
		// }else{
			var onClickSpField = DBFieldName;
		// }

		$.ajax({
			type : 'POST',
			dataType: 'json',
			data : {ValidationOn:'onclick', FormID: <?php echo $FormID; ?>, FieldID: onClickSpField, SelectedVALUE:validateOnClickSelectedValue, SelectedLabel:validateOnClickSelectedLabel, formData: $('form').serialize()},
			url : 'validateForm.php',
			success : function(result){
				//Success = 0 - show message & stop submit
				//Success = 1 - submit form
				//Success = 2 - show message. If press ok then submit form or if press close then stop submit
				//Success = 3 - show message & stop submit(Error in exicution of query)

				if(result['status'] == true){
					if(result['data'][0]['Success'] == 0){
						alert(result['data'][0]['Msg']);
					}

					if(result['data'][0]['Success'] == 0){
						alert("Internal Server Error");
					}
				}
			}
		});
	}
	
	$(document).ready(function(){
		// Attach the event to all date inputs
		// $('input[type="date"]').on('keyup blur', function () {
		$('input[type="date"]').on('blur', function () {
			// alert('hi');
			const dateInput = $(this);
			const dateValue = dateInput.val();
			
			// Regular expression to check the format YYYY-MM-DD
			const regex = /^(19\d{2}|20\d{2}|21[0-9]{2})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/;
			
			// if(dateValue.length == 10 && (dateValue.startsWith('19') || dateValue.startsWith('20'))){
				const fullDate = new Date(dateValue);
				const minDate = new Date(dateInput.attr('min'));
				const maxDate = new Date(dateInput.attr('max'));

				// Check if minDate and maxDate are valid Date objects
				if (minDate != 'Invalid Date' || maxDate !='Invalid Date') {
					if (fullDate && (fullDate < minDate || fullDate > maxDate)) {
						alert("Please enter a date within the allowed range!");
						dateInput.val(""); // Clear the invalid input
					}
				}
			// }



			return;
			// Check if the date input is complete (YYYY-MM-DD format)
			if (dateValue.length >= 10) {  // Minimum length for a valid date (YYYY-MM-DD) is 10 characters
				// Only validate if the date matches the expected format
				console.log(dateValue, regex.test(dateValue));
				if (regex.test(dateValue)) {
					const fullDate = new Date(dateValue);
					const minDate = new Date(dateInput.attr('min'));
					const maxDate = new Date(dateInput.attr('max'));

					// Check if minDate and maxDate are valid Date objects
					if (minDate != 'Invalid Date' || maxDate !='Invalid Date') {
						// Check if the value is out of range
						if (fullDate && (fullDate < minDate || fullDate > maxDate)) {
							alert("Please enter a date within the allowed range!");
							dateInput.val(""); // Clear the invalid input
						}
					} 
				} else {
					console.log('valid date')
				}
			}
		});

		
		//SHIFTED TO K_HEADER
		// $(window).bind('keydown', function(event) {	//FOR SHORTCUTS AROUND THE APPLICATION
		// 	if (event.ctrlKey || event.metaKey) {
		// 		switch (String.fromCharCode(event.which).toLowerCase()) {
		// 			case 's':
		// 				//On ctrl+s save & update form
		// 				event.preventDefault();
		// 				document.getElementById('SaveUpdate').click();
		// 				break;
		// 			case 'e':
		// 				//On ctrl+e preview page open edit form
		// 				event.preventDefault();
		// 				const search = window.location.search;
		// 				const substr = 'preview';
		// 				if(search.includes(substr)){
		// 					const pathname = window.location.pathname;
		// 					const newSearch = search.split('&');
		// 					const nLength = newSearch.length;
		// 					const pathSearch = newSearch[nLength-1].replace('preview','edit');
		// 					window.location.href = pathname+""+newSearch[0]+"&"+pathSearch;
		// 				}
		// 				break;
		// 			case 'p':
		// 				//On ctrl+p save & preview
		// 				event.preventDefault();
		// 				document.getElementById('SaveAndPreview').click();
		// 				break;
		// 			case 'd':
		// 				event.preventDefault();
		// 				document.getElementById('previewDeleteButton').click();
		// 				break;
		// 			case 'x':
		// 				if(data1.AdjustableAmount == 'enable'){
		// 					if (confirm('Are you sure? You want to save adjustment amount.')) {
		// 						$('#saveAdjAmtButton').click();
		// 					}else{
		// 						$('.close-modal').off('click');
		// 					}
		// 				}else{
		// 					$('#saveAdjAmtButton').attr('disabled', false);
		// 					$("#DropDownModal").modal("hide"); 
		// 				}

		// 		}
		// 	}

		// 	if (event.altKey || event.metaKey) {
		// 		switch (String.fromCharCode(event.which).toLowerCase()) {
		// 			case 'a':
		// 				//On alt+a save & open new form
		// 				event.preventDefault();
		// 				document.getElementById('SaveUpdateAddMore').click();
		// 				break;
		// 			case 'f':
		// 				//On alt+f Find from Menu
		// 				event.preventDefault();
		// 				$("#mySearch").focus();
		// 				$('.sidebar-toggle.hidden-xs').click();
		// 				break;
		// 			case 'n':
		// 				//On alt+n listview open new form
		// 				event.preventDefault();
		// 				const search = window.location.search;
		// 				const substr = 'view=1';
		// 				if(search.includes(substr)){
		// 					const pathname = window.location.pathname;
		// 					const newSearch = search.split('&');
		// 					const newUrl = pathname+"?"+newSearch[1];
		// 					window.location.href = newUrl;
		// 				}
		// 				break;
		// 			case 'g':
		// 				//On alt+g grid plus button press
		// 				// var targetIdSplit = event.target.id.split('-');
		// 				// var gridSerialNo = targetIdSplit[2].charAt(0);
		// 				// $(".add"+gridSerialNo).click();

		// 				// alert(targetIdSplit);
		// 				// alert(event.currentTarget.parentNode);
		// 				// alert(event.target.parentElement.parentElement.id);
		// 				var tmp = event.target.parentElement;
		// 				do{
		// 					tmp = tmp.parentElement;
		// 					// alert(tmp.tagName);
		// 				}while(tmp.tagName != 'TABLE');
		// 				var panelID = tmp.parentElement.id;
		// 				// alert(panelID.charAt(0));
		// 				$(".add" + panelID.slice(-1)).click();
		// 				break;
		// 		}
		// 	}
		// });


		// Open Select2 dropdown on focus for respective elements
		$(document).on('focus', '.select2-selection.select2-selection--single', function () {
		const sel = $(this).closest('.select2-container').prev('select');
		if (!sel.data('select2-open')) {
			sel.select2('open');
		}
		});

		// var timepicker = new TimePicker('time', {
		// 	lang: 'en',
		// 	theme: 'dark'
		// });
		// timepicker.on('change', function(evt) {
		// 	var value = (evt.hour || '00') + ':' + (evt.minute || '00');
		// 	evt.element.value = value;
		// });


		$('.amodal').click(function(){ //Open modal for Add Master entry
			var BtnVal = $(this).val().split('-');
			var FormId = BtnVal[0];  //Get formid
			var targetFieldId = BtnVal[1]; //Get target field id
			var formType = BtnVal[2];
			var AddModal = 'AddMoreModal'; //Modal id
			var AddModalForm = 'ModelContent'; //Model bidy id where we display form
			$.ajax({
				type : 'POST',
				dataType: 'json',
				data : {},
				url : '/api/form.php?FormID='+FormId+'&editID=0',
				success : function(result){
					// console.log(result);
					showModalField(result,FormId,targetFieldId,formType,AddModal,AddModalForm); //On ajax response create input fields
				}
			});
		});	

		function showModalField(result, FormId, targetFieldId,formType,AddModal,AddModalForm){
			var code = result['code'];
			var mfld = "";
			var mreqv = [];
			var mreqf = [];	

			mfld += "<div class='container1'><div class='row' ><div class='col-md-12 allfields'>";
			for(var i = 0; i < code.length; i++){
				mreqv[i] = "";
				mreqf[i] ="";
				if(code[i][5] == 1){
					mreqf[i] = 'required';
					mreqv[i] = '<span class="required"> *</span>'
				}
				mfld += "<div class='"+code[i][0]+"'>";
				if(code[i][1] == 1){ //Text
					mfld += "<div class='col-md-6'><label>"+code[i][2]+"</label>"+mreqv[i]+"<input class='form-control dyn1' "+mreqf[i]+" type='text' name='"+code[i][0]+"' id='"+code[i][0]+"' value=''></div>";
				}

				if(code[i][1] == 3){ //Textarea
					mfld += "<div class='col-md-6'><label>"+code[i][2]+"</label>"+mreqv[i]+"<textarea class='form-control dyn1' "+mreqf[i]+" name='"+code[i][0]+"' id='"+code[i][0]+"' value=''></textarea></div>";
				}

				if(code[i][1] == 6){ //Date
					mfld += "<div class='col-md-6'><label>"+code[i][2]+"</label>"+mreqv[i]+"<input class='form-control dyn1' "+mreqf[i]+" type='date' name='"+code[i][0]+"' id='"+code[i][0]+"' value=''></div>";
				}

				if(code[i][1] == 7){ //Number
					mfld += "<div class='col-md-6'><label>"+code[i][2]+"</label>"+mreqv[i]+"<input class='form-control dyn1' "+mreqf[i]+" type='number' name='"+code[i][0]+"' id='"+code[i][0]+"' value=''></div>";
				}

				if(code[i][1] == 8){ //Email
					mfld += "<div class='col-md-6'><label>"+code[i][2]+"</label>"+mreqv[i]+"<input class='form-control dyn1' "+mreqf[i]+" type='email' name='"+code[i][0]+"' id='"+code[i][0]+"' value=''></div>";
				}

				if(code[i][1] == 5){ //Select From DB
					var v =  result[code[i][0]]['result_set'];
					var k =  result[code[i][0]]['keys'];

					mfld += "<div class='col-md-6'><label>"+code[i][2]+"</label>"+mreqv[i]+"<select style='width:-webkit-fill-available;' name='"+code[i][0]+"' id='"+code[i][0]+"'>";

					mfld += "<option value='0' ></option>";

					for(var j=0; j < v.length; j++){ 
						mfld += "<option value='"+v[j][k[0]]+"' >"+v[j][k[1]]+"</option>";
					}

					mfld += "</select></div>";
				}

				if(code[i][1] == 9){ //Checkbox From DB
					var v =  result[code[i][0]]['result_set'];
					var k =  result[code[i][0]]['keys'];
					mfld += "<div class='col-md-6'><label>"+code[i][2]+"</label></br>";
					for(var j=0; j < v.length; j++){ 
						mfld += "<label><input '"+code[i][5]+"' type='checkbox' name='"+code[i][0]+"[]' id='"+code[i][0]+"' value=''>"+v[j][k[1]]+"</label></br>";
					}
					mfld += "</div>";
				}

				if(code[i][1] == 4){ //Radio From DB
					var v =  result[code[i][0]]['result_set'];
					var k =  result[code[i][0]]['keys'];
					mfld += "<div class='col-md-6'><label>"+code[i][2]+"</label></br>";

					for(var j=0; j < v.length; j++){ 
						mfld += "<label><input '"+code[i][5]+"' type='radio' name='"+code[i][0]+"' value=''>"+v[j][k[1]]+"</label></br>";
					}

					mfld += "</div>";
				}

				mfld += "</div>";
			}

			mfld += "<input type='hidden' id='formid' value='"+FormId+"'>";
			mfld += "<input type='hidden' id='hiddenTargetField' value='"+targetFieldId+"'>";
			mfld += "<input type='hidden' id='formType' value='"+formType+"'>";
			mfld += "</div></div></div>";
			$('#'+AddModalForm).html(mfld);
			$('#'+AddModal).modal('show');
		}

		$("#modalForm").submit(function(event){
			var targetFieldId = document.getElementById('hiddenTargetField').value;
			var FormID = document.getElementById('formid').value;
			var formType = document.getElementById('formType').value;
			saveAddMaster(targetFieldId,FormID,formType);
			return false;
		});

		function saveAddMaster(targetFieldId,FormID,formType){
			$.ajax({
				type : 'POST',
				dataType: 'json',
				data : $('form#modalForm').serializeArray(),

				url : '/api/appdb.php?FormID='+FormID,

				success : function(result){
					console.log(result['data']);
					if(formType == 14){
						var gridIdentifier = ($('#'+targetFieldId).parent().parent().attr("id")).slice(0,1);
						var gridCnt = $("#cnt" + gridIdentifier).val();
						var lastElementFlag = false;			

						for(var i = gridCnt - 1; i >= 0; i--){
							// debugger;
							if($('#' + gridIdentifier + i ).length){ //if element exists
								if(!lastElementFlag){	//if last element then select the added value
									if($('#' + gridIdentifier + i +' select[name="aQualityName[]"]').val() > 0){ //If last element has pre selected value then add a new row
										$('.add' + gridIdentifier).click();
										i++;
									}

									$('#' + gridIdentifier + i + ' select[name="aQualityName[]"]').append($("<option></option>")
										.attr("value", result['data'][0]['ID'])
										.text(result['data'][0]['Name']));
									lastElementFlag = true;
									$('#' + gridIdentifier + i +' select[name="aQualityName[]"]').val(result['data'][0]['ID']).trigger('change');
								}else{

									$('#' + gridIdentifier + i + ' select[name="aQualityName[]"]').append($("<option></option>")
									.attr("value", result['data'][0]['ID'])
									.text(result['data'][0]['Name']))
								}
							}
						}
					}else{
						$('#'+targetFieldId).append($("<option></option>")
						.attr("value", result['data'][0]['ID'])
						.text(result['data'][0]['Name']));
						$('#'+targetFieldId).val(result['data'][0]['ID']).trigger('change');
					}

					$("#AddMoreModal").modal('hide');
					// $('#success-info').show();
					// $("#success-info").delay(5000).fadeOut(); 
				},
			});
		}
	});

	//Check value is exist or not
	function onLostFocus(thiss,editID,type){
		var thissParameters = $("#" + thiss.id + "-hidden").val();
		var fieldName = $(thiss).attr('id');
		var fieldValue = $(thiss).val();
		
		var str1 = "response";
		var responsePos = -1;
		if(thissParameters.indexOf(str1) != -1){
			responsePos = thissParameters.indexOf(str1);
		}

		var str2 = "DB";
		var dbPos = -1;
		if(thissParameters.indexOf(str2) != -1){
			dbPos = thissParameters.indexOf(str2);
		}

		var newArr = ["", ""];
		if(dbPos != -1 && responsePos != -1){ //to split between both the parameters

			var splitIndex = dbPos > responsePos ? dbPos : responsePos;
 			newArr[0] = thissParameters.substring(0, splitIndex - 1)
 			newArr[1] = thissParameters.substring(splitIndex);
	
			var responseParams = newArr[0].substring(
						newArr[0].indexOf("{") + 1, 
						newArr[0].lastIndexOf("}")
					);

			var dbParams = newArr[1].substring(
						newArr[1].indexOf("{") + 1, 
						newArr[1].lastIndexOf("}")
					);

			// console.log(responseParams);	//#Broker : 5 | response.ID | response.Name
			// console.log(dbParams);
			var responseP = responseParams.split(',');	//THIS will give all the parameters required in response
			var finalParams = [];
			var finalParamsValue = [];
			var Parameters = [];
			var flag = 0;
			for(var i = 0; i < responseP.length; i++){
				finalParams[i] = responseP[i].trim().split('|');
				for(var j=0; j<finalParams[i].length; j++)	{
					finalParamsValue[j] =  $('#'+finalParams[i][j].trim()).val();

					if(finalParamsValue[j].length > 0){
						flag = 1;
						if(editID == 0){
							if(type == 'grid'){
								var finalParamsGridFieldSplit = finalParams[i][j].split('-');
								var paramkv = finalParamsGridFieldSplit[2].substring(1) + "=" + "'"+finalParamsValue[j]+"'";
							}else{
								var paramkv = finalParams[i][j] + "=" + "'"+finalParamsValue[j]+"'";
							}
						}else{

							var paramkv = finalParams[i][j] + "=" + "'"+finalParamsValue[j]+"'";
						}
						Parameters.push(paramkv);
					}
				}
			}
		}


		if(flag == 1){
			$.ajax({
				type : 'POST',
				dataType: 'json',
				data : {selectedValue: Parameters, editId: editID, fromRepo: dbParams},
				url : 'checkDuplicateData.php',
				success : function(result){
					//CHECK TYPE OF PARAMETERS
					// console.log(result);
					if(result['rows'] > 0){
						if(editID == 0){
							alert(fieldValue+" already exists.");
						}else{
							$( "."+fieldName ).find('div').append( "<p id='"+fieldName+"-hide' style='color:red;'>"+fieldValue+" already exists.</p>" );
							setTimeout(function() {
								$("#"+fieldName+"-hide").remove();
							}, 2000);
						}
						$("#"+fieldName).val('');

					}
									
				}
			});
		}
	}

	//Exicute given formula & place value in perticular field (NOTE- for calculation onkeyupparameter using so this function no longer need)
	// function onLostFocusCalculation(thiss,type,gridSerialNo){
	// 	var thissParameters = $("#" + thiss.id + "-hidden").val();
	
	// 	var str1 = "Formula";
	// 	var FormulaPos = -1;
	// 	if(thissParameters.indexOf(str1) != -1){
	// 		FormulaPos = thissParameters.indexOf(str1);
	// 	}

	// 	var str2 = "responseField";
	// 	var responseFieldPos = -1;
	// 	if(thissParameters.indexOf(str2) != -1){
	// 		responseFieldPos = thissParameters.indexOf(str2);
	// 	}

	// 	var newArr = ["", ""];
	// 	if(FormulaPos != -1 && responseFieldPos != -1){ //to split between both the parameters

	// 		var splitIndex = FormulaPos > responseFieldPos ? FormulaPos : responseFieldPos;
 	// 		newArr[0] = thissParameters.substring(0, splitIndex - 1)
 	// 		newArr[1] = thissParameters.substring(splitIndex);
	
	// 		var FormulaParams = newArr[0].substring(
	// 			newArr[0].indexOf("{") + 1, 
	// 			newArr[0].lastIndexOf("}")
	// 		);

	// 		var responseFieldParams = newArr[1].substring(
	// 			newArr[1].indexOf("{") + 1, 
	// 			newArr[1].lastIndexOf("}")
	// 		);

	// 	}
	// 	if(FormulaParams.length > 1){
	// 		var extractedparameters = FormulaParams.replace(/\*+/g, ' * ').replace(/\++/g, ' + ').replace(/\-+/g, ' - ').replace(/\\+/g, ' \ ');
			
			
	// 		var parameters = extractedparameters.split(" ");
	// 		var requestParams = '';

	// 		for(var i=0; i<parameters.length; i++){
	// 			if(parameters[i].indexOf("*") >= 0 || parameters[i].indexOf("+") >= 0 || parameters[i].indexOf("*") >= 0 || parameters[i].indexOf("/") >= 0 || parameters[i].indexOf("=") >= 0){
	// 				requestParams += parameters[i].trim() +" ";
	// 			}else{
	// 				if(parameters[i].length > 1){
	// 					if(type == 'grid'){
	// 						requestParams += $('#kreon-grid-'+gridSerialNo+parameters[i]).val();
	// 					}else{
	// 						requestParams += $('#'+parameters[i]).val();
	// 					}
	// 				}
	// 			}
	// 		}
	// 	}
		

	// 	if(responseFieldParams.length > 1){
	// 		if(type == 'grid'){
	// 			$('#kreon-grid-'+gridSerialNo+responseFieldParams).val(eval(requestParams));
	// 		}else{
	// 			$('#'+responseFieldParams).val(eval(requestParams));
	// 		}
	// 	}
	// }

	function dynamicFreeFill(thiss){
		var str1 = "response";
		var responsePos = -1;
		if(thiss.indexOf(str1) != -1){
			responsePos = thiss.indexOf(str1);
		}
		var str2 = "url";
		var urlPos = -1;
		if(thiss.indexOf(str2) != -1){
			urlPos = thiss.indexOf(str2);
		}

		var newArr = ["", ""];
		if(urlPos != -1 && responsePos != -1){ //to split between both the parameters
			var splitIndex = urlPos > responsePos ? urlPos : responsePos;
 			newArr[0] = thiss.substring(0, splitIndex - 1)
 			newArr[1] = thiss.substring(splitIndex);
			// console.log(newArr[0]);
			// console.log(newArr[1]);
			var responseParams = newArr[0].substring(
						newArr[0].indexOf("{") + 1, 
						newArr[0].lastIndexOf("}")
					);

			var urlParams = newArr[1].substring(
						newArr[1].indexOf("{") + 1, 
						newArr[1].lastIndexOf("}")
					);

			// console.log(responseParams);	//#Broker : 5 | response.ID | response.Name
			// console.log(urlParams);
			var responseP = responseParams.split(',');	//THIS will give all the parameters required in response
			var finalParams = [];
			for(var i = 0; i < responseP.length; i++){
				finalParams[i] = responseP[i].trim().split('|');
			}
		}
		// console.log(finalParams);
		$.ajax({
			type : 'POST',
			dataType: 'json',
			data : {},
			url : urlParams,
			success : function(result){
				//CHECK TYPE OF PARAMETERS
				console.log(result);
				for(var i = 0; i < finalParams.length; i++){
					if(finalParams[i][1].trim() == 0 || finalParams[i][1].trim() == 1 || finalParams[i][1].trim() == 3 || finalParams[i][1].trim() == 6 || finalParams[i][1].trim() == 7 || finalParams[i][1].trim() == 8 ){	//type = textbox & others //#TrspPay | 1 | TrspPay 
						$(finalParams[i][0].trim()).val(result[finalParams[i][2].trim()]);
					}
				}				
			}
		});
	}

	async function CreateOnChangeScriptGridCalculation(gridCalculationFieldNames, gridCalculationTypes, gridSerialNo) {
		console.log("in CreateOnChangeScriptGridCalculation");
		console.log(gridCalculationFieldNames);
		console.log(gridCalculationTypes);
		// console.log(gridSerialNo);

		try {
			// Get the number of rows excluding the header
			var noOfRows = $('#cnt' + gridSerialNo).val();
			
			// Initialize an array to hold the results for each column
			var columnResults = new Array(gridCalculationFieldNames.length).fill(0);
			var columnCounts = new Array(gridCalculationFieldNames.length).fill(0); // Track non-empty values for count

			// Use async/await to wait for the DOM manipulation before proceeding
			await calculateColumnResults(noOfRows, gridCalculationFieldNames, gridCalculationTypes, gridSerialNo, columnResults, columnCounts);

			// Assuming you want to add a footer row with the result for each column
			updateFooterWithColumnResults(gridSerialNo, gridCalculationFieldNames, columnResults, columnCounts, gridCalculationTypes, noOfRows);

		} catch (error) {
			console.error("Error in calculation:", error);
		}	
	}

	// Function to calculate column results (sum, average, count) asynchronously
	// async function calculateColumnResults(noOfRows, gridCalculationFieldNames, gridCalculationTypes, gridSerialNo, columnResults, columnCounts) {
	// 	return new Promise(resolve => {
	// 		setTimeout(() => {
	// 			for (var j = 0; j < gridCalculationFieldNames.length; j++) {
	// 				for (var i = 0; i < noOfRows; i++) {
	// 					var serialNo = gridSerialNo + (i + 1);
	// 					// Parse the value to ensure it's a number
	// 					var value = parseFloat($("#" + serialNo + " td #" + gridSerialNo + gridCalculationFieldNames[j]).val());
						
	// 					if (!isNaN(value)) {
	// 						if (gridCalculationTypes[j] === 1) { // Sum
	// 							columnResults[j] += value;
	// 						} else if (gridCalculationTypes[j] === 2) { // Average
	// 							columnResults[j] += value;
	// 						} else if (gridCalculationTypes[j] === 3) { // Count
	// 							columnCounts[j]++;
	// 						}
	// 					}
	// 				}
	// 			}
	// 			resolve();
	// 		}, 1000); // Simulate an async delay of 1 second
	// 	});
	// }

	async function calculateColumnResults(noOfRows, gridCalculationFieldNames, gridCalculationTypes, gridSerialNo, columnResults, columnCounts) {
		// console.log("calculateColumnResults",noOfRows, gridCalculationFieldNames, gridCalculationTypes, gridSerialNo, columnResults, columnCounts) ;
		return new Promise(resolve => {
			setTimeout(() => {
				// console.log(gridSerialNo);
				var rows = document.querySelectorAll(`.tmp_GridTable${gridSerialNo} tbody tr.grp${gridSerialNo}:not(#a0)`);
				// console.log("IN CAL",rows);

				rows.forEach(row => {
					gridCalculationFieldNames.forEach((field, index) => {
						// const input = row.querySelector(`[name$="${field}[]"]`);
						const input = row.querySelector(`[name="${gridSerialNo}${field}[]"]`); //changed by mitesh due to issue with the above code
						const value = parseFloat(input?.value);
						if (!isNaN(value)) {
							switch (gridCalculationTypes[index]) {
								case 1: columnResults[index] += value; break; // Sum
								case 2: columnResults[index] += value; columnCounts[index]++; break; // Avg
								case 3: columnCounts[index]++; break; // Count
							}
						}
						// console.log("calculating:", value, field, index, columnResults[index], input);
					});
				});
				// console.log(columnResults, columnCounts);
				
				resolve();
			}, 1000); // Simulate async delay
		});
	}

	// Function to update footer values based on column results (sum, average, count)
	function updateFooterWithColumnResults(gridSerialNo, gridCalculationFieldNames, columnResults, columnCounts, gridCalculationTypes, noOfRows) {
		
		for (var j = 0; j < gridCalculationFieldNames.length; j++) {
			// Find the footer element for this column
			var footerElement = $('#' + gridSerialNo + gridCalculationFieldNames[j] + "Footer");
			if (footerElement.length > 0) {
				if (gridCalculationTypes[j] === 1) { // Sum
					// footerElement.val(Math.round(columnResults[j] * 100) / 100);

					//updated this code by pornima for decimal places not showing proper on date 21/3/2025
					var decimalPlaces = $("#kreon-grid-"+gridSerialNo+""+ gridCalculationFieldNames[j].trim()).data('key');
					if(decimalPlaces == undefined){
						footerElement.val(columnResults[j]);
					}else{
						footerElement.val(Math.round(columnResults[j] * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces));
					}
					//previous code
					// footerElement.val(columnResults[j]);

				} else if (gridCalculationTypes[j] === 2) { // Average
					// Calculate average by dividing the sum by the total number of rows
					if (noOfRows > 0) {
						var average = columnResults[j] / noOfRows;
						// footerElement.val(Math.round(average * 100) / 100); // Round average to 2 decimal places
						footerElement.val(average); // Round average to 2 decimal places

						// footerElement.val(columnResults[j] / noOfRows);
					} else {
						footerElement.val(0); // Avoid division by zero
					}
				} else if (gridCalculationTypes[j] === 3) { // Count
					footerElement.val(columnCounts[j]);
				}
			}
		}
	}


	// function CreateOnChangeScriptGridCalculation(thiss, calcuType, type, currentrowno) {
	// 	// console.log("CreateOnChangeScriptGridCalculation",thiss, calcuType, type, currentrowno)
	// 	var fieldId = thiss;
	// 	// Extract the footer ID from the field ID
	// 	var splitString = fieldId.split('kreon-grid-');
	// 	var footerID = splitString[1].slice(1) + "Footer";

	// 	var sum = 0;
	// 	var arrayvalue = [];
	// 	var fieldvalue, footerValue = 0;
		
	// 	// Get the current value of the changed field
	// 	fieldvalue = $("#" + fieldId).val();
		
	// 	// Get the footer value or initialize it to 0
	// 	footerValue = $("#" + footerID).val();
	// 	if (footerValue === "") {
	// 		footerValue = 0;
	// 	}

	// 	// Get the number of rows excluding the header
	// 	var noOfRows = $('#cnt'+splitString[1][0]).val();

	// 	// Handle case when there's an active editing row
	// 	var element = $(".AddEdit a:nth-child(2)");
	// 	var editRowId = element.attr("id");

	// 	// If no edit row, proceed with recalculating for all rows
	// 	setTimeout(
	// 		function() {
	// 			for (var i = 0; i < noOfRows; i++) {
	// 				var columnID = splitString[1][0] + (i+1);
	// 				var fieldvalue = $("#" + columnID + " td #" + splitString[1]).val();
	// 				// console.log("#" + columnID + " td #" + splitString[1], fieldvalue);
					
	// 				//Next 6 lines added by Mitesh as we dont want hidden rows for CUTPACK
	// 				var checkHiddenKeyword = $("#" + columnID ).attr("class");	
	// 				// console.log(checkHiddenKeyword)
	// 				if(checkHiddenKeyword !== undefined){
	// 					if(checkHiddenKeyword.indexOf("hidden") != -1){
	// 						continue;
	// 					}
	// 				}
	// 				if (fieldvalue !== undefined && fieldvalue !== '') {
	// 					arrayvalue.push(fieldvalue);
	// 				}
	// 			}

	// 			// Perform calculation based on the type selected
	// 			var calculation;
	// 			if (calcuType === 1) {
	// 				// Sum Calculation
	// 				calculation = arrayvalue.reduce(function (acc, val) {
	// 					return acc + parseFloat(val);
	// 				}, 0);
	// 			} else if (calcuType === 2) {
	// 				// Average Calculation
	// 				if (type === 'dynamic') {
	// 					calculation = arrayvalue.reduce(function (acc, val) {
	// 						return acc + parseFloat(val);
	// 					}, 0);

	// 					$("#" + footerID).val(calculation);

	// 					if (currentrowno === noofrows - 1) {
	// 						footerValue = $("#" + footerID).val();
	// 						calculation = footerValue / noofrows;
	// 					}
	// 				} else {
	// 					var average = arrayvalue.reduce(function (acc, val) {
	// 						return acc + parseFloat(val);
	// 					}, 0);
	// 					calculation = average / arrayvalue.length;
	// 				}
	// 			} else if (calcuType === 3) {
	// 				// Count Calculation
	// 				if (type === 'dynamic') {
	// 					calculation = noofrows;
	// 				} else {
	// 					calculation = arrayvalue.length;
	// 				}
	// 			}

	// 			// Update the footer value with the calculated result
	// 			$("#" +splitString[1][0]+ footerID).val(calculation);

	// 	}, 1050);
	// }

	// function CreateOnChangeScriptGridCalculation(thiss, calcuType, type, currentrowno) {
		
	// 	if(type == "dynamic"){
	// 		var fieldId = thiss;
	// 	}else{

	// 		var fieldId = thiss[1];
	// 	}
	// 	console.log(fieldId);

	// 	// Extract the footer ID from the field ID
	// 	var splitString = fieldId[0];
	// 	var footerID = fieldId.slice(1) + "Footer";

	// 	var sum = 0;
	// 	var arrayvalue = [];
	// 	var fieldvalue, footerValue = 0;
		
	// 	// Get the current value of the changed field
	// 	// fieldvalue = $("#" + fieldId).val();
		
	// 	// Get the footer value or initialize it to 0
	// 	footerValue = $("#" + footerID).val();
	// 	if (footerValue === "") {
	// 		footerValue = 0;
	// 	}

	// 	// var table = $('#panel'+splitString[1][0]).find('#GridTable').find('tbody tr');
	// 	// console.log(table);
	// 	// Get the number of rows excluding the header
		
	// 	var noOfRows = $('#cnt'+splitString).val();

	// 	// Handle case when there's an active editing row
	// 	var element = $(".AddEdit a:nth-child(2)");
	// 	var editRowId = element.attr("id");

	// 	console.log(noOfRows);
	// 	// If no edit row, proceed with recalculating for all rows
	// 	if (editRowId === undefined) {
	// 		console.log('new row');
	// 		// Loop through all rows to collect column data
	// 		// arrayvalue.push($('#'+thiss).val());
	// 		for (var i = 0; i < noOfRows; i++) {
	// 			var columnID = splitString[0] + (i+1);
	// 			var fieldvalue = $("#" + columnID).find("td input#"+fieldId).val();

	// 			if (fieldvalue !== undefined && fieldvalue !== '') {
	// 				arrayvalue.push(fieldvalue);
	// 			}
	// 		}
	// 	} else {
	// 		console.log('edit row');
	// 		// Handle recalculating while editing a specific row
	// 		for (var i = 0; i < noOfRows; i++) {
	// 			var columnID = "a" + i;

	// 			if (editRowId !== columnID) {
	// 				var fieldvalue = $("#" + columnID + " td #" + splitString[1]).val();
	// 				if (fieldvalue !== undefined && fieldvalue !== '') {
	// 					arrayvalue.push(fieldvalue);
	// 				}
	// 			}

	// 			if (editRowId === columnID) {
	// 				var fieldvalue1 = $("#" + fieldId).val();
	// 				if (fieldvalue1 !== undefined && fieldvalue1 !== '') {
	// 					arrayvalue.push(fieldvalue1);
	// 				}
	// 			}
	// 		}
	// 	}
		
	// 	console.log(arrayvalue);
	// 	// Perform calculation based on the type selected
	// 	var calculation;
	// 	if (calcuType === 1) {
	// 		// Sum Calculation
	// 		calculation = arrayvalue.reduce(function (acc, val) {
	// 			return acc + parseFloat(val);
	// 		}, 0);
	// 	} else if (calcuType === 2) {
	// 		// Average Calculation
	// 		if (type === 'dynamic') {
	// 			calculation = arrayvalue.reduce(function (acc, val) {
	// 				return acc + parseFloat(val);
	// 			}, 0);

	// 			$("#" + footerID).val(calculation);

	// 			if (currentrowno === noofrows - 1) {
	// 				footerValue = $("#" + footerID).val();
	// 				calculation = footerValue / noofrows;
	// 			}
	// 		} else {
	// 			var average = arrayvalue.reduce(function (acc, val) {
	// 				return acc + parseFloat(val);
	// 			}, 0);
	// 			calculation = average / arrayvalue.length;
	// 		}
	// 	} else if (calcuType === 3) {
	// 		// Count Calculation
	// 		if (type === 'dynamic') {
	// 			calculation = noofrows;
	// 		} else {
	// 			calculation = arrayvalue.length;
	// 		}
	// 	}
		
	// 	// Update the footer value with the calculated result
	// 	if(!isNaN(calculation)){

	// 		$("#" + footerID).val(calculation);
	// 	}
	// }

	// function CreateOnChangeScriptGridCalculation(thiss, calcuType, type, currentrowno){
	// 	// 1 = for sum
	// 	// 2 = for average
	// 	// 3 = For count
	// 	var table = document.getElementById("GridTable");
	// 	var fieldId = thiss;

	// 	// var gridIdentifier = $(thiss).parent().closest('tr').attr('id');
	// 	var sum = 0;
	// 	var arrayvalue = [];
	// 	var splitString = fieldId.split('kreon-grid-');
	// 	var footerID = splitString[1].slice(1)+"Footer";

	// 	var fieldvalue, footerValue = 0;
	// 	fieldvalue = $("#"+fieldId).val();
	// 	footerValue = $("#"+footerID).val();

	// 	var noOfRows = table.rows.length-1;

	// 	var element = $(".AddEdit a:nth-child(2)");
	// 	var editRowId = '';
	// 	editRowId = element.attr("id");

	// 	if(footerValue == ""){
	// 		footerValue = 0;
	// 	}

	// 	// for(var i=0; i<noOfRows; i++){
	// 	// 	var columnID = "a"+i;
	// 	// 	if(editRowId == undefined){
	// 	// 		if(i == 0){
	// 	// 			var fieldvalue1=$("#" + fieldId).val();
	// 	// 			if(fieldvalue1 != undefined){
	// 	// 				arrayvalue.push(fieldvalue1);
	// 	// 			}

	// 	// 			var fieldvalue=$("#" + columnID + " td " + "#" + splitString[1]).val();
	// 	// 			if(fieldvalue != undefined && fieldvalue != ''){
	// 	// 				arrayvalue.push(fieldvalue);
	// 	// 			}else if(fieldvalue == ''){
	// 	// 				return;
	// 	// 			}

	// 	// 		}else{
	// 	// 			var fieldvalue=$("#" + columnID + " td " + "#" + splitString[1]).val();
	// 	// 			if(fieldvalue != undefined && fieldvalue != ''){
	// 	// 				arrayvalue.push(fieldvalue);
	// 	// 			}else if(fieldvalue == ''){
	// 	// 				return;
	// 	// 			}
	// 	// 		}
	// 	// 	}else{  
	// 	// 		if(editRowId != columnID){
	// 	// 			var fieldvalue=$("#" + columnID + " td " + "#" + splitString[1]).val();
	// 	// 			if(fieldvalue != undefined && fieldvalue != ''){
	// 	// 				arrayvalue.push(fieldvalue);
	// 	// 			}else if(fieldvalue == ''){
	// 	// 				return;
	// 	// 			}
	// 	// 		}
				
	// 	// 		if(editRowId == columnID){
	// 	// 			var fieldvalue1=$("#" + fieldId).val();
	// 	// 			if(fieldvalue1 != undefined && fieldvalue1 != ''){
	// 	// 				arrayvalue.push(fieldvalue1);
	// 	// 			}else if(fieldvalue1 == ''){
	// 	// 				return;
	// 	// 			}
	// 	// 		}		
	// 	// 	}	
	// 	// }

	// 	if(editRowId == undefined){

	// 		if(editRowId == undefined){
	// 			var fieldvalue1=$("#" + fieldId).val();
	// 			if(fieldvalue1 != undefined){
	// 				arrayvalue.push(fieldvalue1);
	// 			}
	// 			arrayvalue.push(footerValue);
	// 		}
	// 	}else{
	// 		for(var i=0; i<noOfRows; i++){
	// 			var columnID = "a"+i;

	// 			// alert(editRowId+ " == " +columnID);
	// 			if(editRowId != columnID){
	// 				var fieldvalue=$("#" + columnID + " td " + "#" + splitString[1]).val();
	// 				if(fieldvalue != undefined && fieldvalue != ''){
	// 					arrayvalue.push(fieldvalue);
	// 				}else if(fieldvalue == ''){
	// 					return;
	// 				}
	// 			}

	// 			if(editRowId == columnID){
	// 				var fieldvalue1=$("#" + fieldId).val();
	// 				if(fieldvalue1 != undefined && fieldvalue1 != ''){
	// 					arrayvalue.push(fieldvalue1);
	// 				}else if(fieldvalue1 == ''){
	// 					return;
	// 				}
	// 			}		
				
	// 		}
	// 	}

	// 	var calculation;
	// 	// if(type == 'dynamic'){
	// 	// 	arrayvalue.push(footerValue);
	// 	// }

	// 	if(calcuType == 1){

	// 		calculation = eval(arrayvalue.join('+'));

	// 	}else if(calcuType == 2){
	// 		if(type == 'dynamic'){
	// 			calculation = eval(arrayvalue.join('+'));
	// 			$("#" + footerID).val(calculation);

	// 			if(currentrowno == noofrows-1){
	// 				footerValue = $("#"+footerID).val();
	// 				calculation = footerValue / noofrows;
	// 			}
	// 		}else{
	// 			var average = eval(arrayvalue.join('+'));
	// 			calculation = average / arrayvalue.length;
	// 		}
	// 	}else if(calcuType == 3){
	// 		if(type == 'dynamic'){
	// 			calculation = noofrows;
	// 		}else{
	// 			calculation = arrayvalue.length;
	// 		}
	// 	}

	// 	$("#" + footerID).val(calculation);
	// }

	

	// function CreateOnkeyupScriptGrid(thisElement,type,insertType){
		
	// 	var total = 1;
	// 	var flag = 0;
	// 	if(type == 'grid'){
	// 		if(insertType == 'dynamic'){
	// 			var thiss = $("#" + thisElement + "-onkeyup-hidden").val();
	// 			var splitFieldName = thisElement.split('kreon-grid-');
	// 			var gridSerial = splitFieldName[1].charAt(0);
	// 		}else{
				
	// 			var thiss = $("#" + thisElement.name.slice(0,-2) + "-onkeyup-hidden").val();
	// 			var splitFieldName = thisElement.id.split('kreon-grid-');
	// 			var gridSerial = splitFieldName[1].charAt(0);
	// 		}
	// 	}else{
	// 		var thiss = $("#" + thisElement.name + "-onkeyup-hidden").val();
	// 	}

	// 	var string = thiss.substring(
	// 		thiss.indexOf("{") + 1, 
	// 		thiss.lastIndexOf("}") 
	// 	);
	// 	// console.log(string);
	// 	// string = string.replace('if','').replace('elseif','');
	// 	// string = string.split(/[()]/);
	// 	var string1 = string.split(",");
		

	// 	for(var k=0; k<string1.length; k++){
	// 		string = string1[k].split(/(?=.*if)(?=elseif)(?=else)/);
			
	// 		if(string.length > 1){
	// 			for(var i=0; i<string.length; i++){
	// 				if(string[1].length > 1){
	// 					console.log("befor "+string[i]);
	// 					splitString = string[i].split(/[()]/);
	// 					console.log("after "+splitString);
	// 					conditionParams = splitString[1].split("=");
	// 					conditionParamsValue = $("#kreon-grid-"+gridSerial+""+conditionParams[0]).next('span').find('#select2-kreon-grid-aunitid-container').text();
						
	// 					if(conditionParamsValue.trim() == conditionParams[1].trim()){
	// 						var splitStringExtract = splitString[2].replace('{','').replace('}','');
	// 						var calculationParams = splitStringExtract.split('=');					
	// 						flag=1;
	// 					}
	// 				}
	// 			}
				
	// 		}else if(string.length == 1){
	// 			var calculationParams = string[0].split('=');
	// 			flag=1;
	// 		}
			
	// 		if(flag){
				
	// 			var str = calculationParams[1].replace(/\s/g, ""); // Remove Space from string
	// 			var string = str.split(/([+,\-,*,(,),/])/); //Split string into array
	// 			var calcuParams = string.filter(function(e){return e});
	// 			var arrayWithOperator = []; // Get field value using 
	// 			var arrayWithValue = ''; 
	// 			var decimal=  /^-?\d*\.?\d*$/;
				
	// 			for (let i = 0; i < calcuParams.length; i++) {
	// 				if (calcuParams[i] !== '+' && calcuParams[i] !== '-' && calcuParams[i] !== '*' && calcuParams[i] !== '/' && calcuParams[i] !== '(' && calcuParams[i] !== ')' && !calcuParams[i].match(decimal) ) {
						
	// 					if(type == 'grid'){
	// 						var fieldValue = $("#kreon-grid-"+gridSerial+""+ calcuParams[i].trim()).val();
	// 						if(fieldValue == ''){
	// 							return;
	// 						}
	// 					}else{
	// 						var fieldValue = $("#"+calcuParams[i].trim()).val();
	// 						// console.log(fieldValue);
	// 						if(fieldValue == ''){
	// 							return;
	// 						}
	// 					}
	// 					arrayWithOperator.push(fieldValue);	
	// 				}else{
	// 					arrayWithOperator.push(calcuParams[i].trim());
	// 				}	
	// 			}
			
	// 			arrayWithValue = arrayWithOperator.join("");
	// 			if(arrayWithValue != undefined){
	// 				// console.log(arrayWithValue);
	// 				// console.log("------------------------------");
	// 				total = eval(arrayWithValue); 
	// 			}

	// 			if(!isNaN(total)){
	// 				if(type == 'grid'){
	// 					$("#kreon-grid-"+gridSerial+""+ calculationParams[0].trim()).val(total);
	// 					// return;
	// 				}else{
	// 					$("#"+calculationParams[0].trim()).val(total);
	// 					// return;
	// 				}

	// 			}
	// 		}
	// 	}
	// }

	// Function to handle the isnull() expression dynamically
	function handleIsNullInString(str, gridSerial) {
		// Regular expression to find isnull() and capture the content inside
		let isNullPattern = /isnull\(([^,]+),\s*([^)]*)\)/g;  // Matches isnull(disc, 0.0)

		// Replace the isnull() expression dynamically
		return str.replace(isNullPattern, (match, fieldName, defaultValue) => {
			var fieldValue = $("#kreon-grid-"+gridSerial+""+ fieldName).val();
			console.log(fieldName,fieldValue);
			// Dynamically evaluate the expression for disc (e.g., variable)
			let discValueUsed = eval(fieldValue) === null ? parseFloat(defaultValue) : eval(fieldValue);

			// Return the replacement expression
			return discValueUsed;
		});
	}

	function CreateOnkeyupScriptGrid(thisElement,type,insertType){
		// alert(type);
		console.log("==>>CreateOnkeyupScriptGrid");
		var total = 1;
		var flag = 0;
		if(type == 'grid'){
			if(insertType == 'dynamic'){
				var thiss = $("#" + thisElement + "-onkeyup-hidden").val();
				var splitFieldName = thisElement.split('kreon-grid-');
				var gridSerial = splitFieldName[1].charAt(0);
			}else{
				
				var thiss = $("#" + thisElement.name.slice(0,-2) + "-onkeyup-hidden").val();
				var splitFieldName = thisElement.id.split('kreon-grid-');
				var gridSerial = splitFieldName[1].charAt(0);
			}
		}else{
			var thiss = $("#" + thisElement.name + "-onkeyup-hidden").val();
		}

		// Step 1: Replace commas inside parentheses with a placeholder (e.g., ~)
		let modifiedString = thiss.replace(/\(([^)]+)\)/g, (match) => {
			return match.replace(/,/g, "~");
		});

		var string = modifiedString.substring(
			modifiedString.indexOf("{") + 1, 
			modifiedString.lastIndexOf("}") 
		);

		

		// var string = thiss.substring(
		// 	thiss.indexOf("{") + 1, 
		// 	thiss.lastIndexOf("}") 
		// );
		// string = string.replace('if','').replace('elseif','');
		// string = string.split(/[()]/);
		var string1 = string.split(",");
		console.log("string1 "+string1);

		// Step 3: Restore the original commas inside parentheses
		string1 = string1.map(item => item.replace(/~/g, ","));

		for(var k=0; k<string1.length; k++){
			string = string1[k].split(/(?=.*if)(?=elseif)(?=else)/);
			console.log(string);
			if(string.length > 1){
				for(var i=0; i<string.length; i++){
					if(string[1].length > 1){
						console.log(string[i]);
						// splitString = string[i].split(/[()]/);

						const regex = /(\w+)\(([^)]*)\)\s*\{([^}]*)\}/;
						var match = string[i].match(regex);
						var conditionalString = '';
						splitString = match ? [match[1], `${match[2]}`, `${match[3]}`] : null;

						console.log("splitString "+splitString);

						conditionParamsArray = splitString[1].split("&");
						console.log("conditionParamsArray "+conditionParamsArray);
						for(var l=0; l<conditionParamsArray.length; l++){
							conditionParams = conditionParamsArray[l].split("=");
							console.log("conditionParams "+conditionParams[1]);
							conditionParamsValue = $("#kreon-grid-"+gridSerial+""+conditionParams[0].trim()).next('span').find('#select2-kreon-grid-'+gridSerial+""+conditionParams[0].trim()+'-container').text();
							
							conditionalString += '\'' + conditionParamsValue.trim() + '\' == \'' + conditionParams[1].trim() + '\'';

							if(l != conditionParamsArray.length-1){
								conditionalString += " && ";
							}
						}

						console.log("conditionalString "+conditionalString);
						
						if (eval(conditionalString)) {
							var splitStringExtract = splitString[2].replace('{','').replace('}','');
							var calculationParams = splitStringExtract.split('=');				
							flag=1;
						}

						// conditionParams = splitString[1].split("=");
						// conditionParamsValue = $("#kreon-grid-"+gridSerial+""+conditionParams[0]).next('span').find('#select2-kreon-grid-'+gridSerial+""+conditionParams[0]+'-container').text();
						
						// if(conditionParamsValue.trim() == conditionParams[1].trim()){
						// 	var splitStringExtract = splitString[2].replace('{','').replace('}','');
						// 	var calculationParams = splitStringExtract.split('=');				

						// 	flag=1;
						// }
					}
				}
				
			}else if(string.length == 1){
				var calculationParams = string[0].split('=');
				flag=1;
			}
			
			if(flag){
				console.log(calculationParams[1]);
				var str = calculationParams[1].replace(/\s/g, ""); // Remove Space from string
				// console.log("str");
				// console.log(str);
				// Check if the string contains 'isnull'
				var isNullPattern = /isnull\(([^,]+),\s*([^)]*)\)/;  // Matches isnull(disc, 0.0)
				var match = str.match(isNullPattern);
				// console.log(match);
				if (match) {
					// Apply the dynamic replacement in the formula string
					str = handleIsNullInString(str,gridSerial);
				}
				var string = str.split(/([+,\-,*,(,),/])/); //Split string into array
				var calcuParams = string.filter(function(e){return e});
				var arrayWithOperator = []; // Get field value using 
				var arrayWithValue = ''; 
				var decimal=  /^-?\d*\.?\d*$/;
				// console.log("calcuParams",calcuParams);
				for (let i = 0; i < calcuParams.length; i++) {
					if (calcuParams[i] !== '+' && calcuParams[i] !== '-' && calcuParams[i] !== '*' && calcuParams[i] !== '/' && calcuParams[i] !== '(' && calcuParams[i] !== ')' && !calcuParams[i].match(decimal) ) {
						
						if(type == 'grid'){
							// console.log("#kreon-grid-"+gridSerial+""+ calcuParams[i].trim(), $("#kreon-grid-"+gridSerial+""+ calcuParams[i].trim()).val());
							var fieldValue = $("#kreon-grid-"+gridSerial+""+ calcuParams[i].trim()).val();
							if(fieldValue == ''){
								return;
							}else if(fieldValue == undefined){
								alert(calcuParams[i].trim()+" not found");
								return;
							}
						}else{
							var fieldValue = $("#"+calcuParams[i].trim()).val();
							// console.log(fieldValue);
							if(fieldValue == ''){
								return;
							}
						}
						arrayWithOperator.push(fieldValue);	
					}else{
						arrayWithOperator.push(calcuParams[i].trim());
					}	
				}
			
				arrayWithValue = arrayWithOperator.join(" ");
				// console.log("------------------------------");
				// console.log(arrayWithValue);
				if(arrayWithValue != undefined){
					console.log("arrayWithValue", arrayWithValue, eval(arrayWithValue));
					total = eval(arrayWithValue); 
				}

				if(!isNaN(total)){
					if(type == 'grid'){
						var decimalPlaces = $("#kreon-grid-"+gridSerial+""+ calculationParams[0].trim()).data('key');
						if(decimalPlaces == undefined){
							var roundedTotal = total;
						}else{
							var roundedTotal = Math.round(total * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces);
						}
						// console.log(roundedTotal);
						// $("#kreon-grid-"+gridSerial+""+ calculationParams[0].trim()).val(Math.round(total * 100) / 100);
						$("#kreon-grid-"+gridSerial+""+ calculationParams[0].trim()).val(roundedTotal);
					}else{
						var decimalPlaces = $("#"+calculationParams[0].trim()).data('key');
						if(decimalPlaces == undefined){
							var roundedTotal = total;
						}else{
							var roundedTotal = Math.round(total * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces);
						}
						// $("#"+calculationParams[0].trim()).val(Math.round(total * 100) / 100);
						$("#"+calculationParams[0].trim()).val(roundedTotal);
					}
				}
			}
		}
	}

	//In grid  Onchage dropdown fill value in other input
	function CreateOnchangeScriptGrid(thisElement){
		selectedValue = thisElement.value;
		//elementId = thisElement.name.slice(0,-2);
		var thiss = $("#" + thisElement.name.slice(0,-2) + "-hidden").val();
		// function CreateOnchangeScript(thiss,elementId,type){
		var gridIdentifier = $(thisElement).parent().closest('tr').attr('id');
		var elementId = "#" + gridIdentifier + " td #" + thisElement.name.slice(0,-2);
		var type = 5;
		var str1 = "response";
		var responsePos = -1;
		if(thiss.indexOf(str1) != -1){
			responsePos = thiss.indexOf(str1);
		}

		var str2 = "url";
		var urlPos = -1;
		if(thiss.indexOf(str2) != -1){
			urlPos = thiss.indexOf(str2);
		}

		var newArr = ["", ""];
		if(urlPos != -1 && responsePos != -1){ //to split between both the parameters

			var splitIndex = urlPos > responsePos ? urlPos : responsePos;
 			newArr[0] = thiss.substring(0, splitIndex - 1)
 			newArr[1] = thiss.substring(splitIndex);
			// console.log(newArr[0]);
			// console.log(newArr[1]);
			var responseParams = newArr[0].substring(
						newArr[0].indexOf("{") + 1, 
						newArr[0].lastIndexOf("}")
					);

			var urlParams = newArr[1].substring(
						newArr[1].indexOf("{") + 1, 
						newArr[1].lastIndexOf("}")
					);

			console.log(responseParams);	//#Broker : 5 | response.ID | response.Name
			console.log(urlParams);
			var responseP = responseParams.split(',');	//THIS will give all the parameters required in response
			var finalParams = [];

			for(var i = 0; i < responseP.length; i++){
				finalParams[i] = responseP[i].trim().split('|');
			}
		}

		//loggedin user id, company id, year code, division, auth key for api

		$.ajax({
			type : 'POST',
			dataType: 'json',
			data : { selectedValue : selectedValue },
			url : urlParams,
			success : function(result){
				//CHECK TYPE OF PARAMETERS
				// console.log(result);
				for(var i = 0; i < finalParams.length; i++){
					if(finalParams[i][1].trim() == 0 || finalParams[i][1].trim() == 1 || finalParams[i][1].trim() == 3 || finalParams[i][1].trim() == 6 || finalParams[i][1].trim() == 7 || finalParams[i][1].trim() == 8 ){	//type = textbox & others //#TrspPay | 1 | TrspPay 
						console.log(result[finalParams[i][2]]);
						$("#" + gridIdentifier + " td " + finalParams[i][0].trim()).val(result[finalParams[i][2].trim()])
					}
				}				
			}
		});
	}

	function CreateOnchangeScript(thisElement,type){
		elementId = thisElement.name;
		var thiss = $("#" + elementId + "-hidden").val();
		
		
		var str1 = "response";
		var responsePos = -1;
		if(thiss.indexOf(str1) != -1){
			responsePos = thiss.indexOf(str1);
		}

		var str2 = "url";
		var urlPos = -1;
		if(thiss.indexOf(str2) != -1){
			urlPos = thiss.indexOf(str2);
		}

		var newArr = ["", ""];
		if(urlPos != -1 && responsePos != -1){ //to split between both the parameters
			var splitIndex = urlPos > responsePos ? urlPos : responsePos;
 			newArr[0] = thiss.substring(0, splitIndex - 1)
 			newArr[1] = thiss.substring(splitIndex);
			console.log(newArr[0]);
			console.log(newArr[1]);
			var responseParams = newArr[0].substring(
						newArr[0].indexOf("{") + 1, 
						newArr[0].lastIndexOf("}")
					);

			var urlParams = newArr[1].substring(
						newArr[1].indexOf("{") + 1, 
						newArr[1].lastIndexOf("}")
					);
			// console.log(responseParams);	//#Broker : 5 | response.ID | response.Name
			// console.log(urlParams);
			var responseP = responseParams.split(',');	//THIS will give all the parameters required in response
			var finalParams = [];
			for(var i = 0; i < responseP.length; i++){
				finalParams[i] = responseP[i].trim().split('|');
			}
		}
		// alert(elementId);
		if(type == 4){
			var selectedValue = $('input:radio[name="'+ elementId+'"]:checked').val();
		}else{
			var selectedValue = $("#" + elementId).val();
		}
		// alert(selectedValue);
		//loggedin user id, company id, year code, division, auth key for api

		$.ajax({
			type : 'POST',
			dataType: 'json',
			data : { selectedValue : selectedValue },
			url : urlParams,
			success : function(result){
				// alert(result);
				//CHECK TYPE OF PARAMETERS
				for(var i = 0; i < finalParams.length; i++){
					console.log(finalParams);
					if(finalParams[i][1].trim() == 0 || finalParams[i][1].trim() == 1 || finalParams[i][1].trim() == 3 || finalParams[i][1].trim() == 6 || finalParams[i][1].trim() == 7 || finalParams[i][1].trim() == 8 ){	//type = textbox & others //#TrspPay | 1 | TrspPay 
						$(finalParams[i][0].trim()).val(result[finalParams[i][2].trim()])
					}

					if(finalParams[i][1].trim() == 5){	//type = dropdown //#Broker | 5 | BrokerName|ID | Name
						resultArr = result[finalParams[i][2].trim()];
						var selectDD = "";
						for(var j = 0; j < resultArr.length; j++){
							var ddID = finalParams[i][3].trim();
							var ddVal = finalParams[i][4].trim();
							if( i == 0 ){
								selectDD += "<option selected value='" + resultArr[j][ddID] + "'>" + resultArr[j][ddVal] + "</option>"
							}else{
								selectDD += "<option value='" + resultArr[j][ddID] + "'>" + resultArr[j][ddVal] + "</option>"					
							}
							$(finalParams[i][0].trim()).html(selectDD);
						}
					}

					if(finalParams[i][1].trim() == 10){	//type = dropdown //#Broker | 5 | BrokerName|ID | Name
						resultArr = result[finalParams[i][2].trim()];
						var selectDD = "";
						for(var j = 0; j < resultArr.length; j++){
							var ddID = finalParams[i][3].trim();
							var ddVal = finalParams[i][4].trim();
							$(finalParams[i][0].trim()).select2('data', {id: ddID, text: ddVal});      
						}
					}

					if(finalParams[i][1].trim() == 4){	//type = radio button //#Broker | 4 | BrokerName|ID
						resultArr = result[finalParams[i][2].trim()];
						var selectRB = "";
						for(var j = 0; j < resultArr.length; j++){
							var ddID = finalParams[i][3].trim();
							var ddVal = finalParams[i][4].trim();
							selectRB += "<label><input type='radio' name='"+ finalParams[i][2] +"' value='"+ resultArr[j][ddID] +"' >&nbsp;"+ resultArr[j][ddVal]+"</label></br>";
						}
						$(finalParams[i][0].trim()+ "-div").html(selectRB);
					}

					if(finalParams[i][1].trim() == 9){	//type = checkbox //#Broker | 9 | BrokerName|ID
						resultArr = result[finalParams[i][2].trim()];
						var selectCB = "";
						for(var j = 0; j < resultArr.length; j++){
							var ddID = finalParams[i][3].trim();
							var ddVal = finalParams[i][4].trim();
							selectCB += "<label> <input type='checkbox' name='"+ finalParams[i][2].trim() +"[]' id='"+finalParams[i][2].trim()+"' value='"+ resultArr[j][ddID] +"' >&nbsp;"+ resultArr[j][ddVal]+"</label></br>";
						}
						$(finalParams[i][0].trim() + "-multi-div").html(selectCB);
					}
				}				
			}
		});
	}

	// //In main form Onkeyenter fill value in other input
	// function CreateOnkeyEnterScript(thisElement,type){
	// 	elementId = thisElement.id;
	// 	var thiss = $("#" + elementId + "-hidden").val();

	// 	var splitString = elementId.split('kreon-grid-');
	// 	var gridIdentifier = splitString[1].charAt(0);

	// 	var str1 = "response";
	// 	var responsePos = -1;
	// 	if(thiss.indexOf(str1) != -1){
	// 		responsePos = thiss.indexOf(str1);
	// 	}

	// 	var str2 = "DB";
	// 	var DBPos = -1;
	// 	if(thiss.indexOf(str2) != -1){
	// 		DBPos = thiss.indexOf(str2);
	// 	}

	// 	var newArr = ["", ""];
	// 	if(DBPos != -1 && responsePos != -1){ //to split between both the parameters
	// 		var splitIndex = DBPos > responsePos ? DBPos : responsePos;
 	// 		newArr[0] = thiss.substring(0, splitIndex - 1)
 	// 		newArr[1] = thiss.substring(splitIndex);

	// 		var str3 = "DB";
	// 		var DBPos1 = -1;
	// 		if(newArr[1].indexOf(str3) != -1){
	// 			DBPos1 = newArr[1].indexOf(str3);
	// 		}

	// 		var str4 = "cursorPosition";
	// 		var cursorPositionPos = -1;
	// 		if(newArr[1].indexOf(str4) != -1){
	// 			cursorPositionPos = newArr[1].indexOf(str4);
	// 		}

	// 		var str5 = 'parameters';
	// 		var parametersPos = -1;
	// 		if(newArr[1].indexOf(str5) != -1){
	// 			parametersPos = newArr[1].indexOf(str5);
	// 		}

	// 		if(DBPos1 != -1){
	// 			if(cursorPositionPos != -1){
	// 				newArr[2] = newArr[1].substring(0,cursorPositionPos);
	// 				if(parametersPos != -1){
	// 					newArr[3] = newArr[1].substring(cursorPositionPos,parametersPos);
	// 					newArr[4] = newArr[1].substring(parametersPos);
	// 				}else{
	// 					newArr[3] = newArr[1].substring(cursorPositionPos);
	// 				}
	// 			}else{
	// 				if(parametersPos != -1){
	// 					newArr[2] = newArr[1].substring(0,parametersPos);
	// 					newArr[4] = newArr[1].substring(parametersPos);
	// 				}else{
	// 					newArr[2] = newArr[1].substring(DBPos1);
	// 				}

	// 			}
	// 		}

	// 		var responseParams = newArr[0].substring(
	// 					newArr[0].indexOf("{") + 1, 
	// 					newArr[0].lastIndexOf("}")
	// 				);

	// 		var DBParams = newArr[2].substring(
	// 					newArr[2].indexOf("{") + 1, 
	// 					newArr[2].lastIndexOf("}")
	// 				);

	// 		if(newArr[3] != undefined){
	// 			var cursorPositionParams = newArr[3].substring(
	// 				newArr[3].indexOf('{') + 1, 
	// 				newArr[3].lastIndexOf('}')
	// 			);
	// 		}	
			
	// 		if(newArr[4] != undefined){
	// 			var parametersParams = newArr[4].substring(
	// 				newArr[4].indexOf('{') + 1, 
	// 				newArr[4].lastIndexOf('}')
	// 			);
	// 		}
			
			
	// 		var responseP = responseParams.split(',');	//THIS will give all the parameters required in response
	// 		var finalParams = [];
	// 		var searchColumn = [];

	// 		for(var i = 0; i < responseP.length; i++){
	// 			finalParams[i] = responseP[i].trim().split('|');
	// 			searchColumn.push(finalParams[i][2].trim());
	// 		}
	// 	}
	// 	if(type == 4){
	// 		var selectedValue = $('input:radio[name="'+ elementId+'"]:checked').val();
	// 	}else{
	// 		var selectedValue = $("#" + elementId).val();
	// 	}

	// 	if(parametersParams.length > 5){
	// 		var extractedparameters = parametersParams.replace(/\(+/g, ' (').replace(/\?+/g, ' ?').replace(/\#+/g, ' #').replace(/\)+/g, ' )');
	// 		var parameters = extractedparameters.split(" ");
			
	// 		var requestParams = '';
	// 		for(var i=0; i<parameters.length; i++){
			
	// 			if(parameters[i].indexOf("?") >= 0 || parameters[i].indexOf("#") >= 0){
	// 				if(parameters[i].length > 0){

	// 					if(parameters[i].includes("?") == true){
						
	// 						var qExtractedString = "kreon-grid-"+parameters[i].replace(/\?+/g, '');
	// 					}
						
	// 					if(parameters[i].indexOf("#") >= 0){
	// 						//If # found it remove
	// 						var qExtractedString = parameters[i].replace(/\#+/g, '');
	// 					}
						
	// 					if(qExtractedString.length > 0){
	// 						var NumberOfElements = document.querySelectorAll("[id="+qExtractedString+"]");
							
							
	// 						if(NumberOfElements.length == 2){
	// 							requestParams += $(NumberOfElements[1]).val() !="" ? $(NumberOfElements[1]).val().trim() : '0';
	// 							requestParams += " ";
	// 						}else{
	// 							if(qExtractedString == 'user_id'){
	// 								requestParams += <?php echo $_SESSION['user_id']; ?> 
	// 							}else{
	// 								requestParams += $('#'+qExtractedString).val() !="" ? $('#'+qExtractedString).val() : '0';
	// 							}
	// 							requestParams += " ";
	// 						}
							
	// 					}

	// 				}
	// 			}else{
	// 				requestParams += parameters[i].trim() +" ";
	// 			}
	// 		}
	// 	}
	// 	// console.log(requestParams);
	// 	// var Parameters = [];
	// 	// var IParameters = [];
	// 	// var str = [];
	// 	// if(parameters != undefined && parametersParams != ""){
	// 	// 	var finalParametersParams = parametersParams.split(',');
	// 	// 	for(var i=0; i < finalParametersParams.length; i++){
	// 	// 		IParameters = finalParametersParams[i].split(':');
	// 	// 		str[i] = IParameters[1];
	// 	// 		if((str[i][1] == "'" && str[i][str[i].length - 1] == "'") || (str[i][1] == "\"" && str[i][str[i].length - 1] == "\"")){
	// 	// 			var parameterValue = IParameters[1].replace(/[\"']/g, '');
	// 	// 		}else{
	// 	// 			var parameterValue =  $('#'+IParameters[1].trim()).val();
	// 	// 		}
	// 	// 		// var parameterValue = IParameters[1];

	// 	// 		var paramkv = IParameters[0].trim()+"="+parameterValue;
	// 	// 		Parameters.push(paramkv);
	// 	// 	}       
	// 	// }

	// 	//loggedin user id, company id, year code, division, auth key for api

	// 	$.ajax({
	// 		type : 'POST',
	// 		dataType: 'json',
	// 		data : { searchParam : selectedValue, searchColumn:searchColumn, fromRepo : DBParams, parameters : requestParams },
	// 		url : 'getOnkeyEnterData.php',
	// 		success : function(result){
	// 			console.log(result);
	// 			//CHECK TYPE OF PARAMETERS
	// 			for(var i = 0; i < finalParams.length; i++){
	// 				// console.log(finalParams);
	// 				if(finalParams[i][1].trim() == 0 || finalParams[i][1].trim() == 1 || finalParams[i][1].trim() == 3 || finalParams[i][1].trim() == 6 || finalParams[i][1].trim() == 7 || finalParams[i][1].trim() == 8 ){	//type = textbox & others //#TrspPay | 1 | TrspPay 
	// 					console.log(finalParams[i][0] +" - "+result[finalParams[i][2].trim()]);
	// 					$(finalParams[i][0].trim()).val(result[finalParams[i][2].trim()])
	// 				}

	// 				if(finalParams[i][1].trim() == 5){	//type = dropdown //#Broker | 5 | BrokerName|ID | Name
	// 					resultArr = result[finalParams[i][2].trim()];
	// 					var selectDD = "";
	// 					for(var j = 0; j < resultArr.length; j++){
	// 						var ddID = finalParams[i][3].trim();
	// 						var ddVal = finalParams[i][4].trim();
	// 						if( i == 0 ){
	// 							selectDD += "<option selected value='" + resultArr[j][ddID] + "'>" + resultArr[j][ddVal] + "</option>"
	// 						}else{
	// 							selectDD += "<option value='" + resultArr[j][ddID] + "'>" + resultArr[j][ddVal] + "</option>"					
	// 						}
	// 						$(finalParams[i][0].trim()).html(selectDD);
	// 					}
	// 				}

	// 				if(finalParams[i][1].trim() == 10){	//type = dropdown //#Broker | 5 | BrokerName|ID | Name
	// 					resultArr = result[finalParams[i][2].trim()];
	// 					var selectDD = "";
	// 					for(var j = 0; j < resultArr.length; j++){
	// 						var ddID = finalParams[i][3].trim();
	// 						var ddVal = finalParams[i][4].trim();
	// 						$(finalParams[i][0].trim()).select2('data', {id: ddID, text: ddVal});      
	// 					}
	// 				}

	// 				if(finalParams[i][1].trim() == 4){	//type = radio button //#Broker | 4 | BrokerName|ID
	// 					resultArr = result[finalParams[i][2].trim()];
	// 					var selectRB = "";
	// 					for(var j = 0; j < resultArr.length; j++){
	// 						var ddID = finalParams[i][3].trim();
	// 						var ddVal = finalParams[i][4].trim();
	// 						selectRB += "<label><input type='radio' name='"+ finalParams[i][2] +"' value='"+ resultArr[j][ddID] +"' >&nbsp;"+ resultArr[j][ddVal]+"</label></br>";
	// 					}
	// 					$(finalParams[i][0].trim()+ "-div").html(selectRB);
	// 				}

	// 				if(finalParams[i][1].trim() == 9){	//type = checkbox //#Broker | 9 | BrokerName|ID
	// 					resultArr = result[finalParams[i][2].trim()];
	// 					var selectCB = "";
	// 					for(var j = 0; j < resultArr.length; j++){
	// 						var ddID = finalParams[i][3].trim();
	// 						var ddVal = finalParams[i][4].trim();
	// 						selectCB += "<label> <input type='checkbox' name='"+ finalParams[i][2].trim() +"[]' id='"+finalParams[i][2].trim()+"' value='"+ resultArr[j][ddID] +"' >&nbsp;"+ resultArr[j][ddVal]+"</label></br>";
	// 					}
	// 					$(finalParams[i][0].trim() + "-multi-div").html(selectCB);
	// 				}
	// 			}	
				
	// 			if(cursorPositionParams !="" && cursorPositionParams != undefined){
	// 				$('#'+cursorPositionParams).focus();
	// 			}else{
	// 				$(".add"+gridIdentifier).click();
	// 			}
				
	// 		}
	// 	});
	// }

	//In main form Onkeyenter fill value in other input
	function CreateOnkeyEnterScript(thisElement,type){
		console.log("CreateOnkeyEnterScript");
		elementId = thisElement.id;
		var thiss = $("#" + elementId + "-hidden").val();

		var splitString = elementId.split('kreon-grid-');
		var gridIdentifier = splitString[1].charAt(0);

		var str1 = "response";
		var responsePos = -1;
		if(thiss.indexOf(str1) != -1){
			responsePos = thiss.indexOf(str1);
		}

		var str2 = "DB";
		var DBPos = -1;
		if(thiss.indexOf(str2) != -1){
			DBPos = thiss.indexOf(str2);
		}

		var newArr = ["", ""];
		if(DBPos != -1 && responsePos != -1){ //to split between both the parameters
			var splitIndex = DBPos > responsePos ? DBPos : responsePos;
 			newArr[0] = thiss.substring(0, splitIndex - 1)
 			newArr[1] = thiss.substring(splitIndex);

			var str3 = "DB";
			var DBPos1 = -1;
			if(newArr[1].indexOf(str3) != -1){
				DBPos1 = newArr[1].indexOf(str3);
			}

			var str4 = "cursorPosition";
			var cursorPositionPos = -1;
			if(newArr[1].indexOf(str4) != -1){
				cursorPositionPos = newArr[1].indexOf(str4);
			}

			var str5 = 'parameters';
			var parametersPos = -1;
			if(newArr[1].indexOf(str5) != -1){
				parametersPos = newArr[1].indexOf(str5);
			}

			var str6 = 'GridId';
			var GridIdPos = -1;
			if(newArr[1].indexOf(str6) != -1){
				GridIdPos = newArr[1].indexOf(str6);
			}

			var str7 = 'DataSpName';
			var DataSpNamePos = -1;
			if(newArr[1].indexOf(str7) != -1){
				DataSpNamePos = newArr[1].indexOf(str7);
			}

			if(DBPos1 != -1){
				if(DataSpNamePos != -1){
					if(cursorPositionPos != -1){
						if(parametersPos != -1){
							if(GridIdPos != -1){
								newArr[2] = newArr[1].substring(0,DataSpNamePos);
								newArr[6] = newArr[1].substring(DataSpNamePos,cursorPositionPos);
								newArr[3] = newArr[1].substring(cursorPositionPos,parametersPos);
								newArr[4] = newArr[1].substring(parametersPos,GridIdPos);
								newArr[5] = newArr[1].substring(GridIdPos);
							}
						}
					}
				}
			}

			// console.log(newArr[0]);
			// console.log(newArr[1]);
			// console.log(newArr[2]);
			// console.log(newArr[3]);
			// console.log(newArr[4]);
			// console.log(newArr[5]);
			// console.log(newArr[6]);


			var responseParams = newArr[0].substring(
						newArr[0].indexOf("{") + 1, 
						newArr[0].lastIndexOf("}")
					);

			var DBParams = newArr[2].substring(
						newArr[2].indexOf("{") + 1, 
						newArr[2].lastIndexOf("}")
					);

			if(newArr[3] != undefined || newArr[3].length > 1){
				var cursorPositionParams = newArr[3].substring(
					newArr[3].indexOf('{') + 1, 
					newArr[3].lastIndexOf('}')
				);
			}	
			
			if(newArr[4] != undefined || newArr[4].length > 1){
				var parametersParams = newArr[4].substring(
					newArr[4].indexOf('{') + 1, 
					newArr[4].lastIndexOf('}')
				);
			}

			if(newArr[5] != undefined || newArr[5].length > 1){
				var GridIdParams = newArr[5].substring(
					newArr[5].indexOf('{') + 1, 
					newArr[5].lastIndexOf('}')
				);

				if(GridIdParams.length > 0){
					var gridId = GridIdParams.split(":");
				}
			}

			if(newArr[6] != undefined || newArr[6].length > 1){
				var DataSpNameParams = newArr[6].substring(
					newArr[6].indexOf('{') + 1, 
					newArr[6].lastIndexOf('}')
				);
			}
			
			
			// var responseP = responseParams.split(',');	//THIS will give all the parameters required in response
			// var finalParams = [];
			// var searchColumn = [];

			// for(var i = 0; i < responseP.length; i++){
			// 	finalParams[i] = responseP[i].trim().split('|');
			// 	searchColumn.push(finalParams[i][2]);
			// }
		}
		// if(type == 4){
		// 	var selectedValue = $('input:radio[name="'+ elementId+'"]:checked').val();
		// }else{
		// 	var selectedValue = $("#" + elementId).val();
		// }

		if(parametersParams.length > 5){
			var extractedparameters = parametersParams.replace(/\(+/g, ' (').replace(/\?+/g, ' ?').replace(/\#+/g, ' #').replace(/\)+/g, ' )');
			var parameters = extractedparameters.split(" ");
			
			var requestParams = '';
			for(var i=0; i<parameters.length; i++){
			
				if(parameters[i].indexOf("?") >= 0 || parameters[i].indexOf("#") >= 0){
					if(parameters[i].length > 0){

						if(parameters[i].includes("?") == true){
							var gridIdentifier = $('#grid-id-'+gridId[1]).attr('class');		
							var gridSerialNo = gridIdentifier.slice(4);

							var qExtractedString = "kreon-grid-"+gridSerialNo+""+parameters[i].replace(/\?+/g, '');
						}
						
						if(parameters[i].indexOf("#") >= 0){
							//If # found it remove
							var qExtractedString = parameters[i].replace(/\#+/g, '');
						}
						
						if(qExtractedString.length > 0){
							var NumberOfElements = document.querySelectorAll("[id="+qExtractedString+"]");
							
							if(NumberOfElements.length == 2){
								requestParams += $(NumberOfElements[1]).val() !="" ? "'"+$(NumberOfElements[1]).val().trim()+"'" : '0';
								requestParams += " ";
							}else{
								if(qExtractedString == 'user_id'){
									requestParams += <?php echo $_SESSION['user_id']; ?> 
								}else{
									requestParams += $('#'+qExtractedString).val() !="" ? "'"+$('#'+qExtractedString).val().trim()+"'" : '0';
								}
								requestParams += " ";
							}	
						}
					}
				}else{
					requestParams += parameters[i].trim() +" ";
				}
			}
		}

		var hidden = $('form').find(':hidden');	
		hidden.show();   
		var  formSerialize = $('form').serialize();	
		hidden.hide();
		var editid = $('input[name=editID]').val();

		//loggedin user id, company id, year code, division, auth key for api
		$.ajax({
			type : 'POST',
			dataType: 'json',
			data : { responseParams:responseParams, fromRepo : DBParams, fromRepoSP : DataSpNameParams, parameters : requestParams, GridId: gridId[1], FormData: formSerialize, editid:editid},
			url : 'getOnkeyEnterData.php',
			success : function(result){
				console.log("getOnkeyEnterData", result);
				finalResponseParams = result['finalParams'];
				var isBarcode = 0;
				// if(result['Msg']){
				// 	if(result['Msg'][0]['Msg'].length > 0){
				// 		alert(result['Msg'][0]['Msg']);
				// 	}
				// }

				isBarcode = 1;

				if(result['Success'] == 0){
					if(result['Msg'][0]['Msg'].length > 0){
						alert(result['Msg'][0]['Msg']);
						return;
					}
				}else if(result['Success'] == 2){
					if (confirm(result['data']['Msg']+'. Are you sure you want to show barcode details?') == true) {
						isBarcode = 1;
					}else{
						if(result['Msg'][0]['Msg'].length > 0){
							alert(result['Msg'][0]['Msg']);
							return;
						}
					}
				}
					
				//if isBarcode is 1 then exicute response
				if(isBarcode == 1){
					//CHECK TYPE OF PARAMETERS
					for(var i = 0; i < finalResponseParams.length; i++){
						responseFinalParams = finalResponseParams[i].split(":");
						if(responseFinalParams[2] == "0" || responseFinalParams[2] == "1" || responseFinalParams[2] == "3" || responseFinalParams[2] == "6" || responseFinalParams[2] == "7" || responseFinalParams[2] == "8" ){
							console.log(responseFinalParams);
							$("#kreon-grid-"+gridSerialNo+""+responseFinalParams[0].trim()).val(result['data'][0][responseFinalParams[1]]);
						}
	
						if(responseFinalParams[2] == "5" || responseFinalParams[2] == "19"){
							// console.log(result['data'][0][responseFinalParams[3]]);
							var $newOption = $("<option selected=\"selected\"></option>").val(result['data'][0][responseFinalParams[3]]).text(result['data'][0][responseFinalParams[1]]);
							$("#kreon-grid-"+gridSerialNo+""+responseFinalParams[0].trim()).select2('open');
							$("#kreon-grid-"+gridSerialNo+""+responseFinalParams[0].trim()).append($newOption).trigger("change");
							$("#kreon-grid-"+gridSerialNo+""+responseFinalParams[0].trim()).select2('close');
						}
	
						if(responseFinalParams[2] == '17'){
							$('#kreon-grid-' + gridSerialNo + responseFinalParams[0].trim() +'-text').val(result['data'][0][responseFinalParams[1]]);
							$('#kreon-grid-'+gridSerialNo + responseFinalParams[0].trim()).val(result['data'][0][responseFinalParams[3]]);
							
						}	
	
					}
	
					setTimeout(
					function() {
						if(result['bordercolor']['bordercolor'].length > 0){
							$('#panel'+gridSerialNo).find('#GridTable tbody tr').css('border','');
							var cnt = parseInt(document.getElementById("cnt"+gridSerialNo).value);
							var color = '#'+result['bordercolor']['bordercolor'];
							// console.log('#'+gridSerialNo+cnt);
							$('#'+gridSerialNo+cnt).css('border','3px solid'+ color);
						}
					}, 500);
					
					if(cursorPositionParams !="" && cursorPositionParams != undefined){
						console.log("CreateOnkeyEnterScript Fn")
						$('#kreon-grid-'+gridSerialNo+cursorPositionParams).focus();
					}else{
						$(".add"+gridSerialNo).click();
					}
				}
				
			}
		});
	}

	function isValidText(pattern,elementID) {
		var fieldValue = $("#" + elementID ).val();
		const regex = new RegExp(pattern);
		console.log(regex.test(fieldValue));
		if(regex.test(fieldValue) == true){
			alert("Not valid format");
			$( "."+elementID ).find('div').append( "<p id='"+elementID+"-hide' style='color:red;'>Not valid format.</p>" );
					setTimeout(function() {
						$("#"+elementID+"-hide").remove();
						$("#"+elementID).val('');
					}, 2000);
		}
	}

	var isNumeric = function(val, decimals) {
		var reString = "^\\s*((\\d+(\\.\\d{0," + decimals + "})?)|((\\d*(\\.\\d{1," + decimals + "}))))\\s*$"
		var re = new RegExp(reString);
		console.log(re);
	}

	function isValidDigit(digit,elementID) {
		var digits = digit.split("=");
		var decimaldigits = digits[1].split(".");
		var NoOfDigit = decimaldigits[1].length;
		var fieldValue = $("#" + elementID ).val();
		success = false;
		if(NoOfDigit == 2){
			regEx = /^\d*\.?\d{0,1}$/;
		}else if(NoOfDigit == 3){
			regEx = /^\d*\.?\d{0,2}$/;
		}else if(NoOfDigit == 4){
			regEx = /^\d*\.?\d{0,3}$/;
		}else if(NoOfDigit == 1){
			regEx = /^\d*\.?\d{0,0}$/;
		}
	
		if(regEx.test(fieldValue)) {
			success = true;
		}else {
			console.log(event.keyCode);
			if(event.keyCode != 8 && event.keyCode != 46 && event.keyCode != 9){
				event.preventDefault();
			}
			success = true;
		}
		return success;	
	}

	function onBlurUpdateLostFocusTabIndex(thiss,fieldtype){
		if(fieldtype == 17 || fieldtype == 16){
			
			if(thiss.id == undefined){
				$("#"+thiss).attr("tabindex", "-1");
				$("#"+thiss).css("pointer-events","none").css("background-color","#eee");
				
			}else{
				$("#"+thiss.id).attr("tabindex", "-1");
				$("#"+thiss.id).css("pointer-events","none").css("background-color","#eee");
			}	
		}else if(fieldtype == 5 || fieldtype == 19){
			$('#'+thiss.id).next('span').find('.select2-selection--single').css("pointer-events","none").css("background-color","#eee");
			setTimeout(
				function() {
					$('.' + thiss.id + ' .select2-selection--single').attr("tabindex","-1");
				}, 500);
		}else{
			$("#"+thiss.id).attr("tabindex", "-1");
			$("#"+thiss.id).prop("readonly", true);
		}
	}

</script>

<?php
	// Add More Form Modal
	echo '<div class="modal fade" id="AddMoreModal">
		<div class="modal-dialog modal-dialog-centered">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">×</button> 
					<h4 class="modal-title">Add</h4>                                                             
				</div> 
				<div class="modal-body" >
					<form id="modalForm" name="modal" role="form">
						<div id="ModelContent" >
						</div>
						<div class="modal-footer" style="margin-top:10px;">					
							<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
							<input type="submit" class="btn btn-success" id="submit">
						</div>
					</form>
				</div>   
			</div>                                                                       
		</div>                                          
	</div>';

	?>

	<!-- Log Modal -->
	<div class="modal fade" id="logModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
		<div class="modal-dialog modal-lg" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<h5 class="modal-title" id="exampleModalLabel">Log Data</h5>
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
				</div>
				<div class="modal-body">
					<table class="table" id="logTable"> <!-- id="dropdownContent" class="dropdown-content" -->
						<thead>
							<tr id="logTableHeader">
								<!-- Table headers will be inserted dynamically -->
							</tr>
						</thead>
						<tbody id="logItems">
							<!-- Data will be loaded here -->
						</tbody>
					</table>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
				</div>
			</div>
		</div>
	</div>

	<script>
		// When the button is clicked, open the modal and fetch data
		function openLogModal(type = ''){
			// alert(type);
			$('#logModal').modal('show');
			getLogData(type);  // Call AJAX to fetch data when modal opens
		}

		function getLogData(type){
			var columnFilters = $(".search-column").map(function() {
				const column = $(this).attr("id");
				const columnValue = $(this).val().toLowerCase();
				return { column, columnValue };
			}).get();

			// Create a custom string, for example: "column1:value1, column2:value2"
			var columnFiltersString = columnFilters
				.map(function(filter) {
					if (filter.columnValue) {
						return filter.column + ":" + filter.columnValue;
					}
				})
				.filter(function(item) {
					return item !== undefined;
				})
				.join("|");

			console.log("columnFiltersString "+columnFiltersString);
			if(type == 'full') $editID = 0;
			$.ajax({
				url: 'getLogData.php', // Replace with your server endpoint
				method: 'POST',
				dataType: 'json',
				data: { start: 0, limit: 30, FormID: <?php echo $FormID; ?>, ColumnFilters: columnFiltersString, EditID: <?php echo $editID; ?>},
				success: function(response) {
					if(response['data'].length <= 0){
						$("#logItems").html("<p>No Log Found..</p>");
					}
					responseHeaders = Object.keys(response['data'][0]);
					console.log(responseHeaders);
					// Load the table headers
					let logHeaderHtml = '';

					// Iterate over each key-value pair in the current object
					for (var i=0; i<responseHeaders.length; i++) {
						logHeaderHtml += `<th>
							${responseHeaders[i].charAt(0).toUpperCase() + responseHeaders[i].slice(1)}
							<input type="search" class="search-column form-control popup-search${i}"  id="${responseHeaders[i]}" placeholder="Search ${responseHeaders[i]}" data-column="${i}">
						</th>`
					}
	
					$("#logTableHeader").empty();  // Clear existing rows
					$("#logTableHeader").append(`${logHeaderHtml}`);
		
					var ddTbody = $("#logItems");
					var rowHtml;
					response['data'].forEach((row, rowIndex )=> {
						rowHtml = `<tr>`;
						responseHeaders.forEach((column, columnIndex)=> {
							const columnName = column;
							if (columnName in row) {
							
								if(row[columnName] && row[columnName].date){
									rowHtml += `<td>${row[columnName].date}</td>`;
								}else{
									rowHtml += `<td>${row[columnName]}</td>`;
								}
							
							}
						});
						rowHtml += `</tr>`;
						console.log("after");
						ddTbody.append(rowHtml);
					});
				},
				error: function(xhr, status, error) {
					console.error('Failed to load data:', error);
					callback(false); // Callback with false on error
				}
			});
		}

		function applyLogFilters() {
			// const globalValue = $("#globalSearch").val().toLowerCase();
			activeRowIndex=-1;
			startIndex = 0;
			var ddTbody = $("#logItems");
			ddTbody.empty();  // Clear existing rows
			getLogData();
		}

		$(document).on("keydown", function(e) {
			const visibleRows = $("#logItems tr:visible");

			if (!$("#logModal").is(":visible")) return;

			switch (e.key) {
				case "Enter":
					if (!$("#logModal").is(":visible")) {
						// Open the modal if it's not visible
						console.log("on enter");
						openLogModal();
					} else {
						var triggeringFieldId = $(e.target).attr('id');
						// alert("SearchFieldName"+SearchFieldName);
						// Apply the filter if the modal is visible
						applyLogFilters();
					}
				
			}
		});

	</script>


	<!-- Added code for field type 16 & 17 on date 01-01-2025 -->
	<div class="modal fade" id="myModal" data-backdrop="static" tabindex="-1" aria-labelledby="myModalLabel"  aria-hidden="true">
		<div class="modal-dialog modal-lg">
			<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title" id="myModalLabel">Select Items</h5>
				<button id="PopulateGridButton" class="btn btn-primary" >Populate Grid</button>
				<button type="button" class="btn-close" data-bs-dismiss="modal" data-dismiss="modal" aria-label="Close">X</button>
			</div>

			<div class="modal-body">
				<div id="dropdownContent" class="dropdown-content">
					<div class="col-md-12">
					<div id="ShowAdjustableAmountField" style="display:flex;gap:3px;margin-bottom:2px;margin-top:2px;display:none;">
							<div class='PopupAmount'>
								<label>Amount:</label>
								<input type="text" class="form-control" name="PopupAmount" id="PopupAmount" value="5000" readonly>
								<input type="hidden" class="form-control" name="PopupGridId" id="PopupGridId" value="">
							</div>

							<div class='PopupAdjamount'>
								<label>AdjAmount:</label>
								<input type="text" class="form-control" name="PopupAdjamount" id="PopupAdjamount" value="" readonly>
							</div>

							<div class='PopupBalance'>
								<label>Balance:</label>
								<input type="text" class="form-control" name="PopupBalance" id="PopupBalance" value="" readonly>
							</div>

							<div class='saveAdjAmtButton'>
								<label></label><br>
								<button style="display: none;" type="button" class="btn btn-danger" id="saveAdjAmtButton" onclick="saveAdjAmtData();">Save Adjustment Amount</button>
								<button style="display: none;" type="button" class="btn btn-default" id="saveAdjAmtCloseButton" data-dismiss="modal" aria-label="Close">Close</button>
							</div>
						</div>

						<div class="col-md-8">
							<div id="pillContainer"></div>
							<div id="pillContainerClear"><button type="button" class="btn" style="padding:1px 5px;" onclick="deselectAllRow();">Clear All Selected Row</button></div>
						</div>

						<div class="col-md-4">
							<input type="search" id="globalSearch" class="form-control popup-search" placeholder="Search All Columns">
						</div>
					</div>

					<div id="ValidationMessage" style="display:none;"></div>

					<input type="hidden" id="DropDownModalID" value="">
					
					<table class="table table-striped" id="ddTable">
						<thead>
							<tr id="tableHeader">
							<!-- Table headers will be inserted dynamically -->
							</tr>
						</thead>
						<tbody id="dropdownItems">
						<!-- Dynamically loaded rows will go here -->
						</tbody>
						<!-- <tfoot id="tableFooter">
					
						</tfoot> -->
					</table>
					<div class="loading" id="loadingData">Loading...</div>
					<div class="loading" id="loadingMessage">Loading more...</div>
				</div>
			</div>
			<div class="modal-footer" style="text-align:left;">
				<button id="clearSelection" class="btn btn-secondary" onclick="clearSelection()">Clear All</button>
			</div>
			</div>
		</div>
	</div>

	<script>
		var ddSelectedItemsValue = [];
		var ddSelectedItemsText = [];
		var startIndex = 0;
		var itemsPerPage = 30;  // Number of items to load per scroll
		var activeRowIndex = -1;
		var triggeringButtonTabIndex = null;
		var isSpacebarPressed = false;
		var triggeringButtonId = null;
		var ddDBFieldType = 0;
		var ddResponseData = [];
		var ddisGrid = 0;
		var GridID = 0; 
		var requestParams = '';
		var formReeponseString = '';
		var triggeringButtonSrNo = '';
		var ddRowSelectType = '';

		var ddResponseTotalColumnIndex = [];
		var responseHeaders = [];
		var ddResponsePopulateFields = '';
		var ddResponsePopulateFieldInGrids = '';
		var ddResponsePopulateGrid = '';
		var ddResponseSytemType = 0;
		

		async function toggleModal() {
			targetFieldId = $(event.target).attr('id');
			ddDBFieldType = $(event.target).data('type');

			if (targetFieldId.includes('kreon-grid')) {
				
				ddResponseSytemType = $('#'+targetFieldId).data('systemtype');
				targetFieldId = targetFieldId.split("-");
				triggeringButtonId = targetFieldId[2].substring(1);
				triggeringButtonSrNo = targetFieldId[2].charAt(0);
				ddisGrid = 1;
				GridID = $('#GridId'+targetFieldId[2].charAt(0)).val();

			}else{
				targetFieldId = targetFieldId.split("-");
				console.log("targetFieldId",targetFieldId);
				triggeringButtonId = targetFieldId[0]; 
				ddisGrid = 0;
				
				// Check if the value contains only digits 
				const isOnlyDigits = /^\d+$/.test(triggeringButtonId);
				if (isOnlyDigits == true) {
					GridID = triggeringButtonId;
					ddDBFieldType = 14;
					ddResponseSytemType = $('#'+triggeringButtonId).data('systemtype');
				}else{
					GridID = 0;
					ddResponseSytemType = $('#'+triggeringButtonId+'-text').data('systemtype');
				}

				if(ddResponseSytemType == 2){
					var buttonType = $('#'+triggeringButtonId+'-text').data('buttontype');
					if(buttonType != undefined){
						var splitButtonType = buttonType.split("-");
						GridID = splitButtonType[1];
						ddDBFieldType = 14;
					}else{
						GridID = 0;
					}
				}
			}


			var ddTbody = $("#dropdownItems");
			ddTbody.empty(); 
			$('#myModal').modal('hide');
			$('#myModal').modal('show');
			$('#DropDownModalID').val($(event.target).attr('id'));
			
			
			$("#pillContainer").empty();
			ddSelectedItemsValue = [];
			ddSelectedItemsText = [];
			startIndex = 0;
			triggeringButtonTabIndex = event.target.getAttribute('tabindex');
			// console.log("hello "+$("#kreon-grid-"+triggeringButtonSrNo+triggeringButtonId+"-text[type='button']").val());
			//on load in edit mode selected checkbox check & pill also display
			if(ddDBFieldType == 17){
				if(ddisGrid == 1){
					ddSelectedItemsText = $("#kreon-grid-"+triggeringButtonSrNo+triggeringButtonId+"-text[type='button']").val() ? $("#kreon-grid-"+triggeringButtonSrNo+triggeringButtonId+"-text[type='button']").val().split(",") : [];
					SingleValues = $("#kreon-grid-"+triggeringButtonSrNo+triggeringButtonId+"[type='hidden']").val().split(",");// .map(Number); //MITESH REMOVED
					
					// SingleValues = SingleValues.length === 1 && SingleValues[0] === 0 ? [] : SingleValues;
					// Check if the value is either undefined, empty string, or an array with an empty string
					if (!SingleValues || (Array.isArray(SingleValues) && SingleValues.length === 1 && SingleValues[0] === '')) {
						ddSelectedItemsValue = [];
					} else {
						ddSelectedItemsValue = SingleValues
					}
					
					$('#PopulateGridButton').hide();
				}else{
					ddSelectedItemsText = $("#"+triggeringButtonId+"-text[type='button']").val() ? $("#"+triggeringButtonId+"-text[type='button']").val().split(",") : [];
					SingleValues = $("#"+triggeringButtonId+"[type='hidden']").val().split(",");// .map(Number); //MITESH REMOVED
				
					// Check if the value is either undefined, empty string, or an array with an empty string
					if (!SingleValues || (Array.isArray(SingleValues) && SingleValues.length === 1 && SingleValues[0] === '')) {
						ddSelectedItemsValue = [];
					} else {
						ddSelectedItemsValue = SingleValues
					}

					// SingleValues = SingleValues.length === 1 && SingleValues[0] === 0 ? [] : SingleValues;
					// ddSelectedItemsValue = SingleValues;
					$('#PopulateGridButton').hide();
				}
	
			}else if(ddDBFieldType == 16){
				if(ddisGrid == 1){
					console.log("In field type 16");
					console.log(triggeringButtonSrNo,triggeringButtonId);
					ddSelectedItemsText = $("#kreon-grid-"+triggeringButtonSrNo+triggeringButtonId+"-text[type='button']").val() ? $("#kreon-grid-"+triggeringButtonSrNo+triggeringButtonId+"-text[type='button']").val().split(",") : [];
					MultipleValues = $("#kreon-grid-"+triggeringButtonSrNo+triggeringButtonId+"[type='hidden']").val().split(",");// .map(Number); //MITESH REMOVED
					if (!MultipleValues || (Array.isArray(MultipleValues) && MultipleValues[0] === '')) {
						ddSelectedItemsValue = [];
					} else {
						ddSelectedItemsValue = MultipleValues
					}
				}else{
					ddSelectedItemsText = $("#"+triggeringButtonId+"-text[type='button']").val() ? $("#"+triggeringButtonId+"-text[type='button']").val().split(",") : [];
					MultipleValues = $("#"+triggeringButtonId+"[type='hidden']").val().split(",");// .map(Number); //MITESH REMOVED
					if (!MultipleValues || (Array.isArray(MultipleValues) && MultipleValues[0] === '')) {
						ddSelectedItemsValue = [];
					} else {
						ddSelectedItemsValue = MultipleValues
					}
				}
				// MultipleValues = MultipleValues.length === 1 && MultipleValues[0] === 0 ? [] : MultipleValues;
				$('#PopulateGridButton').hide();
			}else if(ddDBFieldType == 14){
				$('#PopulateGridButton').show();
				$('#myModal').find('#PopulateGridButton').prop("disabled", false);
			}else if(ddDBFieldType == 23){
				$('#PopulateGridButton').hide();
			}
			
			// console.log("toggleModal", ddSelectedItemsValue);

			ddResponseData = [];
			// if(ddDBFieldType == 14){
			// 	var systemType = $('#'+triggeringButtonId).data('systemtype');
			// }else{
			// 	if(ddisGrid == 1){
			// 		var systemType = $('#kreon-grid-'+triggeringButtonSrNo+triggeringButtonId+'-text').data('systemtype');
			// 	}else{
			// 		console.log("in else");
			// 		var systemType = $('#'+triggeringButtonId+'-text').data('systemtype');
			// 	}
			// }


			if(ddResponseSytemType == 1){
				//old system for fld 16, 17
				await fetchTableData(triggeringButtonId);
				loadData(triggeringButtonId);
			}else{
				//New system for fld 16, 17
				await loadHeaderData(triggeringButtonId);
			}

			activeRowIndex=-1;

			if(ddSelectedItemsValue.length > 0){
				await displaySelectedItems();
			}

			if(ddResponseData['AdjustableAmount'] == 'enable'){
				$('#ShowAdjustableAmountField').css('display', 'flex');
				// $('#ShowAdjustableAmountField').css('display', 'block');
				$('#PopupAmount').val(0);
				$('#PopupAdjamount').val(0);
				$('#PopupBalance').val(0);

				var gridid = ddResponseData['PopulateGrid'].split(":");
				var gridIdentifier = $('#grid-id-'+gridid[1]).attr('class');		
				var gridSerialNo = gridIdentifier.slice(4);
				$('#PopupAmount').val($('#kreon-grid-'+gridSerialNo+'amount').val());
				// $('#PopupAdjamount').val(0);
				// alert($('#kreon-grid-'+gridSerialNo+'amount').val(), $('#kreon-grid-'+gridSerialNo+'SingleDDModaladjamount').val())
				if($('#kreon-grid-'+gridSerialNo+'amount').val() > 0){
					var baseSelector = '#kreon-grid-' + gridSerialNo;
					var adjField = $(baseSelector + 'adjamount-text').length ? 'adjamount-text' : 'adjamt-text';

					var adjInput = $(baseSelector + adjField);
					var amount = parseInt($(baseSelector + 'amount').val()) || 0;
					var adjValue = parseInt(adjInput.val()) || 0;

					if (adjInput.val() !== '' && adjValue > 0) {
						$('#PopupBalance').val(amount - adjValue);
						$('#PopupAdjamount').val(adjValue);
					}else{
						$('#PopupBalance').val(parseInt($('#kreon-grid-'+gridSerialNo+'amount').val()) - 0);

					}

					// if($('#kreon-grid-'+gridSerialNo+'adjamount-text').val() != ''){
					// 	if($('#kreon-grid-'+gridSerialNo+'adjamount-text').val() > 0){
					// 		$('#PopupBalance').val(parseInt($('#kreon-grid-'+gridSerialNo+'amount').val()) - parseInt($('#kreon-grid-'+gridSerialNo+'adjamount-text').val()));
					// 		$('#PopupAdjamount').val(parseInt($('#kreon-grid-'+gridSerialNo+'adjamount-text').val()));
					// 	}
					// }else{
					// 	$('#PopupBalance').val(parseInt($('#kreon-grid-'+gridSerialNo+'amount').val()) - 0);

					// }

				}else{
					$('#PopupBalance').val(0);
				}

				// if($('#kreon-grid-aSingleDDModalAdjAmt').val() != ''){
				// 	$('#PopupAdjamount').val(parseInt($('#kreon-grid-aSingleDDModalAdjAmt').val()));
				// }
				$('#PopupGridId').val(GridID);

				setTimeout(function() {
					$('#myModal .modal-body').find('#dropdownContent tbody tr:first').find('input[name="adjamount"],input[name="adjamt"]').focus();
				}, 500);

				$("#pillContainer").hide();
				$("#clearSelection").hide();
				$(".modal-footer").html("<p>Press Alt + K for Quick Adjustment</p>");
			}else{
				setTimeout(function() {
					$('.popup-search1').focus();
				}, 500);
				$("#pillContainer").show();

			}

		}

		function isValidDate(dateString) {
			// Regular expression for a basic date format (YYYY-MM-DD)
			const regex = /^\d{4}-\d{2}-\d{2}$/;

			// Check if the date matches the basic format
			if (!dateString.match(regex)) {
				return false;
			}

			// Try to create a Date object from the string
			const date = new Date(dateString);

			// Check if the resulting Date object is valid
			return date instanceof Date && !isNaN(date.getTime());
		}

		function getRequestCondition(){
			requestParams='';
			formReeponseString='';

			//Request parameters
			if(ddResponseData['requestParams'] != null && ddResponseData['requestParams'].length > 1 ){
				// Define the input string
				var inputString = ddResponseData['requestParams'];

				// Define the regular expression to match the desired patterns
				var regex = /([?#][^ \'\)\(\\"\\=]+)[ \'\)\(\\"\\=]/g;

				// Use match method to find all matches
				var matches = [];
				var match;
				while ((match = regex.exec(inputString)) !== null) {
					matches.push(match[1]);
				}
				var parameters = matches;
				var replaceString = '';
				
				for(var i=0; i<parameters.length; i++){
					replaceString='';
					if(parameters[i].indexOf("?") >= 0 || parameters[i].indexOf("#") >= 0){
						if(parameters[i].length > 1){
							if(parameters[i].indexOf("?") >= 0){
								var gridid = ddResponseData['PopulateGrid'].split(":");
								var gridIdentifier = $('#grid-id-'+gridid[1]).attr('class');		
								var gridSerialNo = gridIdentifier.slice(4);
								var qExtractedString = "kreon-grid-"+gridSerialNo +""+parameters[i].replace(/\?+/g, '');
							}

							if(parameters[i].indexOf("#") >= 0){
								//If # found it remove
								var qExtractedString = parameters[i].replace(/\#+/g, '');
								
							}
							
							if(qExtractedString.length > 0){
								var NumberOfElements = document.querySelectorAll("[id="+qExtractedString+"]");
								if(NumberOfElements.length == 2){
									replaceString += $(NumberOfElements[1]).val() !="" ? $(NumberOfElements[1]).val().trim() : '0';
									replaceString += " ";
								}else{
									if(qExtractedString == 'userid'){
										replaceString += <?php echo $_SESSION['user_id']; ?> 
									}else if(qExtractedString == 'companyid'){
										replaceString += <?php echo $_SESSION['dbCompany']; ?> 
									}else if(qExtractedString == 'divisionid'){
										replaceString += <?php echo $_SESSION['dbDivision']; ?> 
									}else if(qExtractedString == 'yearcode'){
										replaceString += <?php echo $_SESSION['dbYear']; ?> 
									}else if(qExtractedString == 'formid'){
										replaceString += <?php echo $FormID; ?> 
									}else{
										console.log(qExtractedString +" == "+ $('#'+qExtractedString.trim()).val());
										//replaceString += $('#'+qExtractedString).val() !="" ? $('#'+qExtractedString).val().trim() : '0';
										// replaceString += ($('#' + qExtractedString).length > 0 && $('#' + qExtractedString).val() !== "") ? $('#' + qExtractedString).val().trim() : '0';
										var elem = $('#' + qExtractedString);
										replaceString += (elem.length > 0 && elem.val() && elem.val().trim() !== "") ? elem.val().trim() : '0';
										
									}
								}
							}
							// console.log("replaceString",replaceString,parameters[i])
							// inputString.replace(parameters[i], replaceString);
							inputString = inputString.replaceAll(parameters[i], replaceString);
							// inputString.split(parameters[i]).join(replaceString);
							// console.log("inputString", inputString);
						}
					}
				}
				requestParams = inputString;
			}
			
			
			//FormResponse parameters
			if(ddResponseData['FormResponse'] != null && ddResponseData['FormResponse'].length > 1){
				var FormResponseArray=[];
				var FormResponseExtract = [];
				var FormResponseArray = ddResponseData['FormResponse'].split('|');
				var formResponseFieldText = "";
				var formResponseFieldValue = "";
				// console.log("FormResponse",ddResponseData['FormResponse']);
				for(var j=0; j<FormResponseArray.length; j++){
					FormResponseExtract = FormResponseArray[j].split(':');
					if(FormResponseExtract[2] == 17){

					}else if(FormResponseExtract[2] == 5){
						console.log(FormResponseExtract[1].trim() +" == "+ $('#'+FormResponseExtract[1].trim()).val());
					
						formResponseFieldValue = $('select#'+FormResponseExtract[1].trim()).val();
						formResponseFieldText = $('select#'+FormResponseExtract[1].trim()).find('option:selected').text();
					
					}else{
						formResponseFieldValue = $('#'+FormResponseExtract[1].trim()).val();
					}
					
					formReeponseString += FormResponseExtract[0]+":"+formResponseFieldValue;
	
					
					if(formResponseFieldText.length > 0){
						formReeponseString += ":"+formResponseFieldText;
					}

					if(j != FormResponseArray.length-1){
						formReeponseString += " | ";
					}
				}
				console.log("formReeponseString "+formReeponseString);
			}
		}


		// async function getSelect2Value(selector) {
		// 	console.log("in selector", selector);
		// 	const selectElement = $('#' + selector);
		// 	console.log("selectElement:", selectElement);
			
		// 	// Wait until Select2 is initialized
		// 	while (!selectElement.data('select2')) {
		// 		console.log("Waiting for Select2 to initialize...");
		// 		await new Promise(resolve => setTimeout(resolve, 100));  // Wait for 100ms before checking again
		// 	}

		// 	console.log("Select2 is ready, returning: ", selectElement.val());
		// 	// Once Select2 is initialized, return the value
		// 	return selectElement.val();
		// }


		// async function getRequestCondition2(){
		// 	// alert("hi");
		// 	requestParams='';
		// 	formReeponseString='';

		// 	//Request parameters
		// 	if(ddResponseData['requestParams'] != null && ddResponseData['requestParams'].length > 1 ){
		// 		// Define the input string
		// 		var inputString = ddResponseData['requestParams'];

		// 		// Define the regular expression to match the desired patterns
		// 		var regex = /([?#][^ \'\)\(\\"\\=]+)[ \'\)\(\\"\\=]/g;

		// 		// Use match method to find all matches
		// 		var matches = [];
		// 		var match;
		// 		while ((match = regex.exec(inputString)) !== null) {
		// 			matches.push(match[1]);
		// 		}
		// 		var parameters = matches;
		// 		var replaceString = '';
				
		// 		for(var i=0; i<parameters.length; i++){
		// 			replaceString='';
		// 			if(parameters[i].indexOf("?") >= 0 || parameters[i].indexOf("#") >= 0){
		// 				if(parameters[i].length > 1){
		// 					if(parameters[i].indexOf("?") >= 0){
		// 						var gridid = ddResponseData['PopulateGrid'].split(":");
		// 						var gridIdentifier = $('#grid-id-'+gridid[1]).attr('class');		
		// 						var gridSerialNo = gridIdentifier.slice(4);
		// 						var qExtractedString = "kreon-grid-"+gridSerialNo +""+parameters[i].replace(/\?+/g, '');
		// 					}

		// 					if(parameters[i].indexOf("#") >= 0){
		// 						//If # found it remove
		// 						var qExtractedString = parameters[i].replace(/\#+/g, '');
								
		// 					}
							
		// 					if(qExtractedString.length > 0){
		// 						var NumberOfElements = document.querySelectorAll("[id="+qExtractedString+"]");
		// 						if(NumberOfElements.length == 2){
		// 							replaceString += $(NumberOfElements[1]).val() !="" ? $(NumberOfElements[1]).val().trim() : '0';
		// 							replaceString += " ";
		// 						}else{
		// 							if(qExtractedString == 'userid'){
		// 								replaceString += <?php echo $_SESSION['user_id']; ?> 
		// 							}else if(qExtractedString == 'companyid'){
		// 								replaceString += <?php echo $_SESSION['dbCompany']; ?> 
		// 							}else if(qExtractedString == 'divisionid'){
		// 								replaceString += <?php echo $_SESSION['dbDivision']; ?> 
		// 							}else if(qExtractedString == 'yearcode'){
		// 								replaceString += <?php echo $_SESSION['dbYear']; ?> 
		// 							}else if(qExtractedString == 'formid'){
		// 								replaceString += <?php echo $FormID; ?> 
		// 							}else{
		// 								console.log(qExtractedString +" == "+ $('#'+qExtractedString.trim()).val());
		// 								//replaceString += $('#'+qExtractedString).val() !="" ? $('#'+qExtractedString).val().trim() : '0';
		// 								replaceString += ($('#' + qExtractedString).length > 0 && $('#' + qExtractedString).val() !== "") ? $('#' + qExtractedString).val().trim() : '0';
										
		// 							}
		// 						}
		// 					}
		// 					// console.log("replaceString",replaceString,parameters[i])
		// 					// inputString.replace(parameters[i], replaceString);
		// 					inputString = inputString.replaceAll(parameters[i], replaceString);
		// 					// inputString.split(parameters[i]).join(replaceString);
		// 					// console.log("inputString", inputString);
		// 				}
		// 			}
		// 		}
		// 		requestParams = inputString;
		// 	}
			
		// 	//FormResponse parameters
		// 	if(ddResponseData['FormResponse'] != null && ddResponseData['FormResponse'].length > 1){
		// 		console.log(ddResponseData['FormResponse']);
		// 		var FormResponseArray=[];
		// 		var FormResponseExtract = [];
		// 		var FormResponseArray = ddResponseData['FormResponse'].split('|');
		// 		var formResponseFieldText = "";
		// 		var formResponseFieldValue = "";
		// 		for(var j=0; j<FormResponseArray.length; j++){
		// 			FormResponseExtract = FormResponseArray[j].split(':');
		// 			if(FormResponseExtract[2] == 17){

		// 			}else if(FormResponseExtract[2] == 5){
		// 				// console.log(FormResponseExtract[1].trim() +" == "+ $('#'+FormResponseExtract[1].trim()).val());
		// 				console.log("in if condition");
		// 				formResponseFieldValue = await getSelect2Value(FormResponseExtract[1].trim());
		// 				formResponseFieldText = $('#'+FormResponseExtract[1].trim()).find('option:selected').text();
		// 				console.log(formResponseFieldValue);
		// 				console.log(formResponseFieldText);

		// 			}else{
		// 				formResponseFieldValue = $('#'+FormResponseExtract[1].trim()).val();
		// 			}
					
		// 			formReeponseString += FormResponseExtract[0]+":"+formResponseFieldValue;
	
					
		// 			if(formResponseFieldText.length > 0){
		// 				formReeponseString += ":"+formResponseFieldText;
		// 			}

		// 			if(j != FormResponseArray.length-1){
		// 				formReeponseString += " | ";
		// 			}
		// 		}
		// 		console.log("formReeponseString "+formReeponseString);
		// 	}

		// 	// Return the final value (requestParams or formReeponseString or both)
		// 	return {
		// 		requestParams: requestParams,
		// 		formReeponseString: formReeponseString
		// 	};
		// }

		function closeModal() {
			// if(ddDBFieldType == 14){
			// 	var systemType  = $('#'+triggeringButtonId).data('systemtype');
			// }else{
			// 	if(ddisGrid == 1){
			// 		var systemType = $('#kreon-grid-'+triggeringButtonSrNo+triggeringButtonId+'-text').data('systemtype');
			// 	}else{
			// 		var systemType  = $('#'+triggeringButtonId+'-text').data('systemtype');
			// 	}
			// }
			//Populate Grid on row selection
			if(ddResponseSytemType == 1){
				if(ddResponseData['gridResponseParams'] != null && ddResponseData['gridResponseParams'].length > 1 && ddResponseData['PopulateGrid'] != null && ddResponseData['PopulateGrid'].length > 1 ){
					// getRequestCondition();
					var selectedItems = ddSelectedItemsValue.toString();
					if(selectedItems.length > 0){
						console.log("in gridResponseParams "+formReeponseString);
						AddResultInGrid(ddRowSelectType,selectedItems,ddDBFieldType,triggeringButtonId,ddisGrid,requestParams,formReeponseString);
					}
				}
				
			}else{
				if($('#myModal').find('.modal-body').find('#error').length == 0){
					var selectedItems = ddSelectedItemsValue.toString();
					if(selectedItems.length > 0){
						try{
							// Making AJAX call with async/await
							$.ajax({
								type : 'POST',
								dataType: 'json',
								data : {selectedItems : selectedItems, FormID : <?php echo $FormID; ?>, DBFieldName: triggeringButtonId, GridID: GridID},
								url : 'getPopulateGridData.php',
								success : function(result){
									if (result['success'] == false) {
										alert(result['error']);
										return;
									}else{

										AddNewResultInGrid(result['data'], result['gridData'], result['gridResponse'], result['selecedRowIdName'], result['DynamicGridInsertFirstRowEdit'],result['formResponse']);
									}
								}
							});
						} catch (error) {
							console.error('Error:', error);
						}
					}
				}
			}

			$('#myModal').modal('hide');
			setTimeout(function() {
				if ($(".modal-backdrop").length > 0) {
					$(".modal-backdrop").remove();
				}
			}, 50); // Adjust delay as needed

			$(".popup-search1").val('');
			// for(var j=0; j<ddResponseData['columns'].length; j++){
			// 	$("#"+ddResponseData['columns'][j]['name']).val('');
			// }
		}

		// async function AddNewResultInGrid(data, gridData, gridResponse, selecedRowIdName, DynamicGridInsertFirstRowEdit,formResponse){

		// 	var OnkeyupParameterCalculation = [];
		// 	var gridCalculationFieldName = [];
		// 	var gridCalculationType = [];

		// 	for (var t = 0; t < gridData.length; t++) {
		// 		if (gridData[t]['GridCalculation'] > 0) {
		// 			gridCalculationFieldName.push(gridData[t]['DbFieldName']);
		// 			gridCalculationType.push(gridData[t]['GridCalculation']);
		// 		}
		// 		if (gridData[t]['OnkeyupParameters'].length > 2) {
		// 			if (gridData[t]['FieldType'] != 5) {
		// 				OnkeyupParameterCalculation.push([gridData[t]['DbFieldName'], gridData[t]['FieldType'], gridData[t]['OnkeyupParameters']]);
		// 			}
		// 		}
		// 	}

		// 	if (Array.isArray(data)) {
		// 		console.log("Data is an array:", data);
		// 		var promises = [];

		// 		for (var l = 0; l < data.length; l++) {
		// 			// gridResponseArray = ddResponseData['gridResponseParams'].split("|");

		// 			// Split the string based on the delimiter "|", or any suitable pattern
		// 			var splitString = gridResponse.split(" | "); // splitting by " | " ensures each segment is separated

		// 			// Loop through the array of strings
		// 			for (var i = 0; i < splitString.length; i++) {
		// 				var PopulateFieldInGrids = splitString[i];

		// 				// Regular expression to split the string
		// 				var regex = /(\d+)\((.*)\)/;

		// 				// Apply the regular expression to the string
		// 				var match = PopulateFieldInGrids.match(regex);

		// 				if (match) {
		// 					// Dynamically extracting the two parts
		// 					var gridid = match[1]; // This will be "2302"
		// 					var GridResponseString = match[2]; // This will be "gfld1:bname:1|gfld7:cntl:7"						
		// 				}

		// 				var gridIdentifier = $('#grid-id-' + gridid).attr('class');
		// 				var gridSerialNo = gridIdentifier.slice(4);
		// 				var gridResponseArray = GridResponseString.split("|");

		// 				for (var k = 0; k < gridResponseArray.length; k++) {

		// 					gridRes = gridResponseArray[k].split(":");

		// 					if (gridRes[2] == '1') {
		// 						$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).val(data[l][gridRes[1].trim()]);
								
		// 						// Call function for grid calculation
		// 						for (var m = 0; m < OnkeyupParameterCalculation.length; m++) {
		// 							if (OnkeyupParameterCalculation[m] != undefined) {
		// 								if (OnkeyupParameterCalculation[m].includes(gridRes[0].trim()) == true) {
		// 									promises.push(CreateOnkeyupScriptGrid("kreon-grid-" + gridSerialNo + "" + gridRes[0].trim(), 'grid', 'dynamic'));
		// 								}
		// 							}
		// 						}
		// 					} else if (gridRes[2] == '5' || gridRes[2] == '19') {
		// 						if (result['data'][l][gridRes[1]] != undefined) {
		// 							var $newOption = $("<option selected=\"selected\"></option>").val(data[l][gridRes[3].trim()]).text(result['data'][l][gridRes[1].trim()]);
		// 							if ($('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).data('select2')) {
		// 								$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).select2('open');
		// 								$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).append($newOption);
		// 								$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).select2('close');
		// 							} else {
		// 								$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).append($newOption);
		// 							}
		// 						}
		// 					} else if (gridRes[2].trim() == '17') {
		// 						$('#kreon-grid-' + gridSerialNo + gridRes[0].trim() + '-text').val(data[l][gridRes[1].trim()]);
		// 						$('#kreon-grid-' + gridSerialNo + gridRes[0].trim()).val(data[l][gridRes[3]]);
		// 					}
		// 				}

		// 				//Add form field value in grid using formResponse
		// 				if (formResponse != null && formResponse.length > 0) {
		// 					formResponseArray = formResponse.split("|");
		// 					for (var n = 0; n < formResponseArray.length; n++) {
		// 						formRes = formResponseArray[n].split(":");
		// 						if (formRes[2].trim() == '1' || formRes[2].trim() == '3' || formRes[2].trim() == '6' || formRes[2].trim() == '7' || formRes[2].trim() == '8') {
		// 							$('#kreon-grid-' + gridSerialNo + '' + formRes[0].trim()).val(formRes[1].trim());
		// 						} else if (formRes[2].trim() == '5' || formRes[2].trim() == '19') {
		// 							var $newOption = $("<option selected=\"selected\"></option>").val(formRes[1]).text(formRes[3]);
		// 							$('#kreon-grid-' + gridSerialNo + '' + formRes[0]).select2('open');
		// 							$('#kreon-grid-' + gridSerialNo + '' + formRes[0]).append($newOption).trigger("change");
		// 							$('#kreon-grid-' + gridSerialNo + '' + formRes[0]).select2('close');
		// 						} else if (formRes[2].trim() == '17') {
		// 							$('#kreon-grid-' + gridSerialNo + formRes[0].trim() + '-text').val(formRes[3].trim());
		// 							$('#kreon-grid-' + gridSerialNo + formRes[0].trim()).val(formRes[1]);
		// 						}
		// 					}
		// 				}

		// 				// Add new row and add the promise to the array
		// 				promises.push(AddNewRow($(this), gridData, gridSerialNo, data[l][selecedRowIdName], DynamicGridInsertFirstRowEdit));
		// 			}

		// 			// Wait for all promises to finish before moving forward
		// 			await Promise.all(promises);

		// 			// Once the new row is added, perform calculations
		// 			await CreateOnChangeScriptGridCalculation(gridCalculationFieldName, gridCalculationType, gridSerialNo, );

		// 			// $('#DropDownModal').modal('hide');
		// 			$('#myModal').modal('hide');
		// 			$(".modal-backdrop").remove(); // Removes the backdrop

		// 		}
		// 	}
		// }

		async function AddNewResultInGrid(data, gridData, gridResponse, selecedRowIdName, DynamicGridInsertFirstRowEdit,formResponse){
			var OnkeyupParameterCalculation = [];
			var gridCalculationFieldName = [];
			var gridCalculationType = [];

			for (var t = 0; t < gridData.length; t++) {
				if (gridData[t]['gridcalculation'] > 0) {
					gridCalculationFieldName.push(gridData[t]['dbfieldname']);
					gridCalculationType.push(gridData[t]['gridcalculation']);
				}
				if (gridData[t]['onkeyupparameters'] && gridData[t]['onkeyupparameters'].length > 2) {
					if (gridData[t]['fieldtype'] != 5) {
						OnkeyupParameterCalculation.push([gridData[t]['dbfieldname'], gridData[t]['fieldtype'], gridData[t]['onkeyupparameters']]);
					}
				}
			}

			if (Array.isArray(data)) {
				console.log("Data is an array:", data);
				var promises = [];
				var flag = 0;

				for (var l = 0; l < data.length; l++) {
					for (var t = 0; t < gridData.length; t++) {
						if (gridData[t]['required'] == 1) {
							var fldvalue = data[l][gridData[t]['dbfieldname']];
							if (fldvalue == "" || fldvalue == 0 || fldvalue == null) {
								flag = 1;
								alert(gridData[t]['displayname'] + " is required");
								if (DBFieldType == 17) {
									$('#' + DBFieldName +'-text').val("");
									$('#' + DBFieldName).val("");
								} else if (DBFieldType == 16) {
									$('#' + DBFieldName+'-text').val("");
									$('#' + DBFieldName).val("");
								}
								$('#DropDownModal').modal('hide');
								return;
							}
						}
					}

					if (flag == 0) {
						// Split the string based on the delimiter "|", or any suitable pattern
						var splitString = gridResponse.split(" | "); // splitting by " | " ensures each segment is separated

						// Loop through the array of strings
						for (var i = 0; i < splitString.length; i++) {
							var PopulateFieldInGrids = splitString[i];
							console.log(PopulateFieldInGrids);
							// Regular expression to split the string
							var regex = /(\d+)\((.*)\)/;

							// Apply the regular expression to the string
							var match = PopulateFieldInGrids.match(regex);

							if (match) {
								// Dynamically extracting the two parts
								var gridid = match[1]; // This will be "2302"
								var GridResponseString = match[2]; // This will be "gfld1:bname:1|gfld7:cntl:7"						
							}

							var gridIdentifier = $('#grid-id-' + gridid).attr('class');
							var gridSerialNo = gridIdentifier.slice(4);
							var gridResponseArray = GridResponseString.split("|");
							console.log("gridResponseArray");
							console.log(gridResponseArray);
							for (var k = 0; k < gridResponseArray.length; k++) {
								gridRes = gridResponseArray[k].split(":");

								// console.log(gridSelect2Fields);
								
								if (gridRes[2] == '1') {
									console.log(gridRes);
									$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).val(data[l][gridRes[1].trim()]);
									
									// Call function for grid calculation
									for (var m = 0; m < OnkeyupParameterCalculation.length; m++) {
										if (OnkeyupParameterCalculation[m] != undefined) {
											if (OnkeyupParameterCalculation[m].includes(gridRes[0].trim()) == true) {
												promises.push(CreateOnkeyupScriptGrid("kreon-grid-" + gridSerialNo + "" + gridRes[0].trim(), 'grid', 'dynamic'));
											}
										}
									}
								} else if (gridRes[2] == '5' || gridRes[2] == '19') {
									if (data[l][gridRes[1]] != undefined) {
										var newOptionValue = data[l][gridRes[1].trim()+'id'] === null ? 0 : data[l][gridRes[1].trim()+'id'];
										var $newOption = $("<option selected=\"selected\"></option>").val(newOptionValue).text(data[l][gridRes[1].trim()]);
										if ($('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).data('select2')) {
											$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).select2('open');
											$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).append($newOption);
											$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).select2('close');
										} else {
											$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).append($newOption);
										}
									}
								} else if (gridRes[2].trim() == '17' || gridRes[2].trim() == '16') {
									$('#kreon-grid-' + gridSerialNo + gridRes[0].trim() + '-text').val(data[l][gridRes[1].trim()]);
									$('#kreon-grid-' + gridSerialNo + gridRes[0].trim()).val(data[l][gridRes[3]]);
								}

							}

							//Add form field value in grid using formResponse
							if (formResponse != null && formResponse.length > 0) {
								formResponseArray = formResponse.split("|");
								for (var n = 0; n < formResponseArray.length; n++) {
									formRes = formResponseArray[n].split(":");
									if (formRes[2].trim() == '1' || formRes[2].trim() == '3' || formRes[2].trim() == '6' || formRes[2].trim() == '7' || formRes[2].trim() == '8') {
										$('#kreon-grid-' + gridSerialNo + '' + formRes[0].trim()).val(formRes[1].trim());
									} else if (formRes[2].trim() == '5' || formRes[2].trim() == '19') {
										var $newOption = $("<option selected=\"selected\"></option>").val(formRes[1]).text(formRes[3]);
										$('#kreon-grid-' + gridSerialNo + '' + formRes[0]).select2('open');
										$('#kreon-grid-' + gridSerialNo + '' + formRes[0]).append($newOption).trigger("change");
										$('#kreon-grid-' + gridSerialNo + '' + formRes[0]).select2('close');
									} else if (formRes[2].trim() == '17' || formRes[2].trim() == '16') {
										$('#kreon-grid-' + gridSerialNo + formRes[0].trim() + '-text').val(formRes[3].trim());
										$('#kreon-grid-' + gridSerialNo + formRes[0].trim()).val(formRes[1]);
									}
								}
							}

							// Add new row and add the promise to the array
							promises.push(AddNewRow($(this), gridData, gridSerialNo, data[l][selecedRowIdName], DynamicGridInsertFirstRowEdit));
						}
					}

					// Wait for all promises to finish before moving forward
					await Promise.all(promises);

					// Once the new row is added, perform calculations
					await CreateOnChangeScriptGridCalculation(gridCalculationFieldName, gridCalculationType, gridSerialNo, );

					// $('#DropDownModal').modal('hide');
					$('#myModal').modal('hide');
					$(".modal-backdrop").remove(); // Removes the backdrop

				}
			}
		}

		function validateRequiredFields(requiredFieldsStr) {
			var RequiredFieldsArray = requiredFieldsStr.split('|');

			for (var k = 0; k < RequiredFieldsArray.length; k++) {
				var RequiredFieldValue = $("#" + RequiredFieldsArray[k]).val();
				console.log("RequiredFieldValue",RequiredFieldValue);
				if (RequiredFieldValue == "") {
					$("#ValidationMessage").show();
					$('#ValidationMessage').html(
						'<span style="color: red;">' + RequiredFieldsArray[k] + ' field is required.</span>'
					);
					return false;  // Stop processing
				}
			}
			$("#ValidationMessage").hide();
			$('#ValidationMessage').html('');
			return true;
		}

		// AJAX call to fetch data dynamically
		// async function fetchTableData(DBFieldName) {
		// 	await $.ajax({
		// 	url: 'getMultyDDValue1.php', // Replace with your API URL
		// 	method: 'GET',
		// 	dataType: 'json',
		// 	data: {FormID:<?php echo $FormID; ?>, GridID:GridID, DBFieldName:DBFieldName, DBFieldType:ddDBFieldType, isGrid:ddisGrid}, // Sending start index and limit for pagination
		// 	success: function(response) {
		// 		ddResponseData = response;
				
		// 		//if RequiredFields found then check validation
		// 		// if(ddResponseData['RequiredFields'] && ddResponseData['RequiredFields'].length > 0){
		// 		// 	validateRequiredFields(ddResponseData['RequiredFields']);
		// 		// }
				
		// 		var columns = response['columns'];
		// 		// Load the table headers
		// 		let headerHtml = '';
		// 		for (let i = 0; i < columns.length; i++) {
		// 			var column = columns[i];
		// 			if(i==0){
		// 				headerHtml += `<th></th>`;
		// 			}else{
		// 				headerHtml += `<th>
		// 					${column['name'].charAt(0).toUpperCase() + column['name'].slice(1)}<br>

		// 					<input type="search" class="search-column form-control popup-search${i}"  id="${column['name']}" placeholder="Search ${column['name']}" data-column="${i}">
		// 				</th>`;
		// 			}
		// 		}

		// 		$("#tableHeader").empty();  // Clear existing rows
		// 		$("#tableHeader").append(`${headerHtml}`);

		// 		// Load the first batch of data
		// 		// loadData(DBFieldName);
		// 	},
		// 	error: function() {
		// 		alert('Failed to load data');
		// 	}
		// 	});
		// }

		async function fetchTableData(DBFieldName) {
			let passedValidation = false;
			await $.ajax({
				url: 'getMultyDDValue1.php',
				method: 'GET',
				dataType: 'json',
				data: {
					FormID: <?php echo $FormID; ?>,
					GridID: GridID,
					DBFieldName: DBFieldName,
					DBFieldType: ddDBFieldType,
					isGrid: ddisGrid
				},
				success: function(response) {
					ddResponseData = response;

					let shouldContinue = true;
					if (ddResponseData['RequiredFields'] && ddResponseData['RequiredFields'].length > 0) {
						shouldContinue = validateRequiredFields(ddResponseData['RequiredFields']);
					}

					if (!shouldContinue) {
						passedValidation = false; //Don't continue
						return;  // Exit before loading data or headers
					}

					// If validation passed
					passedValidation = true;

					var columns = response['columns'];
					let headerHtml = '';
					for (let i = 0; i < columns.length; i++) {
						var column = columns[i];
						if (i == 0) {
							headerHtml += `<th></th>`;
						} else {
							headerHtml += `<th>
								${column['name'].charAt(0).toUpperCase() + column['name'].slice(1)}<br>
								<input type="search" class="search-column form-control popup-search${i}"  id="${column['name']}" placeholder="Search ${column['name']}" data-column="${i}">
							</th>`;
						}
					}
					// console.log(headerHtml);
					$("#tableHeader").empty();
					$("#tableHeader").append(`${headerHtml}`);

					// ✅ Only called when validation passes
					// loadData(DBFieldName);
				},
				error: function() {
					alert('Failed to load data');
				}
			});
			return passedValidation;
		}

		function loadData(DBFieldName) {
			getRequestCondition();

			// if (startIndex >= data.length) return;  // If all data is already loaded, stop loading more
			$("#loadingMessage").show();

			// Show loading spinner inside the table
			$("#loadingData").show();

			var globalValue = $("#globalSearch").val().toLowerCase();

			var columnFilters = $(".search-column").map(function() {
				const column = $(this).attr("id");
				const columnValue = $(this).val().toLowerCase();
				return { column, columnValue };
			}).get();

			// Create a custom string, for example: "column1:value1, column2:value2"
			var columnFiltersString = columnFilters
				.map(function(filter) {
					if (filter.columnValue) {
						return filter.column + ":" + filter.columnValue;
					}
				})
				.filter(function(item) {
					return item !== undefined;
				})
				.join("|");
			
			$.ajax({
				url: 'getFieldType17Data.php', // Replace with your server endpoint
				method: 'POST',
				dataType: 'json',
				data: { start: startIndex, limit: itemsPerPage, DBFieldName: DBFieldName, FormID: <?php echo $FormID; ?>, GridID:GridID, isGrid:ddisGrid, DBFieldType:ddDBFieldType, GlobalSearch: globalValue, ColumnFilters: columnFiltersString, requestParams: requestParams, formData: $('form').serialize()},
				success: function(response) {
					// Hide loading spinner when data is loaded
					$("#loadingData").hide();

					var columns = ddResponseData['columns'];
					
					var rows = response['data']; // Adjust this based on your server response format
					var ddTbody = $("#dropdownItems");

					// If there are no rows, show the "No data found" message
					if (!Array.isArray(rows) || rows.length === 0) {
						// Only show "No data found" message if it isn't already visible
						if (!ddTbody.find('.no-data-row').length) {
							ddTbody.append('<tr class="no-data-row"><td colspan="' + columns.length + '" class="text-center"><center>No data found</center></td></tr>');
						}
						$("#loadingMessage").hide();
						return;
					}

					

					// Ensure 'columns' is defined and contains the expected structure
					if (!columns || columns.length === 0) {
						console.error("Columns are not defined properly.");
						return;
					}
					console.log(rows);
					var rowHtml;
					rows.forEach(row => {
						rowHtml = `<tr>`;
						columns.forEach(column => {
							const columnName = column['name'];
							if (columnName in row) {
								if (columnName.trim() === columns[0]['name'].trim()) {
									// Check if the value is in ddSelectedItemsValue
									// var isChecked = ddSelectedItemsValue.includes((row[columnName].toString()));
									var isChecked = row[columnName] !== null && row[columnName] !== undefined && ddSelectedItemsValue.includes(row[columnName].toString());
									// const isChecked = ddSelectedItemsValue.includes(parseInt(row[columnName]));
									rowHtml += `<td><input type="checkbox" id="checkbox" class="item-checkbox" value="${row[columnName]}" onclick="toggleSelection(this)" ${isChecked ? 'checked' : ''}></td>`;

									//`<td><input type="hidden" value="${row[columnName]}"></td>`;
									//<input type="checkbox" class="item-checkbox" value="${row[columnName]}" onclick="toggleSelection(this)">
								} else {
									if(row[columnName] && row[columnName].date){
										rowHtml += `<td>${row[columnName].date}</td>`;
									}else{
										rowHtml += `<td>${row[columnName]}</td>`;
									}
								}
							}
						});
						rowHtml += `</tr>`;
						// console.log(rowHtml);
						ddTbody.append(rowHtml);
					});

					var hiddenColumns = [];
					//logic for hide columns & add data as data-key attribute in previous row if hidden columns are present & field type is 5,19,16 & 17
					if(ddResponseData['hiddenColumns'] != null && ddResponseData['hiddenColumns'] != ""){
						var hiddenColumns = ddResponseData['hiddenColumns'].split(',');

						if(hiddenColumns.length > 0){
							var headers = $('#tableHeader th');
							var rows = $("#dropdownItems tr");
							// console.log(rows);
							// Convert jQuery object to a plain array
							Array.from(headers).forEach(function(header, colIndex) {
								if (hiddenColumns.includes(header.innerText.trim())) {
									// Hide the column header (th)
									header.classList.add('hidden');
									
									// Loop through each row and hide the cell in the hidden column
									rows.each(function(index,row) {
										// Check if the row has enough cells
										if (row.cells.length > colIndex) {
											row.cells[colIndex].classList.add('hidden');
											
											// Add the hidden column's data to the previous cell's data-key attribute
											if (colIndex > 0) {  // Make sure there's a previous column to attach the data to
												var previousCell = row.cells[colIndex - 1];
												previousCell.setAttribute('data-key', row.cells[colIndex].innerText);
											}
										} 
										else {
											// console.log("Row does not have enough cells: ", row);
										}
									});
								}
							});
						}
					}
						
					startIndex += itemsPerPage;
					$("#loadingMessage").hide();
					// generateFooter();  // Re-generate the footer after data load
				},
				error: function(xhr, status, error) {
					console.error('Failed to load data:', error);
					$("#loadingMessage").hide();
				}
			});

			startIndex += itemsPerPage;
			$("#loadingMessage").hide();
		}

		async function loadHeaderData(DBFieldName) {
			// getRequestCondition();

			// if (startIndex >= data.length) return;  // If all data is already loaded, stop loading more
			$("#loadingMessage").show();

			var globalValue = $("#globalSearch").val().toLowerCase();

			var columnFilters = $(".search-column").map(function() {
				const column = $(this).attr("id");
				const columnValue = $(this).val().toLowerCase();
				return { column, columnValue };
			}).get();

			// Create a custom string, for example: "column1:value1, column2:value2"
			var columnFiltersString = columnFilters
				.map(function(filter) {
					if (filter.columnValue) {
						return filter.column + ":" + filter.columnValue;
					}
				})
				.filter(function(item) {
					return item !== undefined;
				})
				.join("|");

			await $.ajax({
				url: 'getNewFieldType17Data.php', // Replace with your server endpoint
				method: 'POST',
				dataType: 'json',
				data: { start: startIndex, limit: itemsPerPage, DBFieldName: DBFieldName, FormID: <?php echo $FormID; ?>, GridID:GridID, isGrid:ddisGrid, DBFieldType:ddDBFieldType, GlobalSearch: globalValue, ColumnFilters: columnFiltersString, requestParams: requestParams, formData: $('form').serialize()},
				success: function(response) {

					if (response['success'] == false) {
						$('#myModal').find('.modal-body').html('<p id=\'error\' style=\'color:red;\'>'+response['error']+'</p>');
						callback(false); // Callback with false
					}else{
						
						responseHeaders = Object.keys(response['data'][0]);

						//define ddResponseTotalColumnIndex globally for accessing in toggleSelection function 
						var TotalColumnNameArray = response['totalcolumnname'].split('|');
						for(var j=0; j<TotalColumnNameArray.length; j++){
							var index = responseHeaders.indexOf(TotalColumnNameArray[j]);
							ddResponseTotalColumnIndex.push(index);
						}

						//define ddResponsePopulateFields globally for accessing in toggleSelection function 
						ddResponsePopulateFields = response['PopulateFields'];

						//define ddResponsePopulateFields globally for accessing in toggleSelection function 
						ddResponsePopulateFieldInGrids = response['PopulateFieldInGrid'];

						//define ddResponsePopulateGrid globally for accessing in toggleSelection function
						ddResponsePopulateGrid = response['PopulateGrid'];

						//define ddResponseHiddenColumns globally for accessing in toggleSelection function
						ddResponsePopulateGrid = response['PopulateGrid'];

						// Load the table headers
						let headerHtml = '';

						// Iterate over each key-value pair in the current object
						for (var i=0; i<responseHeaders.length; i++) {
							if(i==0){
								headerHtml += `<th></th>`;
							}else{
								headerHtml += `<th>
									${responseHeaders[i].charAt(0).toUpperCase() + responseHeaders[i].slice(1)}<br>
									<input type="search" class="search-column form-control popup-search${i}"  id="${responseHeaders[i]}" placeholder="Search ${responseHeaders[i]}" data-column="${i}">
								</th>`
							}
						}
		
						$("#tableHeader").empty();  // Clear existing rows
						$("#tableHeader").append(`${headerHtml}`);
						// console.log(responseHeaders[0]);
						var ddTbody = $("#dropdownItems");
						var rowHtml;
						response['data'].forEach((row, rowIndex )=> {
							rowHtml = `<tr>`;
							responseHeaders.forEach((column, columnIndex)=> {
								const columnName = column;
								if (columnName in row) {
									if (columnName.trim() === responseHeaders[0]) {
										// Check if the value is in ddSelectedItemsValue
										var isChecked = ddSelectedItemsValue.includes((row[columnName].toString()));
										// const isChecked = ddSelectedItemsValue.includes(parseInt(row[columnName]));
										rowHtml += `<td><input type="checkbox" id="checkbox" class="item-checkbox" value="${row[columnName]}" onclick="toggleSelection(this)" ${isChecked ? 'checked' : ''}></td>`;

										//`<td><input type="hidden" value="${row[columnName]}"></td>`;
										//<input type="checkbox" class="item-checkbox" value="${row[columnName]}" onclick="toggleSelection(this)">
									} else {
										if(row[columnName] && row[columnName].date){
											rowHtml += `<td>${row[columnName].date}</td>`;
										}else{
											rowHtml += `<td>${row[columnName]}</td>`;
										}
									}
								}
							});
							rowHtml += `</tr>`;
							// console.log(rowHtml);
							ddTbody.append(rowHtml);
						});

						var rowHtml;
						// console.log('hi');
						var hiddenColumns = [];
						//logic for hide columns & add data as data-key attribute in previous row if hidden columns are present & field type is 5,19,16 & 17
						if(response['hiddenColumns'] != null && response['hiddenColumns'] != ""){
							// console.log("in");
							
							var hiddenColumns = response['hiddenColumns'].split(',');

							// Capitalize the first letter of each column name
							hiddenColumns = hiddenColumns.map(function(column) {
								return column.charAt(0).toUpperCase() + column.slice(1);
							});
						
							if(hiddenColumns.length > 0){
								var headers = $('#tableHeader th');
								var rows = $("#dropdownItems tr");
								// console.log(headers);
								
								// Convert jQuery object to a plain array
								Array.from(headers).forEach(function(header, colIndex) {
									if (hiddenColumns.includes(header.innerText.trim())) {
										// Hide the column header (th)
										header.classList.add('hidden');
										
										// Loop through each row and hide the cell in the hidden column
										rows.each(function(index,row) {
											// Check if the row has enough cells
											if (row.cells.length > colIndex) {
												row.cells[colIndex].classList.add('hidden');
												
												// Add the hidden column's data to the previous cell's data-key attribute
												if (colIndex > 0) {  // Make sure there's a previous column to attach the data to
													var previousCell = row.cells[colIndex - 1];
													previousCell.setAttribute('data-key', row.cells[colIndex].innerText);
												}
											} 
											else {
												// console.log("Row does not have enough cells: ", row);
											}
										});
									}
								});
							}
						}
						
						// generateFooter();  // Re-generate the footer after data load
					}
				},
				error: function(xhr, status, error) {
					console.error('Failed to load data:', error);
					$("#loadingMessage").hide();
				}
			});

			startIndex += itemsPerPage;
			$("#loadingMessage").hide();
		}

		async function loadBankRecoData(DBFieldName) {
			// getRequestCondition();

			// if (startIndex >= data.length) return;  // If all data is already loaded, stop loading more
			$("#loadingMessage").show();

			var globalValue = $("#globalSearch").val().toLowerCase();

			var columnFilters = $(".search-column").map(function() {
				const column = $(this).attr("id");
				const columnValue = $(this).val().toLowerCase();
				return { column, columnValue };
			}).get();

			// Create a custom string, for example: "column1:value1, column2:value2"
			var columnFiltersString = columnFilters
				.map(function(filter) {
					if (filter.columnValue) {
						return filter.column + ":" + filter.columnValue;
					}
				})
				.filter(function(item) {
					return item !== undefined;
				})
				.join("|");

			var bankRecoFlag = '';
			if(DBFieldName == 'bankreco'){
				bankRecoFlag = 'uncleared';
			}else if(DBFieldName == 'bankreco1'){
				bankRecoFlag = 'cleared';
			}else if(DBFieldName == 'bankrecoexport'){
				bankRecoFlag = 'export';
			}

			await $.ajax({
				url: 'getBankRecoData.php', // Replace with your server endpoint
				method: 'POST',
				dataType: 'json',
				data: { start: startIndex, limit: itemsPerPage, DBFieldName: DBFieldName, FormID: <?php echo $FormID; ?>, GridID:GridID, isGrid:ddisGrid, DBFieldType:ddDBFieldType, GlobalSearch: globalValue, ColumnFilters: columnFiltersString, requestParams: requestParams, formData: $('form').serialize(), bankRecoFlag:bankRecoFlag},
				success: function(response) {

					if (response['success'] == false) {
						$('#myModal').find('.modal-body').html('<p id=\'error\' style=\'color:red;\'>'+response['error']+'</p>');
						callback(false); // Callback with false
					}else{
						if(DBFieldName == 'bankrecoexport'){
							const jsonData = response.json();
							console.log(jsonData);
						}else{
							responseHeaders = Object.keys(response['data'][0]);

							// Load the table headers
							let headerHtml = '';

							// Iterate over each key-value pair in the current object
							for (var i=0; i<responseHeaders.length; i++) {
								if(i==0){
									headerHtml += `<th></th>`;
								}else{
									headerHtml += `<th>
										${responseHeaders[i].charAt(0).toUpperCase() + responseHeaders[i].slice(1)}<br>
										<input type="search" class="search-column form-control popup-search${i}"  id="${responseHeaders[i]}" placeholder="Search ${responseHeaders[i]}" data-column="${i}">
									</th>`
								}
							}
			
							$("#tableHeader").empty();  // Clear existing rows
							$("#tableHeader").append(`${headerHtml}`);
							// console.log(responseHeaders[0]);
							var ddTbody = $("#dropdownItems");
							var rowHtml;
							response['data'].forEach((row, rowIndex )=> {
								rowHtml = `<tr>`;
								responseHeaders.forEach((column, columnIndex)=> {
									const columnName = column;
									if (columnName in row) {
										if (columnName.trim() === responseHeaders[0]) {
											// Check if the value is in ddSelectedItemsValue
											var isChecked = ddSelectedItemsValue.includes((row[columnName].toString()));
											// const isChecked = ddSelectedItemsValue.includes(parseInt(row[columnName]));
											rowHtml += `<td><input type="checkbox" id="checkbox" class="item-checkbox" value="${row[columnName]}" onclick="toggleSelection(this)" ${isChecked ? 'checked' : ''}></td>`;

											//`<td><input type="hidden" value="${row[columnName]}"></td>`;
											//<input type="checkbox" class="item-checkbox" value="${row[columnName]}" onclick="toggleSelection(this)">
										} else {
											if(row[columnName] && row[columnName].date){
												rowHtml += `<td>${row[columnName].date}</td>`;
											}else{
												rowHtml += `<td>${row[columnName]}</td>`;
											}
										}
									}
								});
								rowHtml += `</tr>`;
								// console.log(rowHtml);
								ddTbody.append(rowHtml);
							});

							var rowHtml;
							// console.log('hi');
							var hiddenColumns = [];
							//logic for hide columns & add data as data-key attribute in previous row if hidden columns are present & field type is 5,19,16 & 17
							if(response['hiddenColumns'] != null && response['hiddenColumns'] != ""){
								// console.log("in");
								
								var hiddenColumns = response['hiddenColumns'].split(',');

								// Capitalize the first letter of each column name
								hiddenColumns = hiddenColumns.map(function(column) {
									return column.charAt(0).toUpperCase() + column.slice(1);
								});
							
								if(hiddenColumns.length > 0){
									var headers = $('#tableHeader th');
									var rows = $("#dropdownItems tr");
									// console.log(headers);
									
									// Convert jQuery object to a plain array
									Array.from(headers).forEach(function(header, colIndex) {
										if (hiddenColumns.includes(header.innerText.trim())) {
											// Hide the column header (th)
											header.classList.add('hidden');
											
											// Loop through each row and hide the cell in the hidden column
											rows.each(function(index,row) {
												// Check if the row has enough cells
												if (row.cells.length > colIndex) {
													row.cells[colIndex].classList.add('hidden');
													
													// Add the hidden column's data to the previous cell's data-key attribute
													if (colIndex > 0) {  // Make sure there's a previous column to attach the data to
														var previousCell = row.cells[colIndex - 1];
														previousCell.setAttribute('data-key', row.cells[colIndex].innerText);
													}
												} 
												else {
													// console.log("Row does not have enough cells: ", row);
												}
											});
										}
									});
								}
							}
						}
					
						// generateFooter();  // Re-generate the footer after data load
					}
				},
				error: function(xhr, status, error) {
					console.error('Failed to load data:', error);
					$("#loadingMessage").hide();
				}
			});

			startIndex += itemsPerPage;
			$("#loadingMessage").hide();
		}

		$('#PopulateGridButton').off().on('click', async function(e) {
			var GridMDDModalHiddenValue = ddSelectedItemsValue.toString();
			// var GridMDDModalHiddenValue = $('#GridMultyDDModal-hidden-' + DBFieldName).val();
			// var MultiSelectGridId = $('#MultiSelectGridId-hidden-' + DBFieldName).val();
			console.log("inside");
			// Disable the button
			$('#myModal').find('#PopulateGridButton').prop("disabled", true);
			if(<?php echo $FormID; ?> == 4894){

				var response = await getRequestCondition2();
				console.log(response);
			}else{
				getRequestCondition();
			}
			try {

				if(ddResponseSytemType == 1){
					var result = await $.ajax({
						type: 'POST',
						dataType: 'json',
						data: {
							selectedRowId : GridMDDModalHiddenValue, 
							GridId : GridID, 
							FormId: <?php echo $FormID; ?>, 
							DBFieldType:ddDBFieldType, 
							FieldName:triggeringButtonId, 
							isGrid:ddisGrid, 
							requestParams:requestParams

						},
						url: 'getGridData.php'
					});

					if(ddResponseSytemType == 1){

						var gridIdentifier = $('#grid-id-' + triggeringButtonId).attr('class');
					}else{
						var gridIdentifier = $('#grid-id-' + GridID).attr('class');

					}
					var gridSerialNo = gridIdentifier.slice(4);

					var gridRes = [];
					var gridCalculationFieldName = [];
					var gridCalculationType = [];
					var gridResponseArray = [];
					var OnkeyupParameterCalculation = [];

					

					for (var t = 0; t < result['gridData'].length; t++) {
						if (result['gridData'][t]['gridcalculation'] > 0) {
							gridCalculationFieldName.push(result['gridData'][t]['dbfieldname']);
							gridCalculationType.push(result['gridData'][t]['gridcalculation']);
						}
						if (result['gridData'][t]['onkeyupparameters'].length > 2) {
							if (result['gridData'][t]['fieldtype'] != 5) {
								OnkeyupParameterCalculation.push([result['gridData'][t]['dbfieldname'], result['gridData'][t]['fieldtype'], result['gridData'][t]['onkeyupparameters']]);
							}
						}
					}

					if (Array.isArray(result['data'])) {
						console.log("Data is an array:", result['data']);
						
						var promises = [];

						for (var l = 0; l < result['data'].length; l++) {
							gridResponseArray = ddResponseData['gridResponseParams'].split("|");
							
							for (var k = 0; k < gridResponseArray.length; k++) {
								console.log("Processing row", l);
								gridRes = gridResponseArray[k].split(":");

								if (gridRes[2] == '1' || gridRes[2] == '7') {
									$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).val(result['data'][l][gridRes[1].trim()]);
									
									// Call function for grid calculation
									for (var m = 0; m < OnkeyupParameterCalculation.length; m++) {
										if (OnkeyupParameterCalculation[m] != undefined) {
											if (OnkeyupParameterCalculation[m].includes(gridRes[0].trim()) == true) {
												promises.push(CreateOnkeyupScriptGrid("kreon-grid-" + gridSerialNo + "" + gridRes[0].trim(), 'grid', 'dynamic'));
											}
										}
									}
								}else if (gridRes[2].trim() == '6') {
									var dateObj = result['data'][l][gridRes[1].trim()];
									
									const date = new Date(dateObj.date);
									const options = {
										timeZone: 'Asia/Calcutta',
										year: 'numeric',
										month: '2-digit',
										day: '2-digit'
									};
									const formattedDate = new Intl.DateTimeFormat('en-GB', options).format(date);
									const [day, month, year] = formattedDate.split('/');
									const inputDateValue = `${year}-${month}-${day}`;
									$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).val(inputDateValue);
								} else if (gridRes[2] == '5' || gridRes[2] == '19') {
									if (result['data'][l][gridRes[1].trim()] != undefined) {
										var $newOption = $("<option selected=\"selected\"></option>").val(result['data'][l][gridRes[3].trim()]).text(result['data'][l][gridRes[1].trim()]);
										if ($('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).data('select2')) {
											$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).select2('open');
											$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).append($newOption);
											$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).select2('close');
										} else {
											$('#kreon-grid-' + gridSerialNo + '' + gridRes[0].trim()).append($newOption);
										}
									}
								} else if (gridRes[2].trim() == '17' || gridRes[2].trim() == '16') {
									$('#kreon-grid-' + gridSerialNo + gridRes[0].trim() + '-text').val(result['data'][l][gridRes[1].trim()]);
									$('#kreon-grid-' + gridSerialNo + gridRes[0].trim()).val(result['data'][l][gridRes[3]]);
								}
							}

							DynamicGridInsertFirstRowEdit = ddResponseData['DynamicGridInsertFirstRowEdit'];

							// Add new row and add the promise to the array
							promises.push(AddNewRow($(this), result['gridData'], gridSerialNo, result['data'][l][result['selecedRowIdName']], DynamicGridInsertFirstRowEdit));

						}

						// Wait for all promises to finish before moving forward
						await Promise.all(promises);

						// Once the new row is added, perform calculations
						// for (var l = 0; l < gridCalculationFieldName.length; l++) {
							await CreateOnChangeScriptGridCalculation(gridCalculationFieldName, gridCalculationType, gridSerialNo);
						// }

						// $('#DropDownModal').modal('hide');
						$('#myModal').modal('hide');
						$(".modal-backdrop").remove(); // Removes the backdrop

					} else {
						console.log('result["data"] is not an array', result['data']);
					}
				}else{
					var selectedItems = ddSelectedItemsValue.toString();
					// Making AJAX call with async/await
					$.ajax({
						type : 'POST',
						dataType: 'json',
						data : {selectedItems : selectedItems, FormID : <?php echo $FormID; ?>, DBFieldName: triggeringButtonId, GridID: GridID},
						url : 'getPopulateGridData.php',
						success : function(result){
							console.log(result);
							if (result['success'] == false) {
								alert(result['error']);
								return;
							}else{

								AddNewResultInGrid(result['data'], result['gridData'], result['gridResponse'], result['selecedRowIdName'], result['DynamicGridInsertFirstRowEdit'],result['formResponse']);
							}
						}
					});
				}

			} catch (error) {
				console.error('Error:', error);
			}
		});

		function applyFilters(DBFieldName) {
			// const globalValue = $("#globalSearch").val().toLowerCase();
			activeRowIndex=-1;
			startIndex = 0;
			var ddTbody = $("#dropdownItems");
			ddTbody.empty();  // Clear existing rows
			// if(ddDBFieldType == 14){
			// 	var systemType  = $('#'+DBFieldName).data('systemtype');
			// }else{
			// 	if(ddisGrid == 1){
			// 		var systemType = $('#kreon-grid-'+triggeringButtonSrNo+DBFieldName+'-text').data('systemtype');
			// 	}else{
			// 		var systemType  = $('#'+DBFieldName+'-text').data('systemtype');
			// 	}
			// }

			if(ddResponseSytemType == 1){
				loadData(DBFieldName);
			}else{
				loadHeaderData(DBFieldName);
			}
		}

		function toggleSelection(checkbox) {
			
			getRequestCondition();
			var rowText = checkbox.value;
			
			// Get the text of the first column (column 1) of the selected row
			var firstColumnText = $(checkbox).closest("tr").find("td:nth-child(2)").text();
			

			if (checkbox.checked) {
				if(ddDBFieldType == 17){
					// Uncheck all checkboxes
					$(".item-checkbox").prop("checked", false);
					ddSelectedItemsValue = [];
					ddSelectedItemsText = [];
					checkbox.checked = true;
					ddRowSelectType = 'single';
				}else{
					ddRowSelectType = 'multi';
				}

				ddSelectedItemsValue.push((rowText));
				// ddSelectedItemsValue.push(parseInt(rowText));
				ddSelectedItemsText.push(firstColumnText);

				
			} else {
				const index = ddSelectedItemsValue.indexOf((rowText));
				// const index = ddSelectedItemsValue.indexOf(parseInt(rowText));
				if (index !== -1) {
					ddSelectedItemsValue.splice(index, 1);
				}

				const index1 = ddSelectedItemsText.indexOf(firstColumnText);
				if (index1 !== -1) {
					ddSelectedItemsText.splice(index1, 1);
				}
			}

			displaySelectedItems();

			if(ddDBFieldType == 16 || ddDBFieldType == 17){
				// if(ddisGrid == 1){
				// 	var systemType = $('#kreon-grid-'+triggeringButtonSrNo+triggeringButtonId+'-text').data('systemtype');
				// }else{
				// 	var systemType = $('#'+triggeringButtonId+'-text').data('systemtype');
				// }


				//Populate Field in Form
				if(ddResponseSytemType == 1){
					if(ddResponseData['PopulateFields'] != null  && ddResponseData['PopulateFields'].length > 1){
						var headerCol = [];
						$.each(ddResponseData['columns'], function(index, value) {
							headerCol.push(value.name);  // Push the 'name' property of each object into headerCol array
						});
						
						var PoulateFieldArray = ddResponseData['PopulateFields'].split('|');
						
						for(var n=0; n < PoulateFieldArray.length; n++){
							
							for(var m=0; m < headerCol.length; m++){
								var pFieldArray = PoulateFieldArray[n].split(':');
								
								if(headerCol[m].trim() == pFieldArray[1].trim()){
									var indexOfHideColumn = headerCol.indexOf(headerCol[m]);
									// var selectedOption = $(checkbox).closest("tr").find('td:eq('+indexOfHideColumn+')').text();

									var selectedOption = $(checkbox).closest("tr").find('td:eq(' + indexOfHideColumn + ')');

									var columnContent = selectedOption.contents(); // Get the contents of the column cell

									// Check if the content is an input element (textbox)
									if (columnContent.is('input[type="text"]')) {
										selectedOption = columnContent.val(); // Get the value if it's a textbox
									} else {
										selectedOption = columnContent.text(); // Get the text if it's not a textbox
									}
			
									
									//popup field type text, textarea, number
									if(pFieldArray[2] == 1 || pFieldArray[2] == 3 || pFieldArray[2] == 8){
										
										$('#'+pFieldArray[0].trim()+'[type="text"]').val(selectedOption);
									}else if(pFieldArray[2] == 7){
										$('#'+pFieldArray[0].trim()+'[type="number"]').val(selectedOption);
									}else if(pFieldArray[2] == 5 || pFieldArray[2] == 19){
										
										var fldvalue = $(checkbox).closest("tr").find('td:eq('+indexOfHideColumn+')').attr('data-key');
										var newOption = $('<option selected="selected"></option>').val(fldvalue).text(selectedOption);
										Elements = document.querySelectorAll("select[id="+pFieldArray[0].trim()+"]");

										if($('#'+Elements.id).data('select2')) {
											$('#'+Elements.id).select2('open');
											$(Elements).append(newOption); //.trigger("change");
											$('#'+Elements.id).select2('close');
										}else{
											$(Elements).append(newOption);
										}
									}else if(pFieldArray[2] == 6){
										const [day, month, year] = selectedOption.split('-');
										// Create a new Date object (months are 0-indexed in JavaScript)
										const date = new Date(year, month - 1, day);
										// Format the date to yyyy-MM-dd
										const formattedDate = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
										
										$('#'+pFieldArray[0].trim()).val(formattedDate);
									}else if(pFieldArray[2] == 16){
										var fldvalue = $(checkbox).closest("tr").find('td:eq('+indexOfHideColumn+')').attr('data-key');
									
										$('#'+pFieldArray[0].trim()+'-text').val(selectedOption);
										$('#'+pFieldArray[0].trim()+'[type="hidden"]').val(fldvalue);
									}else if(pFieldArray[2] == 17){
										var fldvalue = $(checkbox).closest("tr").find('td:eq('+indexOfHideColumn+')').attr('data-key');
										$('#'+pFieldArray[0].trim()+"-text"+'[type="button"]').val(selectedOption);
										$('#'+pFieldArray[0].trim()+'[type="hidden"]').val(fldvalue);
										
									}
								}
							}	
						}
						
					}
				}else{
					if(ddResponsePopulateFields != null && ddResponsePopulateFields.length > 1){
						// var headerCol = [];
						// $.each(ddResponseData['columns'], function(index, value) {
						// 	headerCol.push(value.name);  // Push the 'name' property of each object into headerCol array
						// });

						var headerCol = responseHeaders;
						
						var PoulateFieldArray = ddResponsePopulateFields.split('|');

						for(var n=0; n < PoulateFieldArray.length; n++){
							
							for(var m=0; m < headerCol.length; m++){
								var pFieldArray = PoulateFieldArray[n].split(':');
								
								if(headerCol[m].trim() == pFieldArray[1].trim()){
									var indexOfHideColumn = headerCol.indexOf(headerCol[m]);
									// var selectedOption = $(checkbox).closest("tr").find('td:eq('+indexOfHideColumn+')').text();

									var selectedOption = $(checkbox).closest("tr").find('td:eq(' + indexOfHideColumn + ')');

									var columnContent = selectedOption.contents(); // Get the contents of the column cell

									// Check if the content is an input element (textbox)
									if (columnContent.is('input[type="text"]')) {
										selectedOption = columnContent.val(); // Get the value if it's a textbox
									} else {
										selectedOption = columnContent.text(); // Get the text if it's not a textbox
									}
			
									//popup field type text, textarea, number
									if(pFieldArray[2] == 1 || pFieldArray[2] == 3 || pFieldArray[2] == 8){
										$('#'+pFieldArray[0].trim()+'[type="text"]').val(selectedOption);
									}else if(pFieldArray[2] == 7){
										$('#'+pFieldArray[0].trim()+'[type="number"]').val(selectedOption);
									}else if(pFieldArray[2] == 5 || pFieldArray[2] == 19){
										
										var fldvalue = $(checkbox).closest("tr").find('td:eq('+indexOfHideColumn+')').attr('data-key');
										var newOption = $('<option selected="selected"></option>').val(fldvalue).text(selectedOption);
										Elements = document.querySelectorAll("select[id="+pFieldArray[0].trim()+"]");

										if($('#'+Elements.id).data('select2')) {
											$('#'+Elements.id).select2('open');
											$(Elements).append(newOption); //.trigger("change");
											$('#'+Elements.id).select2('close');
										}else{
											$(Elements).append(newOption);
										}
									}else if(pFieldArray[2] == 6){
										const [day, month, year] = selectedOption.split('-');
										// Create a new Date object (months are 0-indexed in JavaScript)
										const date = new Date(year, month - 1, day);
										// Format the date to yyyy-MM-dd
										const formattedDate = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;

										$('#'+pFieldArray[0].trim()).val(selectedOption);
									}else if(pFieldArray[2] == 16){
										var fldvalue = $(checkbox).closest("tr").find('td:eq('+indexOfHideColumn+')').attr('data-key');
									
										$('#'+pFieldArray[0].trim()+'-text').val(selectedOption);
										$('#'+pFieldArray[0].trim()+'[type="hidden"]').val(fldvalue);
									}else if(pFieldArray[2] == 17){
										var fldvalue = $(checkbox).closest("tr").find('td:eq('+indexOfHideColumn+')').attr('data-key');
										$('#'+pFieldArray[0].trim()+"-text"+'[type="button"]').val(selectedOption);
										$('#'+pFieldArray[0].trim()+'[type="hidden"]').val(fldvalue);
										
									}
								}
							}	
						}
					}
				}

				//Populate Field in Grid
				if(ddResponseSytemType == 1){
					if(ddResponseData['PopulateGridFields'] != null && ddResponseData['PopulateGridFields'].length > 1){
						var gridid = ddResponseData['PopulateGrid'].split(":");
						var gridIdentifier = $('#grid-id-'+gridid[1]).attr('class');		
						var gridSerialNo = gridIdentifier.slice(4);

						var headerCol = [];
						$.each(ddResponseData['columns'], function(index, value) {
							headerCol.push(value.name);  // Push the 'name' property of each object into headerCol array
						});
						var PoulateGridFieldArray = ddResponseData['PopulateGridFields'].split('|');
						console.log(PoulateGridFieldArray);
						for(var n=0; n < PoulateGridFieldArray.length; n++){
							for(var m=0; m < headerCol.length; m++){
								var pFieldArray = PoulateGridFieldArray[n].split(':');
								
								if(headerCol[m].trim() == pFieldArray[1].trim()){
									
									var indexOfHideColumn = headerCol.indexOf(headerCol[m]);
									// var selectedOption = $(checkbox).closest("tr").find('td:eq('+indexOfHideColumn+')').text();

									var selectedOption = $(checkbox).closest("tr").find('td:eq(' + indexOfHideColumn + ')');

									var columnContent = selectedOption.contents(); // Get the contents of the column cell

									// Check if the content is an input element (textbox)
									if (columnContent.is('input[type="text"]')) {
										selectedOption = columnContent.val(); // Get the value if it's a textbox
									} else {
										selectedOption = columnContent.text(); // Get the text if it's not a textbox
									}
	
									if(pFieldArray[2].trim() == 1 || pFieldArray[2].trim() == 3 || pFieldArray[2].trim() == 8){		
										$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()+'[type="text"]').val(selectedOption);
									}else if(pFieldArray[2].trim() == 7){
										$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()+'[type="number"]').val(selectedOption);
									}else if(pFieldArray[2].trim() == 5 || pFieldArray[2].trim() == 19){
					
										var fldvalue = $(checkbox).closest("tr").find('td:eq('+indexOfHideColumn+')').attr('data-key');
										var newOption = $('<option selected="selected"></option>').val(fldvalue).text(selectedOption);
										Elements = document.querySelectorAll("select[id=kreon-grid-"+gridSerialNo+pFieldArray[0].trim()+"]");

										if($('#'+Elements.id).data('select2')) {
											$('#'+Elements.id).select2('open');
											$(Elements).append(newOption).trigger("change");
											$('#'+Elements.id).select2('close');
										}else{
											$(Elements).append(newOption);
										}
									}else if(pFieldArray[2].trim() == 6){
										const [day, month, year] = selectedOption.split('-');
										// Create a new Date object (months are 0-indexed in JavaScript)
										const date = new Date(year, month - 1, day);
										// Format the date to yyyy-MM-dd
										const formattedDate = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;

										$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()).val(selectedOption);
									}else if(pFieldArray[2].trim() == 16){
										var fldvalue = $(this).find('td:eq('+indexOfHideColumn+')').attr('data-key');

										$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()+'-text').val(selectedOption);
										$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()+'[type="hidden"]').val(fldvalue);
									}else if(pFieldArray[2].trim() == 17){
										var fldvalue = $(this).find('td:eq('+indexOfHideColumn+')').attr('data-key');

										$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()+'-text').val(selectedOption);
										$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()+'[type="hidden"]').val(fldvalue);
									}
								}
							}	
						}
					}
				}else{
					// if(ddResponsePopulateFieldInGrids != null && ddResponsePopulateFieldInGrids.length > 1){
					// 	var gridid = ddResponsePopulateGrid.split(":");
					// 	var gridIdentifier = $('#grid-id-'+gridid[1]).attr('class');		
					// 	var gridSerialNo = gridIdentifier.slice(4);

					// 	// var headerCol = [];
					// 	// $.each(ddResponseData['columns'], function(index, value) {
					// 	// 	headerCol.push(value.name);  // Push the 'name' property of each object into headerCol array
					// 	// });

					// 	var headerCol = responseHeaders;
					// 	var PoulateGridFieldArray = ddResponsePopulateFieldInGrids.split('|');
						
					// 	for(var n=0; n < PoulateGridFieldArray.length; n++){
					// 		for(var m=0; m < headerCol.length; m++){
					// 			var pFieldArray = PoulateGridFieldArray[n].split(':');
					// 			console.log(pFieldArray);
					// 			if(headerCol[m].trim() == pFieldArray[1].trim()){
					// 				var indexOfHideColumn = headerCol.indexOf(headerCol[m]);
					// 				var selectedOption = $(checkbox).closest("tr").find('td:eq('+indexOfHideColumn+')').text();
									
					// 				if(pFieldArray[2] == 1 || pFieldArray[2] == 3 || pFieldArray[2] == 8){					
					// 					console.log('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim());
					// 					$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()+'[type="text"]').val(selectedOption);
					// 				}else if(pFieldArray[2] == 7){

					// 					$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()+'[type="number"]').val(selectedOption);

					// 				}else if(pFieldArray[2] == 5 || pFieldArray[2] == 19){
					
					// 					var fldvalue = $(checkbox).closest("tr").find('td:eq('+indexOfHideColumn+')').attr('data-key');
					// 					var newOption = $('<option selected="selected"></option>').val(fldvalue).text(selectedOption);
					// 					Elements = document.querySelectorAll("select[id=kreon-grid-"+gridSerialNo+pFieldArray[0].trim()+"]");

					// 					if($('#'+Elements.id).data('select2')) {
					// 						$('#'+Elements.id).select2('open');
					// 						$(Elements).append(newOption).trigger("change");
					// 						$('#'+Elements.id).select2('close');
					// 					}else{
					// 						$(Elements).append(newOption);
					// 					}
					// 				}else if(pFieldArray[2] == 6){
					// 					const [day, month, year] = selectedOption.split('-');
					// 					// Create a new Date object (months are 0-indexed in JavaScript)
					// 					const date = new Date(year, month - 1, day);
					// 					// Format the date to yyyy-MM-dd
					// 					const formattedDate = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;

					// 					$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()).val(selectedOption);
					// 				}else if(pFieldArray[2] == 16){
					// 					var fldvalue = $(this).find('td:eq('+indexOfHideColumn+')').attr('data-key');

					// 					$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()+'-text').val(selectedOption);
					// 					$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()+'[type="hidden"]').val(fldvalue);
					// 				}else if(pFieldArray[2] == 17){
					// 					var fldvalue = $(this).find('td:eq('+indexOfHideColumn+')').attr('data-key');

					// 					$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()+'-text').val(selectedOption);
					// 					$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()+'[type="hidden"]').val(fldvalue);
					// 				}
					// 			}
					// 		}	
					// 	}
					// }

					if(ddResponsePopulateFieldInGrids != null && ddResponsePopulateFieldInGrids.length > 1){

						var headerCol = responseHeaders;

						// Split the string based on the delimiter "|", or any suitable pattern
						var splitString = ddResponsePopulateFieldInGrids.split(" | "); // splitting by " | " ensures each segment is separated

						// Loop through the array of strings
						for (var i = 0; i < splitString.length; i++) {
							var PopulateFieldInGrids = splitString[i];

							// Regular expression to split the string
							var regex = /(\d+)\((.*)\)/;

							// Apply the regular expression to the string
							var match = PopulateFieldInGrids.match(regex);

							if (match) {
								// Dynamically extracting the two parts
								var gridid = match[1]; // This will be "2302"
								var PopulateFieldInGrids = match[2]; // This will be "gfld1:bname:1|gfld7:cntl:7"						
							}

							var gridIdentifier = $('#grid-id-'+gridid).attr('class');
							
							if (typeof gridIdentifier === 'undefined') {
								alert(gridid +' grid is not exist in this form');
							} else {
								var gridSerialNo = gridIdentifier.slice(4);
								var PoulateGridFieldArray = PopulateFieldInGrids.split('|');

								for(var n=0; n < PoulateGridFieldArray.length; n++){
									for(var m=0; m < headerCol.length; m++){
										var pFieldArray = PoulateGridFieldArray[n].split(':');

										if(headerCol[m].trim() == pFieldArray[1].trim()){
											var indexOfHideColumn = headerCol.indexOf(headerCol[m]);
											// var selectedOption = $(checkbox).closest("tr").find('td:eq('+indexOfHideColumn+')').text();

											var selectedOption = $(checkbox).closest("tr").find('td:eq(' + indexOfHideColumn + ')');

											var columnContent = selectedOption.contents(); // Get the contents of the column cell

											// Check if the content is an input element (textbox)
											if (columnContent.is('input[type="text"]')) {
												selectedOption = columnContent.val(); // Get the value if it's a textbox
											} else {
												selectedOption = columnContent.text(); // Get the text if it's not a textbox
											}
											
											if(pFieldArray[2] == 1 || pFieldArray[2] == 3 || pFieldArray[2] == 8){					
												console.log('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim());
												$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()+'[type="text"]').val(selectedOption);
											}else if(pFieldArray[2] == 7){

												$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()+'[type="number"]').val(selectedOption);

											}else if(pFieldArray[2] == 5 || pFieldArray[2] == 19){
							
												var fldvalue = $(checkbox).closest("tr").find('td:eq('+indexOfHideColumn+')').attr('data-key');
												var newOption = $('<option selected="selected"></option>').val(fldvalue).text(selectedOption);
												Elements = document.querySelectorAll("select[id=kreon-grid-"+gridSerialNo+pFieldArray[0].trim()+"]");

												if($('#'+Elements.id).data('select2')) {
													$('#'+Elements.id).select2('open');
													$(Elements).append(newOption).trigger("change");
													$('#'+Elements.id).select2('close');
												}else{
													$(Elements).append(newOption);
												}
											}else if(pFieldArray[2] == 6){
												const [day, month, year] = selectedOption.split('-');
												// Create a new Date object (months are 0-indexed in JavaScript)
												const date = new Date(year, month - 1, day);
												// Format the date to yyyy-MM-dd
												const formattedDate = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;

												$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()).val(selectedOption);
											}else if(pFieldArray[2] == 16){
												var fldvalue = $(this).find('td:eq('+indexOfHideColumn+')').attr('data-key');

												$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()+'-text').val(selectedOption);
												$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()+'[type="hidden"]').val(fldvalue);
											}else if(pFieldArray[2] == 17){
												var fldvalue = $(this).find('td:eq('+indexOfHideColumn+')').attr('data-key');

												$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()+'-text').val(selectedOption);
												$('#kreon-grid-'+gridSerialNo+pFieldArray[0].trim()+'[type="hidden"]').val(fldvalue);
											}
										}
									}	
								}
							}		

							
						}
					}	
				}
			}

			if(ddDBFieldType == 14 || ddDBFieldType == 16){
				// if(ddResponseData['TotalColumnIndexResponse'] != undefined && ddResponseData['TotalColumnIndexResponse'].length > 0 && ddResponseData['TotalColumnIndexResponse'] != null){  					
				// 	var responseParamsArray = ddResponseData['responseParams'].split('|');
				// 	// foot.html('');
				// 	var foot = $('#ddTable').find('tfoot');

				// 	if (foot.length == 0){
				// 		foo = $('<tfoot>').appendTo('#ddTable'); 
				// 		// foot = $('<tr>').appendTo('tfoot');
				// 		foot = $('<tr>').appendTo(foo);
				// 		for(var i=0; i<responseParamsArray.length; i++){
				// 			foot.append($('<th style="text-align:right;"></th>'));
				// 		}
				// 	}

				// 	var columns = ddResponseData['TotalColumnIndexResponse'].split(',');
				// 	for(var j=0; j< columns.length; j++){
				// 		var totalAmount=0;
				// 		totalAmount = $('tfoot tr th').eq(columns[j]-1).text(); 
						
				// 		if(totalAmount == ""){
				// 			totalAmount = 0;
				// 		}else{
				// 			totalAmount = parseFloat(totalAmount);
				// 		}
						
				// 		if($(checkbox).closest("tr").find('td:eq('+columns[j]+')').text() != ""){
				// 			totalAmount += parseFloat($(checkbox).closest("tr").find('td:eq('+columns[j]+')').text());
				// 			$('tfoot tr th').eq(columns[j]-1).html(totalAmount);
				// 		}
				// 	}
				// }
				// if(ddDBFieldType == 14){
				// 	var systemType = $('#'+triggeringButtonId).data('systemtype');
				// }else{
				// 	var systemType = $('#'+triggeringButtonId+'-text').data('systemtype');
				// }
				if(ddResponseSytemType == 1){
					if(ddResponseData['TotalColumnIndexResponse'] != undefined && ddResponseData['TotalColumnIndexResponse'].length > 0 && ddResponseData['TotalColumnIndexResponse'] != null){  					
						var responseParamsArray = ddResponseData['responseParams'].split('|');
						// foot.html('');
						var foot = $('#ddTable').find('tfoot');

						if (foot.length == 0){
							foo = $('<tfoot>').appendTo('#ddTable'); 
							// foot = $('<tr>').appendTo('tfoot');
							foot = $('<tr>').appendTo(foo);
							for(var i=0; i<responseParamsArray.length; i++){
								foot.append($('<th style="text-align:right;"></th>'));
							}
						}

						var columns = ddResponseData['TotalColumnIndexResponse'].split(',');
						for(var j=0; j< columns.length; j++){
							var totalAmount=0;
							totalAmount = $('tfoot tr th').eq(columns[j]-1).text(); 
							
							if(totalAmount == ""){
								totalAmount = 0;
							}else{
								totalAmount = parseFloat(totalAmount);
							}
							
							if($(checkbox).closest("tr").find('td:eq('+columns[j]+')').text() != ""){
								totalAmount += parseFloat($(checkbox).closest("tr").find('td:eq('+columns[j]+')').text());
								$('tfoot tr th').eq(columns[j]-1).html(totalAmount);
							}
						}
					}
				}else{
					
					// var responseParamsArray = ddResponseData['responseParams'].split('|');
					// foot.html('');
					var foot = $('#ddTable').find('tfoot');

					if (foot.length == 0){
						foo = $('<tfoot>').appendTo('#ddTable'); 
						// foot = $('<tr>').appendTo('tfoot');
						foot = $('<tr>').appendTo(foo);
						for(var i=0; i<responseHeaders.length; i++){
							foot.append($('<th style="text-align:right;"></th>'));
						}
					}

					var columns = ddResponseTotalColumnIndex;
					for(var j=0; j< columns.length; j++){
						var totalAmount=0;
						totalAmount = $('tfoot tr th').eq(columns[j]).text(); 
						
						if(totalAmount == ""){
							totalAmount = 0;
						}else{
							totalAmount = parseFloat(totalAmount);
						}
						
						if($(checkbox).closest("tr").find('td:eq('+columns[j]+')').text() != ""){
							totalAmount += parseFloat($(checkbox).closest("tr").find('td:eq('+columns[j]+')').text());
							console.log("totalAmount "+totalAmount);
							$('tfoot tr th').eq(columns[j]).html(totalAmount);
						}
					}
				}
			}
			
			// updateFooterTotal();
		}

		function displaySelectedItems() {
			console.log("ddSelectedItemsValue "+ddSelectedItemsValue);
			console.log("ddSelectedItemsText "+ddSelectedItemsText);

			return new Promise((resolve) => {
				if (ddDBFieldType == 17) {
					if (ddisGrid == 1) {
						var button = $("#kreon-grid-" + triggeringButtonSrNo + triggeringButtonId + '-text' + '[type="button"]');
						$('#kreon-grid-' + triggeringButtonSrNo + triggeringButtonId).val('');
					} else {
						var button = $("#" + triggeringButtonId + '-text' + '[type="button"]');
						$('#' + triggeringButtonId).val('');
					}
				} else if (ddDBFieldType == 16) {
					if (ddisGrid == 1) {
						var button = $("#kreon-grid-" + triggeringButtonSrNo + triggeringButtonId + '-text' + '[type="button"]');
						$('#' + triggeringButtonId).val('');
					}else{
						var button = $("#" + triggeringButtonId + '-text' + '[type="button"]');
						$('#' + triggeringButtonId).val('');
					}
				}


				var buttonTextString = '';
				var selectedItemValueString = '';
				$("#pillContainer").empty();

				// Iterate over selected values and create pill containers
				for (var i = 0; i < ddSelectedItemsValue.length; i++) {
					$("#pillContainer").append("<div id='pill-" + ddSelectedItemsValue[i] + "'><div class='pill-label btn-primary'>" + ddSelectedItemsText[i] + "&nbsp;<span onclick='deselectRow(" + ddSelectedItemsValue[i] + ")' class='pill-remove bg-danger'>X</span></div></div>");

					// Concatenate text and values
					buttonTextString += ddSelectedItemsText[i];
					selectedItemValueString += ddSelectedItemsValue[i];

					if (i != ddSelectedItemsText.length - 1) {
						buttonTextString += ",";
						selectedItemValueString += ",";
					}
				}

				if (ddDBFieldType != 14) {
					if (ddisGrid == 1) {
						console.log("ddSelectedItemsText ", ddSelectedItemsText);
						$('#kreon-grid-' + triggeringButtonSrNo + triggeringButtonId + '-text').val(buttonTextString);
					} else {
						// console.log("innn "+ JSON.stringify(button));
						setTimeout(function () {
							button.val(buttonTextString);
						}, 300);
					}
				}

				// Update hidden fields with selected item values
				if (ddDBFieldType == 17) {
					if (ddisGrid == 1) {
						setTimeout(function () {
							console.log("ddSelectedItemsValue ", ddSelectedItemsValue);
							$('#kreon-grid-' + triggeringButtonSrNo + triggeringButtonId + '[type="hidden"]').val(ddSelectedItemsValue);
							resolve(); // Resolve promise after setting hidden field
						}, 300);
					} else {
						setTimeout(function () {
							$('#' + triggeringButtonId + '[type="hidden"]').val(ddSelectedItemsValue);
							resolve(); // Resolve promise after setting hidden field
						}, 300);
					}
				} else if (ddDBFieldType == 16) {
					if (ddisGrid == 1) {
						setTimeout(function () {
							$('#kreon-grid-' + triggeringButtonSrNo + triggeringButtonId + '[type="hidden"]').val(ddSelectedItemsValue);
							resolve(); // Resolve promise after setting hidden field
						}, 300);
					}else{
						setTimeout(function () {
							$('#' + triggeringButtonId + '[type="hidden"]').val(ddSelectedItemsValue);
							resolve(); // Resolve promise after setting hidden field
						}, 300);
					}
				}
			});
		}

		// Function to deselect a row and remove it from the selected lists
		function deselectRow(value) {
			// Find the index of the element
			let index = ddSelectedItemsValue.indexOf(String(value).trim());
			// If the element is found, remove it
			if (index !== -1) {
				// Remove value from selectedItemsValue array
				ddSelectedItemsValue.splice(index, 1);
				ddSelectedItemsText.splice(index, 1);
			}

			// Get and escape the associated text of the pill
			var valueText = $("#pill-" + value).find('.pill-label').text().trim().replace(/"/g, '\\"');

			// Uncheck the checkbox corresponding to the value
			$("input[type='checkbox'][value='" + value + "']").prop("checked", false);

			// Remove the pill element from the DOM
			$("#pill-" + value).remove();

			// Update the display after a short delay
			setTimeout(displaySelectedItems, 1000);
		}

		function clearSelection() {
			ddSelectedItemsValue = [];
			$(".item-checkbox").prop("checked", false);
			displaySelectedItems();
		}

		function updateActiveRow(visibleRows) {
			$("#dropdownItems tr").removeClass("active");
			if (activeRowIndex >= 0 && activeRowIndex < visibleRows.length) {
				const selectedRow = $(visibleRows[activeRowIndex]);
				selectedRow.addClass("active");
				
				// Toggle the checkbox state for the active row
				const checkbox = selectedRow.find("input[type='checkbox']");
				checkbox.focus();
			}
		}

		$(document).ready(function () {
			// Infinite scrolling to load more items when user reaches bottom
			$('#dropdownContent').scroll(function() {
				const bottom = $(this)[0].scrollHeight === $(this).scrollTop() + $(this).innerHeight();
				if (bottom) {
					if(ddResponseSytemType == 1){
						loadData(triggeringButtonId);  // Load more rows when the user scrolls to the bottom
					}else{
						loadHeaderData(triggeringButtonId); // Load more rows when the user scrolls to the bottom
					}
				}
			});	
		});

		function scrollToActiveRow() {
			const visibleRows = $("#dropdownItems tr:visible");
			// console.log(visibleRows)
			const activeRow = $(visibleRows[activeRowIndex]);
			// console.log(activeRowIndex,activeRow)
			const dropdownContent = $("#dropdownContent");
			const rowOffsetTop = activeRow.position().top;
			const dropdownScrollTop = dropdownContent.scrollTop();
			const rowHeight = activeRow.outerHeight();

			if (rowOffsetTop < 0) {
				dropdownContent.scrollTop(dropdownScrollTop + rowOffsetTop);
			} else if (rowOffsetTop + rowHeight > dropdownContent.innerHeight()) {
				dropdownContent.scrollTop(dropdownScrollTop + rowOffsetTop - dropdownContent.innerHeight() + rowHeight);
			}
		}

		// Add click event listener on table rows to toggle checkbox
		// $(document).on('click', '#dropdownItems tr', function() {
		$(document).on('click', '#dropdownItems tr, #dropdownItems input[type="checkbox"]', function(event) {
			if (isSpacebarPressed) return; // Prevent action if spacebar is pressed (to avoid interference)
			const checkbox = $(this).find("input[type='checkbox']");
			checkbox.prop("checked", !checkbox.prop("checked"));
			toggleSelection(checkbox[0]);
		});

		// Add an event listener to the close button
		$(".btn-close").on("click", function() {
			closeModal(); // Call your custom closeModal function
		});

		// Keyboard navigation and selection
		$(document).on("keydown", function(e) {
			const visibleRows = $("#dropdownItems tr:visible");
			const numRows = visibleRows.length;
			// var DBFieldName = $(e.target).attr('id');
			
			if (!$("#myModal").is(":visible")) return;
			// if (!$("#myModal").is(":visible") || numRows === 0) return;
			
			switch (e.key) {
				case "ArrowDown":
				e.preventDefault();
				if (activeRowIndex < numRows - 1) {
					activeRowIndex++;
					updateActiveRow(visibleRows);
					scrollToActiveRow();
				}
				break;

				case "ArrowUp":
				e.preventDefault();
				if (activeRowIndex > 0) {
					activeRowIndex--;
					updateActiveRow(visibleRows);
					scrollToActiveRow();
				}else{
					if (activeRowIndex == 0) {
						$(".popup-search1").focus();
					}
				}
				break;

				case "Enter":
					if (!$("#myModal").is(":visible")) {
						// Open the modal if it's not visible
						toggleModal();
					} else {
						// Apply the filter if the modal is visible & if input type number or text then don't call applyFilters
						if (!$(e.target).is("input[type='number']") && !$(e.target).is("input[type='text']")) {
							applyFilters(triggeringButtonId);
						}
					}
				break;

				case " ":
					// Allow spacebar to work for typing in search input
					if ($(e.target).is('input[type="text"], input[type="search"], textarea')) {
						return; // Let the spacebar work in the search input
					}
				
					e.preventDefault();
					isSpacebarPressed = true; // Set flag to indicate spacebar is pressed
					if (activeRowIndex >= 0 && numRows > 0) {
						const selectedRow = $(visibleRows[activeRowIndex]);
						const checkbox = selectedRow.find("input[type='checkbox']");
						
						// Toggle the checkbox checked state
						checkbox.prop("checked", !checkbox.prop("checked"));
						
						// Update the selected items list
						toggleSelection(checkbox[0]);

						// Optionally, you could highlight the active row to indicate selection change
						selectedRow.toggleClass("active");
						isSpacebarPressed = false;
					}
				break;

				case "Escape":
					console.log("in escape "+ddResponseData);
					// var tabindexValue = $('#yourElementId').attr('tabindex');
					if (triggeringButtonTabIndex !== null) {
						triggeringButtonTabIndex++;
						$("[tabindex='"+triggeringButtonTabIndex+"']").focus();
					}

					closeModal();
					break;

				 
				case "Home":
					// Add the event listener for the Home key
					$('#myModal .modal-body').find('#dropdownContent').scrollTop(0);
					break;

				case "Tab":
					if (e.shiftKey) {
						// Focus on the first search input field (global search or the first search-column)
						const firstSearchInput = document.querySelector('.search-column');
						if (firstSearchInput) {
							$('#myModal .modal-body').find('#dropdownContent').scrollTop(0);
							firstSearchInput.focus();
							event.preventDefault(); // Prevent default behavior to avoid jumping to the first element in the DOM
						}
					}
			}
		});	

	</script>
	<?php
	$ddtableIncludes = '

	<link rel="stylesheet" type="text/css" href="./assets/vendor/jquery-datatables/media/css/jquery.dataTables.css">
	<link rel="stylesheet" type="text/css" href="./assets/vendor/jquery-datatables/extras/TableTools/css/dataTables.tableTools.css">
	<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/keytable/2.12.0/css/keyTable.dataTables.css">
	<link rel="stylesheet" type="text/css" href="./assets/vendor/jquery-datatables/media/css/select.dataTables.css">

	<script type="text/javascript" language="javascript" src="./assets/vendor/jquery-datatables/media/js/jquery.dataTables.js"></script>
	<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/keytable/2.12.0/js/dataTables.keyTable.js"></script>
	<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/keytable/2.12.0/js/keyTable.dataTables.js"></script>
	<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/select/2.0.0/js/select.dataTables.js"></script>
	<style>
		.btn-close {
			background-color: transparent; /* Keep background transparent */
			border: 0;
			position: absolute;
			top: 10px;     /* Adjust top distance */
			right: 10px;   /* Adjust right distance */
			width: 1.5rem; /* Size of the button */
			height: 1.5rem;
			padding: 0;    /* Adjust padding if necessary */
			color: #000;   /* Icon color */
			opacity: 0.7;  /* Default opacity */
			cursor: pointer;
			transition: opacity 0.2s ease;
		}

		.dropdown-content {
			display: block;
			max-height: 500px;
			overflow-y: auto;
			position: relative;  /* Ensure it is the reference for sticky elements inside it */
		}

		.dropdown-content tfoot {
			position: sticky;
			bottom: 0;
			background-color: #fff; /* Set a background color for readability */
			z-index: 1; /* Ensure the footer is above other table rows */
		}

		.dropdown-content table {
			width: 100%;
			border-collapse: collapse;
		}

		.modal-body {
			max-height: calc(100vh - 200px); /* Set a max height for the modal body */
			overflow-y: auto;
			position: relative; /* Ensure proper containment for sticky elements */
		}

		.dropdown-content th, .dropdown-content td {
			padding: 8px;
			text-align: left;
			border-bottom: 1px solid #ddd;
		}

		.dropdown-content table thead th {
			background-color: #f4f4f4;
			position: sticky;
			top: 0;
		}

		.dropdown-content > table > tbody > tr:nth-of-type(odd) > td {
			background-color: #f9f9f9;
		}

		
		.dropdown-content .search-column, .dropdown-content #globalSearch {
			width: 100%;
			padding: 5px;
			margin: 5px 0;
			box-sizing: border-box;
		}

		.dropdown-content tr {
			cursor: pointer;
		}

		.dropdown-content tr:hover, .dropdown-content tr.active {
			background-color: #ddd;
		}

		.loading {
			text-align: center;
			padding: 10px;
			display: none;
		}

		.selected-items {
			margin-top: 10px;
			padding: 10px;
			background-color: #f9f9f9;
			border: 1px solid #ddd;
			max-height: 100px;
			overflow-y: auto;
		}

		.pill-label{
			display: block;
			float:left;
			// background-color: black;
			// color: white;
			border-radius: 16px;
			height: 25px;
			padding-left: 8px;
			padding-top: 2px;
			padding-right: 2px;
			margin-right:2px;
			margin-bottom:2px;
		}

		.pill-remove{
			display: block;
			float:right;
			// background: #d8d8d8;
			// color: white;
			border-radius: 21px;
			width: 21px;
			padding-left: 6px;
			cursor: pointer;
		}

		#pillContainerClear{
			display:none;
			margin-bottom:4px;
		}

		// Log modal style for sticky header
		#logTable thead {
			position: sticky;
			top: 0;
			z-index: 1; 
			 
			background-color: #f4f4f4;
			box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); 
		}

		#logModal .modal-body{
			padding: 0px 15px 15px 15px;
		}
	</style>
	';

//code array
//Types: 0=hidden 1=Text Box; 2=SelectDropDown 3=textbox 4=radio 5=Select-from-DB 6=date 7=number 8=Email 9=Checkbox-From-DB 10=MULTI-SELECT-from-DB 11=MULTI SELECT SEARCH from DB WITH MAPPING TABLE 12=Single Media Upload 13=Multiple Media Upload
//Image needs an extra folder name, Type allowed, max size allowed, single or multiple
function createInputs($arr,$FormID,$editID){

	global $k_debug;
	global $ddtableIncludes;
	$flds = "";

	// print_r($arr);

	
	
	if(1){ //BLOCK OTHER SETTINGS OF A FIELD
		if($arr[5] == 1){ //to check if required field
			$reqf = 'required';
			$reqv = '<span class="required"> *</span>';
		}
		else $reqf = $reqv = '';

		$divClass = "";
		$textLength="";
		
		// if($arr[21] == 1){ // Onload dynamic free fill data
		// 	if(strlen($arr[4]) == 0){
			
		// 		echo "<script>
		// 		window.addEventListener('load', function() {
		// 			dynamicFreeFill('".$arr[22]."');
		// 		}); </script>";
		// 		// $readOnly = "readonly";
		// 	}
		// }

		if($arr[9] != 0){ //to check text length size
			$textLength = "maxlength = $arr[9]";
		}

		$maxValue = "";
		if($arr[10] != 0){ //to check field max value
			$maxValue = "max = $arr[10]";
		}

		$minValue = "";
		if($arr[11] != 0){ //to check if field minimum value
			$minValue = "min = $arr[11]";
		}

		$readOnly = "";
		if($arr[12] != 0){ //to check if field readonly
			$readOnly = "readonly";
		}

		if($arr[13] == 0 ){  //to check visibility
			$divClass .= " hidden ";
		}

		//SIZES: col-md >= 992; col-sm >= 768; col-xs < 768 
		if($arr[16] !=0 ){	//Fields Size XS
			$divClass .= " col-xs-" . $arr[16] . " ";
		}

		if($arr[14] !=0 ){	//Field Size SM
			$divClass .= " col-sm-" . $arr[14] . " ";
		}

		if($arr[15] !=0 ){	//Fields Size MD
			$divClass .= " col-md-" . $arr[15] . " ";
		}

		if($arr[14] == 0 && $arr[15] == 0 && $arr[16] == 0 ){ //both the sizes are not set
			$divClass .= " col-md-3 ";
		}
		$textareaHeight = "";

		if($arr[20] != 0){ //to check textarea height
			$textareaHeight = "rows = $arr[20]";
		}

		if($arr[21] != 0){ //to check if field readonly
			$readOnly = "readonly";
		}

		$otherInputParams = $maxValue . " " . $minValue . " " . $textLength . " " . $readOnly;
		$formType = 1; // Added For Add More functionality
	}
	
	if($arr[1] == 0){ 	//hidden
		if(strlen($arr[31]) >= 1 && strlen($arr[4]) == 0){
			$arr[4] = $arr[31];
		}
		return "<input value='".$arr[4]."' type='hidden' ".$arr[6]." name='".$arr[0]."' id='".$arr[0]."' />";
	}

	// print_r($arr);
	// echo "<pre></pre>";

	if($arr[1] == 1){ 	//Textbox
		// global ${$arr[0]};
		
		
		if(strlen($arr[19]) < 2){	//CONDITION TO CHECK ShowTextBoxInGrid
			// if(isset(${$arr[0]})){
			// 	$arr[4] = ${$arr[0]};
			// }  

			//carry on field
			if(isset($_SESSION['carryOn'])){
				if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
					if(isset($_SESSION['carryOn'][$arr[0]])){
						$arr[4] = $_SESSION['carryOn'][$arr[0]];
						unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
					}
				}
			}

			echo "<script>
				$(document).ready(function(){
					if('".$arr[39]."' == 1){
						$('#".$arr[0]."').focus();
					}
				});
			</script>";

			//Set default value
			if(strlen($arr[31]) >= 1 && $arr[4] == ''){
				$arr[4] = $arr[31];
			}
			// print_r($arr);
			$flds = "<div class='".$divClass."'><label>".$arr[2].$reqv."</label><input ".$reqf." class='form-control dyn1' value='".$arr[4]."' type='text' ".$otherInputParams." ".$arr[6]." name='".$arr[0]."' id='".$arr[0]."' ";
			if(strlen($arr[33]) > 2){
				$flds .= "onfocusout=\"isValidText('".$arr[33]."','".$arr[0]."');\"";
			}
			if($arr[32] == 1){ //to check if field editable
				if($editID > 0){
					$flds .= "readonly";
				}
			}

			//Added for getting dynamic decimalplaces
			if($arr[44] != ''){
				$flds .=" data-key=\"".$arr[44]."\"";
			}

			//Check validation using sp on lost focus
			if($arr[35] == 1){
				$flds .= "onfocusout=\"validateOnLostFocusSp(this,0);\""; //0 is gridid in form it is set as 0
			}

			//On keyup focus exicute formula calculation
			$onkeyupparameterHidden = "";
			if(strlen($arr[40]) > 5){
				$flds .= "onkeyup=\"CreateOnkeyupScriptGrid(this,'form');\""; //0 is gridid in form it is set as 0
				$onkeyupparameterHidden = '<input type="hidden" id="'.$arr[0].'-onkeyup-hidden" value="'.$arr[40].'">';
			}

			//Check value is exist or not
			$onlostfocusHidden = "";
			if($arr[26] == 1){
				$flds .= "onfocusout=\"onLostFocus(this,$editID,'form');\"";
				$onlostfocusHidden = '<input type="hidden" id="'.$arr[0].'-hidden" value="'.$arr[27].'">';
			}
			
			//Once lost focus from perticular field then again don't allow to focus on this field
			if($arr[41] == 1){
				$flds .= "onblur=\"onBlurUpdateLostFocusTabIndex(this,$arr[1]);\"";
			}

			$flds .= " tabindex='".$arr[39]."'/>".$onlostfocusHidden.$onlostfocuscalculationHidden.$onkeyupparameterHidden;
			
			$flds .= "</div>";

			if($arr[39] == 0){
				echo "<script>
				$('#company').focus();
				</script>";
			}
			
			if($arr[43] == 1){
				echo "<script>
					$(document).ready(function(){
						$('#".$arr[0]."').keypress(function(event) { 
							if (event.which === 13) { 
								$('#".$arr[0]."').trigger('blur');
								setTimeout(function () {
                            		console.log('barcode entered....')
									$('#".$arr[0]."').val('');
									$('#".$arr[0]."').focus();
                        		}, 1000);
							}
						});
					});
				</script>";
			}
			return $flds;
		}else{
			return "<script>window.addEventListener('load', function() {

						$('#".$arr[19]."Footer').attr('name', '".$arr[0]."'); 

						$('#".$arr[19]."Footer').val($arr[4]);

					}); </script>";
		}
	}

	if($arr[1] == 2){ 	//SELECT
		global $radio;
		$r = $radio;
		$m = $arr;
		$p = $arr[4];

		$flds = '<div class="col-md-4"><label>'.$arr[2].$reqv.'</label><select '.$reqf.' name="'.$m[0].'" id="'.$m[0].'" class="form-control" '.$arr[6].' tabindex="'.$arr[39].'" ><option></option>';

		for($j = 0; $j < sizeof($r[$m[3]]); $j++){
			$k = $j+1;  //value
			if($p == $k) {
				$flds .= '<option selected value="'. $k .'" >'.$r[$m[3]][$j].'</option>&nbsp;';
			}
			else {
				$flds .= '<option value="'. $k .'">'.$r[$m[3]][$j].'</option>&nbsp;';
			}
		}

		$flds .= '</select></div>';
		return $flds;
	}

	if($arr[1] == 3){ 	//Textarea
		//carry on fields
		if(isset($_SESSION['carryOn'])){
			if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
				if(isset($_SESSION['carryOn'][$arr[0]])){
					$arr[4] = $_SESSION['carryOn'][$arr[0]];
					unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
				}
			}
		}   

		echo "<script>
				$(document).ready(function(){
					if('".$arr[39]."' == 1){
						$('#".$arr[0]."').focus();
					}
				});
			</script>";

	    // if(strcmp($arr[6], "maxlength=")) $maxChar = "maxlength='1000'";    //ADDED FOR VAPT 
	    // else $maxChar = '';

		//Set default value
		if(strlen($arr[31]) >= 1 && $arr[4] == ''){
			$arr[4] = $arr[31];
		}
		
	    if(extractAttribute($arr[8], "RichTextEditor") !== null){     //FOR ADD NEW FUNCTION added on 15062023 for react cap api
		    $flds .= "<div class='".$divClass."'><label>".$arr[2].$reqv."</label><textarea ".$reqf." ".$textareaHeight." class='form-control dyn1' ".$arr[6]." ".$otherInputParams." name='".$arr[0]."' id='".$arr[0]."' tabindex='".$arr[39]."' ";
			if(strlen($arr[33]) > 2){
				$flds .= "onfocusout=\"isValidText('".$arr[33]."','".$arr[0]."');\"";
			}

			if($arr[32] == 1){ //to check if field editable
				if($editID > 0){
					$flds .= "readonly";
				}
			}

			//Check validation using sp on lost focus
			if($arr[35] == 1){
				$flds .= "onfocusout=\"validateOnLostFocusSp(this,0);\"";  //0 is gridid in form it is set as 0
			}

			//Once lost focus from perticular field then again don't allow to focus on this field
			if($arr[41] == 1){
				$flds .= "onblur=\"onBlurUpdateLostFocusTabIndex(this,$arr[1]);\"";
			}

			$flds .= ">".htmlspecialchars($arr[4],ENT_QUOTES)."</textarea></div><script type='text/javascript'>CKEDITOR.replace(\"".$arr[0]."\", {height:'300px'});</script>";
			return $flds;
		}else{
		    $flds .= "<div class='".$divClass."'><label>".$arr[2].$reqv."</label><textarea ".$reqf." ".$textareaHeight." class='form-control dyn1' ".$arr[6]." ".$otherInputParams." name='".$arr[0]."' id='".$arr[0]."' tabindex='".$arr[39]."' ";
			if(strlen($arr[33]) > 2){
				$flds .= "onfocusout=\"isValidText('".$arr[33]."','".$arr[0]."');\"";
			}

			if($arr[32] == 1){ //to check if field editable
				if($editID > 0){
					$flds .= "readonly";
				}
			}

			//Check validation using sp on lost focus
			if($arr[35] == 1){
				$flds .= "onfocusout=\"validateOnLostFocusSp(this,0);\"";  //0 is gridid in form it is set as 0
			}

			//Once lost focus from perticular field then again don't allow to focus on this field
			if($arr[41] == 1){
				$flds .= "onblur=\"onBlurUpdateLostFocusTabIndex(this,$arr[1]);\"";
			}

			$flds .= ">".htmlspecialchars($arr[4],ENT_QUOTES)."</textarea></div>";
			return $flds;
		}
	}

	if($arr[1] == 4){ 	//Radio
		/*global $radio;
		$r = $radio;
		$m = $arr;
		$p = $arr[4];
		$flds = '<div class="col-md-4"><label>'.$arr[2].$reqv.'</label><br />';
		//print_r($r[$m[3]]);
		for($j = 0; $j < sizeof($r[$m[3]]); $j++){
			$k = $j+1;  //value
			if($p == $k) {
				$flds .= '<input type="radio" checked value="'. $k .'" name="'.$m[0].'" />&nbsp;'.$r[$m[3]][$j].'<br/>';
			}
			else {
				$flds .= '<input type="radio" value="'. $k .'" name="'.$m[0].'" />&nbsp;'.$r[$m[3]][$j].'<br/>';
			}
		}
		$flds .= '</div>';
		return $flds;*/

		global $extradb;
		$eresult = array();

		for($i = 0; $i < sizeof($extradb); $i++){
			if($extradb[$i][0] == $arr[3]){

				$edb = $extradb[$i][1];

				$eid = $extradb[$i][2];

				$elb = $extradb[$i][3];

				$end = checkVariable($extradb[$i][4]);

				$eval = array();
				
				$esql = "SELECT ".$eid.",".$elb." FROM ".$edb . " " . $end;

				if($k_debug) echo '<br/>'.$esql.'<br/>';

				$eresult = db::getInstance()->db_select($esql);

				if($k_debug){ echo '<br/>'; print_r($eresult); }

				break;

			}
		}

		if(sizeof($eresult) > 0){

			if(sizeof($eresult['result_set']) > 0){

				$m = $arr;
				$p = $arr[4];	

				// $flds = '<div class="'.$divClass.'" id="'.$arr[0].'-div"><label>'.$arr[2].$reqv.'</label>';

				$flds = '<div class="'.$divClass.'"><label>'.$arr[2].$reqv.'</label><br /><div id="'.$arr[0].'-div">';

				for($j = 0; $j < sizeof($eresult['result_set']); $j++){

					$k = $eresult['result_set'][$j][$eid];  //ID

					if($p == $k) { 

						$flds .= '<input '.$reqf.'  type="radio" '.$arr[6].' checked value="'. $k .'" name="'.$m[0].'" tabindex="'.$arr[39].'" ';

						if($arr[17] == 1){

							$onchange = " onchange=\"CreateOnchangeScript('" . $arr[18] . "', '".$m[0]."', '".$arr[1]."');\"";

							$flds .= $onchange;

						}

						if($arr[32] == 1){ //to check if field editable
							if($editID > 0){
								$flds .= "onclick=\"return false\"";
							}
						}

						$flds .= '/>&nbsp;'.$eresult['result_set'][$j][$elb].'<br/>';
					}
					else {

						$flds .= '<input '.$reqf.' type="radio" '.$arr[6].' value="'. $k .'" name="'.$m[0].'" tabindex="'.$arr[39].'" ';

						if($arr[17] == 1){

							$onchange = " onchange=\"CreateOnchangeScript('" . $arr[18] . "', '".$m[0]."', '".$arr[1]."');\"";

							$flds .= $onchange;

						}

						if($arr[32] == 1){ //to check if field editable
							if($editID > 0){
								$flds .= "onclick=\"return false\"";
							}
						}

						$flds .= '/>&nbsp;'.$eresult['result_set'][$j][$elb].'<br/>';
					}
				}
				$flds .= '</div>';
				return $flds;
			}else{
				return null;
			}
		}else{
			return null;
		}
	}

	if($arr[1] == 5){ 	//SELECT from DB
		global $extradb;		
		//carry on fields
		if(isset($_SESSION['carryOn'])){
			if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
				if(isset($_SESSION['carryOn'][$arr[0]])){
					$arr[4] = $_SESSION['carryOn'][$arr[0]];
					unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
				}
			}
		}

		$eresult = array();
		$m = $arr;
		$p = $arr[4];
		
		for($i = 0; $i < sizeof($extradb); $i++){
			if($extradb[$i][0] == $arr[3]){
				$edb = $extradb[$i][1];
				$eid = trim($extradb[$i][2]);
				$elb = trim($extradb[$i][3]);
				$end = $extradb[$i][4];
				
				// $end = checkVariable($extradb[$i][4]);
				$eval = array();
				if($p){
					//$esql = "SELECT ".$eid.",".$elb." FROM ".$edb . " " . $end;
					$esql = "SELECT ".$eid.",".$elb." FROM ".$edb;
					if(strlen($end) > 1){
						$esql .= " ".$end;
					}else{
						$esql .= " WHERE 1=1";
					}
					$esql .= " AND ".$eid ."=". $p ;

					if($k_debug) echo '<br/>'.$esql.'<br/>';

					$eresult = db::getInstance()->db_select($esql);

					if($k_debug){ echo '<br/>'; print_r($eresult); }
				}

				//Query for Default value
				if(strlen($arr[31]) > 0 ){
					$esql1 = "SELECT ".$eid.",".$elb." FROM ".$edb;
					if(strlen($end) > 1){
						$esql1 .= " ".$end;
					}else{
						$esql1 .= " WHERE 1=1";
					}
					$esql1 .= " AND ".$eid ."=". $arr[31];

					$eresult1 = db::getInstance()->db_select($esql1);
				}

				break;
			}
		}
		
		$flds = '<div class="'.$divClass.'"><label class="col-md-12x">'.$arr[2].$reqv.'</label>';
		
		if($arr[23] > 0){	
			$flds .= '<button type="button" id="AddBtnModal-'.$m[0].'"  value="'.$arr[23].'-'.$m[0].'-'.$formType.'" class="amodal" style="background: transparent;border: none !important;font-size:1;color:blue"><i class="fa fa-plus-circle"></i></button>';
		}
		
		echo "<script>
			$(document).ready(function(){

				// $('#".$m[0]."').on('select2:select', function (e) { // select option by enter then cursor going to next field
				// 	var self = $(this), form = self.parents('form:eq(0)'), focusable, next;
				// 	focusable = form.find('input,a,select,button,textarea').filter(':visible');
				// 	next = focusable.eq(focusable.index(this)+1);
				// 	if (next.length) {
				// 		next.focus();
				// 	} 
				// 	return false;
				// });
				
				//Once lost focus from perticular field then again don't allow to focus on this field
				if('".$arr[41]."' == 1){
					$('#".$m[0]."').on('select2:open select2:select', function (e) {
							// alert($('#".$m[0]."').find(':selected'));
							onBlurUpdateLostFocusTabIndex(this,'".$arr[1]."');
						
					});
				}

                if('".$arr[39]."' == 1){
					setTimeout(
						function() {
							console.log('Line 4547 Focus');
							$('#".$m[0]."').next('span').find('.select2-selection--single').focus();
						}, 300);
				}	
				
			});
		</script>";

		?>
		<script>
				if(<?php echo $arr[39]; ?> == 1){
					var tabindex1 = <?php echo $arr[39]; ?>;
					tabindex1++;
					// alert(tabindex1);
					console.log('Line 4561 Focus');
					$("[tabindex='"+tabindex1+"']").focus();
				}
			
		</script>

		<?php
		
			$DDSearchPreLoadedArray = [];
			$DDSearchPreLoadedData = '';
			if($edb != ''){
				$ddsql = "SELECT TOP 10 ".$elb." FROM ". $edb; //." WHERE 1=1 "
				if(strlen($end) > 1){
					$ddsql .= " ".$end;
				}
				
				$ddresult = db::getInstance()->db_select($ddsql);
				
				if($ddresult){
					for($i=0; $i < count($ddresult['result_set']); $i++){
						// echo $ddresult['result_set'][$i][$elb];
						array_push($DDSearchPreLoadedArray,trim($ddresult['result_set'][$i][$elb]));
					}

					$DDSearchPreLoadedData = implode(',',$DDSearchPreLoadedArray);
					
					echo "<script>
						$(document).ready(function(){
					        
							var finalParams = ['".$eid."','".$elb."'];
							var DDSearchMinInputLen = [];

							if('".$arr[29]."' > 0){
								DDSearchMinInputLen['".$m[0]."'] = '".$arr[29]."';
							}else{
								DDSearchMinInputLen['".$m[0]."'] = 0;
							}
					
							var DDSearchPreLoadedData = [];
							var DDSearchPreLoadedDataString = [];
				
							DDSearchPreLoadedData['".$m[0]."'] = ".json_encode($DDSearchPreLoadedData).";
							if(DDSearchPreLoadedData['".$m[0]."'].length > 0){
								DDSearchPreLoadedDataString['".$m[0]."'] = ' or more char (like: '+ DDSearchPreLoadedData['".$m[0]."'] +')';
							}else{
								DDSearchPreLoadedDataString['".$m[0]."'] = ' char ';
							}						
							// // Automatically open and focus on search when the field gets focus
							// $('.godownid div span span span').on('focus', function () {
							// 	console.log('Focussed on GD');
							// 	$(this).select2('open');
							// });


							// $(document).on('focus', '.select2-selection.select2-selection--single', function (e) {
							//   // Trigger dropdown opening
							//   $(this).closest('.select2-container').prev('select').select2('open');
							// });

							// // Prevent immediate close when focusing
							// $('select').on('select2:opening', function(e) {
							//   $(this).data('is_opening', true);
							// });
							// $('select').on('select2:closing', function(e) {
							//   if ($(this).data('is_opening')) {
							//     e.preventDefault();
							//     $(this).data('is_opening', false);
							//   }
							// });




							// $(document).on('focus', '.select2-selection.select2-selection--single', function (e) {
							//   const sel = $(this).closest('.select2-container').prev('select');
							//   if (!sel.data('select2-open')) {
							//     sel.select2('open');
							//   }
							// });

							// // Track dropdown open/close state
							// $('select').on('select2:opening', function(e) {
							//   $(this).data('select2-open', true);
							// });
							// $('select').on('select2:closing', function(e) {
							//   $(this).data('select2-open', false);
							// });

							// // Close dropdown after selection
							// $('select').on('select2:select', function(e) {
							//   $(this).select2('close');
							// });





							// // Open Select2 dropdown on focus for respective elements
							// $(document).on('focus', '.select2-selection.select2-selection--single', function () {
							//   const sel = $(this).closest('.select2-container').prev('select');
							//   if (!sel.data('select2-open')) {
							//     sel.select2('open');
							//   }
							// });

							// Track dropdown open/close state for respective elements
							// $('#".$m[0]."').on('select2:opening', function () {
							//   $(this).data('select2-open', true);
							// });

							// $('#".$m[0]."').on('select2:closing', function (e) {
							//   if ($(this).data('select2-open')) {
							//     e.preventDefault();
							//     $(this).data('select2-open', false);
							//   }
							// });

							// // Close dropdown on selection for respective elements
							// $('#".$m[0]."').on('select2:select', function () {
							//   $(this).select2('close');
							// });




							$('#".$m[0]."').select2({

								//tags: true,
								// multiple: true,
								tokenSeparators: [','],
									debug: true,
									placeholder: 'select',
								//allowClear: true,
								minimumInputLength: DDSearchMinInputLen['".$m[0]."'],
								language: {
									inputTooShort: function() {
										return 'Enter ' + DDSearchMinInputLen['".$m[0]."'] + ' '+ DDSearchPreLoadedDataString['".$m[0]."'] ;
									}
								},
								//minimumResultsForSearch: 10,
								ajax: {
									url: 'getDropDownValue.php',
									dataType: 'json',
									type: 'POST',
									data: function (params) {
										var queryParameters = {
										searchText: encodeURIComponent(params.term)
										,searchParam:'".$elb."'
										,fromRepo:'".$edb."'
										,repoCondition:'".$end."'
										,responseParams:finalParams.toString()
										,searchText1: $('form').serialize()
										,currentId:'".$m[0]."'
										,gridSerial:''
										,FormID:'".$FormID."'
										,GridID:0
										
									}
									return queryParameters;
								},
								processResults: function (data) {
									if(data['data'].length == 0){
										$('#".$m[0]."').val('');//.trigger('change')
									}
									return {
										results: $.map(data['data'], function (item) {
											$('#".$m[0]."').next('span').find('.select2-selection--single').css('border-color','#ccc');
											$('#".$m[0]."').next('span').find('.select2-selection--multiple').css('border-color','#ccc');
											return {
												// $('#".$m[0]."').select2('open');
												text: item.ddVal,	
												id: item.ddID
												// $('#".$m[0]."').select2('close');
											}
										})
									};
								}
							}

						});
					
					});

					
					</script>";
				}
			}
				
			
		$flds .= '<select '.$reqf.'   name="'.$m[0].'" id="'.$m[0].'" class="form-control populate" style="width:100%" tabindex="'.$arr[39].'" ';

		//Check validation using sp onChange
		if($arr[36] == 1){
			$flds .= " onchange=\"validateOnChangeSp(this,0);\""; //0 is gridid in form it is set as 0
		}

		if($arr[17] == 1){
			$onchange = " onchange=\"CreateOnchangeScript(this, '".$arr[1]."');\"";
			$flds .= $onchange;
			$onchangeHidden = '<input type="hidden" id="'.$m[0].'-hidden" value="' . $arr[18] . '" />';
		}

		if($arr[32] == 1){ //to check if field editable
			if($editID > 0){
				$flds .= " style=\"pointers-event:none;\"";
			}
		}

		//Check validation using sp on lost focus
		if($arr[35] == 1){
			$flds .= "onchange=\"validateOnLostFocusSp(this,0);\"";  //0 is gridid in form it is set as 0
		}

		$flds .= '><option value=""></option>'; 

		//Default value
		if(strlen($arr[31]) > 0){
			for($j = 0; $j < sizeof($eresult1['result_set']); $j++){
				$flds .= '<option  value="'. $eresult1['result_set'][$j][trim($eid)] .'" selected>'.$eresult1['result_set'][$j][$elb].'</option>&nbsp;';
			}
		}

		if(sizeof($eresult) > 0){
			
			if(sizeof($eresult['result_set']) > 0){
				
				for($j = 0; $j < sizeof($eresult['result_set']); $j++){
					$k = $eresult['result_set'][$j][$eid];  //ID	

					if($p == $k) {
						$flds .= '<option selected value="'. $k .'" >'.$eresult['result_set'][$j][$elb].'</option>&nbsp;';
					}
				}
			}
		}

		$flds .= '</select>'.$onchangeHidden.'</div>';

		if($arr[12] == 1){ //to check if field readonly
			echo '<script>
				// document.addEventListener("DOMContentLoaded", function() {
				// alert("Page Loaded!");
					setTimeout(
						function() {
							$("#'.$m[0].'").next("span").find(".select2-selection--single").css("pointer-events","none").css("background","#eee");
						}, 2000);
				// });
			</script>';
		}
		
		return $flds;

		
	}

	?>

	<?php

	if($arr[1] == 6){ 	//Date
		//carry on fields
		if(isset($_SESSION['carryOn'])){
			if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
				if(isset($_SESSION['carryOn'][$arr[0]])){
					$arr[4] = $_SESSION['carryOn'][$arr[0]];
					unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
				}
			}
		}

		echo "<script>
			$(document).ready(function(){
				if('".$arr[39]."' == 1){
					$('#".$arr[0]."').focus();
				}
			});
		</script>";

		// if($arr[4] != NULL){		
		// 	if (gettype($arr[4]) == "string"){
		// 		$arr[4] =  date("d-m-Y", strtotime($arr[4]));
		// 	}
		// }
		/*
			FOR DateValidation add in Other Attributes
			Example: DateValidation="From=YearCode_Start&To=YearCode_End"
			Values Allowed:
				YearCode_Start
				YearCode_End
				today
				tomorrow
				yesterday
				2024-01-11 (value as it is)
		*/
		$date_max = '';
		$date_min = '';

		if(extractAttribute($arr[8], "DateValidation") !== null ){     
			$DateValidation = extractAttribute($arr[8], "DateValidation");
		} elseif (substr($arr[0], 0, 3) === 'ent') {
			//elseif ($arr[0] === 'entdt' || $arr[0] === 'entry_dt') yy
			// Default DateValidation string if $arr[0] is 'entdt'
			$DateValidation = "From=YearCode_Start&To=YearCode_End";
		}

		if(isset($DateValidation)){ 
			  
			// $DateValidation = extractAttribute($arr[8], "DateValidation");
			$DateValidationArr = explode('&', $DateValidation); 
			$sql = "select sdate,edate from AiYear where Label = '" . $_SESSION['dbYear'] . "'";
			$result = db::getInstanceMaster()->db_select($sql);
			// if($FormID == 3010)	print_r($result);
			$yearStartDate = $result['result_set'][0]['sdate'];
			$yearStartDate = $yearStartDate->format('Y-m-d');
			// if($FormID == 3010)	echo $yearStartDate;
			$yearEndDate = $result['result_set'][0]['edate'];
			$yearEndDate = $yearEndDate->format('Y-m-d');
			// if($FormID == 3010)	echo $yearEndDate;
			foreach ($DateValidationArr as $pair) { 
				list($key, $value) = explode('=', $pair); 
				// if($FormID == 2644)	echo $key . ',' . $value;
				if (strtolower($key) === 'from') { 
					$date_min = $value;
					if(strtolower($date_min) == 'yearcode_start'){
						$date_min = $yearStartDate;
					}
					if(strtolower($date_min) == 'yearcode_end'){
						$date_min = $yearEndDate;
					}
					if(strtolower($date_min) == 'today'){
						$date_min = date("Y-m-d");
					}
					if(strtolower($date_min) == 'tomorrow'){
						$date_min = date("Y-m-d",strtotime("tomorrow"));
					}
					if(strtolower($date_min) == 'yesterday'){
						$date_min = date("Y-m-d",strtotime("yesterday"));
					}
				} elseif (strtolower($key) === 'to') { 
					$date_max = $value; 
					if(strtolower($date_max) == 'yearcode_start'){
						$date_max = $yearStartDate;
					}
					if(strtolower($date_max) == 'yearcode_end'){
						$date_max = $yearEndDate;
					}
					if(strtolower($date_max) == 'today'){
						$date_max = date("Y-m-d");
					}
					if(strtolower($date_max) == 'tomorrow'){
						// $datetime = new DateTime('tomorrow');
						// $date_max = $datetime->format('Y-m-d H:i:s');
						$date_max = date("Y-m-d",strtotime("tomorrow"));
					}
					if(strtolower($date_max) == 'yesterday'){
						// $tmp = new DateTime(); 
						// $tmp->modify('-1 day');
						$date_max = date("Y-m-d",strtotime("yesterday"));
					}
				} 
			}
		}

		if(strlen($arr[31]) >= 1 && $arr[31] == 'today' && $arr[4] == ''){
			// Check if $arr[4] is between $date_min and $date_max
			$tempDate = date('Y-m-d');
			
			if ($tempDate >= $date_min && $tempDate <= $date_max) {
				// $arr[4] is between $date_min and $date_max
				$arr[4] = $tempDate;
			}else{
				$arr[4] = $tempDate;
			}
			
		}elseif(strlen($arr[31]) >= 1 && $arr[31] == 'tomorrow' && $arr[4] == ''){
			$datetime = new DateTime('tomorrow');
			$tempDate = $datetime->format('Y-m-d');
			if ($tempDate >= $date_min && $tempDate <= $date_max) {
				// $arr[4] is between $date_min and $date_max
				$arr[4] = $tempDate;
			}
		}elseif($arr[4] != NULL ){
			if(gettype($arr[4]) == 'string'){
				$date = strtotime($arr[4]);
				$arr[4] = date('Y-m-d', $date);
			}else{
				$arr[4] = $arr[4]->format('Y-m-d');
			}
		}

		// echo $arr[4];
		// $newDate = DateTime::createFromFormat("l dS F Y", $arr[4]);
		// $arr[4] = $newDate->format('d/m/Y'); // for example

		$flds .= "<div class='".$divClass."'><label>".$arr[2].$reqv."</label><input ".$reqf." min='".$date_min."' max='".$date_max."' maxlength='4' pattern='\d{4}' class='form-control dyn1' value='".trim($arr[4])."' type='date'  ".$arr[6]." ".$otherInputParams." name='".$arr[0]."' id='".$arr[0]."' tabindex='".$arr[39]."'  ";
		if($arr[32] == 1){ //to check if field editable
			if($editID > 0){
				$flds .= "readonly";
			}
		}

		//Check validation using sp on lost focus
		if($arr[35] == 1){
			$flds .= "onfocusout=\"validateOnLostFocusSp(this,0);\"";  //0 is gridid in form it is set as 0
		}

		//Once lost focus from perticular field then again don't allow to focus on this field
		if($arr[41] == 1){
			$flds .= "onblur=\"onBlurUpdateLostFocusTabIndex(this,$arr[1]);\"";
		}
		
		$flds .="/></div>";
		return $flds;
	}

	if($arr[1] == 7){ 	//Number
		
		//carry on fields
		if(isset($_SESSION['carryOn'])){
			if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
				if(isset($_SESSION['carryOn'][$arr[0]])){
					$arr[4] = $_SESSION['carryOn'][$arr[0]];
					unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
				}
			}
		}

		echo "<script>
			$(document).ready(function(){
				if('".$arr[39]."' == 1){
					$('#".$arr[0]."').focus();
				}
			});
		</script>";

		//Set default value
		if(strlen($arr[31]) >= 1 && $arr[4] == ''){
			$arr[4] = $arr[31];
		}

	    // if(strcmp($arr[6], "maxlength=")) $maxChar = "maxlength='100'";    //ADDED FOR VAPT 
	    // else $maxChar = '';
		$flds .= "<div class='".$divClass."'><label>".$arr[2].$reqv."</label><input ".$reqf." class='form-control dyn1' value='".$arr[4]."' type='number' ".$arr[6]." ".$otherInputParams." name='".$arr[0]."' id='".$arr[0]."' tabindex='".$arr[39]."'  ";
		
		if($arr[6] > 2){
			if(strpos($arr[6], 'step') !== false){
				$flds .= "onfocusout=\"isValidDigit('".$arr[6]."','".$arr[0]."');\"";
			}
		}
		

		if(strlen($arr[33]) > 2){
			$flds .= "onfocusout=\"isValidText('".$arr[33]."','".$arr[0]."');\"";
		}

		if($arr[32] == 1){ //to check if field editable
			if($editID > 0){
				$flds .= "readonly";
			}
		}

		//Added for getting dynamic decimalplaces
		if($arr[44] != ''){
			$flds .=" data-key=\"".$arr[44]."\"";
		}

		//Check validation using sp on lost focus
		if($arr[35] == 1){
			$flds .= "onfocusout=\"validateOnLostFocusSp(this,0);\"";  //0 is gridid in form it is set as 0
		}

		//On keyup focus exicute formula calculation
		$onkeyupparameterHidden = "";
		if(strlen($arr[40]) > 5){
			$flds .= "onkeyup=\"CreateOnkeyupScriptGrid(this,'form');\""; //0 is gridid in form it is set as 0
			$onkeyupparameterHidden = '<input type="hidden" id="'.$arr[0].'-onkeyup-hidden" value="'.$arr[40].'">';
		}

		//Once lost focus from perticular field then again don't allow to focus on this field
		if($arr[41] == 1){
			$flds .= "onblur=\"onBlurUpdateLostFocusTabIndex(this,$arr[1]);\"";
		}

		$flds .= "/>".$onkeyupparameterHidden."</div>";
		?>
		<script>
			$(document).ready(function(){
				document.getElementById(<?php echo "'".$arr[0]."'"; ?>).addEventListener('focus', function(event) {
					// Trigger selection on focus if Tab or Enter was used
					this.select();
				});
			});
		</script>
		<?php
		return $flds;
	}

	if($arr[1] == 8){ 	//Email
		//carry on fields
		if(isset($_SESSION['carryOn'])){
			if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
				if(isset($_SESSION['carryOn'][$arr[0]])){
					$arr[4] = $_SESSION['carryOn'][$arr[0]];
					unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
				}
			}
		}

		echo "<script>
			$(document).ready(function(){
				if('".$arr[39]."' == 1){
					$('#".$arr[0]."').focus();
				}
			});
		</script>";

		//Set default value
		if(strlen($arr[31]) >= 1 && $arr[4] == ''){
			$arr[4] = $arr[31];
		}

		$flds .= "<div class='".$divClass."'><label>".$arr[2].$reqv."</label><input ".$reqf." class='form-control dyn1' ".$otherInputParams." value='".$arr[4]."' type='email' name='".$arr[0]."' id='".$arr[0]."'tabindex='".$arr[39]."'  ";
		if(strlen($arr[33]) > 2){
			$flds .= "onfocusout=\"isValidText('".$arr[33]."','".$arr[0]."');\"";
		}

		if($arr[32] == 1){ //to check if field editable
			if($editID > 0){
				$flds .= "readonly";
			}
		}

		//Check validation using sp on lost focus
		if($arr[35] == 1){
			$flds .= "onfocusout=\"validateOnLostFocusSp(this,0);\"";  //0 is gridid in form it is set as 0
		}

		//Once lost focus from perticular field then again don't allow to focus on this field
		if($arr[41] == 1){
			$flds .= "onblur=\"onBlurUpdateLostFocusTabIndex(this,$arr[1]);\"";
		}

		$flds .= "/></div>";
		return $flds;
	}

	if($arr[1] == 9){ 	//Checkbox-From-DB
		global $extradb;
		$eresult = array();
		for($i = 0; $i < sizeof($extradb); $i++){
			if($extradb[$i][0] == $arr[3]){
				// print_r($extradb);
				$edb = $extradb[$i][1];
				$eid = $extradb[$i][2];
				$elb = $extradb[$i][3];
				$end = $extradb[$i][4];
				$emnydb = $extradb[$i][6];
				$emnyid = $extradb[$i][7];
				$emnyfk = $extradb[$i][8];
				$eval = array();
				$esql = "SELECT ".$eid.",".$elb." FROM ".$edb . " " . $end;

				if($k_debug) echo '<br/>'.$esql.'<br/>';
				$eresult = db::getInstance()->db_select($esql);
				if($k_debug){ echo '<br/>'; print_r($eresult); }
				break;
			}
		}

		if(sizeof($eresult) > 0){
			if(sizeof($eresult['result_set']) > 0){
				$m = $arr;
				$p = $arr[4];
				// print_r($p);
				$addNewFn = "";

    		if(extractAttribute($arr[8], "AddNewFn") !== null){     //FOR ADD NEW FUNCTION added on 23052023 for react cap api
    		    $AddNewFnName = extractAttribute($arr[8], "AddNewFn");
    		    $addNewFn = '<a style="padding:3px;" href="javascript:void(0)" class="btn btn-success" onclick="' . $AddNewFnName . '()">
    				<span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span></a>';
    		} 		

				$flds = '<div class="'.$divClass.'"><label>'.$arr[2].$reqv.'</label>'.$addNewFn.'<br/><div class="multi-check" id="'.$arr[0].'-multi-div">';

				if(extractAttribute($arr[8], "TreeCheckbox") !== null){     //FOR ADD NEW FUNCTION added on 23052023 for react cap api
        		  //  $AddNewFnName = extractAttribute($arr[8], "TreeCheckbox");
        		  //  for($j = 0; $j < sizeof($eresult['result_set']); $j++){
    				// 	$k = $eresult['result_set'][$j][$eid];  //ID
    					/*[
					{title: "n1", expanded: true, key: "1", children: [
						{title: "n1.1 (selected)", key: "1.1"},
						{title: "n1.2", key: "1.2"},
						{title: "n1.3", key: "1.3"}
					]},
					{title: "n2", expanded: true, key: "2", children: [
						{title: "n2.1 (selected)", selected: true, key: "2.1"},
						{title: "n2.2", selected: false, key: "2.2"},
						{title: "n2.3", selected: null, key: "2.3"}
					]},
					{title: "n3", expanded: true, key: "3", children: [
						{title: "n3.1", expanded: true, key: "3.1", children: [
							{title: "n3.1.1 (unselectable)", unselectable: true, key: "3.1.1"},
							{title: "n3.1.2 (unselectable)", unselectable: true, key: "3.1.2"},
							{title: "n3.1.3", key: "3.1.3"}
						]},

						{title: "n3.2", expanded: true, key: "3.2", children: [
							{title: "n3.2.1 (unselectableStatus: true)", unselectableStatus: true, key: "3.2.1"},
							{title: "n3.2.2 (unselectableStatus: false)", unselectableStatus: false, key: "3.2.3"},
							{title: "n3.2.3", key: "3.2.3"}
						]},
						{title: "n3.3", expanded: true, key: "3.3", children: [
							{title: "n3.3.1 (unselectableStatus: true, unselectableIgnore)",
									unselectableStatus: true,  unselectableIgnore: true, key: "3.3.1"},
							{title: "n3.3.2 (unselectableStatus: false, unselectableIgnore)",
									unselectableStatus: false, unselectableIgnore: true, key: "3.3.2"},
							{title: "n3.3.3", key: "3.3.3"}
						]}
					]}
					]*/
    				// 	if($p == $k) {
    				// 		$flds .= '<label><input type="checkbox" checked '.$arr[6].' name="'.$m[0].'" id="'.$m[0].'" value="'. $k .'" >&nbsp;'.$eresult['result_set'][$j][$elb].'</label><br/>';
    				// 	}
    				// 	else { //CHANGED ON 03022020
    				// 		$flds .= '<label><input type="checkbox" '.$arr[6].' name="'.$m[0].'" id="'.$m[0].'" value="'. $k .'" >&nbsp;'.$eresult['result_set'][$j][$elb].'</label><br/>';
    				// 	}
    				// }

					if(1){
						$flds .= '
							<script src="//code.jquery.com/jquery-3.6.0.min.js"></script>
							<script src="//code.jquery.com/ui/1.13.0/jquery-ui.min.js"></script>
							<link href="fancytree/src/skin-win8/ui.fancytree.css" rel="stylesheet">
							<script src="fancytree/src/jquery.fancytree.js"></script>
							<link href="fancytree/lib/prettify.css" rel="stylesheet">
							<script src="fancytree/lib/prettify.js"></script>
							<script src="fancytree/src/jquery.fancytree.filter.js"></script>
							<style type="text/css">
								p.warning,
							p.info,
							div.info {
								font-size: small;
								background-color: #fff3cd;
								background-image: url(../doc/iconInfo_32x32.png);
								background-repeat: no-repeat;
								padding: 5px;
								padding-left: 40px;
								min-height: 25px;
							}
							.sampleButtonContainer
							{
								margin-right: 10px;
							}
							.sampleButtonContainer a
							{
								color: #212529;
								text-decoration: undereline;
								text-decoration-style: dotted;
								padding: 1px 3px;
								font-size: 70%;
							}
							.sampleButtonContainer button
							{
								margin-bottom: 3px;
							}
							p#sampleButtons h5
							{
								font-size: 9pt;
								margin-top: 3px;
								margin-bottom: 3px;
								font-weight: bold;
							}
							.description a,
							.description a:hover,
							.description a:visited,
							p.sample-links a,
							p.sample-links a:hover,
							p.sample-links a:visited
							{
								color: #212529;
							/* text-decoration: none; */
							text-decoration-style: dotted;
							/* font-weight: 600; */
							}

							.description a:hover,
							p.sample-links a:hover
							{
								text-decoration: underline;
							}
							p.sample-links a,
							p.sample-links a:hover,
							p.sample-links a:visited
							{
								margin-left: 15px;
								padding: 1px 3px;
								font-size: small;
							}
							pre{
							background:#f7f7f7;
							border:1px solid #999;
							padding:.5em;
							margin:.5em;
							font-size:.9em;
							}

							body.example button {
								display: inline-block;
								font-weight: 400;
								color: #212529;
								text-align: center;
								vertical-align: middle;
								-webkit-user-select: none;
								-moz-user-select: none;
								-ms-user-select: none;
								user-select: none;
								background-color: transparent;
								border: 1px solid transparent;
								padding: .375rem .75rem;
								font-size: 1rem;
								line-height: 1.5;
								border-radius: .25rem;
								transition: color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out;
								/* small: */
								padding: .15rem .3rem;
								font-size: .675rem;
								line-height: 1.2;
								border-radius: .2rem;
								/* .btn-secondary */
								color: #fff;
								background-color: #6c757d;
								border-color: #6c757d;
							}
							[type=button]:not(:disabled), [type=reset]:not(:disabled), [type=submit]:not(:disabled), button:not(:disabled) {
							cursor: pointer;
							}

							body.example button:hover {
								text-decoration: none;
								/* .btn-secondary */
								background-color: #5a6268;
								border-color: #545b62;
							}

							body.example button:focus {
								box-shadow: 0 0 0 0.2rem rgba(130,138,145,.5);
							}

							body.example input[type=submit],
							body.example button[type=submit] {
								/* .btn-primary */
								color: #fff;
								background-color: #007bff;
								border-color: #007bff;
							}

							body.example input[type=submit]:hover,
							body.example button[type=submit]:hover {
								/* .btn-primary */
							background-color: #0069d9;
							border-color: #0062cc;
							}

							ul.fancytree-container {
								margin: 4px; /* leave some room for the safari focus border */
							}

							iframe.embedded-plunkr {
								border: 1px solid #f7f7f7;
								width: 100%;
								height: 500px;
							}

							iframe.embedded-jsbin {
								border: 1px solid #f7f7f7;
								width: 100%;
								height: 500px;
							}

							</style>

							<script type="text/javascript">
								(function ($) {
								var PLUGIN_NAME = "skinswitcher",
									defaultOptions = {
										base: "",
										choices: [],
										// extraChoices: [],
										// Events:
										change: $.noop,
									},
									methods = {
										init: function (options) {
											var i,
												opts = $.extend({}, defaultOptions, options),
												hrefs = [],
												$link = null,
												initialChoice;
											if (!this.length) {
												return this;
											}

											// Attach options to skinswitcher combobox for later access
											this.data("options", opts);
											// Find the <link> tag that is used to includes our skin CSS.
											// Add a class for later access.
											$.each(opts.choices, function () {
												hrefs.push(this.href.toLowerCase());
											});

											$("head link").each(function () {
												for (i = 0; i < hrefs.length; i++) {
													if (this.href.toLowerCase().indexOf(hrefs[i]) >= 0) {
														$link = $(this);
														$link.addClass(PLUGIN_NAME);
														initialChoice = opts.choices[i];
													}
												}
											});
											if (!$link) {
												$link = $("link." + PLUGIN_NAME);
											}

											if (!$link.length) {
												$.error(
													"Unable to find <link> tag for skinswitcher. Either set `href` to a known skin url or add a `skinswitcher` class."
												);
											}
											//
											return this.each(function () {
												// Add options to dropdown list
												var $combo = $(this);
												$combo
													.empty()
													.skinswitcher("addChoices", opts.choices)
													.change(function (event) {
														var choice = $(":selected", this).data("choice");
														$("link." + PLUGIN_NAME).attr(
															"href",
															opts.base + choice.href
														);
														opts.change(choice);
													});
												// Find out initial selection
												if (opts.init) {
													$combo.val(opts.init).change();
												} else if (initialChoice) {
													// select combobox value to match current <link> tag
													// decouple this call to prevent IE6 exception
													setTimeout(function () {
														$combo.val(initialChoice.value);
														opts.change(initialChoice);
													}, 100);
												}
											});
										},
										option: function (name, value) {
											var opts = this.data("options");
											if (typeof value !== "undefined") {
												opts[name] = value;
												return this;
											}
											return opts[name];
										},
										addChoices: function (choices) {
											var $combo = $(this);
											if ($.isPlainObject(choices)) {
												choices = [choices];
											}
											$.each(choices, function (i, choice) {
												var $opt = $("<option>", {
													text: choice.name,
													value: choice.value,
												}).data("choice", choice);
												$combo.append($opt);
											});
											return this;
										},
										change: function (value) {
											$(this).val(value).change();
											return this;
										},
										reset: function () {
											$(this).val("").change();
											return this;
										},
									};

								$.fn[PLUGIN_NAME] = function (method) {
									// Method calling logic
									if (methods[method]) {
										return methods[method].apply(
											this,
											Array.prototype.slice.call(arguments, 1)
										);
									} else if (typeof method === "object" || !method) {
										return methods.init.apply(this, arguments);
									}
									$.error(
										"Method " + method + " does not exist on jQuery." + PLUGIN_NAME
									);
								};
							})(jQuery);

							/**
							* Replacement for $().toggle(func1, func2), which was deprecated with jQuery 1.8
							* and removed in 1.9.;
							* Taken from http://stackoverflow.com/a/4911660/19166
							* By Felix Kling
							*/
							(function ($) {
								var SAMPLE_BUTTON_DEFAULTS = {
									id: undefined,
									label: "Sample",
									newline: true,
									code: function () {
										alert("not implemented");
									},
								};
								$.fn.clickToggle = function (func1, func2) {
									var funcs = [func1, func2];
									this.data("toggleclicked", 0);
									this.click(function () {
										var data = $(this).data(),
										tc = data.toggleclicked;
										$.proxy(funcs[tc], this)();
										data.toggleclicked = (tc + 1) % 2;
									});
									return this;
								};

								window.getUrlVars = function () {
									var vars = {};
									window.location.href.replace(
										/[?&]+([^=&]+)=([^&]*)/gi,
										function (m, key, value) {
											vars[key] = value;
										}
									);
									return vars;
								};
								window.getUrlParam = function (parameter, defaultvalue) {
									var urlparameter = defaultvalue;
									if (window.location.href.indexOf(parameter) > -1) {
										urlparameter = window.getUrlVars()[parameter];
									}
									return urlparameter;
								};
								window.addSampleButton = function (options) {
									var sourceCode,
										opts = $.extend({}, SAMPLE_BUTTON_DEFAULTS, options),
										$buttonBar = $("#sampleButtons"),
										$container = $("<span />", {
											class: "sampleButtonContainer",
										});

									$("<button />", {
										id: opts.id,
										title: opts.tooltip,
										text: opts.label,
									})
										.click(function (e) {
											e.preventDefault();
											opts.code();
										})
										.appendTo($container);
									$("<a />", {
										text: "Source code",
										href: "#",
										class: "showCode",
									})
										.appendTo($container)
										.click(function (e) {
											try {
												prettyPrint();
											} catch (e2) {
												alert(e2);
											}
											var $pre = $container.find("pre");
											if ($pre.is(":visible")) {
												$(this).text("Source code");
											} else {
												$(this).text("Hide source");
											}
											$pre.toggle("slow");
											return false;
										});
									sourceCode = "" + opts.code;
									// Remove outer function(){ CODE }
									//    sourceCode = sourceCode.match(/[]\{(.*)\}/);
									sourceCode = sourceCode.substring(
										sourceCode.indexOf("{") + 1,
										sourceCode.lastIndexOf("}")
									);
									//    sourceCode = $.trim(sourceCode);
									// Reduce tabs from 8 to 2 characters
									sourceCode = sourceCode.replace(/\t/g, "  ");
									// Format code samples
									$("<pre />", {
										text: sourceCode,
										class: "prettyprint",
									})
										.hide()
										.appendTo($container);
									if (opts.newline) {
										$container.append($("<br />"));
									}
									if (opts.header) {
										$("<h5 />", { text: opts.header }).appendTo($("p#sampleButtons"));
									}
									if (!$("#sampleButtons").length) {
										$.error(
											"addSampleButton() needs a container with id #sampleButtons"
										);
									}
									$container.appendTo($buttonBar);
								};

								function initCodeSamples() {
									var info,
										$source = $("#sourceCode");

										$("#codeExample").clickToggle(
										function () {
											$source.show("fast");
											if (!this.old) {
												this.old = $(this).html();
												$.get(
													this.href,
													function (code) {
														// Remove <!-- Start_Exclude [...] End_Exclude --> blocks:
														code = code.replace(
															/<!-- Start_Exclude(.|\n|\r)*?End_Exclude -->/gi,
															"<!-- (Irrelevant source removed.) -->"
														);

														// Reduce tabs from 8 to 2 characters
														code = code.replace(/\t/g, "  ");
														$source.text(code);
														// Format code samples
														try {
															prettyPrint();
														} catch (e) {
															alert(e);
														}
													},
													"html"
												);
											}
											$(this).html("Hide source code");
										},

										function () {
											$(this).html(this.old);
											$source.hide("fast");
										}
									);
									if (jQuery.ui) {
										info =
											"Fancytree " +
											jQuery.ui.fancytree.version +
											", jQuery UI " +
											jQuery.ui.version +
											", jQuery " +
											jQuery.fn.jquery;
										$("p.sample-links").after(
											"<p class=\'version-info\'>" + info + "</p>"
										);
									}
								}

								$(function () {
									if (top.location === window.location) {
										$(".hideOutsideFS").hide();
									} else {
										$(".hideInsideFS").hide();
									}
									initCodeSamples();
									$("select#skinswitcher")
										.skinswitcher({
											base: "fancytree/src/",
											choices: [
												{
													name: "XP",
													value: "xp",
													href: "skin-xp/ui.fancytree.css",
												},
												{
													name: "Vista (classic Dynatree)",
													value: "vista",
													href: "skin-vista/ui.fancytree.css",
												},
												{
													name: "Win7",
													value: "win7",
													href: "skin-win7/ui.fancytree.css",
												},
												{
													name: "Win8",
													value: "win8",
													href: "skin-win8/ui.fancytree.css",
												},
												{
													name: "Win8-N",
													value: "win8n",
													href: "skin-win8-n/ui.fancytree.css",
												},
												{
													name: "Win8 xxl",
													value: "win8xxl",
													href: "skin-win8-xxl/ui.fancytree.css",
												},
												{
													name: "Lion",
													value: "lion",
													href: "skin-lion/ui.fancytree.css",
												},
											],
											change: function (choice) {
												$("#connectorsSwitch").toggle(choice.value !== "xp");
											},
										})
										.after($("<label id=\'connectorsSwitch\'><input name=\'cbConnectors\' type=\'checkbox\'>Connectors</label>"));
									$("input[name=cbConnectors]").on("change", function (e) {
										$(".fancytree-container").toggleClass("fancytree-connectors",$(this).is(":checked"));
									});
								});
							})(jQuery);

							</script>

							<script type="text/javascript">
								$(function(){
									$("#btnDeselectAll3").click(function(){
										$.ui.fancytree.getTree("#tree3").selectAll(false);
										return false;
									});
									$("#btnSelectAll3").click(function(){
										$.ui.fancytree.getTree("#tree3").selectAll();
										return false;
									});

									$("#btnGetSelected3").click(function(){
										var selNodes = $.ui.fancytree.getTree("#tree3").getSelectedNodes();
										var selData = $.map(selNodes, function(n){
											return n.toDict();
										});

										// alert(JSON.stringify(selData));
										return false;
									});

									$("#tree3").fancytree({
										checkbox: true,
										selectMode: 3,
										extensions: ["filter"],
										quicksearch: true,
										filter: {
											autoApply: true,   // Re-apply last filter if lazy data is loaded
											autoExpand: true, // Expand all branches that contain matches while filtered
											counter: true,     // Show a badge with number of matching child nodes near parent icons
											fuzzy: false,      // Match single characters in order,
											hideExpandedCounter: true,  // Hide counter badge if parent is expanded
											hideExpanders: false,       // Hide expanders if all child nodes are hidden by filter
											highlight: true,   // Highlight matches by wrapping inside <mark> tags
											leavesOnly: false, // Match end nodes only
											nodata: true,      // Display a no data status node if result is empty
											mode: "dimm"       // Grayout unmatched nodes (pass "hide" to remove unmatched node instead)
										},

										icon: false,
										source:[], /*
										source: [
											{title: "n1", expanded: true, key: "1", children: [
												{title: "n1.1 (selected)", key: "1.1"},
												{title: "n1.2", key: "1.2"},
												{title: "n1.3", key: "1.3"}
											]},

											{title: "n2", expanded: true, key: "2", children: [
												{title: "n2.1 (selected)", selected: true, key: "2.1"},
												{title: "n2.2", selected: false, key: "2.2"},
												{title: "n2.3", selected: null, key: "2.3"}
											]},

											{title: "n3", expanded: true, key: "3", children: [
												{title: "n3.1", expanded: true, key: "3.1", children: [
													{title: "n3.1.1 (unselectable)", unselectable: true, key: "3.1.1"},
													{title: "n3.1.2 (unselectable)", unselectable: true, key: "3.1.2"},
													{title: "n3.1.3", key: "3.1.3"}
												]},

												{title: "n3.2", expanded: true, key: "3.2", children: [
													{title: "n3.2.1 (unselectableStatus: true)", unselectableStatus: true, key: "3.2.1"},
													{title: "n3.2.2 (unselectableStatus: false)", unselectableStatus: false, key: "3.2.3"},
													{title: "n3.2.3", key: "3.2.3"}
												]},

												{title: "n3.3", expanded: true, key: "3.3", children: [
													{title: "n3.3.1 (unselectableStatus: true, unselectableIgnore)",
															unselectableStatus: true,  unselectableIgnore: true, key: "3.3.1"},
													{title: "n3.3.2 (unselectableStatus: false, unselectableIgnore)",
															unselectableStatus: false, unselectableIgnore: true, key: "3.3.2"},

													{title: "n3.3.3", key: "3.3.3"}
												]}
											]}
											],*/

										init: function(event, data) {
											data.tree.visit(function(n) { n.expanded = true; });
										},

										select: function(event, data) {
											var selKeys = $.map(data.tree.getSelectedNodes(), function(node){ return node.key; });
											$("#echoSelection3").text(selKeys.join(", "));
											$("#'.$m[0].'").val(selKeys.join(", "));
											var selRootNodes = data.tree.getSelectedNodes(true);
											console.log(selRootNodes);
											var selRootKeys = $.map(selRootNodes, function(node){ return node.key; });
											// console.log(selRootKeys);
											$("#echoSelectionRootKeys3").text(selRootKeys.join(", "));
										},

										cookieId: "fancytree-Cb3",
										idPrefix: "fancytree-Cb3-"
									});
		
									$("input[name=fancyTreeCBSearch]").on("change search", function(e){

										if (e.type === "change" && e.target.onsearch !== undefined ) { return; }
										var n, tree = $.ui.fancytree.getTree(),
										match = $.trim($(this).val());
										n = tree.filterNodes(match, { mode: "hide" });
									$("span.matches").text(n ? "(" + n + " matches)" : "");
									});
								});

							</script>

						<div class="cbtree" style="display:none;">
							<p><a href="#" id="btnSelectAll3">Select all</a> - <a href="#" id="btnDeselectAll3">Deselect all</a> - <a href="#" id="btnGetSelected3">Get selected</a></p>
							<label for="filter">Filter:</label>
							<input type="search" name="fancyTreeCBSearch" incremental placeholder="Search text" autocomplete="off">
							<span class="matches"></span>
							<div>Selected keys: <span id="echoSelection3">-</span></div>
							<input type="text" name="'.$m[0].'" id="'.$m[0].'" value="'. $k .'" />
							<div>Selected root keys: <span id="echoSelectionRootKeys3">-</span></div>
						</div>
						<div id="tree3"></div>';
					}
        		}else{ //END OF ADDED FOR CHECKBOX TREE 31052023
					// echo "<br>";
					// print_r($arr);
					
    				for($j = 0; $j < sizeof($eresult['result_set']); $j++){
						$k = $eresult['result_set'][$j][$eid];  //ID
						
						$flag = 0;
						if(!(is_null($p) || $p == "")) {
							for($g = 0; $g < sizeof($p); $g++){
								if($k == $p[$g][$emnyfk]){
									$flag = 1;
									break;
								}
							}
						}
						
    					if($flag) {
    						$flds .= '<label><input type="checkbox" checked ".$otherInputParams." '.$arr[6].' name="'.$m[0].'[]" id="'.$m[0].'" value="'. $k .'" tabindex="'.$arr[39].'">&nbsp;'.$eresult['result_set'][$j][$elb].'</label><br/>';
    					}
    					else { //CHANGED ON 03022020
    						$flds .= '<label><input type="checkbox" '.$arr[6].' name="'.$m[0].'[]" id="'.$m[0].'" value="'. $k .'" tabindex="'.$arr[39].'">&nbsp;'.$eresult['result_set'][$j][$elb].'</label><br/>';
    					}
    				}
        		}
				$flds .= '</div></div>';
				return $flds;
			}else{
				return null;
			}
		}else{
			return null;
		}
	}

	if($arr[1] == 10){ 	//MULTI SELECT SEARCH from DB
        /*HEADER
            $k_head_include = '
                <link rel="stylesheet" href="assets/vendor/select2/css/select2.css" />
                <link rel="stylesheet" href="assets/vendor/select2-bootstrap-theme/select2-bootstrap.min.css" />
                <link rel="stylesheet" href="assets/vendor/bootstrap-multiselect/bootstrap-multiselect.css" />'; */
        /*FOOTER
            $k_footer_before = '
            <script src="assets/vendor/select2/js/select2.js"></script>
            <script src="assets/vendor/bootstrap-multiselect/bootstrap-multiselect.js"></script>';		*/

		global $extradb;
		$eresult = array();
		for($i = 0; $i < sizeof($extradb); $i++){
			if($extradb[$i][0] == $arr[3]){
				$edb = $extradb[$i][1];
				$eid = $extradb[$i][2];
				$elb = $extradb[$i][3];
				$eval = array();
				$esql = "SELECT ".$eid.",".$elb." FROM ".$edb;// . " " . $end;

				if($k_debug) echo '<br/>'.$esql.'<br/>';
				$eresult = db::getInstance()->db_select($esql);
				if($k_debug){ echo '<br/>'; print_r($eresult); }

				break;
			}
		}
		//print_r($eresult);
		if(sizeof($eresult) > 0){
			if(sizeof($eresult['result_set']) > 0){
				//print_r($arr);
				$m = $arr;
				$p = $arr[4]; //print_r($eresult);
				$flds = '<div class="'.$divClass.'"><label>'.$arr[2].$reqv.'</label><select multiple data-plugin-selectTwo '.$reqf.' name="'.$m[0].'[]" id="'.$m[0].'" class="form-control populate" '.$arr[6].' tabindex="'.$arr[39].'">';

				for($j = 0; $j < sizeof($eresult['result_set']); $j++){
					$k = $eresult['result_set'][$j][$eid];  //ID
					$flag = 0;
					//echo "HHHH";
					// print_r($p);
					if(!empty($p)){
						for($g = 0; $g < sizeof($p); $g++){
							if($k == $p[$g][$extradb[0][2]]){
								$flag = 1;
								break;
							}
						}
					}

					if($flag) {
						$flds .= '<option selected value="'. $k .'" >'.$eresult['result_set'][$j][$elb].'</option>&nbsp;';
					}
					else {
						$flds .= '<option value="'. $k .'" >'.$eresult['result_set'][$j][$elb].'</option>&nbsp;';
					}
				}

				$flds .= '</select></div>';
				return $flds;

			}else{
				return null;
			}
		}else{
			return null;
		}
	}

	if($arr[1] == 11){ 	//MULTI SELECT SEARCH from DB WITH MAPPING TABLE
		global $extradb;
		$eresult = array();

		for($i = 0; $i < sizeof($extradb); $i++){
			if($extradb[$i][0] == $arr[3]){

				$edb = $extradb[$i][1];
				$eid = $extradb[$i][2];
				$elb = $extradb[$i][3];
				$end = $extradb[$i][4];
				$ear = $extradb[$i][5];
				$emnydb = $extradb[$i][6];
				$emnyid = $extradb[$i][7];
				$emnyfk = $extradb[$i][8];
				
				$eval = array();
				
				$esql = "SELECT ".$eid.",".$elb." FROM ".$edb;// . " " . $end;
			
				if($k_debug) echo '<br/>'.$esql.'<br/>';
							
				$eresult = db::getInstance()->db_select($esql);
				
				
				if($k_debug){ echo '<br/>'; print_r($eresult); }
				break;
			}
		}

		$m = $arr;
		$p = $arr[4]; 
		
		$addNewFn = "";

		if(extractAttribute($arr[8], "AddNewFn") !== null){     //FOR ADD NEW FUNCTION added on 23052023 for react cap api

		    $AddNewFnName = extractAttribute($arr[8], "AddNewFn");
		    $addNewFn = '<a style="padding:3px;" href="javascript:void(0)" class="btn btn-success" onclick="' . $AddNewFnName . '()">

				<span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span></a>';
		}

		$flds = '<div class="'.$divClass.'"><label>'.$arr[2].$reqv.'</label>&nbsp;'.$addNewFn.' ';

		if($edb != ''){
			$DDSearchPreLoadedArray = [];
			$DDSearchPreLoadedData = '';
			$ddsql = "SELECT TOP 10 ".$elb." FROM ". $edb;
			$ddresult = db::getInstance()->db_select($ddsql);
			if($ddresult){
				for($i=0; $i < count($ddresult['result_set']); $i++){
					array_push($DDSearchPreLoadedArray,$ddresult['result_set'][$i][$elb]);
				}
				$DDSearchPreLoadedData = implode(',',$DDSearchPreLoadedArray);
				
				echo "<script>
						$(document).ready(function(){
							var finalParams = ['".$eid."','".$elb."'];
							var DDSearchMinInputLen = [];

							if('".$arr[29]."' > 0){
								DDSearchMinInputLen['".$m[0]."'] = '".$arr[29]."';
							}else{
								DDSearchMinInputLen['".$m[0]."'] = 3;
							}

							var DDSearchPreLoadedData = [];
							var DDSearchPreLoadedDataString = [];
						
							DDSearchPreLoadedData['".$m[0]."'] = '".json_encode($DDSearchPreLoadedData)."';
							if(DDSearchPreLoadedData['".$m[0]."'].length > 0){
								DDSearchPreLoadedDataString['".$m[0]."'] = ' or more char (like: '+ DDSearchPreLoadedData['".$m[0]."'] +')';
							}else{
								DDSearchPreLoadedDataString['".$m[0]."'] = ' char ';
							}

							$('#".$m[0]."').select2({
								//tags: true,
								multiple: true,
								tokenSeparators: [',', ' '],
								debug: true,
									placeholder: 'select',
								//allowClear: true,
								minimumInputLength: DDSearchMinInputLen['".$m[0]."'],
								language: {
									inputTooShort: function() {
										return 'Enter ' + DDSearchMinInputLen['".$m[0]."'] + ' '+ DDSearchPreLoadedDataString['".$m[0]."'] ;
									}
								},
								//minimumResultsForSearch: 10,
								ajax: {
									url: 'getDropDownValue.php',
									dataType: 'json',
									type: 'POST',
									data: function (params) {
										console.log(params.term);
										var queryParameters = {
											flag: 0
											,searchText: params.term
											,searchParam:'".$elb."'
											,fromRepo:'".$edb."'
											,responseParams:finalParams.toString()
										}
										return queryParameters;
									},
									processResults: function (data) {
										if(data['data'].length == 0){
											$('#".$m[0]."').val('');//.trigger('change')
										}
										return {
											results: $.map(data['data'], function (item) {
												// console.log(ddVal);
												//console.log(item[0].Label);
												return {													
													text: item.ddVal,	
													id: item.ddID
												}
											})
										};
									}
								}
							});

							// var selectElement1 = $('#".$m[0]."');
							// $.ajax({
							// 	type : 'POST',
							// 	dataType: 'json',
							// 	data : {flag: 1,searchParam:'".$elb."',fromRepo:'".$edb."',responseParams:finalParams.toString()},
							// 	url : 'getDropDownValue.php',
							// 	success : function(data){
							// 		// console.log(data['data']);
							// 		var option = [];
							// 		for(var i=0; i< data['data'].length; i++){
							// 			option[i] = new Option(data['data'][i].ddVal, data['data'][i].ddID, false, false);
							// 			// console.log(option[i]);
							// 			selectElement1.append(option[i]).trigger('change');
							// 		}
							// 	}
							// })

						});
					</script>";
			}
		}

		$flds .= '<select multiple '.$reqf.' name="'.$m[0].'[]" id="'.$m[0].'" class="form-control populate" '.$arr[6].' tabindex="'.$arr[39].'" ';
		
		if(strlen($arr[28]) == 0){
			$flds .= ' data-plugin-selectTwo';
		}

		$flds .= '>';

		if(sizeof($eresult) > 0){

			if(sizeof($eresult['result_set']) > 0){

				for($j = 0; $j < sizeof($eresult['result_set']); $j++){

					$k = $eresult['result_set'][$j][$eid];  //ID
					$flag = 0;

					if(!(is_null($p) || $p == "")) {

						for($g = 0; $g < sizeof($p); $g++){

							if($k == $p[$g][$emnyfk]){

								$flag = 1;
								break;
							}
						}
					}

					if($flag) {

						$flds .= '<option selected value="'. $k .'" >'.$eresult['result_set'][$j][$elb].'</option>&nbsp;';

					}else{
						if(strlen($arr[28]) == 0){
							$flds .= '<option value="'. $k .'" >'.$eresult['result_set'][$j][$elb].'</option>&nbsp;';
						}

					}
				}
			}
		}
		$flds .= '</select></div>';
		return $flds;
	}

	if($arr[1] == 12){ 	//SINGLE IMAGE UPLOAD 
	    return "<div class='".$divClass."'><label class='custom-file-upload'><i class='fa fa-cloud-upload'></i> ".$arr[2].$reqv." <input type='file' ".$arr[6]." name='".$arr[0]."[]' id='".$arr[0]."' ".$arr[6]." tabindex='".$arr[39]."'/>".$arr[4]."</label></div>";
		//return "<div class='col-md-4'><label>".$arr[2].$reqv."</label><input type='file' ".$arr[6]." name='".$arr[0]."[]' id='".$arr[0]."[]' ".$arr[6]."/>".$arr[4]."</div>";
	}

	if($arr[1] == 13){ 	//MULTIPLE IMAGE UPLOAD 
		return "<div class='".$divClass."'><label>".$arr[2].$reqv."</label><input style='display: block;' type='file' ".$arr[6]." name='".$arr[0]."[]' id='".$arr[0]."' ".$arr[6]." multiple='true' tabindex='".$arr[39]."'/>".$arr[4]."</div>";
	}

	if($arr[1] == 14){ 	//GRID
	    //IGNORE THIS ELEMENT 
	}	
	
	if($arr[1] == 15){ 	//Toggle Switch
		//carry on fields
		if(isset($_SESSION['carryOn'])){
			if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
				if(isset($_SESSION['carryOn'][$arr[0]])){
					$arr[4] = $_SESSION['carryOn'][$arr[0]];
					unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
				}
			}
		} 

		//If Default value set 1 then toggle is active
		if(strlen($arr[31]) >= 1 && strlen($arr[4]) == 0){
			$arr[4] = $arr[31];
		}

		$flds = "<div class='".$divClass."'><label>".$arr[2].$reqv."</label><br /><div class='switch switch-sm switch-primary' ";
		if($arr[32] == 1){ //to check if field editable
			if($editID > 0){
				$flds .= "style=\"pointer-events:none\";";
			}
		}
		$flds .=">";

		$flds .= "<input type='checkbox' name='".$arr[0]."' id='".$arr[0]."' value='1' data-plugin-ios-switch=''  style='display: none;' tabindex='".$arr[39]."' ";
		if($arr[4] == 1){
			$flds .= "checked='checked'";
		}

		if($arr[32] == 1){ //to check if field editable
			if($editID > 0){
				$flds .= "style=\"pointers-event:none;\"";
			}
		}
	
		$flds .= "></div></div>";
		return $flds;
	}
	

	if($arr[1] == 16){ //Multiselect Popup Datatable
		global $extradb;
		$eresult = array();
	    // print_r($arr);
		for($i = 0; $i < sizeof($extradb); $i++){
			if($extradb[$i][0] == $arr[3]){

				$edb = $extradb[$i][1];
				$eid = $extradb[$i][2];
				$elb = $extradb[$i][3];
				$end = $extradb[$i][4];
				$ear = $extradb[$i][5];
				$emnydb = $extradb[$i][6];
				$emnyid = $extradb[$i][7];
				$emnyfk = $extradb[$i][8];
				
				$eval = array();

				$esql = "SELECT ".$eid.",".$elb." FROM ".$edb . " " . $end;
				
				if($k_debug) echo '<br/>'.$esql.'<br/>';
				
				$eresult = db::getInstance()->db_select($esql);
				
				if($k_debug){ echo '<br/>'; print_r($eresult); }
				break;
			}
		}

		//carry on fields
		if(isset($_SESSION['carryOn'])){
			if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
				$fld16String = $_SESSION['carryOn'][$arr[0]];
				if(isset($_SESSION['carryOn'][$arr[0]])){
					$arr[4] = array();
					$carryOnArray = array();
					$carryOnArray = explode(",", $fld16String);
				
					for($j=0; $j<sizeof($carryOnArray); $j++){
						
						$esql1 = "SELECT ".$eid.",".$elb." FROM ".$edb . " WHERE " . $eid."=".$carryOnArray[$j];
						$eresult1 = db::getInstance()->db_select($esql1);
						
						array_push($arr[4],array($emnyfk=>$eresult1['result_set'][0][$eid],$elb=>$eresult1['result_set'][0][$elb]));
						
					}
					unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
				}
			}
		} 
		
		$m = $arr;
		$mapTableValue = [];
		$mapTableID = [];
		$mTValue = '';
		$mTID = '';
		
		if(is_array($arr[4])){
			for($i=0; $i<sizeof($arr[4]); $i++){
				array_push($mapTableValue, $arr[4][$i][$elb]);
				array_push($mapTableID, $arr[4][$i][$emnyfk]);
			}
			$mTValue = implode(',', $mapTableValue);
			$mTID = implode(',', $mapTableID);
		}

		// $flds = "<div class='".$divClass."'><label>".$arr[2].$reqv."</label><input ".$reqf." readonly type='text' name='".$arr[0]."-text' id='MultyDDModal-".$m[0]."'  data-tag-class='badge badge-primary' class='form-control' value='".$mTValue."' style='cursor: pointer;
		// background-color: white;' tabindex='".$arr[39]."' ";

		//Once lost focus from perticular field then again don't allow to focus on this field
		// if($arr[41] == 1){
		// 	$flds .= "onblur=\"onBlurUpdateLostFocusTabIndex(this,$arr[1]);\"";
		// }
		
		// $flds .= "><input type='hidden' id='MultyDDModal-hidden-".$m[0]."' name='".$arr[0]."' value='".$mTID."'></div> ";
		
		$flds .= '<div class="'.$divClass.'"><label>'.$arr[2].$reqv.'</label><input type="button" data-type="'.$arr[1].'" onclick="toggleModal()" class="form-control" tabindex="'.$arr[39].'" name="'.$arr[0].'-text" id="'.$m[0].'-text" value="'.$mTValue.'" ';
		
		$SPName = 'sp_GetData_'.$FormID.'_'.$arr[0];
		$sql = "SELECT COUNT(*) AS found FROM information_schema.routines WHERE routine_schema = 'DBO'  AND  routine_name = '$SPName'";
		$checkSp = db::getInstance()->db_select($sql);
		if($checkSp['result_set'][0]['found'] == 1){
			$flds .=" data-systemtype=\"2\"";
		}else{
			$flds .=" data-systemtype=\"1\"";
		}
		
		$flds .= ' style="text-align: left;white-space: nowrap;overflow:hidden;text-overflow:ellipsis">';
		$flds .= '<input type="hidden" id="'.$m[0].'" name="'.$arr[0].'" value="'.$mTID.'"></div>';

		//response{ID | Name } url { https://krdemo.kreonsolutions.in/getSender.php} parameters{ID: Sender}
		echo $ddtableIncludes;
		echo "
		<script>
		$(document).ready(function(){

			//Onload if tabindex is 1 then set focus
			if('".$arr[39]."' == 1){
				$('#MultyDDModal-".$m[0]."').focus();
			}

			$('#MultyDDModal-".$m[0]."').keypress(function(e) {
				if(e.which == 13) {
					$('#MultyDDModal-". $m[0]."').click();
				}
			});
		});

		</script>";

		?>
		<script>

			$(document).ready(function () {
				// Focus on the open button after the modal is shown
				// $('#'+<?php echo "'$arr[0]-text'"; ?>).focus(function() {
				// 	toggleModal();
				// });

				$('#'+<?php echo "'$arr[0]-text'"; ?>).keypress(function(e) {
					if(e.which == 13) {
						$('#'+<?php echo "'$arr[0]-text'"; ?>).click();
					}
				});
			});

			//On escape button press cursor move to next tabindex field
			$('#myModal').on('hidden.bs.modal', function (e) {
				console.log("escape");
				//Check validation using sp on lost focus
				if(<?php echo $arr[35]; ?> == 1){
					validateOnLostFocusSp(<?php echo "'".$m[0]."'"; ?>,0); //0 is gridid in form it is set as 0
				}

				var currentFieldId = $('#DropDownModalID').val();
				//Once lost focus from perticular field then again don't allow to focus on this field
				if(<?php echo $arr[41]; ?> == 1){
					if($("#"+<?php echo "'".$arr[0]."'"; ?>).val() != ''){
						onBlurUpdateLostFocusTabIndex(<?php echo "'".$arr[0]."'"; ?>,<?php echo $arr[1]; ?>);
					}
				}

				var tabindex = $('#'+currentFieldId).attr("tabindex");
				tabindex++;
				// console.log("'#DropDownModal' .on('hidden.bs.modal', function")
				$("[tabindex='"+tabindex+"']").focus();
			})
		</script>
		<?php

		return $flds;

		$ddtableIncludes = "";
	}
	
	if($arr[1] == 17){ //Single-select Popup Datatable
		global $extradb;

		
		if(isset($_SESSION['carryOn'])){
			if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
				if(isset($_SESSION['carryOn'][$arr[0]])){
					$arr[4] = $_SESSION['carryOn'][$arr[0]];
					unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
				}
			}
		}
		
		$eresult = array();
		
		$p = $arr[4];
		for($i = 0; $i < sizeof($extradb); $i++){
			if($extradb[$i][0] == $arr[3]){

				$edb = $extradb[$i][1];
				$eid = $extradb[$i][2];
				$elb = $extradb[$i][3];
				$end = $extradb[$i][4];
				$ear = $extradb[$i][5];
				$emnydb = $extradb[$i][6];
				$emnyid = $extradb[$i][7];
				$emnyfk = $extradb[$i][8];
				
				$eval = array();

				if($p){

					$esql = "SELECT ".$eid.",".$elb." FROM ".$edb;
					if(strlen($end) > 1){
						$esql .= " ".$end;
					}else{
						$esql .= " WHERE 1=1";
					}
					$esql .= " AND ".$eid ."='". $p ."'" ;

					// if($FormID == 5258){
					// 	echo $esql;
						
					// }
					

					if($k_debug) echo '<br/>'.$esql.'<br/>';
	
					$eresult = db::getInstance()->db_select($esql);
					if($k_debug){ echo '<br/>'; print_r($eresult); }
				}

				//Query for Default value
				if(strlen($arr[31]) > 0 ){
					$esql1 = "SELECT ".$eid.",".$elb." FROM ".$edb . " where ". $eid ."=". $arr[31];
					$eresult1 = db::getInstance()->db_select($esql1);
					$eresult['result_set'][0][$elb] = $eresult1['result_set'][0][$elb];
					$eresult['result_set'][0][$eid] = $eresult1['result_set'][0][$eid];
				}

				break;
			}
		}
		
		$m = $arr;
		$mapTableValue = [];
		$mapTableID = [];
		$mTValue = '';
		$mTID = '';
		// echo gettype($arr[4]);
		// if($arr[4]){
		// 	$rsql = "SELECT ".$eid.",".$elb." FROM ".$edb . " where " . $eid . " = " . $arr[4];
		// 	$rresult = db::getInstance()->db_select($rsql);
		// 	$mTValue = $rresult['result_set'][0][$elb];
		// 	$mTID = $rresult['result_set'][0][$eid];
		// }

		// $flds = "<div class='".$divClass."'><label>".$arr[2].$reqv."</label><input ".$reqf." readonly type='text'  name='".$arr[0]."-text' id='SingleDDModal-".$m[0]."'  data-tag-class='badge badge-primary' class='form-control' value='".$eresult['result_set'][0][$elb]."' style='cursor: pointer;
		// background-color: white;' tabindex='".$arr[39]."' ";

		//Once lost focus from perticular field then again don't allow to focus on this field
		// if($arr[41] == 1){
		// 	$flds .= "onblur=\"onBlurUpdateLostFocusTabIndex(this,$arr[1]);\"";
		// }
		
		// $flds .= "><input type='hidden' id='SingleDDModal-hidden-".$m[0]."' name='".$arr[0]."' value='".$eresult['result_set'][0][$eid]."'></div> ";
		
		$flds .= '<div class="'.$divClass.'"><label>'.$arr[2].$reqv.'</label><input type="button" data-type="'.$arr[1].'" onclick="toggleModal()" class="form-control" tabindex="'.$arr[39].'" name="'.trim($arr[0]).'-text" id="'.trim($arr[0]).'-text" value="'.$eresult['result_set'][0][$elb].'" ';
		
		$SPName = 'sp_GetData_'.$FormID.'_'.$m[0];
		$sql = "SELECT COUNT(*) AS found FROM information_schema.routines WHERE routine_schema = 'DBO'  AND  routine_name = '$SPName'";
		$checkSp = db::getInstance()->db_select($sql);
		if($checkSp['result_set'][0]['found'] == 1){
			$flds .=" data-systemtype=\"2\"";
		}else{
			$flds .=" data-systemtype=\"1\"";
		}
		
		$flds .= ' style="text-align: left;white-space: nowrap;overflow:hidden;text-overflow:ellipsis">';
		$flds .= '<input type="hidden" id="'.trim($m[0]).'" name="'.trim($arr[0]).'" value="'.(isset($eresult['result_set'][0][$eid]) ? $eresult['result_set'][0][$eid] : '').'"></div>';

		//response{ID | Name } url { https://krdemo.kreonsolutions.in/getSender.php} parameters{ID: Sender}
		echo $ddtableIncludes;
		echo "
		<script>
		$(document).ready(function(){
			//Onload if tabindex is 1 then set focus
			if('".$arr[39]."' == 1){
				$('#".$m[0]."-text').focus();
			}

			$('#".$m[0]."-text').keypress(function(e) {
				if(e.which == 13) {
					$('#". $m[0]."-text').click();
				}
			});
		});

		</script>";

		?>
		<script>
			$(document).ready(function () {
				// Focus on the open button after the modal is shown
				// $('#'+<?php echo "'$arr[0]-text'"; ?>).focus(function() {
				// 	toggleModal();
				// });			
			});

			//On escape button press cursor move to next tabindex field
			$('#myModal').on('hidden.bs.modal', function (e) {
				var currentFieldId = $('#DropDownModalID').val();
				if(<?php echo "'".$arr[0]."-text'"; ?> == currentFieldId){
					// alert('hello');
					var tabindex = $('#'+currentFieldId).attr("tabindex");
					tabindex++;

					// Once lost focus from perticular field then again don't allow to focus on this field
					if(<?php echo $arr[41]; ?> == 1){

						if($("#"+<?php echo "'".$arr[0]."-text'"; ?>).val() != ''){
							onBlurUpdateLostFocusTabIndex(<?php echo "'".$arr[0]."-text'"; ?>,<?php echo $arr[1]; ?>);
						}
					}

					//Check validation using sp on lost focus
					if(<?php echo $arr[35]; ?> == 1){
						validateOnLostFocusSp(<?php echo "'".$m[0]."'"; ?>,0); //0 is gridid in form it is set as 0
					}
					
					setTimeout(
						function() {
							console.log("Line 6401 focus")
							$("[tabindex='"+tabindex+"']").focus();
						}, 300);
				}

				
			})
		</script>
		<?php
		return $flds;

		$ddtableIncludes = "";
	}

    if($arr[1] == 18){ //TIME
		//carry on fields
		if(isset($_SESSION['carryOn'])){
			if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
				if(isset($_SESSION['carryOn'][$arr[0]])){
					$arr[4] = $_SESSION['carryOn'][$arr[0]];
					unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
				}
			}
		}   

		// if($arr[4] != NULL){
		// 	if(gettype($arr[4]) == 'string'){
		// 		$date = strtotime($arr[4]);
		// 		$arr[4] = date('h:i', $date);
		// 	}else{
		// 		$arr[4] = $arr[4]->format('h:i');
		// 	}
		// }

		$flds .= "<div class='".$divClass."'><label>".$arr[2].$reqv."</label><input ".$reqf." class='form-control dyn1' value='".$arr[4]."' type='text' id='time' ".$arr[6]." ".$otherInputParams." name='".$arr[0]."' id='".$arr[0]."' tabindex='".$arr[39]."' ";

		// $flds .= "<div class='".$divClass."'><label>".$arr[2].$reqv."</label><input ".$reqf." class='form-control dyn1' value='".$arr[4]."' type='time' ".$arr[6]." ".$otherInputParams." name='".$arr[0]."' id='".$arr[0]."' ";
		if($arr[32] == 1){ //to check if field editable
			if($editID > 0){
				$flds .= "readonly";
			}
		}

		//Check validation using sp on lost focus
		if($arr[35] == 1){
			$flds .= "onfocusout=\"validateOnLostFocusSp(this,0);\""; //0 is gridid in form it is set as 0
		}

		//Once lost focus from perticular field then again don't allow to focus on this field
		if($arr[41] == 1){
			$flds .= "onblur=\"onBlurUpdateLostFocusTabIndex(this,$arr[1]);\"";
		}

		return $flds .= "/></div>";
	}
	
	if($arr[1] == 19){ 	//SELECT from DB
		global $extradb;

		//carry on fields
		if(isset($_SESSION['carryOn'])){
			if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
				if(isset($_SESSION['carryOn'][$arr[0]])){
					$arr[4] = $_SESSION['carryOn'][$arr[0]];
					unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
				}
			}
		}

		$eresult = array();
        $m = $arr;
		$p = $arr[4];
		for($i = 0; $i < sizeof($extradb); $i++){
			if($extradb[$i][0] == $arr[3]){
				$edb = $extradb[$i][1];
				$eid = $extradb[$i][2];
				$elb = $extradb[$i][3];
				// $end = $extradb[$i][4];
				$end = checkVariable($extradb[$i][4]);
				$eval = array();
				
                $esql = "SELECT ".$eid.",".$elb." FROM ".$edb;
				if(strlen($end) > 1){
					$esql .= " ".$end;
				}

				$esql .= " ORDER BY ".$elb;

                if($k_debug) echo '<br/>'.$esql.'<br/>';

                $eresult = db::getInstance()->db_select($esql);
            
				if($k_debug){ echo '<br/>'; print_r($eresult); }
				
				break;
			}
		}

		//Default value set exicute
		if(strlen($arr[31]) > 0 ){
			$p = $arr[31];
		}

			
		//print_r($p);
		$flds = '<div class="'.$divClass.'"><label>'.$arr[2].$reqv.'</label>';
		
		if($arr[23] > 0){	
			$flds .= '<button type="button" id="AddBtnModal-'.$m[0].'"  value="'.$arr[23].'-'.$m[0].'-'.$formType.'" class="amodal" style="background: transparent;border: none !important;font-size:1;color:blue"><i class="fa fa-plus-circle"></i></button>';
		}
		
		echo "<script>
			$(document).ready(function(){

				// $('#".$m[0]."').on('select2:select', function (e) { // select option by enter then cursor going to next field
				// 	var self = $(this), form = self.parents('form:eq(0)'), focusable, next;
				// 	focusable = form.find('input,a,select,button,textarea').filter(':visible');
				// 	next = focusable.eq(focusable.index(this)+1);
				// 	if (next.length) {
				// 		next.focus();
				// 	} 
				// 	return false;
				// });

				//Once lost focus from perticular field then again don't allow to focus on this field
				if('".$arr[41]."' == 1){
					$('#".$m[0]."').on('select2:select', function (e) {
						onBlurUpdateLostFocusTabIndex(this,'".$arr[1]."');
					});
				}

				if('".$arr[39]."' == 1){
					setTimeout(
						function() {
							console.log('Line 4547 Focus');
							$('#".$m[0]."').next('span').find('.select2-selection--single').focus();
						}, 300);
				}
			});
		</script>";

		?>

		<script>
			if(<?php echo $arr[39]; ?> == 1){
				var tabindex1 = <?php echo $arr[39]; ?>;
				tabindex1++;
				// alert(tabindex1);
				console.log("Line 6539 focus")
				$("[tabindex='"+tabindex1+"']").focus();
			}	
		</script>

		<?php

			
		$flds .= '<select data-plugin-selectTwo '.$reqf.' name="'.$m[0].'" id="'.$m[0].'" class="form-control populate" tabindex="'.$arr[39].'" ';


		if($arr[17] == 1){
			$onchange = " onchange=\"CreateOnchangeScript(this, '".$arr[1]."');\"";
			$flds .= $onchange;
			$onchangeHidden = '<input type="hidden" id="'.$m[0].'-hidden" value="' . $arr[18] . '" />';
		}

		if($arr[32] == 1){ //to check if field editable
			if($editID > 0){
				$flds .= "style=\"pointers-event:none;\"";
			}
		}

		//Check validation using sp on lost focus
		if($arr[35] == 1){
			$flds .= "onchange=\"validateOnLostFocusSp(this,0);\"";  //0 is gridid in form it is set as 0
		}
		
		//Check validation using sp onChange
		if($arr[36] == 1){
			$flds .= "onchange=\"validateOnChangeSp(this,0);\""; //0 is gridid in form it is set as 0
		}

		if($arr[12] == 1){ //to check if field readonly
			echo '<script>
				setTimeout(
					function() {
						$("#'.$m[0].'").next("span").find(".select2-selection--single").css("pointer-events","none").css("background","#eee");
					}, 300);
			</script>';
		}

		// $flds .= '><option value=""></option>'; 

		$flds .= '>'; 
		// $flds .= '><option value="0"></option>'; 

		// if(strlen($arr[6]) > 4){
			
		// 	//Use for get conditional data
		// 	echo "<script>
		// 	$(document).ready(function(){
		// 		$('#".$m[0]."').on('select2:open', function (e) {
		// 			var str1 = 'parameters';
		// 			var parametersPos = -1;
		// 			var thissParameters = '".$arr[6]."';
		// 			if(thissParameters.indexOf(str1) != -1){
		// 				parametersPos = thissParameters.indexOf(str1);
		// 			}

		// 			if(parametersPos != -1){
		// 				var parametersParams = thissParameters.substring(
		// 					thissParameters.indexOf('{') + 1, 
		// 					thissParameters.lastIndexOf('}')
		// 				);
		// 			}

		// 			if(parametersParams.length > 3){
		// 				var hidden = $('form').find(':hidden');	
		// 				hidden.show();   
		// 				var  formSerialize = $('form').serialize();	
		// 				hidden.hide(); 	

		// 				$.ajax({
		// 					type : 'POST',
		// 					dataType: 'json',
		// 					data : {FieldOtherCondition : '".$arr[6]."', gridSerial:0,searchText: formSerialize, GridID:0, dbarray0: '".$edb."', dbarray1:'".$eid."', dbarray2: '".$elb."', dbarray3: '".$end."'},
		// 					url : 'getPreLoadedDropDownValue.php',
		// 					success : function(result){
		// 						var newOption = '';
		// 						$('#".$m[0]."').empty().trigger('change');

		// 						for(var j=0; j < result['data'].length; j++){ 
		// 							newOption += '<option value=\"'+result['data'][j]['".$eid."']+'\">'+result['data'][j]['".$elb."']+'</option>';
		// 							/* var newOption = $('<option ></option>').val(result['data'][j]['".$eid."']).text(result['data'][j]['".$elb."']) */
		// 						}	
		// 						$('#".$m[0]."').append(newOption).trigger('change');

								
		// 					}
		// 				});
		// 			}
		// 		});
		// 	});
		// 	</script>";
		// }else{
			if(sizeof($eresult) > 0){
				if(sizeof($eresult['result_set']) > 0){
					for($j = 0; $j < sizeof($eresult['result_set']); $j++){
						$k = $eresult['result_set'][$j][$eid];  //ID	
	
						if($p == $k) {
							$flds .= '<option selected value="'. $k .'" >'.$eresult['result_set'][$j][$elb].'</option>&nbsp;';
						}
						else {
							$flds .='<option value="'. $k .'" >'.$eresult['result_set'][$j][$elb].'</option>&nbsp;';
						}
					}
				}
			}
		// }


		$flds .= '</select>'.$onchangeHidden.'</div>';

		return $flds;
	}

	if($arr[1] == 20){ 	//Session Default hidden
		global $viewSettings;
		
		if (strpos($viewSettings[8], $arr[0]) !== false){
			$sessionVariable = extractParameters($viewSettings[8]);
			return "<input value='".$sessionVariable."' type='hidden' ".$arr[6]." name='".$arr[0]."' />";
		}else{
			return "<input value='0' type='hidden' ".$arr[6]." name='".$arr[0]."' />";
		}
	}

	if($arr[1] == 21){ 	//Section Open
		return '<div class="'.$divClass.'">
		            <div class="toggle toggle-primary" data-plugin-toggle>
						<section class="toggle active">
							<label style="margin:0;">'.$arr[2].'</label>
							<div class="toggle-content">';
	}

	if($arr[1] == 22){ 	//Section Close
	    return "</div></section></div></div>";
	}

	
	// if($arr[1] == 23){  //GRID Multiselect Popup Datatable
	// 	global $extradb;

	// 	$eresult = array();
	// 	// print_r($extradb);
	// 	for($i = 0; $i < sizeof($extradb); $i++){
	// 		if($extradb[$i][0] == $arr[3]){

	// 			$edb = $extradb[$i][1];
	// 			$eid = $extradb[$i][2];
	// 			$elb = $extradb[$i][3];
	// 			$end = $extradb[$i][4];
	// 			$ear = $extradb[$i][5];
	// 			$emnydb = $extradb[$i][6];
	// 			$emnyid = $extradb[$i][7];
	// 			$emnyfk = $extradb[$i][8];
				
	// 			$eval = array();

	// 			$esql = "SELECT ".$eid.",".$elb." FROM ".$edb . " " . $end;
				
	// 			if($k_debug) echo '<br/>'.$esql.'<br/>';
				
	// 			$eresult = db::getInstance()->db_select($esql);
				
	// 			if($k_debug){ echo '<br/>'; print_r($eresult); }
	// 			break;
	// 		}
	// 	}
		
	// 	$m = $arr;
	// 	$mapTableValue = [];
	// 	$mapTableID = [];
	// 	$mTValue = '';
	// 	$mTID = '';
	
	// 	if(is_array($arr[4])){
	// 		for($i=0; $i<sizeof($arr[4]); $i++){
	// 			array_push($mapTableValue, $arr[4][$i][$elb]);
	// 			array_push($mapTableID, $arr[4][$i][$emnyfk]);
	// 		}
	// 		$mTValue = implode(',', $mapTableValue);
	// 		$mTID = implode(',', $mapTableID);
	// 	}
		

	// 	$flds  .= '<button type="button"  id="GridMultyDDModal-'.$m[0].'" >'.$arr[2].'</button>';
	// 	$flds .= "<input type='hidden' id='GridMultyDDModal-hidden-".$m[0]."' name='".$arr[0]."' value='".$mTID."'>";
	// 	$flds .= "<input type='hidden' id='MultiSelectGridId-hidden-".$m[0]."' name='".$arr[0]."' value='".$arr[34]."'>";


	// 	// $flds = "<div class='".$divClass."'><label>".$arr[2].$reqv."</label><input ".$reqf." readonly type='text' name='".$arr[0]."-text' id='MultyDDModal-".$m[0]."'  data-tag-class='badge badge-primary' class='form-control' value='".$mTValue."' style='cursor: pointer;
	// 	// background-color: white;'><input type='hidden' id='MultyDDModal-hidden-".$m[0]."' name='".$arr[0]."' value='".$mTID."'></div> ";
		
	// 	//response{ID | Name } url { https://krdemo.kreonsolutions.in/getSender.php} parameters{ID: Sender}
	// 	echo $ddtableIncludes;
	// 	echo "
	// 		<script>
	// 			$(document).ready(function(){
	// 				$('#GridMultyDDModal-".$m[0]."').keypress(function(e) {
	// 					if(e.which == 13) {
	// 						$('#GridMultyDDModal-". $m[0]."').click();
	// 					}
	// 				});

	// 				$('#GridMultyDDModal-". $m[0]."').click(function(){
	// 					$('#modalLabelLarge').text('Multiple selection Dropdown');				
	// 					$('#DropDownModal').modal('show');
	// 					jqxhr = $.ajax('getMultyDDValue1.php?FormID=".$FormID."&GridID=0&DBFieldName=". $m[0]."&DBFieldType=23&isGrid=0')
	// 					.done(function () {
	// 							tableName= '#ddTable',
	// 							data1 = JSON.parse(jqxhr.responseText);
							
	// 							DBFieldName='". $m[0]."';
	// 							DBFieldType ='23';
	// 							FormID = '".$FormID."';
	// 							GridID = 0;
	// 							isGrid = '0';
								
	// 							var row = '<tr>';
	// 							var rowFilter = '<tr class=\"headerFilter\">';
	// 							$.each(data1.columns, function (k, colObj) {
									
	// 								row += '<th>' + colObj.name + '</th>';
	// 								var searchColName = colObj.name.trim();
	// 								if(k == 0){
	// 									rowFilter += '<th></th>';
	// 								}else{
	// 									rowFilter += '<th><input type=\"search\" class=\"form-control\" onkeydown=\"SearchFunction(event,DBFieldName,DBFieldType,FormID,GridID,isGrid)\" placeholder=' + searchColName + ' id='+searchColName+' name='+searchColName+'></th>';
	// 								}
									
	// 							});
	// 							rowFilter += '</tr>';
	// 							row += '</tr>';
	// 							if ($.fn.dataTable.isDataTable('#ddTable')) {
	// 								//console.log('In data');
	// 								$('#ddTable').DataTable().destroy();
	// 							}
	// 							$('#ddTable').html('<thead></thead>');
	// 							$('#ddTable thead').html(row + rowFilter);
	// 							$('.headerFilter th:first').hide();

								
	// 							var is_variable='';
	// 							var is_value='';
	// 							$('#PopulateGridButton').hide();
	// 							// console.log('calling load_dd 5771');
	// 							load_dd(DBFieldName,DBFieldType,FormID,GridID,isGrid);
	// 						});
	// 				});
	// 			});
	// 		</script>";
	// 	return $flds;

	// 	$ddtableIncludes = "";
	// }

	if($arr[1] == 23){  //GRID Multiselect Popup Datatable
		global $extradb;

		$eresult = array();
		// print_r($extradb);
		for($i = 0; $i < sizeof($extradb); $i++){
			if($extradb[$i][0] == $arr[3]){

				$edb = $extradb[$i][1];
				$eid = $extradb[$i][2];
				$elb = $extradb[$i][3];
				$end = $extradb[$i][4];
				$ear = $extradb[$i][5];
				$emnydb = $extradb[$i][6];
				$emnyid = $extradb[$i][7];
				$emnyfk = $extradb[$i][8];
				
				$eval = array();

				$esql = "SELECT ".$eid.",".$elb." FROM ".$edb . " " . $end;
				
				if($k_debug) echo '<br/>'.$esql.'<br/>';
				
				$eresult = db::getInstance()->db_select($esql);
				
				if($k_debug){ echo '<br/>'; print_r($eresult); }
				break;
			}
		}
		
		$m = $arr;
		$mapTableValue = [];
		$mapTableID = [];
		$mTValue = '';
		$mTID = '';
	
		if(is_array($arr[4])){
			for($i=0; $i<sizeof($arr[4]); $i++){
				array_push($mapTableValue, $arr[4][$i][$elb]);
				array_push($mapTableID, $arr[4][$i][$emnyfk]);
			}
			$mTValue = implode(',', $mapTableValue);
			$mTID = implode(',', $mapTableID);
		}
		

		// $flds  .= '<button type="button"  id="GridMultyDDModal-'.$m[0].'" >'.$arr[2].'</button>';
		// $flds .= "<input type='hidden' id='GridMultyDDModal-hidden-".$m[0]."' name='".$arr[0]."' value='".$mTID."'>";
		// $flds .= "<input type='hidden' id='MultiSelectGridId-hidden-".$m[0]."' name='".$arr[0]."' value='".$arr[34]."'>";

		$flds  .= '<div class="'.$divClass.'"><label>'.$arr[2].$reqv.'</label><input type="button" data-type="'.$arr[1].'" onclick="toggleModal()" class="form-control" tabindex="'.$arr[39].'" name="'.$arr[0].'-text" id="'.$arr[0].'-text" style="text-align: left;white-space: nowrap;overflow:hidden;text-overflow:ellipsis"></div>';

		// $flds = "<div class='".$divClass."'><label>".$arr[2].$reqv."</label><input ".$reqf." readonly type='text' name='".$arr[0]."-text' id='MultyDDModal-".$m[0]."'  data-tag-class='badge badge-primary' class='form-control' value='".$mTValue."' style='cursor: pointer;
		// background-color: white;'><input type='hidden' id='MultyDDModal-hidden-".$m[0]."' name='".$arr[0]."' value='".$mTID."'></div> ";
		
		//response{ID | Name } url { https://krdemo.kreonsolutions.in/getSender.php} parameters{ID: Sender}
		echo $ddtableIncludes;
		// echo "
		// 	<script>
		// 		$(document).ready(function(){
		// 			$('#GridMultyDDModal-".$m[0]."').keypress(function(e) {
		// 				if(e.which == 13) {
		// 					$('#GridMultyDDModal-". $m[0]."').click();
		// 				}
		// 			});

		// 			$('#GridMultyDDModal-". $m[0]."').click(function(){
		// 				$('#modalLabelLarge').text('Multiple selection Dropdown');				
		// 				$('#DropDownModal').modal('show');
		// 				jqxhr = $.ajax('getMultyDDValue1.php?FormID=".$FormID."&GridID=0&DBFieldName=". $m[0]."&DBFieldType=23&isGrid=0')
		// 				.done(function () {
		// 						tableName= '#ddTable',
		// 						ddResponseData = JSON.parse(jqxhr.responseText);
							
		// 						DBFieldName='". $m[0]."';
		// 						DBFieldType ='23';
		// 						FormID = '".$FormID."';
		// 						GridID = 0;
		// 						isGrid = '0';
								
		// 						var row = '<tr>';
		// 						var rowFilter = '<tr class=\"headerFilter\">';
		// 						$.each(ddResponseData.columns, function (k, colObj) {
									
		// 							row += '<th>' + colObj.name + '</th>';
		// 							var searchColName = colObj.name.trim();
		// 							if(k == 0){
		// 								rowFilter += '<th></th>';
		// 							}else{
		// 								rowFilter += '<th><input type=\"search\" class=\"form-control\" onkeydown=\"SearchFunction(event,DBFieldName,DBFieldType,FormID,GridID,isGrid)\" placeholder=' + searchColName + ' id='+searchColName+' name='+searchColName+'></th>';
		// 							}
									
		// 						});
		// 						rowFilter += '</tr>';
		// 						row += '</tr>';
		// 						if ($.fn.dataTable.isDataTable('#ddTable')) {
		// 							//console.log('In data');
		// 							$('#ddTable').DataTable().destroy();
		// 						}
		// 						$('#ddTable').html('<thead></thead>');
		// 						$('#ddTable thead').html(row + rowFilter);
		// 						$('.headerFilter th:first').hide();

								
		// 						var is_variable='';
		// 						var is_value='';
		// 						$('#PopulateGridButton').hide();
		// 						// console.log('calling load_dd 5771');
		// 						load_dd(DBFieldName,DBFieldType,FormID,GridID,isGrid);
		// 					});
		// 			});
		// 		});
		// 	</script>";

		?>

		<script>
			$(document).ready(function () {
				// Focus on the open button after the modal is shown
				$('#'+<?php echo "'$arr[0]-text'"; ?>).focus(function() {
					
					toggleModal();
				});			
			});
		</script>

		<?php

		return $flds;

		$ddtableIncludes = "";
	}

	if($arr[1] == 24){  //Button on click call sp & set data into grid
		global $extradb;
				
		$fieldotherconditions = "'".$arr[6]."'";

			if (stripos($fieldotherconditions, 'openLinkParameters') !== false) {
				$tableName = '';
				$TablePK = '';

				// Case-insensitive match for openLinkButtonCondition block
				if (preg_match('/openLinkButtonCondition\{([^}]*)\}/i', $fieldotherconditions, $matches)) {
					$params = explode(',', $matches[1]);
					foreach ($params as $param) {
						$parts = array_map('trim', explode('=', $param));
						if (count($parts) == 2) {
							if (strcasecmp($parts[0], 'TableName') == 0) {
								$tableName = $parts[1];
							} elseif (strcasecmp($parts[0], 'TablePK') == 0) {
								$TablePK = $parts[1];
							}
						}
					}
				}

				if ($tableName && $TablePK) {
					$checkAdjAvailable = "SELECT * FROM $tableName WHERE $TablePK = $editID";
					$resultCheckAdjAvailable = db::getInstance()->db_select($checkAdjAvailable);
				}

				// Extract openLinkUrl{} content
				if (preg_match('/openLinkUrl\{([^}]*)\}/i', $fieldotherconditions, $matches)) {
					$urlParts = explode('|', $matches[1]);
					foreach ($urlParts as $part) {
						[$key, $val] = array_map('trim', explode('=', $part));
						if (strcasecmp($key, 'ModuleType') == 0) $moduleType = $val;
						if (strcasecmp($key, 'urlData') == 0) $urlData = $val;
						if (strcasecmp($key, 'Target') == 0) $Target = $val;
					}

					// Now fetch URL using ModuleType
					if ($moduleType) {
						$getUrlQuery = "SELECT * FROM Master_ModuleType WHERE ID=$moduleType";
						$getUrl = db::getInstanceMaster()->db_select($getUrlQuery);
						// Assume result stored in $urlFromDB
						// Mock value for example
						$urlFromDB = $getUrl['result_set'][0]['url']; // Replace this with fetched URL from DB
						// urlData agar empty nahi hai to usi ko use karo, warna adjustmentFormId append karo
						if (!empty($urlData)) {
							$url = $urlData;
						} else {
							$url = $urlFromDB . "' + $('#adjustmentFormId').val() + '";
						}
						// $url = $urlFromDB . $urlData;
						$windowTarget = $Target;
					}
				}

				// Extract openLinkParameters{} content
				if (preg_match('/openLinkParameters\{([^}]*)\}/i', $fieldotherconditions, $matches)) {
					$paramParts = explode('|', $matches[1]);
					foreach ($paramParts as $param) {
						[$key, $val] = array_map('trim', explode(':', $param));
						$parameters[$key] = $val;
					}
				}

				// print_r($resultCheckAdjAvailable);

				$flds = '<div class="'.$divClass.'" ><label></label><button id="'.$arr[0].'" class="btn btn-default" style="display: block;font-size: 12px;padding: 3px 16px;  height: 26px !important; padding: 1px 2px !important; font-size: 12px !important;" type="button" tabindex="'.$arr[39].'"';
				$flds .= '>';
				if(isset($resultCheckAdjAvailable)){
					if($resultCheckAdjAvailable['num_rows'] == 0)
					{
						$flds .= 'AddAdjustment';
					}else{
						$flds .= 'EditAdjustment';
					}
				}else{
					$flds .= $arr[2];
				}
				$flds .= '</button></div>';
			}else{
				$flds = '<div class="'.$divClass.'" ><label></label><button id="'.$arr[0].'" class="btn btn-default" style="display: block;font-size: 12px;padding: 3px 16px;  height: 26px !important; padding: 1px 2px !important; font-size: 12px !important;" type="button" tabindex="'.$arr[39].'"';
				$flds .= '>'.$arr[2].'</button></div>';
			}

			echo $ddtableIncludes;
			echo "
				<script>
					$(document).ready(function(){
						$('#".$arr[0]."').keypress(function(e) {
							if(e.which == 13) {
								$('#". $arr[0]."').click();
							}
						});

						$('#". $arr[0]."').click(function(){
							// console.log('else');

							var FieldOtherCondition = '".str_replace("'","\'",json_encode($arr[6]))."';
							var newArr = ['', ''];

							// console.log('fieldotherconditions',FieldOtherCondition.length,FieldOtherCondition);
							if(FieldOtherCondition.length == 2){
								var baseUrl = window.location.origin + window.location.pathname +'?form='+$('#adjustmentFormId').val()+'&preview='+$('#entryid').val();
								window.location.href = baseUrl;
								return;
							}

							if (FieldOtherCondition.includes('openLinkParameters')) {
								var url = '".$url."'; 
								var FormID = '".$FormID."'; 

								// Dynamically build parameters
								const postData = {";
								if($resultCheckAdjAvailable['num_rows'] != 0){
									echo "editID: '".$resultCheckAdjAvailable['result_set'][0]['id']."',";
								}
								foreach ($parameters as $key => $val) {
									if(strlen($val) > 1){

										if (preg_match('/^[A-Z_][A-Za-z0-9_]*$/', $val)) {
											echo "\n\t\t\t\t$key: $val,";
										} else {
											echo "\n\t\t\t\t$key: $('#$val').val(),";
										}
									}
								}
								echo "
								};

								var windowOptions;
								if ('".$target."' === 'newwindow') {
									windowOptions = 'width=800,height=600,resizable=yes,scrollbars=yes';
								} else if ('".$target."' === 'newtab') {
									windowOptions = '_blank'; // new tab
								} else {
									windowOptions = '_self'; // sametab
								}
								
								openWindowWithPost(url, postData, windowOptions);
									
							}else{
								$.ajax({
									type : 'POST',
									dataType: 'json',
									data : {FieldOtherCondition : FieldOtherCondition, FormID : '".$FormID."'},
									url : 'getFieldType24Data.php',
									success : function(result){
									console.log(result);
										if(result['FormParameters'] != null && result['FormParameters'].length > 0){
				
											var extractedparameters = result['FormParameters'].split('|');
											var extractParameters = [];
											var requestParams = '';
				
											for(var i=0; i<extractedparameters.length; i++){
												console.log(extractedparameters[i]);
												extractParameters = extractedparameters[i].split(':');
												requestParams += extractParameters[0]+':';
												var NumberOfElements = document.querySelectorAll('[id='+extractParameters[0]+']');	
												console.log('NumberOfElements '+NumberOfElements.length);
												if(NumberOfElements.length == 2){
													if(extractParameters[1] == 17){
														requestParams += $(NumberOfElements[1]).val() !='' ? $(NumberOfElements[1]).val().trim() : '0';
													}else if(extractParameters[1] == 16){
														requestParams += $(NumberOfElements[1]).val() !='' ? $(NumberOfElements[1]).val().trim() : '0';
													}else{
														requestParams += $(NumberOfElements[1]).val() !='' ? $(NumberOfElements[1]).val().trim() : '0';
													}
												}else{
													if(extractParameters[1] == 17){
														requestParams += $('#'+extractParameters[0].trim()).val() !='' ? $('#'+extractParameters[0].trim()+'-text').val() : '0';

													}else if(extractParameters[1] == 16){
														requestParams += $('#'+extractParameters[0].trim()).val() !='' ? $('#'+extractParameters[0].trim()+'-text').val() : '0';

													}else{
														requestParams += $('#'+extractParameters[0].trim()).val() !='' ? $('#'+extractParameters[0].trim()).val() : '0';
													}
												}

												if(i != extractedparameters.length-1){
													requestParams += '|';
												}
											}
										}
										console.log('requestParams '+requestParams);

										CallSpForFieldType24(requestParams,result['GridResponse'],result['SpName'],result['PopulateGridID'],$('input[name=editID]').val(),'".$_SESSION['user_id']."',result['GridId'],result['PopulateField'],result['Type']);
										
									}
								});
							}


						});
					});
				</script>";
				?>
		
				<script>
					var pressedButtonId='';
					// Capture the button ID when a button is clicked to trigger saveAdjAmtButton button
					if(<?php echo "'".$arr[0]."'"; ?> == 'bankreco'){
						$('#saveAdjAmtCloseButton').on('click', function() {
							pressedButtonId = $(this).attr('id');
						});
					}

					//On escape button press cursor move to next tabindex field
					$('#myModal').on('hidden.bs.modal', function (e) {
						// alert("hi",pressedButtonId);
						if(<?php echo "'".$arr[0]."'"; ?> == 'bankreco'){
							saveBankRecoDetails();
							// if( pressedButtonId == ""){
								// $('#saveBankRecoDetails').click();
							// }else{
							// 	pressedButtonId = "";
							// }
						}
					});
				</script>
		<?php
		return $flds;
	}

	

	if($arr[1] == 25){  //HTML DIV
		global $extradb;

		$flds  .= '<div class="col-md-12" id="'.$arr[0].'"></div>';

		return $flds;
	}
}

function createMore1($m,$d,$gd,$r,$id,$FormID, $db){

	global $ddtableIncludes;
	global $k_debug;
	
	$v = array();
	$row1 = array();
	$formType = 14; // Added For Add More functionality
    
	if($id > 0){		
		// if($FormID == 4609 || $FormID == 4618){ 
			if($d[5] !== $db[1]){	
				//THIS HAS BEEN CHANGED BY MITESH ON 25/02/2025 AS THE TableUnique of Grid and TablePrimary of Main could be different as raised by Shrikant
				$sql = 'SELECT ' . $d[5] . ' FROM ' . $db[0] . ' WHERE ' . $db[1] . ' = ' . $id;
				$result = db::getInstance()->db_select($sql);
				$id = $result['result_set'][0][$d[5]];
 			}
		// }
		
		$sql = "SELECT * FROM ".$d[0]." WHERE ".$d[1]." = ".$id." ";
		if(strlen($d[10]) > 0){
			$sql .= $d[10];
		}else{
			$sql .= "ORDER BY ".$d[5]." DESC";
		}
		// if($FormID == 4609){ echo $sql;  }
		$result = db::getInstance()->db_select($sql);

        if($k_debug){	echo '<br /><br />' . $sql . '<br />'; print_r($result); echo '<br /><br />'; }
		$row = $result['result_set'];
		$size = sizeof($m);
		for($i = 0; $i < $result['num_rows']; $i++){
            $v[$i] = array($size);
			for($j = 0; $j < $size; $j++){
                // if($m[$j][0] == 18){
                //     // print_r($row[$i][$m[$j][1]]);
                //     $temp = $row[$i][$m[$j][1]];
                //     if(isset($temp)){
                //         $v[$i][$j] =  $temp->format('h:i:s'); //strtotime("H:ia", $temp);
                //     }
                // }else
				if($m[$j][0] == 6){
                    $temp = $row[$i][$m[$j][1]];                    
                    if(isset($temp)){
                        $v[$i][$j] =  $temp->format('Y-m-d'); //date("d-m-Y", strtotime($temp));
                    }else{
                        $v[$i][$j] = "";
                    }
                }else{
                    if(strlen($row[$i][$m[$j][1]]) == 0){
                        $v[$i][$j] = $row[$i][ucfirst($m[$j][1])];
                    }else{
                        $v[$i][$j] = $row[$i][$m[$j][1]];
                    }
                }
			}
			$v[$i][$j] = $row[$i][$d[1]];
			$v[$i][$j+1] = $row[$i][$d[5]];
			$v[$i][$j+2] = $row[$i]['uniqueid'];
		}
	}

	

	$d[8] = "";
	$downloadExcel = '';
	$tempDataSpName = "sp_ImportGrid_".$d[7]."";
	$checkSp = "SELECT COUNT(*) AS found FROM information_schema.routines WHERE routine_schema = 'DBO'  AND  routine_name = '$tempDataSpName'";
	$checkSpResult = db::getInstance()->db_select($checkSp);
	// print_r($checkSpResult);
	if($checkSpResult['result_set'][0]['found'] == 1){
		$d[8] = $tempDataSpName;
		
		$tempDataSpName = "sp_ImportGridExcelExport_".$d[7]."";
		$checkSp = "SELECT COUNT(*) AS found FROM information_schema.routines WHERE routine_schema = 'DBO'  AND  routine_name = '$tempDataSpName'";
		$checkSpResult = db::getInstance()->db_select($checkSp);
		// print_r($checkSpResult);
		if($checkSpResult['result_set'][0]['found'] == 1){
			$downloadExcel = '<a style="font-size: 11px;margin-top: -8px;display: block;float: right;padding-bottom: 0;" href="#" onclick="exportGridTableToExcelusingSP(\''.$d[7].'\',\'Excel for Grid - '.trim($d[2]).'\');" class="btn bizbtn" >Download Excel</a>'; 
		}else{
			//Blank Template
			$downloadExcel = '<a style="font-size: 11px;margin-top: -8px;display: block;float: right;padding-bottom: 0;" href="#" onclick="exportGridTableToExcel(\'GridTable'.$d[3].'\',\'Excel Import Template for Grid - '.trim($d[2]).'\')" class="btn bizbtn" >Download Blank Template</a>';
		}
	}
	// echo $d[8];
	$excelImport = "";
    if(strlen($d[8]) > 1){	//if grid import SP is given
		$excelImport = '<!--script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script-->
			<div class="col-md-6" style="float:right"><div class="col-md-6"></div><div class="col-md-6"><label class="custom-file-upload" style="margin-top:0;padding: 0px 12px;"><i class="fa fa-cloud-upload"></i> Grid Bulk Import <input type="file" name="BulkImport_GridTable'.$d[3].'" id="BulkImport_GridTable'.$d[3].'" accept=".csv,.xls,.xlsx, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"></label>
			'.$downloadExcel.'
			</div></div>
			<script src="https://cdn.jsdelivr.net/npm/xlsx@0.18.0/dist/xlsx.full.min.js"></script>
			<script>
				// console.log("BulkImport_GridTable'.$d[3].'");
				document.getElementById("BulkImport_GridTable'.$d[3].'").addEventListener("change", handleFileSelectGrid'.$d[3].', false);
				function handleFileSelectGrid'.$d[3].'(evt) {
					var files = evt.target.files; // FileList object
					// console.log(evt);
					// console.log(files);
					var xl2json = new ExcelToJSONGrid'.$d[3].'();
					xl2json.parseExcel(files[0]);
				}
				
				var ExcelToJSONGrid'.$d[3].' = function() {
					
					var cols = [];
					this.parseExcel = function(file) {
						var reader = new FileReader();
						reader.onload = function(e) {
							var data = e.target.result;
							var workbook = XLSX.read(data, {
								type: "binary"
							});
							var rwCnt = 0;
							workbook.SheetNames.forEach(function(sheetName) {
							
								var XL_row_object = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName], {
									defval: null // This ensures empty cells are represented as null
								});

								// Loop through each row
								// XL_row_object.forEach(row => {
								// 	for (let key in row) {
								// 		const value = row[key];
								// 		if (typeof value === "number" && value > 20000 && value < 60000) {
								// 			// Likely an Excel date serial (between 1954 and 2079)
								// 			const jsDate = XLSX.SSF.parse_date_code(value);
								// 			if (jsDate) {
								// 				// Format the date as "YYYY-MM-DD"
								// 				const formattedDate = `${jsDate.y}-${String(jsDate.m).padStart(2, "0")}-${String(jsDate.d).padStart(2, "0")}`;
								// 				row[key] = formattedDate;
								// 			}
								// 		}
								// 	}
								// });

								// Only keep columns where the first row is not null or blank
								// if (XL_row_object.length > 0) {
								// 	var firstRow = XL_row_object[0];
								// 	for (var key in firstRow) {
								// 		console.log("CHECKING...", firstRow[key]);
								// 		if (firstRow[key] === null || firstRow[key] === "") {
								// 			console.log("DELETING...");
								// 			// If the first cell in this column is blank, delete the column
								// 			XL_row_object.forEach(row => delete row[key]);
								// 		}
								// 	}
								// }
								// Convert rows into a JSON array
								var productList = JSON.parse(JSON.stringify(XL_row_object));
								console.log(XL_row_object); // Check the output
								console.log(productList);

								ImportGridExcelValidateSP(productList,"' . $d[7] . '","'.$id.'");

							});
						};
						reader.onerror = function(ex) {
						  console.log(ex);
						};
						reader.readAsBinaryString(file);
					};
				};
			</script>';
	}
		
	$temp = '<div class="grid'.$d[3].'" id="grid-id-'.$d[7].'" style="margin-top:2px;"><label class="col-md-12"><b>'.$d[2].'</b>&nbsp;</label>';
	// if(strlen($gd[1]) > 1){

	// 	$temp .= $excelImport.' <div class="col-md-6" style="margin-bottom:4px;"> <button  class="btn btn-primary" type="button"  id="GridMultyDDModal-'.$d[7].'" tabindex="'.$gd[2].'">'.$gd[3].'</button>
	// 		<input type="hidden" id="GridMultyDDModal-hidden-'.$d[7].'" value="">
	// 		<input type="hidden" id="MultiSelectGridId-hidden-'.$d[7].'" value="'.$d[7].'"></div>';

	// 	echo $ddtableIncludes;
	// 	echo "
	// 		<script>
	// 			$(document).ready(function(){
	// 				$('#GridMultyDDModal-".$d[7]."').keypress(function(e) {
	// 					if(e.which == 13) {
	// 						$('#GridMultyDDModal-". $d[7]."').click();
	// 					}
	// 				});

	// 				$('#GridMultyDDModal-". $d[7]."').click(function(){
						
	// 					$('#modalLabelLarge').text('Grid Multiple selection Dropdown');				
	// 					$('#DropDownModal').modal('show');
	// 					jqxhr = $.ajax('getMultyDDValue1.php?FormID=".$FormID."&GridID=".$d[7]."&DBFieldName=". $gd[0]."&DBFieldType=14&isGrid=0')
	// 					.done(function () {
	// 							tableName= '#ddTable',
	// 							data1 = JSON.parse(jqxhr.responseText);
	// 							DBFieldName='". $d[7]."';
	// 							DBFieldType ='14';
	// 							GridID = '".$d[7]."';
	// 							gridResponseParams = data1['gridResponseParams'];
	// 							isGrid = '0';
	// 							FormID = '".$FormID."';
								
	// 							var row = '<tr>';
	// 							var rowFilter = '<tr class=\"headerFilter\">';
	// 							$.each(data1.columns, function (k, colObj) {
									
	// 								row += '<th>' + colObj.name + '</th>';
									
	// 								var searchColName = colObj.name.trim();
	// 								if(k == 0){
	// 									rowFilter += '<th></th>';
	// 								}else{
	// 									rowFilter += '<th><input type=\"search\" class=\"form-control\" autocomplete=\"off\" onkeydown=\"SearchFunction(event,DBFieldName,DBFieldType,FormID,GridID,isGrid,gridResponseParams)\" placeholder=' + searchColName + ' id='+searchColName+' name='+searchColName+'></th>';
	// 								}
									
	// 							});
	// 							rowFilter += '</tr>';
	// 							row += '</tr>';


	// 							if('GridMultyDDModal-". $d[7]."' != $('#DropDownModalID').val()){
	// 								$('#DropDownModalID').val('GridMultyDDModal-".$d[7]."');
	// 								if ($.fn.dataTable.isDataTable('#ddTable')) {
	// 									$('#ddTable').DataTable().state.clear();
	// 									$('#ddTable').DataTable().destroy();
	// 								}
	// 							}else{
	// 								if ($.fn.dataTable.isDataTable('#ddTable')) {
	// 									$('#ddTable').DataTable().destroy();
	// 								}
	// 							}

								
	// 							$('#ddTable').html('<thead></thead>');
	// 							$('#ddTable thead').html(row + rowFilter);

	// 							$('.headerFilter th:first').hide();
								
	// 							var is_variable='';
	// 							var is_value='';
	// 							$('#PopulateGridButton').show();
	// 							// console.log('calling load_dd 5995');
								
	// 							load_dd(DBFieldName,DBFieldType,FormID,GridID,isGrid,gridResponseParams);
	// 						});
	// 				});
	// 			});
	// 		</script>";
	// }else{
	// 	$temp .= $excelImport;
	// }

	// if(strlen($gd[1]) > 1){

		// $temp .= $excelImport.' <div class="col-md-6" style="margin-bottom:4px;"> <button  class="btn btn-primary" type="button"  id="'.$d[7].'" onclick="toggleModal()" tabindex="'.$gd[2].'" ';
		
		// $SPName = 'sp_GetData_'.$FormID.'_'.$d[7];
		// $sql = "SELECT COUNT(*) AS found FROM information_schema.routines WHERE routine_schema = 'DBO'  AND  routine_name = '$SPName'";
		// $checkSp = db::getInstance()->db_select($sql);
		// if($checkSp['result_set'][0]['found'] == 1){
		// 	$temp .=" data-systemtype=\"2\"";
		// }else{
		// 	$temp .=" data-systemtype=\"1\"";
		// }
		
		// $temp .= '>'.$gd[3].'</button><input type="hidden" id="GridMultyDDModal-hidden-'.$d[7].'" value=""><input type="hidden" id="MultiSelectGridId-hidden-'.$d[7].'" value=""></div>';

		// echo $ddtableIncludes;

		$SPName = 'sp_GetData_'.$FormID.'_'.$d[7].'_'.$gd[0];
		$sql = "SELECT COUNT(*) AS found FROM information_schema.routines WHERE routine_schema = 'DBO'  AND  routine_name = '$SPName'";
		$checkSp = db::getInstance()->db_select($sql);
		if($checkSp['result_set'][0]['found'] == 1){
			$temp .= $excelImport.' <div class="col-md-6" style="margin-bottom:4px;"> <button  class="btn btn-primary" type="button"  id="'.$gd[0].'-text" data-buttontype="grid-'.$d[7].'" onclick="toggleModal()" tabindex="'.$gd[2].'" ';
			$temp .=" data-systemtype=\"2\"";

			$temp .= '>'.$gd[3].'</button>';
			$temp .= '<input type="hidden" id="GridMultyDDModal-hidden-'.$gd[0].'" value="">
				<input type="hidden" id="MultiSelectGridId-hidden-'.$gd[0].'" value=""></div>';

			echo $ddtableIncludes;
		}else{
			if(strlen($gd[1]) > 1){
				$temp .= $excelImport.' <div class="col-md-6" style="margin-bottom:4px;"> <button  class="btn btn-primary" type="button"  id="'.$d[7].'" onclick="toggleModal()" tabindex="'.$gd[2].'" ';
				$temp .=" data-systemtype=\"1\"";
				$temp .= '>'.$gd[3].'</button>';
				$temp .= '<input type="hidden" id="GridMultyDDModal-hidden-'.$d[7].'" value="">
					<input type="hidden" id="MultiSelectGridId-hidden-'.$d[7].'" value=""></div>';

				echo $ddtableIncludes;
			}
		}
		
	// }else{
		$temp .= $excelImport;
	// }


	// style="max-height:200px;overflow-y: scroll;"
	$temp .= '<div class="col-md-12 panel-body" id="panel'.$d[3].'" style="overflow-x:auto;padding: 0 15px;max-height:'.$gd[4].'px;overflow-y: auto;">';
	$tableStart = '<table style="table-layout: fixed;word-wrap: break-word;" id="GridTable" class="table table-responsive table-bordered table-hover tmp_GridTable'.$d[3].'">';
	$tableEnd = '</table>';
	$tableHead = '<thead class=" thead-light"><tr>';
	
	$flds = '';
	//$flds = '<div class="form-group grp'.$d[3].'" id="'.$d[3].'0">';
	// $flds = '<td style="padding:2px 3px !important;" class="AddEdit" ><a  style="padding:3px;"  href="javascript:void(0)" class="btn btn-success add'.$d[3].'"><span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span></a><a  style="padding:3px; display:none;" href="javascript:void(0)" class="btn btn-success update'.$d[3].'"><span class="glyphicon glyphicon glyphicon-ok" aria-hidden="true"></span></a><a  style="padding:3px; margin-left:2px; display:none;" href="javascript:void(0)" class="btn btn-info refresh'.$d[3].'"><span class="glyphicon glyphicon glyphicon-refresh" aria-hidden="true"></span></a></td>';
	$headerGridStart = '<tr class="grp'.$d[3].'" id="'.$d[3];
	$headerGridEnd   = '">';
	$reqf = [];
	$maxTabindex = 0;
	$sizeoffield = 0;
	$lastFieldId;
	if($k_debug){	echo '<br /><br />'; print_r($m); echo '<br /><br />'; }
	for($i = 0; $i < sizeof($m); $i++){		
		if($m[$i][24] > $maxTabindex){
			//Add Tabindex in plus button
			if($m[$i][0] != 0){
				$maxTabindex = $m[$i][24];
			}
		}

		// Loop through the fields to find the last valid field
		// Check if field is not readonly and tabindex is greater than 0
		if ($m[$i][23] != 1 && $m[$i][24] > 0) {
			// Store the ID of the current field
			$lastFieldId = "kreon-grid-".$d[3].$m[$i][1];  // Assuming $m[$i][$j][0] is the field ID
		}

		// print_r($m[$i]);
	    //style='width:".$m[$i][7]."px' width='".$m[$i][7]."'
		
		if($m[$i][5] == 1){ //to check if required field
			// $reqf[$i] = '<span class="required"> *</span>';
			$reqData = 'data-f-color="ffff0000"';	//compulsor in excel
		}else{
			$reqData = '';
		}

		if($m[$i][26] == 0 ){  //to check visibility
			$thClass = "thHidden";
		}else{
			$thClass = "";
		}

		$tableHead .= '<th class="'.$thClass.'" width="'.$m[$i][7].'"  '.$reqData.'>'.$m[$i][2].' ';

		if($m[$i][13] > 0){
			$tableHead .= '<button type="button" class="amodal" id="AddBtnModal-'.$m[$i][1].'" value="'.$m[$i][13].'-'.$m[$i][1].'-'.$formType.'" style="background: transparent;border: none !important;font-size:1;color:blue"><i class="fa fa-plus-circle"></i></button>';
		}

		$tableHead .= $reqf[$i]."</th>";

		
		if($m[$i][0] == 0){	//IF HIDDEN		width="'.$m[$i][7].'" style="width:'.$m[$i][7].'px" 

			$flds .= '<td class='.$thClass.'><input  type="hidden" value="'.$gridInputVal.'" class="form-control" name="kreon-grid-'.$d[3].''.$m[$i][1].'[]" id="kreon-grid-'.$d[3].''.$m[$i][1].'" placeholder="'.$m[$i][2].'" '.$m[$i][6].' tabindex="'.$m[$i][24].'" ';

			$flds .= '/></td>';

		}

		if($m[$i][0] == 1){	//IF TEXT BOX		width="'.$m[$i][7].'" style="width:'.$m[$i][7].'px" 

			//carry on field
			if(isset($_SESSION['carryOn'])){
				if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
					if(isset($_SESSION['carryOn'][$d[3].$m[$i][1]])){
						$gridInputVal = $_SESSION['carryOn'][$d[3].$m[$i][1]];
						unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
					}else{
						$gridInputVal = '';
					}
				}
			}
		
			//Set Default Value
			if(strlen($m[$i][17]) > 0 && $gridInputVal == ''){
				$gridInputVal = $m[$i][17];
			}

			$flds .= '<td class='.$thClass.'><input  type="text" value="'.$gridInputVal.'" class="form-control" name="kreon-grid-'.$d[3].''.$m[$i][1].'[]" id="kreon-grid-'.$d[3].''.$m[$i][1].'" placeholder="'.$m[$i][2].'" '.$m[$i][6].'  tabindex="'.$m[$i][24].'" ';

			if(strlen($m[$i][19]) > 0){
				$flds .= "onfocusout=\"isValidText('".$m[$i][19]."','kreon-grid-".$d[3].$m[$i][1]."');\"";
			}

			//Check validation using sp on lost focus
			if($m[$i][21] == 1){
				$flds .= "onfocusout=\"validateOnLostFocusSp(this,'".$d[7]."');\"";
			}

			if($m[$i][23] != 0){ //to check if field readonly
				$flds .= " readonly ";
			}

			//Added for getting dynamic decimalplaces	
			if($m[$i][29] != ''){ //to check if field readonly
				$flds .=" data-key=\"".$m[$i][29]."\"";
			}

			
			//On keyup focus exicute formula
			if(strlen($m[$i][11]) > 2){
				$onkeyupHidden = "";
				$flds .= " onkeydown=\"CreateOnkeyupScriptGrid(this,'grid');\"";
				$onkeyupHidden = '<input type="hidden" id="kreon-grid-'.$d[3].$m[$i][1].'-onkeyup-hidden" value="' . $m[$i][11] . '" />';
			}
			
			// if($m[$i][12] > 0){
			// 	$onchange = " onblur=\"CreateOnChangeScriptGridCalculation('kreon-grid-".$d[3]."".$m[$i][1]."',".$m[$i][12].", 'grid',0);\"";
			// 	$flds .= $onchange;
			// }

			if(strlen($m[$i][20]) > 0){
				$onGridkeyupHidden = '<input type="hidden" id="kreon-grid-'.$d[3].''.$m[$i][1].'-hidden" value="' . $m[$i][20] . '" />';

				echo '<script>
					$(document).ready(function() { 
						$("#kreon-grid-'.$d[3].''.$m[$i][1].'").keydown(function(e) {
						
							if(e.which == 13) {
								CreateOnkeyEnterScript(this,"kreon-grid-'.$d[3].''.$m[$i][1].'");
							}
							
						});
					}); 
				</script>';
			}

			//Once lost focus from perticular field then again don't allow to focus on this field
			if($m[$i][25] == 1){
				$flds .= "onblur=\"onBlurUpdateLostFocusTabIndex(this,'".$m[$i][0]."');\"";
			}

			//Check value is exist or not
			$onlostfocusHidden = "";
			if(strlen($m[$i][27]) > 5){
				$flds .= "onfocusout=\"onLostFocus(this,0,'grid');\"";
				$onlostfocusHidden = '<input type="hidden" id="kreon-grid-'.$d[3].''.$m[$i][1].'-hidden" value="'.$m[$i][27].'">';
			}


			$flds .= '/>';
			$flds .= $onkeyupHidden;
			$flds .= $onGridkeyupHidden ;
			$flds .= $gridonlostfocuscalculationHidden;
			$flds .= $onlostfocusHidden;

			$flds .= '</td>';
			$gridInputVal = "";
		}

		if($m[$i][0] == 3){	//IF TEXTAREA		width="'.$m[$i][7].'" style="width:'.$m[$i][7].'px" 
			//carry on field
			if(isset($_SESSION['carryOn'])){
				if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
					if(isset($_SESSION['carryOn'][$d[3].$m[$i][1]])){
						$gridInputVal = $_SESSION['carryOn'][$d[3].$m[$i][1]];
						unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
					}else{
						$gridInputVal = '';
					}
				}
			}

			//Set default value
			if(strlen($m[$i][17]) > 0 && $gridInputVal == ''){
				$gridInputVal = $m[$i][17];
			}

			$flds .= '<td class='.$thClass.'><textarea class="form-control" name="kreon-grid-'.$d[3].''.$m[$i][1].'[]" id="kreon-grid-'.$d[3].''.$m[$i][1].'" placeholder="'.$m[$i][2].'" '.$m[$i][6].' tabindex="'.$m[$i][24].'" ';
			if(strlen($m[$i][19]) > 0){
				$flds .= "onfocusout=\"isValidText('".$m[$i][19]."','kreon-grid-".$d[3].$m[$i][1]."');\"";
			}

			//Check validation using sp on lost focus
			if($m[$i][21] == 1){
				$flds .= "onfocusout=\"validateOnLostFocusSp(this,'".$d[7]."');\"";
			}

			if($m[$i][23] != 0){ //to check if field readonly
				$flds .= " readonly ";
			}

			//Once lost focus from perticular field then again don't allow to focus on this field
			if($m[$i][25] == 1){
				$flds .= "onblur=\"onBlurUpdateLostFocusTabIndex(this,'".$m[$i][0]."');\"";
			}
			
			$flds .= '>'.$gridInputVal.'</textarea></td>';

			$gridInputVal = "";
		}

		if($m[$i][0] == 12){	//IF Image
			$flds .= '<td class='.$thClass.'><label class="custom-file-upload" style="margin-top:0px; padding:3px 12px;"><i class="fa fa-cloud-upload"></i><input type="file" class="form-controlz" name="kreon-grid-'.$d[3].''.$m[$i][1].'[]" id="kreon-grid-'.$d[3].''.$m[$i][1].'" '.$m[$i][6].' tabindex="'.$m[$i][24].'" /></label></td>';

			// $flds .= '<td><input type="file" class="form-controlz" name="kreon-grid-'.$d[3].''.$m[$i][1].'[]" id="kreon-grid-'.$d[3].''.$m[$i][1].'" '.$m[$i][6].' /></td>';
		}

		if($m[$i][0] == 6){	//IF Date BOX		added by sneha on 01-11-2023
			//carry on field
			if(isset($_SESSION['carryOn'])){
				if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
					if(isset($_SESSION['carryOn'][$d[3].$m[$i][1]])){
						$gridInputVal = $_SESSION['carryOn'][$d[3].$m[$i][1]];
						unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
					}else{
						$gridInputVal = '';
					}
				}
			}

			if(strlen($m[$i][17]) >= 1 && $m[$i][17] == 'today'){
				$gridInputVal = date('Y-m-d');
			}elseif(strlen($m[$i][17]) >= 1 && $m[$i][17] == 'tomorrow'){
				$datetime = new DateTime('tomorrow');
				$gridInputVal = $datetime->format('Y-m-d');
			}elseif($gridInputVal != NULL ){
				if(gettype($gridInputVal) == 'string'){
					$date = strtotime($gridInputVal);
					$gridInputVal = date('Y-m-d', $date);
				}else{
					$gridInputVal = $gridInputVal->format('Y-m-d');
				}
			}

			$flds .= '<td class='.$thClass.'><input type="date" value="'.$gridInputVal.'" class="form-control" name="kreon-grid-'.$d[3].''.$m[$i][1].'[]" id="kreon-grid-'.$d[3].''.$m[$i][1].'" placeholder="'.$m[$i][2].'" '.$m[$i][6].' tabindex="'.$m[$i][24].'"';
			
			//Check validation using sp on lost focus
			if($m[$i][21] == 1){
				$flds .= "onfocusout=\"validateOnLostFocusSp(this,'".$d[7]."');\"";
			}

			if($m[$i][23] != 0){ //to check if field readonly
				$flds .= " readonly ";
			}

			//Once lost focus from perticular field then again don't allow to focus on this field
			if($m[$i][25] == 1){
				$flds .= "onblur=\"onBlurUpdateLostFocusTabIndex(this,'".$m[$i][0]."');\"";
			}

			$flds .= '/></td>';
			$gridInputVal = '';
		}

		if($m[$i][0] == 7){	//IF Number BOX		added by sneha on 01-11-2023
			//carry on field
			if(isset($_SESSION['carryOn'])){
				if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
					if(isset($_SESSION['carryOn'][$d[3].$m[$i][1]])){
						$gridInputVal = $_SESSION['carryOn'][$d[3].$m[$i][1]];
						unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
					}else{
						$gridInputVal = "";
					}
				}
			}

			// echo $m[$i][17]." == ".$gridInputVal;

			//Set default value
			if(strlen($m[$i][17]) > 0 && $gridInputVal == ""){
				$gridInputVal = $m[$i][17];
			}
			 
			$flds .= '<td class='.$thClass.'><input type="number" value="'.$gridInputVal.'" class="form-control" name="kreon-grid-'.$d[3].''.$m[$i][1].'[]" id="kreon-grid-'.$d[3].''.$m[$i][1].'" '.$m[$i][6].'  tabindex="'.$m[$i][24].'" ';
			
			// Only add onfocus select if field is not read-only
			if ($m[$i][23] == 0) {
				$flds .= ' onfocus="this.select()"';
			}

			//If step added in fieldothercondition then don't aloow to type
			if($m[$i][6] > 2){
				if(strpos($m[$i][6], 'step') !== false){
					$flds .= "onkeydown=\"isValidDigit('".$m[$i][6]."','kreon-grid-".$d[3].$m[$i][1]."');\"";
				}
			}
			
			
			if(strlen($m[$i][19]) > 0){
				$flds .= "onkeydown=\"isValidText('".$m[$i][19]."','kreon-grid-".$d[3].$m[$i][1]."');\"";
			}

			//On keyup focus exicute formula
			if(strlen($m[$i][11]) > 2){
				$onkeyupHidden = "";
				$flds .= " onkeydown=\"CreateOnkeyupScriptGrid(this,'grid');\"";
				$onkeyupHidden = '<input type="hidden" id="kreon-grid-'.$d[3].$m[$i][1].'-onkeyup-hidden" value="' . $m[$i][11] . '" />';
			}

			// if($m[$i][12] > 0){
			// 	$onchange = " onblur=\"CreateOnChangeScriptGridCalculation('kreon-grid-".$d[3]."".$m[$i][1]."',".$m[$i][12].", 'grid',0);\"";
			// 	$flds .= $onchange;
			// }

			//Check validation using sp on lost focus
			if($m[$i][21] == 1){
				$flds .= "onfocusout=\"validateOnLostFocusSp(this,'".$d[7]."');\"";
			}

			if($m[$i][23] != 0){ //to check if field readonly
				$flds .= " readonly ";
			}

			//Added for getting dynamic decimalplaces	
			if($m[$i][29] != ''){ //to check if field readonly
				$flds .=" data-key=\"".$m[$i][29]."\"";
			}

			//Once lost focus from perticular field then again don't allow to focus on this field
			if($m[$i][25] == 1){
				$flds .= "onblur=\"onBlurUpdateLostFocusTabIndex(this,'".$m[$i][0]."');\"";
			}

			$flds .= '/>'.$onkeyupHidden.'</td>';

			$gridInputVal = "";
		}

		if($m[$i][0] == 5){	//Dropdown From DB

			// select option by enter then cursor going to next field
			echo "<script>
				$(document).ready(function(){

					// $('#kreon-grid-".$d[3].$m[$i][1]."').on('select2:select, select2:close', function (e) { 
					// 	console.log('inform');
					// 	var self = $(this), form = self.parents('form:eq(0)'), focusable, next;
					// 	focusable = form.find('input,a,select,button,textarea').filter(':visible');
					// 	next = focusable.eq(focusable.index(this)+1);
					// 	if (next.length) {
					// 		next.focus();
					// 	} 
					// 	return false;
					// });

                    //Onload focus on TabIndex if field tabindex 1
                    if('".$m[$i][24]."' == 1){
                        setTimeout(
                            function() {
								console.log('Line 7373 focus')
                                $('#kreon-grid-".$d[3].$m[$i][1]."').next('span').find('.select2-selection--single').focus();
                            }, 300);
                    }

					//Once lost focus from perticular field then again don't allow to focus on this field
					if('".$m[$i][25]."' == 1){
						$('#kreon-grid-".$d[3].$m[$i][1]."').on('select2:open select2:select', function (e) {
							onBlurUpdateLostFocusTabIndex(this,'".$m[$i][0]."');
						});
					}

					if('".$m[$i][23]."' == 1){ //to check if field readonly
						setTimeout(
                            function() {
                                $('#kreon-grid-".$d[3].$m[$i][1]."').next('span').find('.select2-selection--single').css('pointer-events','none').css('background','#eee');
                            }, 300);
					}

					
				});
			</script>";
			if($m[$i][3] < 0){	//IF to be taken from DB
				$index = ((-1) * $m[$i][3]) - 1;
				global $moreextradb;

				// if($FormID == 5098 && $m[$i][1] == 'taxid'){
				// 	print_r($moreextradb);
				// }
				//updated by pornima now moreextradb getting using dbFieldName
				// $dbarray = $moreextradb[$index];
				$dbarray = $moreextradb[$m[$i][1]];
				if($k_debug){ echo "<br /><br />MITESH DEBUG==>>".$i.$m[$i][1]."<pre>"; print_r($m[$i]); print_r($dbarray); echo "</pre>";}
				//carry on field
				if(isset($_SESSION['carryOn'])){
					if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
						if(isset($_SESSION['carryOn'][$d[3].$m[$i][1]])){
							$gridInputVal = $_SESSION['carryOn'][$d[3].$m[$i][1]];
							unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
						}else{
							$gridInputVal = '';
						}
					}
				}

				//If DefaultValue, OnDropDownAjax, CarryOn not empty 
				// if(strlen($m[$i][14]) > 0 || strlen($m[$i][17]) > 2 || $gridInputVal !=''){
				// if(strlen($dbarray[3]) > 2){
				// 	$sql1 .= " AND ".$dbarray[3];
				// }
				// echo "<br>".$m[$i][17] ."===". $gridInputVal;

				if(strlen($m[$i][17]) > 2 && $gridInputVal !=''){
					// If carryon field then added in where condition
					$sql1 = "SELECT ".$dbarray[1].",".$dbarray[2]." FROM ".$dbarray[0];
					if(strlen($dbarray[3]) > 1){
						$sql1 .= " ".$dbarray[3];
					}else{
						$sql1 .= " WHERE 1=1";
					}
					$sql1 .= " AND ".$dbarray[1]."=".$gridInputVal;

					$result1 = db::getInstance()->db_select($sql1);
					$row1 = $result1['result_set'];
				}else if(strlen($m[$i][17]) > 0){
					// If Deault value then added in where condition
					$sql1 = "SELECT ".$dbarray[1].",".$dbarray[2]." FROM ".$dbarray[0];	
					if(strlen($dbarray[3]) > 1){
						$sql1 .= " ".$dbarray[3];
					}else{
						$sql1 .= " WHERE 1=1";
					}
					$sql1 .= " AND ".$dbarray[1]."=".$m[$i][17];

					$result1 = db::getInstance()->db_select($sql1);
					$row1 = $result1['result_set'];
				}else if($gridInputVal !=''){
					// If carryon field then added in where condition
					$sql1 = "SELECT ".$dbarray[1].",".$dbarray[2]." FROM ".$dbarray[0];
					if(strlen($dbarray[3]) > 1){
						$sql1 .= " ".$dbarray[3];
					}else{
						$sql1 .= " WHERE 1=1";
					}
					$sql1 .= " AND ".$dbarray[1]."=".$gridInputVal;

					$result1 = db::getInstance()->db_select($sql1);
					$row1 = $result1['result_set'];
				}
			
				$ddsql = "SELECT TOP 10 ".$dbarray[1].",".$dbarray[2]." FROM ".$dbarray[0]." ".$dbarray[3] ." ";
				$ddresult = db::getInstance()->db_select($ddsql);
				// print_r($ddresult);
				if($ddresult){
					$DDSearchPreLoadedArray = [];
					for($r=0; $r < count($ddresult['result_set']); $r++){
						array_push($DDSearchPreLoadedArray,$ddresult['result_set'][$r][$dbarray[2]]);
					}
					$DDSearchPreLoadedD = '';
					$DDSearchPreLoadedD = implode(',',$DDSearchPreLoadedArray);
					
					echo "<script>
						$(document).ready(function(){
							var fieldCondition = '\".$m[$i][6].\"';
							var finalParams = ['".$dbarray[1]."','".$dbarray[2]."'];
							var DDSearchMinInputLen = [];
							if('".$m[$i][15]."' > 0){
								DDSearchMinInputLen['kreon-grid-".$d[3].$m[$i][1]."'] = '".$m[$i][15]."';
							}else{
								DDSearchMinInputLen['kreon-grid-".$d[3].$m[$i][1]."'] = 0;
							}
							var DDSearchPreLoadedData = [];
							var DDSearchPreLoadedDataString = [];
							DDSearchPreLoadedData['kreon-grid-".$d[3].$m[$i][1]."'] = '".json_encode($DDSearchPreLoadedD)."';

							if(DDSearchPreLoadedData['kreon-grid-".$d[3].$m[$i][1]."'].length > 0){
								DDSearchPreLoadedDataString['kreon-grid-".$d[3].$m[$i][1]."'] = ' or more char (like: '+ DDSearchPreLoadedData['kreon-grid-".$d[3].$m[$i][1]."'] +')';
							}else{
								DDSearchPreLoadedDataString['kreon-grid-".$d[3].$m[$i][1]."'] = ' char ';
							}
	
							$('#kreon-grid-".$d[3].$m[$i][1]."').select2({
								//tags: true,
								// multiple: true,
								tokenSeparators: [','],
								//allowClear: true,
								// placeholder: 'select',
								// debug: true,
								minimumInputLength: DDSearchMinInputLen['kreon-grid-".$d[3].$m[$i][1]."'],
								language: {
									inputTooShort: function() {
										return 'Enter ' + DDSearchMinInputLen['kreon-grid-".$d[3].$m[$i][1]."'] + ' '+ DDSearchPreLoadedDataString['kreon-grid-".$d[3].$m[$i][1]."'] ;
									}
								},
								ajax: {
									url: 'getDropDownValue.php',
									dataType: 'json',
									type: 'POST',
									data: function (params) {
										// var hidden = $('form').find(':hidden');	
										var hidden = $('form').find('input[type=\'hidden\']');
										hidden.show();   
										var  formSerialize = $('form').serialize();	
										hidden.hide(); 			
										var queryParameters = {
											searchText: encodeURIComponent(params.term)
											,searchParam:'".$dbarray[2]."'
											,fromRepo:'".$dbarray[0]."'
											,repoCondition:'".$dbarray[3]."'
											,responseParams:finalParams.toString()
											,currentId:'".$m[$i][1]."'
											,FormID:'".$FormID."'
											,GridID:'".$d[7]."'
											,gridSerial:'".$d[3]."'
											,searchText1: formSerialize
										}
										return queryParameters;
									},
									processResults: function (data) {
										return {
											results: $.map(data['data'], function (item) {
												return {
													text: item.ddVal,	
													id: item.ddID
												}
											})
										};
									}
								}
							});

							// initSelection : function (element, callback) {
							// 	var data = {id: 'IN', text: 'INDIA'};
							// 	callback(data);
							// }
							// var newOption = new Option('Pending, 1, false, false);
							// $('#kreon-grid-".$d[3].$m[$i][1]."').append(newOption).trigger('change');
						});

					</script>";
				}
				
				$flds .= '<td class='.$thClass.'><select name="kreon-grid-'.$d[3].''.$m[$i][1].'[]" id="kreon-grid-'.$d[3].''.$m[$i][1].'" class="form-control populate" tabindex="'.$m[$i][24].'" ';
				$changeScript = "";
				$onchangeHidden = "";
				
				if($m[$i][8] == 1){
					$onchange = " onchange=\"CreateOnchangeScriptGrid(this);\"";
					$flds .= $onchange;
					$onchangeHidden = '<input type="hidden" id="kreon-grid-'.$d[3].$m[$i][1].'-hidden" value="' . $m[$i][9] . '" />';//,' . $m[$i][1] . ',' . $m[$i][0] . '
				}

				//Check validation using sp onChange
				if($m[$i][22] == 1){
					$flds .= "onchange=\"validateOnChangeSp(this,'".$d[7]."');\"";
				}

				//Check validation using sp on change
				if($m[$i][21] == 1){
					$flds .= "onchange=\"validateOnLostFocusSp(this,'".$d[7]."');\"";
				}

                //On change exicute formula
				$onkeyupHidden ='';
				if(strlen($m[$i][11]) > 2){
					$onkeyupHidden = "";
					$flds .= " onchange=\"CreateOnkeyupScriptGrid(this,'grid');\"";
					$onkeyupHidden = '<input type="hidden" id="kreon-grid-'.$d[3].$m[$i][1].'-onkeyup-hidden" value="' . $m[$i][11] . '" />';
				}
				$flds .= '>';
				if($gridInputVal != '' && strlen($m[$i][17]) == 0){
					//For CarryOn 
					for($j = 0; $j < sizeof($row1); $j++){ 
						$flds .= '<option  value="'. $row1[$j][$dbarray[1]] .'" selected>'.$row1[$j][$dbarray[2]].'</option>&nbsp;';
					}
				}else if($gridInputVal == '' && strlen($m[$i][17]) > 0){
					//For DefaultValue
					for($j = 0; $j < sizeof($row1); $j++){ 
						$flds .= '<option  value="'. $row1[$j][$dbarray[1]] .'" selected>'.$row1[$j][$dbarray[2]].'</option>&nbsp;';
					}
				}else{
					$flds .= '<option value="0">'.$m[$i][2].'</option>';
				}

				$flds .= '</select>'.$onchangeHidden.$onkeyupHidden.'</td>';

				echo "<script>
					$(document).ready(function(){
						if ($('.toggle').is(':visible')) {
							$('#kreon-grid-".$d[3].$m[$i][1]."').next('span').css('width','100%');
						}
					});
					</script>";

				if($m[$i][23] > 0){ //to check if field readonly
					echo '<script>
						$("#kreon-grid-'.$d[3].''.$m[$i][1].'").next("span").find(".select2-selection--single").css("pointer-events","none").css("background","#eee");
					</script>';
				}
				$row1 = [];
			}else{			
				// if($FormID == 5102 ){ echo "<br /><br/>" . $m[$i][1] . " -------- "; var_dump($r[$m[$i][3]]);}
				$flds .= '<td><select name="'.$d[3].''.$m[$i][1].'[]" id="'.$m[$i][1].'" class="form-control" '.$m[$i][6].' >';	//<option value="0">'.$m[$i][2].'</option>
				if(is_array($r[$m[$i][3]])){
					for($j = 0; $j < sizeof($r[$m[$i][3]]); $j++){
						$k = $j+1;  //value
						$flds .= '<option value="'. $k .'" >'.$r[$m[$i][3]][$j].'</option>&nbsp;';
					}
				}else{
					if($_SESSION['Category'] == 2) //Show error for developer login else only error code
						echo "<p style='font-size:20px; color:red;'>Error Code 652: Grid Fields for ".$m[$i][1]." => SOLUTION: Add -1 to ValueFromDb to the Field</p>";
					else 
						echo "<p style='font-size:20px; color:red;'>Error Code 652: Grid Fields for ".$m[$i][1].", Contact Developer</p>";
					$sqlDev = "INSERT INTO DeveloperErrorLogs([FormID],[CompanyID],[DivisionID],[YearCode],[Database_name],[ErrorCode],[ErrorField],[Description],[RecommendedSolution],[CreatedBy],[CreatedAt])Values("
								. $FormID .",". $_SESSION['dbCompany'].",".$_SESSION['dbDivision'].",".$_SESSION['dbYearID'].",'".$_SESSION['dbName']."',652,'".$m[$i][1]."','Dropdown not converted to array','Add -1 to ValueFromDb to the Field',".$_SESSION['user_id'].",GETDATE())";
					$resultDev = db::getInstanceMaster()->db_insertQuery($sqlDev);
				}
				$flds .= '</select></td>';
			}
		}

		if($m[$i][0] == 16){ //Single-select Popup Datatable
			$index = ((-1) * $m[$i][3]) - 1;
			global $moreextradb;
			$dbarray = $moreextradb[$m[$i][1]];

			//carry on field
			if(isset($_SESSION['carryOn'])){
				if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
					if(isset($_SESSION['carryOn'][$d[3].$m[$i][1]])){
						$gridInputVal = $_SESSION['carryOn'][$d[3].$m[$i][1]];
						unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
					}else{
						$gridInputVal = '';
					}
				}
			}

			if(isset($_SESSION['carryOn'][$d[3].$m[$i][1]])){
				$sql1 = "SELECT ".$dbarray[1].",".$dbarray[2]." FROM ".$dbarray[0]." where " . $dbarray[1] ."=". $gridInputVal . " " .$dbarray[3];					
				$result1 = db::getInstance()->db_select($sql1);
				$row1 = $result1['result_set'];	
			}
			// else{
			// 	$sql1 = "SELECT ".$dbarray[1].",".$dbarray[2]." FROM ".$dbarray[0]." ".$dbarray[3];
			// }
		
			$flds .= "<td class=".$thClass."><input type='button' data-type='".$m[$i][0]."' onclick='toggleModal()' class='form-control' tabindex='".$m[$i][24]."' name='kreon-grid-".$d[3]."".$m[$i][1]."-text[]' id='kreon-grid-".$d[3]."".$m[$i][1]."-text' ";
			
			$SPName = 'sp_GetData_'.$FormID.'_'.$d[7].'_'.$m[$i][1];
			$sql = "SELECT COUNT(*) AS found FROM information_schema.routines WHERE routine_schema = 'DBO'  AND  routine_name = '$SPName'";
			$checkSp = db::getInstance()->db_select($sql);
			if($checkSp['result_set'][0]['found'] == 1){
				$flds .=" data-systemtype=\"2\"";
			}else{
				$flds .=" data-systemtype=\"1\"";
			}

			$flds .= " style='text-align: left;white-space: nowrap;overflow:hidden;text-overflow:ellipsis'>";
		
			$flds .= "<input type='hidden' id='kreon-grid-".$d[3].$m[$i][1]."' name='kreon-grid-".$d[3]."".$m[$i][1]."[]' value='".$gridInputVal."' tabindex='".$m[$i][24]."'></td> ";
			
			echo $ddtableIncludes;
			$row1 = [];
			?>
				<script>
					var pressedButtonId='';
					// Capture the button ID when a button is clicked to trigger saveAdjAmtButton button
					if(<?php echo "'".$m[$i][1]."'"; ?> == 'adjamount' || <?php echo "'".$m[$i][1]."'"; ?> == 'adjamt'){
						$('#saveAdjAmtCloseButton').on('click', function() {
							pressedButtonId = $(this).attr('id');
						});
					}

					//On escape button press cursor move to next tabindex field
					$('#myModal').on('hidden.bs.modal', function (e) {
						if(<?php echo "'".$m[$i][1]."'"; ?> == 'adjamount' || <?php echo "'".$m[$i][1]."'"; ?> == 'adjamt'){
							if( pressedButtonId == ""){
								$('#saveAdjAmtButton').click();
							}else{
								pressedButtonId = "";
							}
						}
						var currentFieldId = $('#DropDownModalID').val();
						if(<?php echo "'kreon-grid-".$d[3].$m[$i][1]."-text'"; ?> == currentFieldId){
							var tabindex = $('#'+currentFieldId).attr("tabindex");
							tabindex++;

							if(<?php echo $m[$i][25]; ?> == 1){
								if($("#"+<?php echo "'kreon-grid-".$d[3].$m[$i][1]."'"; ?>).val() != ''){							
									onBlurUpdateLostFocusTabIndex(currentFieldId,<?php echo $m[$i][0]; ?>);
								}
							}

							//Check validation using sp on lost focus
							if(<?php echo $m[$i][21]; ?> == 1){
								validateOnLostFocusSp(<?php echo "'kreon-grid-".$d[3].$m[$i][1]."-text'"; ?>,<?php echo "'".$d[7]."'"; ?>);
							}
					
							setTimeout(
								function() {
									// console.log("Line 7718 focus")
									$("[tabindex='"+tabindex+"']").focus();
								}, 300);
						}
					})

					$(document).ready(function () {
						// Focus on the open button after the modal is shown
						$('#'+<?php echo "'kreon-grid-".$d[3].$m[$i][1]."-text'"; ?>).focus(function() {
							toggleModal();
						});			
					});

				</script>
			<?php
		}

		if($m[$i][0] == 17){ //Single-select Popup Datatable
			$index = ((-1) * $m[$i][3]) - 1;
			global $moreextradb;
			//updated by pornima now moreextradb getting using dbFieldName
			// $dbarray = $moreextradb[$index];
			$dbarray = $moreextradb[$m[$i][1]];

			//carry on field
			if(isset($_SESSION['carryOn'])){
				if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
					if(isset($_SESSION['carryOn'][$d[3].$m[$i][1]])){
						$gridInputVal = $_SESSION['carryOn'][$d[3].$m[$i][1]];
						unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
					}else{
						$gridInputVal = '';
					}
				}
			}

			if(isset($_SESSION['carryOn'][$d[3].$m[$i][1]])){
				$sql1 = "SELECT ".$dbarray[1].",".$dbarray[2]." FROM ".$dbarray[0]." where " . $dbarray[1] ."=". $gridInputVal . " " .$dbarray[3];					
				$result1 = db::getInstance()->db_select($sql1);
				$row1 = $result1['result_set'];	
			}
		
			$flds .= "<td class=".$thClass."><input type='button' data-type='".$m[$i][0]."' onclick='toggleModal()' class='form-control' tabindex='".$m[$i][24]."' name='kreon-grid-".$d[3]."".$m[$i][1]."-text[]' id='kreon-grid-".$d[3]."".$m[$i][1]."-text' ";
			
			$SPName = 'sp_GetData_'.$FormID.'_'.$d[7].'_'.$m[$i][1];
			$sql = "SELECT COUNT(*) AS found FROM information_schema.routines WHERE routine_schema = 'DBO'  AND  routine_name = '$SPName'";
			$checkSp = db::getInstance()->db_select($sql);
			if($checkSp['result_set'][0]['found'] == 1){
				$flds .=" data-systemtype=\"2\"";
			}else{
				$flds .=" data-systemtype=\"1\"";
			}
			
			$flds .= " style='text-align: left;white-space: nowrap;overflow:hidden;text-overflow:ellipsis'>";
			$flds .= "<input type='hidden' id='kreon-grid-".$d[3].$m[$i][1]."' name='kreon-grid-".$d[3]."".$m[$i][1]."[]' value='".$gridInputVal."' tabindex='".$m[$i][24]."'></td> ";
			
			echo $ddtableIncludes;
			$row1 = [];
		?>
			<script>
				var pressedButtonId='';
				// Capture the button ID when a button is clicked to trigger saveAdjAmtButton button
				if(<?php echo "'".$m[$i][1]."'"; ?> == 'adjamount' || <?php echo "'".$m[$i][1]."'"; ?> == 'adjamt'){
					// alert("hi");
					$('#saveAdjAmtCloseButton').on('click', function() {
						pressedButtonId = $(this).attr('id');
					});
				}

				
				//On escape button press cursor move to next tabindex field
				$('#myModal').on('hidden.bs.modal', function (e) {
					
					var currentFieldId = $('#DropDownModalID').val();

					if(<?php echo "'".$m[$i][1]."'"; ?> == 'adjamount' || <?php echo "'".$m[$i][1]."'"; ?> == 'adjamt'){
						// alert("hello");
						if( pressedButtonId == ""){
							$('#saveAdjAmtButton').click();
						}else{
							pressedButtonId = "";
						}
					}
					
					if(<?php echo "'kreon-grid-".$d[3].$m[$i][1]."-text'"; ?> == currentFieldId){
						var tabindex = $('#'+currentFieldId).attr("tabindex");
						tabindex++;

						if(<?php echo $m[$i][25]; ?> == 1){
							if($("#"+<?php echo "'kreon-grid-".$d[3].$m[$i][1]."'"; ?>).val() != ''){							
								onBlurUpdateLostFocusTabIndex(currentFieldId,<?php echo $m[$i][0]; ?>);
							}
						}

						//Check validation using sp on lost focus
						if(<?php echo $m[$i][21]; ?> == 1){
							console.log("hhhh");
							validateOnLostFocusSp(<?php echo "'kreon-grid-".$d[3].$m[$i][1]."-text'"; ?>,<?php echo "'".$d[7]."'"; ?>);
						}
				
						setTimeout(
                            function() {
								// console.log("Line 7718 focus")
								$("[tabindex='"+tabindex+"']").focus();
							}, 300);
					}
				})

				$(document).ready(function () {
					// Focus on the open button after the modal is shown
					$('#'+<?php echo "'kreon-grid-".$d[3].$m[$i][1]."-text'"; ?>).focus(function() {
						toggleModal();
					});			
				});

			</script>
		<?php
		}

        if($m[$i][0] == 18){

			//carry on field
			if(isset($_SESSION['carryOn'])){
				if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
					if(isset($_SESSION['carryOn'][$d[3].$m[$i][1]])){
						$gridInputVal = $_SESSION['carryOn'][$d[3].$m[$i][1]];
						unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
					}else{
						$gridInputVal = '';
					}
				}
			}

			if($gridInputVal != NULL){
				if(gettype($gridInputVal) == 'string'){
					$date = strtotime($gridInputVal);
					$gridInputVal = date('h:i', $date);
				}else{
					$gridInputVal = $gridInputVal->format('h:i');
				}
			}

			$flds .= '<td class='.$thClass.'><input class="form-control" type="time" value="'.$gridInputVal.'"  name="kreon-grid-'.$d[3].''.$m[$i][1].'[]" id="kreon-grid-'.$d[3].''.$m[$i][1].'" '.$m[$i][6].' tabindex="'.$m[$i][24].'" ';
			
			//Check validation using sp on lost focus
			if($m[$i][21] == 1){
				$flds .= "onfocusout=\"validateOnLostFocusSp(this,'".$d[7]."');\"";
			}

			if($m[$i][23] != 0){ //to check if field readonly
				$flds .= " readonly ";
			}

			//Once lost focus from perticular field then again don't allow to focus on this field
			if($m[$i][25] == 1){
				$flds .= "onblur=\"onBlurUpdateLostFocusTabIndex(this,'".$m[$i][0]."');\"";
			}

			$flds .='/></td>';
		}
		
		if($m[$i][0] == 19){	//Dropdown From DB
			// print_r($m[$i]);
			// select option by enter then cursor going to next field
			echo "<script>
				$(document).ready(function(){

					// $('#kreon-grid-".$d[3].$m[$i][1]."').on('select2:select, select2:close', function (e) { 
					// 	console.log('in');
					// 	var self = $(this), form = self.parents('form:eq(0)'), focusable, next;
					// 	focusable = form.find('input,a,select,button,textarea').filter(':visible');
					// 	next = focusable.eq(focusable.index(this)+1);
					// 	if (next.length) {
					// 		next.focus();
					// 	} 
					// 	return false;
					// });

					//Onload focus on TabIndex if field tabindex 1
                    if('".$m[$i][24]."' == 1){
                        setTimeout(
                            function() {
                                $('#kreon-grid-".$d[3].$m[$i][1]."').next('span').find('.select2-selection--single').focus();
                            }, 300);
                    }

					//Once lost focus from perticular field then again don't allow to focus on this field
					if('".$m[$i][25]."' == 1){
						$('#kreon-grid-".$d[3].$m[$i][1]."').on('select2:open select2:select', function (e) {
							onBlurUpdateLostFocusTabIndex(this,'".$m[$i][0]."');
						});
					}

					if('".$m[$i][23]."' == 1){ //to check if field readonly
						setTimeout(
                            function() {
                                $('#kreon-grid-".$d[3].$m[$i][1]."').next('span').find('.select2-selection--single').css('pointer-events','none').css('background','#eee');
                            }, 300);
					}
				});
			</script>";
			
			if($m[$i][3] < 0){	//IF to be taken from DB
				$index = ((-1) * $m[$i][3]) - 1;
				global $moreextradb;

				//updated by pornima now moreextradb getting using dbFieldName
				// $dbarray = $moreextradb[$index];
				$dbarray = $moreextradb[$m[$i][1]];

				//carry on field
				if(isset($_SESSION['carryOn'])){
					if(trim($FormID) == trim($_SESSION['carryOn']['FormId'])){
						if(isset($_SESSION['carryOn'][$d[3].$m[$i][1]])){
							$gridInputVal = $_SESSION['carryOn'][$d[3].$m[$i][1]];
							unset($_SESSION['carryOn'][$d[3].$m[$i][1]]);
						}else{
							$gridInputVal = '';
						}
					}
				}

				$sql = "SELECT ".$dbarray[1].",".$dbarray[2]." FROM ".$dbarray[0]." ".$dbarray[3] ." ";
				$eresult = db::getInstance()->db_select($sql);
			
		
				if(strlen($m[$i][17]) > 2 && $gridInputVal !=''){
					// If carryon field then added in where condition
					$sql1 = "SELECT ".$dbarray[1].",".$dbarray[2]." FROM ".$dbarray[0];
					if(strlen($dbarray[3]) > 1){
						$sql1 .= " ".$dbarray[3];
					}else{
						$sql1 .= " WHERE 1=1";
					}
					$sql1 .= " AND ".$dbarray[1]."=".$gridInputVal;

					$result1 = db::getInstance()->db_select($sql1);
					$p = $result1['result_set'][0][$dbarray[1]];
				}else if(strlen($m[$i][17]) > 0){
					// If Deault value then added in where condition
					$sql1 = "SELECT ".$dbarray[1].",".$dbarray[2]." FROM ".$dbarray[0];	
					if(strlen($dbarray[3]) > 1){
						$sql1 .= " ".$dbarray[3];
					}else{
						$sql1 .= " WHERE 1=1";
					}
					$sql1 .= " AND ".$dbarray[1]."=".$m[$i][17];

					$result1 = db::getInstance()->db_select($sql1);

					$p = $result1['result_set'][0][$dbarray[1]];
				}else if($gridInputVal !=''){
					// If carryon field then added in where condition
					$sql1 = "SELECT ".$dbarray[1].",".$dbarray[2]." FROM ".$dbarray[0];
					if(strlen($dbarray[3]) > 1){
						$sql1 .= " ".$dbarray[3];
					}else{
						$sql1 .= " WHERE 1=1";
					}
					$sql1 .= " AND ".$dbarray[1]."=".$gridInputVal;

					$result1 = db::getInstance()->db_select($sql1);
					$p = $result1['result_set'][0][$dbarray[1]];
				}

				
				
				$flds .= '<td class='.$thClass.'><select data-plugin-selectTwo name="kreon-grid-'.$d[3].''.$m[$i][1].'[]"  placeholder="'.$m[$i][2].'" id="kreon-grid-'.$d[3].''.$m[$i][1].'" class="form-control populate" tabindex="'.$m[$i][24].'"';

				$changeScript = "";

				$onchangeHidden = "";

				if($m[$i][8] == 1){
					$onchange = " onchange=\"CreateOnchangeScriptGrid(this);\"";
					$flds .= $onchange;
					$onchangeHidden = '<input type="hidden" id="kreon-grid-'.$d[3].$m[$i][1].'-hidden" value="' . $m[$i][9] . '" />';//,' . $m[$i][1] . ',' . $m[$i][0] . '
				}

				//Check validation using sp onChange
				if($m[$i][22] == 1){
					$flds .= "onchange=\"validateOnChangeSp(this,'".$d[7]."');\"";
				}

				//Check validation using sp on change
				if($m[$i][21] == 1){
					$flds .= "onchange=\"validateOnLostFocusSp(this,'".$d[7]."');\"";
				}

				//Check validation using sp on change
				if($m[$i][21] == 1){
						$flds .= "onchange=\"validateOnLostFocusSp(this,'".$d[7]."');\"";
				}

				//On change exicute formula
				$onkeyupHidden ='';
				if(strlen($m[$i][11]) > 2){
					$onkeyupHidden = "";
					$flds .= " onchange=\"CreateOnkeyupScriptGrid(this,'grid');\"";
					$onkeyupHidden = '<input type="hidden" id="kreon-grid-'.$d[3].$m[$i][1].'-onkeyup-hidden" value="' . $m[$i][11] . '" />';
				}

				$flds .= '>';


				// if(strlen($m[$i][6]) > 4){
				// 	//Use for get conditional data
				// 	echo "<script>
				// 	$(document).ready(function(){
				// 		$('#kreon-grid-".$d[3].$m[$i][1]."').on('select2:open', function (e) {
							
				// 			var str1 = 'parameters';
				// 			var parametersPos = -1;
				// 			var thissParameters = '".$m[$i][6]."';
				// 			if(thissParameters.indexOf(str1) != -1){
				// 				parametersPos = thissParameters.indexOf(str1);
				// 			}

				// 			if(parametersPos != -1){
				// 				var parametersParams = thissParameters.substring(
				// 					thissParameters.indexOf('{') + 1, 
				// 					thissParameters.lastIndexOf('}')
				// 				);
				// 			}

				// 			if(parametersParams.length > 3){
				// 				var hidden = $('form').find(':hidden');	
				// 				hidden.show();   
				// 				var  formSerialize = $('form').serialize();	
				// 				hidden.hide(); 	

				// 				$.ajax({
				// 					type : 'POST',
				// 					dataType: 'json',
				// 					data : {FieldOtherCondition : '".$m[$i][6]."', gridSerial:'".$d[3]."',searchText: formSerialize, GridID:'".$d[7]."', dbarray0: '".$dbarray[0]."', dbarray1:'".$dbarray[1]."', dbarray2: '".$dbarray[2]."', dbarray3: '".$dbarray[3]."'},
				// 					url : 'getPreLoadedDropDownValue.php',
				// 					success : function(result){
				// 						var newOption = '';
				// 						// $('#kreon-grid-".$d[3]."".$m[$i][1]."').empty().trigger('change');

				// 						for(var j=0; j < result['data'].length; j++){ 
				// 							newOption += '<option value=\"'+result['data'][j]['".$dbarray[1]."']+'\">'+result['data'][j]['".$dbarray[2]."']+'</option>';
				// 							/* var newOption = $('<option ></option>').val(result['data'][j]['".$dbarray[1]."']).text(result['data'][j]['".$dbarray[2]."']) */
				// 						}	
				// 						// if($('#kreon-grid-".$d[3]."".$m[$i][1]."').data('select2')) {
				// 							// $('#kreon-grid-".$d[3]."".$m[$i][1]."').select2('open');
				// 							$('#kreon-grid-".$d[3]."".$m[$i][1]."').append(newOption);
				// 							// $('#kreon-grid-".$d[3]."".$m[$i][1]."').select2('close');
				// 						// }
										
				// 					}
				// 				});
				// 			}
				// 		});
				// 	});
				// 	</script>";
				// }else{
					if(sizeof($eresult) > 0){
						if(sizeof($eresult['result_set']) > 0){
							for($j = 0; $j < sizeof($eresult['result_set']); $j++){
								$k = $eresult['result_set'][$j][$dbarray[1]];  //ID	
								if($p == $k) {
									$flds .= '<option selected value="'. $k .'" >'.$eresult['result_set'][$j][$dbarray[2]].'</option>&nbsp;';
								}
								else {
									$flds .='<option value="'. $k .'" >'.$eresult['result_set'][$j][$dbarray[2]].'</option>&nbsp;';
								}
							}
						}
					}
				// }

				$flds .= '</select>'.$onchangeHidden.'</td>';

			}else{			

				$flds .= '<td class='.$thClass.'><select name="'.$d[3].''.$m[$i][1].'[]" placeholder="'.$m[$i][2].'" id="'.$m[$i][1].'" class="form-control" '.$m[$i][6].' tabindex="'.$m[$i][24].'"';
				
				//Check validation using sp onChange
				if($m[$i][22] == 1){
					$flds .= "onchange=\"validateOnChangeSp(this,'".$d[7]."');\"";
				}

				$flds .= '>';
				// $flds .= '><option value="0">'.$m[$i][2].'</option>';

				for($j = 0; $j < sizeof($r[$m[$i][3]]); $j++){
					$k = $j+1;  //value
					$flds .= '<option value="'. $k .'" >'.$r[$m[$i][3]][$j].'</option>&nbsp;';
				}

				$flds .= '</select></td>';
			}
		}
	}
	
	?>

	<!-- On grid last field tab press Then add or update row -->
	<script>

		$(document).ready(function() {
		var isKeyDown = false;
		$(document).on('keydown', '#panel<?php echo $d[3]; ?> #GridTable tbody > tr:first > td input, #panel<?php echo $d[3]; ?> #GridTable tbody > tr:first > td select', function(event) {
			console.log('call keydown');
			// event.stopPropagation();
			// event.preventDefault(); 
			var lastFieldId = <?php echo "'".$lastFieldId."'"; ?>;
			var targetId = event.target.id.trim();

			if(lastFieldId.trim() == targetId.trim()){
				var targetIdSplit = event.target.id.split('-');
				var gridSerialNo = targetIdSplit[2].charAt(0);
				// console.log(lastFieldId.trim() +"=="+ targetId.trim())
				if (event.key === "Tab") {
					// event.preventDefault(); // Prevent default tab behavior
					onGridLastFieldAddRow("panel<?php echo $d[3]; ?>");	
					// console.log("after onGridLastFieldAddRow");
					//The issue occurs when trying to move the focus from one grid to the next grid after adding a row. The following code should be updated to fix this behavior. 
					setTimeout(function() {
						var nextField = $(event.target)
						if (nextField.length) {
							var tabindex = parseInt(nextField.attr('tabindex'))+2;
							// console.log("keydown function tabindex");
							$("[tabindex='"+tabindex+"']").focus();
						}
					}, 0);
				}
			}
		});
		
		});
	</script>

	<?php
	$tableHead .= '<th style="width:20px;padding:2px 3px !important;"></th>';
	$gridLastTabIndex = $maxTabindex+1;
	$flds .= '<td style="padding1:2px 3px !important;" class="AddEdit" >
				<a  style="padding:3px;"  href="javascript:void(0)" class="btn btn-success add'.$d[3].'" tabindex="'.$gridLastTabIndex.'" title="Alt+G">
					<span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span>
				</a>
				<a  style="padding:3px; display:none;" href="javascript:void(0)" class="btn btn-success update'.$d[3].'">
					<span class="glyphicon glyphicon glyphicon-ok" aria-hidden="true"></span>
				</a>
				<a  style="padding:3px; margin-left:2px;" href="javascript:void(0)" class="btn btn-info refresh'.$d[3].'">
					<span class="glyphicon glyphicon glyphicon-refresh" aria-hidden="true"></span>
				</a>
				<input type="hidden" name="'.$d[3].'GridEditID[]" id="'.$d[3].'GridEditID" value="0">
				<input type="hidden" name="'.$d[3].'GridTempID[]" id="'.$d[3].'GridTempID" value="0">
			</td>';
	$flds .= '</div>';
	$end = '</div></tr>';		
	$script = '<script>
			$(document).ready(function(){
				'.$changeScript.'
				//add more row fields
				var mArray = '.json_encode($m).';
				var gridCalculationFieldName = [];
				var gridCalculationType = [];
				// console.log(mArray);
				//Get current row data
				function getCurrentRowDataForSpValidation(onCLickVariable, mArray,gridEdit){
					// console.log("getCurrentRowDataForSpValidation",onCLickVariable, mArray,gridEdit);
					var cuurrentRowData = "";
					var cuurrentRowDataWithComma = "";
					
					
					for(var t=0; t < mArray.length; t++){			
						if(mArray[t][0] == 17 || mArray[t][0] == 16){
							if(gridEdit == 1){
								var fldvalue= onCLickVariable.closest("tr").find("#'.$d[3].'"+mArray[t][1]).val();
							}else{
								var fldvalue= onCLickVariable.closest("tr").find("#kreon-grid-'.$d[3].'"+mArray[t][1]).val();
							}
						}else{
							if(gridEdit == 1){
							var fldvalue = onCLickVariable.closest("tr").find("#'.$d[3].'"+mArray[t][1]).val();
							}else{
								var fldvalue = onCLickVariable.closest("tr").find("#kreon-grid-'.$d[3].'"+mArray[t][1]).val();
							}
						}
						
						cuurrentRowData += mArray[t][1]+"=";
						if(mArray[t][0] == 0 || mArray[t][0] == 1 || mArray[t][0] == 3 || mArray[t][0] == 6 || mArray[t][0] == 8 || mArray[t][0] == 18){
							cuurrentRowData += fldvalue != "" ? fldvalue : "";
						}else if(mArray[t][0] == 2 || mArray[t][0] == 5 || mArray[t][0] == 7 || mArray[t][0] == 9 || mArray[t][0] == 10 || mArray[t][0] == 11 || mArray[t][0] == 15 || mArray[t][0] == 16 || mArray[t][0] == 17 || mArray[t][0] == 19){
								if(fldvalue == null){
									cuurrentRowData += 0;
								}else{
									cuurrentRowData += fldvalue != "" ? fldvalue : 0;
								}
						}else if(mArray[t][0] == 12 || mArray[t][0] == 13){
							cuurrentRowData += 0;
						}
						if(t != mArray.length-1)
							cuurrentRowData += "|";

						var decimal=  /^-?\d*\.?\d*$/;

						cuurrentRowDataWithComma += mArray[t][1]+"=";
						if(mArray[t][0] == 0 || mArray[t][0] == 1 || mArray[t][0] == 3 || mArray[t][0] == 5 || mArray[t][0] == 6 || mArray[t][0] == 8 || mArray[t][0] == 18 || mArray[t][0] == 16 || mArray[t][0] == 17){
							// console.log(mArray[t][1] +" == "+ mArray[t][0] +" == "+ fldvalue);
							if(fldvalue == undefined || fldvalue == "" || fldvalue == null){
								// if(mArray[t][0] == 5){
									// console.log("if"+mArray[t][1]);
									cuurrentRowDataWithComma += (fldvalue != "" && fldvalue != null) ? "\'\'"+fldvalue+"\'\'" : "\'\'\'\'";
								// }else{
								// 	cuurrentRowDataWithComma += fldvalue != "" ? "\'\'"+fldvalue+"\'\'" : "\'\'\'\'";
								// }
							}else{
								
								if(fldvalue.match(decimal)) {
									// console.log("decimal"+mArray[t][1]);
									cuurrentRowDataWithComma += fldvalue != "" ? fldvalue : "\'\'\'\'"; //remove single quote
								}else{
									// console.log("not decimal"+mArray[t][1]);
									cuurrentRowDataWithComma += fldvalue != "" ? "\'\'"+fldvalue+"\'\'" : "\'\'\'\'";
								}
							}
						}else if(mArray[t][0] == 2 || mArray[t][0] == 7 || mArray[t][0] == 9 || mArray[t][0] == 10 || mArray[t][0] == 11 || mArray[t][0] == 15 || mArray[t][0] == 19){
							cuurrentRowDataWithComma += fldvalue != "" ? fldvalue : 0;
						}else if(mArray[t][0] == 12 || mArray[t][0] == 13){
							cuurrentRowDataWithComma += 0;
						}
						if(t != mArray.length-1)
							cuurrentRowDataWithComma += ",";
					}
					return [cuurrentRowData,cuurrentRowDataWithComma];
				}
			
				$(".add'.$d[3].'").click(function(){
					// console.log("call add function");
					//Add Button disabled
					$(".add'.$d[3].'").attr("disabled", true);
					
					var onCLickVariable = $(this);
					var gridEdit = 0
					var [currentData,cuurrentRowDataWithComma] = getCurrentRowDataForSpValidation(onCLickVariable,mArray,gridEdit);
					
					var prevCnt = $("#cnt'.$d[3].'").val();	
					var cnt = +prevCnt + 1;
					// console.log("going into ajax");
					$.ajax({
						type : "POST",
						dataType: "json",
						data : {ValidationOn:"gridsave", formData: currentData, formDataWithComma:cuurrentRowDataWithComma, mainFormData:$("form").serialize(), FormID:"'.$FormID.'", GridID:$("#GridId'.$d[3].'").val(), GridRowTempId: cnt},
						url : "validateForm.php",
						success: function(output) {
							// console.log(output);
							if(output["status"] == true){
								if(output["data"]["Success"] == 1){
									// await AddRow(onCLickVariable,prevCnt,cnt);
									AddRow(onCLickVariable, prevCnt, cnt).then(() => {
										$(".add'.$d[3].'").attr("disabled", false);
										if(output["tempData"]){
											console.log(output["tempData"][0]);
											$("#"+Object.keys(output["tempData"][0])).empty();
											for(var i=0; i<output["tempData"].length; i++){
												$("#"+Object.keys(output["tempData"][i])).append(Object.values(output["tempData"][i]));
											}
										}
									});
								}else{
									if(output["data"]["Success"] == 2){
										if (confirm(output["data"]["Msg"]+". Are you sure you want to submit?") == true) {
											// await AddRow(onCLickVariable,prevCnt,cnt);
											AddRow(onCLickVariable, prevCnt, cnt).then(() => {
												$(".add'.$d[3].'").attr("disabled", false);
												if(output["tempData"]){
													$("#"+Object.keys(output["tempData"][0])).empty();
													for(var i=0; i<output["tempData"].length; i++){
														$("#"+Object.keys(output["tempData"][i])).append(Object.values(output["tempData"][i]));
													}
												}
											});

										}else {
											$(".add'.$d[3].'").attr("disabled", false);
										}
									}else if(output["data"]["Success"] == 0){
										alert(output["data"]["Msg"]);
										$(".add'.$d[3].'").attr("disabled", false);
									}else{
										alert("Internal Server Error");
										$(".add'.$d[3].'").attr("disabled", false);
									}
								}
							}else{
								AddRow(onCLickVariable, prevCnt, cnt).then(() => {
									// await AddRow(onCLickVariable,prevCnt,cnt);
									$(".add'.$d[3].'").attr("disabled", false);
								});
							}
						}
					});
				});

				async function getBase64(file) {
					return new Promise((resolve) => {
						// Check if the file is a valid Blob or File object
						if (!(file instanceof Blob)) {
						console.error("Expected a Blob or File object");
						return resolve(null); // Resolve with null in case of an error
						}

						const reader = new FileReader();
						
						reader.onload = () => {
						// Resolve with the base64 string (cleaned up)
						resolve(reader.result.replace("data:", "").replace(/^.+,/, ""));
						};
						
						reader.onerror = () => {
						// Resolve with null if an error occurs while reading the file
						resolve(null);
						};
						
						// Read the file as a data URL (base64)
						reader.readAsDataURL(file);
					});
				}

				//Call ajax for upload base64 image & save & get id to assign created row
				// async function base64AjaxFunction(base64String,imgFieldName,gridSerial,ext,prevCnt,gridId){
				// 	console.log("base64AjaxFunction",imgFieldName,gridSerial,ext,prevCnt,gridId)
				// 	await $.ajax({
				// 		type:"POST",
				// 		url:"media.php",
				// 		data: {Media:base64String, ext:ext, gridId:gridId, imgFieldName:imgFieldName},
				// 		success: function(result){
				// 			// console.log(result)
				// 			result=JSON.parse(result);
				// 			console.log(result);
				// 			if(result.response== "true"){ 
				// 				return result;
				// 			}
				// 			else alert("Error while saving data..!");
				// 		}
				// 	});
				// 	return null;
				// }

				async function base64AjaxFunction(base64String, imgFieldName, gridSerial, ext, prevCnt, gridId) {
				console.log("base64AjaxFunction", imgFieldName, gridSerial, ext, prevCnt, gridId);

				// Wrap the AJAX call in a Promise so we can use await
				const result = await new Promise((resolve, reject) => {
					$.ajax({
						type: "POST",
						url: "media.php",
						data: { Media: base64String, ext: ext, gridId: gridId, imgFieldName: imgFieldName },
						success: function (response) {
							try {
								const parsedResult = JSON.parse(response);
								console.log(parsedResult);
								if (parsedResult.response === "true") {
									resolve(parsedResult);  // Resolving the promise with the result
								} else {
									alert("Error while saving data..!");
									resolve(null);  // Resolving with null in case of error
								}
							} catch (error) {
								reject(error);  // Rejecting if JSON parsing fails
							}
						},
						error: function (xhr, status, error) {
							reject(error);  // Rejecting in case of AJAX error
						}
					});
				});

				return result;
			}


				//this function use for after adding row focus move on field type 5
				function focusSelect2Search() {
					const searchField = document.querySelector(".select2-container--open .select2-search__field");
					if (searchField) {
						searchField.focus();
					} else {
						setTimeout(focusSelect2Search, 50); // Try again in 50ms
					}
				}


				// Add row in grid
				async function AddRow(onCLickVariable,prevCnt,cnt){
					// console.log("AddRow FN")
					var fieldArray = [];
					var mfld = "";
					var thClass="";
					var MediaDetails;
					
					for(var t=0; t < mArray.length; t++){
						
						if(mArray[t][0] == 17 || mArray[t][0] == 16){
							var fldvalue = onCLickVariable.closest("tr").find("#kreon-grid-'.$d[3].'"+mArray[t][1]+"-text").val();
							
							var fldvalue1 = onCLickVariable.closest("tr").find("#kreon-grid-'.$d[3].'"+mArray[t][1]).val();
							
						}else{
							var fldvalue = onCLickVariable.closest("tr").find("#kreon-grid-'.$d[3].'"+mArray[t][1].trim()).val();
						}
						
						fieldArray.push([mArray[t][0],"kreon-grid-'.$d[3].'"+mArray[t][1],mArray[t][16],mArray[t][17],mArray[t][12],mArray[t][23]]);
						
						if(mArray[t][5] == 1 && fldvalue == ""){
							if(mArray[t][0] == 17 || mArray[t][0] == 16){
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]+"-text").css("border","1px solid red");
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]+"-text").focus();

							}else{
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).css("border","1px solid red");
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).focus();
							}
							return false;
						}else if(mArray[t][5] == 1 && fldvalue == "0"){
							if(mArray[t][0] == 5 || mArray[t][0] == 19){
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).next("span").css("border","1px solid red");
							}else{
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).css("border","1px solid red");
							}
							$("#kreon-grid-'.$d[3].'"+mArray[t][1]).focus();
							return false;
						}

						if(mArray[t][26] == 0){ //To check visibility
							thClass = " thHidden ";
						}

						if(mArray[t][0] == 1){
							fldvalue = fldvalue.replace("\'", "&lsquo;");
							mfld += "<td class=\'"+thClass+"\'><input readonly type=\"text\" value=\'"+fldvalue+"\' class=\'form-control\'   name=\''.$d[3].'"+mArray[t][1]+"[]\'   id=\''.$d[3].'"+mArray[t][1]+"\'></td>";

							// If GridCalculation 1 then add sum in footer
							if(mArray[t][12] > 0){
								if (!gridCalculationFieldName.includes(mArray[t][1])) {
									gridCalculationFieldName.push(mArray[t][1]);
									gridCalculationType.push(mArray[t][12]);
								}
							}
						}

						if(mArray[t][0] == 3){
							fldvalue = fldvalue.replace("\'", "&lsquo;");
							mfld += "<td class=\'"+thClass+"\'><textarea readonly type=\"number\" class=\'form-control\'   name=\''.$d[3].'"+mArray[t][1]+"[]\'   id=\''.$d[3].'"+mArray[t][1]+"\'>"+fldvalue+"</textarea></td>";
						}

						if(mArray[t][0] == 7){
							
							mfld += "<td class=\'"+thClass+"\'><input readonly type=\"number\" value=\'"+fldvalue+"\' class=\'form-control\'   name=\''.$d[3].'"+mArray[t][1]+"[]\'   id=\''.$d[3].'"+mArray[t][1]+"\'></td>";
							if(mArray[t][12] > 0){
								if (!gridCalculationFieldName.includes(mArray[t][1])) {
									gridCalculationFieldName.push(mArray[t][1]);
									gridCalculationType.push(mArray[t][12]);
								}
							}
							
						}

						if(mArray[t][0] == 6){
							mfld += "<td class=\'"+thClass+"\'><input readonly type=\"date\" value=\'"+fldvalue+"\' class=\'form-control\'   name=\''.$d[3].'"+mArray[t][1]+"[]\'   id=\''.$d[3].'"+mArray[t][1]+"\'></td>";
						}

						if(mArray[t][0] == 5 || mArray[t][0] == 19){
							var selectedOption = $("#kreon-grid-'.$d[3].'"+mArray[t][1] + " :selected").text();
							mfld += "<td class=\'"+thClass+"\'><select readonly style=\'pointer-events: none;\' name=\''.$d[3].'"+mArray[t][1]+"[]\'  placeholder=\'"+mArray[t][2]+"\' id=\''.$d[3].'"+mArray[t][1]+"\' class=\'form-control populate\'>";
							fldvalue = fldvalue === null ? 0 : fldvalue;
							mfld += "<option value=\'"+fldvalue+"\'>"+selectedOption+"</option>";
							mfld += "</select></td>";
						}


						if(mArray[t][0] == 12){
							
							var file = $("#kreon-grid-'.$d[3].'"+mArray[t][1]).prop("files")[0];
							var imgFieldName = mArray[t][1];
							var gridSerial = "'.$d[3].'";
							var filePath = onCLickVariable.closest("tr").find("#kreon-grid-'.$d[3].'"+mArray[t][1]).val(); 
							var ext = filePath.substr(filePath.lastIndexOf(".")+1,filePath.length);
							var targetRowId = gridSerial+""+cnt;
							var gridId = "'.$d[7].'";
							var imgFlag = false;
							console.log(t, imgFieldName, gridSerial, ext, targetRowId, gridId);

							if(file !== undefined){
								// Get base64 string & assign to function
								base64String = await getBase64(file);
								if(base64String !== null){
									// Await the result of base64AjaxFunction before proceeding
									let MediaResult = await base64AjaxFunction(base64String, imgFieldName, gridSerial, ext, targetRowId, gridId);

									if (MediaResult && MediaResult.response === "true") {
										var MediaID = MediaResult.data;
										var MediaPath = MediaResult.imagepath;

										if (MediaID == 0) {
											console.log("MediaID is 0", MediaID);
										} else {
										 	imgFlag = true;
											console.log("MediaID is not 0", MediaID, MediaPath);
											mfld += "<td class=\'" + thClass + "\'><input readonly type=\'hidden\' value=\'" + MediaID + "\' class=\'form-control\' name=\'' . $d[3] . '" + mArray[t][1] + "[]\' id=\'' . $d[3] . '" + mArray[t][1] + "\'>"
												+ "<a target=\'_blank\' href=\'" + MediaPath + "\'><img src=\'" + MediaPath + "\' width=\'60\'></a>"
												+ "</td>";
										}
									} else {
										console.log("Error while fetching MediaID");
									}
								}
							}
							if(!imgFlag){
								mfld += "<td class=\'" + thClass + "\'><input readonly type=\'hidden\' value=\'\' class=\'form-control\' name=\'' . $d[3] . '" + mArray[t][1] + "[]\' id=\'' . $d[3] . '"+ mArray[t][1] + "\'></td>";
							}
							/*
							// //Get base64 string & assing to function
							// base64String = await getBase64(file)
							// // MediaResult = await base64AjaxFunction(base64String,imgFieldName,gridSerial,ext,targetRowId,gridId)
							// var MediaID = 0; var MediaPath= "";
							// base64AjaxFunction(base64String,imgFieldName,gridSerial,ext,targetRowId,gridId).then(function(MediaResult) {
							// 	console.log("MediaResult", MediaResult)
							// 	MediaID = MediaResult.data;
							// 	MediaPath = MediaResult.imagepath;
							// });
							
							// if(MediaID == 0){
							// 	console.log("MediaID is 0", MediaID)
							// 	mfld += "<td class=\'"+thClass+"\'><input readonly type=\"hidden\" value=\"\" class=\'form-control\'   name=\''.$d[3].'"+mArray[t][1]+"[]\'   id=\''.$d[3].'"+mArray[t][1]+"\'></td>";
							// }else{
							// 	console.log("MediaID is not 0", MediaID, MediaPath)
							// 	mfld += "<td class=\'"+thClass+"\'><input readonly type=\"hidden\" value=\"" + MediaID + "\" class=\'form-control\'   name=\''.$d[3].'"+mArray[t][1]+"[]\'   id=\''.$d[3].'"+mArray[t][1]+"\'>"
							// 		+ "<a target=\"_blank\" href=\'" + MediaPath + "\'><img src=\'" + MediaPath + "\' width=\"60\"></a>"
							// 		+ "</td>";
							// }	
							*/
							
						}

						if(mArray[t][0] == 16){
							mfld += "<td class=\'"+thClass+"\'><input type=\"text\" value=\'"+fldvalue+"\' class=\'form-control\'  name=\''.$d[3].'"+mArray[t][1]+"-text[]\'   id=\''.$d[3].'"+mArray[t][1]+"-text\'><input type=\"hidden\" id=\''.$d[3].'"+mArray[t][1]+"\' name=\''.$d[3].'"+mArray[t][1]+"[]\' value=\'"+fldvalue1+"\'></td>";
							console.log(mfld);
						}

						if(mArray[t][0] == 17){
							mfld += "<td class=\'"+thClass+"\'><input disabled type=\"text\" value=\'"+fldvalue+"\' class=\'form-control\'  name=\''.$d[3].'"+mArray[t][1]+"-text[]\'   id=\''.$d[3].'"+mArray[t][1]+"-text\'><input type=\"hidden\" id=\''.$d[3].'"+mArray[t][1]+"\' name=\''.$d[3].'"+mArray[t][1]+"[]\' value=\'"+fldvalue1+"\'></td>";
							// console.log(mfld);
						}

						if(mArray[t][0] == 18){
							mfld += "<td class=\'"+thClass+"\'><input readonly type=\"time\" value=\'"+fldvalue+"\' class=\'form-control\'   name=\''.$d[3].'"+mArray[t][1]+"[]\'   id=\''.$d[3].'"+mArray[t][1]+"\'></td>";
						}
					}
					
					prevCnt = parseInt(prevCnt)+1;
					var headofGrid = \''.$headerGridStart.'\' + prevCnt + \''.$headerGridEnd.'\';

					mfld += "<td><a style=\"padding:3px;margin:2px;\" href=\"javascript:void(0)\" class=\"btn btn-info edits'.$d[3].'\"><span class=\"glyphicon glyphicon glyphicon-pencil\" aria-hidden=\"true\"></span></a><a style=\"padding:2px 3px;\" href=\"javascript:void(0)\" class=\"btn btn-danger removes'.$d[3].'\"><span class=\"glyphicon glyphicon glyphicon-remove\" aria-hidden=\"true\"></span></a><input type=\"hidden\" name=\"'.$d[3].'GridEditID[]\" id=\"'.$d[3].'GridEditID\" value=\"0\"><input type=\"hidden\" name=\"'.$d[3].'GridTempID[]\" id=\"'.$d[3].'GridTempID\" value=\'"+cnt+"\'></td>";

					// $(".edits"+"'.$d[3].'").closest("#'.$d[3].'GridTempID").val("'.$d[3].'" + cnt);
					var end =\''.$end.'\';
					/* / var fieldHTML=\''.$flds.'\';*/
					document.getElementById("cnt'.$d[3].'").value = cnt;
					
					$("#panel'.$d[3].' table tbody tr:first").after(headofGrid + mfld + end);
					// $("#panel'.$d[3].' table tbody").append(headofGrid + mfld + end);
					
					//Call grid total calculation function
					if(gridCalculationFieldName.length > 0){
						CreateOnChangeScriptGridCalculation(gridCalculationFieldName, gridCalculationType,"'.$d[3].'");	
					}

					//Clear input value of first row
					for(var s=0; s < fieldArray.length; s++){
						if(fieldArray[s][2] != 1 && fieldArray[s][2] != 2){
							
							if(fieldArray[s][0] == 5 || fieldArray[s][0] == 19){
								
								if(fieldArray[s][3] == null || fieldArray[s][3].length == 0){

									// Step 1: Get the current onchange function as a string. onchange function remove & then adding because after adding row onchange function triggering so
									let onchangeFunction = $("#" + fieldArray[s][1]).attr("onchange");

									// Step 2: If onchange function exists, store it, remove it temporarily
									if (onchangeFunction) {
										// Step 2: Remove the onchange attribute temporarily
										$("#" + fieldArray[s][1]).removeAttr("onchange");

										$("#"+fieldArray[s][1]).select2("open");
										$("#"+fieldArray[s][1]).val(0).trigger("change");
										$("#"+fieldArray[s][1]).select2("close");

										// Step 4: Re-add the original onchange function with the stored value
										$("#" + fieldArray[s][1]).attr("onchange", onchangeFunction);
									}else{
										$("#"+fieldArray[s][1]).select2("open");
										$("#"+fieldArray[s][1]).val(0).trigger("change");
										$("#"+fieldArray[s][1]).select2("close");
									}
									
								} 
								//after add if not readonly then remove readonly
								if(fieldArray[s][5] != 1){
									$("#"+fieldArray[s][1]).next("span").find(".select2-selection--single").prop("readonly", false).css("pointer-events","").css("background-color","white");
								}
							}
							if(fieldArray[s][0] == 17 || fieldArray[s][0] == 16){
								if(fieldArray[s][3] == null || fieldArray[s][3].length == 0){
									fieldName = fieldArray[s][1].split("-");
									// console.log("fieldName "+fieldName);
									$("#"+fieldName[0]+"-"+fieldName[1]+"-'.$d[3].'"+fieldName[2].slice(1)+"-text").val("");
									$("#"+fieldName[0]+"-"+fieldName[1]+"-'.$d[3].'"+fieldName[2].slice(1)).val("");
								}
								
								//after add if not readonly then remove readonly
								if(fieldArray[s][5] != 1){
									$("#"+fieldName[0]+"-"+fieldName[1]+"-'.$d[3].'"+fieldName[2].slice(1)+"-text").prop("readonly", false).css("pointer-events","").css("background-color","white");
								}
							}else{
								if(fieldArray[s][3] == null){
									$("#"+fieldArray[s][1].trim()).val("");
									$("#'.$d[3].'GridTempID").val(0);
								}else{
									$("#"+fieldArray[s][1]).val(fieldArray[s][3]);	
								}

								//after add if not readonly then remove readonly
								if(fieldArray[s][5] != 1){
									$("#"+fieldArray[s][1]).prop("readonly", false).css("pointer-events","").css("background-color","white");
								}		
							}

							$("#"+fieldArray[s][1]).css("border","1px solid #aa8a");
						}

						if(fieldArray[s][2] == 2){
							var fieldValueStr = $("#"+fieldArray[s][1]).val();
							var splitIndex = fieldValueStr.lastIndexOf("-");
							var part1 = fieldValueStr.substring(0, splitIndex); // Before the last hyphen
							var part2 = fieldValueStr.substring(splitIndex + 1); // After the last hyphen

							var increementValue = parseInt(part2)+1;
							if(part1.length > 0){
								var changeValue = part1+"-"+increementValue;
							}else{
								var changeValue = increementValue;
							}
							$("#"+fieldArray[s][1]).val(changeValue);
						}
					}


					//on row add Cursor focus on grid first field					
					if("'.$d[9].'".length < 1){
						if(mArray[0][0] == 17){
							$("#kreon-grid-'.$d[3].'"+mArray[0][1]+"-text").focus(); //Cursor focus on grid first field
						}else if(mArray[0][0] == 16){
							$("#kreon-grid-'.$d[3].'"+mArray[0][1]+"-text").focus(); //Cursor focus on grid first field
						}else{
							$("#kreon-grid-'.$d[3].'"+mArray[0][1]).focus(); //Cursor focus on grid first field
						}
					}else{

						//Cursor focus on grid perticular field that was mention in AfterGridAddCursorPosition field
						for(var s=0; s < fieldArray.length; s++){
							// if("'.$d[9].'".length > 1){
								if (fieldArray[s][0] == 17 || fieldArray[s][0] == 16) {
									if (fieldArray[s][1]+"-text" == "kreon-grid-' . $d[3] . '' . $d[9] . '-text") {
										$("#" + fieldArray[s][1]+"-text").focus();
										break;
									}
								}else if(fieldArray[s][0] == 5){
									if(fieldArray[s][1] == "kreon-grid-'.$d[3].''.$d[9].'"){
										// console.log(fieldArray[s][1] +"=="+ "kreon-grid-'.$d[3].''.$d[9].'");
										// $("#" + fieldArray[s][1])
										// 	.one("select2:open", function () {
										// 		setTimeout(() => {
										// 			document.querySelector(".select2-container--open .select2-search__field")?.focus();
										// 		}, 1000);
										// 	})
										// 	.select2("open");

										// $("#" + fieldArray[s][1]).val(0).trigger("change"); // Optional: reset value

										$("#" + fieldArray[s][1]).select2("open");
										focusSelect2Search();
										break;
									}
								}else{
									if(fieldArray[s][1] == "kreon-grid-'.$d[3].''.$d[9].'"){
										console.log(fieldArray[s][1] +"=="+ "kreon-grid-'.$d[3].''.$d[9].'");
										setTimeout(function() {
											$("#" + fieldArray[s][1]).focus();
										}, 500);
										break;
									}
								}
							// }
						}
					}
			
				}

				//remove row
				$("#panel'.$d[3].'").on("click",".removes'.$d[3].'",function(){
					//Delete Button disabled
					$(".removes'.$d[3].'").attr("disabled", true);

					var onCLickVariable = $(this);
					var gridEdit = 1;
					var [currentData, cuurrentRowDataWithComma] = getCurrentRowDataForSpValidation(onCLickVariable,mArray,gridEdit);
					console.log(currentData);
					var GridEditId = onCLickVariable.closest("tr").find("#'.$d[3].'GridEditID").val();
					var GridTempId = onCLickVariable.closest("tr").find("#'.$d[3].'GridTempID").val();
					

					$.ajax({
						type : "POST",
						dataType: "json",
						data : {ValidationOn:"griddelete", formData: currentData, FormID:"'.$FormID.'", GridID:$("#GridId'.$d[3].'").val(), GridEditID: GridEditId, GridRowTempId: GridTempId},
						url : "validateForm.php",
						success:function(output) {
							if(output["status"] == true){
								if(output["data"]["Success"] == 1){
									RemoveRow(onCLickVariable);
									$(".removes'.$d[3].'").attr("disabled", false);
									if(output["tempData"]){
										console.log(output["tempData"][0]);
										$("#"+Object.keys(output["tempData"][0])).empty();
										for(var i=0; i<output["tempData"].length; i++){
											$("#"+Object.keys(output["tempData"][i])).append(Object.values(output["tempData"][i]));
										}
									}
								}else{
									if(output["data"]["Success"] == 2){
										if (confirm(output["data"]["Msg"]+". Are you sure you want to submit?") == true) {
											RemoveRow(onCLickVariable);
											$(".removes'.$d[3].'").attr("disabled", false);
											if(output["tempData"]){
												// console.log(output["tempData"][0]);
												$("#"+Object.keys(output["tempData"][0])).empty();
												for(var i=0; i<output["tempData"].length; i++){
													$("#"+Object.keys(output["tempData"][i])).append(Object.values(output["tempData"][i]));
												}
											}
										} else {
											$(".removes'.$d[3].'").attr("disabled", false);
										}
									}else if(output["data"]["Success"] == 0){
										alert(output["data"]["Msg"]);
										$(".removes'.$d[3].'").attr("disabled", false);
									}else{
										alert("Internal Server Error");
										$(".add'.$d[3].'").attr("disabled", false);
									}
								}
							}else{
								RemoveRow(onCLickVariable);
								$(".removes'.$d[3].'").attr("disabled", false);
							}
						}
					});
				});

				function RemoveRow(onCLickVariable){
					// If GridCalculation 1 then add sum in footer
					for(var t=0; t < mArray.length; t++){
						//If GridCalculation greater than 0 then add field name & calculation type in array
						if(mArray[t][12] > 0){
							if (!gridCalculationFieldName.includes(mArray[t][1])) {
								gridCalculationFieldName.push(mArray[t][1]);
								gridCalculationType.push(mArray[t][12]);
							}
						}
					}

					var cnt = document.getElementById("cnt'.$d[3].'").value;
					cnt = +cnt - 1;
					onCLickVariable.parents(".grp'.$d[3].'").remove();
					document.getElementById("cnt'.$d[3].'").value=cnt;

					//Call grid total calculation function
					if(gridCalculationFieldName.length > 0){
						CreateOnChangeScriptGridCalculation(gridCalculationFieldName, gridCalculationType,"'.$d[3].'");	
					}
				}

				//On edit validation if noneditablefields get then add readonly in respective field
				function UpdateNonEditableField(NonEditableFieldString, SerialNo){
					var NonEditableFields = NonEditableFieldString.split("|");
					for(var i =0; i < NonEditableFields.length; i++){
						var type = $("#kreon-grid-"+SerialNo+NonEditableFields[i].trim()).prop("type");

						if(type == "text" || type == "date" || type == "number" || type == "email" || type == "textarea" || type == "time"){
							$("#kreon-grid-"+SerialNo+NonEditableFields[i].trim()).css("pointer-events","none").css("background","#eee");
						}else if(type == "select-one"){
							$("#kreon-grid-"+SerialNo+NonEditableFields[i].trim()).next("span").find(".select2-selection--single").css("pointer-events","none").css("background","#eee");
						}else if(type == "select-multiple"){
							$("#kreon-grid-"+SerialNo+NonEditableFields[i].trim()).next("span").find(".select2-selection--multiple").css("pointer-events","none").css("background","#eee");
						}else if(type == "checkbox"){
							if($("#kreon-grid-"+SerialNo+NonEditableFields[i].trim()).parent().hasClass("switch-primary") == true){
								$("#kreon-grid-"+SerialNo+NonEditableFields[i].trim()).parent().css("pointer-events","none");
							}else{
								$("*[id*="+NonEditableFields[i].trim()+"]").each(function() {
									$(this).css("pointer-events","none");
								});
							}
						}else if(type == "radio"){
							$("*[id*="+NonEditableFields[i].trim()+"]").each(function() {
									$(this).css("pointer-events","none");
								});
						}else if(type == "file"){
							if($("#kreon-grid-"+SerialNo+NonEditableFields[i].trim()).prop("multiple") == true){
								$("#kreon-grid-"+SerialNo+NonEditableFields[i].trim()).css("pointer-events","none");
								$("#kreon-grid-"+SerialNo+NonEditableFields[i].trim()).nextAll().find("span").remove();	
							}
						}else{
							$("#kreon-grid-"+SerialNo+NonEditableFields[i].trim()).css("pointer-events","none").css("background","#eee");
						}
					}
				}

				//edit row 
				$("#panel'.$d[3].'").on("click",".edits'.$d[3].'",function(){

					$(".edits'.$d[3].'").attr("disabled", true);

					var onCLickVariable = $(this);
					var gridEdit = 1;
					var [currentData, cuurrentRowDataWithComma] = getCurrentRowDataForSpValidation(onCLickVariable,mArray,gridEdit);
			
					//On edit previour row edit & remove button enabled
					if($(".AddEdit a:nth-child(3)").attr("id") != undefined){
						// Find the last <td> in the first row
						var firstRowLastTdId = $(".AddEdit a:nth-child(3)").attr("id");

						// Find the second row using the ID from the last <td> in the first row
						$("tr#" + firstRowLastTdId).find("td:last").find(".edits'.$d[3].'").show();
						$("tr#" + firstRowLastTdId).find("td:last").find(".removes'.$d[3].'").show();
					}

					var GridEditId = onCLickVariable.closest("tr").find("#'.$d[3].'GridEditID").val();
					var GridTempId = onCLickVariable.closest("tr").find("#'.$d[3].'GridTempID").val();

					$.ajax({
						type : "POST",
						dataType: "json",
						data : {ValidationOn:"gridedit", formData: currentData, formDataWithComma:cuurrentRowDataWithComma, FormID:"'.$FormID.'", GridID:$("#GridId'.$d[3].'").val(), GridEditID: GridEditId, GridRowTempId: GridTempId},
						url : "validateForm.php",
						success:function(output) {
						
							if(output["status"] == true){
								if(output["data"]["Success"] == 1){
									//Remove edited row edit & delete button
									onCLickVariable.closest("tr").find("td:last").find(".edits'.$d[3].'").css("display","none");
									onCLickVariable.closest("tr").find("td:last").find(".removes'.$d[3].'").css("display","none");

									EditRow(onCLickVariable,GridEditId,GridTempId);
									$(".edits'.$d[3].'").attr("disabled", false);
									if(output["data"]["NonEditableFields"]){
										
										return UpdateNonEditableField(output["data"]["NonEditableFields"],"'.$d[3].'");
									}
								}else{
									if(output["data"]["Success"] == 2){
										if (confirm(output["data"]["Msg"]+". Are you sure you want to submit?") == true) {
											//Remove edited row edit & delete button
											onCLickVariable.closest("tr").find("td:last").find(".edits'.$d[3].'").css("display","none");
											onCLickVariable.closest("tr").find("td:last").find(".removes'.$d[3].'").css("display","none");

											EditRow(onCLickVariable,GridEditId,GridTempId);
											if(output["data"]["NonEditableFields"]){
												
												return UpdateNonEditableField(output["data"]["NonEditable"]);
											}
											$(".edits'.$d[3].'").attr("disabled", false);
										} else {
											$(".edits'.$d[3].'").attr("disabled", false);
										}
									}else if(output["data"]["Success"] == 0){
										alert(output["data"]["Msg"]);
										$(".edits'.$d[3].'").attr("disabled", false);
									}else{
										alert("Internal Server Error");
										$(".add'.$d[3].'").attr("disabled", false);
									}
								}
							}else{
								//Remove edited row edit & delete button
								onCLickVariable.closest("tr").find("td:last").find(".edits'.$d[3].'").css("display","none");
								onCLickVariable.closest("tr").find("td:last").find(".removes'.$d[3].'").css("display","none");
								
								EditRow(onCLickVariable,GridEditId,GridTempId);
								$(".edits'.$d[3].'").attr("disabled", false);
							}
						}
					});
				});

				// Edit row in grid
				function EditRow(onCLickVariable,GridEditId,GridTempId){
					console.log("EDITING....sending cursor to '.$d[9].'")
					var row = onCLickVariable.parents("tr").attr("id");
					
					var rowClass = $("#" + row).attr("class");
					$("." + rowClass).removeClass("being-edited");
					$("#" + row).addClass("being-edited");

					$(".AddEdit a:nth-child(2)").attr("id", row);
					$(".AddEdit a:nth-child(2)").attr("tabindex", "'.$gridLastTabIndex.'");
					$(".AddEdit a:nth-child(3)").attr("id", row);
					$(".AddEdit").find("#'.$d[3].'GridEditID").val(GridEditId);
					$(".AddEdit").find("#'.$d[3].'GridTempID").val(GridTempId);

					$(".AddEdit a:nth-child(2)").show();
					$(".AddEdit a:nth-child(3)").show();
					$(".AddEdit a:nth-child(1)").css("display","none");

					
					for(var t=0; t < mArray.length; t++){
						if(mArray[t][0] == 17 || mArray[t][0] == 16){
							var EditFldvalue = onCLickVariable.closest("tr").find("#'.$d[3].'"+mArray[t][1]+"-text").val();
							var EditFldvalue1 = onCLickVariable.closest("tr").find("#'.$d[3].'"+mArray[t][1]).val();
						}else if(mArray[t][0] == 12){
							var EditFldvalue = onCLickVariable.closest("tr").find("#'.$d[3].'"+mArray[t][1]).next("a").attr("href");
							var EditFldvalue1 = onCLickVariable.closest("tr").find("#'.$d[3].'"+mArray[t][1]).val();
							
						}else{
							var EditFldvalue = onCLickVariable.closest("tr").find("#'.$d[3].'"+mArray[t][1]).val();
						
						}
						
						if(mArray[t][0] == 5 || mArray[t][0] == 19){
							var selectedOption = onCLickVariable.closest("tr").find("#'.$d[3].'"+mArray[t][1] + " :selected").text();
							var $newOption = $("<option selected=\"selected\"></option>").val(EditFldvalue).text(selectedOption)
							$("#kreon-grid-'.$d[3].'"+mArray[t][1]).select2("open");
							$("#kreon-grid-'.$d[3].'"+mArray[t][1]).append($newOption);
							$("#kreon-grid-'.$d[3].'"+mArray[t][1]).select2("close");

						}else if(mArray[t][0] == 17 || mArray[t][0] == 16){
							$("#kreon-grid-'.$d[3].'"+mArray[t][1]+"-text").val(EditFldvalue);
							$("#kreon-grid-'.$d[3].'"+mArray[t][1]).val(EditFldvalue1);
						}else if(mArray[t][0] == 12){
							if(EditFldvalue1 != 0){
								console.log(EditFldvalue1,EditFldvalue);
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).parent("label").after("<input readonly=\"\" type=\"hidden\" value=\'"+EditFldvalue1+"\' name=\"kreon-grid-'.$d[3].'"+mArray[t][1]+"[]\" id=\"kreon-grid-'.$d[3].'"+mArray[t][1]+"\"><a target=\"_blank\" href=\'"+EditFldvalue+"\'><img src=\'"+EditFldvalue+"\' width=\"60\"></a>")
							}
						}else{
							$("#kreon-grid-'.$d[3].'"+mArray[t][1]).val(EditFldvalue);
			
							//Grid calculation on edit button
							// if(mArray[t][12] > 0){
							// 	console.log(mArray[t][12]+" == "+mArray[t][1]);
							// 	CreateOnChangeScriptGridCalculation("kreon-grid-'.$d[3].'"+mArray[t][1], mArray[t][12],"grid",0);
							// }
						}

						if(mArray[t][18] == 1){
							$("#kreon-grid-'.$d[3].'"+mArray[t][1]).prop("readonly", true); 
						}			
					}
						
					//Cursor focus on grid perticular field that was mention in AfterGridAddCursorPosition field
					if("'.$d[9].'".length > 1){
						$("#kreon-grid-'.$d[3].$d[9].'").focus(); 
					}
				}

				$("#panel'.$d[3].'").on("click",".update'.$d[3].'",function(){
					$(".update'.$d[3].'").attr("disabled", true);

					var onCLickVariable = $(this);
					var gridEdit = 0;
					var [currentData, cuurrentRowDataWithComma] = getCurrentRowDataForSpValidation(onCLickVariable,mArray,gridEdit);
					var GridTempId = onCLickVariable.closest("tr").find("#'.$d[3].'GridTempID").val();

					$.ajax({
						type : "POST",
						dataType: "json",
						data : {ValidationOn:"gridupdate", formData: currentData, formDataWithComma:cuurrentRowDataWithComma, mainFormData:$("form").serialize(), FormID:"'.$FormID.'", GridID:$("#GridId'.$d[3].'").val(), GridEditID: onCLickVariable.closest("tr").find("#'.$d[3].'GridEditID").val(),GridRowTempId: GridTempId},
						url : "validateForm.php",
						success:function(output) {
							if(output["status"] == true){
							
								if(output["data"]["Success"] == 1){
									UpdateRow(onCLickVariable);
									$(".update'.$d[3].'").attr("disabled", false);
									if(output["tempData"]){
										$("#"+Object.keys(output["tempData"][0])).empty();
										for(var i=0; i<output["tempData"].length; i++){
											$("#"+Object.keys(output["tempData"][i])).append(Object.values(output["tempData"][i]));
										}
									}
								}else{
									if(output["data"]["Success"] == 2){
										if (confirm(output["data"]["Msg"]+". Are you sure you want to submit?") == true) {
											UpdateRow(onCLickVariable);
											$(".update'.$d[3].'").attr("disabled", false);
											if(output["tempData"]){
												$("#"+Object.keys(output["tempData"][0])).empty();
												for(var i=0; i<output["tempData"].length; i++){
													$("#"+Object.keys(output["tempData"][i])).append(Object.values(output["tempData"][i]));
												}
											}
										} else {
											$(".update'.$d[3].'").attr("disabled", false);
										}
									}else if(output["data"]["Success"] == 0){
										alert(output["data"]["Msg"]);
										$(".update'.$d[3].'").attr("disabled", false);
									}else{
										alert("Internal Server Error");
										$(".update'.$d[3].'").attr("disabled", false);
									}
								}
							}else{
								UpdateRow(onCLickVariable);
								$(".update'.$d[3].'").attr("disabled", false);
							}
						}
					});
					
				});

				async function UpdateRow(onCLickVariable){
					element = $(".AddEdit a:nth-child(2)");
					var targetRowId = element.attr("id").split("-").pop();	//a1, a2 ...
					$(".AddEdit a:nth-child(2)").removeAttr("id");
					$(".AddEdit a:nth-child(3)").removeAttr("id");
					$(".AddEdit").find("#'.$d[3].'GridEditID").val(0);

					//edited row show edit & remove button
					$("#"+targetRowId).find("td:last").find(".edits'.$d[3].'").css("display","block");
					$("#"+targetRowId).find("td:last").find(".removes'.$d[3].'").css("display","block");

					for(var t=0; t < mArray.length; t++){
						if(mArray[t][0] == 17 || mArray[t][0] == 16){
							var fldvalue = onCLickVariable.closest("tr").find("#kreon-grid-'.$d[3].'"+mArray[t][1]+"-text").val();
							var fldvalue1 = onCLickVariable.closest("tr").find("#kreon-grid-'.$d[3].'"+mArray[t][1]).val();
						}else if(mArray[t][0] == 12){
							var fldvalue = $("#kreon-grid-'.$d[3].'"+mArray[t][1]).prop("files")[0];
						}else{
							var fldvalue = onCLickVariable.closest("tr").find("#kreon-grid-'.$d[3].'"+mArray[t][1]).val();
						}

						if(mArray[t][0] == 5 || mArray[t][0] == 19){
							var selectedOption = $("#kreon-grid-'.$d[3].'"+mArray[t][1] + " :selected").text();
							var $newOption = $("<option selected=\"selected\"></option>").val(fldvalue).text(selectedOption)

							$("#"+targetRowId).find("#'.$d[3].'"+mArray[t][1]).append($newOption).trigger("change");
							

							if(mArray[t][17] == null || mArray[t][17].length == 0 || mArray[t][16] == null || mArray[t][16].length == 0){
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).select2("open");
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).val(0).trigger("change"); // Remove DD selected field from first row
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).select2("close");
							}

							//after update if not readonly then remove readonly
							if(mArray[t][23] != 1){
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).next("span").find(".select2-selection--single").prop("readonly", false).css("pointer-events","").css("background-color","white");
							}

						}else if(mArray[t][0] == 17 || mArray[t][0] == 16){
							$("#"+targetRowId).find("#'.$d[3].'"+mArray[t][1]+"-text").val(fldvalue);
							$("#"+targetRowId).find("#'.$d[3].'"+mArray[t][1]).val(fldvalue1);

							if(mArray[t][17] == null || mArray[t][17].length == 0 || mArray[t][16] == null || mArray[t][16].length == 0){
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]+"-text").val("");
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).val("");
							}

							if(mArray[t][23] != 1){
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]+"-text").prop("readonly", false).css("pointer-events","").css("background-color","white");
							}
						}else if(mArray[t][0] == 12){
							if(fldvalue == undefined){
								if($("#kreon-grid-'.$d[3].'"+mArray[t][1]).parent("label").next().length != 0){
									$("#kreon-grid-'.$d[3].'"+mArray[t][1]).parent("label").next().remove();
									$("#kreon-grid-'.$d[3].'"+mArray[t][1]).parent("label").next().remove();
								}
								
							}else{
								var imgFieldName = mArray[t][1];
								var gridSerial = "'.$d[3].'";
								var filePath = onCLickVariable.closest("tr").find("#kreon-grid-'.$d[3].'"+mArray[t][1]).val(); 
								var ext = filePath.substr(filePath.lastIndexOf(".")+1,filePath.length);

								var gridId = "'.$d[7].'";
								var imgFlag = false;

								$("#"+targetRowId).find("#'.$d[3].'"+mArray[t][1]).next().remove();
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).parent("label").next().remove();

								base64String = await getBase64(fldvalue);

								if(base64String !== null){
									// Await the result of base64AjaxFunction before proceeding
									let MediaResult = await base64AjaxFunction(base64String, imgFieldName, gridSerial, ext, targetRowId, gridId);

									if (MediaResult && MediaResult.response === "true") {
										var MediaID = MediaResult.data;
										var MediaPath = MediaResult.imagepath;

										if (MediaID == 0) {
											console.log("MediaID is 0", MediaID);
										} else {
											imgFlag = true;
											console.log("MediaID is not 0", MediaID, MediaPath);
											$("#"+targetRowId).find("#'.$d[3].'"+mArray[t][1]).val(MediaID);
											$("#"+targetRowId).find("#'.$d[3].'"+mArray[t][1]).after("<a target=\"_blank\" href=\'"+MediaPath+"\'><img src=\'"+MediaPath+"\' width=\"60\"></a>")

											// $("#"+targetRowId).find("#'.$d[3].'"+mArray[t][1]).next().remove();
											$("#kreon-grid-'.$d[3].'"+mArray[t][1]).parent("label").nextAll("a").first().remove();
										}
									} else {
										console.log("Error while fetching MediaID");
									}
								}

								//Get base64 string & assing to function
								// getBase64(fldvalue).then(
								// 	base64String => {
								// 		base64AjaxFunction(base64String,imgFieldName,gridSerial,ext,targetRowId,gridId)
								// 	  }
								// );
							}
						}else{
							
							$("#"+targetRowId).find("#'.$d[3].'"+mArray[t][1]).val(fldvalue);

							if(mArray[t][17] == null){
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).val("");
							}else{
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).val(mArray[t][17]);
							}

							if(mArray[t][16] == null){
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).val("");
							}else{
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).val(mArray[t][16]);
							}


							if(mArray[t][23] != 1){
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).prop("readonly", false).css("pointer-events","").css("background-color","white");
							}

							//If GridCalculation greater than 0 then add field name & calculation type in array
							if(mArray[t][12] > 0){
								if (!gridCalculationFieldName.includes(mArray[t][1])) {
									gridCalculationFieldName.push(mArray[t][1]);
									gridCalculationType.push(mArray[t][12]);
								}
							}

						}

						$(".AddEdit a:nth-child(1)").show();
						$(".AddEdit a:nth-child(2)").css("display","none");
						$(".AddEdit a:nth-child(3)").css("display","none");
					}

					var gridAlphabet = targetRowId.slice(0,1);
					var gridNumber = +targetRowId.slice(1) + 1;
					
					$("#" + gridAlphabet + gridNumber + " td a.edits" + gridAlphabet).click();

					//Call grid total calculation function
					if(gridCalculationFieldName.length > 0){
						CreateOnChangeScriptGridCalculation(gridCalculationFieldName, gridCalculationType,"'.$d[3].'");	
					}
				}

				$("#panel'.$d[3].'").on("click",".refresh'.$d[3].'",function(){
					element = $(".AddEdit a:nth-child(2)");

					if(element.attr("id") != undefined){
						var targetRowId = element.attr("id").split("-").pop();
						$(".AddEdit a:nth-child(2)").removeAttr("id");
						$(".AddEdit a:nth-child(3)").removeAttr("id");
						var rowClass = $("#" + targetRowId).attr("class");
						$("." + rowClass.replace(" ",".")).removeClass("being-edited");

						var editRowId = $("#" + targetRowId).attr("id");
						$("#"+editRowId).find("td:last a:nth-child(1)").show();
						$("#"+editRowId).find("td:last a:nth-child(2)").show();
					}

					$("#'.$d[3].'GridEditID").val(0);
					$("#'.$d[3].'GridTempID").val(0);

					for(var t=0; t < mArray.length; t++){
						if(mArray[t][0] == 5 || mArray[t][0] == 19){
							if(mArray[t][17].trim().length <= 0){
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).val(null).trigger("change"); // Remove DD selected field from first row
							}
						}else if(mArray[t][0] == 17){
							if(mArray[t][17].trim().length <= 0){
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]+"-text").val("");
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).val("");
							}
						}else if(mArray[t][0] == 16){
							if(mArray[t][17].trim().length <= 0){
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]+"-text").val("");
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).val("");
							}
						}else if(mArray[t][0] == 12){
							if(mArray[t][17].trim().length <= 0){
								//Remove thumbnail & input value
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).val("");
								$("#kreon-grid-'.$d[3].'"+mArray[t][1]).parent("label").nextAll("a").first().remove();
							}
						}

						if(mArray[t][17].trim().length <= 0){
							$("#kreon-grid-'.$d[3].'"+mArray[t][1]).val("");
						}
						$(".AddEdit a:nth-child(1)").show();
						$(".AddEdit a:nth-child(2)").css("display","none");
						$(".AddEdit a:nth-child(3)").css("display","none");

					}
				});

			});
		</script>';
		// print_r($v);
		if(empty($v)){
			$fieldCount = 0;
		}else{
			
			$tableStart = '<table style="table-layout: fixed;word-wrap: break-word;" id="GridTable" class="table table-responsive table-bordered table-hover tmp_GridTable'.$d[3].'">';
			$tableEnd = '</table>';
			$tableHead = '<thead class=" thead-light"><tr>';
			$fvals = '';
			$headerGridStart = '<tr class="grp'.$d[3].'" id="'.$d[3];
			$headerGridEnd   = '">';

			global $moreextradb;
			$gridCalculationFieldName = [];
			$gridCalculationType = [];
			// print_r($m);
			// echo "<pre></pre></br>";
			// print_r($v);
			// echo "<pre></pre></br>";
			// print_r($m[4]);

				for($i = 0; $i < sizeof($m); $i++){		   //style='width:".$m[$i][7]."px' width='".$m[$i][7]."'
				if($m[$i][26] == 0 ){  //to check visibility
					$thClass = " thHidden ";
				}else{
					$thClass = "";
				}
				$tableHead .= "<th class=".$thClass." style='width:".$m[$i][7]."px' width='".$m[$i][7]."'>".$m[$i][2]."";

				if($m[$i][13] > 0){

					$tableHead .= '<button type="button" class="btn btn-primary btn-sm amodal" style="margin-left:2px;" id="AddBtnModal-'.$m[$i][1].'" value="'.$m[$i][13].'-'.$m[$i][1].'-'.$formType.'">Add</button>';

				}

				$tableHead .="</th>";
			}
			$tableHead .= '<th style="width:20px;padding:2px 3px !important;"></th>';

			
			for($k = 0; $k < sizeof($v); $k++){
				// $fvals .= $headerGridStart . $k . $headerGridEnd;
				$fvals .= $headerGridStart . sizeof($v)-$k . $headerGridEnd;

				for($i = 0; $i < sizeof($m); $i++){
				
                    // print_r($v[$k][$i]); 
                    $inputVal = isset($v[$k][$i]) ? $v[$k][$i] : null;

					if($m[$i][26] == 0 ){  //to check visibility
						$thClass .= " thHidden ";
					}else{
						$thClass = "";
					}

					if($m[$i][0] == 0){
					
					    $fvals .= '<td class='.$thClass.'><input readonly type="hidden" class="form-control" name="'.$d[3].''.$m[$i][1].'[]" id="'.$d[3].''.$m[$i][1].'" placeholder="'.$m[$i][2].'" value="'.$inputVal.'" ';

						$fvals .= '></td>';
					}

					if($m[$i][0] == 1){

					    $fvals .= '<td class='.$thClass.'><input readonly type="text" class="form-control" title="'.$inputVal.'" name="'.$d[3].''.$m[$i][1].'[]" id="'.$d[3].''.$m[$i][1].'" placeholder="'.$m[$i][2].'" value="'.htmlspecialchars($inputVal,ENT_QUOTES).'" ';

						$fvals .= '></td>';

						// If GridCalculation 1 then add sum in footer
						if($m[$i][12] == 1 || $m[$i][12] == 2 || $m[$i][12] == 3){
							if (!in_array($m[$i][1], $gridCalculationFieldName)) {
								array_push($gridCalculationFieldName, $m[$i][1]);
								array_push($gridCalculationType,$m[$i][12]);
							}
						}
					
						//$fvals .= '<td><input type="text" value="'.$inputVal.'" class="form-control" name="'.$d[3].''.$m[$i][1].'[]" placeholder="'.$m[$i][2].'" /></td>';
					}

					if($m[$i][0] == 3){
					    $fvals .= '<td ><textarea readonly class="form-control" name="'.$d[3].''.$m[$i][1].'[]" id="'.$d[3].''.$m[$i][1].'" placeholder="'.$m[$i][2].'" '.$m[$i][6].'>'.$inputVal.'</textarea></td>';
					}

            		if($m[$i][0] == 12){	//IF Image
            		    $gridImg = "";
            		    if($inputVal > 0){

            		        $sql1 = "SELECT * FROM kmainmedia WHERE MediaID = " . $inputVal;
							$result1 = db::getInstance()->db_select($sql1);
							$tmpRes = array_change_key_case($result1['result_set'][0]);
							// print_r($tmpRes);
							$tmpMediaName = $tmpRes[strtolower('MediaName')];
							$tmpMediaFolder = $tmpRes[strtolower('MediaFolder')];
							$tmpMediaType = $tmpRes[strtolower('MediaType')];
							if($result1['num_rows'] > 0)
								if(str_contains($tmpMediaType, "png") || str_contains($tmpMediaType, "jpeg") || str_contains($tmpMediaType, "jpg") || str_contains($tmpMediaType, "gif")){
									$gridImg = '<a target="_blank" href="'.$tmpMediaFolder.$tmpMediaName.'"><img src="'.$tmpMediaFolder.$tmpMediaName.'" width="60" /></a>';
								}else{
									$gridImg = '<a target="_blank" href="'.$tmpMediaFolder.$tmpMediaName.'">VIEW</a>';

								}
            		    }

            			$fvals .= '<td><input readonly type="hidden" value="'.$inputVal.'" name="'.$d[3].''.$m[$i][1].'[]" id="'.$d[3].''.$m[$i][1].'" '.$m[$i][6].' />'.$gridImg.'</td>';
            		}

            		if($m[$i][0] == 6){	//IF Date BOX	added by sneha on 01-11-2023
            			$fvals .= '<td ><input readonly type="date" value="'.$inputVal.'" class="form-control dyn1" name="'.$d[3].''.$m[$i][1].'[]" id="'.$d[3].''.$m[$i][1].'" placeholder="'.$m[$i][2].'" '.$m[$i][6].' /></td>';
            		}

					if($m[$i][0] == 7){	//IF Number BOX	added by pornima on 29-1-2024
            			$fvals .= '<td ><input readonly type="number" value="'.$inputVal.'" class="form-control" name="'.$d[3].''.$m[$i][1].'[]" id="'.$d[3].''.$m[$i][1].'" '.$m[$i][6].' /></td>';

						// If GridCalculation 1 then add sum in footer
						if($m[$i][12] == 1 || $m[$i][12] == 2 || $m[$i][12] == 3){
							if (!in_array($m[$i][1], $gridCalculationFieldName)) {
								array_push($gridCalculationFieldName, $m[$i][1]);
								array_push($gridCalculationType,$m[$i][12]);
							}
						}
            		}

					if($m[$i][0] == 5){ // iF DD

						if($m[$i][3] < 0){ //FROM DB

							$index = ((-1) * $m[$i][3]) - 1;
							global $moreextradb;
							//updated by pornima now moreextradb getting using dbFieldName
							// $dbarray = $moreextradb[$index];
							$dbarray = $moreextradb[$m[$i][1]];
							
							$sql1 = "SELECT ".$dbarray[1].",".$dbarray[2]." FROM ".$dbarray[0];				
							if(strlen($dbarray[3]) > 1){
								$sql1 .= " ".$dbarray[3];
							}else{
								$sql1 .= " WHERE 1=1";
							}
							$sql1 .= " AND ".$dbarray[1] ."=". $inputVal ;
							
							$result1 = db::getInstance()->db_select($sql1);
							$row1 = $result1['result_set'];												
							$fvals .= '<td><select readonly style="pointer-events:none;"  name="'.$d[3].''.$m[$i][1].'[]" id="'.$d[3].''.$m[$i][1].'" class="form-control"  ';
							$onchangeHidden = "";
				
							$fvals .= '>';
						
							for($j = 0; $j < sizeof($row1); $j++){ 
								if($inputVal == $row1[$j][trim($dbarray[1])])
									$fvals .= '<option value="'. $row1[$j][trim($dbarray[1])] .'" selected>'.$row1[$j][trim($dbarray[2])].'</option>&nbsp;';
									?>
									<script>
										$(document).ready(function() {
											// Add title to the <select> dropdown
											

											// $(<?php echo "'#".$d[3].$m[$i][1]."'" ?>).on('mouseover', function(event) {
												// console.log('IN');
												$(this).attr('title', <?php echo "'".$row1[$j][trim($dbarray[2])]."'"; ?>);

											// });

										});
									</script>
									<?php
							}
							$fvals .= '</select>'.$onchangeHidden.'</td>';

			
						}
						//else{
							//$fvals .= '<td><select name="'.$d[3].''.$m[$i][1].'[]" placeholder="'.$m[$i][2].'" id="'.$m[$i][1].'" class="form-control" '.$m[$i][6].' ><option value="0">'.$m[$i][2].'</option>';
							//for($j = 0; $j < sizeof($r[$m[$i][3]]); $j++){
								//$p = $j+1;  //value
								//if($p == $inputVal) $fvals .= '<option selected value="'. $p .'" >'.$r[$m[$i][3]][$j].'</option>&nbsp;';
								//else $fvals .= '<option value="'. $p .'" >'.$r[$m[$i][3]][$j].'</option>&nbsp;';
							//}
							$fvals .= '</select></td>';
						//}
					}

					if($m[$i][0] == 16){ //Multy-select Popup Datatable
						// echo "hi";
						if($m[$i][3] < 0){ //FROM DB
							// $index = ((-1) * $m[$i][3]) - 1;
							global $moreextradb;
							// print_r($moreextradb);
							$mapTable = $moreextradb[$m[$i][1]][5];				
							$mapIndex = $moreextradb[$m[$i][1]][6];				
							$mapVariant = $moreextradb[$m[$i][1]][7];
							$joinTable = $moreextradb[$m[$i][1]][0];
							$joinIndex = $moreextradb[$m[$i][1]][1];
							$mapTableEid = $moreextradb[$m[$i][1]][1];
							$mapTableElb = $moreextradb[$m[$i][1]][2];

							$mapTableQuery = 'SELECT '. $joinTable.'.'.$mapTableEid .','. $joinTable.'.'.$mapTableElb .' FROM ' . $mapTable ;
							$mapTableQuery .= ' LEFT JOIN ' . $joinTable . ' ON ' . $mapTable.'.'.$mapVariant . ' = ' . $joinTable.'.'.$joinIndex . ' WHERE ' . $mapIndex . ' = ' . $v[$k][sizeof($v[$k])-2] ;
							
							$mapQueryResult = db::getInstance()->db_select($mapTableQuery);
							
							$mapQueryData = $mapQueryResult['result_set'];
							
							$mapQueryValue = [];
							$mapQueryID = [];
							if(is_array($mapQueryData)){
								for($t=0; $t<sizeof($mapQueryData); $t++){
									array_push($mapQueryValue, $mapQueryData[$t][$mapTableEid]);
									array_push($mapQueryID, $mapQueryData[$t][$mapTableElb]);
								}
								$mTValue = implode(',', $mapQueryValue);
								$mTID = implode(',', $mapQueryID);
							}
						}
						$fvals .= "<td><input disabled type='text'  id='".$d[3].$m[$i][1]."-text' name='".$d[3].$m[$i][1]."-text[]'  data-tag-class='badge badge-primary' class='form-control' style='cursor: pointer;background-color: white;' value='".$mTID."'><input type='hidden' id='".$d[3].$m[$i][1]."' name='".$d[3].$m[$i][1]."[]' value='".$mTValue."'></td> ";

					}

					if($m[$i][0] == 17){ //Single-select Popup Datatable
						if(strlen($m[$i][6]) > 3){
							$str1 = 'AdjustableAmount';
							$AdjustableAmountPos = -1;
							if (strpos($m[$i][6], $str1) !== false){
								$AdjustableAmountPos = strpos($m[$i][6],$str1);
							}
							if($AdjustableAmountPos != -1){
								$newArr = substr($m[$i][6], $AdjustableAmountPos);
								$AdjustableAmountS = substr($newArr, strpos($newArr, '{')+1, strpos($newArr,'}') - 1);
								$AdjustableAmountStatus = str_replace('}','',$AdjustableAmountS);
							}
						}

						if(trim($AdjustableAmountStatus) == 'enable'){
							if($m[$i][1] == 'adjamount' || $m[$i][1] == 'adjamt'){
								$fvals .= "<td><input disabled type='text'  id='".$d[3]."".$m[$i][1]."-text' name='".$d[3]."".$m[$i][1]."-text[]'  data-tag-class='badge badge-primary' class='form-control' style='cursor: pointer;background-color: white;' value='".$inputVal."'><input type='hidden' id='".$d[3]."".$m[$i][1]."' name='".$d[3]."".$m[$i][1]."[]' value='".$inputVal."'></td> ";
							}
						}else{
							if($m[$i][3] < 0){ //FROM DB
								$index = ((-1) * $m[$i][3]) - 1;
								global $moreextradb;
								//updated by pornima now moreextradb getting using dbFieldName
								// $dbarray = $moreextradb[$index];
								$dbarray = $moreextradb[$m[$i][1]];
								// print_r($dbarray);
								$sql1 = "SELECT ".$dbarray[1].",".$dbarray[2]." FROM ".$dbarray[0]." ".$dbarray[3] ." WHERE ".$dbarray[1]."='".$inputVal."'";					
								$result1 = db::getInstance()->db_select($sql1);
								$row1 = $result1['result_set'];	
								if($k_debug){	echo '<br /><br />' . $sql1 . '<br />'; print_r($result1); echo '<br /><br />'; }
							}
							$fvals .= "<td><input disabled type='text'  id='".$d[3].$m[$i][1]."-text' name='".$d[3].$m[$i][1]."-text[]'  data-tag-class='badge badge-primary' class='form-control' style='cursor: pointer;background-color: white;' value='".$row1[0][$dbarray[2]]."'><input type='hidden' id='".$d[3].$m[$i][1]."' name='".$d[3].$m[$i][1]."[]' value='".$inputVal."'></td> ";
						}
					}

                    if($m[$i][0] == 18){
                        // echo $inputVal;
						$fvals .= '<td ><input disabled class="form-control" type="time" value="'.$inputVal.'"  name="'.$d[3].''.$m[$i][1].'[]" id="'.$d[3].''.$m[$i][1].'" "'.$m[$i][6].'" /></td>';
					}

                    if($m[$i][0] == 19){ // iF DD
						if($m[$i][3] < 0){ //FROM DB
							$index = ((-1) * $m[$i][3]) - 1;
							global $moreextradb;
							//updated by pornima now moreextradb getting using dbFieldName
							// $dbarray = $moreextradb[$index];
							$dbarray = $moreextradb[$m[$i][1]];
							$sql1 = "SELECT ".$dbarray[1].",".$dbarray[2]." FROM ".$dbarray[0]." ". $dbarray[3];					
							$result1 = db::getInstance()->db_select($sql1);
							$row1 = $result1['result_set'];												
							$fvals .= '<td><select readonly style="pointer-events: none;" name="'.$d[3].''.$m[$i][1].'[]" id="'.$d[3].''.$m[$i][1].'" class="form-control"  ';
							$onchangeHidden = "";
							// if($m[$i][8] == 1){
							// 	$onchange = " onchange=\"CreateOnchangeScriptGrid(this);\"";
							// 	$fvals .= $onchange;
							// 	$onchangeHidden = '<input type="hidden" id="kreon-grid-'.$d[3].$m[$i][1].'-hidden" value="' . $m[$i][9] . '" />';//,' . $m[$i][1] . ',' . $m[$i][0] . '
							// 	// $changeScript .= "$('[name=\"".$d[3].$m[$i][1]."[]\"]').change(function(evt) { alert('The option with value ' + $(this).val() + ' and text ' + evt.target.value + ' ' + $(this).text() + ' was selected.'); });";
							// }
							$fvals .= '>';
							// $fvals .= '><option value="0">'.$m[$i][2].'</option>';

							for($j = 0; $j < sizeof($row1); $j++){ 
								if($inputVal == $row1[$j][$dbarray[1]])
									$fvals .= '<option value="'. $row1[$j][$dbarray[1]] .'" selected>'.$row1[$j][$dbarray[2]].'</option>&nbsp;';
								else
									$fvals .= '<option value="'. $row1[$j][$dbarray[1]] .'" >'.$row1[$j][$dbarray[2]].'</option>&nbsp;';
							}
							$fvals .= '</select>'.$onchangeHidden.'</td>';
						}
						else{
							$fvals .= '<td><select name="'.$d[3].''.$m[$i][1].'[]" placeholder="'.$m[$i][2].'" id="'.$m[$i][1].'" class="form-control" '.$m[$i][6].' >'; //<option value="0">'.$m[$i][2].'</option>

							for($j = 0; $j < sizeof($r[$m[$i][3]]); $j++){
								$p = $j+1;  //value
								if($p == $inputVal) $fvals .= '<option selected value="'. $p .'" >'.$r[$m[$i][3]][$j].'</option>&nbsp;';
								else $fvals .= '<option value="'. $p .'" >'.$r[$m[$i][3]][$j].'</option>&nbsp;';
							}
							$fvals .= '</select></td>';
						}
					}
				}
				// echo "jjjj".$v[$k][sizeof($v[$k])-1];
				$fvals .= '<td style="padding1:2px 3px !important;"><a style="padding:3px;margin:2px;" href="javascript:void(0)" class="btn btn-info edits'.$d[3].'"><span class="glyphicon glyphicon glyphicon-pencil" aria-hidden="true"></span></a><a style="padding:3px;" href="javascript:void(0)" class="btn btn-danger removes'.$d[3].'"><span class="glyphicon glyphicon glyphicon-remove" aria-hidden="true"></span></a><input type="hidden" name="'.$d[3].'GridEditID[]" id="'.$d[3].'GridEditID" value="'.$v[$k][sizeof($v[$k])-2].'"><input type="hidden" name="'.$d[3].'GridTempID[]" id="'.$d[3].'GridTempID" value="'.$v[$k][sizeof($v[$k])-1].'"></td>';
				$fvals .= '</tr>';
				//$fvals .= '<td><a href="javascript:void(0)" class="btn btn-danger removes'.$d[3].'"><span class="glyphicon glyphicon glyphicon-remove" aria-hidden="true"></span></a></td>';
			}	

			$fieldCount = $k;
		}

		$fldCnt = '<input type="hidden" value="'.$fieldCount.'" name="cnt'.$d[3].'" id="cnt'.$d[3].'" data-minrowsreq="'.$d[11].'"><input type="hidden" value="'.$d[7].'" name="GridId'.$d[3].'" id="GridId'.$d[3].'">';
		$tableHead .= "</tr>";
		$tableHead .= "</thead>";
		$hiddenTableForImport = '<table class="hidden" id="GridTable'.$d[3].'">'.$tableHead.'</table>';
		//echo htmlspecialchars($tableHead);
		$footerGrid = "";
		// if($d[6] == 1){	
			$footerGrid = '<tfoot><tr>'; //class="grp'.$d[3].'" id="'.$d[3].'GridFooter"
				for($i = 0; $i < sizeof($m); $i++){	
					// echo sizeof($m);
					if($m[$i][12] > 0){	 
						$footerGrid .= '<td><input type="number" class="form-control" readonly name="" id="'.$d[3]."".$m[$i][1].'Footer" value=""></td>';
					}else{
						if($m[$i][0] > 0 || $m[$i][0] == 1){	 
							$footerGrid .= '<td ></td>';
						}
					}
				}
				$footerGrid .= '<td></td>';
			$footerGrid .= '</tr></tfoot>';
		// }

		if($gridCalculationFieldName != null && count($gridCalculationFieldName) > 0){
			?>
			<script>
				setTimeout(
					function() {
						CreateOnChangeScriptGridCalculation(<?php echo json_encode($gridCalculationFieldName); ?>,<?php echo json_encode($gridCalculationType); ?>,<?php echo "'".$d[3]."'"; ?>);
					},
				500);
			</script>
			<?php
		}

		if(empty($v)){
			return $script . $temp . $fldCnt . $hiddenTableForImport . $tableStart . $tableHead . $headerGridStart . "0" . $headerGridEnd . $flds . $end . $footerGrid . $tableEnd."</div></div>";
		}else{
			return $script . $temp . $fldCnt . $hiddenTableForImport . $tableStart . $tableHead . $flds . $fvals . $end . $footerGrid . $tableEnd."</div></div>";
		}
}

function debugArray($arr){		//DebugArray($result);
	print("<pre>".print_r($arr,true)."</pre>");
}

	//Reqd for More Grid ID Section Identifier. Also Reqd for View mode serials
	//$serials = array("a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q");

	include 'model.php';

	// Function to log developer-level errors to the DeveloperErrorLogs table
	function logDevError($FieldName, $FormID, $session, $desc, $solution) {
		// SQL INSERT statement to log error details into DeveloperErrorLogs
		$sqlDev = "INSERT INTO DeveloperErrorLogs (
			[FormID], [CompanyID], [DivisionID], [YearCode], [Database_name],
			[ErrorCode], [ErrorField], [Description], [RecommendedSolution], [CreatedBy], [CreatedAt]
		) VALUES (
			$FormID,
			{$session['dbCompany']},
			{$session['dbDivision']},
			{$session['dbYearID']},
			'{$session['dbName']}',
			100,
			'$FieldName',
			'$desc',
			'$solution',
			{$session['user_id']},
			GETDATE()
		)";
		// Execute the insert query using the master DB instance
		db::getInstanceMaster()->db_insertQuery($sqlDev);
	}

	function array_change_key_case_recursive($array) {
		$result = [];
		foreach ($array as $key => $value) {
			$lowercaseKey = strtolower($key);
			if (is_array($value)) {
				$result[$lowercaseKey] = array_change_key_case_recursive($value); // Recursively change case for nested arrays
			} else {
				$result[$lowercaseKey] = $value;
			}
		}
		return $result;
	}

	if($viewpage == 1){	//VIEW MODE

		// Check if the FormEditValidationMessage is set in the session
		if (isset($_SESSION['FormEditValidationMessage'])) {
			$alertMessage = $_SESSION['FormEditValidationMessage'];
			// Display the alert with JavaScript
			echo '<script>alert("' . htmlspecialchars($alertMessage, ENT_QUOTES, 'UTF-8') . '");</script>';
			
			// Unset the session variable after displaying the alert so it doesn't show again on refresh
			unset($_SESSION['FormEditValidationMessage']);
		}

		function handleDeleteEntry($delID, $FormID, $db) {
			// exit();
			$isDeletedFlag = false;
			$deleteErrorMsg = '';
			
			// Check if delete SP exists
			$deleteSPName = "sp_ValidateDelete_$FormID";
			$sql = "SELECT COUNT(*) AS found FROM information_schema.routines WHERE routine_schema = 'DBO'  AND  routine_name = '$deleteSPName'";
			$sqlresult = db::getInstance()->db_select($sql);
		
			if ($sqlresult['result_set'][0]['found'] == 0) {
				$deleteConfirm = 1; // No SP, delete directly
			} else {
				// Call SP to check validations before deleting
				$session = 'YearCode='.$_SESSION['dbYear'].' AND DivisionId='.$_SESSION['dbDivision'].' AND CompanyId='.$_SESSION['dbCompany'].' AND FormID='.$FormID;
				$sp['params'] = ['@DELETEID','@TABLENAME','@USERID','@SESSION'];
				$sp['values'] = ["'$delID'","'".$db[0]."'","'".$_SESSION['user_id']."'","'$session'"];
				
				$result = db::getInstance()->db_sp_select($deleteSPName, $sp['params'], $sp['values']);
				if ($result['error'] == 1) {
					$deleteConfirm = -1;
					$deleteErrorMsg = $result['error_statement'];
				} else {
					$row = $result['result_set'][0][0];
					$deleteConfirm = ($row['Success'] == 1) ? 1 : -1;
					if ($deleteConfirm == -1) {
						$deleteErrorMsg = $row['Msg'];
					}
				}
			}
		
			if ($deleteConfirm == 1) {
				// Delete from main table
				$delData = "SELECT * FROM " . $db[0] . " WHERE " . $db[1]." = " . $delID;
				$delResult = db::getInstance()->db_select($delData);
		
				$query1 = "DELETE FROM " . $db[0] . " WHERE " . $db[1]." = " . $delID;
				$query1result = db::getInstance()->db_update($query1);
		
				if ($query1result['error']) {
					$isDeletedFlag = false;
					$deleteErrorMsg = $query1result['error_statement'];
				} else {
					$isDeletedFlag = true;
		
					// Get grid fields
					$query2 = "SELECT GridId FROM kmainfields WHERE FieldType=14 AND FormID=$FormID";
					$query2result = db::getInstanceMaster()->db_select($query2);
					$gridResult = $query2result['result_set'];
		
					// Loop through grids and delete
					for ($i = 0; $i < sizeof($gridResult); $i++) {
						$query3 = "SELECT TableName, TablePrimaryKey, TableUnique FROM kmaingrid WHERE GridId=" . $gridResult[$i]['GridId'];
						$query3result = db::getInstanceMaster()->db_select($query3);
		
						$tableInfo = $query3result['result_set'][0];
						$query4 = "DELETE FROM " . $tableInfo['TableName'] . " WHERE " . $tableInfo['TablePrimaryKey'] . " = " . $delResult['result_set'][0][$tableInfo['TableUnique']];
						db::getInstance()->db_update($query4);
					}
				}
			} else {
				$isDeletedFlag = false;
			}
		
			// Return result as an array
			return [
				'isDeleted' => $isDeletedFlag,
				'errorMsg' => $deleteErrorMsg
			];
		}

		$isDeletedFlag = false;
		$deleteConfirm = 0;
		$deleteErrorMsg = 'Unable to delete';
		if(isset($_POST["delID"]) || isset($_GET['delID'])){	//check if delete was called
			if(isset($_POST["delID"])){
				$delID = $_POST["delID"];
			}else{
				$delID = $_GET["delID"];
			}


			if($delID > 0){

				/*1742025 This section will checck session & db companyid, divisionid, yearcode are same then allow to edit */

				if($viewSettings[12] != 1)
				{
					// Construct SQL query to select data from the specified table and column based on the given ID
					$checkSessionData = "SELECT * FROM " . $db[0] . " WHERE " . $db[1]." = " . $delID;
					// Execute the SQL query and store the result
					$resultSessionData = db::getInstance()->db_select($checkSessionData);

					$caseSensitiveResultDbData = array_change_key_case_recursive($resultSessionData);

					// Prepare an array of session-based checks comparing session data with database data
					$checks = [
						'CompanyId' => [$_SESSION['dbCompany'], $caseSensitiveResultDbData['result_set'][0]['companyid']],
						'DivisionId' => [$_SESSION['dbDivision'], $caseSensitiveResultDbData['result_set'][0]['divisionid']],
						'YearCode' => [$_SESSION['dbYear'], $caseSensitiveResultDbData['result_set'][0]['yearcode']],
					];

					// Loop through each check and compare the session value with the corresponding database value
					foreach ($checks as $field => [$expected, $posted]) {
						// If any value does not match, log the error and redirect user back to the form view
						if ($posted != $expected) {
							logDevError($field, $FormID, $_SESSION, "$field mismatch: POST = $expected, Expected = $posted", "Mismatch post & session $field");

							// Set the alert message in the session
							$_SESSION['FormEditValidationMessage'] = "Entry Not Deleted. $field mismatch"; // Customize the message

							$fullURL = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

							// Redirect user to the referring page, indicating a view mode with the form ID
							echo '<script>window.location="'.$fullURL.'";</script>';
							exit(); // Stop script execution after redirect
						}
					}
				}

				/* --------------------------------------------- */

				$result = handleDeleteEntry($delID, $FormID, $db);
				$isDeletedFlag = $result['isDeleted'];
				$deleteErrorMsg = $result['errorMsg'];

				$fullURL = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

				if(isset($_GET['delID']))
				{
					$fullURL = preg_replace('/&delID=.*$/', '', $fullURL);
				}		

				if($isDeletedFlag){
					echo '<script>
						alert("Entry deleted successfully.");
						window.location="'.$fullURL.'";
					</script>';
				} else {
					echo '<script>
						alert("'.$deleteErrorMsg.'");
						window.location="'.$fullURL.'";
					</script>';
				}
			}
		}

		$viewvals = array();

		//there are 3 ways to get the data - SP, View, Table
		if(strlen($db[3]) > 0){ //If ListViewDBName
			$sqlfields = "TOP 1 " . $db[3].".*";
		}else if(strlen($db[4]) > 0){ //If SPName
			$sqlfields = "TOP 1 " . $db[4].".*";
		}else{	//Table Name
			$sqlfields = "TOP 1 " . $db[0].".*";
		}
		
		
		$sqljoin = "";

		$serialCntFrImages = 0;

		if(strlen($db[3]) == 0 && strlen($db[4]) == 0){		//if it is not using SP or View, means Table name
			if(isset($extradb)){ 
				for($i = 0;$i < sizeof($extradb); $i++){
					//if(isset($extra))
					$temp = ""; $noFlag = 0; 
					//$mediaFlag = 0;
					for($j = 0; $j < sizeof($code); $j++){
						if($extradb[$i][0] == $code[$j][3]){

							$temp = $code[$j][0];

							if($code[$j][1] == 10) $noFlag=1; 

							if($code[$j][1] == 11) $noFlag=1; 

							if($code[$j][1] == 12) $noFlag=1; 

							if($code[$j][1] == 13) $noFlag=1; 

							break;
						}
					}

					if($noFlag) goto a;

					$sqlfields .= " , COALESCE(".$serials[$i].".".$extradb[$i][3].", '-') as ".$temp.$serials[$i];
					
					$sqljoin .= " LEFT JOIN ".$extradb[$i][1]." ".$serials[$i]." ON ".$db[0].".".$temp." = ".$serials[$i].".".$extradb[$i][2] ;

					//if($mediaFlag) goto a;
					a: $temp="";
				}
				$serialCntFrImages = sizeof($extradb);
			}
		}
	
		//FOR MEDIA JOIN ONLY 12 on List View & 13 cannot be on List View
		if(strlen($db[3]) == 0 &&  strlen($db[4]) == 0){		//if it is not using SP or View, means Table name
			if(isset($code)){
				for($j = 0; $j < sizeof($code); $j++){
					if($code[$j][1] == 12){ 

						$temp = $code[$j][0];

						$sqlfields .= " , ISNULL(".$serials[$i].".MediaName, '-') as ".$temp.$serials[$i]."Name";

						$sqlfields .= " , ISNULL(".$serials[$i].".MediaFolder, '-') as ".$temp.$serials[$i]."Folder";

						$sqlfields .= " , ISNULL(".$serials[$i].".MediaType, '-') as ".$temp.$serials[$i]."Type";

						$sqljoin .= " LEFT JOIN kmainmedia ".$serials[$i]." ON ".$db[0].".".$temp." = ".$serials[$i++].".MediaID";
					}
				}
			}else{
				echo "Please add Form fields";
				exit();
			}
				
		}

		/************NEW 12 07 2020************/
		if(!isset($forceCondition)){
			$forceCondition = "";
		}
		if(strlen($forceCondition) > 2){
		    $db[2] = $forceCondition;
		}else{
			if(strlen($db[2]) > 2){
				$db[2] = " ".$db[2];
			}elseif(strlen($db[3]) > 2){
				$db[3] = $db[3];
			}elseif(strlen($db[4]) > 2){
				$db[4] = $db[4];
			}else{
				$db[2] = " Order by ".$db[0].'.'.$db[1]." Desc";
			}
		}
		
		if(strlen($db[4]) > 0){		//USING SP
			$sql = $db[4]; //." Order by ".$db[0].'.'.$db[1]." Desc";
			$sqlColumns = "sp_describe_first_result_set N'$db[4]'";
			// $sqlColumns = $db[4];
			$data['params'] = [];
			$sp['values'] = [];

			$resultColumns = db::getInstance()->db_sp_select($sqlColumns, $data['params'], $sp['values']);
	
			$resultColumnsRow = $resultColumns['result_set'];
		}else if(strlen($db[3]) > 0){	//USING VIEW
			$sql = "SELECT ".$sqlfields." FROM ".$db[3]; //." Order by ".$db[0].'.'.$db[1]." Desc";
			$sqlColumns = "SELECT * FROM information_schema.columns WHERE table_name="."'".trim($db[3])."'";

			$resultColumns = db::getInstance()->db_select($sqlColumns);
			$resultColumnsRow = $resultColumns['result_set'];
			
		}else{		//USING TABLE
			$sql = "SELECT ".$sqlfields." FROM ".$db[0]." " .$sqljoin."  ".$db[2]; //." Order by ".$db[0].'.'.$db[1]." Desc";
		}
		//$sql = "SELECT ".$sqlfields." FROM ".$db[0]." " .$sqljoin." Order by ".$db[0].'.'.$db[1]." Desc";
		
		if($k_debug){ echo '<br/>' . $sql; }

		if (in_array($db[0],$MainDBTable)){	//CHECK WHY IS THIS USED?????????????????????
			$Instance = db::getInstanceMaster();
			$dbName = 'Main';
		}else{
			$Instance = db::getInstance();
			$dbName = $_SESSION['dbName'];
		}

		if(strlen($db[4]) > 0){
			$resultColumns = db::getInstance()->db_sp_select($sql, $data['params'], $sp['values']);
		}else{
			$result = $Instance->db_select($sql);
		}
		
		if($k_debug){ echo '<br/>'; print_r($result); }
		// print("<pre>".print_r($result)."</pre>");
		$sr = array();

		for($j = 0; $j < sizeof($code); $j++){
			if($code[$j][1] == 12 || $code[$j][1] == 13 ){
				$sr[$j] = $serialCntFrImages++;
			}
		}

		if(strlen($db[3]) == 0 || strlen($db[4]) == 0){
			for($i = 0; $i < $result['num_rows']; $i++){ //WHILE LOOP FOR $row
				for($j = 0; $j < sizeof($code); $j++){
					//echo "<br />" . $code[$j][3];
					if($code[$j][1] == 14 ){
						//Grid 
						$viewvals[$i][$j] = "";
					}else{
						if($code[$j][1] == 10 ){ //DB REFERENCE WITH MULTIPLE
							$viewvals[$i][$j] = "";
						}else{
							if($code[$j][1] == 11 || $code[$j][1] == 13 || $code[$j][1] == 16){ //DB REFERENCE WITH MULTIPLE
								$viewvals[$i][$j] = "";
							}else{
								if($code[$j][1] == 12){ //SINGLE MEDIA 
									$mediaType = $result['result_set'][$i][$code[$j][0].$serials[$sr[$j]]."Type"];
									if($mediaType == "jpg" || $mediaType == "jpeg" || $mediaType == "png" || $mediaType == "bmp" ){
										$viewvals[$i][$j] = "<a href='" . SITE_ROOT . $result['result_set'][$i][$code[$j][0].$serials[$sr[$j]]."Folder"] . "" . $result['result_set'][$i][$code[$j][0].$serials[$sr[$j]]."Name"]."' target='_blank'>" . "<img src='" . SITE_ROOT . $result['result_set'][$i][$code[$j][0].$serials[$sr[$j]]."Folder"] . "" . $result['result_set'][$i][$code[$j][0].$serials[$sr[$j]]."Name"]."' height='100px'>"."</a>";
									}else{
										$viewvals[$i][$j] = "<a href='" . SITE_ROOT . $result['result_set'][$i][$code[$j][0].$serials[$sr[$j]]."Folder"] . "" . $result['result_set'][$i][$code[$j][0].$serials[$sr[$j]]."Name"]."' target='_blank'>" . $result['result_set'][$i][$code[$j][0].$serials[$sr[$j]]."Name"]."</a>";
									}
								}else{							
									/*REMOVED EMAIL FROM DB REFERENCE 10 03 2021*/							
									if($code[$j][1] == 9 || $code[$j][1] == 10 || $code[$j][3] < 0 ) //DB REFERENCE 
										$viewvals[$i][$j] = $result['result_set'][$i][$code[$j][0].$serials[(-1*$code[$j][3])-1]];
									else 
										$viewvals[$i][$j] = $result['result_set'][$i][$code[$j][0]];
								}
							}
						}
					}
				}

				//$serialCntFrImages++;
				// print_r($viewvals); //exit();

				$viewvals[$i][$j] = $result['result_set'][$i][$db[1]];

				// print_r($db); exit();
				// print_r($viewvals);
			}
		}
		// print_r($viewvals);
		$k_table_title = "List View";
		// 		$k_table_button_link = $_SERVER['PHP_SELF'].'?form='.$FormID;
		// 		$k_table_button = "Add New";

		if($pageAccessForUser['AddBtn'] == 1 || $bypassUserRoles) { 
    		$k_table_button_link = $_SERVER['PHP_SELF'].'?form='.$FormID;
    		$k_table_button = "Add New";
		}

		//$k_print_title = "Print Title";
		$k_table_headings = '<tr>';
		$k_table_headings1 = '';
		
		//VALIDATION TO CHECK IF PRIMARY KEY IS AVAILABLE IN SP OR VIEW
		$isPrimaryKeyPresent = false;

		if(strlen($db[3]) > 0 || strlen($db[4]) > 0){
			$columnArray = [];
		
			if(strlen($db[4]) > 0){
				if(is_array($resultColumnsRow[0])){
					for($i=0;$i<sizeof($resultColumnsRow[0]); $i++){
						$columnArray[$i]['Field'] = $resultColumnsRow[0][$i]['name'];
						$columnArray[$i]['Type'] = $resultColumnsRow[0][$i]['system_type_name'];
						if($db[1] == $columnArray[$i]['Field'])	$isPrimaryKeyPresent = true;
					}
				}
			}else if(strlen($db[3]) > 0){
				for($i=0;$i<sizeof($resultColumnsRow); $i++){
					// if($resultColumnsRow[$i]['COLUMN_NAME'] == $db[1]) continue;	//ADDED BY MITESH FOR VIEW NAME
					$columnArray[$i]['Field'] = $resultColumnsRow[$i]['COLUMN_NAME'];
					$columnArray[$i]['Type'] = $resultColumnsRow[$i]['DATA_TYPE'];
					// echo $db[1] . " && " . $columnArray[$i]['Field'] . "<br />";
					if($db[1] == $columnArray[$i]['Field'])	$isPrimaryKeyPresent = true;
				}
			}
			//VALIDATION TO CHECK IF PRIMARY KEY IS AVAILABLE IN SP OR VIEW
			if(!$isPrimaryKeyPresent)	{ 
				if($_SESSION['Category'] == 2) //Show error for developer login else only error code
					echo "<p style='font-size:20px; color:red;'>Error Code 635: Primary Key not present in LIST VIEW</p>";
				else 
					echo "<p style='font-size:20px; color:red;'>Error Code 635: ERROR IN STRUCTURE. CONTACT DEVELOPER</p>";
				$sqlDev = "INSERT INTO DeveloperErrorLogs([FormID],[CompanyID],[DivisionID],[YearCode],[Database_name],[ErrorCode],[ErrorField],[Description],[RecommendedSolution],[CreatedBy],[CreatedAt])Values("
							. $FormID .",". $_SESSION['dbCompany'].",".$_SESSION['dbDivision'].",".$_SESSION['dbYearID'].",'".$_SESSION['dbName']."',635,'".$m[$i][1]."','Primary Key not present in LIST VIEW','Add Primary Key in LIST VIEW',".$_SESSION['user_id'].",GETDATE())";
				$resultDev = db::getInstanceMaster()->db_insertQuery($sqlDev);
				// echo "ERROR IN STRUCTURE. CONTACT DEVELOPER.<script>console.log('Primary Key not present in LIST VIEW');</script>";
				goto NoTablePrint; 
			}

			// array_shift($columnArray);
			$extraButtonCount = 0;
			for($j=0;$j<sizeof($viewcode); $j++){
				if($viewcode[$j][0] == "-2"){
					if($pageAccessForUser['EditBtn'] == 1 || $bypassUserRoles)
						array_push($columnArray, ['Field' => "Edit", 'Type' => "-2"]);
						$extraButtonCount++;
				}else{
					if($viewcode[$j][0] == "-3"){
						if($pageAccessForUser['DeleteBtn'] == 1 || $bypassUserRoles)
							array_push($columnArray, ['Field' => "Delete", 'Type' => "-3"]);
							$extraButtonCount++;				
					}else{
						if($viewcode[$j][0] == "-4"){
							if($pageAccessForUser['OtherBtn'] == 1 || $bypassUserRoles)
								array_push($columnArray, ['Field' => "Action", 'Type' => "-4"]);
								$extraButtonCount++;
						}else{
							if($viewcode[$j][0] == "-5"){
								if($pageAccessForUser['ViewBtn'] == 1 || $bypassUserRoles)
									array_push($columnArray, ['Field' => "View", 'Type' => "-5"]);
									$extraButtonCount++;
							}else{
								if($viewcode[$j][0] == "-6"){
									if($pageAccessForUser['SaveAndPreviewBtn'] == 1 || $bypassUserRoles)
										array_push($columnArray, ['Field' => "SaveAndPreview", 'Type' => "-6"]);
										$extraButtonCount++;
								}else{
									if($viewcode[$j][0] == "-1" )
										array_unshift($columnArray, ['Field' => "Sr. No", 'Type' => "-1"]);

								}
							}	
						}
					}
				}
			}
			
			//Added column for checkbox
			array_unshift($columnArray,['Field' => "checkbox", 'Type' => "-99"]);
			// echo "<pre>";
			// print_r($columnArray);
			echo "<script> var viewcodeArray = []; </script>";
			for($i=0;$i<sizeof($columnArray); $i++){
				if($columnArray[$i]['Type'] > 0){
					$k_table_headings .= '<th>'.$columnArray[$i]['Field'].'</th>';
					$k_table_headings1 .= '<th>';
					if($columnArray[$i]['Type'] > 0 ){
						echo "<script>
							viewcodeArray.push('".$columnArray[$i]['Field']."'); 
							var currentId = '".$columnArray[$i]['Field']."'; // Get the ID
								if (currentId.indexOf(' ') !== -1) {
								// Alert that the ID has a space
								alert(currentId+' search ID contain a space');
							}
						</script>";
						$k_table_headings1 .= "<input type='search' class='form-control' ";
						$k_table_headings1 .= 'id="'.$columnArray[$i]['Field'].'" name="'.$columnArray[$i]['Field'].'" placeholder="'.$columnArray[$i]['Field'].'">';

						echo '<script>
								$(document).ready(function() { 
									$("#'.$columnArray[$i]['Field'].'").keypress(function(e) {
										if(e.which == 13) {
											var variable = "";
											var value = "";
											for(var m=0; m<viewcodeArray.length; m++){
												if(viewcodeArray[m] != ""){
													// Check if the ID contains a space
													// if (viewcodeArray[m].indexOf(" ") !== -1) {
													// 	alert("ID containing space: " + viewcodeArray[m]);
													// 	return; // Stop further execution if theres an ID with space
													// }
													
													if($("#"+viewcodeArray[m]).val() != "" && $("#"+viewcodeArray[m]).val() != undefined){
														value += $("#"+viewcodeArray[m]).val();
														value += ",";
														variable += viewcodeArray[m]+",";
													}
												}
											}
											if (value.charAt(value.length - 1) === ",") {
												value = value.slice(0, -1);
											}
											if (variable.charAt(variable.length - 1) === ",") {
												variable = variable.slice(0, -1);
											}
											$("#datatable-table").DataTable().destroy();
											console.log(variable,value);
											if(value != undefined && value != ""){
												load_data(variable,value);
											}else{
												variable = "";
												value = "";
												load_data(variable,value);
											}
										}
									});
								}); 
							</script>';
					}
					$k_table_headings1 .= '</th>';
				}else{
					if($columnArray[$i]['Type'] == "-99"){	//FOR CHECKBOX
						$k_table_headings .= '<th></th>';
						$k_table_headings1 .= '<th></th>';
					}
					if($columnArray[$i]['Type'] == "-1"){	//FOR Sr No
						$k_table_headings .= '<th>'.$columnArray[$i]['Field'].'</th>';
						$k_table_headings1 .= '<th></th>';
					}
				}				
			}
			$k_table_headings .= '<th>Action(s)</th>';
			$k_table_headings1 .= '<th></th>';
		}else{
			
			//Added column for checkbox
			array_unshift($viewcode,['0' => '-99', '1' => "checkbox", '2' => "", '3' => "", '4' => ""]);

			// echo "<pre>";
			// print_r($viewcode);

			echo "<script> var viewcodeArray = []; </script>";
			for($i=0;$i<sizeof($viewcode); $i++){

				if($viewcode[$i][0] == "-99"){	//FOR CHECKBOX
					$k_table_headings .= '<th></th>';
					$k_table_headings1 .= '<th></th>';
				}else{
					if($viewcode[$i][0] == "-1"){	//FOR Sr No
						$k_table_headings .= '<th>'.$viewcode[$i][1].'</th>';
						$k_table_headings1 .= '<th></th>';
					}else{
							if($viewcode[$i][1] != "Sr. No." && $viewcode[$i][1] != "Edit" && $viewcode[$i][1] != "Delete" && $viewcode[$i][1] != "Action" && $viewcode[$i][1] != "View" && $viewcode[$i][1] != "SaveAndPreview"){
							$k_table_headings .= '<th>'.$viewcode[$i][1].'</th>';
							$k_table_headings1 .= '<th>';

							echo "<script>viewcodeArray.push('".$viewcode[$i][3]."'); </script>";
							$k_table_headings1 .= "<input type='search' class='form-control' ";
							$k_table_headings1 .= 'id="'.$viewcode[$i][3].'" name="'.$viewcode[$i][3].'" placeholder="'.$viewcode[$i][1].'">';

							echo '<script>
								$(document).ready(function() { 
									$("#'.$viewcode[$i][3].'").keypress(function(e) {
										if(e.which == 13) {
											var variable = "";
											var value = "";
											for(var m=0; m<viewcodeArray.length; m++){
												if(viewcodeArray[m] != ""){
													
												if($("#"+viewcodeArray[m]).val() != ""){
														value += $("#"+viewcodeArray[m]).val();
														value += ",";
														variable += viewcodeArray[m]+",";
													}
												}
											}
											if (value.charAt(value.length - 1) === ",") {
												value = value.slice(0, -1);
											}
											if (variable.charAt(variable.length - 1) === ",") {
												variable = variable.slice(0, -1);
											}
											$("#datatable-table").DataTable().destroy();
											if(value != undefined && value != "")
											{
												load_data(variable,value);
											}
											else
											{
												variable = "";
												value = "";
												load_data(variable,value);
											}
										}
									});
								}); 
							</script>';
							}
						
					}
				}
				
			}
			$k_table_headings .= '<th>Action(s)</th>';
			$k_table_headings1 .= '<th></th>';
		}
		
		// print_r($result);
		// var_dump($viewvals);
		$k_table_headings .= '</tr>';
		$k_table_body='';
		/*
			// if(strlen($db[3]) > 0){
			// 	/*
			// 	This was used earlier for list view but as the data is coming from Fetch.php dynamically,
			// 	now this is not required.
			// 	for($i=0; $i<sizeof($result['result_set']); $i++){
			// 		$k_table_body .= '<tr>';
			// 		for($j=0; $j<sizeof($columnArray); $j++){

			// 			if($columnArray[$j]['Field'] == "Edit"){	//EDIT 	
			// 				// if(is_array($viewvals[$i])){
			// 				$x = '<form name="form"  action="'.$_SERVER['PHP_SELF'].'?form='.$FormID.'" method="POST">
			// 						<input type="hidden" name="editID" value='.$viewvals[$i][sizeof($viewvals[$i])-1].' />							
			// 						<button class="btn btn-primary bizbtn" style="font-size;10px;" type="submit" data-toggle="tooltip" data-placement="bottom" title="edit"><i class="fa fa-pencil"></i></button>
			// 					</form>' ; 
		
			// 				$k_table_body .= '<td>'. $x .'</td>';
			// 				// }		
			// 			}else{
			// 				if($columnArray[$j]['Field'] == "View"){	//View 	
			// 					$viewlink = "";
			// 					if(isset($viewcodeextra)){
			// 						for($p = 0; $p < sizeof($viewcodeextra); $p++){
			// 							if($viewcodeextra[$p][0] == -3){
			// 								$viewlink = $viewcodeextra[$p][1];
			// 								break;
			// 							}
			// 						}
			// 					}
			// 					$x = '<form action="' . $viewlink . '?view='.$viewvals[$i][sizeof($viewvals[$i])-1].'" method="POST">
			// 							<input type="hidden" name="viewID" value='.$viewvals[$i][sizeof($viewvals[$i])-1].' />
			// 							<button class="btn btn-primary bizbtn" style="font-size;10px;" type="submit" data-toggle="tooltip" data-placement="bottom" title="view" ><i class="fa fa-eye"></i></button>
			// 						 </form>' ; 
			// 					$k_table_body .= '<td>'. $x .'</td>';		
			// 				}else{
			// 					if($columnArray[$j]['Field'] == "Delete"){	//DELETE 	
			// 						$viewlink = "";
			// 						if(isset($viewcodeextra)){
			// 							for($p = 0; $p < sizeof($viewcodeextra); $p++){
			// 								if($viewcodeextra[$p][0] == -3){
			// 									$viewlink = $viewcodeextra[$p][1];
			// 									break;
			// 								}
			// 							}
			// 						}
			// 						// if(is_array($viewvals[$i])){
			// 						$x = '<form  method="POST">
			// 								<input type="hidden" name="delID" value='.$viewvals[$i][sizeof($viewvals[$i])-1].' />
			// 								<button class="btn btn-danger bizbtn" style="font-size;10px;" type="submit" data-toggle="tooltip" data-placement="bottom" title="view" ><i class="fa fa-trash"></i></button>
			// 							 </form>' ; 
			// 						$k_table_body .= '<td>'. $x .'</td>';
			// 						// }		
			// 					}else{
			// 						if($columnArray[$j]['Field'] == "Action"){	//OTHER BTN 									
			// 							$x = '<form action="' . $viewSettings[7] . '" method="POST">
			// 									<input type="hidden" name="editID" value='.$viewvals[$i][sizeof($viewvals[$i])-1].' />
			// 									<button class="btn btn-primary bizbtn" style="font-size;10px;" type="submit" data-toggle="tooltip" data-placement="bottom" title="" ><i class="fa '.$viewSettings[6].'"></i></button>
			// 								 </form>' ; 
					
			// 							$k_table_body .= '<td>'. $x .'</td>';		
			// 						}else{
			// 							if($columnArray[$j]['Field'] == "Sr. No"){ //Serial Number
			// 								$x = $i + 1 ; 
			// 								$k_table_body .= '<td>'. $x .'</td>';	
			// 							}else{	
											
			// 								if($columnArray[$j]['Type'] == "date" || $columnArray[$j]['Type'] == "timestamp" || $columnArray[$j]['Type'] == "smalldatetime"){ //if DATE
			// 									// echo gettype($result['result_set'][$i][$columnArray[$j]['Field']]);
			// 									$temp = $result['result_set'][$i][$columnArray[$j]['Field']];
			// 									if($temp != NULL){
			// 										if (gettype($temp) == "string"){
			// 											// $temp = $temp->format('Y-m-d');
			// 											$temp =  date("d-m-Y", strtotime($temp));
			// 										}else{
			// 											$temp = $temp->format('d-m-Y');
			// 										}
			// 									}
			// 									// echo $temp;
			// 									$k_table_body .= '<td>'.$temp.'</td>';			
			// 								}else{
			// 									// echo "<br>".$columnArray[$j]['Field'];
			// 									$k_table_body .= '<td>'.$result['result_set'][$i][$columnArray[$j]['Field']].'</td>';			
			// 								}
			// 							}
			// 						}								
			// 					}
								
			// 				}
			// 			}
			// 		}
				
			// 		$k_table_body .= '</tr>';
			// 	}
			// 	* /
			// }else{
			// 	/*for($i=0;$i<sizeof($viewvals); $i++){

			// 		$k_table_body .= '<tr>';

			// 		for($j=0;$j<sizeof($viewcode); $j++){

			// 			//echo '<br>'.$viewcode[$j][0].'=>'.$viewvals[$i][$viewcode[$j][0]];

			// 			if($viewcode[$j][0] >= 0){	

			// 				$temp = $viewvals[$i][$viewcode[$j][0]];

			// 				//echo $viewcode[$j][0];

			// 				if($code[$viewcode[$j][0]][1] == 2){ //if SELECT

			// 					if($temp == 0) $temp = '-';

			// 					else $temp = $radio[$code[$viewcode[$j][0]][3]][$temp-1];
			// 				}

			// 				if($code[$viewcode[$j][0]][1] == 6){ //if DATE
			// 					if($temp != NULL){		
			// 						if (gettype($temp) == "string"){

			// 							$temp =  date("d-m-Y", strtotime($temp));
					
			// 						}else{
			// 							$temp = $temp->format('d-m-Y');
			// 						}
			// 					}
				
			// 					//if (gettype($temp) == "string"){
									
			// 						//$temp =  date("d-m-Y", strtotime($temp));

			// 					//}else{
							
			// 						//$temp = $temp->format('d-m-Y');
			// 					//}
			// 				}

			// 				if($code[$viewcode[$j][0]][1] == 12){ //if MEDIA

			// 					//$temp =  "<img src=";
			// 				}

			// 				$k_table_body .= '<td>'.$temp.'</td>';			
			// 			}

			// 			if($viewcode[$j][0] == -1){ //Serial Number
			// 				$x = $i + 1 ; 
			// 				$k_table_body .= '<td>'. $x .'</td>';		
			// 			}

			// 			if($viewcode[$j][0] == -2){	//EDIT 	
					
			// 				$x = '<form name="form"  action="'.$_SERVER['PHP_SELF'].'?form='.$FormID.'" method="POST">

			// 						<input type="hidden" name="editID" value='.$viewvals[$i][sizeof($viewvals[$i])-1].' />							

			// 						<button class="btn btn-primary bizbtn" style="font-size;10px;" type="submit" data-toggle="tooltip" data-placement="bottom" title="edit" ><i class="fa fa-pencil"></i></button>

			// 					</form>' ; 

			// 				$k_table_body .= '<td>'. $x .'</td>';		
			// 			}

			// 			if($viewcode[$j][0] == -9){	//View 	

			// 				$viewlink = "";

			// 				if(isset($viewcodeextra)){

			// 					for($p = 0; $p < sizeof($viewcodeextra); $p++){

			// 						if($viewcodeextra[$p][0] == -3){

			// 							$viewlink = $viewcodeextra[$p][1];
			// 							break;
			// 						}
			// 					}
			// 				}

			// 				$x = '<form action="' . $viewlink . '?view='.$viewvals[$i][sizeof($viewvals[$i])-1].'" method="POST">

			// 						<input type="hidden" name="viewID" value='.$viewvals[$i][sizeof($viewvals[$i])-1].' />

			// 						<button class="btn btn-primary bizbtn" style="font-size;10px;" type="submit" data-toggle="tooltip" data-placement="bottom" title="view" ><i class="fa fa-eye"></i></button>

			// 					</form>' ; 

			// 				$k_table_body .= '<td>'. $x .'</td>';		
			// 			}

			// 			if($viewcode[$j][0] == -3){	//DELETE 	

			// 				$viewlink = "";

			// 				if(isset($viewcodeextra)){

			// 					for($p = 0; $p < sizeof($viewcodeextra); $p++){

			// 						if($viewcodeextra[$p][0] == -3){

			// 							$viewlink = $viewcodeextra[$p][1];

			// 							break;
			// 						}
			// 					}
			// 				}

			// 				$x = '<form  method="POST">
			// 						<input type="hidden" name="delID" value='.$viewvals[$i][sizeof($viewvals[$i])-1].' />

			// 						<button class="btn btn-danger bizbtn" style="font-size;10px;" type="submit" data-toggle="tooltip" data-placement="bottom" title="view" ><i class="fa fa-trash"></i></button>

			// 					</form>' ; 
			// 				$k_table_body .= '<td>'. $x .'</td>';		
			// 			}

			// 			if($viewcode[$j][0] == -4){	//OTHER BTN 	
				
			// 				$x = '<form action="' . $viewSettings[7] . '" method="POST">

			// 						<input type="hidden" name="editID" value='.$viewvals[$i][sizeof($viewvals[$i])-1].' />

			// 						<button class="btn btn-primary bizbtn" style="font-size;10px;" type="submit" data-toggle="tooltip" data-placement="bottom" title="" ><i class="fa '.$viewSettings[6].'"></i></button>
			// 					</form>' ; 

			// 				$k_table_body .= '<td>'. $x .'</td>';		
			// 			}
			// 		}

			// 		$k_table_body .= '</tr>';
			// 	}* /

			// }
			
		*/
		include "k_files/k_table.php";
		NoTablePrint:
	} else {	
		if(isset($_REQUEST['preview'])){
			$editID = $_REQUEST['preview'];
			$isPreview = 1;
		}

		if(isset($_REQUEST['display'])){
			$editID = $_REQUEST['display'];
			$isView = 1;
			echo "<style>
				.form-control[disabled]{
					border: 0;
					box-shadow: none;
					border-bottom: 1px solid #ccc;
					border-radius: 0;
				}
				.select2-container--default.select2-container--disabled .select2-selection--single {
					cursor: not-allowed;
					border: 0;
					box-shadow: none;
					border-bottom: 1px solid #ccc;
					border-radius: 0;
				  }
			</style>";
		}

		if(isset($_REQUEST['edit'])){
			$editID = $_REQUEST['edit'];
		}
			
		if($editID > 0){ //EDIT MODE

			/*1742025 This section will check session & db companyid, divisionid, yearcode are same then allow to edit */
			
			if($viewSettings[12] != 1)
			{
				$checkDbData = "SELECT * FROM ".$db[0]." WHERE ".$db[1]." =".$editID;
				$resultDbData = db::getInstance()->db_select($checkDbData);
				$caseSensitiveResultDbData = array_change_key_case_recursive($resultDbData);

				// Prepare an array of session-based checks comparing session data with database data
				$checks = [
					'CompanyId' => [$_SESSION['dbCompany'], $caseSensitiveResultDbData['result_set'][0]['companyid']],
					'DivisionId' => [$_SESSION['dbDivision'], $caseSensitiveResultDbData['result_set'][0]['divisionid']],
					'YearCode' => [$_SESSION['dbYear'], $caseSensitiveResultDbData['result_set'][0]['yearcode']],
				];
				// if($FormID == 4921){
				// 	echo $checkDbData;
				// 	print_r($caseSensitiveResultDbData['result_set']);
				// 	print_r($checks);
				// 	exit();
				// }

				// Loop through each check and compare the session value with the corresponding database value
				foreach ($checks as $field => [$expected, $posted]) {
					// If any value does not match, log the error and redirect user back to the form view
					if ($posted != $expected) {
						logDevError($field, $FormID, $_SESSION, "$field mismatch: POST = $expected, Expected = $posted", "Mismatch post & session $field");

						// Set the alert message in the session
						$_SESSION['FormEditValidationMessage'] = "Entry Not Edited. $field mismatch"; // Customize the message

						$fullURL = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]&view=1";

						// Parse the URL components
						$parsedUrl = parse_url($fullURL);

						// Convert query string to an associative array
						parse_str($parsedUrl['query'] ?? '', $queryParams);

						// Remove 'preview' if it exists
						if (isset($queryParams['preview'])) {
							unset($queryParams['preview']);
						}

						// Optional: Add or update 'view=1' if needed
						$queryParams['view'] = 1;

						// Rebuild the query string
						$newQuery = http_build_query($queryParams);

						// Reconstruct the clean URL
						$cleanURL = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . $parsedUrl['path'];
						if (!empty($newQuery)) {
							$cleanURL .= '?' . $newQuery;
						}

						// Redirect user to the referring page, indicating a view mode with the form ID
						echo '<script>window.location="'.$cleanURL.'";</script>';
						exit(); // Stop script execution after redirect
					}
				}
			}

			/* 13042025
			This section will check if the entry is open in edit mode.
			0. Check if its open in Edit mode and not in preview mode
			1. Check if table exist in client db
			2. If not, create table =>> CREATE TABLE [kFormEditLock](
										[ID] [bigint] IDENTITY(1,1),
										[EntryID] [bigint] NULL,
										[FormID] [int] NULL,
										[UserID] [int] NULL,
										[CompanyID] [int] NULL,
										[DivisionID] [int] NULL,
										[YearCode] nvarchar(10) NULL,
										[isDeleted] [bit] NULL,
										[CreatedAt] [datetime] NULL,
										[UpdatedAt] [datetime] NULL
									) ON [PRIMARY]
			3. Check if entry id exists for any other user in the table (isDeleted = 0)
			4. If exists then open the page in Preview mode. $_REQUEST['preview'] = $editID; ALSO SHOW WHICH USER HAD OPENED THE ENTRY 
			5. If not, Insert a new row in the above table with all its params. (isDel = 0 & Created at GETDATE())
			=> This will get removed only in form unload API : isDeleted=1 & UpdatedAT = Getdate()
			=> Check this table for this user when he tries to change the company. Disallow
			=> If user logs out then all the isDeleted for the user should be 1 and updatedAt timestatmp
			*/

			//ENTRY IS OPEN IN EDIT MODE
			if(!isset($_REQUEST['preview'])){
				echo '<script>$(document).ready(function(){
						$(".page-header .breadcrumbs li").html("");
							});</script>';
		
					$checkTableExist = "SELECT * FROM information_schema.columns WHERE table_name='kFormEditLock'";
					$result = db::getInstance()->db_select($checkTableExist);

					if ($result['num_rows'] == 0) {
						$query = "CREATE TABLE [kFormEditLock] (
							[ID] [bigint] IDENTITY(1,1) PRIMARY KEY,
							[EntryID] [bigint] NULL,
							[FormID] [int] NULL,
							[UserID] [int] NULL,
							[CompanyID] [int] NULL,
							[DivisionID] [int] NULL,
							[YearCode] nvarchar(10) NULL,
							[isDeleted] [bit] NULL,
							[CreatedAt] [datetime] NULL,
							[UpdatedAt] [datetime] NULL
						) ON [PRIMARY];";
						
						$createResult = db::getInstance()->db_create_table($query);
					}

					// Common logic after ensuring table exists
					$sql = "SELECT * FROM kFormEditLock 
							WHERE FormID = $FormID AND EntryID = $editID AND CompanyID = {$_SESSION['dbCompany']} 
							AND DivisionID = {$_SESSION['dbDivision']} AND YearCode = '{$_SESSION['dbYear']}' 
							AND UserID <> {$_SESSION['user_id']} AND isDeleted = 0";
					
					$sqlresult = db::getInstance()->db_select($sql);

					if ($sqlresult['num_rows'] > 0) {
						//If user editing this entry then redirect to priview page & show message
						$userQuery = "SELECT * FROM users WHERE ID={$sqlresult['result_set'][0]['UserID']}";
						$userResult = db::getInstanceMaster()->db_select($userQuery);
						
						$fullURL = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]&preview=".$editID;
						echo '<script>
							alert("' . $userResult['result_set'][0]['Username'] . ' is currently editing this entry. You will be able to view in preview mode only.");
							window.location="'.$fullURL.'";</script>';
						exit();
					} else {
						$sql1 = "INSERT INTO kFormEditLock 
							(EntryID, FormID, UserID, CompanyID, DivisionID, YearCode, isDeleted, CreatedAt) 
							VALUES ($editID, $FormID, {$_SESSION['user_id']}, {$_SESSION['dbCompany']}, {$_SESSION['dbDivision']}, '{$_SESSION['dbYear']}', 0, GETDATE())";
						
						$sqlresult1 = db::getInstance()->db_insertQuery($sql1);
					}
			}


			if($FormID == 4921){
				// print_r($_POST);
				if (!empty($_POST)) {
					// Loop through all POST parameters
					foreach ($_POST as $key => $value) {
						if (isset($value)) {
							?>
							<script>
								$(document).ready(function(){
									var selector = <?php echo json_encode($key); ?>;
									var value = <?php echo json_encode($value); ?>;

									if ($(selector).length > 0) {
										console.log("Setting value for: " + selector);
										$(selector).val(value);
									}
								});
							</script>
							<?php	
						}
					}
				}
			}


			//This section will check on before edite mode validation 
			//update by pornima if condition this code only work when page in edit mode
			if(!isset($_REQUEST['preview'])){
				//To check if edit SP exists in the db 
				$editSPName = "sp_ValidateEdit_$FormID";
				$sql = "SELECT COUNT(*) AS found FROM information_schema.routines WHERE routine_schema = 'DBO'  AND  routine_name = '$editSPName'";
				
				$sqlresult = db::getInstance()->db_select($sql);
				$editFlag = 1;

				if($sqlresult['result_set'][0]['found'] == 1){	//if delete SP nt found then delete directly
					
					//CALL Edit SP to check validations before editing the entry
					$session = 'YearCode='.$_SESSION['dbYear'].' AND DivisionId='.$_SESSION['dbDivision'].' AND CompanyId='.$_SESSION['dbCompany'].' AND FormID='.$FormID.'';

					$sp['params'] = ['@EDITID','@TABLENAME','@USERID','@SESSION'];
					$sp['values'] = ["'$editID'","'".$db[0]."'","'".$_SESSION['user_id']."'","'$session'"];
			
					$result = db::getInstance()->db_sp_select($editSPName, $sp['params'], $sp['values']);
	
					$result = array_change_key_case_recursive($result);

					if($result['error'] == 1){
						$ErrorMsg = $result['error_statement'];
					}else{
						// print_r($result['result_set'][0][0]['Success']);
						$row = $result['result_set'][0][0];
						
						if($row['success'] == 1){
							
							if($row['noneditable']){
								$nonEditableFieldArray = explode(",",$row['noneditable']);
								$nonEditableFieldString = '';
								// print_r($nonEditableFieldArray);
								for($i=0; $i<sizeof($nonEditableFieldArray); $i++){
									$sql1 = "SELECT FieldType FROM kmainfields where FormId=$FormID AND DbFieldName='".trim($nonEditableFieldArray[$i])."'";
									
									$sql1result = db::getInstanceMaster()->db_select($sql1);
									if($sql1result['num_rows'] > 0){
										$nonEditableFieldString .= trim($nonEditableFieldArray[$i]).":".trim($sql1result['result_set'][0]['FieldType']);
										if($i != sizeof($nonEditableFieldArray)-1){
											$nonEditableFieldString .= " | ";
										}
									}
								}
								
								$nEditableFields = explode("|",$nonEditableFieldString);
								for($i=0; $i<sizeof($nEditableFields); $i++){
									// echo $nonEditableFieldArray[$i];
									$fieldWithType = explode(":",$nEditableFields[$i]);
									if($fieldWithType[1] == 1 || $fieldWithType[1] == 6 || $fieldWithType[1] == 7 || $fieldWithType[1] == 8 || $fieldWithType[1] == 3 || $fieldWithType[1] == 18){
										echo "<script>$(document).ready(function() { $('#".trim($fieldWithType[0])."').prop('readonly', true).css('background','#eee'); });</script>";	
									}else if($fieldWithType[1] == 5 || $fieldWithType[1] == 19){
										echo "<script>$(window).on('load', function() {
												$('#".trim($fieldWithType[0])."').select2({
													// your Select2 config
												}).on('select2:opening', function (e) {
													e.preventDefault(); // prevents dropdown from opening
												}).prop('readonly', true) // this won't have much effect on select
												.css('pointer-events', 'none');

												setTimeout(
													function() {
														$('#".trim($fieldWithType[0])."').next('span').find('.select2-selection--single').css('background-color','#eee')
													},
												300);

												// $('#".trim($fieldWithType[0])."').select2({
												// 	disabled: true
												// });
												// console.log($('#".trim($fieldWithType[0])."').next('span').find('.select2-selection--single').css('background-color','#eee'));
												// $('#".trim($fieldWithType[0])."').next('span').find('.select2-selection--single').css('background-color','#eee'); 
												// select2Elem.prop('readonly', true).css('pointer-events', 'none').css('background-color', '#eee');
											});
										</script>";
									}else if($fieldWithType[1] == 12 || $fieldWithType[1] == 13){
										echo "<script>$(document).ready(function() { 
												if($('#".trim($fieldWithType[0])."').prop('multiple') == true){
													$('#".trim($fieldWithType[0])."').css('pointer-events','none');
													// console.log($('#'+NonEditableFields[i]).parent());
													$('#".trim($fieldWithType[0])."').nextAll().find('span').remove()
												}
											});</script>";	
									}
								}
							}

							if($result['result_set'][1]){
								?>
								<script>
									$(document).ready(function() {
										var tempData = <?php echo json_encode($result['result_set'][1]); ?>;

										// Empty the target element based on the key
										$("#" + Object.keys(tempData[0])[0]).empty();  // This will empty the element with ID 'Form_40473'

										// Loop through the array and append the content
										for (var i = 0; i < tempData.length; i++) {
											var key = Object.keys(tempData[i])[0];  // Extract the key (e.g., "Form_40473")
											var value = Object.values(tempData[i])[0];  // Get the corresponding value (HTML string)
											// Append the HTML content to the element with the ID that matches the key
											console.log(key);
											console.log(value);
											$("#" + key).append(value);  // Append the HTML content
										}
									});
								</script>
								<?php
							}
						}else if($row['success'] == 0){
							//If Success 0 then don't allow to edit & redirect to listview page
							$fullURL = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]&view=1";
							
							// Set the alert message in the session
							$_SESSION['FormEditValidationMessage'] = $row['msg']; // Customize the message
			
							echo '<script>
								window.location.href ="'.$fullURL.'";
							</script>';
						}
					}
				}
			}

			if(isset($_REQUEST['preview'])){
				
				$editSPName = "sp_ValidatePreview_$FormID";
				
				$sql = "SELECT COUNT(*) AS found FROM information_schema.routines WHERE routine_schema = 'DBO'  AND  routine_name = '$editSPName'";
				
				$sqlresult = db::getInstance()->db_select($sql);

				if($sqlresult['result_set'][0]['found'] == 1){	//if preview SP found
					//CALL preview SP to check validations before editing the entry
					$session = 'YearCode='.$_SESSION['dbYear'].' AND DivisionId='.$_SESSION['dbDivision'].' AND CompanyId='.$_SESSION['dbCompany'].' AND FormID='.$FormID.'';

					$sp['params'] = ['@EDITID','@TABLENAME','@USERID','@SESSION'];
					$sp['values'] = ["'$editID'","'".$db[0]."'","'".$_SESSION['user_id']."'","'$session'"];
			
					$result = db::getInstance()->db_sp_select($editSPName, $sp['params'], $sp['values']);
	
					$result = array_change_key_case_recursive($result);

					// if($FormID == 5189){
					// 	print_r($result);
					// }

					if($result['error'] == 1){
						$ErrorMsg = $result['error_statement'];
					}else{
						// print_r($result['result_set'][0][0]['Success']);
						$row = $result['result_set'][0][0];
						
						if($row['success'] == 1){

							if($result['result_set'][1]){
								?>
								<script>
									$(document).ready(function() {
										var tempData = <?php echo json_encode($result['result_set'][1]); ?>;

										// Empty the target element based on the key
										$("#" + Object.keys(tempData[0])[0]).empty();  // This will empty the element with ID 'Form_40473'

										// Loop through the array and append the content
										for (var i = 0; i < tempData.length; i++) {
											var key = Object.keys(tempData[i])[0];  // Extract the key (e.g., "Form_40473")
											var value = Object.values(tempData[i])[0];  // Get the corresponding value (HTML string)
											// Append the HTML content to the element with the ID that matches the key
											$("#" + key).append(value);  // Append the HTML content
										}
									});
								</script>
								<?php
							}
						}
					}
				}
			}

			$sql = "SELECT * FROM ".$db[0]." WHERE ".$db[1]." =".$editID;
			$result = db::getInstance()->db_select($sql);
			if($k_debug)	echo '<br /><br />'.$sql . '<br /><br />';
			if($k_debug)	echo '<br /><br />' . print_r($result) . '<br /><br />';
			for($i = 0; $i < $result['num_rows']; $i++){ //WHILE LOOP FOR $row
				
				for($j = 0; $j < sizeof($code); $j++){
					
					if($code[$j][1] == 10){  //FOR MAPPING TABLE Multi-Select
						$temp = array();
						//SELECT * FROM map_vendor_categories LEFT JOIN categories ON map_vendor_categories.cat_id = categories.cat_id WHERE vendor_id = 1
						
						$mapArray  = $extradb[((-1)*$code[$j][3])-1][5];
						
						$joinTable = $extradb[((-1)*$code[$j][3])-1][1];
						
						$joinIndex = $extradb[((-1)*$code[$j][3])-1][2];
						
						$mapTable = $mapArray[0];
						
						$mapIndex = $mapArray[1];
						
						$mapVariant = $mapArray[2];
						
						$sql = 'SELECT * FROM ' . $mapTable  . ' WHERE ' . $mapIndex . ' = ' . $editID ;///. ' LEFT JOIN ' . $joinTable . ' ON ' . $mapTable.'.'.$mapVariant . ' = ' . $joinTable.'.'.$joinIndex . ' WHERE ' . $mapIndex . ' = ' . $editID ;
						
						$rs = db::getInstance()->db_select($sql);
						if($k_debug)	echo '<br /><br />' . $sql . '<br />' . print_r($rs) . '<br /><br />';
						//	print_r($rs['result_set']);
						
						$code[$j][4] = $rs['result_set'];
					}else{
						if($code[$j][1] == 11 || $code[$j][1] == 9){  //FOR MAPPING TABLE Multi-Select

							$temp = array();

							$mapArray  = $extradb[((-1)*$code[$j][3])-1];

							$joinTable = $extradb[((-1)*$code[$j][3])-1][1];

							$joinIndex = $extradb[((-1)*$code[$j][3])-1][2];

							$mapTable = $mapArray[6];

							$mapIndex = $mapArray[7];

							$mapVariant = $mapArray[8];

							$sql = 'SELECT * FROM ' . $mapTable  . ' WHERE ' . $mapIndex . ' = ' . $editID ;

							///. ' LEFT JOIN ' . $joinTable . ' ON ' . $mapTable.'.'.$mapVariant . ' = ' . $joinTable.'.'.$joinIndex . ' WHERE ' . $mapIndex . ' = ' . $editID ;

							$rs = db::getInstance()->db_select($sql);
							if($k_debug)	echo '<br /><br />' . $sql . '<br />' . print_r($rs) . '<br /><br />';
							$code[$j][4] = $rs['result_set'];

						}else{
							if($code[$j][1] == 13){  //FOR MAPPING TABLE Multi-Select

							    //echo "<br />" . $code[$j][1];
							    //print_r($code[$j]);
								//echo "<br />";
								//print_r($extradb[((-1)*$code[$j][3])-1]);
          
					        	$temp = array();

								$mapArray  = $extradb[((-1)*$code[$j][3])-1];

								$joinTable = "kmainmedia";

								$joinIndex = "MediaId";

								$mapTable = $mapArray[6];

								$mapIndex = $mapArray[7];

								$mapVariant = $mapArray[8];

								$sql = 'SELECT kmainmedia.MediaId, kmainmedia.MediaName, kmainmedia.MediaType, kmainmedia.MediaFolder FROM ' . $mapTable . ' LEFT JOIN ' . $joinTable . ' ON ' . $mapTable.'.' .$mapVariant . ' = ' . $joinTable . '.' . $joinIndex . ' WHERE ' . $mapIndex . ' = ' . $editID ;

								$rs = db::getInstance()->db_select($sql);
								if($k_debug)	echo '<br /><br />' . $sql . '<br />' . print_r($rs) . '<br /><br />';
								//print_r($rs['result_set']);

								?>
								<style>
									.button-container {
									position: relative;
									display: inline-block;
									}

									.button-link {
									padding: 3px 10px;
									background-color: #007bff;
									color: white;
									text-decoration: none;
									border-radius: 4px;
									font-family: sans-serif;
									display: inline-block;
									position: relative;
									margin-right:6px;
									}

									.close-button {
									position: absolute;
									top: -6px;
									right: -6px;
									background-color: #d8d8d8;
									color: white;
									width: 20px;
									height: 20px;
									text-align: center;
									line-height: 20px;
									border-radius: 50%;
									cursor: pointer;
									font-size: 12px;
									}
								</style>

								<?php

								$images = "";

								for($z = 0; $z < sizeof($rs['result_set']); $z++){
									
									if(strlen($rs['result_set'][$z]['MediaName']) > 4){
										if(str_contains($rs['result_set'][$z]['MediaName'], ".png") || str_contains($rs['result_set'][$z]['MediaName'], ".jpeg")
											|| str_contains($rs['result_set'][$z]['MediaName'], ".jpg")){
											$images .= "
												<div id='".$code[$j][0]."".$rs['result_set'][$z]['MediaId']."' style='display: inline'>
													<a href='".SITE_ROOT.$rs['result_set'][$z]['MediaFolder']."/".$rs['result_set'][$z]['MediaName']."' target='_blank'><img src='".SITE_ROOT.$rs['result_set'][$z]['MediaFolder']."/".$rs['result_set'][$z]['MediaName']."' width='100' height='auto' style='/max-height:100px;/ margin-right:10px;border: 1px solid #d7d7d7a6;margin-top: 7px;'/></a>
													<span onclick='deleteImage(13,\"".$code[$j][0]."\",".$rs['result_set'][$z]['MediaId'].");' style='background: #d8d8d8;position: absolute;margin-left: -23px;margin-top: -3px;color: white;width: 21px;text-align: center;border-radius: 21px;cursor: pointer;'>X</span>
												</div>";
										}else{
											
											// $images .= "
											// 	<div id='".$code[$j][0]."".$rs['result_set'][$z]['MediaId']."' style='display: inline'>
											// 		<a href='".SITE_ROOT.$rs['result_set'][$z]['MediaFolder']."/".$rs['result_set'][$z]['MediaName']."' target='_blank'>VIEW</a>
											// 		<span onclick='deleteImage(13,\"".$code[$j][0]."\",".$rs['result_set'][$z]['MediaId'].");' style='background: #d8d8d8;position: absolute;margin-left: 0px;margin-top: -8px;color: white;width: 21px;text-align: center;border-radius: 21px;cursor: pointer;'>X</span>
											// 	</div>";

											$images .= "
											<div id='".$code[$j][0]."".$rs['result_set'][$z]['MediaId']."' class='button-container'>
												<a href='".SITE_ROOT.$rs['result_set'][$z]['MediaFolder']."/".$rs['result_set'][$z]['MediaName']."' target='_blank' class='button-link'>VIEW</a>
												<span class='close-button' onclick='deleteImage(13,\"".$code[$j][0]."\",".$rs['result_set'][$z]['MediaId'].");'>X</span>
											</div>";
										}
									}
								}
								// if($z > 0){
								    // $code[$j][4] = "<div class='' id='".$arr[0]."Img'>" . $images . "</div>";
								// }else{
								    $code[$j][4] = "$images";
								// }					

							}else{
								if($code[$j][1] == 12){

									//echo "<br />" . $code[$j][1];
									//print_r($code[$j]);
									//echo "<br />";
									//print_r($extradb[((-1)*$code[$j][3])-1]);
					                $temp = array();
									$mapArray  = $extradb[((-1)*$code[$j][3])-1];
									$joinTable = $extradb[((-1)*$code[$j][3])-1][1];
									$joinIndex = $extradb[((-1)*$code[$j][3])-1][2];
									$sql = 'SELECT kmainmedia.MediaId, kmainmedia.MediaName, kmainmedia.MediaType, kmainmedia.MediaFolder FROM kmainmedia 

									LEFT JOIN ' . $db[0] . ' ON ' . $db[0] . '.' . $code[$j][0].' = kmainmedia.MediaID'.' WHERE ' . $db[0] . '.' . $db[1] . ' = ' . $editID ;

									$rs = db::getInstance()->db_select($sql);

									$images = ""; //print_r($rs);

									for($z = 0; $z < sizeof($rs['result_set']); $z++){
										if(strlen($rs['result_set'][$z]['MediaName']) > 4){
											if(str_contains($rs['result_set'][$z]['MediaName'], ".png") || str_contains($rs['result_set'][$z]['MediaName'], ".jpeg")
												|| str_contains($rs['result_set'][$z]['MediaName'], ".jpg")){
											
												$images .= "
													<div id='".$code[$j][0]."".$rs['result_set'][$z]['MediaId']."' style='display: inline'>
														<a href='".SITE_ROOT.$rs['result_set'][$z]['MediaFolder']."/".$rs['result_set'][$z]['MediaName']."' target='_blank'><img src='".SITE_ROOT.$rs['result_set'][$z]['MediaFolder']."/".$rs['result_set'][$z]['MediaName']."' width='100' height='auto' style='/max-height:100px;/ margin-right:10px;border: 1px solid #d7d7d7a6;margin-top: 7px;'/></a>
														<span onclick='deleteImage(12,\"".$code[$j][0]."\",".$rs['result_set'][$z]['MediaId'].");' style='background: #d8d8d8;position: absolute;margin-left: -5px;margin-top: -3px;color: white;width: 21px;text-align: center;border-radius: 21px;cursor: pointer;'>X</span>
													</div>";
											}else{
										
												$images .= "
													<div id='".$code[$j][0]."".$rs['result_set'][$z]['MediaId']."' style='display: inline'>
														<a href='".SITE_ROOT.$rs['result_set'][$z]['MediaFolder']."/".$rs['result_set'][$z]['MediaName']."' target='_blank'>VIEW</a>
														<span onclick='deleteImage(12,\"".$code[$j][0]."\",".$rs['result_set'][$z]['MediaId'].");' style='background: #d8d8d8;position: absolute;margin-left: 0px;margin-top: -8px;color: white;width: 21px;text-align: center;border-radius: 21px;cursor: pointer;'>X</span>
													</div>";
											}
										}
									}
									$code[$j][4] = $images;
								}else{
									if($code[$j][1] == 14){
										//Ignore GRIDS
									}else{
										if($code[$j][1] == 16){  //FOR MAPPING TABLE Multi-Select
											$temp = array();				
											$mapArray  = $extradb[((-1)*$code[$j][3])-1];
											$joinTable = $extradb[((-1)*$code[$j][3])-1][1];
											$joinIndex = $extradb[((-1)*$code[$j][3])-1][2];				
											$mapTable = $mapArray[6];				
											$mapIndex = $mapArray[7];				
											$mapVariant = $mapArray[8];
											$sql = 'SELECT * FROM ' . $mapTable ;
											$sql .= ' LEFT JOIN ' . $joinTable . ' ON ' . $mapTable.'.'.$mapVariant . ' = ' . $joinTable.'.'.$joinIndex . ' WHERE ' . $mapTable .'.'. $mapIndex . ' = ' . $editID ;
											
											$rs = db::getInstance()->db_select($sql);				
											if($k_debug)	echo '<br /><br />' . $sql . '<br />' . print_r($rs) . '<br /><br />';
											$code[$j][4] = $rs['result_set'];
										}else{
											//  echo $code[$j][1] . "<br />";
											// print_r($result['result_set'][$i]['Username']);
											// echo $result['result_set'][$i]['CarryOn'];
											
											//tmp array to convert the array to case insesitive:
											$result_case_insensitive = $result['result_set'][$i];

											$result_case_insensitive = array_change_key_case($result_case_insensitive);

											// if($FormID == 4568 && $code[$j][1] == 1) echo "<br /><br />".print_r($result_case_insensitive);
											
											// print_r($result_case_insensitive);
											// if($FormID == 4568 && $code[$j][1] == 1) echo "<br /><br />".$code[$j][0];
											// if($FormID == 4568 && $code[$j][1] == 1) echo "<br /><br />".print_r($result_case_insensitive);
											$code[$j][4] = $result_case_insensitive[strtolower($code[$j][0])];
											// $code[$j][4] = $result['result_set'][$i][$code[$j][0]];
										}
									}
								}
							}
						}
					}
				}
			} 


			?>



			<?php
			

			$buttonname = "UPDATE";
			if(isset($isPreview) == 1){
				$k_table_title = "Preview";
				$k_table_log_button = '<button class="btn btn-warning btn-xs" onclick="openLogModal()">Logs</button>';
			}else{
				$k_table_title = "Edit";
				$k_table_log_button = '<button class="btn btn-warning btn-xs" onclick="openLogModal()">Logs</button>';
			}

			if(isset($isView) == 1){
				$k_table_title = "View";
			}
			$buttonsaveaddmore = "UpdateAddMore";
			
			echo '<script>
				$(window).ready(function() { 
					$("form.form-bordered").on("keypress", function (event) { 
						//console.log("aaya"); 
						var keyPressed = event.keyCode || event.which; 
						if (keyPressed === 13) { 
							//alert("You pressed the Enter key!!"); 
							event.preventDefault(); 
							return false; 
						} 
					}); 
				}); </script>
			';
			
			//	print_r($result);
			//SCRIPT FOR DELETING IMAGE
			?>
			<script>
			    // function deleteImage(fieldType, fieldName, mediaID){
				// 	alert('delete image');
			    //     var fid = $("#FormID").val();

			    //     $.ajax({

                //         url: "form-api.php", 
                //         type: 'POST',
                //         data: {apicase:'1', fid: fid, fieldType:fieldType, fieldName:fieldName, mediaID:mediaID},
                //         dataType: 'json',
                //         success: function(result){
                //             console.log(result);
                //             if(result.error){

                //                 alert(result.error_msg);
                //                 //console.log(result.error_msg);
                //             }else{

				// 				alert(result.data);

                //                 if(mediaID === undefined){

                //                     $('#' + fieldName).html("Image deleted.");

                //                     setTimeout(
                //                         function() {
                //                             $('#' + fieldName).html("");
                //                         },
                //                         2000);
                //                 }else{
                //                     $('#' + fieldName + mediaID).html("Image deleted.");
                //                     setTimeout(

				// 						function() {

                //                             $('#' + fieldName + mediaID).html("");
                //                         },
                //                         2000);
                //                 }
                //             }
                //         },
				// 		error: function(jqXHR, textStatus, errorThrown) {
				// 			console.log("Error 261: ", textStatus);
				// 			// console.log("Got ERROR Data frm API", jqXHR);
				// 			// callback(new Error(textStatus || errorThrown));
				// 		}
                //     });
			    // }

				function deleteImage(fieldType, fieldName, mediaID){
					alert('delete image');
			        var fid = $("#FormID").val();

			        $.ajax({

                        url: "deleteImage.php", 
                        type: 'POST',
                        data: { fid: fid, fieldType:fieldType, fieldName:fieldName, mediaID:mediaID},
                        dataType: 'json',
                        success: function(result){
                            console.log(result);
                            if(result.error){

                                alert(result.error_msg);
                                //console.log(result.error_msg);
                            }else{

								alert(result.data);
								console.log("mediaID",mediaID);
                                if(mediaID === undefined){
									console.log("if");
                                    $('#' + fieldName).html("Image deleted.");

                                    setTimeout(
                                        function() {
                                            $('#' + fieldName).html("");
                                        },
                                        2000);
                                }else{
									console.log("else");
                                    $('#' + fieldName + mediaID).html("Image deleted.");
                                    setTimeout(

										function() {

                                            $('#' + fieldName + mediaID).html("");
                                        },
                                        2000);
                                }
                            }
                        },
						error: function(jqXHR, textStatus, errorThrown) {
							console.log("Error 261: ", textStatus);
							// console.log("Got ERROR Data frm API", jqXHR);
							// callback(new Error(textStatus || errorThrown));
						}
                    });
			    }
			</script>

			<?php
			
		} else{ 	//ADD MODE
			if($k_debug){ echo "<br /><br />MITESH DEBUG->-><pre>"; print_r($moreextradb);echo "</pre>";}
			//Sp call for delete temporary table data
			$SPName = 'sp_ValidateAdd_'.$FormID;
			$sql = "SELECT COUNT(*) AS found FROM information_schema.routines WHERE routine_schema = 'DBO'  AND  routine_name = '$SPName'";
			$sqlresult = db::getInstance()->db_select($sql);
			
			if($sqlresult['result_set'][0]['found'] == 1){
				$session ='YearCode= '.$_SESSION['dbYear'].' AND DivisionId='.$_SESSION['dbDivision'].' AND CompanyId='.$_SESSION['dbCompany'].' AND FormID='.$FormID.' ';
				
				$data['params'] = ['@USERID','@TABLENAME','@FORMID','@session'];
				$sp['values'] = [$_SESSION['user_id'],$db[0],$FormID,"'$session'"];
				$result = db::getInstance()->db_sp_select($SPName, $data['params'], $sp['values']);
				// print_r($result);
				// exit();
			}

			//Carry on logic
			$url = explode("&",$_SERVER['REQUEST_URI']);
			$carryOn = [];
			if(sizeof($url) > 1){
				array_shift($url);
				for($i=0; $i<sizeof($url); $i++){
					$urlArray = explode("=",$url[$i]);
					$ParamField = $urlArray[0];
					$ParamValue = $urlArray[1];
					
					// ${$ParamField} = isset($_REQUEST[$ParamField]) ? $_REQUEST[$ParamField] : "";
					
					if(isset($_REQUEST[$ParamField])){
						$carryOn += array('FormId'=>$FormID,$ParamField=>$_REQUEST[$ParamField]);
						$_SESSION['carryOn'] = $carryOn;
					}
				}
			}

			$k_table_title = "Add New";

			$buttonname = "SAVE";
			$buttonsaveaddmore = "SaveAddMore";

			echo '<script>
				$(window).ready(function() { 
					$("form.form-bordered").on("keypress", function (event) { 
						//console.log("aaya"); 
						var keyPressed = event.keyCode || event.which; 
						if (keyPressed === 13) { 
							//alert("You pressed the Enter key!!"); 
							event.preventDefault(); 
							return false; 
						} 
					}); 
				}); </script>
			';

			if($FormID == 4921){
				if (!empty($_POST)) {
					// print_r($_POST);
					// Loop through all POST parameters
					foreach ($_POST as $key => $value) {
						if (isset($value)) {
							?>
							<script>
								$(document).ready(function(){
									$(<?php echo $key; ?>).val(<?php echo "'".$value."'"; ?>);
								});
							</script>
							<?php	
						}
					}

					?>
						<script>
							// window.onload = function () {
							// 	var $link = $('#prevBackButton');
							// 	var baseUrl = $link.attr('href');
							// 	console.log("baseurl"+baseUrl);
							// 	// Add your desired string (e.g., query parameter)
							// 	var newUrl = baseUrl + "?form=<?php echo urlencode($_POST['prevFormid']); ?>&preview=<?php echo urlencode($_POST['entryid']); ?>";
								
							// 	// Update the href attribute
							// 	$link.attr('href', newUrl);
							// }
						</script>
					<?php
				}
			}
		}

		?>

        <style type="text/css">
			
			.toggle-content{
                border: 1px solid #d0d0d0;
                overflow: hidden;
                margin-bottom: 10px;
                padding: 10px;
                background: #f5f5f58c;
            }
			
            .toggle label{
                color: inherit !important;
                /*display: inline-block;*/
                max-width: 100%;
                margin-bottom: 5px;
            }
            .cke_textarea_inline{

               border: 1px solid black;
            }

            input[type="file"] {
                display: none;
            }

            .custom-file-upload {

                border: 1px solid #ccc;
                display: inline-block;
                padding: 6px 12px;
                cursor: pointer;
                width: 100%;
                margin-top: 27px;
            }

			
			td.select-checkbox.checkbox_align {
				padding-left: 15px !important;
				text-align: left;
			}
			table.dataTable > tbody > tr > td.select-checkbox:before{
				left: 0;
			}
			table.dataTable tbody th, table.dataTable tbody td {
				vertical-align: middle;
			}
        </style>
    
		<script src='assets/ckeditor/ckeditor.js'></script>

		<section class="panel forminit">

			<header class="panel-heading"><meta http-equiv="Content-Type" content="text/html; charset=utf-8">

			<div style="display: flex; justify-content: space-between;">
					<!-- Left Section: Title -->
					<h2 class="panel-title" style="margin: 0;"><?php echo isset($k_table_title) ? $k_table_title : ""; ?></h2>

					<!-- Right Section: Buttons -->
					<?php
						if (isset($_REQUEST['preview'])) {
							// In preview mode Prev & Next button code
							$sql = "SELECT (SELECT COUNT(" . $db[1] . ") FROM " . $db[0] . " WHERE YearCode = ".$_SESSION['dbYear'].") AS TotalRecords, (SELECT TOP 1 " . $db[1] . " FROM " . $db[0] . " WHERE YearCode = ".$_SESSION['dbYear']." ) AS FirstRecordID";
							$result = db::getInstance()->db_select($sql);

							$startID = $result['result_set'][0]['FirstRecordID'];
							$totalRecords = $result['result_set'][0]['TotalRecords'];

							// Function to check if a record exists in the database and return the next valid ID
							function getNextValidRecord($id, $db, $direction = 1) {
								// Check the next or previous valid record based on direction (1 for next, -1 for previous)
								$originalID = $id;
								while (true) {
									$query = "SELECT * FROM " . $db[0] . " WHERE " . $db[1] . " = " . $id ." AND YearCode = ".$_SESSION['dbYear'];
									$result1 = db::getInstance()->db_select($query);
									if ($result1['num_rows'] > 0) {
										return $id; // Record exists
									}
									$id += $direction; // Increment or decrement the ID based on direction
									if (($direction == 1 && $id > $originalID + 5) || ($direction == -1 && $id < $originalID - 5)) {
										// Stop after a reasonable limit to avoid endless loop (based on direction)
										break;
									}
								}
								return null; // No valid record found
							}

							// Determine the current edit ID
							$editID = isset($_REQUEST['preview']) ? $_REQUEST['preview'] : $startID;

							// Calculate the previous and next record IDs
							$preEditId = $editID - 1;
							$nextEditId = $editID + 1;

							// Get the next valid record for previous and next IDs
							$preExists = getNextValidRecord($preEditId, $db, -1); // For previous, use -1 as direction
							$nextExists = getNextValidRecord($nextEditId, $db, 1);  // For next, use 1 as direction

							// Dynamically check if we are at the first or last record
							$isFirst = ($editID == $startID);
							$isLast = ($nextExists === null); // If no next valid record, we are on the last record

						?>
						<div>
							<!-- Previous Button -->
							<?php if (!$isFirst && $preExists) { ?>
								<a href="<?php echo $_SERVER['PHP_SELF'] . "?form=$FormID&preview=$preExists"; ?>" class="btn btn-warning btn-xs" id="prev-btn" onclick="navigate(-1)">Previous</a>
							<?php } else { ?>
								<span class="btn btn-warning btn-xs disabled" id="prev-btn">Previous</span>
							<?php } ?>

							<!-- Next Button -->
							<?php if (!$isLast && $nextExists) { ?>
								<a href="<?php echo $_SERVER['PHP_SELF'] . "?form=$FormID&preview=$nextExists"; ?>" class="btn btn-warning btn-xs" id="next-btn" onclick="navigate(1)">Next</a>
							<?php } else { ?>
								<span class="btn btn-warning btn-xs disabled" id="next-btn">Next</span>
							<?php } ?>
						</div>
						<?php } ?>

					

					<?php 
						if(isset($k_table_log_button)){
							// Button to trigger modal for log
							echo $k_table_log_button;
						} 
					?>
					
				</div>

				<p class="panel-subtitle"><?php echo isset($k_table_subtitle) ? $k_table_subtitle : ""; ?></p>
			</header>

				<div class="panel-body">

					<form autocomplete="off" name="form" id="form" class="form-bordered"  method="post" action="db.php" enctype="multipart/form-data"> <?php
					//onsubmit="validateFormOnSave(event);"
					echo '<input type="hidden" name="FormID" id="FormID" value='.$FormID.' />';
	}

?>
<!-- https://github.com/linways/table-to-excel -->
<script src="https://cdn.jsdelivr.net/gh/linways/table-to-excel@v1.0.4/dist/tableToExcel.js"></script>
<!-- USED FOR EXPORTING SP DATA TO EXCEL WITH PROTECTION -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
<div class="modal fade" id="ImportExcelModal">
	<div class="modal-dialog modal-dialog-centered modal-lg" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<!-- <button type="button" class="close" data-dismiss="modal">×</button>  -->
				<h4 class="modal-title">Imported Data</h4>                                                         
				<h6 class="modal-subtitle">Please check the result columns for error and re-upload the excel.</h6>
			</div> 
			<div class="modal-body" >
				<!-- <form id="modalForm" name="modal" role="form"> -->
					<div id="ModelContent" >

					</div>
					<div class="modal-footer" style="margin-top:10px;">					
						<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
						<!-- <button type="button" class="btn btn-success" id="submit">Submit</button> -->
					</div>
				<!-- </form> -->
			</div>   
		</div>                                                                       
	</div>                                          
</div>
<script>
	// function ImportAddResultInGrid(ddRowSelectType,selectedItemId,DBFieldType,DBFieldName,isGrid,requestParams,formReeponseString){
	function ImportGridExcelValidateSP(gridData, GridID, EditID){
		console.log("gridData",gridData);
		var excelData = gridData;
		var allreadyValidate = false;
		$.ajax({
			type : 'POST',
			dataType: 'json',
			data : {gridData : gridData, GridID : GridID, FormID: <?php echo $FormID; ?>, EditID: EditID},
			url : 'getGridExcelValidateData.php',
			success : function(result){
				console.log("result from Validate Import");
				console.log(result);
				var gridIdentifier = $('#grid-id-'+GridID).attr('class');
				var gridSerialNo = gridIdentifier.slice(4);

				if(result['output'] == null || result['output'] == undefined){
					alert("Error in Import: Output not in correct Format. Please contact developer.");
				}
				var Output = result['output'][0];
				var GridData = result['gridData'];	//validated data to be entered in the grid
				var GridFieldData = result['data'];	//datastructure

				if(Output['Success'] == 1){
					$('#cnt'+gridSerialNo).val(0);
					$('.tmp_GridTable'+gridSerialNo+' tbody tr').not(':first').remove();
					// console.log("excelData", excelData);
					
					for(var l=0; l<GridData.length; l++){
						// var tmp = `<tr><td>${l + 1}</td>`;
						// var head = `<tr><th>SrNo</th>`;			
						for(var k=0; k<GridFieldData.length; k++){
							// tmp += `<td>${GridData[l][GridFieldData[k]['DbFieldName']]}</td>`;
							// head += `<th>${GridFieldData[k]['DisplayName']}</th>`;
							var gFieldTyp = GridFieldData[k]['FieldType'];
							var gGridData = GridData[l][GridFieldData[k]['DbFieldName']];
							var gDispName = GridFieldData[k]['DisplayName'].trim();
							var gDispData = excelData[l][gDispName.replace(/^\n|\n$/g, '')];
							var gFieldNam = GridFieldData[k]['DbFieldName'];

							console.log(gFieldTyp + " ==>> " + gGridData + " <<==>> " + gFieldNam + " -> " + gDispData + ' => #kreon-grid-' + gridSerialNo + gFieldNam);
							if(gFieldTyp == '1' || gFieldTyp == '3' || gFieldTyp == '7' || gFieldTyp == '8'){
								$('#kreon-grid-'+gridSerialNo+''+ gFieldNam).val(gGridData);
							}else if(gFieldTyp == '5' || gFieldTyp == '19'){
								// var newOption = new Option(gDispData, gGridData, false, false); 
								// $('#kreon-grid-'+gridSerialNo+'' + gFieldNam).append(newOption) //.trigger('change');
								// $('#kreon-grid-'+gridSerialNo+'' + gFieldNam).append('<option selected="selected" value="'+gGridData+'">'+gDispData+'</option>').trigger('change');
								// alert(gGridData)
								// $('#kreon-grid-'+gridSerialNo+'' + gFieldNam).val(gGridData).trigger('change'); 

								// var newOption = new Option(gDispData, gGridData, true, true); 
								// $('#mySelect').append(newOption).trigger('change');

								var $newOption = $("<option selected=\"selected\"></option>").val(gGridData).text(gDispData)
								$('#kreon-grid-'+gridSerialNo+'' + gFieldNam).append($newOption).trigger("change");
							}else if(gFieldTyp == '17'){
								$('#kreon-grid-'+gridSerialNo+gFieldNam+'-text').val(gGridData);
								$('#kreon-grid-'+gridSerialNo+gFieldNam).val(gGridData);
							}else if(gFieldTyp == '6'){
								console.log(gGridData);
								var dateObj = gGridData;
								var date = new Date(dateObj);
								var inputDateValue = date.toISOString().split('T')[0]; // will still be "2025-04-15"
								$('#kreon-grid-'+gridSerialNo+gFieldNam).val(inputDateValue);
							}
						}
						//Add row
						ImportAddNewRow($(this),result['data'],gridSerialNo,0,allreadyValidate=true);	
						// break;	
					}
					alert("Import successful");
					
				}else{
					if(+Output['Success'] == 3){
						alert("Data saved succesfully.")
					}else{
						alert("Error in input data: " + Output['Msg']);

						$("#ImportExcelModal").modal({backdrop: 'static', keyboard: false},'show');
						$("#ImportExcelModal #ModelContent").append('<table class="table table-striped table-bordered table-hover table-condensed" id="ImportExcelTable"><thead></thead><tbody></tbody></table>');

						var GridData = result['gridData'];
						var GridFieldData = result['data'];

						for(var l=0; l<GridData.length; l++){
							var tmp = `<tr><td>${l + 1}</td>`;
							var head = `<tr><th>SrNo</th>`;			
							for(var k=0; k<GridFieldData.length; k++){
								tmp += `<td>${GridData[l][GridFieldData[k]['DbFieldName']]}</td>`;
								head += `<th>${GridFieldData[k]['DisplayName']}</th>`;
								// if(GridFieldData[k]['FieldType'] == '1' || GridFieldData[k]['FieldType'] == '3' || GridFieldData[k]['FieldType'] == '6' || GridFieldData[k]['FieldType'] == '7' || GridFieldData[k]['FieldType'] == '8'){
								// 	console.log(GridFieldData[k]['FieldType'] +"  == "+GridData[l][GridFieldData[k]['DbFieldName']]);
								// 	$('#kreon-grid-'+gridSerialNo+''+GridFieldData[k]['DbFieldName']).val(GridData[l][GridFieldData[k]['DbFieldName']]);
								// }else if(GridFieldData[k]['FieldType'] == '5' || GridFieldData[k]['FieldType'] == '19'){
								// 	var $newOption = $("<option selected=\"selected\"></option>").val(GridData[l][GridFieldData[k]['DbFieldName']]).text(GridData[l][GridFieldData[k]['DbFieldName']])
								// 	$('#kreon-grid-'+gridSerialNo+''+GridFieldData[k]['DbFieldName']).append($newOption).trigger("change");
								// }else if(GridFieldData[k]['FieldType'] == '17'){
								// 	$('#kreon-grid-'+gridSerialNo+'SingleDDModal'+GridFieldData[k]['DbFieldName']).val(GridData[l][GridFieldData[k]['DbFieldName']]);
								// 	$('#kreon-grid-'+gridSerialNo+'SingleDDModal-hidden-'+GridFieldData[k]['DbFieldName']).val(GridData[l][GridFieldData[k]['DbFieldName']]);
								// }
							}
							if(l == 0){
								head +=`<th>Result</th></tr>`;
								$("#ImportExcelModal #ModelContent #ImportExcelTable thead").append(head);
							}
							if(GridData[l]['ImportValidationResult'] == null || GridData[l]['ImportValidationResult'] == 'null' || GridData[l]['ImportValidationResult'] == '')
								tmp += `<td>-</td></tr>`;
							else
								tmp += `<td style="background: red;color: white;">${GridData[l]['ImportValidationResult']}</td></tr>`;
							$("#ImportExcelModal #ModelContent #ImportExcelTable tbody").append(tmp);
							//Add row
							// ImportAddNewRow($(this),result['data'],gridSerialNo,0);		
						}
					}
				}
			}
		});
	}

	function ImportAddNewRow(onCLickVariable,mArray,gridSerialNo,selectedRowId,allreadyValidate){
		console.log("Adding Row", mArray)
		var fieldArray = [];
		var mfld = "";
		var headerGridStart = '<tr class="grp'+gridSerialNo+'" data-rowid="'+selectedRowId+'" id="'+gridSerialNo;
		var headerGridEnd   = '">';
		var cuurrentRowData = "";
		var cuurrentRowDataWithComma = "";
		
		for(var t=0; t < mArray.length; t++){
			if(mArray[t]['FieldType'] == 17){
				// var fldvalue = onCLickVariable.closest("tr").find('#kreon-grid-'+gridSerialNo+'SingleDDModal'+mArray[t]['DbFieldName']).val();
				var fldvalue = $('#kreon-grid-'+gridSerialNo+mArray[t]['DbFieldName']+'-text').val();
				// var fldvalue1 = onCLickVariable.closest("tr").find('#kreon-grid-'+gridSerialNo+'SingleDDModal-hidden-'+mArray[t]['DbFieldName']).val();
				var fldvalue1 = $('#kreon-grid-'+gridSerialNo+mArray[t]['DbFieldName']).val();
			}else{
				var fldvalue = $('#kreon-grid-'+gridSerialNo+''+mArray[t]['DbFieldName']).val();
			}
			// console.log(fldvalue);
			fieldArray.push([mArray[t]['FieldType'],'kreon-grid-'+gridSerialNo+''+mArray[t]['DbFieldName']]); //,mArray[t]['CarryOn']
			
			//Required validation not using becuse it validate in Add Result In Grid function
			// if(mArray[t]['Required'] == 1 && fldvalue == "" || fldvalue == "0"){
			// 	alert(mArray[t]['DbFieldName']+" field is required");
			// 	if(ddRowSelectType == 'multi'){
			// 		var selectedRowIds = $("[data-rowid='" + selectedRowId + "']");
					
			// 		RemoveGridRow(gridSerialNo,selectedRowIds,ddRowSelectType);
			// 	}else{
			// 		RemoveGridRow(gridSerialNo,selectedItemId,ddRowSelectType);
			// 	}
			// }

			if(mArray[t]['FieldType'] == 1){
				mfld += "<td><input readonly type='text' value='"+fldvalue+"' class='form-control'   name='"+gridSerialNo+""+mArray[t]['DbFieldName']+"[]'   id='"+gridSerialNo+""+mArray[t]['DbFieldName']+"' ></td>";
			}
			if(mArray[t]['FieldType'] == 3){
				mfld += "<td><textarea readonly type='number' class='form-control'   name='"+gridSerialNo+""+mArray[t]['DbFieldName']+"[]'   id='"+gridSerialNo+""+mArray[t]['DbFieldName']+"'>"+fldvalue+"</textarea></td>";
			}
			if(mArray[t]['FieldType'] == 7){
				mfld += "<td><input readonly type='number' value='"+fldvalue+"' class='form-control'   name='"+gridSerialNo+""+mArray[t]['DbFieldName']+"[]'   id='"+gridSerialNo+""+mArray[t]['DbFieldName']+"'></td>";
			}
			if(mArray[t]['FieldType'] == 6){
				mfld += "<td><input readonly type='date' value='"+fldvalue+"' class='form-control'   name='"+gridSerialNo+""+mArray[t]['DbFieldName']+"[]'   id='"+gridSerialNo+""+mArray[t]['DbFieldName']+"'></td>";
			}
			if(mArray[t]['FieldType'] == 5 || mArray[t]['FieldType'] == 19){
				var selectedOption = $("#kreon-grid-"+gridSerialNo+""+mArray[t]['DbFieldName'] + " :selected").text();
				mfld += "<td><select readonly style='pointer-events: none;' name='"+gridSerialNo+""+mArray[t]['DbFieldName']+"[]'  placeholder='"+mArray[t]['DisplayName']+"\' id='"+gridSerialNo+""+mArray[t]['DbFieldName']+"' class=\'form-control populate\'>";
				mfld += "><option value='"+fldvalue+"'>"+selectedOption+"</option>";
				mfld += "</select></td>";
			}
			if(mArray[t]['FieldType'] == 17){
				mfld += "<td><input disabled type='text' value='"+fldvalue+"' class='form-control'  name='"+gridSerialNo+mArray[t]['DbFieldName']+"-text[]'  id='"+gridSerialNo+mArray[t]['DbFieldName']+"-text'><input type='hidden' id='"+gridSerialNo + mArray[t]['DbFieldName']+"' name='"+gridSerialNo+""+mArray[t]['DbFieldName']+"[]' value=\'"+fldvalue1+"\'></td>";
			}
			if(mArray[t]['FieldType'] == 18){
				mfld += "<td><input readonly type='time' value='"+fldvalue+"' class='form-control'   name='"+gridSerialNo+""+mArray[t]['DbFieldName']+"[]\'   id='"+gridSerialNo+""+mArray[t]['DbFieldName']+"'></td>";
			}	
			//pass value to sp at adding row
			cuurrentRowData += mArray[t]['DbFieldName']+"=";
			if(mArray[t]['FieldType'] == 0 || mArray[t]['FieldType'] == 1 || mArray[t]['FieldType'] == 3 || mArray[t]['FieldType'] == 6 || mArray[t]['FieldType'] == 8 || mArray[t]['FieldType'] == 12 || mArray[t]['FieldType'] == 13 || mArray[t]['FieldType'] == 18){
				cuurrentRowData += fldvalue != "" ? "\'\'"+fldvalue+"\'\'" : "\'\'\'\'";
			}else if(mArray[t]['FieldType'] == 2 || mArray[t]['FieldType'] == 5 || mArray[t]['FieldType'] == 7 || mArray[t]['FieldType'] == 9 || mArray[t]['FieldType'] == 10 || mArray[t]['FieldType'] == 11 || mArray[t]['FieldType'] == 15 || mArray[t]['FieldType'] == 19){
				cuurrentRowData += fldvalue != "" ? fldvalue : 0;
			}else if(mArray[t]['FieldType'] == 17){
				cuurrentRowData += fldvalue1 != "" ? fldvalue1 : 0;
			}
			if(t != mArray.length-1)
				cuurrentRowData += "|";

			cuurrentRowDataWithComma += mArray[t]['DbFieldName']+"=";
			if(mArray[t]['FieldType'] == 0 || mArray[t]['FieldType'] == 1 || mArray[t]['FieldType'] == 3 || mArray[t]['FieldType'] == 6 || mArray[t]['FieldType'] == 8 || mArray[t]['FieldType'] == 12 || mArray[t]['FieldType'] == 13 || mArray[t]['FieldType'] == 18){
				cuurrentRowDataWithComma += fldvalue != "" ? "\'\'"+fldvalue+"\'\'" : "\'\'\'\'";
			}else if(mArray[t]['FieldType'] == 2 || mArray[t]['FieldType'] == 5 || mArray[t]['FieldType'] == 7 || mArray[t]['FieldType'] == 9 || mArray[t]['FieldType'] == 10 || mArray[t]['FieldType'] == 11 || mArray[t]['FieldType'] == 15 || mArray[t]['FieldType'] == 19){
				cuurrentRowDataWithComma += fldvalue != "" ? fldvalue : 0;
			}else if(mArray[t]['FieldType'] == 17){
				cuurrentRowDataWithComma += fldvalue1 != "" ? fldvalue1 : 0;
			}
			if(t != mArray.length-1)
				cuurrentRowDataWithComma += ",";
		}
		
		var cnt = $('#cnt'+gridSerialNo).val();	
		cnt = +cnt + 1;
		mfld += "<td><a style='padding:2px 3px;' href='javascript:void(0)' class='btn btn-danger removes"+gridSerialNo+"'><span class='glyphicon glyphicon glyphicon-remove' aria-hidden='true'></span></a><a style='padding:3px;margin:2px;' href='javascript:void(0)' class='btn btn-info edits"+gridSerialNo+"'><span class='glyphicon glyphicon glyphicon-pencil' aria-hidden='true'></span></a><input type='hidden' name='"+gridSerialNo+"GridEditID[]' id='"+gridSerialNo+"GridEditID' value='0'><input type='hidden' name='"+gridSerialNo+"GridTempID[]' id='"+gridSerialNo+"GridTempID' value='"+cnt+"'></td>";
		var headofGrid = headerGridStart+''+cnt +''+headerGridEnd;
		var end = end ;
		document.getElementById('cnt'+gridSerialNo).value = cnt;
		
		if(allreadyValidate == true){
			$('#panel'+gridSerialNo+' table tbody').append(headofGrid + mfld + end);
			$(".add"+gridSerialNo).attr("disabled", false);
		}else{
			$.ajax({
				type : "POST",
				dataType: "json",
				data : {ValidationOn:"gridsave", formData: cuurrentRowData, formDataWithComma:cuurrentRowDataWithComma, mainFormData:$("form").serialize(), FormID:<?php echo $FormID?>, GridID:$('#GridId'+gridSerialNo).val(), GridRowTempId: cnt},
				url : "validateForm.php",
				success:function(output) {

					if(output["status"] == true){
						if(output["data"]["Success"] == 1){									
							$('#panel'+gridSerialNo+' table tbody').append(headofGrid + mfld + end);
							$(".add"+gridSerialNo).attr("disabled", false);
						}else{
							if(output["data"]["Success"] == 2){
								if (confirm(output["data"]["Msg"]+". Are you sure you want to add?") == true) {
									$('#panel'+gridSerialNo+' table tbody').append(headofGrid + mfld + end);
									$(".add"+gridSerialNo).attr("disabled", false);
								} else {
									$(".add"+gridSerialNo).attr("disabled", false);
								}
							}else{
								// alert(output["data"]["Msg"]);
								$(".add"+gridSerialNo).attr("disabled", false);
							}
						}		
					}else{
						$('#panel'+gridSerialNo+' table tbody').append(headofGrid + mfld + end);
						// $(".add'.$d[3].'").attr("disabled", false);
					}
				}
			});
		}
		
		//Clear input value of first row
		for(var s=0; s < fieldArray.length; s++){
			// if(fieldArray[s][2] != 1){				
				if(fieldArray[s][0] == 5){
					$("#"+fieldArray[s][1]).val(null).trigger("change");
				}
				if(fieldArray[s][0] == 17){
					fieldName = fieldArray[s][1].split("-");
					$("#"+fieldName[0]+"-"+fieldName[1]+"-"+gridSerialNo+fieldName[2].slice(1)+"-text").val("");
					$("#"+fieldName[0]+"-"+fieldName[1]+"-"+gridSerialNo+fieldName[2].slice(1)).val("");
				}else{
					document.getElementById(fieldArray[s][1]).value = "";
				}
				$("#"+fieldArray[s][1]).css("border","1px solid #aa8a");
			// }
		}
		
		if(mArray[0]['FieldType'] == 17){
			$("#kreon-grid-"+gridSerialNo+mArray[0][1]+"-text").focus(); //Cursor focus on grid first field
		}else{
			$("#kreon-grid-"+gridSerialNo+""+mArray[0][1]).focus(); //Cursor focus on grid first field
		}	
	}

	// async function exportGridTableToExcelusingSP(GridID, filename = ''){
	// 	//Grid ID, FormID, User ID
	// 	// ajax
	// 	//fprm 3721
	// 	await new Promise((resolve, reject) => {
	// 		$.ajax({
	// 			type: "POST",
	// 			url: "importSpDataIntoExcel.php",
	// 			data: { GridID: GridID, FormID: <?php echo $FormID ?>, UserID: <?php echo $_SESSION['user_id']; ?>},
	// 			success: function (response) {
	// 				try {
	// 					const parsedResult = JSON.parse(response);
	// 					if (parsedResult.response === "true") {
	// 						resolve(parsedResult);  // Resolving the promise with the result
	// 					} else {
	// 						alert("Error while saving data..!");
	// 						resolve(null);  // Resolving with null in case of error
	// 					}
	// 				} catch (error) {
	// 					reject(error);  // Rejecting if JSON parsing fails
	// 				}
	// 			},
	// 			error: function (xhr, status, error) {
	// 				reject(error);  // Rejecting in case of AJAX error
	// 			}
	// 		});
	// 	});

	// }

	
	async function exportGridTableToExcelusingSP(GridID, filename = 'export') {
		var editID = $('#editID').val();
		try {
			// Make the AJAX call to the server
			const response = await new Promise((resolve, reject) => {
				$.ajax({
					type: "POST",
					url: "importSpDataIntoExcel.php", // The PHP file handling the data
					data: {
						GridID: GridID,
						FormID: <?php echo $FormID; ?>,
						UserID: <?php echo $_SESSION['user_id']; ?>,
						editID: editID
					},
					success: function (response) {
						try {
							const parsedResult = JSON.parse(response);
							if (parsedResult.response === "true") {
								resolve(parsedResult);  // Resolve with the successful result
							} else {
								alert("Error: " + parsedResult.message);
								resolve(null);  // Resolve with null in case of error
							}
						} catch (error) {
							reject(error);  // Reject if JSON parsing fails
						}
					},
					error: function (xhr, status, error) {
						reject(error);  // Reject if AJAX fails
					}
				});
			});

			// Process the response if it's successful
			if (response) {
				let jsonData = response;
				console.log("success",response, response.sheets);
				if(response.sheets !== undefined){
					//MULTI SHEETS
					if (jsonData.response === "true" && jsonData.sheets) {
						const wb = XLSX.utils.book_new();  // Create a new workbook

						jsonData.sheets.forEach(sheet => {
							// Convert data objects into an array of arrays
							const formattedData = sheet.data.map(row => sheet.headers.map(header => row[header] || ""));

							// Create worksheet with headers and formatted data
							const ws = XLSX.utils.aoa_to_sheet([sheet.headers, ...formattedData]);

							// Append worksheet to workbook
							XLSX.utils.book_append_sheet(wb, ws, sheet.sheetName);
						});

						// Export the workbook to an Excel file
						XLSX.writeFile(wb, 'exported_data.xlsx');
					} else {
						alert('No data found or error fetching data.');
					}
				}else{
					//SINGLE SHEET
					// Extract headers
					const headers = jsonData.headers;

					// Convert data to array format
					//const data = jsonData.data.map(row => headers.map(header => row[header] || ""));
					const data = jsonData.data.map(row => 
					headers.map(header => {
						const value = row[header];
						return value === 0 || value ? value : ""; // Keep 0 as 0, otherwise empty string
					})
					);
					// Add headers to the data array
					data.unshift(headers);

					// Create worksheet
					const ws = XLSX.utils.aoa_to_sheet(data);

					// Protect the sheet
					ws['!protect'] = {
						password: "1234",  // Set your password
						sheet: true,
						formatCells: false,
						formatColumns: false,
						formatRows: false
					};

					// Create workbook
					const wb = XLSX.utils.book_new();
					XLSX.utils.book_append_sheet(wb, ws, "ProtectedSheet");

					// Save the file
					XLSX.writeFile(wb, filename+".xlsx");
				}
				// const { headers, data } = response;

				// // Create the table dynamically from the response data
				// const table = document.createElement('table');
				// table.id = 'data-table'; // Set table ID for export

				// // Create table header row
				// const thead = document.createElement('thead');
				// const headerRow = document.createElement('tr');
				// headers.forEach(function (header) {
				// 	const th = document.createElement('th');
				// 	th.textContent = header; // Set header text
				// 	headerRow.appendChild(th);
				// });
				// thead.appendChild(headerRow);
				// table.appendChild(thead);

				// // Create table body rows
				// const tbody = document.createElement('tbody');
				// data.forEach(function (rowData) {
				// 	const row = document.createElement('tr');
				// 	headers.forEach(function (header) {
				// 		const td = document.createElement('td');
				// 		td.textContent = rowData[header];  // Fill table cell with data
				// 		row.appendChild(td);
				// 	});
				// 	tbody.appendChild(row);
				// });
				// table.appendChild(tbody);

				// // Append the table to the document body (or specific container)
				// document.body.appendChild(table);

				// filename = filename + '.xlsx';
				
				// // Now use TableToExcel.js to export the table to an Excel file
				// TableToExcel.convert(table, {
				// 	name: filename, // Excel file name
				// 	sheet: {
				// 	name: 'Sheet 1' // Name of the sheet
				// 	}
				// });

				// // Optionally remove the table after export (to keep the page clean)
				// document.body.removeChild(table);

				// // Protect specific cells using exceljs
				// const ExcelJS = window.ExcelJS;
				// const workbook = new ExcelJS.Workbook();

				// // Load the generated Excel file
				// fetch(filename)
				// 	.then(response => response.arrayBuffer())
				// 	.then(buffer => workbook.xlsx.load(buffer))
				// 	.then(() => {
				// 		const worksheet = workbook.getWorksheet('Sheet 1');

				// 		// Protect the entire worksheet
				// 		worksheet.eachRow({ includeEmpty: true }, function(row, rowNumber) {
				// 			row.eachCell({ includeEmpty: true }, function(cell, colNumber) {
				// 				cell.protection = { locked: true };
				// 			});
				// 		});

				// 		// Unlock specific cells (e.g., cells in row 3 and below)
				// 		for (let rowNumber = 3; rowNumber <= worksheet.rowCount; rowNumber++) {
				// 			worksheet.getRow(rowNumber).eachCell({ includeEmpty: true }, function(cell, colNumber) {
				// 				cell.protection = { locked: false };
				// 			});
				// 		}

				// 		// Protect the worksheet with a password
				// 		worksheet.protect('password', {
				// 			selectLockedCells: true,
				// 			selectUnlockedCells: true
				// 		});

				// 		// Save the updated workbook
				// 		workbook.xlsx.writeBuffer().then(buffer => {
				// 			const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
				// 			saveAs(blob, filename);
				// 		});
				// 	});
			}			

		} catch (error) {
			console.error('Error during export:', error);
		}
	}



	function exportGridTableToExcel(datatable, filename = ''){
		TableToExcel.convert(document.getElementById(datatable),{
			name: filename?filename+".xlsx":"excel_data.xlsx"
		});

		/*var downloadLink;
		// var dataType = 'application/vnd.ms-excel';
		var dataType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
		// console.log(document.getElementById("GridTable"))
		// console.log(document.getElementsByClassName("GridTablea"))
		// console.log(document.getElements("#panela #GridTable"))
		// console.log($("#panela #GridTable").text())
		// var tableSelect = document.getElementsByClassName(datatable) //$("#panela #GridTable");//document.getElementById(datatable).getElementsByTagName('table');
		// var tableHTML = tableSelect[0].outerHTML.replace(/ /g, '%20');

		var tableSelect = document.getElementById(datatable);
		var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');

		// Specify file name
		filename = filename?filename+'.xls':'excel_data.xls';
		
		// Create download link element
		downloadLink = document.createElement("a");
		document.body.appendChild(downloadLink);
		if(navigator.msSaveOrOpenBlob){
			var blob = new Blob(['\ufeff', tableHTML], {
				type: dataType
			});
			navigator.msSaveOrOpenBlob( blob, filename);
		}else{
			// Create a link to the file
			downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
			// Setting the file name
			downloadLink.download = filename;
			//triggering the function
			downloadLink.click();
		}*/
	}

	
</script>