//
function check_searchBox() {
	// search employee
	$("input#srchEmployee").keyup(function() {
		var passedValue = $("input#srchEmployee").val();
		if(passedValue.val!="") {
			$("div.suggest").css("display", "table-row");
			$.post("./includes/hrm.php?action=searchRec&dest=srchEmployee", { passedValue:passedValue }, function(data) {
				$("div.suggest").html(data);
			});
		}
	});
	$("input#srchEmployee").blur(function() {
		if($("#srchEmployee").val()=="") {
			$("div.suggest").css("display", "none");
		}
	});
		// search employee for payroll
	$("input#searchEmp4payroll").keyup(function() {
		var passedValue = $("input#searchEmp4payroll").val();
		if(passedValue.val!="") {
			$("div.suggest.client").css("display", "table-row");
			$.post("./includes/hrm.php?action=searchRec&dest=search_payroll", { passedValue: passedValue }, function(data) {
				$("div.suggest").html(data);
			});
		}
	});
	$("input#searchEmp4payroll").blur(function() {
		if($("#searchEmp4payroll").val()=="") {
			$("div.suggest.client").css("display", "none");
		}
	});

	$("input#srchGpyroll").keyup(function() {
		var passedValue = $("input#srchGpyroll").val();
		if(passedValue.val!="") {
			$("div.suggest.client").css("display", "table-row");
			$.post("./includes/hrm.php?action=searchRec&dest=search_Gpayroll", { passedValue: passedValue }, function(data) {
				$("div.suggest").html(data);
			});
		}
	});
	$("input#srchGpyroll").blur(function() {
		if($("#srchGpyroll").val()=="") {
			$("div.suggest.client").css("display", "none");
		}
	});

	// search user
	$("input#srchUser").keyup(function() {
		var passedValue = $("input#srchUser").val();
		if(passedValue.val!="") {
			$("div.suggest").css("display", "table-row");
			$.post("./includes/hrm.php?action=searchRec&dest=srchUser", { passedValue:passedValue }, function(data) {
				$("div.suggest").html(data);
			});
		}
	});
	$("input#srchUser").blur(function() {
		if($("#srchUser").val()=="") {
			$("div.suggest").css("display", "none");
		}
	});

	$("input#srchSkill").keyup(function() {
		var passedValue = $("input#srchSkill").val();
		if(passedValue.val!="") {
			$("div.suggest").css("display", "table-row");
			$.post("./includes/hrm.php?action=searchRec&dest=srchSkill", { passedValue:passedValue }, function(data) {
				$("div.suggest").html(data);
			});
		}
	});
	$("input#srchSkill").blur(function() {
		if($("#srchSkill").val()=="") {
			$("div.suggest").css("display", "none");
		}
	});

}





/* ---------------------------------------------
------------------- employees --------------------
------------------------------------------------ */

