function check_searchBox() {
	// search receipt
	$("input#srchRecpt").keyup(function() {
		var passedValue = $("input#srchRecpt").val();
		if(passedValue.val!="") {
			$("div.suggest").css("display", "table-row");
			$.post("./includes/finance.php?action=searchRec&dest=srchRecpt", { passedValue: passedValue, myRole: myRole, myWhouse: myWhouse}, function(data) {
				$("div.suggest").html(data);
			});
		}
	});
	$("input#srchRecpt").blur(function() {
		if($("#srchRecpt").val()=="") {
			$("div.suggest").css("display", "none");
		}
	});
	/*// search payment
	$("input#srchPyment").keyup(function() {
		var passedValue = $("input#srchPyment").val();
		if(passedValue.val!="") {
			$("div.suggest").css("display", "table-row");
			$.post("./includes/finance.php?action=searchRec&dest=srchPyment", { passedValue: passedValue, myRole: myRole, myWhouse: myWhouse }, function(data) {
				$("div.suggest").html(data);
			});
		}
	});
	$("input#srchPyment").blur(function() {
		if($("#srchPyment").val()=="") {
			$("div.suggest").css("display", "none");
		}
	});
	// search payment request
	$("input#srchPymentRequest").keyup(function() {
		var passedValue = $("input#srchPymentRequest").val();
		if(passedValue.val!="") {
			$("div.suggest").css("display", "table-row");
			$.post("./includes/finance.php?action=searchRec&dest=srchPymentRequest", { passedValue: passedValue, myRole: myRole, myWhouse: myWhouse }, function(data) {
				$("div.suggest").html(data);
			});
		}
	});
	$("input#srchPymentRequest").blur(function() {
		if($("#srchPymentRequest").val()=="") {
			$("div.suggest").css("display", "none");
		}
	});*/
	// search other income
	$("input#srchOthrInc").keyup(function() {
		var passedValue = $("input#srchOthrInc").val();
		if(passedValue.val!="") {
			$("div.suggest").css("display", "table-row");
			$.post("./includes/finance.php?action=searchRec&dest=srchOthrInc", { passedValue: passedValue, myRole: myRole, myWhouse: myWhouse}, function(data) {
				$("div.suggest").html(data);
			});
		}
	});
	$("input#srchOthrInc").blur(function() {
		if($("#srchOthrInc").val()=="") {
			$("div.suggest").css("display", "none");
		}
	});
	// search receivable
	$("input#srchRcvble").keyup(function() {
		var passedValue = $("input#srchRcvble").val();
		if(passedValue.val!="") {
			$("div.suggest").css("display", "table-row");
			$.post("./includes/finance.php?action=searchRec&dest=srchRcvble", { passedValue: passedValue, myRole: myRole, myWhouse: myWhouse}, function(data) {
				$("div.suggest").html(data);
			});
		}
	});
	$("input#srchRcvble").blur(function() {
		if($("#srchRcvble").val()=="") {
			$("div.suggest").css("display", "none");
		}
	});
	// search payable
	$("input#srchPyable").keyup(function() {
		var passedValue = $("input#srchPyable").val();
		if(passedValue.val!="") {
			$("div.suggest").css("display", "table-row");
			$.post("./includes/finance.php?action=searchRec&dest=srchPyable", { passedValue: passedValue, myRole: myRole, myWhouse: myWhouse }, function(data) {
				$("div.suggest").html(data);
			});
		}
	});
	$("input#srchPyable").blur(function() {
		if($("#srchPyable").val()=="") {
			$("div.suggest").css("display", "none");
		}
	});
	// search share
	$("input#srchShare").keyup(function() {
		var passedValue = $("input#srchShare").val();
		if(passedValue.val!="") {
			$("div.suggest").css("display", "table-row");
			$.post("./includes/finance.php?action=searchRec&dest=srchShare", { passedValue: passedValue, myRole: myRole, myWhouse: myWhouse }, function(data) {
				$("div.suggest").html(data);
			});
		}
	});
	$("input#srchShare").blur(function() {
		if($("#srchShare").val()=="") {
			$("div.suggest").css("display", "none");
		}
	});
	// search asset
	$("input#srchAsset").keyup(function() {
		var passedValue = $("input#srchAsset").val();
		if(passedValue.val!="") {
			$("div.suggest").css("display", "table-row");
			$.post("./includes/finance.php?action=searchRec&dest=srchAsset", { passedValue: passedValue, myRole: myRole, myWhouse: myWhouse }, function(data) {
				$("div.suggest").html(data);
			});
		}
	});
	$("input#srchAsset").blur(function() {
		if($("#srchAsset").val()=="") {
			$("div.suggest").css("display", "none");
		}
	});
	// search prepaid account
	$("input#srchPaidAcc").keyup(function() {
		var passedValue = $("input#srchPaidAcc").val();
		if(passedValue.val!="") {
			$("div.suggest").css("display", "table-row");
			$.post("./includes/finance.php?action=searchRec&dest=srchPaidAcc", { passedValue: passedValue, myRole: myRole, myWhouse: myWhouse }, function(data) {
				$("div.suggest").html(data);
			});
		}
	});
	$("input#srchPaidAcc").blur(function() {
		if($("#srchPaidAcc").val()=="") {
			$("div.suggest").css("display", "none");
		}
	});

	//srchRvnueAcc

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

/* ---------------------------------------------------------------
---------------------------- receipts ----------------------------
-----------------------------------------------------------------*/
function catchRecpt(recID) {
	$.post("./includes/finance.php?action=get_recInfo&dest=getRecpt", { recID:recID, myRole:myRole }, function(data) {
		$("div#foundNum").html('<label id="count">1</label> record has been selected.<span id="refresh"></span>');
		$("div#dataTable").html(data);
	});
	$("div.suggest").css("display", "none");
}
function updateRecptBar() {
	var perPage = $("#slcPerPage").val();
	var period = $("#slcPeriod").val();
	var count = $("label#count").html();
	var dataString = 'myRole='+ myRole+'&myWhouse='+ myWhouse;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=setFoundNum&dest=receipts",
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			if(result != count){
				$("#refresh").html('records changed, \
				<a onclick="return loadRecptsTwo(0,'+perPage+',\''+period+'\');">refresh now!</a>');
				(function blink(){
					$("span#refresh a").fadeOut(500).fadeIn(500, blink);
				})();
			}
			setTimeout(function(){
				updateRecptBar();
			}, 60000);
		}
	});
}
function loadRecpts(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&period='+ period+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=loadTable&dest=allRecpts&myRole="+myRole+"&myWhouse="+myWhouse,
		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 loadRecptsTwo(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&period='+ period+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=loadTable&dest=allRecpts&myRole="+myRole+"&myBranch="+myBranch+"&myWhouse="+myWhouse,
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			$("#dataTable div#loading").css("display", "none");
			$(".load_more_link").addClass('noneLink');
			$("#dataTable").html('<div id="record" class="header"><div class="cave button">Action</div><div class="cave middle">RV Number</div><div class="cave long">Department Name</div><div class="cave lshort">Amount</div><div class="cave middle">Inventry Order</div></div>');
			$("#dataTable").append(result);

			(function (){
				//
				var dataString = 'myRole='+ myRole+'&myWhouse='+ myWhouse;
				$.ajax({
					type: "POST",
					url: "./includes/finance.php?action=setFoundNum&dest=receipts",
					data: dataString,
					cache: false,
					timeout: 2000,
					success: function(numRecs){
						$("#foundNum").html('<label id="count">'+numRecs+'</label> record(s) has been found\
						<span id="refresh"></span>');
					}
				});
				//
			})();
		},
	});
	return false;
}
function smpfyAddRecpt() {
	$("input#txtDepart").keyup(function () {
		var departName = $("#txtDepart").val();
		if(departName!="") {
			$("div.suggest").css("display", "table-row");
			$.post("./includes/finance.php?action=searchRec&dest=pickDepart4Recpt", { departName:departName, myWhouse:myWhouse }, function(data) {
				$("div.suggest").html(data);
			});
		} else { $("div.suggest").css("display", "none"); }
		clearErrors();
	});
	//
	$("#slcBankID").change(function() {
		var bankID = $("#slcBankID").val();
		if(bankID=="") {
			$("#slcBankAcc").html('');
		} else {
			$.post("./includes/finance.php?action=searchRec&dest=allBankAccs", { bankID: bankID, myWhouse:myWhouse }, function(data) {
				$("#slcBankAcc").html(data);
			});
		}
	});
	//
	$("#slcBankIDTo").change(function() {
		var bankID = $("#slcBankIDTo").val();
		if(bankID=="") {
			$("#slcBankAccTo").html('');
		} else {
			$.post("./includes/finance.php?action=searchRec&dest=allBankAccs", { bankID: bankID, myWhouse:myWhouse }, function(data) {
				$("#slcBankAccTo").html(data);
			});
		}
	});
	$("#txtAmount").keyup(function() {
		inWords($("#txtAmount").val());
	});
	$("#txtAmount").change(function() {
		inWords($("#txtAmount").val());
	});
	//
	$("#txtRemarks").keyup(function(){
		return tweetFeed(this, 500);
	});
	//
	var kahor1sano = Number(sanadkan)-1;
	var picker = new Pikaday({
		field: document.getElementById("transDate"),
		firstDay: 6,
		maxDate: new Date(maanta),
		yearRange: [kahor1sano,sanadkan],
	});
}
function catchDepartment4Rcpt(departID, departName, salesNo, totalAmnt, balance) {
	$("#departID").val(departID);
	$("#salesNo").val(salesNo);
	$("p#pONumber").html('#'+salesNo);
	$("#txtDepart").val(departName);
	$("#txtAmount").val(balance);
	inWords($("#txtAmount").val());
	$("#txtPaidBy").val(departName);
	$("p#pOAmount").html('$'+totalAmnt);
	$("#totalDue").val(balance);
	$("p#pOBalance").html('$'+balance);
	$("div.displayInfo").css("display", "inline");
	$("div.suggest").css("display", "none");
}
function check_addReceipt() {
	var departID	= $("#departID").val();
	var salesNo		= $("#salesNo").val();
	var txtDepart	= $("#txtDepart").val();
	var totalDue	= parseFloat($("#totalDue").val());

	var txtAmount	= $("#txtAmount").val();
	var txtInWords	= $("#txtInWords").val();

	var slcBankID	= $("#slcBankID").val();
	var slcBankAcc	= $("#slcBankAcc").val();
	var slcBankIDTo	= $("#slcBankIDTo").val();
	var slcBankAccTo	= $("#slcBankAccTo").val();

	var txtPaidBy	= $("#txtPaidBy").val();
	var txtRemarks	= $("#txtRemarks").val();
	var transDate	= $("#transDate").val();

	if(departID.length < 1 || salesNo.length < 1 || txtDepart.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Please find and select department");
		$("#txtDepart").css("border-color", "#FF0000");
		$("div.suggest").css("display", "none");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtAmount.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Amount value is missing");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) <= 0) {
		$("#error2").css("display", "inline");
		$("#error2").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else if(parseFloat(txtAmount) > parseFloat(totalDue)) {
		$("#error2").css("display", "inline");
		$("#error2").html("Amount can't be more than $"+totalDue);
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankID.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Please select bank");
		$("#slcBankID").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankAcc=="---" || slcBankAcc.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Select bank account");
		$("#slcBankAcc").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankIDTo.length < 1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Please select bank");
		$("#slcBankIDTo").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three .column.right").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankAccTo=="---" || slcBankAccTo.length < 1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Select bank account");
		$("#slcBankAccTo").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three .column.right").offset().top }, 'slow'); }
		return false;
	} else if(slcBankAcc==slcBankAccTo) {
		$("#error4").css("display", "inline");
		$("#error4").html("Select different accounts");
		$("#slcBankAccTo").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three .column.right").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtPaidBy.length < 1) {
		$("#error5").css("display", "inline");
		$("#error5").html("This field can't be empty");
		$("#txtPaidBy").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

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

	$.post("./includes/finance.php?action=saveNew&dest=addReceipt&addedBy="+myUser, { myWhouse:myWhouse, departID:departID, salesNo:salesNo, txtAmount:txtAmount, txtInWords:txtInWords, slcBankID:slcBankID, slcBankAcc:slcBankAcc, slcBankIDTo:slcBankIDTo, slcBankAccTo:slcBankAccTo, txtPaidBy:txtPaidBy, 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 collected receipt from restaurant successfully.",
				icon: "success",
					timer: 1500,
					button: false
				})
					.then(value=> {
					var params = '&task=finance&subtask=receipts&action=showInfo&recID='+lastID;
					window.location = "./workplace.php?role="+myRole+params;
				}
			);
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnSave").attr("disabled", false);
		$("#btnSave").html("Save");
	});
	return false;
}
function check_editReceipt() {
	var receiptID	= $("#receiptID").val();
	var orderNo		= $("#orderNo").val();
	var totalDue	= parseFloat($("#totalDue").val());
	var status		= $("#status").val();

	var txtAmount	= $("#txtAmount").val();
	var txtInWords	= $("#txtInWords").val();

	var slcBankID	= $("#slcBankID").val();
	var slcBankAcc	= $("#slcBankAcc").val();
	var txtCheque	= $("#txtCheque").val();

	var txtPaidBy	= $("#txtPaidBy").val();
	var txtRemarks	= $("#txtRemarks").val();

	if(orderNo.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Order number is missing");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(status=="returned") {
		if(txtAmount.length < 1) {
			$("#error2").css("display", "inline");
			$("#error2").html("Amount value is missing");
			$("#txtAmount").css("border-color", "#FF0000");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }
	} else {
		if(txtAmount.length < 1) {
			$("#error2").css("display", "inline");
			$("#error2").html("Amount value is missing");
			$("#txtAmount").css("border-color", "#FF0000");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
			return false;
		} else if(Number(txtAmount) < 0.1) {
			$("#error2").css("display", "inline");
			$("#error2").html("Amount must be greater than 0");
			$("#txtAmount").css("border-color", "#FF0000");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
			return false;
		} else if(parseFloat(txtAmount) > parseFloat(totalDue)) {
			$("#error2").css("display", "inline");
			$("#error2").html("Amount can't be more than $"+totalDue);
			$("#txtAmount").css("border-color", "#FF0000");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }
	}

	if(slcBankID.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Please select bank");
		$("#slcBankID").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankAcc=="---" || slcBankAcc.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Select bank account");
		$("#slcBankAcc").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtPaidBy.length < 1) {
		$("#error4").css("display", "inline");
		$("#error4").html("This field can't be empty");
		$("#txtPaidBy").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

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

	$.post("./includes/finance.php?action=updateExist&dest=editReceipt&updatedBy="+myUser, { receiptID:receiptID, orderNo:orderNo, txtAmount:txtAmount, txtInWords:txtInWords, slcBankID:slcBankID, slcBankAcc:slcBankAcc, txtCheque:txtCheque, txtPaidBy:txtPaidBy, txtRemarks:txtRemarks }, function(data) {
		$("label.process").css("display", "none");
		if(data=="updated") {
			swal({
				title: "Success!",
				text: "You updated receipt record successfully.",
				icon: "success",
				timer: 1500,
				button: false
			})
				.then(value=> {
					var params = '&task=finance&subtask=receipts&action=showInfo&recID='+receiptID;
					window.location = "./workplace.php?role="+myRole+params;
				}
			);
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnUpdate").attr("disabled", false);
		$("#btnUpdate").html("Update");
	});
	return false;
}


/* ---------------------------------------------------------------
-------------------------- other income --------------------------
-----------------------------------------------------------------*/
function updateOthrIncBar() {
	var perPage = $("#slcPerPage").val();
	var period = $("#slcPeriod").val();
	var count = $("label#count").html();
	var dataString = 'myRole='+ myRole+'&myWhouse='+ myWhouse;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=setFoundNum&dest=othrIncome",
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			if(result != count){
				console.log(result);
				$("#refresh").html('records changed, \
				<a onclick="return loadOthrIncTwo(0,'+perPage+',\''+period+'\');">refresh now!</a>');
				(function blink(){
					$("span#refresh a").fadeOut(500).fadeIn(500, blink);
				})();
			}
			setTimeout(function(){
				updateOthrIncBar();
			}, 60000);
		}
	});
}
function loadOthrInc(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+pageLimit+'&period='+period+'&perPage='+perPage+'&myWhouse='+myWhouse;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=loadTable&dest=allOthrInc&myRole="+myRole,
		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 loadOthrIncTwo(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+pageLimit+'&period='+period+'&perPage='+perPage+'&myWhouse='+myWhouse;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=loadTable&dest=allOthrInc&myRole="+myRole,
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			$("#dataTable div#loading").css("display", "none");
			$(".load_more_link").addClass('noneLink');
			$("#dataTable").html('<div id="record" class="header"><div class="cave button">Action</div><div class="cave middle">RV Number</div><div class="cave lmiddle">Received From</div><div class="cave lmiddle">Collected For</div><div class="cave lshort">Amount</div></div>');
			$("#dataTable").append(result);

			(function (){
				//
				var dataString = 'myRole='+myRole+'&myWhouse='+myWhouse;
				$.ajax({
					type: "POST",
					url: "./includes/finance.php?action=loadTable&dest=setFoundNum&dest=othrIncome",
					data: dataString,
					cache: false,
					timeout: 2000,
					success: function(numRecs){
						$("#foundNum").html('<label id="count">'+numRecs+'</label> record(s) has been found\
						<span id="refresh"></span>');
					}
				});
				//
			})();
		},
	});
	return false;
}
function catchOthrInc(recID) {
	$.post("./includes/finance.php?action=get_recInfo&dest=getOthrIncome", { recID:recID, myRole:myRole }, function(data) {
		$("div#foundNum").html('<label id="count">1</label> record has been selected.<span id="refresh"></span>');
		$("div#dataTable").html(data);
	});
	$("div.suggest").css("display", "none");
}
function simplifyFnceAdd() {
	$("#slcBankID").change(function() {
		var bankID = $("#slcBankID").val();
		if(bankID=="") {
			$("#slcBankAcc").html('');
		} else {
			$.post("./includes/finance.php?action=searchRec&dest=myBankAccs", { bankID: bankID, myWhouse:myWhouse }, function(data) {
				$("#slcBankAcc").html(data);
			});
		}
	});
	$("#txtAmount").keyup(function() {
		inWords($("#txtAmount").val());
	});
	$("#txtAmount").change(function() {
		inWords($("#txtAmount").val());
	});
	//
	$("#txtRemarks").keyup(function(){
		return tweetFeed(this, 500);
	});
	//
	$(".btnAdd.prepaid").click(function() {
		var accName 		= $("#txtaccount").val();
		var accCode 		= $("#txtcode").val();
		if(accName.length < 1) {
			$("#dpvalerror1").css("display", "inline");
			$("#dpvalerror1").html("Account name is required");
			$("#txtaccount").addClass("highlight");
			return false;
		}

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

		$.post("./includes/finance.php?action=saveNew&dest=addPrepaidPop&addedBy="+myUser, { myWhouse:myWhouse,accName:accName, accCode:accCode }, function(data) {
		var addedpos = data.search("added");
		if(addedpos==0) {
			var lastID = data.slice(6,21);
			//
			if(accCode==""){accCode="N/A";}
			$("input#accID").val(lastID);
			$("input#txtPrpaidAcc").val(accName);
			$("p#pCode").html(accCode);
			$("p#pAmount, p#pBalance, p#pDrtion").html('$0');
			$("div.displayInfo").css("display", "inline");
			$("#lean_overlay").fadeOut(200);
			$("div.popupContainer").css("display","none");
			swal({
				title: "Success!",
				text: "You added new prepaid account successfully.",
				icon: "success",
				timer: 1500,
				button: false
			});
			$("#accName").val("");
			$("#accCode").val("");
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnAdd").attr("disabled", false);
		$("#btnAdd").html("Add");
		$("#newAccounts").prop("checked", false);
		$("label.process").css("display", "none");
	});
	});
	$(".btnAdd.financial").click(function() {
		var accName 		= $("#txtaccount").val();
		var accCode 		= $("#txtcode").val();
		var accType 		= $("#txtAccType").val();
		if(accName.length < 1) {
			$("#dpvalerror1").css("display", "inline");
			$("#dpvalerror1").html("Account name is required");
			$("#txtaccount").addClass("highlight");
			return false;
		}

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

		$.post("./includes/finance.php?action=saveNew&dest=addFnAcc&addedBy="+myUser, { accType:accType,accName:accName, accCode:accCode}, 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 "+accType+" account successfully.",
					icon: "success",
					timer: 1500,
					button: false,
				});
				$("#btnSave").attr("disabled", false);
				$("#btnSave").html("Save");
				$("#newAccounts").prop("checked", false);
				$.post("./includes/finance.php?action=searchRec&dest=pickFnAcc",{lastID:lastID,accType:accType}, function(data) {
				if(accType==='expense'){$("#slcExpense").append(data);}
				else{$("#slcIncAcc").append(data);}
				$("#txtaccount").val("");
				$("#txtcode").val("");
				$("#txtAccType").val("");

			});
			} else {
				swal("Oow!", data, "error");
			}
		});
		return false;
	});
	var kahor1sano = Number(sanadkan)-1;
	var picker = new Pikaday({
		field: document.getElementById("transDate"),
		firstDay: 6,
		//maxDate: new Date(maanta),
		yearRange: [kahor1sano,sanadkan],
	});
}
function check_addOthrInc() {
	var slcIncAcc	= $("#slcIncAcc").val();
	var slcBankID	= $("#slcBankID").val();
	var slcBankAcc	= $("#slcBankAcc").val();
	var txtCheque	= $("#txtCheque").val();
	var txtReceived	= $("#txtReceived").val();
	var txtAmount	= $("#txtAmount").val();
	var txtInWords	= $("#txtInWords").val();
	var txtRemarks	= $("#txtRemarks").val();
	var transDate	= $("#transDate").val();

	if(slcIncAcc.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Please select income account");
		$("#slcIncAcc").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankID.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Please select bank");
		$("#slcBankID").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankAcc=="---" || slcBankAcc.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Select bank account");
		$("#slcBankAcc").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtReceived.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("This field can't be empty");
		$("#txtReceived").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtAmount.length < 1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Amount value is missing");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 0.1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

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

	$.post("./includes/finance.php?action=saveNew&dest=addOtherIncome&addedBy="+myUser, { myWhouse:myWhouse, slcIncAcc:slcIncAcc, slcBankID:slcBankID, slcBankAcc:slcBankAcc, txtCheque:txtCheque, txtReceived:txtReceived,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 other income record successfully.",
				icon: "success",
				timer: 1500,
				button: false
			})
				.then(value=> {
					var params = '&task=finance&subtask=othrIncome&action=showInfo&recID='+lastID;
					window.location = "./workplace.php?role="+myRole+params;
				}
			);
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnSave").attr("disabled", false);
		$("#btnSave").html("Save");
	});
	return false;
}
function check_editOthrInc() {
	var receiptID	= $("#receiptID").val();
	var slcIncAcc	= $("#slcIncAcc").val();
	var slcBankID	= $("#slcBankID").val();
	var slcBankAcc	= $("#slcBankAcc").val();
	var txtCheque	= $("#txtCheque").val();
	var txtReceived	= $("#txtReceived").val();
	var txtAmount	= $("#txtAmount").val();
	var txtInWords	= $("#txtInWords").val();
	var txtRemarks	= $("#txtRemarks").val();

	if(slcIncAcc.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Please select income account");
		$("#slcIncAcc").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankID.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Please select bank");
		$("#slcBankID").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankAcc=="---" || slcBankAcc.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Select bank account");
		$("#slcBankAcc").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtReceived.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("This field can't be empty");
		$("#txtReceived").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtAmount.length < 1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Amount value is missing");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 0.1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

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

	$.post("./includes/finance.php?action=updateExist&dest=editOtherIncome&updatedBy="+myUser, { receiptID:receiptID, slcIncAcc:slcIncAcc, slcBankID:slcBankID, slcBankAcc:slcBankAcc, txtCheque:txtCheque, txtReceived:txtReceived,txtAmount:txtAmount, txtInWords:txtInWords, txtRemarks:txtRemarks }, function(data) {
		$("label.process").css("display", "none");
		if(data=="updated") {
			swal({
				title: "Success!",
				text: "You updated other income record successfully.",
				icon: "success",
				timer: 1500,
				button: false
			})
				.then(value=> {
					var params = '&task=finance&subtask=othrIncome&action=showInfo&recID='+receiptID;
					window.location = "./workplace.php?role="+myRole+params;
				}
			);
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnUpdate").attr("disabled", false);
		$("#btnUpdate").html("Update");
	});
	return false;
}


/* ---------------------------------------------------------------
---------------------------- payments ----------------------------
-----------------------------------------------------------------*/
function updatePaymentBar() {
	var perPage = $("#slcPerPage").val();
	var period = $("#slcPeriod").val();
	var count = $("label#count").html();
	var dataString = 'myRole='+ myRole+'&myWhouse='+ myWhouse;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=setFoundNum&dest=payments",
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			if(result != count){
				$("#refresh").html('records changed, \
				<a onclick="return loadPaymentsTwo(0,'+perPage+',\''+period+'\');">refresh now!</a>');
				(function blink(){
					$("span#refresh a").fadeOut(500).fadeIn(500, blink);
				})();
			}
			setTimeout(function(){
				updatePaymentBar();
			}, 60000);
		}
	});
}
function loadPayments(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&period='+ period+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=loadTable&dest=allPayments&myRole="+myRole+"&myWhouse="+myWhouse,
		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 loadPaymentsTwo(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&period='+ period+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/functions.php?action=loadTable&dest=allPayments&myRole="+myRole+"&myWhouse="+myWhouse,
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			$("#dataTable div#loading").css("display", "none");
			$(".load_more_link").addClass('noneLink');
			$("#dataTable").html('<div id="record" class="header"><div class="cave button">Action</div><div class="cave middle">PV Number</div><div class="cave long">Paid To</div><div class="cave lshort">Amount</div><div class="cave middle">Account Details</div></div>');
			$("#dataTable").append(result);

			(function (){
				//
				var dataString = 'myRole='+ myRole+'&myWhouse='+ myWhouse;
				$.ajax({
					type: "POST",
					url: "./includes/functions.php?action=setFoundNum&dest=payments",
					data: dataString,
					cache: false,
					timeout: 2000,
					success: function(numRecs){
						$("#foundNum").html('<label id="count">'+numRecs+'</label> record(s) has been found\
						<span id="refresh"></span>');
					}
				});
				//
			})();
		},
	});
	return false;
}
//
function loadPaymentRequests(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&period='+ period+'&perPage='+ perPage+'&myUser='+myUser;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=loadTable&dest=allPaymentRequests&myRole="+myRole+"&myWhouse="+myWhouse,
		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 catchPayment(recID) {
	$.post("./includes/finance.php?action=get_recInfo&dest=getPayment", { recID:recID, myRole:myRole }, function(data) {
		$("div#foundNum").html('<label id="count">1</label> record has been selected.<span id="refresh"></span>');
		$("div#dataTable").html(data);
	});
	$("div.suggest").css("display", "none");
}
//
function catchPaymentRequest(recID) {
	$.post("./includes/finance.php?action=get_recInfo&dest=getPaymentRequest", { recID:recID, myRole:myRole }, function(data) {
		$("div#foundNum").html('<label id="count">1</label> record has been selected.<span id="refresh"></span>');
		$("div#dataTable").html(data);
	});
	$("div.suggest").css("display", "none");
}
function smpfyAddPyment() {
	// if expense required
	$("select#slcPaidFor").change(function () {
		$("input#txtAmount").removeAttr("readonly","readonly");
		$("div#divs4Expense, div#divs4Vendor, div#divs4ExtraPrchse, div#divs4Commission").css("display", "none");
		$("div.suggest, div.displayInfo").css("display", "none");
		$("select#slcExpense option:first").attr("selected", "selected");
		$("#vendorID, #prchseRef, #prchseBlnce, #txtVendorName, #prchseID, #prchseEBlnce, #txtPaidTo, #txtPrchseRef, #txtAmount, #txtInWords").val('');
		//
		var slcPaidFor = $("select#slcPaidFor").val();
		if(slcPaidFor=='expense' || slcPaidFor=='restaurant-expense') {
			$("div#divs4Expense").css("display", "inline");
		} else if(slcPaidFor=='vendor') {
			$("div#divs4Vendor").css("display", "inline");
		} else if(slcPaidFor=='commission') {
			$("div#divs4Commission").css("display", "inline");
		} else if(slcPaidFor=='extraPrchse') {
			$("div#divs4ExtraPrchse").css("display", "inline");
		}
	});
	// if vendor name provided
	$("input#txtVendorName").keyup(function () {
		$("#vendorID, #prchseRef, #prchseBlnce, #txtPaidTo").val('');
		$("div.displayInfo.vendor").css("display", "none");
		var vendorName	= $("#txtVendorName").val();
		var pvNumber 	= $("#pvNumber").val();
		if(vendorName!="") {
			$("div.suggest.one").css("display", "table-row");
			$.post("./includes/finance.php?action=searchRec&dest=pickVendor4Pay", { vendorName:vendorName, pvNumber:pvNumber }, function(data) {
				$("div.suggest.one").html(data);
			});
		} else { $("div.suggest.one").css("display", "none"); }

		clearErrors();
	});
	// if vendor name provided
	$("input#txtCommissioner").keyup(function () {
		$("#invoiceID, #txtPaidTo").val('');
		var commissioner	= $("#txtCommissioner").val();

		if(commissioner!="") {
			$("div.suggest.three").css("display", "table-row");
			$.post("./includes/finance.php?action=searchRec&dest=pickCommissioner4Pay", { commissioner:commissioner }, function(data) {
				$("div.suggest.three").html(data);
			});
		} else { $("div.suggest.three").css("display", "none"); }

		clearErrors();
	});
	// if purchase reference provided
	$("input#txtPrchseRef").keyup(function () {
		$("#prchseID, #prchseEBlnce, #txtPaidTo").val('');
		$("div.displayInfo.extraPrchse").css("display", "none");
		var passedVal	= $("#txtPrchseRef").val();
		var pvNumber 	= $("#pvNumber").val();
		if(passedVal!="") {
			$("div.suggest.two").css("display", "table-row");
			$.post("./includes/finance.php?action=searchRec&dest=pickPrchsRefInPay", { passedVal:passedVal, pvNumber:pvNumber}, function(data) {
				$("div.suggest.two").html(data);
			});
		} else { $("div.suggest.two").css("display", "none"); }
		clearErrors();
	});

	//paymentItemDetails
	$(document).on('change', "input[name^='paymentItemDesc'], input[name^='paymentItemQty'], input[name^='paymentItemPrice'], input[name^='paymentItemttl'], input[name^='total']", function(e) {
    	let listItm = $(e.target).parents('.itemRecord')
    	let paymentItemDesc   = $(listItm).find("input[name^='paymentItemDesc']").val();
    	let paymentItemQty    = parseFloat($(listItm).find("input[name^='paymentItemQty']").val())
    	let paymentItemPrice  = parseFloat($(listItm).find("input[name^='paymentItemPrice']").val());

    	if(!paymentItemQty) paymentItemQty = 0;
    	if(!paymentItemPrice) paymentItemPrice = 0;

		let paymentItemttl = (paymentItemQty * paymentItemPrice).toFixed(2);
		let total = (paymentItemttl =+ paymentItemttl).toFixed(2);

    	let txtAmount = parseFloat($("#txtAmount").val());
    	if(!txtAmount) txtAmount = 0;
    	let newAmnt  = (txtAmount + paymentItemttl).toFixed(2);

    	// console.log(txtAmount,newAmnt)
		$("#txtAmount").val(newAmnt)
  		inWords($("#txtAmount").val());

    	$(listItm).find(".paymentItemttl span").html(paymentItemttl);
    	$(listItm).find("input[name^='paymentItemttl']").val(paymentItemttl);

    	$(listItm).find(".total span").html(total);
    	$(listItm).find("input[name^='total']").val(total);

    	$('.record.totals span').html(`$${newAmnt}`)

    	// console.log(paymentItemPrice);
    	let newRow = `<div class="listItem itemRecord  num${paymentItemNo}">
            <div class="cave small" style="display: flex; justify-content: space-between; align-items: center; ">
            	<span>${paymentItemNo}</span>
            	<span class="fa fa-trash" style="margin-right: 15px; cursor: pointer; font-size: 1.5em; color: #606060" onclick="removePaymentDetail(this)"></span>
            </div>
            <div class="cave large" style="position: static;">
                <input type="hidden" name="paymentItemID[]">
                <input type="text" class="srchInput" placeholder="Search item name" name="paymentItemDesc[]">
                <div class="suggest one" style=" width: 25%;margin-left: -10px;"></div>
            </div>
            <div class="cave small">
                  <input type="text" name="paymentItemQty[]">
            </div>
            <div class="cave small">
              <input type="text"  name="paymentItemPrice[]">
            </div>
            <div class="cave paymentItemttl medium">
            	<span></span>
            	<input type="hidden" name="paymentItemttl[]">
            </div>
        </div>`
        if (paymentItemDesc && paymentItemQty && paymentItemPrice) {
        	$('.paymentItems').append(newRow)
        	paymentItemNo++;
        }
        return false
    });

    $(document).on("keyup", "input[name^='paymentItemDesc']", function(e) {
    	let listItm = $(e.target).parents('.itemRecord')
    	let pymentItemDesc   = $(listItm).find("input[name^='paymentItemDesc']").val();
    	$(listItm).find("div.suggest").css("display", "block");
    	$.post("./includes/finance.php?action=searchRec&dest=findPaymentItem",{desc:pymentItemDesc,myUser:myUser, myRole:myRole, myWhouse:myWhouse}, function(data){
    		$(listItm).find("div.suggest").html(data)
    	})
    	return false;
  	});
}
function removePaymentDetail(el){
	let row = $(el).parents('.itemRecord')
	let rowTotal = parseFloat($(row).find("input[name^='paymentItemttl']").val());
	let txtAmount = parseFloat($("#txtAmount").val());

	if(!rowTotal) rowTotal = 0;
	if(!txtAmount) txtAmount = 0;

	let newAmnt  = (txtAmount - rowTotal).toFixed(2);
	$("#txtAmount").val(newAmnt)
	inWords($("#txtAmount").val());
	$(row).fadeOut(500, function(){ $(this).remove(); });
}
function catchItem4paymentDetails(ID = false,desc = false, el){
	let row = $(el).parents('.itemRecord')
	if (ID && desc ) {
		$(row).find("input[name^='paymentItemDesc']").val(desc);
	    $(row).find("input[name^='paymentItemID']").val(ID)
	    $(row).find("div.suggest").css("display", "none");
	} else{
		$(row).find("input[name^='paymentItemDesc']").val(desc);
		$(row).find("div.suggest").css("display", "none");

		$.post("./includes/finance.php?action=saveNew&dest=addPaymentItem&addedBy="+myUser,{item:desc,myUser:myUser, myRole:myRole, myWhouse:myWhouse}, function(data){
    		$(row).find("input[name^='paymentItemID']").val(data)
    	})
	}

	return false

}
function catchVendor4Pay(vndID, fullname, phone, balance) {
	$("input#vendorID").val(vndID);
	$("input#txtVendorName").val(fullname);
	$("input#txtPaidTo").val(fullname);
	$("div.displayInfo.vendor").css("display", "inline");
	$("p#pvndName").html(fullname);
	$("p#pvndPhone").html(phone);
	if(balance>0) {
		$("p#pvndBlnce").html('$'+balance);
	} else {
		$("p#pvndBlnce").html(balance);
	}
	$("div.suggest").css("display", "none");
}
function catchCommissioner4Pay(invocID,commissionerID, commissionerName, phone, balance) {
	$("input#invoiceID").val(invocID);
	$("input#commissionerID").val(commissionerID);
	$("input#txtCommissioner").val(commissionerName);
	$("input#txtPaidTo").val(commissionerName);
	$("input#txtAmount").val(balance);
	$("input#txtAmount").attr("readonly","readonly");
	inWords($("#txtAmount").val());


	$("div.suggest").css("display", "none");
}
function catchExtraPrchs(prchseID,prchaseRef,ttlCost,balance) {
	$("input#prchseID").val(prchseID);
	$("input#prchseEBlnce").val(balance);
	$("input#txtPrchseRef").val(prchseID);
	$("input#txtPaidTo").val('');
	$("div.displayInfo.extraPrchse").css("display", "inline");
	$("p#pERef").html(prchaseRef);
	$("p#pETotal").html('$'+ttlCost);
	$("input#prchseEBlnce").val(balance);
	if(balance>0) {
		$("p#pEBlnce").html('$'+balance);
	} else {
		$("p#pEBlnce").html(balance);
	}
	$("div.suggest.two").css("display", "none");
}
function check_addPaymentRequest() {
	var expenseType 	= $("#slcPaidFor").val();
	var slcExpense		= $("#slcExpense").val();
	var vendorName		= $("#txtVendorName").val();
	var vendorID		= $("#vendorID").val();
	//
	var txtCommissioner		= $("#txtCommissioner").val();
	var commissionerID		= $("#commissionerID").val();
	var invoiceID			= $("#invoiceID").val();
	//
	var txtPrchseRef	= $("#txtPrchseRef").val();
	var prchseID		= $("#prchseID").val();
	var prchseEBlnce	= parseFloat($("#prchseEBlnce").val());

	var txtAmount		= $("#txtAmount").val();
	var txtInWords		= $("#txtInWords").val();
	var slcBankID		= $("#slcBankID").val();
	var slcBankAcc		= $("#slcBankAcc").val();
	var txtCheque		= $("#txtCheque").val();
	var txtPaidTo		= $("#txtPaidTo").val();
	var txtRemarks		= $("#txtRemarks").val();
	var transDate		= $("#transDate").val();

	if(expenseType=="expense" || expenseType=="restaurant-expense") {
		if(slcExpense=="") {
			$("#error1").css("display", "inline");
			$("#error1").html("Please select expense");
			$("#slcExpense").css("border-color", "#FF0000");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }

	} else if(expenseType=="vendor") {
		if(vendorID.length < 1 || vendorName.length < 1) {
			$("#error2").css("display", "inline");
			$("#error2").html("Please select vendor");
			$("#txtVendorName").css("border-color", "#FF0000");
			$("div.suggest").css("display", "none");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }

	} else if(expenseType=="commission") {
		if(invoiceID.length < 1 || commissionerID.length < 1 || txtCommissioner.length < 1) {
			$("#error7").css("display", "inline");
			$("#error7").html("Please select commissioner");
			$("#txtCommissioner").css("border-color", "#FF0000");
			$("div.suggest").css("display", "none");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }

	} else if(expenseType=="extraPrchse") {
		if(prchseID.length < 1 || txtPrchseRef.length < 1) {
			$("#error3").css("display", "inline");
			$("#error3").html("Please select purchase number");
			$("#txtPrchseRef").css("border-color", "#FF0000");
			$("div.suggest").css("display", "none");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }
	}

	if(txtAmount.length < 1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Amount value is missing");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 0.1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(expenseType=="extraPrchse") {
		if(Number(txtAmount) > prchseEBlnce) {
			$("#error4").css("display", "inline");
			$("#error4").html("Amount can't be greater than $"+prchseEBlnce);
			$("#txtAmount").css("border-color", "#FF0000");
			if(window.innerWidth<=768) {
				$('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }
	}

	/*if(slcBankID.length < 1) {
		$("#error5").css("display", "inline");
		$("#error5").html("Please select bank");
		$("#slcBankID").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankAcc=="---" || slcBankAcc.length < 1) {
		$("#error5").css("display", "inline");
		$("#error5").html("Select bank account");
		$("#slcBankAcc").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }*/

	if(expenseType!="extraPrchse") {
		if(txtPaidTo.length < 1) {
			$("#error6").css("display", "inline");
			$("#error6").html("This field can't be empty");
			$("#txtPaidTo").css("border-color", "#FF0000");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.five").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }
	}

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

	return true;


	/*$.post("./includes/finance.php?action=saveNew&dest=addPaymentRequest&addedBy="+myUser, { myWhouse:myWhouse, expenseType:expenseType, slcExpense:slcExpense, vendorID:vendorID, prchseID:prchseID, invoiceID:invoiceID, commissionerID:commissionerID, txtAmount:txtAmount, txtInWords:txtInWords, slcBankID:slcBankID, slcBankAcc:slcBankAcc, txtCheque:txtCheque, txtPaidTo:txtPaidTo, 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 payment request record successfully.",
				icon: "success",
				timer: 1500,
				button: false
			})
				.then(value=> {
					location.reload();
				}
			);
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnSave").attr("disabled", false);
		$("#btnSave").html("Save");
	});
	return false;*/
}
//
function delPaymentRequest(payID) {
	swal({
		title: "Are you sure to delete?",
		text: "You going to delete this payment request.",
		icon: "warning",
		dangerMode:true,
		buttons: ["Cancel","Yes, delete it!"]
	})
	.then(willDelete=> {
		if(willDelete) {
			$.post("./includes/finance.php?action=deleteRec&dest=delPaymentRequest",{ payID:payID}, function(data){
				if(data=="deleted") {
					swal({
						title: "Success",
						text: "selected record has been removed permenantly",
						icon: "success",
						timer: 1500,
						button: false
					})
						.then(value=> {
						location.reload();
					});
				} else {
					swal("Oops!", data, "error");
				}
			});
		}
	});
	return false;
}
//
function delPayment(payID) {
	swal({
		title: "Are you sure to delete?",
		text: "You going to delete this payment record.",
		icon: "warning",
		dangerMode:true,
		buttons: ["Cancel","Yes, delete it!"]
	})
	.then(willDelete=> {
		if(willDelete) {
			$.post("./includes/finance.php?action=deleteRec&dest=delPayment",{ payID:payID}, function(data){
				if(data=="deleted") {
					swal({
						title: "Success",
						text: "selected record has been removed permenantly",
						icon: "success",
						timer: 1500,
						button: false
					})
						.then(value=> {
						location.reload();
					});
				} else {
					swal("Oops!", data, "error");
				}
			});
		}
	});
	return false;
}
//
function check_addPayment() {
	var expenseType 	= $("#slcPaidFor").val();
	var slcExpense		= $("#slcExpense").val();
	var vendorName		= $("#txtVendorName").val();
	var vendorID		= $("#vendorID").val();
	//
	var txtCommissioner		= $("#txtCommissioner").val();
	var commissionerID		= $("#commissionerID").val();
	var invoiceID			= $("#invoiceID").val();
	//
	var txtPrchseRef	= $("#txtPrchseRef").val();
	var prchseID		= $("#prchseID").val();
	var prchseEBlnce	= parseFloat($("#prchseEBlnce").val());

	var txtAmount		= $("#txtAmount").val();
	var txtInWords		= $("#txtInWords").val();
	var slcBankID		= $("#slcBankID").val();
	var slcBankAcc		= $("#slcBankAcc").val();
	var txtCheque		= $("#txtCheque").val();
	var txtPaidTo		= $("#txtPaidTo").val();
	var txtRemarks		= $("#txtRemarks").val();
	var transDate		= $("#transDate").val();

	var payID		= $("#payID").val();

	if(expenseType=="expense") {
		if(slcExpense=="") {
			$("#error1").css("display", "inline");
			$("#error1").html("Please select expense");
			$("#slcExpense").css("border-color", "#FF0000");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }

	} else if(expenseType=="vendor") {
		if(vendorID.length < 1 || vendorName.length < 1) {
			$("#error2").css("display", "inline");
			$("#error2").html("Please select vendor");
			$("#txtVendorName").css("border-color", "#FF0000");
			$("div.suggest").css("display", "none");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }

	} else if(expenseType=="commission") {
		if(invoiceID.length < 1 || commissionerID.length < 1 || txtCommissioner.length < 1) {
			$("#error7").css("display", "inline");
			$("#error7").html("Please select commissioner");
			$("#txtCommissioner").css("border-color", "#FF0000");
			$("div.suggest").css("display", "none");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }

	} else if(expenseType=="extraPrchse") {
		if(prchseID.length < 1 || txtPrchseRef.length < 1) {
			$("#error3").css("display", "inline");
			$("#error3").html("Please select purchase number");
			$("#txtPrchseRef").css("border-color", "#FF0000");
			$("div.suggest").css("display", "none");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }
	}

	if(txtAmount.length < 1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Amount value is missing");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 0.1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(expenseType=="extraPrchse") {
		if(Number(txtAmount) > prchseEBlnce) {
			$("#error4").css("display", "inline");
			$("#error4").html("Amount can't be greater than $"+prchseEBlnce);
			$("#txtAmount").css("border-color", "#FF0000");
			if(window.innerWidth<=768) {
				$('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }
	}

	if(slcBankID.length < 1) {
		$("#error5").css("display", "inline");
		$("#error5").html("Please select bank");
		$("#slcBankID").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankAcc=="---" || slcBankAcc.length < 1) {
		$("#error5").css("display", "inline");
		$("#error5").html("Select bank account");
		$("#slcBankAcc").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(expenseType!="extraPrchse") {
		if(txtPaidTo.length < 1) {
			$("#error6").css("display", "inline");
			$("#error6").html("This field can't be empty");
			$("#txtPaidTo").css("border-color", "#FF0000");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.five").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }
	}

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

	return true;

	/*$.post("./includes/finance.php?action=saveNew&dest=addPayment&addedBy="+myUser, { payID:payID,myWhouse:myWhouse, expenseType:expenseType, slcExpense:slcExpense, vendorID:vendorID, prchseID:prchseID, invoiceID:invoiceID, commissionerID:commissionerID, txtAmount:txtAmount, txtInWords:txtInWords, slcBankID:slcBankID, slcBankAcc:slcBankAcc, txtCheque:txtCheque, txtPaidTo:txtPaidTo, 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 payment record successfully.",
				icon: "success",
				timer: 1500,
				button: false
			})
				.then(value=> {
					var params = '&task=finance&subtask=payments&action=showInfo&recID='+lastID;
					window.location = "./workplace.php?role="+myRole+params;
				}
			);
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnSave").attr("disabled", false);
		$("#btnSave").html("Save");
	});
	return false;*/
}
function check_editPayment() {
	var payID			= $("#payID").val();
	var pvNumber		= $("#pvNumber").val();
	var expenseType 	= $("#slcPaidFor").val();
	var slcExpense		= $("#slcExpense").val();
	var vendorName		= $("#txtVendorName").val();
	var vendorID		= $("#vendorID").val();
	//
	var txtPrchseRef	= $("#txtPrchseRef").val();
	var prchseID		= $("#prchseID").val();
	var prchseEBlnce	= parseFloat($("#prchseEBlnce").val());

	var txtAmount		= $("#txtAmount").val();
	var txtInWords		= $("#txtInWords").val();
	var slcBankID		= $("#slcBankID").val();
	var slcBankAcc		= $("#slcBankAcc").val();
	var txtCheque		= $("#txtCheque").val();
	var txtPaidTo		= $("#txtPaidTo").val();
	var txtRemarks		= $("#txtRemarks").val();
	var transDate		= $("#transDate").val();

	if(expenseType=="expense") {
		if(slcExpense=="") {
			$("#error1").css("display", "inline");
			$("#error1").html("Please select expense");
			$("#slcExpense").css("border-color", "#FF0000");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }

	} else if(expenseType=="vendor") {
		if(vendorID.length < 1 || vendorName.length < 1) {
			$("#error2").css("display", "inline");
			$("#error2").html("Please select vendor");
			$("#txtVendorName").css("border-color", "#FF0000");
			$("div.suggest").css("display", "none");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }

	} else if(expenseType=="extraPrchse") {
		if(prchseID.length < 1 || txtPrchseRef.length < 1) {
			$("#error3").css("display", "inline");
			$("#error3").html("Please select purchase number");
			$("#txtPrchseRef").css("border-color", "#FF0000");
			$("div.suggest").css("display", "none");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }
	}

	if(txtAmount.length < 1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Amount value is missing");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else if(expenseType!="vendor") {
		if(Number(txtAmount) < 0.1) {
			$("#error4").css("display", "inline");
			$("#error4").html("Amount must be greater than 0");
			$("#txtAmount").css("border-color", "#FF0000");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }
	}

	if(expenseType=="extraPrchse") {
		if(Number(txtAmount) > prchseEBlnce) {
			$("#error4").css("display", "inline");
			$("#error4").html("Amount can't be greater than $"+prchseEBlnce);
			$("#txtAmount").css("border-color", "#FF0000");
			if(window.innerWidth<=768) {
				$('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }
	}

	if(slcBankID.length < 1) {
		$("#error5").css("display", "inline");
		$("#error5").html("Please select bank");
		$("#slcBankID").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankAcc=="---" || slcBankAcc.length < 1) {
		$("#error5").css("display", "inline");
		$("#error5").html("Select bank account");
		$("#slcBankAcc").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(expenseType!="extraPrchse") {
		if(txtPaidTo.length < 1) {
			$("#error6").css("display", "inline");
			$("#error6").html("This field can't be empty");
			$("#txtPaidTo").css("border-color", "#FF0000");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.five").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }
	}

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

	$.post("./includes/finance.php?action=updateExist&dest=editPayment&updatedBy="+myUser, { payID:payID, expenseType:expenseType, slcExpense:slcExpense, vendorID:vendorID, prchseID:prchseID, txtAmount:txtAmount, txtInWords:txtInWords, slcBankID:slcBankID, slcBankAcc:slcBankAcc, txtCheque:txtCheque, txtPaidTo:txtPaidTo, txtRemarks:txtRemarks }, function(data) {
		$("label.process").css("display", "none");
		if(data=="updated") {
			swal({
				title: "Success!",
				text: "You updated payment record successfully.",
				icon: "success",
				timer: 1500,
				button: false
			})
				.then(value=> {
					var params = '&task=finance&subtask=payments&action=showInfo&recID='+payID;
					window.location = "./workplace.php?role="+myRole+params;
				}
			);
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnUpdate").attr("disabled", false);
		$("#btnUpdate").html("Update");
	});
	return false;
}


/* ---------------------------------------------------------------
------------------------ Acc/ receivable  ------------------------
-----------------------------------------------------------------*/

function updateARBar() {
	var perPage = $("#slcPerPage").val();
	var period = $("#slcPeriod").val();
	var count = $("label#count").html();
	var dataString = 'myRole='+ myRole+'&myWhouse='+ myWhouse;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=setFoundNum&dest=receivable",
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			if(result != count){
				$("#refresh").html('records changed, \
				<a onclick="return loadRecvbleTwo(0,'+perPage+',\''+period+'\');">refresh now!</a>');
				(function blink(){
					$("span#refresh a").fadeOut(500).fadeIn(500, blink);
				})();
			}
			setTimeout(function(){
				updateARBar();
			}, 60000);
		}
	});
}
function loadRecvble(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&period='+ period+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=loadTable&dest=allRcvable&myRole="+myRole+"&myWhouse="+myWhouse,
		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 loadRecvbleTwo(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&period='+ period+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=loadTable&dest=allRcvable&myRole="+myRole+"&myWhouse="+myWhouse,
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			$("#dataTable div#loading").css("display", "none");
			$(".load_more_link").addClass('noneLink');
			$("#dataTable").html('<div id="record" class="header"><div class="cave button">Action</div><div class="cave middle">TR Number</div><div class="cave long">Customer</div><div class="cave middle">Account Details</div><div class="cave lshort">Amount</div></div>');
			$("#dataTable").append(result);

			(function (){
				//
				var dataString = 'myRole='+ myRole+'&myWhouse='+ myWhouse;
				$.ajax({
					type: "POST",
					url: "./includes/finance.php?action=setFoundNum&dest=receivable",
					data: dataString,
					cache: false,
					timeout: 2000,
					success: function(numRecs){
						$("#foundNum").html('<label id="count">'+numRecs+'</label> record(s) has been found\
						<span id="refresh"></span>');
					}
				});
				//
			})();
		},
	});
	return false;
}
function addClient() {
	var popCName = $("#popCName").val();
	var popCPhone = $("#popCPhone").val();
	var popCEmail = $("#popCEmail").val();

	if(popCName.length < 1) {
		$("#clientErr1").css("display", "inline");
		$("#clientErr1").html("Please type full name");
		$("#popCName").css("border-color", "#FF0000");
		return false;
	} else { clearErrors(); }

	if(popCPhone.length < 1) {
		$("#clientErr2").css("display", "inline");
		$("#clientErr2").html("Don't forget phone number");
		$("#popCPhone").css("border-color", "#FF0000");
		return false;
	} else { clearErrors(); }

	if(popCEmail.length > 0) {
		if(!popCEmail.match(emailExp)) {
			$("#clientErr3").css("display", "inline");
			$("#clientErr3").html("invalid email address");
			$("#popCEmail").css("border-color", "#FF0000");
			return false;
		}
	} else { clearErrors(); }

	$("#btnAdd").attr("disabled", true);
	$("#btnAdd").html("wait...");
	$("div.suggest.one").css("display", "none");

	$.post("./includes/finance.php?action=saveNew&dest=addClient&addedBy="+myUser, { cname:popCName, cphone:popCPhone, cemail:popCEmail, myWhouse:myWhouse }, function(data) {
		var addedpos = data.search("added");
		if(addedpos==0) {
			var lastID = data.slice(6,21);
			//
			$("input#clientID").val(lastID);
			$("input#txtClient").val(popCName);
			$("p#pCName").html(popCName);
			$("p#pCPhone").html(popCPhone);
			$("p#pCBalance").html(0);
			$("div.displayInfo.client").css("display", "inline");
			$("#clientForm").attr("checked",false);
			$("div.popupContainer").css("display","none");
			swal({
				title: "Success!",
				text: "You added new customer successfully.",
				icon: "success",
				timer: 1500,
				button: false
			});
			$("#popCName, #popCPhone, #popCEmail").val("");
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnAdd").attr("disabled", false);
		$("#btnAdd").html("Add");
	});
	return false;
}
function search_client(page) {
	var txtClient = $("#txtClient").val();
	if(txtClient!="") {
		$("div.suggest.client").css("display", "table-row");
		$.post("./includes/finance.php?action=searchRec&dest=pickClient",{ passedValue:txtClient, myRole:myRole, myWhouse:myWhouse, page:page }, function(data) {
			$("div.suggest.client").html(data);
		});
	} else { $("div.suggest.client").css("display", "none"); }
	clearErrors();
}
function catchClient4form(clientID,clientName,phone,balance) {
	$("#clientID").val(clientID);
	$("p#pCName").html(clientName);
	$("#txtClient").val(clientName);
	$("p#pCPhone").html(phone);
	$("p#pCBalance").html('$'+balance);
	$("div.displayInfo.client").css("display", "inline");
	$("div.suggest.client").css("display", "none");
}
function simplifyAddRecvble() {
	// receive button
	$("#checkBlnce, #switchBalance").click(function(){
		$('div#switchBalance').toggleClass("switchOn");
		if (document.getElementById("beginBalance").checked == true) {
			document.getElementById("beginBalance").checked = false;
		} else {
			document.getElementById("beginBalance").checked = true;
			$("#slcBankID option:first").attr("selected", "selected");
			$("#slcBankAcc").html('');
		}
	});
	//
	$("#btnARReceive").click(function() {
		var actionType = "receive";
		insert_receivable(actionType); return false;
	});
	// pay button
	$("#btnARPay").click(function() {
		var actionType = "pay";
		insert_receivable(actionType); return false;
	});
}
function insert_receivable(actionType) {
	var clientID	= $("#clientID").val();
	var txtClient	= $("#txtClient").val();
	var transDate	= $("#transDate").val();
	var slcPayTerms	= $("#slcPayTerms").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(clientID.length < 1 || txtClient.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Provide customer name");
		$("#txtClient").css("border-color", "#FF0000");
		$("div.suggest").css("display", "none");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankID.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Please select bank");
		$("#slcBankID").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(beginBalance=="no") {
	if(slcBankID.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Please select bank");
		$("#slcBankID").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankAcc=="---" || slcBankAcc.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Select bank account");
		$("#slcBankAcc").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }
  }
	if(txtAmount.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Type amount");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 0.1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	$("label.process").css("display", "inline");
	if(actionType=="receive") {
		$("#btnARReceive").attr("disabled", true); $("#btnARReceive").html("Wait...");
	} else if(actionType=="pay") {
		$("#btnARPay").attr("disabled", true); $("#btnARPay").html("Wait...");
	}

	$.post("./includes/finance.php?action=saveNew&dest=addRcvable&addedBy="+myUser, { transType:actionType, myWhouse:myWhouse, clientID:clientID, clientName:txtClient, transDate:transDate, slcPayTerms:slcPayTerms, slcBankID:slcBankID, slcBankAcc:slcBankAcc, txtCheque:txtCheque, txtAmount:txtAmount, txtInWords:txtInWords, 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 record into account receivable.",
				icon: "success",
				timer: 1500,
				button: false
			})
				.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=finance&subtask=receivable\
					&type="+actionType+"&action=showInfo&recID="+lastID;
				}
			);
		} else if(data=="dueBalance<amount") {
			swal("Oops!", "The amount received from "+txtClient+" \n is less than amount you just typed in.", "error");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		} else {
			swal("Oops!", data, "error");
		}
		if(actionType=="receive") {
			$("#btnARReceive").attr("disabled", false); $("#btnARReceive").html("Receive");
		} else if(actionType=="pay") {
			$("#btnARPay").attr("disabled", false); $("#btnARPay").html("Pay");
		}

	});
	return false;
}
function check_editRcvble() {
	var changedID	= $("#changedID").val();
	var transType	= $("#transType").val();

	var clientID	= $("#clientID").val();
	var txtClient	= $("p#pCName").html();
	var slcPayTerms	= $("#slcPayTerms").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(clientID.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Provide customer name");
		$("#txtClient").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankID.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Please select bank");
		$("#slcBankID").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankAcc=="---" || slcBankAcc.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Select bank account");
		$("#slcBankAcc").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtAmount.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Type amount");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 0.1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

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

	$.post("./includes/finance.php?action=updateExist&dest=editRcvable&updatedBy="+myUser, { changedID:changedID, transType:transType, clientID:clientID, clientName:txtClient, slcPayTerms:slcPayTerms, slcBankID:slcBankID, slcBankAcc:slcBankAcc, txtCheque:txtCheque, txtAmount:txtAmount, txtInWords:txtInWords, txtRemarks:txtRemarks }, function(data) {
		$("label.process").css("display", "none");
		if(data=="updated") {
			swal({
				title: "Success!",
				text: "You updated account receivable details successfully.",
				icon: "success",
				timer: 1500,
				button: false
			})
				.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=finance&subtask=receivable\
					&type="+transType+"&action=showInfo&recID="+changedID;
				}
			);
		} else if(data=="dueBalance<amount") {
			swal("Oops!", "The amount received from "+txtClient+" \n is less than amount you just typed in.", "error");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnUpdate").attr("disabled", false);
		$("#btnUpdate").html("Update");

	});
	return false;
}
function catchRecvble(recID) {
	$.post("./includes/finance.php?action=get_recInfo&dest=getRecvble", { recID:recID, myRole:myRole }, function(data) {
		$("div#foundNum").html('<label id="count">1</label> record has been selected.<span id="refresh"></span>');
		$("div#dataTable").html(data);
	});
	$("div.suggest").css("display", "none");
}

/* ---------------------------------------------------------------
-------------------------- Acc/ payable  -------------------------
-----------------------------------------------------------------*/
function updateAPBar() {
	var perPage = $("#slcPerPage").val();
	var period = $("#slcPeriod").val();
	var count = $("label#count").html();
	var dataString = 'myRole='+ myRole+'&myWhouse='+ myWhouse;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=setFoundNum&dest=payable",
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			if(result != count){
				$("#refresh").html('records changed, \
				<a onclick="return loadPyableTwo(0,'+perPage+',\''+period+'\');">refresh now!</a>');
				(function blink(){
					$("span#refresh a").fadeOut(500).fadeIn(500, blink);
				})();
			}
			setTimeout(function(){
				updateAPBar();
			}, 60000);
		}
	});
}
function loadPyable(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&period='+ period+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=loadTable&dest=allPayble&myRole="+myRole+"&myWhouse="+myWhouse,
		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 loadPyableTwo(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&period='+ period+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=loadTable&dest=allPayble&myRole="+myRole+"&myWhouse="+myWhouse,
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			$("#dataTable div#loading").css("display", "none");
			$(".load_more_link").addClass('noneLink');
			$("#dataTable").html('<div id="record" class="header"><div class="cave button">Action</div><div class="cave middle">TR Number</div><div class="cave long">Customer</div><div class="cave middle">Account Details</div><div class="cave lshort">Amount</div></div>');
			$("#dataTable").append(result);

			(function (){
				//
				var dataString = 'myRole='+ myRole+'&myWhouse='+ myWhouse;
				$.ajax({
					type: "POST",
					url: "./includes/finance.php?action=setFoundNum&dest=payable",
					data: dataString,
					cache: false,
					timeout: 2000,
					success: function(numRecs){
						$("#foundNum").html('<label id="count">'+numRecs+'</label> record(s) has been found\
						<span id="refresh"></span>');
					}
				});
				//
			})();
		},
	});
	return false;
}
function simplifyAddPyble() {
	// receive button
	$("#checkBlnce, #switchBalance").click(function(){
		$('div#switchBalance').toggleClass("switchOn");
		if (document.getElementById("beginBalance").checked == true) {
			document.getElementById("beginBalance").checked = false;
		} else {
			document.getElementById("beginBalance").checked = true;
			$("#slcBankID option:first").attr("selected", "selected");
			$("#slcBankAcc").html('');
		}
	});
	//
	$("#btnARReceive").click(function() {
		var actionType = "receive";
		insert_payable(actionType); return false;
	});
	// pay button
	$("#btnARPay").click(function() {
		var actionType = "pay";
		insert_payable(actionType); return false;
	});
}
function insert_payable(actionType) {

	var slcPayTerms	= $("#slcPayTerms").val();
	var beginBalance	= $("input#beginBalance:checked").val();
	if(beginBalance!="on") { var beginBalance="no"; } else { var beginBalance="yes"; }

	var clientID	= $("#clientID").val();
	var txtClient	= $("#txtClient").val();
	var transDate	= $("#transDate").val();
	var slcPayTerms	= $("#slcPayTerms").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(beginBalance=="yes" && actionType=="pay") {
		clearErrors();
		swal("Oops!", "Only receive action can be used for begin balance", "error");
		return false;
	}

	if(clientID.length < 1 || txtClient.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Provide customer name");
		$("#txtClient").css("border-color", "#FF0000");
		$("div.suggest").css("display", "none");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }
	if(beginBalance=="no") {
	if(slcBankID.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Please select bank");
		$("#slcBankID").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankAcc=="---" || slcBankAcc.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Select bank account");
		$("#slcBankAcc").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }
 }
	if(txtAmount.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Type amount");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 0.1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	$("label.process").css("display", "inline");
	if(actionType=="receive") {
		$("#btnARReceive").attr("disabled", true); $("#btnARReceive").html("Wait...");
	} else if(actionType=="pay") {
		$("#btnARPay").attr("disabled", true); $("#btnARPay").html("Wait...");
	}

	$.post("./includes/finance.php?action=saveNew&dest=addPyble&addedBy="+myUser, { transType:actionType, myWhouse:myWhouse, clientID:clientID, clientName:txtClient, transDate:transDate, slcPayTerms:slcPayTerms, slcBankID:slcBankID, slcBankAcc:slcBankAcc, txtCheque:txtCheque, txtAmount:txtAmount, txtInWords:txtInWords, 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 record into account payable.",
				icon: "success",
				timer: 1500,
				button: false
			})
				.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=finance&subtask=payable\
					&type="+actionType+"&action=showInfo&recID="+lastID;
				}
			);
		} else if(data=="dueBalance<amount") {
			swal("Oops!", "The amount received from "+txtClient+" \n is less than amount you just typed in.", "error");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		} else {
			swal("Oops!", data, "error");
		}
		if(actionType=="receive") {
			$("#btnARReceive").attr("disabled", false); $("#btnARReceive").html("Receive");
		} else if(actionType=="pay") {
			$("#btnARPay").attr("disabled", false); $("#btnARPay").html("Pay");
		}

	});
	return false;
}
function check_editPyable() {
	var changedID	= $("#changedID").val();
	var transType	= $("#transType").val();

	var clientID	= $("#clientID").val();
	var txtClient	= $("p#pCName").html();
	var slcPayTerms	= $("#slcPayTerms").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(clientID.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Provide customer name");
		$("#txtClient").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankID.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Please select bank");
		$("#slcBankID").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankAcc=="---" || slcBankAcc.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Select bank account");
		$("#slcBankAcc").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtAmount.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Type amount");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 0.1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

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

	$.post("./includes/finance.php?action=updateExist&dest=editPyable&updatedBy="+myUser, { changedID:changedID, transType:transType, clientID:clientID, clientName:txtClient, slcPayTerms:slcPayTerms, slcBankID:slcBankID, slcBankAcc:slcBankAcc, txtCheque:txtCheque, txtAmount:txtAmount, txtInWords:txtInWords, txtRemarks:txtRemarks }, function(data) {
		$("label.process").css("display", "none");
		if(data=="updated") {
			swal({
				title: "Success!",
				text: "You updated account payable details successfully.",
				icon: "success",
				timer: 1500,
				button: false
			})
				.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=finance&subtask=payable\
					&type="+transType+"&action=showInfo&recID="+changedID;
				}
			);
		} else if(data=="dueBalance<amount") {
			swal("Oops!", "The amount received from "+txtClient+" \n is less than amount you just typed in.", "error");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnUpdate").attr("disabled", false);
		$("#btnUpdate").html("Update");

	});
	return false;
}
function catchPyable(recID) {
	$.post("./includes/finance.php?action=get_recInfo&dest=getPyable", { recID:recID, myRole:myRole }, function(data) {
		$("div#foundNum").html('<label id="count">1</label> record has been selected.<span id="refresh"></span>');
		$("div#dataTable").html(data);
	});
	$("div.suggest").css("display", "none");
}


