jQuery(document).ready(function(){
	
	 $("input[name='from'], input[name='to'],input[name='dfrom'],input[name='dto'],input[id='visiting_date'],input[id='payment-date'],input[id='cal-date'],input[id='dispatch_date'],input[id='start-date'],input[id='end-date'],input[id='calender']").attr("autocomplete", "off");
	
	jQuery(document).on('focus', 'input[name="from"], input[name="to"]', function(e){
		$("input[name='from'], input[name='to']").attr("autocomplete", "off");
	});
	
	$('#export_link').click(function(event){  //alert('aa');
		var offset = $('#offset').val();
		var total_count = $('#total_count').val();
		if (isNaN(offset)){
			alert('Please enter valid number');
			event.preventDefault();
		}else{
			//window.location.href = $(this).attr('href')+'&offset='+offset;
			var href = $(this).attr('href')+'&offset='+offset;
			$(this).attr('href', href);
			return true; 
		}
	});
	
	/* $.fn.fillSelect = function(data) { 
		$(this).children('option').remove();
		return this.each(function() {
			if (this.tagName == 'SELECT') {
				var dropdownList = this;
				$.each(data, function(index, optionData) {
				var option = new Option(optionData, index);
					// if ($.browser.msie) {
						// dropdownList.add(option);
					// }
				   //else {
						dropdownList.add(option, null);
				   //}
			   });
		 }
		});
	} */
	
	$.fn.fillSelect = function(data) {
		const $select = $(this);
		$select.empty(); // Clear existing options

		
		// Create an array to hold sorted options and find the first option
		let firstKey = null;
		const sortedEntries = [];


		// Iterate over the object properties
		Object.keys(data).forEach(key => {
			if (firstKey === null) {
				// The first property encountered
				firstKey = key;
			} else {
				// Collect other properties for sorting
				sortedEntries.push([key, data[key]]);
			}
		});

		// Append the first option if it exists
		if (firstKey !== null) {
			$select.append($('<option>').val(firstKey).text(data[firstKey]));
		}

		// Sort the remaining entries alphabetically by value
		sortedEntries.sort((a, b) => a[1].localeCompare(b[1]));

		// Append sorted options
		sortedEntries.forEach(([key, value]) => {
			$select.append($('<option>').val(key).text(value));
		});
		
	};

	
	$('input[name="checkall"]').click(function(){
		var isChecked = this.checked;		
		$('input[name="listitems"]').each(function(){
			this.checked = isChecked;
		});
	});
	
	$('#delete-all').submit(function(){
		var checkedItems = $('input[name="listitems"]:checked').length;
		
		if(checkedItems == 0){
			alert(message.selectOne);
			return false;
		}else{
			if(confirm(message.confirmMessage)){
				var chkId = '';
				$('input[name="listitems"]:checked').each(function() {
				  chkId += $(this).val() + ",";
				});
				chkId =  chkId.slice(0,-1);
				$('#deleteAllValue').val(chkId);
				return true;
			}else{
				return false;
			}
		}
		
	});
	$('.verifyAllAct').click(function(){
		verify_all_activity($(this), 1);
		return false;
	});
	$('.unverifyAllAct').click(function(){
		verify_all_activity($(this), 0);
		return false;
	});
});

function error404(){
	alert(message.noPage);
}

function verify_all_activity(elmObj, ind){
	var checkedItems = $('input[name="listitems"]:checked').length;		
	if(checkedItems == 0){
		alert(message.selectOne);
		return false;
	}else{		
		if(ind == 1){
			var postUrl = siteUrl+'others/verify_all';
		}else{
			var postUrl = siteUrl+'others/unverify_all';
		}
		if(elmObj.attr('msg')){
			var cmessage = elmObj.attr('msg');
		}else{
			var cmessage = message.confirmMessage;
		}
		if(confirm(cmessage)){
			var chkId = '';
			$('input[name="listitems"]:checked').each(function() {
			  chkId += $(this).val() + ",";
			});
			chkId =  chkId.slice(0,-1);
			$('#deleteAllValue').val(chkId);
		
			ajaxRunning = true;
			
			jQuery.ajax({
				type: 'POST',
				url: postUrl,
				data: $('#delete-all').serialize(),
				beforeSend: function(){
					elmObj.after('<img src="'+siteUrl+'img/loading20.gif" id="loading"/>');
					elmObj.hide();
				},
				success: function(data){
					jQuery('#loading').remove();
					ajaxRunning = false;
					elmObj.show();
					window.location.href = window.location.href;
				},
				statusCode: {
					404: function() {
						alert( message.noPage );
					}
				}
			});
		}
	}	
}