function loadEmployees(pageLimit, perPage) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/hrm.php?action=loadTable&dest=allEmployees",
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			$("#dataTable div#loading").css("display", "none");
			$(".load_more_link").addClass('noneLink');
			$("#dataTable").append(result);
		},
	});
	return false;
}
function simplifyAddEmployee() {
	//
	autosize(document.querySelectorAll('textarea'));
	$("#txtRemarks").keyup(function(){
		return tweetFeed(this, 1000);
	});
	/*CKEDITOR.replace("txtRemarks", {
		height : 100,
	});*/
	//
	var picker = new Pikaday({
		field: document.getElementById("txtDateJoined"),
		firstDay: 6,
		maxDate: new Date(maanta),
		yearRange: [2006,sanadkan],
	});
	$("#slcBranch").change(function(){
		var slcBranch = $("#slcBranch").val();
		if(slcBranch!="all") {
			$.post("./includes/general.php?action=pick_offices",{ slcBranch:slcBranch }, function(data) {
				$("#slcOffice").html(data);
			});
		} else { $("#slcOffice").html('<option value="all">All</option>'); }
		clearErrors();
	});
	$(".btnAdd.skill").click(function() {
		var txtSkill 		= $("#txtSkill").val();
		var txtAmount 		= $("#txtAmount").val();
		if(txtSkill.length < 1) {
			$("#dpvalerror1").css("display", "inline");
			$("#dpvalerror1").html("Skill name is required");
			$("#txtSkill").addClass("highlight");
			return false;
		}
		if(txtAmount.length < 1) {
			$("#dpvalerror2").css("display", "inline");
			$("#dpvalerror2").html("Amount is required");
			$("#txtAmount").addClass("highlight");
			return false;
		}

		$("label.process").css("display", "inline");
		$("#btnSave").attr("disabled", true);
		$("#btnSave").html("Wait...");

		$.post("./includes/hrm.php?action=saveNew&dest=addskill", { txtSkill:txtSkill, txtAmount:txtAmount}, function(data) {
			var addedpos = data.search("added");
			$("label.process").css("display", "none");
			if(addedpos==0) {
				var lastID = data.slice(6,21);
				swal({
					title: "Success!",
					text: "you added new skill successfully.",
					icon: "success",
						timer: 1500,
						button: false
				});
				$("#btnSave").attr("disabled", false);
				$("#btnSave").html("Save");
				$("#newSkill").prop("checked", false);
				$.post("./includes/hrm.php?action=searchRec&dest=pickSkill",{lastID:lastID}, function(data) {
				$("#txtskills").append(data);
				$("#txtSkill").val("");
				$("#txtAmount").val("");

			});
			} else {
				swal("Oow!", data, "error");
			}
		});
		return false;
	});
	$(".btnAdd.dept").click(function() {
		var department 		= $("#txtDepts").val();
		if(txtDepts.length < 1) {
			$("#dpvalerror3").css("display", "inline");
			$("#dpvalerror3").html("Department name is required");
			$("#txtDepts").addClass("highlight");
			return false;
		}
		$("label.process").css("display", "inline");
		$("#btnSave").attr("disabled", true);
		$("#btnSave").html("Wait...");

		$.post("./includes/general.php?action=saveNew&dest=addDepts", { department:department,}, function(data) {
			var addedpos = data.search("added");
			$("label.process").css("display", "none");
			if(addedpos==0) {
				var lastID = data.slice(6,21);
				swal({
					title: "Success!",
					text: "You added new department successfully.",
					icon: "success",
						timer: 1500,
						button: false
				});
				$("#btnSave").attr("disabled", false);
				$("#btnSave").html("Save");
				$("#newDepts").prop("checked", false);
				$.post("./includes/hrm.php?action=searchRec&dest=pickDepts",{lastID:lastID}, function(data) {
				$("#slscDept").append(data);
				$("#txtDepts").val("");

			});
			} else {
				swal("Oow!", data, "error");
			}
		});
		return false;
	});
}
//
function check_addEmployee() {
	var IDcard				= $("#IDcard").val();
	var empName				= $("#empName").val();
	var txtAddress			= $("#txtAddress").val();
	var txtPhone			= $("#txtPhone").val();
	var slcOffice			= $("#slcOffice").val();
	var txtRemarks			= $("#txtRemarks").val();
	var txtJob 				= $("#txtJob").val();
	var slcJobType			= $("#slcJobType").val();
	var txtDateJoined		= $("#txtDateJoined").val();
	var txtsalary			= $("#txtsalary").val();
	var txtAllowance		= $("#txtAllowance").val();
	var imageFile			= $("#imageFile").val();
	var department			= $("#slscDept").val();
	var txtTitle			= $("#txtTitle").val();
	var txtskills			= $("#txtskills").val();

	if(IDcard.length < 1) {
		$("#error0").css("display", "inline");
		$("#error0").html("Please provide employee ID Number");
		$("#IDcard").addClass("highlight");
		return false;
	} else { clearErrors(); }

	if(empName.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Please provide employee full name");
		$("#empName").addClass("highlight");
		return false;
	} else { clearErrors(); }

	if(txtPhone.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Employee phone is missing");
		$("#txtPhone").addClass("highlight");
		return false;
	} else { clearErrors(); }

	if(txtAddress.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Employee address is missing");
		$("#txtAddress").addClass("highlight");
		return false;
	} else { clearErrors(); }

	if(txtsalary.length < 1) {
		$("#error6").css("display", "inline");
		$("#error6").html("Employee Salary is not mentioned");
		$("#txtsalary").addClass("highlight");
		return false;
	} else { clearErrors(); }

	if(txtskills.length < 1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Employee skill is not mentioned");
		$("#txtskills").addClass("highlight");
		return false;
	} else { clearErrors(); }

	if(department.length < 1) {
		$("#error5").css("display", "inline");
		$("#error5").html("Employee department is required");
		$("#slscDept").addClass("highlight");
		return false;
	} else { clearErrors(); }

	if(txtJob.length < 1) {
		$("#error7").css("display", "inline");
		$("#error7").html("Employee title is missing");
		$("#txtJob").addClass("highlight");
		return false;
	} else { clearErrors(); }
	if(imageFile.length < 1) {
		$("#error8").css("display", "inline");
		$("#error8").html("Select file attached to this employees");
		$("#imageFile").addClass("highlight");
		return false;
	}
		$("label.process").css("display", "inline");
		$("#btnSave").attr("disabled", true);
		$("#btnSave").html("Wait...");

		/*$.post("./includes/hrm.php?action=saveNew&dest=addEmployee&userID="+myUser, { imageFile:imageFile,department:department, txtTitle:txtTitle,txtskills:txtskills, empName:empName, txtJob:txtJob, slcJobType:slcJobType, txtDateJoined:txtDateJoined, txtsalary:txtsalary, txtAllowance:txtAllowance, txtAddress:txtAddress, txtPhone:txtPhone, slcOffice:slcOffice, txtRemarks:txtRemarks}, function(data) {
			var addedpos = data.search("added");
			$("label.process").css("display", "none");
			if(addedpos==0) {
				var lastID = data.slice(6,21);
				swal({
					title: "Success!",
					text: "You added new employee successfully.",
					type: "success",
					timer: 1500,
					showConfirmButton: false
				},
					function() {
						window.location = "./workplace.php?role="+myRole+"&task=hrm&subtask=employees&action=showInfo&recID="+lastID;
					}
				);
			} else {
				swal("Oow!", data, "error");
			}
			$("#btnSave").attr("disabled", false);
			$("#btnSave").html("save");
		});*/
		return true;
}
//
function check_updateEmployee() {
	var empID				= $("#empID").val();
	var IDcard				= $("#IDcard").val();
	var empName				= $("#empName").val();
	var txtAddress			= $("#txtAddress").val();
	var txtPhone			= $("#txtPhone").val();
	var slcOffice			= $("#slcOffice").val();
	var txtRemarks			= $("#txtRemarks").val();
	var txtJob 				= $("#txtJob").val();
	var slcJobType			= $("#slcJobType").val();
	var txtDateJoined		= $("#txtDateJoined").val();
	var txtsalary			= $("#txtsalary").val();
	var txtAllowance		= $("#txtAllowance").val();
	var slcStatus			= $("#slcStatus").val();
	var department			= $("#slscDept").val();
	var txtTitle			= $("#txtTitle").val();
	var txtSkill				= $("#txtskills").val();

	if(IDcard.length < 1) {
		$("#error0").css("display", "inline");
		$("#error0").html("Please provide employee ID Number");
		$("#IDcard").addClass("highlight");
		return false;
	} else { clearErrors(); }

	if(empName.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Please provide employee full name");
		$("#empName").addClass("highlight");
		return false;
	} else { clearErrors(); }

	if(txtPhone.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Employee phone is missing");
		$("#txtPhone").addClass("highlight");
		return false;
	} else { clearErrors(); }

	if(txtAddress.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Employee address is missing");
		$("#txtAddress").addClass("highlight");
		return false;
	} else { clearErrors(); }

	if(txtsalary.length < 1) {
		$("#error6").css("display", "inline");
		$("#error6").html("Employee Salary is not mentioned");
		$("#txtsalary").addClass("highlight");
		return false;
	} else { clearErrors(); }

	if(txtSkill.length < 1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Employee skill is not mentioned");
		$("#txtskills").addClass("highlight");
		return false;
	} else { clearErrors(); }

	if(department.length < 1) {
		$("#error5").css("display", "inline");
		$("#error5").html("Employee department is required");
		$("#slscDept").addClass("highlight");
		return false;
	} else { clearErrors(); }

	if(txtJob.length < 1) {
		$("#error7").css("display", "inline");
		$("#error7").html("Employee title is missing");
		$("#txtJob").addClass("highlight");
		return false;
	} else { clearErrors(); }

		$("label.process").css("display", "inline");
		$("#btnSave").attr("disabled", true);
		$("#btnSave").html("Wait...");
	$.post("./includes/hrm.php?action=updateExist&dest=editEmployee&userID="+myUser, {empID:empID, IDcard:IDcard,department:department,slcStatus:slcStatus, txtTitle:txtTitle,txtSkill:txtSkill, empName:empName, txtJob:txtJob, slcJobType:slcJobType, txtDateJoined:txtDateJoined, txtsalary:txtsalary, txtAllowance:txtAllowance, txtAddress:txtAddress, txtPhone:txtPhone, slcOffice:slcOffice, txtRemarks:txtRemarks}, function(data) {
		var addedpos = data.search("updated");
		$("label.process").css("display", "none");
		if(addedpos==0) {
			var lastID = data.slice(6,21);
			swal({
				title: "Success!",
				text: "You updated employee record successfully.",
				icon: "success",
						timer: 1500,
						button: false
			})
					.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=hrm&subtask=employees&action=showInfo&recID="+empID;
				}
			);
		} else {
			swal("Oow!", data, "error");
		}
		$("#btnSave").attr("disabled", false);
		$("#btnSave").html("Update");
	});
	return false;
}
//
function catchEmployee(recID) {
	$.post("./includes/hrm.php?action=get_recInfo&dest=getEmployee", { recID:recID }, function(data) {
		$("div#foundNum").html('<label id="count">1 records has been selected</label>');
		$("div#dataTable").html(data);
	});
	$("div.suggest").css("display", "none");
}
function delEmployee(recID) {
	swal({
		title: "Are you sure?",
		text: "You going to delete this employee!",
		icon: "warning",
		dangerMode:true,
		buttons: ["Cancel","Yes, delete it!"]
	})
	.then(willDelete=> {
		if(willDelete) {
			$.post("./includes/hrm.php?action=deleteRec&dest=delEmployee", { recID:recID }, function(data) {
				if(data=="deleted") {

					swal({
						title: "Success!",
						text: "Employee record has been deleted successfully.",
						icon: "success",
						timer: 1500,
						button: false
					})
					.then(value=> {
						location.reload();
						}
					);
					var count = $("label#count").html();
					$("label#count").html(count-1);
					$("a.de.rec"+recID).parents("#record").fadeOut(500);
				} else {
					swal("Oops", data, "error");
				}
			});
		}
		return false;
	});
}
function delAttachment(recID) {
	swal({
		title: "Are you sure?",
		text: "You going to delete this attachment!",
		icon: "warning",
		dangerMode:true,
		buttons: ["Cancel","Yes, delete it!"]
	})
	.then(willDelete=> {
		if(willDelete) {
			$.post("./includes/hrm.php?action=deleteRec&dest=delAttachment", { recID:recID }, function(data) {
				if(data=="deleted") {

					swal({
						title: "Success!",
						text: "Attachment record has been deleted successfully.",
						icon: "success",
						timer: 1500,
						button: false
					})
						.then(value=> {
						location.reload();
						}
					);
					var count = $("label#count").html();
					$("label#count").html(count-1);
					$("a.de.rec"+recID).parents("#record").fadeOut(500);
				} else {
					swal("Oops", data, "error");
				}
			});
			return false;
		}
	});
}
function delBonusDeduct(recID,type) {
	swal({
		title: "Are you sure?",
		text: "You going to delete this "+type+"!",
		icon: "warning",
		dangerMode:true,
		buttons: ["Cancel","Yes, delete it!"]
	})
	.then(willDelete=> {
		if(willDelete) {
			$.post("./includes/hrm.php?action=deleteRec&dest=delBonusDeduct", { recID:recID }, function(data) {
				if(data=="deleted") {

					swal({
						title: "Success!",
						text: type+" record has been deleted successfully.",
						icon: "success",
						timer: 1500,
						button: false
					})
						.then(value=> {
						location.reload();
						}
					);
					var count = $("label#count").html();
					$("label#count").html(count-1);
					$("a.de.rec"+recID).parents("#record").fadeOut(500);
				} else {
					swal("Oops", data, "error");
				}
			});
			return false;
		}
	});
}
function addNewReason() {
	swal({
		title: "Create New Reason",
		text: 'Give name to the reason',
		content: "input",
		buttons:["Cancel","Save"]
	})
	.then(reasonname=> {
		if (reasonname) {

			$.post("./includes/hrm.php?action=saveNew&dest=addreason", { reasonname:reasonname }, function(data) {
				var addedpos = data.search("added");
				if(addedpos==0) {
					var lastID = data.slice(6,21);
					swal({
						title: "Success!",
						text: "You added new reason successfully",
						icon: "success",
							timer: 1500,
							button: false
					});
					$("#txtreason").append('<option value="'+reasonname+'" selected="selected">'+reasonname+'</option>');
				} else {
					swal("Oow!", data, "error");
				}
			});
		}
	});
	return false;
}

