
$(document).ready(function() 
{ 
	$('input').css('marginTop', 0); 
	$('input').css('marginBottom',0);
	$('select').css('marginTop',0) ;
	$('select').css('marginBottom',0) ;
	
	// $('#setting-tabs').tabs() ;
	$('#txtSettingName1').focus() ;
	$('#btnSettingUpdate').button().bind('click',updateSetting) ;
	$('#btnSettingUpload').button().bind('click',uploadSetting) ;
	$('#btnSettingRemove').button().bind('click',removeSetting) ;
	

	// $("#txtSettingTime").timepicker({
	// 	minuteStep: 60,
	// 	defaultTime: '06:00',
	// 	showMeridian: false,
	// });
	// $("#txtSettingTimeEnd").timepicker({
	// 	minuteStep: 60,
	// 	defaultTime: '18:00',
	// 	showMeridian: false,
	// });
	if ($('#imgLogo').attr('src') == "") {
		$('#btnSettingRemove').prop('disabled','disabled') ;
	}
        $('#txtSettingOtherEmpCode').val(empCodeToExportValue);
}) ;
function updateSetting() {
	saveSetting(C_UPDATE) ;
}
function saveSetting(type) {
	if (validateSetting()) {
		var data = { "type": type, "name1": $('#txtSettingName1').val(), "code": $('#txtSettingCode').val(),
			"addr1": $('#txtSettingAddr1').val(), "addr2": $('#txtSettingAddr2').val(), 
			"addr3": $('#txtSettingAddr3').val(), "addr4": $('#txtSettingAddr4').val(), 
			"addr5": $('#txtSettingAddr5').val(), "refno": $('#txtSettingRefNo').val(),
			"telno": $('#txtSettingTelNo').val(), "faxno": $('#txtSettingFaxNo').val(),
			"mailServer": $('#txtSettingMailserver').val(), 
			"mailPort": $('#txtSettingMailPort').val(), 
            "mailUser": $('#txtSettingMailUser').val(),
			"mailPwd": $('#txtSettingMailPwd').val(), 
			"ea": ($('#chkSettingMailEnableAuthentication').is(':checked') ? '1' : '0'),
			"ssl": ($('#chkSettingMailSSL').is(':checked') ? '1' : '0'),
			"upload_size_limit": ($('#txtSettingOtherUploadSizeLimit').val()),
			"base_currency": ($('#txtSettingOtherBaseCurrency').val()),
			"claim_day_last": ($('#txtSettingClaimDayLast').val()),
			"emp_code_to_export": ($('#txtSettingOtherEmpCode').val()),
			"allow_delete_master": ($('#chkSettingOtherAllowDeleteMaster').is(':checked') ? '1' : '0'),
			"use_payment_reference": ($('#chkSettingClaimUsePaymentReference').is(':checked') ? '1' : '0'),
			"use_bu": ($('#chkSettingBu').is(':checked') ? '1' : '0'),
			"start_time": $('#txtSettingTime').val(),
			"end_time": $('#txtSettingTimeEnd').val(),

			"use_reminder": ($('#chkSettingReminder').is(':checked') ? '1' : '0'),
			"reminder_message1": $('#txtReminderMessage1').val(),
			"reminder_message2": $('#txtReminderMessage2').val(),
			"reminder_message3": $('#txtReminderMessage3').val(),
			"reminder_message4": $('#txtReminderMessage4').val(),
			"reminder_interval": $('#txtReminderInterval').val(),
			"reminder_interval_unit": $('#txtReminderIntervalType').val(),

			"reminder_interval_approval": $('#txtReminderAutoApproval').val(),
			"reminder_interval_approval_unit": $('#txtReminderIntervalAprrovalType').val(),

			"failcount": "3"} ;
		var url = "request.pzx?c=" + setting_url + "&d=" + new Date().getTime() ;
		clearSetting() ;
		callServer(url,"json",data,onSettingResponse,null) ;
	}
}
function clearSetting() {
	$('#setting_err_mesg').text("") ;
}
function onSettingResponse(obj,resp) {
	if (resp.status == C_OK) {
		if (resp.type == C_DELETE) {
			$('#imgLogo').prop('src','') ;
			$('#btnSettingRemove').prop('disabled','disabled') ;
		}
		swal("System Message",resp.mesg) ;
	} else {
		swal("Error",resp.mesg) ;
	}
}
function validateSetting() {
	$('#setting_err_mesg').text('') ;
	if ($('#txtSettingName1').val()=="")
	{
		$('#setting_err_name1').show() ;
		$('#txtSettingName1').focus() ;
		swal("Organization name can not be blank.") ;
		return false ;
	} else {
		$('#setting_err_name1').hide() ;
	}
	
	if ($('#txtSettingCode').val()=="")
	{
		$('#setting_err_code').show() ;
		$('#txtSettingCode').focus() ;
		swal("Organization code can not be blank.") ;
		return false ;
	} else {
		$('#setting_err_code').hide() ;
	}	
	return true ;
}
function onUploadEnd(resp) {
	$('#setting_upload').hide() ;
	$('#btnSettingUpload').prop('disabled','');
	var s = resp.split("|") ;
	if (s[0]==C_OK) {
		$('#imgLogo').prop('src',s[1]) ;
		$('#btnSettingRemove').prop('disabled','') ;
	} else {
		swal(s[1]) ;
	}
}
function uploadSetting() {
	var fn = $('#fileLogo').val() ;
	if (fn == "") {
		swal('System Message','You must select a logo image file for upload.') ;
		return ;
	}
	if (validImageExt(fn)) {
		$('#btnSettingUpload').prop('disabled','disabled') ;
		var url = "upload.pzx?t=l&n=fileLogo&d=" + new Date().getTime() ;
		$('#setting_upload').show() ;
		uploaded = false ;
		ajaxUpload(this.form,url,onUploadEnd);
	}
}
function removeSetting() {
	// if (confirm("Confirm you want to remove the logo?")) {
	// 	var data = {"type": C_DELETE} ;
	// 	var url = "request.pzx?c=" + setting_url + "&d=" + new Date().getTime() ;
	// 	callServer(url,"json",data,onSettingResponse,null) ;
	// }

	//==================================================

	swal("Confirm do you want to remove the logo : ?", {
		buttons: {
			ok: true,
		  cancel: "Cancel",
		},
	  })
	  .then((value) => {
		switch (value) {
		  case "ok":
			  var data = {"type": C_DELETE} ;
				var url = "request.pzx?c=" + setting_url + "&d=" + new Date().getTime() ;
				callServer(url,"json",data,onSettingResponse,null) ;
		  break;
		}
	  });
}
function validImageExt(file) 
{
	var extArray = new Array(".png", ".jpg", ".jpeg"); 
	var validext = false;
	if (!file) 
		return;
   
	while (file.indexOf("\\") != -1)
		file = file.slice(file.indexOf("\\") + 1);
            
	var ext = file.slice(file.indexOf(".")).toLowerCase();
	for (var n = 0; n < extArray.length; n++) 
	{
		if (extArray[n] == ext) { validext = true; break; }
	}
	if (validext) {
		return true ;
	} else {
		swal("You can only upload files in .png, .jpg, .jpeg format") ;
		return false ;
	}
}

function checkReminder() {
	if($('#chkSettingReminder').is(':checked')){
		//$('#txtReminderMessage1').prop("disabled",false);
		$('#txtReminderMessage2').prop("disabled",false);
		$('#txtReminderMessage3').prop("disabled",false);
		$('#txtReminderMessage4').prop("disabled",false);
		$('#txtReminderInterval').prop("disabled",false);
		$('#txtReminderIntervalType').prop("disabled",false);

		$('#txtReminderAutoApproval').prop("disabled",false);
		$('#txtReminderIntervalAprrovalType').prop("disabled",false);
	}else{
		//$('#txtReminderMessage1').prop("disabled",true);
		$('#txtReminderMessage2').prop("disabled",true);
		$('#txtReminderMessage3').prop("disabled",true);
		$('#txtReminderMessage4').prop("disabled",true);
		$('#txtReminderInterval').prop("disabled",true);
		$('#txtReminderIntervalType').prop("disabled",true);

		$('#txtReminderAutoApproval').prop("disabled",true);
		$('#txtReminderIntervalAprrovalType').prop("disabled",true);
	}
}