
$(document).ready(function() 
{ 
	$('input').css('marginTop', 0); 
	$('input').css('marginBottom',0);
	$('select').css('marginTop',0) ;
	$('select').css('marginBottom',0) ;
		
	$('#txtSettingName1').focus() ;
	$('#btnSettingUpdate').button().bind('click',updateSetting) ;
	$('#btnSettingUpload').button().bind('click',uploadSetting) ;
	$('#btnSettingRemove').button().bind('click',removeSetting) ;
	$('#btnSettingTestEmail').button().bind('click',testEmailSetting) ;
	$('#btnSettingTestEmailModal').button().bind('click',opentestEmailSettingModal) ;
	
	$('#cobExpenseClaimType').val(selectedtype);
	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(), "mailUser": $('#txtSettingMailUser').val(),
			"mailPwd": $('#txtSettingMailPwd').val(), 
			"mailPort": $('#txtSettingMailPort').val(), 
			"ssl": ($('#chkSettingMailSSL').is(':checked') ? '1' : '0'),
			"ea": ($('#chkSettingMailEnableAuthentication').is(':checked') ? '1' : '0'),
			"resend": ($('#chkSettingMailResend').is(':checked') ? '1' : '0'),
			"upload_size_limit": ($('#txtSettingOtherUploadSizeLimit').val()),
			"base_currency": ($('#txtSettingOtherBaseCurrency').val()),
			"limit_type" : ($('#cobExpenseClaimType').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_tax_calculation": ($('#chkSettingClaimUseTaxCalculation').is(':checked') ? '1' : '0'),
			"use_tax_grey": ($('#chkSettingClaimUseTaxGrey').is(':checked') ? '1' : '0'),
			"allow_future_date": ($('#chkSettingClaimAllowFutureDate').is(':checked') ? '1' : '0'),
			"max_password_age":$('#txtSettingMaxPasswordAge').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.fire("System Message",resp.mesg, "success") ;
	} else {
		swal.fire("Error",resp.mesg,"error") ;
	}
}
function validateSetting() {
	$('#setting_err_mesg').text('') ;
	if ($('#txtSettingName1').val() == "")
	{
		alertValidatation("Organization name can not be blank.",'#txtSettingName1');
		return false ;
	}
	
	if ($('#txtSettingCode').val() == "")
	{
		alertValidatation("Organization code can not be blank.",'#txtSettingCode');
		return false ;
	}	
	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.fire("Error!!",s[1],"error") ;
	}
}

function uploadSetting() {
	var fn = $('#fileLogo').val() ;
	if (fn == "") {
		showDialog('System Message','You must select a logo image file for upload.') ;
		return ;
	}
	if (validImageExt(fn)) {
		$('#btnSettingUpload').prop('disabled','disabled') ;
		var url = "upload.pzx?t=1&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.fire("Confirm do you want to remove the logo : ?", "warning", {
		buttons: {
			ok: true,
		  cancel: "Cancel",
		},
	  })
    .then(function(result) {
      switch (result) {
        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;
   	
   	var extension = file.substr( (file.lastIndexOf('.') +1) );

	for (var n = 0; n < extArray.length; n++) 
	{
		if (extArray[n] == extension) { validext = true; break; }
	}
	if (validext) {
		return true ;
	} else {
		swal.fire("Error!","You can only upload files in .png, .jpg, .jpeg format", "error") ;
		return false ;
	}
}
function validImportEmployeeExt(file) 
{
	var extArray = new Array(".xml", ".csv"); 
	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 {
		alert("You can only upload files in .csv, .xml format") ;
		return false ;
	}
}
function opentestEmailSettingModal() {
	$('#emailTestingSetting').modal('show');
}
function testEmailSetting() {
	var data = { "type": "TEST_EMAIL", 
			"mailServer": $('#txtSettingMailserver').val(), "mailUser": $('#txtSettingMailUser').val(),
			"mailPwd": $('#txtSettingMailPwd').val(), 
			"mailPort": $('#txtSettingMailPort').val(), 
			"ssl": ($('#chkSettingMailSSL').is(':checked') ? '1' : '0'),
			"ea": ($('#chkSettingMailEnableAuthentication').is(':checked') ? '1' : '0'),
			"to":$('#emailTestingDestination').val()
			 } ;
		var url = "request.pzx?c=" + setting_url + "&d=" + new Date().getTime() ;
	
		callServer(url,"json",data,onSettingResponse,null) ;
}