/* ------------------------------------------------------
------------------- Generate Payroll --------------------
-------------------------------------------------------*/
function loadGenPayroll(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+pageLimit+'&perPage='+perPage+'&period='+period;
	$.ajax({
		type: "POST",
		url: "./includes/hrm.php?action=loadTable&dest=GeneratedPyroll",
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			$("#dataTable div#loading").css("display", "none");
			$(".load_more_link").addClass('noneLink');
			$("#dataTable").append(result);
		},
	});
	return false;
}
function simplyGenPayrollAdd() {
	$(".advanceMoney").css("display", "none");
	$("#txtMonth").change(function() {
		$(".list").html('');
	});
	$('.btnAdd.generate').click(function() {
		var txtMonth 	= $("input#txtMonth").val();
		$(".list").html('');

		$.post("./includes/hrm.php?action=get_recInfo&dest=generate_payroll", { txtMonth:txtMonth}, function(data) {
				$(".list").append(data);
			});
	});
	$("#txtEmployee").keyup(function() {
		var passedValue = $("#txtEmployee").val();
		var txtMonth 	= $("#txtMonth").val();
		var payID 		= $("#payID").val();
		if(passedValue != "" && txtMonth != "") {
			$("div.suggest").css("display", "table-row");
			$.post("./includes/hrm.php?action=searchRec&dest=pickEmp4GenPayroll", { passedValue:passedValue, txtMonth:txtMonth, payID:payID }, function(data) {
				$("div.suggest").html(data);
			});
		}
	});
	$("input#txtEmployee").on("blur keyup", function() {
		if($("#txtEmployee").val()=="") {
			$("div.suggest").css("display", "none");
		}
	});
	//
}
function catchEmp4GenPayroll(empID, empName, phoneNum, skillRef, ttlSalary, advance, balance, skill, title, Allowance2, salary) {
	$("input#empID").val(empID);
	$("input#skillRef").val(skillRef);
	$("input#balance").val(balance);
	$("input#txtEmployee").val(empName);
	$("p#pEmpTel").html(phoneNum);
	$("p#pSalary").html('$'+salary);
	if(advance) { $(".advanceMoney").css("display", "inline"); $("p#pAdvance").html('$'+advance); }
	if(balance) $("p#pBalance").html('$'+balance); else $("p#pBalance").html(balance);
	$("input#txtAmount").val(balance);
	$("div.displayInfo").css("display", "inline");
	$("div.suggest").css("display", "none");

	$("input#txtadvance").val(advance);
	$("input#txtskill").val(skill);
	$("input#txtTitle").val(title);
	$("input#txtAllowance").val(Allowance2);
	$("input#txtsalary").val(salary);

}
function add_GenPayroll() {
	clearErrors();
	var listItem 	= $("div.list div.listItem").height();
	if(listItem == undefined) {
		$("#error1").css("display", "inline");
		$("#error1").html("no employees to generate!");
		return false;
	};
}
function edit_GenPayroll() {
	clearErrors();
	var pay_genID	= $("#pay_genID").val();
	var txtMonth	= $("#txtMonth").val();

	let salary = [];
	let allowance = [];
	let bonus = [];
	let deduction = [];
	let advance = [];
	let pay_gen_detailID = [];
	// , .allowance, .bonus, .deduction, .advance

	$('.salary').each((i, el) => {
		salary.push($(el).val())
	})

	$('.allowance').each((i, el) => {
		allowance.push($(el).val())
	})

	$('.bonus').each((i, el) => {
		bonus.push($(el).val())
	})

	$('.deduction').each((i, el) => {
		deduction.push($(el).val())
	})

	$('.advance').each((i, el) => {
		advance.push($(el).val())
	})

	$('.pay_gen_detailID').each((i, el) => {
		pay_gen_detailID.push($(el).val())
	})

	if(pay_genID.length < 1 || txtMonth.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("please select correct info");
		$("#txtMonth").addClass("highlight");
		return false;
	}

	$("label.process").css("display", "inline");
	$("#btnSave").attr("disabled", true);
	$("#btnSave").html("Wait.");

	$.post("./includes/hrm.php?action=updateExist&dest=editGenPayroll", { pay_genID:pay_genID, txtMonth:txtMonth, pay_gen_detailID:pay_gen_detailID, advance:advance, deduction:deduction, bonus:bonus, allowance:allowance, salary:salary}, function(data) {
		// console.log(data)
		// return false;
		var addedpos = data.search("updated");
		$("label.process").css("display", "none");
		if(addedpos==0) {
			var lastID = data.slice(6,21);
			swal({
				title: "success!",
				text: "You updated generated payroll successfully.",
				icon: "success",
				timer: 1500,
				button: false
			}).then(value=> {
				window.location = "./workplace.php?role="+myRole+"&task=hrm&subtask=Generate_payroll\
				&action=showInfo&recID="+pay_genID;
			});
		} else {
			swal("Oow!", data, "error");
		}
		$("#btnSave").attr("disabled", false);
		$("#btnSave").html("Update");
	});
	return false;
}
function delGenPayrol(recID) {
	swal({
		title: "Are you sure?",
		text: "You going to delete this payroll!",
		icon: "warning",
		dangerMode:true,
		buttons: ["Cancel","Yes, delete it!"]
	})
	.then(willDelete=> {
		if(willDelete) {
			$.post("./includes/hrm.php?action=deleteRec&dest=delGenPyrol", { recID:recID }, function(data) {
				if(data=="deleted") {

					swal({
						title: "Success!",
						text: "Generated payroll has been deleted successfully.",
						icon: "success",
							timer: 1500,
							button: false
					})
						.then(value=> {
						location.reload();
						}
					);
					var count = $("label#count").html();
					$("label#count").html(count-1);
					$("a.de.rec"+recID).parents("#record").fadeOut(500);
				} else {
					swal("Oops", data, "error");
				}
			});
			return false;
		}
	});
}
/* ---------------------------------------------
------------------- Payroll --------------------
------------------------------------------------ */
function loadPayroll(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+pageLimit+'&perPage='+perPage+'&period='+period;
	$.ajax({
		type: "POST",
		url: "./includes/hrm.php?action=loadTable&dest=allPayroll",
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			$("#dataTable div#loading").css("display", "none");
			$(".load_more_link").addClass('noneLink');
			$("#dataTable").append(result);
		},
	});
	return false;
}
function simplyPayrollAdd() {
	$(".advanceMoney").css("display", "none");
	$("#txtMonth").change(function() {
		$(".advanceMoney, div.displayInfo, div.suggest").css("display", "none");
		$("input#empID, input#balance, input#txtEmployee, input#txtAmount, input#txtInWords, input#advance").val('');
		$("p#pEmpTel, p#pSalary, p#pBalance").html('');
	});
	$("#slcPayType").change(function() {
		var slcPayType = $("#slcPayType").val();
		if(slcPayType==="salary"){$("input#txtAmount").val($("input#advance").val());}
		else{$("input#txtAmount").val('');}
	});
	$("#txtEmployee").keyup(function() {
		var passedValue = $("#txtEmployee").val();
		var slcPayType 	= $("#slcPayType").val();
		var txtMonth 	= $("#txtMonth").val();
		var payID 		= $("#payID").val();
		if(passedValue != "" && txtMonth != "") {
			$("div.suggest").css("display", "table-row");
			if(slcPayType=="advance"){
			$.post("./includes/hrm.php?action=searchRec&dest=pickEmp4Payroll", { passedValue:passedValue, txtMonth:txtMonth }, function(data) {
				$("div.suggest").html(data);
				$("#type").val("advance")
			});}
			if(slcPayType=="salary"){
			$.post("./includes/hrm.php?action=searchRec&dest=pickEmp4GPayroll", { passedValue:passedValue, txtMonth:txtMonth, payID:payID }, function(data) {
				$("div.suggest").html(data);
				$("#type").val("payroll")
			});}
		}
	});
	$("input#txtEmployee").on("blur keyup", function() {
		if($("#txtEmployee").val()=="") {
			$("div.suggest").css("display", "none");
		}
	});

	$("#slcBankID").change(function() {
		var bankID = $("#slcBankID").val();
		if(bankID=="") {
			$("#slcBankAcc").html('');
		} else {
			$.post("./includes/finance.php?action=searchRec&dest=myBankAccs", { bankID:bankID }, function(data) {
				$("#slcBankAcc").html(data);
			});
		}
	});
	$("#txtAmount").on("keyup change", function() {
		inWords($("#txtAmount").val());
	});
	//
	/*
	autosize(document.querySelectorAll('textarea'));
	$("#txtRemarks").keyup(function(){
		return tweetFeed(this, 1000);
	}); */
	//
	var b4year		= Number(thisYear)-1;
	var picker		= new Pikaday({
		field: document.getElementById("transDate"),
		firstDay: 6,
		maxDate: new Date(today),
		yearRange: [b4year,thisYear],
	});
}
function catchEmp4Payroll(empID, empName, phoneNum, ttlSalary2, salary, advance, balance) {
	$("input#empID").val(empID);
	$("input#balance").val(balance);
	$("input#txtEmployee").val(empName);
	$("p#pEmpTel").html(phoneNum);
	$("p#pSalary").html('$'+ttlSalary2);
	if(advance) { $(".advanceMoney").css("display", "inline"); $("p#pAdvance").html('$'+advance); }
	if(balance) $("p#pBalance").html('$'+balance); else $("p#pBalance").html(balance);
	$("#advance").val(balance);
	$("input#txtAmount").val(balance);
	inWords($("#txtAmount").val());
	$("div.displayInfo").css("display", "inline");
	$("div.suggest").css("display", "none");
}
function catchEmp4GPayroll(empID, empName, phoneNum, deduction, salary, advance, balance) {
	$("input#empID").val(empID);
	$("input#balance").val(balance);
	$("input#txtEmployee").val(empName);
	$("p#pEmpTel").html(phoneNum);
	$("p#pSalary").html('$'+salary);
	$("p#pDeduct").html('$'+deduction);
	if(advance) { $(".advanceMoney").css("display", "inline"); $("p#pAdvance").html('$'+advance); }
	if(balance) $("p#pBalance").html('$'+balance); else $("p#pBalance").html(balance);
	$("#advance").val(balance);
	$("input#txtAmount").val(balance);
	$("input#txtAmount").attr('readonly','readonly');
	inWords($("#txtAmount").val());
	$("div.displayInfo").css("display", "inline");
	$("div.suggest").css("display", "none");
}
function check_addPayroll() {
	clearErrors();
	var paytype		= $("#type").val();
	var txtMonth	= $("#txtMonth").val();
	var txtEmployee	= $("#txtEmployee").val();
	var empID		= $("#empID").val();
	var balance		= $("#balance").val();
	var slcBankID	= $("#slcBankID").val();
	var slcBankAcc	= $("#slcBankAcc").val();
	var txtCheque	= $("#txtCheque").val();
	var txtAmount	= $("#txtAmount").val();
	var txtInWords	= $("#txtInWords").val();
	var txtRemarks	= $("#txtRemarks").val();
	var transDate	= $("#transDate").val();

	if(txtEmployee.length < 1 && empID.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("please select employee");
		$("#txtEmployee").addClass("highlight");
		return false;
	}
	if(slcBankID == undefined || slcBankID.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("select paid throug bank");
		$("#slcBankID").addClass("highlight");
		return false;
	}
	if(slcBankAcc == undefined || slcBankAcc == '---') {
		$("#error2").css("display", "inline");
		$("#error2").html("Account number");
		$("#slcBankAcc").addClass("highlight");
		return false;
	}
	if(txtAmount.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("amount is required");
		$("#txtAmount").addClass("highlight");
		return false;
	} else if(Number(txtAmount) <= 0) {
		$("#error3").css("display", "inline");
		$("#error3").html("amount must not greater than 0");
		$("#txtAmount").addClass("highlight");
		return false;
	} else if(parseFloat(txtAmount) > parseFloat(balance)) {
		$("#error3").css("display", "inline");
		$("#error3").html("amount must not greater than $"+balance);
		$("#txtAmount").addClass("highlight");
		return false;
	}
	$("label.process").css("display", "inline");
	$("#btnSave").attr("disabled", true);
	$("#btnSave").html("Wait.");

	$.post("./includes/hrm.php?action=saveNew&dest=addPayroll", { paytype:paytype,txtMonth:txtMonth, empID:empID, slcBankID:slcBankID, slcBankAcc:slcBankAcc, txtCheque:txtCheque, txtAmount:txtAmount, txtInWords:txtInWords, txtRemarks:txtRemarks, transDate:transDate }, function(data) {
		var addedpos = data.search("added");
		$("label.process").css("display", "none");
		if(addedpos==0) {
			var lastID = data.slice(6,21);
			swal({
				title: "success!",
				text: "You added new payroll successfully.",
				icon: "success",
						timer: 1500,
						button: false
			})
					.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=hrm&subtask=payroll\
					&action=showInfo&recID="+lastID;
				}
			);
		} else {
			swal("Oow!", data, "error");
		}
		$("#btnSave").attr("disabled", false);
		$("#btnSave").html("Save");
	});
	return false;
}
function check_editPayroll() {
	clearErrors();
	var payID		= $("#payID").val();
	var txtMonth	= $("#txtMonth").val();
	var txtEmployee	= $("#txtEmployee").val();
	var empID		= $("#empID").val();
	var balance		= $("#balance").val();
	var slcBankID	= $("#slcBankID").val();
	var slcBankAcc	= $("#slcBankAcc").val();
	var txtCheque	= $("#txtCheque").val();
	var txtAmount	= $("#txtAmount").val();
	var txtInWords	= $("#txtInWords").val();
	var txtRemarks	= $("#txtRemarks").val();

	if(txtMonth.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("fadlan dooro bisha");
		$("#txtMonth").addClass("highlight");
		return false;
	}
	if(txtEmployee.length < 1 && empID.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("fadlan dooro shaqaale");
		$("#txtEmployee").addClass("highlight");
		return false;
	}
	if(slcBankID == undefined || slcBankID.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("dooro banki akoonka");
		$("#slcBankID").addClass("highlight");
		return false;
	}
	if(slcBankAcc == undefined || slcBankAcc == '---') {
		$("#error3").css("display", "inline");
		$("#error3").html("dooro akoon nambar");
		$("#slcBankAcc").addClass("highlight");
		return false;
	}
	if(txtAmount.length < 1) {
		$("#error4").css("display", "inline");
		$("#error4").html("amount is required");
		$("#txtAmount").addClass("highlight");
		return false;
	} else if(Number(txtAmount) < 0) {
		$("#error4").css("display", "inline");
		$("#error4").html("iska hubi lacagta aad qortay");
		$("#txtAmount").addClass("highlight");
		return false;
	} else if(parseFloat(txtAmount) > parseFloat(balance)) {
		$("#error4").css("display", "inline");
		$("#error4").html("Amount must not greater than $"+balance);
		$("#txtAmount").addClass("highlight");
		return false;
	}
	$("label.process").css("display", "inline");
	$("#btnUpdate").attr("disabled", true);
	$("#btnUpdate").html("Wait.");

	$.post("./includes/hrm.php?action=updateExist&dest=editPayroll", { payID:payID, txtMonth:txtMonth, empID:empID, slcBankID:slcBankID, slcBankAcc:slcBankAcc, txtCheque:txtCheque, txtAmount:txtAmount, txtInWords:txtInWords, txtRemarks:txtRemarks }, function(data) {
		$("label.process").css("display", "none");
		if(data=="updated") {
			var lastID = data.slice(6,21);
			swal({
				title: "success!",
				text: "You updated Payroll record successfully.",
				icon: "success",
						timer: 1500,
						button: false
			})
					.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=hrm&subtask=payroll\
					&action=showInfo&recID="+payID;
				}
			);
		} else {
			swal("Oow!", data, "error");
		}
		$("#btnUpdate").attr("disabled", false);
		$("#btnUpdate").html("Update");
	});
	return false;
}
function catchPayroll(recID) {
	$.post("./includes/hrm.php?action=get_recInfo&dest=getPayroll", { recID:recID }, function(data) {
		$("div#foundNum").html('<label id="count">1 records has been selected</label>');
		$("div#dataTable").html(data);
	});
	$("div.suggest").css("display", "none");
}
function catchGPayroll(recID) {
	$.post("./includes/hrm.php?action=get_recInfo&dest=getGPayroll", { recID:recID }, function(data) {
		$("div#foundNum").html('<label id="count">1 records has been selected</label>');
		$("div#dataTable").html(data);
	});
	$("div.suggest").css("display", "none");
}