/* ---------------------------------------------------------------
--------------------------- share holder  ------------------------
-----------------------------------------------------------------*/

function updateShareBar() {
	var perPage = $("#slcPerPage").val();
	var period = $("#slcPeriod").val();
	var count = $("label#count").html();
	var dataString = 'myRole='+ myRole+'&myWhouse='+ myWhouse;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=setFoundNum&dest=shares",
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			if(result != count){
				$("#refresh").html('records changed, \
				<a onclick="return loadSharesTwo(0,'+perPage+',\''+period+'\');">refresh now!</a>');
				(function blink(){
					$("span#refresh a").fadeOut(500).fadeIn(500, blink);
				})();
			}
			setTimeout(function(){
				updateShareBar();
			}, 60000);
		}
	});
}
function loadShares(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&period='+ period+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=loadTable&dest=allShares&myRole="+myRole+"&myWhouse="+myWhouse,
		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 loadSharesTwo(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&period='+ period+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=loadTable&dest=allShares&myRole="+myRole+"&myWhouse="+myWhouse,
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			$("#dataTable div#loading").css("display", "none");
			$(".load_more_link").addClass('noneLink');
			$("#dataTable").html('<div id="record" class="header"><div class="cave button">Action</div><div class="cave middle">TR Number</div><div class="cave long">Shareholder</div><div class="cave middle">Account Details</div><div class="cave lshort">Amount</div></div>');
			$("#dataTable").append(result);

			(function (){
				//
				var dataString = 'myRole='+ myRole+'&myWhouse='+ myWhouse;
				$.ajax({
					type: "POST",
					url: "./includes/finance.php?action=setFoundNum&dest=shares",
					data: dataString,
					cache: false,
					timeout: 2000,
					success: function(numRecs){
						$("#foundNum").html('<label id="count">'+numRecs+'</label> record(s) has been found\
						<span id="refresh"></span>');
					}
				});
				//
			})();
		},
	});
	return false;
}
function addShareHolder() {
	var popName = $("#popName").val();
	var popPhone = $("#popPhone").val();
	var popEmail = $("#popEmail").val();

	if(popName.length < 1) {
		$("#qalad1").css("display", "inline");
		$("#qalad1").html("Please type full name");
		$("#popName").css("border-color", "#FF0000");
		return false;
	} else { clearErrors(); }

	if(popPhone.length < 1) {
		$("#qalad2").css("display", "inline");
		$("#qalad2").html("Don't forget phone number");
		$("#popPhone").css("border-color", "#FF0000");
		return false;
	} else { clearErrors(); }

	if(popEmail.length > 0) {
		if(!popEmail.match(emailExp)) {
			$("#qalad3").css("display", "inline");
			$("#qalad3").html("invalid email address");
			$("#popEmail").css("border-color", "#FF0000");
			return false;
		}
	} else { clearErrors(); }

	$("#btnAdd").attr("disabled", true);
	$("#btnAdd").html("wait...");
	$("div.suggest.one").css("display", "none");

	$.post("./includes/finance.php?action=saveNew&dest=addShareHolder&addedBy="+myUser, { popName:popName, popPhone:popPhone, popEmail:popEmail, myWhouse:myWhouse }, function(data) {
		var addedpos = data.search("added");
		if(addedpos==0) {
			var lastID = data.slice(6,21);
			//
			$("input#shareHID").val(lastID);
			$("input#shareHolder").val(popName);
			$("p#pSName").html(popName);
			$("p#pSPhone").html(popPhone);
			$("div.displayInfo").css("display", "inline");
			$("#lean_overlay").fadeOut(200);
			$("div.popupContainer").css("display","none");
			swal({
				title: "Success!",
				text: "You added new shareholder successfully.",
				icon: "success",
				timer: 1500,
				button: false
			});
			$("#popName").val("");
			$("#popPhone").val("");
			$("#popEmail").val("");
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnAdd").attr("disabled", false);
		$("#btnAdd").html("Add");
	});
	return false;
}
function search_shareholder() {
	var shareHolder = $("#shareHolder").val();
	if(shareHolder!="") {
		$("div.suggest.one").css("display", "table-row");
		$.post("./includes/finance.php?action=searchRec&dest=pickSHolder",{ passedValue:shareHolder, myWhouse:myWhouse,myRole:myRole }, function(data) {
			$("div.suggest.one").html(data);
		});
	} else { $("div.suggest.one").css("display", "none"); }
	clearErrors();
}
function catchShare4form(shID,shareHolder,phone) {
	$("#shareHID").val(shID);
	$("#shareHolder").val(shareHolder);
	$("p#pSName").html(shareHolder);
	$("p#pSPhone").html(phone);
	$("div.displayInfo").css("display", "inline");
	$("div.suggest.one").css("display", "none");
}
function simplifyAddShare() {
	// deposit button
	$("#btnDeposit").click(function() {
		var actionType = "deposit";
		insert_share(actionType); return false;
	});
	// withdraw button
	$("#btnWithdraw").click(function() {
		var actionType = "withdraw";
		insert_share(actionType); return false;
	});
}
function insert_share(actionType) {
	var shareHID	= $("#shareHID").val();
	var shareHolder	= $("#shareHolder").val();
	var transDate	= $("#transDate").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(shareHID.length < 1 || shareHolder.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Provide shareholder name");
		$("#shareHolder").css("border-color", "#FF0000");
		$("div.suggest").css("display", "none");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankID.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Please select bank");
		$("#slcBankID").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankAcc=="---" || slcBankAcc.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Select bank account");
		$("#slcBankAcc").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtAmount.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Type amount");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 0.1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	$("label.process").css("display", "inline");
	if(actionType=="deposit") {
		$("#btnDeposit").attr("disabled", true); $("#btnDeposit").html("Wait...");
	} else if(actionType=="withdraw") {
		$("#btnWithdraw").attr("disabled", true); $("#btnWithdraw").html("Wait...");
	}

	$.post("./includes/finance.php?action=saveNew&dest=addShare&addedBy="+myUser, { transType:actionType, myWhouse:myWhouse, shareHID:shareHID, shareHolder:shareHolder, transDate:transDate, slcBankID:slcBankID, slcBankAcc:slcBankAcc, txtCheque:txtCheque, txtAmount:txtAmount, txtInWords:txtInWords, 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 record as shareholder.",
				icon: "success",
				timer: 1500,
				button: false
			})
				.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=finance&subtask=shares\
					&type="+actionType+"&action=showInfo&recID="+lastID;
				}
			);
		} else if(data=="dueBalance<amount") {
			swal("Oops!", "The amount received from "+shareHolder+" \n is less than amount you just typed in.", "error");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		} else {
			swal("Oops!", data, "error");
		}
		if(actionType=="deposit") {
			$("#btnDeposit").attr("disabled", false); $("#btnDeposit").html("Deposit");
		} else if(actionType=="withdraw") {
			$("#btnWithdraw").attr("disabled", false); $("#btnWithdraw").html("Withdraw");
		}
	});
	return false;
}
function check_editShare() {
	var changedID	= $("#changedID").val();
	var transType	= $("#transType").val();

	var shareHID	= $("#shareHID").val();
	var shareHolder	= $("p#pSName").html();
	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(shareHID.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Provide shareholder name");
		$("#txtClient").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankID.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Please select bank");
		$("#slcBankID").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankAcc=="---" || slcBankAcc.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Select bank account");
		$("#slcBankAcc").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtAmount.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Type amount");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 0.1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

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

	$.post("./includes/finance.php?action=updateExist&dest=editShare&updatedBy="+myUser, { changedID:changedID, transType:transType, shareHID:shareHID, shareHolder:shareHolder, slcBankID:slcBankID, slcBankAcc:slcBankAcc, txtCheque:txtCheque, txtAmount:txtAmount, txtInWords:txtInWords, txtRemarks:txtRemarks }, function(data) {
		$("label.process").css("display", "none");
		if(data=="updated") {
			swal({
				title: "Success!",
				text: "You updated share details successfully.",
				icon: "success",
				timer: 1500,
				button: false
			})
				.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=finance&subtask=shares\
					&type="+transType+"&action=showInfo&recID="+changedID;
				}
			);
		} else if(data=="dueBalance<amount") {
			swal("Oops!", "The amount received from "+shareHolder+" \n is less than amount you just typed in.", "error");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnUpdate").attr("disabled", false);
		$("#btnUpdate").html("Update");
	});
	return false;
}
function catchShare(recID) {
	$.post("./includes/finance.php?action=get_recInfo&dest=getShare", { recID:recID, myRole:myRole }, function(data) {
		$("div#foundNum").html('<label id="count">1</label> record has been selected.<span id="refresh"></span>');
		$("div#dataTable").html(data);
	});
	$("div.suggest").css("display", "none");
}