/* ---------------------------------------------
------------------- employee skill--------------
------------------------------------------------ */
function loadSkills(pageLimit, perPage) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/hrm.php?action=loadTable&dest=allSkills",
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			$("#dataTable div#loading").css("display", "none");
			$(".load_more_link").addClass('noneLink');
			$("#dataTable").append(result);
		},
	});
	return false;
}
function check_addskill() {
	 clearErrors();
	var txtSkill 	= $("#txtSkill").val();
	var txtAmount 	= $("#txtAmount").val();
	if(txtSkill.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Skill name is required");
		$("#txtSkill").addClass("highlight");
		return false;
	}
	if(txtAmount.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Skill amount is required");
		$("#txtAmount").addClass("highlight");
		return false;
	}
	$("label.process").css("display", "inline");
	$("#btnSave").attr("disabled", true);
	$("#btnSave").html("Wait...");

	$.post("./includes/hrm.php?action=saveNew&dest=addskill", { txtSkill:txtSkill,txtAmount:txtAmount }, function(data){
		var addedpos = data.search("added");
		$("label.process").css("display", "none");
		if(addedpos==0) {
			var lastID = data.slice(6,21);
			swal({
				title: "Success",
				text: "You added new skill successfully",
				icon: "success",
						timer: 1500,
						button: false
			})
					.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=hrm&subtask=skill";
				}
			);
		} else {
			swal("Oow!", data, "error");
		}
		$("#btnSave").attr("disabled", false);
		$("#btnSave").html("Save");
	});
	return false;
}
function check_editskill() {
	clearErrors();
	var skillRef 	= $("#skillRef").val();
	var txtSkill 	= $("#txtSkill").val();
	var txtAmount 	= $("#txtAmount").val();
	if(txtSkill.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Skill name is required");
		$("#txtSkill").addClass("highlight");
		return false;
	}
	if(txtAmount.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Skill amount is required");
		$("#txtAmount").addClass("highlight");
		return false;
	}
	$("label.process").css("display", "inline");
	$("#btnSave").attr("disabled", true);
	$("#btnSave").html("Wait...");

	$.post("./includes/hrm.php?action=updateExist&dest=editskill", { skillRef:skillRef,txtSkill:txtSkill,txtAmount:txtAmount }, function(data){
		if(data=="updated") {
			swal({
				title: "Success",
				text: "You updated skill record successfully",
				icon: "success",
						timer: 1500,
						button: false
			})
					.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=hrm&subtask=skill";
				}
			);
		} else {
			swal("Oow!", data, "error");
		}
		$("#btnSave").attr("disabled", false);
		$("#btnSave").html("Update");
	});
	return false;
}
function catchSkill(recID) {
	$.post("./includes/hrm.php?action=get_recInfo&dest=getSkill", { recID:recID }, function(data) {
		$("div#foundNum").html('<label id="count">1 records has been selected</label>');
		$("div#dataTable").html(data);
	});
	$("div.suggest").css("display", "none");
}
function delskill(recID) {
	swal({
		title: "Are you sure?",
		text: "You going to delete this skill!",
		icon: "warning",
		dangerMode:true,
		buttons: ["Cancel","Yes, delete it!"]
	})
	.then(willDelete=> {
		if(willDelete) {
			$.post("./includes/hrm.php?action=deleteRec&dest=delskill", { recID:recID }, function(data) {
				if(data=="deleted") {

					swal({
						title: "Success!",
						text: "Skill record has been deleted successfully.",
						icon: "success",
							timer: 1500,
							button: false
					})
						.then(value=> {
						location.reload();
						}
					);
					var count = $("label#count").html();
					$("label#count").html(count-1);
					$("a.de.rec"+recID).parents("#record").fadeOut(500);
				} else {
					swal("Oops", data, "error");
				}
			});
		}
		return false;
	});
}


/* ---------------------------------------------
--------------------- users --------------------
------------------------------------------------ */

function loadUsers(pageLimit, perPage) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/hrm.php?action=loadTable&dest=allUsers",
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			$("#dataTable div#loading").css("display", "none");
			$(".load_more_link").addClass('noneLink');
			$("#dataTable").append(result);
		},
	});
	return false;
}

function catchEmployee4User(empID, empName, phoneNum, jobTitle) {
	$("input#empID").val(empID);
	$("input#empName").val(empName);
	$("p#pEmpName").html(empName);
	$("p#pPhone").html(phoneNum);
	$("p#pJobDetails").html(jobTitle);
	$("div.displayInfo.employee").css("display", "inline");
	$("div.suggest.employee").css("display", "none");
}
function check_editUser() {
	var txtName 	= $("#txtName").val();
	var txtComm 	= $("#txtComm").val();
	var one_grant	= $("input.checkOne:checked").val();

	if(one_grant!="on") {
		$("#error3").css("display", "inline");
		$("#error3").html("Each user must have at least one right to access the system.");
		return false;
	} else { clearErrors(); }

	$("label.process").css("display", "inline");
	$("#btnUpdate").attr("disabled", true);
	$("#btnUpdate").html("Wait...");

	return true;
}
function catchUser(recID) {
	$.post("./includes/hrm.php?action=get_recInfo&dest=getUser", { recID:recID }, function(data) {
		$("div#foundNum").html('<label id="count">1 records has been selected</label>');
		$("div#dataTable").html(data);
	});
	$("div.suggest").css("display", "none");
}
function smpfyAddUser() {
	$("input#txtEmployee").keyup(function() {
		var passedValue = $("input#txtEmployee").val();
		if(passedValue != "") {
			$("div.suggest.client").css("display", "table-row");
			$.post("./includes/hrm.php?action=searchRec&dest=pickEmp4user", { passedValue:passedValue }, function(data) {
				$("div.suggest.client").html(data);
			});
		}
	});
	$("input#txtEmployee").blur(function() {
		if($("#txtEmployee").val()=="") {
			$("div.suggest.client").css("display", "none");
		}
	});
	$("input#txtUser").on('blur keydown', function() {
		var txtUser	= $("input#txtUser").val();
		$("div.workplace div.theForm div.column span.waiting.user").css("display", "inline");
		$.post("./includes/general.php?action=searchRec&dest=checkUserExist", { txtUser: txtUser }, function(data) {
			if(data == "taken") {
				$("input#txtUser").addClass("highlight");
				$("#error1").css("display", "inline");
				$("#error1").html("this user is already taken.");
			} else {
				$("input#txtUser").removeClass("highlight");
				$("#error1").css("display", "none");			}
			$("div.workplace div.theForm div.column span.waiting.user").css("display", "none");
		});
	});
	//
	$("input#txtEmail").on('blur keydown', function() {
		var txtEmail	= $("input#txtEmail").val();
		$("div.workplace div.theForm div.column span.waiting.email").css("display", "inline");
		$.post("./includes/pre-register.php?action=searchRec&dest=checkEmailExist", { txtEmail: txtEmail }, function(data) {
			if(data == "taken") {
				$("input#txtEmail").addClass("highlight");
				$("#error6").css("display", "inline");
				$("#error6").html("Iimeeyl kaan akoon ayaa lagu sameeyay mar hore");
			} else {
				$("input#txtEmail").removeClass("highlight");
				$("#error6").css("display", "none");
			}
			$("div.workplace div.theForm div.column span.waiting.email").css("display", "none");
		});

	});
	// if checkAll is changed
	$("span#tickAll").click(function () {
		if (document.getElementById("checkAll").checked == true) {
			document.getElementById("checkAll").checked = false;
			$("#tickAll").html("Check All");
			$("input.checkOne").removeAttr("checked");
		} else {
			document.getElementById("checkAll").checked = true;
			$("#tickAll").html("Un-check All");
			$("input.checkOne").attr("checked", "checked");
		}
		$("#error6").css("display", "none");
	});
	//
	$("input#checkAll").click(function () {
		if($("#tickAll").html()=="Check All") {
			$("#tickAll").html("Un-check All");
			$("input.checkOne").attr("checked", "checked");
		} else {
			$("#tickAll").html("Check All");
			$("input.checkOne").removeAttr("checked");
		}
		$("#error6").css("display", "none");
	});
	// click one
	$("input.checkOne").click(function () {
		document.getElementById("checkAll").checked = false;
		$("#tickAll").html("Check All");
		$("#error6").css("display", "none");
	});
	// click tick label
	$("label.tickOne").click(function () {
		var checked = $(this).parent("div").find("input[type='checkbox']:checked").length;
		if(checked==0) {
			$(this).parent("div").find("input[type='checkbox']").attr("checked", "checked");
		} else {
			$(this).parent("div").find("input[type='checkbox']").removeAttr("checked");
		}
		document.getElementById("checkAll").checked = false;
		$("#tickAll").html("Check All");
		$("#error6").css("display", "none");
	});
}
function check_addUser() {
	var empID 		= $("#empID").val();
	var txtEmployee 	= $("#txtEmployee").val();
	var txtUser 	= $("#txtUser").val();
	var txtPassword = $("#txtPassword").val();
	var rePassword 	= $("#rePassword").val();
	var one_grant	= $("input.checkOne:checked").val();

	if(txtEmployee.length < 1 || empID.length<1){
		$("#error10").css("display", "inline");
		$("#error10").html("Please provide user name");
		$("#txtEmployee").addClass("highlight");
		return false;

	}

	if(txtUser.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Please provide user name");
		$("#txtUser").addClass("highlight");
		return false;

	} else if(txtUser.match(spaceExp)||(txtUser[0]===' ')) {
		$("#error1").css("display", "inline");
		$("#error1").html("username name cant start with space!");
		$("#txtUser").addClass("highlight");
		return false;

	} else { clearErrors(); }

	if(txtPassword.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Password is needed ");
		$("#txtPassword").addClass("highlight");
		return false;
	} else { clearErrors(); }

	if(rePassword.length < 1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Please re-enter password");
		$("#rePassword").addClass("highlight");
		return false;
	} else if(txtPassword != rePassword) {
		$("#error4").css("display", "inline");
		$("#error4").html("password mis-match");
		$("#rePassword").addClass("highlight");
		return false;
	} else { clearErrors(); }

	if(one_grant!="on") {
		$("#error5").css("display", "inline");
		$("#error5").html("Each user must have at least one right to access the system.");
		return false;
	} else { clearErrors(); }

	$("label.process").css("display", "inline");
	$("#btnSave").attr("disabled", true);
	$("#btnSave").html("Wait...");

	return true;
}
//
function addDefaultBankAcc() { clearErrors();
	var username	= $("#username").val();
	var slcBankID	= $("#slcBankID").val();
	var slcBankAcc	= $("#slcBankAcc").val();
	var isDefault	= $("#isDefault").val();

	if(slcBankID.length < 1 || username.length < 1){
		$("#dpvalerror1").css("display", "inline");
		$("#dpvalerror1").html("Please provide bank");
		$("#slcBankID").addClass("highlight");
		return false;
	}

	if(slcBankAcc.length < 1){
		$("#dpvalerror2").css("display", "inline");
		$("#dpvalerror2").html("Please provide bank account");
		$("#slcBankAcc").addClass("highlight");
		return false;
	}

	$("label.process").css("display", "inline");
	$("#btnSave").attr("disabled", true);
	$("#btnSave").html("Wait...");

	$.post("./includes/hrm.php?action=saveNew&dest=addDefaultBankAcc", { username:username,slcBankID:slcBankID, slcBankAcc:slcBankAcc, isDefault:isDefault }, function(data) {

		var addedpos = data.search("added");
		$("label.process").css("display", "none");
		if(addedpos==0) {
			swal({
				title: "Success!",
				text: "You updated selected record successfully",
				icon: "success",
						timer: 1500,
						button: false
			})
					.then(value=> {
				location.reload();
			}
			);
		} else {
			swal("Oow!", data, "error");
		}
	});

	return false;
}
//
function removeDefaultAcc(recID) {

	swal({
		title: "Are you sure?",
		text: "You going to delete this default bank account!",
		icon: "warning",
		dangerMode:true,
		buttons: ["Cancel","Yes, delete it!"]
	})
	.then(willDelete=> {
		if(willDelete) {
			$.post("./includes/hrm.php?action=deleteRec&dest=delDefaultAcc", { recID:recID}, function(data) {
				if(data=="deleted") {
					swal({
						title: "Success!",
						text: "Selected record has been deleted successfully.",
						icon: "success",
							timer: 1500,
							button: false
					})
						.then(value=> {
						location.reload();
						}
					);
				} else {
					swal("Oops", data, "error");
				}
			});
		}
	});
return false;
}
//
function addUserPenalty() { clearErrors();
	var username	= $("#username").val();
	var slcPenalty	= $("#slcPenalty").val();
	var txtValue	= $("#txtValue").val();
	var penInvoiceID	= $("#penInvoiceID").val();

	if(slcPenalty.length < 1 || username.length < 1 || penInvoiceID.length < 1){
		$("#dpPenerror1").css("display", "inline");
		$("#dpPenerror1").html("Please provide penalty");
		$("#slcPenalty").addClass("highlight");
		return false;
	}

	if(txtValue.length < 1){
		$("#dpPenerror2").css("display", "inline");
		$("#dpPenerror2").html("Please provide amount");
		$("#txtValue").addClass("highlight");
		return false;
	}

	$("label.process").css("display", "inline");
	$("#btnSave").attr("disabled", true);
	$("#btnSave").html("Wait...");

	$.post("./includes/hrm.php?action=saveNew&dest=addUserPenalty", { username:username,penInvoiceID:penInvoiceID,slcPenalty:slcPenalty, txtValue:txtValue }, function(data) {

		var addedpos = data.search("added");
		$("label.process").css("display", "none");
		if(addedpos==0) {
			swal({
				title: "Success!",
				text: "You updated selected record successfully",
				icon: "success",
						timer: 1500,
						button: false
				})
					.then(value=> {
				location.reload();
			}
			);
		} else {
			swal("Oow!", data, "error");
		}
	});

	return false;
}
//
//
function removeUserPenalty(recID) {

	swal({
		title: "Are you sure?",
		text: "You going to delete this user penalty!",
		icon: "warning",
		dangerMode:true,
		buttons: ["Cancel","Yes, delete it!"]
	})
	.then(willDelete=> {
		if(willDelete) {
			$.post("./includes/hrm.php?action=deleteRec&dest=delUserPenalty", { recID:recID}, function(data) {
				if(data=="deleted") {
					swal({
						title: "Success!",
						text: "Selected record has been deleted successfully.",
						icon: "success",
							timer: 1500,
							button: false
					})
						.then(value=> {
						location.reload();
						}
					);
				} else {
					swal("Oops", data, "error");
				}
			});
		}

	});
return false;
}
//
function addUserNote() { clearErrors();
	var username	= $("#username").val();
	var slcNoteType	= $("#slcNoteType").val();
	var txtRemarks	= $("#txtRemarks").val();
	var noteDate	= $("#noteDate").val();


	if(slcNoteType.length < 1 || username.length < 1){
		$("#dpNoteerror1").css("display", "inline");
		$("#dpNoteerror1").html("Please provide note type");
		$("#slcNoteType").addClass("highlight");
		return false;
	}

	if(txtRemarks.length < 1){
		$("#dpNoteerror2").css("display", "inline");
		$("#dpNoteerror2").html("Please provide note");
		$("#txtRemarks").addClass("highlight");
		return false;
	}

	$("label.process").css("display", "inline");
	$("#btnSave").attr("disabled", true);
	$("#btnSave").html("Wait...");

	$.post("./includes/hrm.php?action=saveNew&dest=addUserNote", { username:username,slcNoteType:slcNoteType,txtRemarks:txtRemarks,noteDate:noteDate }, function(data) {

		var addedpos = data.search("added");
		$("label.process").css("display", "none");
		if(addedpos==0) {
			swal({
				title: "Success!",
				text: "You updated selected record successfully",
				icon: "success",
						timer: 1500,
						button: false
				})
					.then(value=> {
				location.reload();
			}
			);
		} else {
			swal("Oow!", data, "error");
		}
	});

	return false;
}
//
function removeUserNote(recID) {

	swal({
		title: "Are you sure?",
		text: "You going to delete this user note!",
		icon: "warning",
		dangerMode:true,
		buttons: ["Cancel","Yes, delete it!"]
	})
	.then(willDelete=> {
		if(willDelete) {
			$.post("./includes/hrm.php?action=deleteRec&dest=delUserNote", { recID:recID}, function(data) {
				if(data=="deleted") {
					swal({
						title: "Success!",
						text: "Selected record has been deleted successfully.",
						icon: "success",
							timer: 1500,
							button: false
					})
						.then(value=> {
						location.reload();
						}
					);
				} else {
					swal("Oops", data, "error");
				}
			});
		}
	});
return false;
}
//
function chngSalesTell(userID,salesTell) {
	swal({
		title: "Edit Sales Tell",
		text: 'Give user saless tell',
		content: "input",
		buttons:["Cancel","Save"]
	})
	.then(newSalesTell=> {
		if (newSalesTell) {
			$.post("./includes/hrm.php?action=updateExist&dest=chngSalesTell", { userID:userID,newSalesTell:newSalesTell }, function(data) {

				if(data=="updated") {
					swal({
						title: "Success!",
						text: "You updated selected record successfully",
						icon: "success",
							timer: 1500,
							button: false
					})
						.then(value=> {
						location.reload();
					}
					);
				} else {
					swal("Oow!", data, "error");
				}
			});
		}
	});
	return false;
}
//
function delStamp(recID) {
	swal({
		title: "Are you sure?",
		text: "You going to delete this stamp\n and also delete attached files!",
		icon: "warning",
		dangerMode:true,
		buttons: ["Cancel","Yes, delete it!"]
	})
	.then(willDelete=> {
		if(willDelete) {
			$.post("./includes/hrm.php?action=deleteRec&dest=delStamp", { recID:recID}, function(data) {
				if(data=="deleted") {
					swal({
						title: "Success!",
						text: "Selected attachment has been deleted successfully.",
						icon: "success",
							timer: 1500,
							button: false
					})
						.then(value=> {
						location.reload();
						}
					);
				} else {
					swal("Oops", data, "error");
				}
			});
		}
		return false;
	});
}
function catchEmployee4User(empID,empname,email) {
	$("#empID").val(empID);
	$("input#txtEmployee").val(empname);
	$("p#pCName").html(empname);
	$("p#pCPhone").html(email);
	$("div.displayInfo.client").css("display", "inline");
	$("div.suggest.client").css("display", "none");
}