/* ------------------------------------------------------------
------------------------ cash transfer ------------------------
--------------------------------------------------------------*/

function simplifyCshTrnsfer() {
	$("#slcBankID").change(function() {
		var bankID = $("#slcBankID").val();
		if(bankID=="") {
			$("#slcBankAcc").html('');
		} else {
			$.post("./includes/finance.php?action=searchRec&dest=myBankAccs", { bankID: bankID, myWhouse:myWhouse }, function(data) {
				$("#slcBankAcc").html(data);
			});
		}
	});
	//
	$("#slcBankIDTo").change(function() {
		var bankID = $("#slcBankIDTo").val();
		if(bankID=="") {
			$("#slcBankAccTo").html('');
		} else {
			$.post("./includes/finance.php?action=searchRec&dest=myBankAccs", { bankID: bankID, myWhouse:myWhouse }, function(data) {
				$("#slcBankAccTo").html(data);
			});
		}
	});
	$("#txtAmount").keyup(function() {
		inWords($("#txtAmount").val());
	});
	$("#txtAmount").change(function() {
		inWords($("#txtAmount").val());
	});
	//
	$("#txtRemarks").keyup(function(){
		return tweetFeed(this, 500);
	});
	//
	var kahor1sano = Number(sanadkan)-1;
	var picker = new Pikaday({
		field: document.getElementById("transDate"),
		firstDay: 6,
		maxDate: new Date(maanta),
		yearRange: [kahor1sano,sanadkan],
	});
}
function check_trnsfrCash() {
	var bankID1		= $("#slcBankID").val();
	var accNum1		= $("#slcBankAcc").val();
	var bankID2		= $("#slcBankIDTo").val();
	var accNum2		= $("#slcBankAccTo").val();
	var txtAmount	= $("#txtAmount").val();
	//
	var txtCheque	= $("#txtCheque").val();
	var txtRemarks	= $("#txtRemarks").val();
	var transDate	= $("#transDate").val();

	if(bankID1 == "---") {
		$("#error1").css("display", "inline");
		$("#error1").html("Select bank where amount comes from");
		$("#slcBankFrom").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(accNum1 == "---") {
		$("#error1").css("display", "inline");
		$("#error1").html("Select account number");
		$("#slcBankAccFrom").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(bankID2 == "---") {
		$("#error2").css("display", "inline");
		$("#error2").html("Select bank getting this amount");
		$("#slcBankTo").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one .twoCol.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(accNum2 == "---") {
		$("#error2").css("display", "inline");
		$("#error2").html("Select account number");
		$("#slcBankAccTo").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one .twoCol.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(accNum1==accNum2) {
		$("#error2").css("display", "inline");
		$("#error2").html("Select different accounts");
		$("#slcBankAccTo").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one .twoCol.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtAmount.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Type amount value");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

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

	$.post("./includes/finance.php?action=saveNew&dest=trnsferCash&addedBy="+myUser, { myWhouse:myWhouse, bankID1:bankID1, accNum1:accNum1, bankID2:bankID2, accNum2:accNum2, txtAmount:txtAmount, txtCheque:txtCheque, txtRemarks:txtRemarks, transDate:transDate }, function(data) {
		var addedpos = data.search("added");
		$("label.process").css("display", "none");
		if(addedpos==0) {
			var transRef = data.slice(6,50);
			swal({
				title: "Success!",
				text: "You transfered cash successfully.",
				icon: "success",
				timer: 1500,
				button: false
			})
				.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=finance&subtask=cshTrnsfer\
					&action=showInfo&transRef="+transRef;
				}
			);
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnTransfer").attr("disabled", false);
		$("#btnTransfer").html("Transfer");

	});
	return false;
}


/* ------------------------------------------------------------
--------------------------- assets ----------------------------
--------------------------------------------------------------*/
function updateAssetBar() {
	var perPage = $("#slcPerPage").val();
	var period = $("#slcPeriod").val();
	var count = $("label#count").html();
	var dataString = 'myRole='+ myRole+'&myWhouse='+ myWhouse;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=setFoundNum&dest=assets",
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			if(result != count){
				$("#refresh").html('records changed, \
				<a onclick="return loadAssetsTwo(0,'+perPage+',\''+period+'\');">refresh now!</a>');
				(function blink(){
					$("span#refresh a").fadeOut(500).fadeIn(500, blink);
				})();
			}
			setTimeout(function(){
				updateAssetBar();
			}, 60000);
		}
	});
}
function loadAssets(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&period='+ period+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=loadTable&dest=allAssets&myRole="+myRole+"&myWhouse="+myWhouse,
		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 loadAssetsTwo(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&period='+ period+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=loadTable&dest=allAssets&myRole="+myRole+"&myWhouse="+myWhouse,
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			$("#dataTable div#loading").css("display", "none");
			$(".load_more_link").addClass('noneLink');
			$("#dataTable").html('<div id="record" class="header"><div class="cave button">Action</div><div class="cave xlmiddle">Asset</div><div class="cave lshort">Amount</div><div class="cave smiddle">Depreciation</div><div class="cave lshort">Usage</div></div>');
			$("#dataTable").append(result);

			(function (){
				//
				var dataString = 'myRole='+ myRole+'&myWhouse='+ myWhouse;
				$.ajax({
					type: "POST",
					url: "./includes/finance.php?action=setFoundNum&dest=assets",
					data: dataString,
					cache: false,
					timeout: 2000,
					success: function(numRecs){
						$("#foundNum").html('<label id="count">'+numRecs+'</label> record(s) has been found\
						<span id="refresh"></span>');
					}
				});
				//
			})();
		},
	});
	return false;
}
function simplifyAssetAdd() {
	//
	$("#checkLoan, #switchLoan").click(function(){
		$('div#switchLoan').toggleClass("switchOn");
		if(document.getElementById("assetLoan").checked == true) {
			document.getElementById("assetLoan").checked = false;
		} else {
			document.getElementById("assetLoan").checked = true;
			$("select#slcBankID option:first").attr("selected", "selected");
			$("select#slcBankAcc").html('');
			$("input#txtCheque").val('');
		}
	});
	//
	$("#checkBlnce, #switchBalance").click(function(){
		$('div#switchBalance').toggleClass("switchOn");
		if(document.getElementById("beginBalance").checked == true) {
			document.getElementById("beginBalance").checked = false;
		} else {
			document.getElementById("beginBalance").checked = true;
			$("#slcBankID option:first").attr("selected", "selected");
			$("#slcBankAcc").html('');
		}
	});
	//
}
function check_addAsset() {
	var txtClient 	= $("#txtClient").val();
	var clientID 	= $("#clientID").val();

	var assetName 	= $("#assetName").val();
	var assetLoan	= $("input#assetLoan:checked").val();
		if(assetLoan!="on") { var payType="paid"; } else { var payType="loan"; }
	var txtUsage	= $("#txtUsage").val();
	var beginBalance	= $("input#beginBalance:checked").val();
	if(beginBalance!="on") { var beginBalance="no"; } else { var beginBalance="yes"; }

	var txtAmount 	= $("#txtAmount").val();
	var txtInWords	= $("#txtInWords").val();
	var slcBankID	= $("#slcBankID").val();
	var slcBankAcc	= $("#slcBankAcc").val();
	var txtCheque	= $("#txtCheque").val();
	var transDate 	= $("#transDate").val();

	if(clientID.length < 1 || txtClient.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Customer is not selected");
		$("#txtClient").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		$("div.suggest").css("display", "none");
		return false;
	} else { clearErrors(); }

	if(assetName.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Provide asset details");
		$("#assetName").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtUsage.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Asset usage not mentioned");
		$("#txtUsage").css("border-color", "#FF0000");
		if(window.innerWidth<=768) {
			$('html, body').animate({ scrollTop: $(".record.three .twoCol.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtAmount.length < 1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Amount can't be empty");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) {
			$('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 0.1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) {
			$('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(payType=="paid" && beginBalance == "no") {
		if(slcBankID.length < 1) {
			$("#error5").css("display", "inline");
			$("#error5").html("Please select bank");
			$("#slcBankID").css("border-color", "#FF0000");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.five").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }

		if(slcBankAcc=="---" || slcBankAcc.length < 1) {
			$("#error5").css("display", "inline");
			$("#error5").html("Select bank account");
			$("#slcBankAcc").css("border-color", "#FF0000");
			if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.five").offset().top }, 'slow'); }
			return false;
		} else { clearErrors(); }
	}

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

	$.post("./includes/finance.php?action=saveNew&dest=addAsset&addedBy="+myUser, { myWhouse:myWhouse, clientID:clientID, assetName:assetName, payType:payType, txtUsage:txtUsage, beginBalance:beginBalance, txtAmount:txtAmount, txtInWords:txtInWords, slcBankID:slcBankID, slcBankAcc:slcBankAcc, txtCheque:txtCheque, 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 asset record successfully.",
				icon: "success",
				timer: 1500,
				button: false
			})
				.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=finance&subtask=assets\
					&action=showInfo&recID="+lastID;
				}
			);
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnSave").attr("disabled", false);
		$("#btnSave").html("Save");
	});
	return false;
}
function check_editAsset() {
	var astID 		= $("#astID").val();
	var payType		= $("#payType").val();
	var txtClient 	= $("#txtClient").val();
	var clientID 	= $("#clientID").val();

	var assetName 	= $("#assetName").val();
	var txtUsage	= $("#txtUsage").val();
	var txtAmount 	= $("#txtAmount").val();
	var txtInWords	= $("#txtInWords").val();

	if(clientID.length < 1 || txtClient.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Customer is not selected");
		$("#txtClient").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		$("div.suggest").css("display", "none");
		return false;
	} else { clearErrors(); }

	if(assetName.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Provide asset details");
		$("#assetName").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtAmount.length < 1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Amount can't be empty");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) {
			$('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 0.1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) {
			$('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

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

	$.post("./includes/finance.php?action=updateExist&dest=editAsset&updatedBy="+myUser, { astID:astID, payType:payType, clientID:clientID, assetName:assetName, txtAmount:txtAmount, txtInWords:txtInWords, txtUsage:txtUsage }, function(data) {
		$("label.process").css("display", "none");
		if(data=="updated") {
			swal({
				title: "Success!",
				text: "You updated asset record successfully.",
				icon: "success",
				timer: 1500,
				button: false
			})
				.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=finance&subtask=assets\
					&action=showInfo&recID="+astID;
				}
			);
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnUpdate").attr("disabled", false);
		$("#btnUpdate").html("Update");
	});
	return false;
}
function check_depreAsset() {
	var astID		= $("#astID").val();
	var payType		= $("#payType").val();
	var txtAmount	= $("#txtAmount").val();
	var txtInWords	= $("#txtInWords").val();
	var theMonth	= $("#theMonth").val();
	var transDate	= $("#transDate").val();

	if(astID.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Asset record is missing");
		$("#shareHolder").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtAmount.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Type amount");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 0.1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	swal({
		title: "Confirm?",
		text: "You going to make depreciation this month "+theMonth,
		type: "info",
		showCancelButton: true,
		closeOnConfirm: false
	},
	function() {
		$("label.process").css("display", "inline");
		$("#btnSave").attr("disabled", true);
		$("#btnSave").html("Wait...");
		$.post("./includes/finance.php?action=updateExist&dest=addAssetDepr&updatedBy="+myUser, { astID:astID, myWhouse:myWhouse,  payType:payType, txtAmount:txtAmount, txtInWords:txtInWords, theMonth:theMonth, transDate:transDate}, function(data) {
			$("label.process").css("display", "none");
			if(data=="updated") {
				swal({
					title: "Success!",
					text: "You made monthly depreciation for this asset",
					icon: "success",
					timer: 1500,
					button: false
				})
					.then(value=> {
						window.location = "./workplace.php?role="+myRole+"&task=finance&subtask=assets\
						&action=showInfo&recID="+astID;
					}
				);
			} else {
				swal("Oops!", data, "error");
			}
			$("#btnSave").attr("disabled", false);
			$("#btnSave").html("Depreciate");
		});
	});
	return false;
}
function check_payAsset() {
	var astID		= $("#astID").val();
	var astName		= $("#astName").val();
	var clientID	= $("#clientID").val();
	var balance		= parseFloat($("#balance").val());

	var txtAmount	= $("#txtAmount").val();
	var txtInWords	= $("#txtInWords").val();
	var slcBankID	= $("#slcBankID").val();
	var slcBankAcc	= $("#slcBankAcc").val();
	var txtCheque	= $("#txtCheque").val();
	var transDate	= $("#transDate").val();

	if(astID.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Asset record is missing");
		$("#astName").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtAmount.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Amount can't be empty");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 0.1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) > balance) {
		$("#error2").css("display", "inline");
		$("#error2").html("Amount can't be greater than $"+balance);
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankID.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Please select bank");
		$("#slcBankID").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankAcc=="---" || slcBankAcc.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Select bank account");
		$("#slcBankAcc").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	swal({
		title: "Confirm?",
		text: "You going to pay sum of $"+txtAmount+" for this asset",
		type: "info",
		showCancelButton: true,
		closeOnConfirm: false
	},
	function() {
		$("label.process").css("display", "inline");
		$("#btnPay").attr("disabled", true);
		$("#btnPay").html("Wait...");
		$.post("./includes/finance.php?action=saveNew&dest=addAssetPay&addedBy="+myUser, { astID:astID, myWhouse:myWhouse, clientID:clientID, txtAmount:txtAmount, txtInWords:txtInWords, slcBankID:slcBankID, slcBankAcc:slcBankAcc, txtCheque:txtCheque, transDate:transDate}, function(data) {
			$("label.process").css("display", "none");
			var addedpos = data.search("added");
			if(addedpos==0) {
				var lastID = data.slice(6,21);
				swal({
					title: "Success!",
					text: "You successfully made payment for this asset",
					icon: "success",
					timer: 1500,
					button: false
				})
					.then(value=> {
						window.location = "./workplace.php?role="+myRole+"&task=finance&subtask=assets\
						&action=showInfo&recID="+astID;
					}
				);
			} else {
				swal("Oops!", data, "error");
			}
			$("#btnPay").attr("disabled", false);
			$("#btnPay").html("Pay Now");
		});
	});
	return false;
}
function catchAsset(recID) {
	$.post("./includes/finance.php?action=get_recInfo&dest=getAsset", { recID:recID, myUser:myUser, myRole:myRole }, function(data) {
		$("div#foundNum").html('<label id="count">1</label> record has been selected.<span id="refresh"></span>');
		$("div#dataTable").html(data);
	});
	$("div.suggest").css("display", "none");
}


/* ------------------------------------------------------------
---------------------- pre-paid account -----------------------
--------------------------------------------------------------*/
function updatePaidAccBar() {
	var perPage = $("#slcPerPage").val();
	var period = $("#slcPeriod").val();
	var count = $("label#count").html();
	var dataString = 'myRole='+ myRole+'&myWhouse='+ myWhouse;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=setFoundNum&dest=paidAccs",
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			if(result != count){
				$("#refresh").html('records changed, \
				<a onclick="return loadPaidAccsTwo(0,'+perPage+',\''+period+'\');">refresh now!</a>');
				(function blink(){
					$("span#refresh a").fadeOut(500).fadeIn(500, blink);
				})();
			}
			setTimeout(function(){
				updatePaidAccBar();
			}, 60000);
		}
	});
}
function loadPaidAccs(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&period='+ period+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=loadTable&dest=allPaidAccs&myRole="+myRole+"&myWhouse="+myWhouse,
		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 loadPaidAccsTwo(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&period='+ period+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=loadTable&dest=allPaidAccs&myRole="+myRole+"&myWhouse="+myWhouse,
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			$("#dataTable div#loading").css("display", "none");
			$(".load_more_link").addClass('noneLink');
			$("#dataTable").html('<div id="record" class="header"><div class="cave button">Action</div><div class="cave long">Account</div><div class="cave lshort">Amount</div><div class="cave lshort">Balance</div><div class="cave lshort">Duration</div><div class="cave lshort">Status</div></div>');
			$("#dataTable").append(result);

			(function (){
				//
				var dataString = 'myRole='+ myRole+'&myWhouse='+ myWhouse;
				$.ajax({
					type: "POST",
					url: "./includes/finance.php?action=setFoundNum&dest=paidAccs",
					data: dataString,
					cache: false,
					timeout: 2000,
					success: function(numRecs){
						$("#foundNum").html('<label id="count">'+numRecs+'</label> record(s) has been found\
						<span id="refresh"></span>');
					}
				});
				//
			})();
		},
	});
	return false;
}
function search_prepaidAcc() {
	var txtPrpaidAcc = $("#txtPrpaidAcc").val();
	if(txtPrpaidAcc!="") {
		$("div.suggest.one").css("display", "table-row");
		$.post("./includes/finance.php?action=searchRec&dest=pickPrpaidAcc",{ passedValue:txtPrpaidAcc, myRole:myRole, myWhouse:myWhouse }, function(data) {
			$("div.suggest.one").html(data);
		});
	} else { $("div.suggest.one").css("display", "none"); }
	clearErrors();
}
function addPrepaid() {
	var popName = $("#popName").val();
	var popCode = $("#popCode").val();

	if(myWhouse=="all" || myWhouse=="") {
		swal("Oops!", "While you won't work on specific wherehouse, \
		\nyou can't create any prepaid account completely.", "error");
		return false;
	}

	if(popName.length < 1) {
		$("#qalad1").css("display", "inline");
		$("#qalad1").html("Please account name");
		$("#popName").css("border-color", "#FF0000");
		return false;
	} else { clearErrors(); }

	$("#btnAdd").attr("disabled", true);
	$("#btnAdd").html("wait...");
	$("div.suggest.one").css("display", "none");

	$.post("./includes/finance.php?action=saveNew&dest=addPrepaidPop&addedBy="+myUser, { popName:popName, popCode:popCode, myWhouse:myWhouse}, function(data) {
		var addedpos = data.search("added");
		if(addedpos==0) {
			var lastID = data.slice(6,21);
			//
			if(popCode==""){popCode="N/A";}
			$("input#accID").val(lastID);
			$("input#txtPrpaidAcc").val(popName);
			$("p#pCode").html(popCode);
			$("p#pAmount, p#pBalance, p#pDrtion").html(0);
			$("div.displayInfo").css("display", "inline");
			$("#lean_overlay").fadeOut(200);
			$("div.popupContainer").css("display","none");
			swal({
				title: "Success!",
				text: "You added new prepaid account successfully.",
				icon: "success",
				timer: 1500,
				button: false
			});
			$("#popName").val("");
			$("#popCode").val("");
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnAdd").attr("disabled", false);
		$("#btnAdd").html("Add");
	});
	return false;
}
function catchprepaid4form(accID,accName,accCode,amount,balance,duration) {
	$("#accID").val(accID);
	$("#txtPrpaidAcc").val(accName);
	$("p#pCode").html(accCode);
	$("p#pAmount").html('$'+amount);
	$("p#pBalance").html('$'+balance);
	$("p#pDrtion").html(duration);
	$("div.displayInfo").css("display", "inline");
	$("div.suggest.one").css("display", "none");
}
function check_addPrepaid(){
	var accID		= $("#accID").val();
	var txtPrpaidAcc= $("#txtPrpaidAcc").val();
	var transDate	= $("#transDate").val();
	var slcBankID	= $("#slcBankID").val();
	var slcBankAcc	= $("#slcBankAcc").val();
	var txtCheque	= $("#txtCheque").val();
	var txtAmount	= $("#txtAmount").val();
	var txtInWords	= $("#txtInWords").val();
	var txtDration	= $("#txtDration").val();

	if(myWhouse=="all" || myWhouse=="") {
		swal("Oops!", "While you won't work on specific wherehouse, \
		\nyou can't add prepaid payment record completely.", "error");
		return false;
	}

	if(accID.length < 1 || txtPrpaidAcc.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Provide account name");
		$("#shareHolder").css("border-color", "#FF0000");
		$("div.suggest").css("display", "none");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankID.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Please select bank");
		$("#slcBankID").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankAcc=="---" || slcBankAcc.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Select bank account");
		$("#slcBankAcc").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtAmount.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Type amount");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 0.1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtDration.length < 1) {
		$("#error6").css("display", "inline");
		$("#error6").html("Type Duration");
		$("#txtDration").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtDration) < 0.1) {
		$("#error6").css("display", "inline");
		$("#error6").html("Duration must be greater than 0");
		$("#txtDration").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.four").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

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

	$.post("./includes/finance.php?action=saveNew&dest=addPrepaidAcc&addedBy="+myUser, { accID:accID, myWhouse:myWhouse, txtAmount:txtAmount, txtInWords:txtInWords, txtDration:txtDration, transDate:transDate, slcBankID:slcBankID, slcBankAcc:slcBankAcc, txtCheque:txtCheque}, 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 prepaid payment successfully.",
				icon: "success",
					timer: 1500,
					button: false
				})
					.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=finance&subtask=paidAccs\
					&action=showInfo&recID="+accID;
				}
			);
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnSave").attr("disabled", false);
		$("#btnSave").html("Save");
	});
	return false;
}
function check_deprePrepaid(){
	var accID		= $("#accID").val();
	var txtPrpaidAcc= $("#txtPrpaidAcc").val();
	var transDate	= $("#transDate").val();
	var txtAmount	= $("#txtAmount").val();
	var txtInWords	= $("#txtInWords").val();
	var balance		= $("#balance").val();
	var txtRemarks	= $("#txtRemarks").val();
	var theMonth	= $("#theMonth").val();

	if(myWhouse=="all") {
		swal("Oops!", "While you won't work on specific wherehouse, \
		\nyou can't make depreciation for any prepaid account completely.", "error");
		return false;
	}

	if(accID.length < 1 || txtPrpaidAcc.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Provide account name");
		$("#shareHolder").css("border-color", "#FF0000");
		$("div.suggest").css("display", "none");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtAmount.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Type amount");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 0.1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(Number(balance) < Number(txtAmount)) {
		$("#error3").css("display", "inline");
		$("#error3").html("Amount can't be greater than balance");
		$("#txtAmount").css("border-color", "#FF0000");
		return false;
	} else { clearErrors(); }

	swal({
		title: "Confirm?",
		text: "You going to generate expense this month "+theMonth,
		type: "info",
		showCancelButton: true,
		closeOnConfirm: false
	},
	function() {
		$("label.process").css("display", "inline");
		$("#btnSave").attr("disabled", true);
		$("#btnSave").html("Wait...");
		$.post("./includes/finance.php?action=saveNew&dest=addDeprPrepaid&addedBy="+myUser, { accID:accID, myWhouse:myWhouse, txtAmount:txtAmount, txtInWords:txtInWords, transDate:transDate, txtRemarks:txtRemarks, theMonth:theMonth}, function(data) {
			$("label.process").css("display", "none");
			if(data=="depreciated") {
				swal({
					title: "Success!",
					text: "You generated monthly expense for prepaid account "+txtPrpaidAcc,
					icon: "success",
					timer: 1500,
					button: false
				})
					.then(value=> {
						window.location = "./workplace.php?role="+myRole+"&task=finance&subtask=paidAccs\
						&action=showInfo&recID="+accID;
					}
				);
			} else {
				swal("Oops!", data, "error");
			}
			$("#btnSave").attr("disabled", false);
			$("#btnSave").html("Generate Expense");
		});
	});
	return false;
}
function check_editPrepaid() {
	var accID		= $("#accID").val();
	var accName		= $("#accName").val();
	var accCode		= $("#accCode").val();
	var slcStatus	= $("#slcStatus").val();

	if(accID.length < 1 || accName.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Provide account name");
		$("#accName").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

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

	$.post("./includes/finance.php?action=updateExist&dest=editPrepaid&updatedBy="+myUser, { accID:accID, accName:accName, accCode:accCode, slcStatus:slcStatus}, function(data) {
		$("label.process").css("display", "none");
		if(data=="updated") {
			swal({
				title: "Success!",
				text: "You updated prepaid account successfully.",
				icon: "success",
					timer: 1500,
					button: false
				})
					.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=finance&subtask=paidAccs\
					&action=showInfo&recID="+accID;
				}
			);
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnUpdate").attr("disabled", false);
		$("#btnUpdate").html("Update");
	});
	return false;
}
function catchPaidAcc(recID) {
	$.post("./includes/finance.php?action=get_recInfo&dest=getPaidAcc", { recID:recID, myRole:myRole }, function(data) {
		$("div#foundNum").html('<label id="count">1</label> record has been selected.<span id="refresh"></span>');
		$("div#dataTable").html(data);
	});
	$("div.suggest").css("display", "none");
}