function check_addEmpHistory() {
	clearErrors();
	var empID 			= $("#empID").val();
	var txtJobTitle 	= $("#txtJobTitle").val();
	var slcJobType 		= $("#slcJobType").val();
	var slcOffice 		= $("#slcOffice").val();
	var transDate 		= $("#transDate").val();
	var txtSalary 		= $("#txtSalary").val();
	var txtAllowance 	= $("#txtAllowance").val();
	var department 		= $("#slscDept").val();
	var txtSkill 		= $("#txtSkill").val();
	var txtTitle 		= $("#txtTitle").val();


	if(txtJobTitle.length < 1) {
		$("#histryErr1").css("display", "inline");
		$("#histryErr1").html("the new title is required");
		$("#txtJobTitle").addClass("highlight");
		return false;
	}
	if(slcOffice == undefined) {
		$("#histryErr2").css("display", "inline");
		$("#histryErr2").html("Select an Office");
		$("#slcOffice").addClass("highlight");
		return false;
	}
	if(txtSalary.length < 1) {
		$("#histryErr3").css("display", "inline");
		$("#histryErr3").html("Salary is required");
		$("#txtSalary").addClass("highlight");
		return false;
	}
	$(".btnAdd.history").attr("disabled", true);
	$(".btnAdd.history").html("Wait.");

	$.post("./includes/hrm.php?action=saveNew&dest=addEmpHistoryP", {department:department,txtSkill:txtSkill,txtTitle:txtTitle,empID:empID, slcOffice:slcOffice, txtJobTitle:txtJobTitle, slcJobType:slcJobType, transDate:transDate, txtSalary:txtSalary, txtAllowance:txtAllowance }, function(data) {
		var addedpos = data.search("added");
		$("label.process").css("display", "none");
		if(addedpos==0) {
			var lastID = data.slice(6,21);
			swal({
				title: "Success!",
				text: "You updated employee history successfully",
				icon: "success",
						timer: 1500,
						button: false
				})
					.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=hrm&subtask=employees\
					&action=showInfo&recID="+empID;
				}
			);
		} else {
			swal("Oow!", data, "error");
		}
		$(".btnAdd.history").attr("disabled", false);
		$(".btnAdd.history").html("Add");
	});
	return false;
}
function simplyAddEmployee() {
	/*autosize(document.querySelectorAll('textarea'));
	$("#txtRemarks").keyup(function(){
		return tweetFeed(this, 1000);
	});*/
	//
	$('.datepicker').each(function(index, element) {
		new Pikaday({
			field: element,
			firstDay: 6,
		});
	});
	//
	$('.btnAdd.editEmpHistory').click(function() {
		clearErrors();
		var histID			= $(this).parentsUntil(".content").find("[name='histID']").val();
		var txtJobTitle		= $(this).parentsUntil(".content").find("[name='txtJobTitle']").val();
		var slcJobType		= $(this).parentsUntil(".content").find("[name='slcJobType']").val();
		var slcOffice		= $(this).parentsUntil(".content").find("[name='slcOffice']").val();
		var joinedDate		= $(this).parentsUntil(".content").find("[name='joinedDate']").val();
		var txtSalary		= $(this).parentsUntil(".content").find("[name='txtSalary']").val();
		var txtAllowance	= $(this).parentsUntil(".content").find("[name='txtAllowance']").val();

		var department		= $(this).parentsUntil(".content").find("[name='slscDept']").val();
		var deduction		= $(this).parentsUntil(".content").find("[name='txtDeduction']").val();
		var txtTitle		= $(this).parentsUntil(".content").find("[name='txtTitle']").val();
		var txtSkill		= $(this).parentsUntil(".content").find("[name='txtSkill']").val();
		var txtBonus		= $(this).parentsUntil(".content").find("[name='txtBonus']").val();

		if(txtJobTitle.length < 1) {
			$(this).parentsUntil(".content").find("label.jobTitle").css("display", "inline");
			$(this).parentsUntil(".content").find("label.jobTitle").html("the new title is required");
			$(this).parentsUntil(".content").find("input[name='txtJobTitle']").addClass("highlight");
			return false;
		}
		if(slcOffice == undefined) {
			$(this).parentsUntil(".content").find("label.office").css("display", "inline");
			$(this).parentsUntil(".content").find("label.office").html("Select an Office");
			$(this).parentsUntil(".content").find("[name='slcOffice']").addClass("highlight");
			return false;
		}
		if(txtSalary.length < 1) {
			$(this).parentsUntil(".content").find("label.salary").css("display", "inline");
			$(this).parentsUntil(".content").find("label.salary").html("Salary is required");
			$(this).parentsUntil(".content").find("input[name='txtSalary']").addClass("highlight");
			return false;
		}
		var leftDate		= '0000-00-00';
		if($(this).parentsUntil(".content").find("[name='leftDate']").height() != undefined) {
			var leftDate	= $(this).parentsUntil(".content").find("[name='leftDate']").val();
		}
		$(this).html('Wait');
		$(this).attr("disabled", true);
		$.post("./includes/hrm.php?action=updateExist&dest=editEmpHistory", {department:department,txtTitle:txtTitle,txtSkill:txtSkill, histID:histID, slcOffice:slcOffice, txtJobTitle:txtJobTitle, slcJobType:slcJobType, joinedDate:joinedDate, leftDate:leftDate, txtSalary:txtSalary, txtAllowance:txtAllowance }, function(data) {
			if(data=="updated") {
				swal({
					title: "Success!",
					text: "You updated employee history successfully.",
					icon: "success",
						timer: 1500,
						button: false
				})
					.then(value=> {
						location.reload();
					}
				);
			} else {
				swal("Oow!", data, "error");
			}
			$('.btnAdd.editEmpHistory').attr("disabled", false);
			$('.btnAdd.editEmpHistory').html("Save");
		});
		return false;
	});
	$('.btnAdd.attach').click(function() {
		var imageFile		= $("#imageFile").val();
		var details			= $("#details").val();
		if(imageFile.length < 1) {
			$("#dpvalimgerror").css("display", "inline");
			$("#dpvalimgerror").html("Select file");
			$("#imageFile").addClass("highlight");
			return false;
		}
		if(details.length < 1) {
			$("#dpvalrror30").css("display", "inline");
			$("#dpvalrror30").html("file description is required");
			$("#details").addClass("highlight");
			return false;
		}
	});
	$('#bonus').click(function() {
		$("#type").val('bonus');
		$("#head").html('New Bonus For '+Head);
	});
	$('#deduction').click(function() {
		$("#type").val('deduction');
		$("#head").html('New Deduction For '+Head);
	});
	$('.btnAdd.bonus').click(function() {
		clearErrors();
		var empID				= $("#empID").val();
		var txtAmount			= $("#txtAmount").val();
		var txtreason			= $("#txtreason").val();
		var slcApprovals		= $("#slcApprovals").val();
		var approved			= $("#approved").val();
		var txtTheMonth			= $("#txtTheMonth").val();
		if(slcApprovals==null || slcApprovals=="" || slcApprovals.length<1) {
			$("#dpvalerror25").css("display", "inline");
			$("#dpvalerror25").html("Please select who approved this bonus");
			$("#slcApprovals").addClass("highlight");
			return false;
		}

		if(txtTheMonth.length < 1) {
			$("#dpvalerror29").css("display", "inline");
			$("#dpvalerror29").html("please provide month");
			$("#txtTheMonth").addClass("highlight");
			return false;
		}

		if(txtAmount.length < 1) {
			$("#dpvalerror26").css("display", "inline");
			$("#dpvalerror26").html("Amount is required");
			$("#txtAmount").addClass("highlight");
			return false;
		}
		if(txtreason.length < 1) {
			$("#dpvalrror27").css("display", "inline");
			$("#dpvalrror27").html("Reason is required");
			$("#txtreason").addClass("highlight");
			return false;
		}

		/*$.post("./includes/hrm.php?action=saveNew&dest=addBonusDeduct", {empID:empID,txtAmount:txtAmount,txtreason:txtreason,type:type}, function(data) {
			var addedpos = data.search("added");
			$("label.process").css("display", "none");
			if(addedpos==0) {
				var lastID = data.slice(6,21);
				swal({
					title: "Success!",
					text: "You addded new "+type+" successfully",
					type: "success",
					timer: 1500,
					showConfirmButton: false,
				},
					function() {
						window.location = "./workplace.php?role="+myRole+"&task=hrm&subtask=employees\
						&action=showInfo&recID="+empID;
					}
				);
			} else {
				swal("Oow!", data, "error");
			}
			$(".btnAdd.bonus").attr("disabled", false);
			$(".btnAdd.bonus").html("Save");
		});
		return false;*/
	});
	$('.btnAdd.deduct').click(function() {
		clearErrors();
		var empID				= $("#empID").val();
		var txtAmount			= $("#txtAmount2").val();
		var txtreason			= $("#txtreason2").val();
		var type				= "deduction";
		if(txtAmount.length < 1) {
			$("#dpvalerror28").css("display", "inline");
			$("#dpvalerror28").html("Amount is required");
			$("#txtAmount2").addClass("highlight");
			return false;
		}
		if(txtreason.length < 1) {
			$("#dpvalrror29").css("display", "inline");
			$("#dpvalrror29").html("Reason is required");
			$("#txtreason2").addClass("highlight");
			return false;
		}
		$.post("./includes/hrm.php?action=saveNew&dest=addBonusDeduct", {empID:empID,txtAmount:txtAmount,txtreason:txtreason,type:type}, function(data) {
			var addedpos = data.search("added");
			$("label.process").css("display", "none");
			if(addedpos==0) {
				var lastID = data.slice(6,21);
				swal({
					title: "Success!",
					text: "You addded new "+type+" successfully",
					icon: "success",
						timer: 1500,
						button: false
				})
					.then(value=> {
						window.location = "./workplace.php?role="+myRole+"&task=hrm&subtask=employees\
						&action=showInfo&recID="+empID;
					}
				);
			} else {
				swal("Oow!", data, "error");
			}
			$(".btnAdd.bonus").attr("disabled", false);
			$(".btnAdd.bonus").html("Save");
		});
		return false;
	});
}

function simplyEmployeeShow () {
		$("#txtClient").keyup(function() {
		var passedValue = $("#txtClient").val();
		var form = "add";
		if(passedValue != "") {
			$("div.suggest.one").css("display", "table-row");
			$.post("./includes/hrm.php?action=searchRec&dest=pickClient4Task", { passedValue:passedValue, form:form }, function(data) {
				$("div.suggest.one").html(data);
			});
		}
	});

	//

	$("#txtClient4edit").keyup(function() {
		var passedValue = $("#txtClient4edit").val();
		var form = "edit";
		if(passedValue != "") {
			$("div.suggest.two").css("display", "table-row");
			$.post("./includes/hrm.php?action=searchRec&dest=pickClient4Task", { passedValue:passedValue, form:form }, function(data) {
				$("div.suggest.two").html(data);
			});
		}
	});
}

function catchClient4Task(clientID, clientName, clientPhone, form) {

	if(form=="add") {
		$("input#clientID").val(clientID);
		$("input#txtClient").val(clientName);

		var empID4Task = $("input#empID4Task").val();

		$.post("./includes/hrm.php?action=searchRec&dest=getClientService4Task", { clientID:clientID, empID4Task:empID4Task }, function(data) {
			$("select#slcService").html(data);
		});
	}

	if(form=="edit") {
		$("input#clientID4edit").val(clientID);
		$("input#txtClient4edit").val(clientName);

		var empID4Task = $("input#empID4Task4edit").val();

		$.post("./includes/hrm.php?action=searchRec&dest=getClientService4Task", { clientID:clientID, empID4Task:empID4Task }, function(data) {
			$("select#slcService4edit").html(data);
		});
	}


	$("div.suggest").css("display", "none");
}