/* ------------------------------------------------------------
--------------------- other service account -------------------
--------------------------------------------------------------*/
//
function catchRvnueAcc(recID) {
	$.post("./includes/finance.php?action=get_recInfo&dest=getRvnueAcc", { recID:recID, myRole:myRole }, function(data) {
		$("div#foundNum").html('<label id="count">1</label> record has been selected.<span id="refresh"></span>');
		$("div#dataTable").html(data);
	});
	$("div.suggest").css("display", "none");
}
function updateRvnAccBar() {
	var perPage = $("#slcPerPage").val();
	var period = $("#slcPeriod").val();
	var count = $("label#count").html();
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=setFoundNum&dest=rvnueAcc",
		cache: false,
		timeout: 2000,
		success: function(result){
			if(result != count){
				$("#refresh").html('records changed, \
				<a onclick="return loadRvnuAccTwo(0,'+perPage+',\''+period+'\');">refresh now!</a>');
				(function blink(){
					$("span#refresh a").fadeOut(500).fadeIn(500, blink);
				})();
			}
			setTimeout(function(){
				updateRvnAccBar();
			}, 60000);
		}
	});
}
function loadRvnuAcc(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&period='+ period+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=loadTable&dest=allRvneAcc&myRole="+myRole,
		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 loadRvnuAccTwo(pageLimit, perPage, period) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&period='+ period+'&perPage='+ perPage;
	$.ajax({
		type: "POST",
		url: "./includes/finance.php?action=loadTable&dest=allRvneAcc&myRole="+myRole+"&myWhouse="+myWhouse,
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			$("#dataTable div#loading").css("display", "none");
			$(".load_more_link").addClass('noneLink');
			$("#dataTable").html('<div id="record" class="header"><div class="cave button">Action</div><div class="cave long">Account</div><div class="cave lshort">Amount</div><div class="cave lshort">Balance</div><div class="cave lshort">Duration</div><div class="cave lshort">Status</div></div>');
			$("#dataTable").append(result);

			(function (){
				//
				var dataString = 'myRole='+ myRole+'&myWhouse='+ myWhouse;
				$.ajax({
					type: "POST",
					url: "./includes/finance.php?action=setFoundNum&dest=rvnueAcc",
					data: dataString,
					cache: false,
					timeout: 2000,
					success: function(numRecs){
						$("#foundNum").html('<label id="count">'+numRecs+'</label> record(s) has been found\
						<span id="refresh"></span>');
					}
				});
				//
			})();
		},
	});
	return false;
}
function addRvnueAcc() {
	var popName = $("#popName").val();
	var popCode = $("#popCode").val();

	if(popName.length < 1) {
		$("#qalad1").css("display", "inline");
		$("#qalad1").html("Please account name");
		$("#popName").css("border-color", "#FF0000");
		return false;
	} else { clearErrors(); }

	$("#btnAdd").attr("disabled", true);
	$("#btnAdd").html("wait...");
	$("div.suggest.one").css("display", "none");

	$.post("./includes/finance.php?action=saveNew&dest=addRvnueAcc&addedBy="+myUser, { popName:popName, popCode:popCode }, function(data) {
		var addedpos = data.search("added");
		if(addedpos==0) {
			var lastID = data.slice(6,21);
			//
			if(popCode==""){popCode="N/A";}
			$("input#accID").val(lastID);
			$("input#txtAccName").val(popName);
			$("p#pCode").html(popCode);
			$("p#pAmount, p#pBalance, p#pDrtion").html(0);
			$("div.displayInfo.account").css("display", "inline");
			$("#addRvnue").attr("checked",false);
			$("div.popupContainer").css("display","none");
			swal({
				title: "Success!",
				text: "You added new revenue account successfully.",
				icon: "success",
				timer: 1500,
				button: false
			});
			$("#popName").val("");
			$("#popCode").val("");
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnAdd").attr("disabled", false);
		$("#btnAdd").html("Add");
	});
	return false;
}
function catchRvnAcc4form(accID,accName,accCode,amount,balance,duration) {
	$("#accID").val(accID);
	$("#txtAccName").val(accName);
	$("p#pCode").html(accCode);
	$("p#pAmount").html('$'+amount);
	$("p#pBalance").html('$'+balance);
	$("p#pDrtion").html(duration);
	$("div.displayInfo.account").css("display", "inline");
	$("div.suggest.one").css("display", "none");
}
function simplfyAddRvnueAccnt() {
	$("a.addNew").leanModal({top : 160, overlay : 0.6, closeButton: "span.close" });
	$("#txtAccName").keyup(function() {
		var txtAccName = $("#txtAccName").val();
		if(txtAccName!="") {
			$("div.suggest.one").css("display", "table-row");
			$.post("./includes/finance.php?action=searchRec&dest=pickRvnueAcc",{ passedValue:txtAccName }, function(data) {
				$("div.suggest.one").html(data);
			});
		} else {
			$("div.suggest.one").css("display", "none");
		}
	});
	$("#txtClient").keyup(function(){
		search_client('payable');
	});
}
function check_addRvnueAccnt() {
	var accID		= $("#accID").val();
	var txtAccName	= $("#txtAccName").val();
	var txtAmount	= $("#txtAmount").val();
	var txtInWords	= $("#txtInWords").val();
	var txtDurtn	= $("#txtDurtn").val();
	var txtClient	= $("#txtClient").val();
	var clientID	= $("#clientID").val();
	var transDate	= $("#transDate").val();

	if(accID.length < 1 || txtAccName.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Provide account name");
		$("#txtAccName").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.one").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtAmount.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Type amount");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 0.1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(clientID.length < 1 || txtClient.length < 1) {
		$("#error3").css("display", "inline");
		$("#error3").html("Provide customer name");
		$("#txtClient").css("border-color", "#FF0000");
		$("div.suggest").css("display", "none");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(txtDurtn.length < 1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Type Duration");
		$("#txtDurtn").css("border-color", "#FF0000");
		if(window.innerWidth<=768) {
			$('html, body').animate({ scrollTop: $(".record.three .column.right").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtDurtn) < 0.1) {
		$("#error4").css("display", "inline");
		$("#error4").html("Duration must be greater than 0");
		$("#txtDurtn").css("border-color", "#FF0000");
		if(window.innerWidth<=768) {
			$('html, body').animate({ scrollTop: $(".record.three .column.right").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

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

	$.post("./includes/finance.php?action=saveNew&dest=addRvnueAccnt&addedBy="+myUser, { accID:accID, txtAmount:txtAmount, txtInWords:txtInWords, txtDurtn:txtDurtn, clientID:clientID, 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 revenue account successfully.",
				icon: "success",
					timer: 1500,
					button: false
				})
					.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=finance&subtask=othrSrvce\
					&action=showInfo&recID="+accID;
				}
			);
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnSave").attr("disabled", false);
		$("#btnSave").html("Save");
	});
	return false;
}
function check_editRvnueAccnt() {
	var accID		= $("#accID").val();
	var amount		= $("#pAmount").html();
	var balance		= $("p#pBalance").html();
	var slcStatus	= $("#slcStatus").val();

	if(amount!=balance) {
		swal("Oops!", "This revenue account can't be updated.", "info");
		return false;
	} else { clearErrors(); }

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

	$.post("./includes/finance.php?action=updateExist&dest=editRvnueAcc&updatedBy="+myUser, { accID:accID, slcStatus:slcStatus}, function(data) {
		$("label.process").css("display", "none");
		if(data=="updated") {
			swal({
				title: "Success!",
				text: "You updated prepaid account successfully.",
				icon: "success",
					timer: 1500,
					button: false
				})
					.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=finance&subtask=othrSrvce\
					&action=showInfo&recID="+accID;
				}
			);
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnUpdate").attr("disabled", false);
		$("#btnUpdate").html("Update");
	});
	return false;
}
function check_rcptOthrSrvce() {
	var accID		= $("#accID").val();
	var clientID	= $("#clientID").val();
	var balance		= parseFloat($("#balance").val());

	var txtAmount	= $("#txtAmount").val();
	var txtInWords	= $("#txtInWords").val();
	var slcBankID	= $("#slcBankID").val();
	var slcBankAcc	= $("#slcBankAcc").val();
	var txtCheque	= $("#txtCheque").val();
	var transDate	= $("#transDate").val();

	if(txtAmount.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Amount can't be empty");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) < 0.1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Amount must be greater than 0");
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else if(Number(txtAmount) > balance) {
		$("#error1").css("display", "inline");
		$("#error1").html("Amount can't be greater than $"+balance);
		$("#txtAmount").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.two").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankID.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Please select bank");
		$("#slcBankID").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	if(slcBankAcc=="---" || slcBankAcc.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Select bank account");
		$("#slcBankAcc").css("border-color", "#FF0000");
		if(window.innerWidth<=768) { $('html, body').animate({ scrollTop: $(".record.three").offset().top }, 'slow'); }
		return false;
	} else { clearErrors(); }

	swal({
		title: "Confirm?",
		text: "You going to collect sum of $"+txtAmount+" for this revenue income",
		type: "info",
		showCancelButton: true,
		closeOnConfirm: false
	},
	function() {
		$("label.process").css("display", "inline");
		$("#btnSave").attr("disabled", true);
		$("#btnSave").html("Wait...");
		$.post("./includes/finance.php?action=saveNew&dest=addRcpt4RvnueAcc&addedBy="+myUser, { accID:accID, myWhouse:myWhouse, clientID:clientID, txtAmount:txtAmount, txtInWords:txtInWords, slcBankID:slcBankID, slcBankAcc:slcBankAcc, txtCheque:txtCheque, transDate:transDate }, function(data) {
			$("label.process").css("display", "none");
			var addedpos = data.search("added");
			if(addedpos==0) {
				var lastID = data.slice(6,21);
				swal({
					title: "Success!",
					text: "You successfully collected receipt for this revenue account",
					icon: "success",
					timer: 1500,
					button: false
				})
				.then(value=> {
					window.location = "./workplace.php?role="+myRole+"&task=finance&subtask=othrSrvce\
					&action=showInfo&recID="+accID;
				});
			} else {
				swal("Oops!", data, "error");
			}
			$("#btnSave").attr("disabled", false);
			$("#btnSave").html("Collect Receipt");
		});
	});
	return false;
}



/* --------------------------------------------
------------------ reports -------------------
----------------------------------------------*/
function resetRptFilter() {
	$("div.filter").css("display", "none");
	$("div.filter.byWhouse").css("display", "inline");
	$("div.filter.byDate").css("display", "inline");
}
function resetRptError() {
	$("div.filter input, div.filter select").css("border","1px solid #ccc");
	$("label.error").css("display", "none");
}
function resetError() {
	$("div.reportArea input[type='text'], div.reportArea input[type='month']").css("border-color", "#ccc");
	$("div.reportArea select").css("border-color", "#ccc");
	$("div.reportArea label.error").css("display", "none");
}
function catchClient4Rpt(clientID, clientName) {
	$("#clientID").val(clientID);
	$("#txtClient").val(clientName);
	$("div.suggest").css("display", "none");
}
function catchSupplyItem4Rpt(supItemID, supItemName) {
	$("#itemID").val(supItemID);
	$("#txtSupplyItem").val(supItemName);
	$("div.suggest").css("display", "none");
}
function simplifyFinReport() {
	$("#slcBankID").change(function() {
		var bankID = $("#slcBankID").val();
		if(bankID=="") {
			$("#slcBankAcc").html('');
		} else {
			$.post("./includes/finance.php?action=searchRec&dest=myBankAccs", { bankID: bankID, myWhouse:myWhouse }, function(data) {
				$("#slcBankAcc").html(data);
			});
		}
	});
	//
	$("#txtClient").keyup(function() {
		$("div.suggest").css("display", "none");
		var txtClient = $("#txtClient").val();
		if(txtClient.length > 0) {
			$("div.suggest.one").css("display", "table-row");
			$.post("./includes/finance.php?action=searchRec&dest=pickClient4Rpt",{ passedValue:txtClient, myRole:myRole, myWhouse:myWhouse }, function(data) {
				$("div.suggest.one").html(data);
			});
		} else { $("div.suggest.one").css("display", "none"); }
		clearErrors();
	});
	//
	$("#txtVendor").keyup(function() {
		$("div.suggest").css("display", "none");
		var txtVendor = $("#txtVendor").val();
		if(txtVendor!="") {
			$("div.suggest.two").css("display", "table-row");
			$.post("./includes/inventory.php?action=searchRec&dest=pickVendor4Rpt",{ vendorName:txtVendor}, function(data) {
				$("div.suggest.two").html(data);
			});
		} else { $("div.suggest.two").css("display", "none"); }
		clearErrors();
	});
	//
	$("#txtSupplyItem").keyup(function() {
		$("div.suggest").css("display", "none");
		var txtSupplyItem = $("#txtSupplyItem").val();
		if(txtSupplyItem!="") {
			$("div.suggest.three").css("display", "table-row");
			$.post("./includes/supply.php?action=searchRec&dest=pickSupplyItem4Rpt",{ itemName:txtSupplyItem}, function(data) {
				$("div.suggest.three").html(data);
			});
		} else { $("div.suggest.three").css("display", "none"); }
		clearErrors();
	});
	//
	/*var picker = new Pikaday({
		field: document.getElementById("startDate"),
		firstDay: 6,
		maxDate: new Date(today),
		yearRange: [2017,thisYear],
	});
	var picker2 = new Pikaday({
		field: document.getElementById("endDate"),
		firstDay: 6,
		maxDate: new Date(today),
		yearRange: [2017,thisYear],
	});*/
	var startDate,
    endDate,
    updateStartDate = function() {
		startPicker.setStartRange(startDate);
        endPicker.setStartRange(startDate);
        endPicker.setMinDate(startDate);
    },
    updateEndDate = function() {
		startPicker.setEndRange(endDate);
        startPicker.setMaxDate(endDate);
        endPicker.setEndRange(endDate);
    },
    startPicker = new Pikaday({
		field: document.getElementById("startDate"),
        maxDate: new Date(today),
		yearRange: [2017,thisYear],
        onSelect: function() {
			startDate = this.getDate();
            updateStartDate();
        }
    }),
    endPicker = new Pikaday({
		field: document.getElementById('endDate'),
        minDate: new Date(),
        maxDate: new Date(today),
		yearRange: [2017,thisYear],
        onSelect: function() {
			endDate = this.getDate();
            updateEndDate();
        }
    }),
    _startDate = startPicker.getDate(),
    _endDate = endPicker.getDate();
	if (_startDate) {
		startDate = _startDate;
        updateStartDate();
    }
	if (_endDate) {
		endDate = _endDate;
        updateEndDate();
    }
	//

	$("div.reportArea label, div.reportArea input[type='radio']").click(function() {
		resetError();
		$("div.rprtType").find("input[type='radio']").attr("checked", false);
		$(this).parents("div.rprtType").find("input[type='radio']").attr("checked", true);
		$("div.filter").css("display", "none");

		var rptType = $("input[name='reportType']:checked").val();
		if(rptType=="dailyRpt" || rptType=="receipts" || rptType=="cshTrnsfr" || rptType=="salvat" || rptType=="vatpay") {
			resetRptFilter();

		} else if(rptType=="clientActvty" || rptType=="clientReserv" || rptType=="clientReceipt") {
			$("div.filter").css("display", "none");
			$("div.filter.byClientActvty").css("display", "inline");
			$("div.filter.byDate").css("display", "inline");

		} else if(rptType=="clientList") {
			$("div.filter").css("display", "none");
			$("div.filter.byWhouse").css("display", "inline");

		} else if(rptType=="clientList" || rptType=="accomRec" || rptType=="RestaurantRec" || rptType=="extraOrderRec" || rptType=="slsRcvble" || rptType=="slsBenefit" || rptType=="shares" || rptType=="payable") {
			$("div.filter").css("display", "none");
			$("div.filter.byWhouse").css("display", "inline");

		} else if(rptType=="purchases") {
			$("div.filter").css("display", "none");
			$("div.filter.byPurchase").css("display", "inline");
			$("div.filter.byDate").css("display", "inline");

		} else if(rptType=="commission") {
			$("div.filter").css("display", "none");
			$("div.filter.byCommission").css("display", "inline");
			$("div.filter.byDate").css("display", "inline");

		} /*else if(rptType=="receivable") {
			$("div.filter").css("display", "none");
			$("div.filter.byReceivable").css("display", "inline");

		}*/ else if(rptType=="sales") {
			resetRptFilter();
			$("div.filter.byInvoice").css("display", "inline");

		} else if(rptType=="payments") {
			resetRptFilter();
			$("div.filter.byExpense").css("display", "inline");

		} else if(rptType=="othrIncome") {
			resetRptFilter();
			$("div.filter.byIncAccs").css("display", "inline");

		} else if(rptType=="assets") {
			$("div.filter").css("display", "none");
			$("div.filter.byAssets").css("display", "inline");

		} else if(rptType=="bankActvty") {
			$("div.filter").css("display", "none");
			$("div.filter.byBankActvty").css("display", "inline");
			$("div.filter.byDate").css("display", "inline");

		} else if(rptType=="incStatment") {
			$("div.filter").css("display", "none");
			$("div.filter.byWhouse").css("display", "inline");
			$("div.filter.byIncStatment").css("display", "inline");

		} else if(rptType=="cashFlow") {
			$("div.filter").css("display", "none");
			$("div.filter.byMyWhouse").css("display", "inline");
			$("div.filter.byDate").css("display", "inline");

		} else if(rptType=="blnceSheet") {
			$("div.filter").css("display", "none");
			$("div.filter.byblnceSheet").css("display", "inline");

		}  else if(rptType=="IncRevenue") {
			$("div.filter").css("display", "none");
			$("div.filter.byIncRevenue").css("display", "inline");
			$("div.filter.byDate").css("display", "inline");

		} else if(rptType=="supplyListing") {
			$("div.filter").css("display", "none");
			$("div.filter.byWhouse").css("display", "inline");

		} else if(rptType=="supplyPrchse") {
			$("div.filter").css("display", "none");
			$("div.filter.byDate").css("display", "inline");

		} else if(rptType=="supplyUsage") {
			$("div.filter").css("display", "none");
			$("div.filter.byDate").css("display", "inline");

		} else if(rptType=="vendorList") {
			$("div.filter").css("display", "none");

		} else if(rptType=="vndActvty") {
		 	$("div.filter").css("display", "none");
			$("div.filter.byVndActvty").css("display", "inline");
			$("div.filter.byDate").css("display", "inline");

		} else if(rptType=="supplyItemTracking") {
		 	$("div.filter").css("display", "none");
			$("div.filter.bySupplyTrack").css("display", "inline");
			$("div.filter.byDate").css("display", "inline");

		}
		resetError();

	});
	// for income-statment filter types
	$("#monthTick, #chkIncome4mnth").click(function() {
		document.getElementById("chkIncome4mnth").checked = true;
		document.getElementById("txtIncMonth").disabled = false;
		document.getElementById("slcIncYear").disabled = true;
		$("select#slcIncYear option:first").attr("selected", "selected");
	});
	$("#yearTick, #chkIncome4yr").click(function() {
		document.getElementById("chkIncome4yr").checked = true;
		document.getElementById("slcIncYear").disabled = false;
		document.getElementById("txtIncMonth").disabled = true;
		$("input#txtIncMonth").val('');
		resetError();
	});
}
function check_reportFinParam() {
	resetError();
	var rptType 	= $("input[name='reportType']:checked").val();
	//console.log(rptType);

	var slcWhouse	= $("select#slcWhouse").val();
	var slcByComm	= $("select#slcByComm").val();
	var txtClient	= $("input#txtClient").val();
	var clientID	= $("input#clientID").val();
	var myWhouse	= $("input#myWhouse").val();
	//
	var startDate	= $("input#startDate").val();
	var endDate		= $("input#endDate").val();
	//
	var slcBankID	= $("select#slcBankID").val();
	var slcBankAcc	= $("select#slcBankAcc").val();
	var incomeFor	= $("input[name='incomeFor']:checked").val();
	var txtIncMonth	= $("input#txtIncMonth").val();
	var slcIncYear	= $("select#slcIncYear").val();
	var blnceYear 	= $("select#blnceYear").val();
	var Revenue 	= $("select#Revenue").val();
	//console.log(Revenue);
	if(rptType=="clientActvty") {
		if(txtClient.length<1 || clientID=="") {
			$("#error1").css("display", "inline");
			$("#error1").html("Please select customer");
			$("input#txtClient").css("border-color", "#FF0000");
			return false;
		} else { resetError(); }
	}
	if(rptType=="commission") {
		if(slcByComm.length<1 || slcByComm=="---") {
			$("#error1").css("display", "inline");
			$("#error1").html("Please select user");
			$("select#slcByComm").css("border-color", "#FF0000");
			return false;
		} else { resetError(); }
	}

	if(rptType=="bankActvty") {
		if(slcBankID.length<1 || slcBankID=="---") {
			$("#error1").css("display", "inline");
			$("#error1").html("Please select bank");
			$("select#slcBankID").css("border-color", "#FF0000");
			return false;
		} else { resetError(); }

		if(slcBankAcc.length<1 || slcBankAcc=="---") {
			$("#error1").css("display", "inline");
			$("#error1").html("Select bank account");
			$("select#slcBankAcc").css("border-color", "#FF0000");
			return false;
		} else { resetError(); }

	}
	if(rptType=="products") {
		if(myWhouse.length<1) {
			$("#error1").css("display", "inline");
			$("#error1").html("Can't find your wherehouse");
			return false;
		} else { resetError(); }

	}
	if(rptType!="supplyListing" && rptType!="vendorList" && rptType!="clientList" && rptType!="accomRec" && rptType!="RestaurantRec" && rptType!="extraOrderRec" && rptType!="slsRcvble" && rptType!="slsBenefit" && rptType!="assets" &&  rptType!="shares" && rptType!="receivable" && rptType!="restaurantReceivable" && rptType!="payable" && rptType!="incStatment" && rptType!="cashFlow" && rptType!="blnceSheet" ) {
		if(slcWhouse.length<1  || slcWhouse=="---") {
			$("#error1").css("display", "inline");
			$("#error1").html("Please select wherehouse");
			$("select#slcWhouse").css("border-color", "#FF0000");
			return false;
		} else { resetError(); }

		if(startDate.length<1) {console.log(rptType);
			$("#error1").css("display", "inline");
			$("#error1").html("Please select begining date");
			$("input#startDate").css("border-color", "#FF0000");
			return false;
		} else { resetError(); }

		if(endDate.length<1) {
			$("#error1").css("display", "inline");
			$("#error1").html("Please select ending date");
			$("input#endDate").css("border-color", "#FF0000");
			return false;
		} else { resetError(); }

	} else if(rptType=="incStatment") {
		if(slcWhouse.length<1  || slcWhouse=="---") {
			$("#error1").css("display", "inline");
			$("#error1").html("Please select wherehouse");
			$("select#slcWhouse").css("border-color", "#FF0000");
			return false;
		} else { resetError(); }

		if(incomeFor=="monthly") {
			if(txtIncMonth.length<1) {
				$("#error1").css("display", "inline");
				$("#error1").html("Please select month");
				$("input#txtIncMonth").css("border-color", "#FF0000");
				return false;
			} else { resetError(); }

		} else if(incomeFor=="yearly") {
			if(slcIncYear=="---") {
				$("#error1").css("display", "inline");
				$("#error1").html("Please select year");
				$("select#slcIncYear").css("border-color", "#FF0000");
				return false;
			} else { resetError(); }
		}
	} else if(rptType=="cashFlow") {
		if(startDate.length<1) {
			$("#error1").css("display", "inline");
			$("#error1").html("Please select begining date");
			$("input#startDate").css("border-color", "#FF0000");
			return false;
		} else { resetError(); }

		if(endDate.length<1) {
			$("#error1").css("display", "inline");
			$("#error1").html("Please select ending date");
			$("input#endDate").css("border-color", "#FF0000");
			return false;
		} else { resetError(); }

	} else if(rptType=="blnceSheet") {
		if(blnceYear.length<1  || blnceYear=="---") {
			$("#error1").css("display", "inline");
			$("#error1").html("Please select year");
			$("select#blnceYear").css("border-color", "#FF0000");
			return false;
		} else { resetError(); }
	} else if(rptType=="incRevenue") {
		if(startDate.length<1) {
			$("#error1").css("display", "inline");
			$("#error1").html("Please select begining date");
			$("input#startDate").css("border-color", "#FF0000");
			return false;
		} else { resetError(); }

		if(endDate.length<1) {
			$("#error1").css("display", "inline");
			$("#error1").html("Please select ending date");
			$("input#endDate").css("border-color", "#FF0000");
			return false;
		} else { resetError(); }
	}


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