function check_addEmployeeTask () {
	clearErrors();
	var empID4Task			= $("#empID4Task").val();
	var clientID			= $("#clientID").val();
	var txtClient			= $("#txtClient").val();
	var slcService			= $("#slcService").val();
	var assignedDate		= $("#assignedDate").val();
	var deadlineDate		= $("#deadlineDate").val();
	var txtRemarks			= $("#txtRemarks").val();
	var slcTaskStatus		= $("#slcTaskStatus").val();

	//alert();return false;

	if(empID4Task.length < 1 || clientID.length < 1 || txtClient.length < 1) {
		$("#taskerror1").css("display", "inline");
		$("#taskerror1").html("provide client");
		$("#txtClient").addClass("highlight");
		return false;
	}
	if(slcService=="") {
		$("#taskerror2").css("display", "inline");
		$("#taskerror2").html("select service");
		$("#slcService").addClass("highlight");
		return false;
	}

	$.post("./includes/hrm.php?action=saveNew&dest=addEmployeeTask", {
		empID4Task		:empID4Task,
		clientID		:clientID,
		txtClient		:txtClient,
		slcService		:slcService,
		assignedDate	:assignedDate,
		deadlineDate	:deadlineDate,
		txtRemarks		:txtRemarks,
		slcTaskStatus	:slcTaskStatus
	}, function(data) {
		var addedpos = data.search("added");
		$("label.process").css("display", "none");
		if(addedpos==0) {
			var lastID = data.slice(6,21);
			swal({
				title: "Success!",
				text: "You addded new "+type+" successfully",
				icon: "success",
						timer: 1500,
						button: false
				})
					.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=hrm&subtask=employees\
					&action=showInfo&recID="+empID4Task;
				}
			);
		} else {
			swal("Oow!", data, "error");
		}
		$(".btnAdd.bonus").attr("disabled", false);
		$(".btnAdd.bonus").html("Save");
	});
	return false;
}


function addNewUserNotetYpe() {
	swal({
		title: "Create New Note Type",
		text: 'Please provide user note type',
		content: "input",
		buttons:["Cancel","Save"]
	})
	.then(noteType=> {
		if (noteType) {

			$.post("./includes/hrm.php?action=saveNew&dest=addNewUserNotetYpe", { noteType:noteType }, function(data) {
				var addedpos = data.search("added");
				if(addedpos==0) {
					var lastID = data.slice(6,21);
					swal({
						title: "Success!",
						text: "You added new reason successfully",
						icon: "success",
							timer: 1500,
							button: false
					});
					//
					$.post("./includes/hrm.php?action=get_recInfo&dest=getNoteTypes", { lastID:lastID }, function(data) {
						$("#slcNoteType").html(data);
					});
				} else {
					swal("Oow!", data, "error");
				}
			});
		}
	});
	return false;
}

function simplifyPayrollEdit() {
	$('.salary, .allowance, .bonus, .deduction, .advance').on('keyup', (e) => {
		let row = $(e.target).parents('.listItem.num')
		let salary = parseFloat($(row).find('.salary').val());
		let allowance = parseFloat($(row).find('.allowance').val());
		let bonus = parseFloat($(row).find('.bonus').val());
		let deduction = parseFloat($(row).find('.deduction').val());
		let advance = parseFloat($(row).find('.advance').val());

		if(!salary) salary = 0;
		if(!allowance) allowance = 0;
		if(!bonus) bonus = 0;
		if(!deduction) deduction = 0;
		if(!advance) advance = 0;
		let total = (salary+allowance+bonus-deduction-advance)

		if(total < 0) {
			total = salary;
			$(e.target).val(0)
		}
		$(row).find('.total').val(`$${total}`)
	})
}