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

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

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

/* -----------------------------------------------
-------------------Tasks Types--------------------- 
--------------------------------------------------*/

function loadTaskTypes(pageLimit) {
	$("div.dynamicData.one #loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&perPage='+15;
	$.ajax({
		type: "POST",
		url: "./includes/houseKpg.php?action=loadTable&dest=allTaskTypes&myUser="+myUser+"&myRole="+myRole,
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			$("div.dynamicData.one #loading").css("display", "none");
			$("div.dynamicData.one .load_more_link").addClass('noneLink');
			$("div.dynamicData.one").html(result);
		}
	});
	return false;
}
function addNewTaskType() {
	swal({
		title: "Add Task Type",
		text: 'Give a name to this Type',
		type: "input",
		showCancelButton: true,
		closeOnConfirm: false,
		animation: "slide-from-top",
		inputPlaceholder: "required",
	},
	function(typeName) {
		if (typeName === false) return false;
		if (typeName === "") {
			swal.showInputError("Please write task type");
			return false
		}
		$.post("./includes/houseKpg.php?action=saveNew&dest=addTaskType&addedBy="+myUser, { typeName:typeName }, function(data) {
			var addedpos = data.search("added");
			if(addedpos==0) {
				var lastID = data.slice(6,21);
				swal({
					title: "Success!",
					text: "You successfully added new task type.",
					type: "success",
					timer: 1500,
					showConfirmButton: false
				},
				function() {
					window.location = "./workplace.php?role="+myRole+"&task=house&subtask=tasks";
				}
				);
			} else {
				swal("Oops!", data, "error");
			}
		});
	});
	return false;
}
function catchTaskType(recID) {
	$.post("./includes/houseKpg.php?action=get_recInfo&dest=getTypeTask", { recID: recID, myUser: myUser, myRole: myRole }, function(data) {
		$("div.twoCols.one header span").html('[1 row]');
		$("div.dynamicData.one").html(data);
	});
	$("div.suggest.one").css("display", "none");
}
function check_editTaskType() {
	var typeID 		= $("#typeID").val();
	var txtType 	= $("#txtType").val();
	var slcStatus 	= $("#slcStatus").val();
	
	if(txtType.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Please type name");
		$("#txtType").css("border-color", "#FF0000");
		return false;
	} else { clearErrors(); }
	
	$("label.process").css("display", "inline");
	$("#btnUpdate").attr("disabled", true);
	$("#btnUpdate").html("Wait...");
	
	$.post("./includes/houseKpg.php?action=updateExist&dest=editTaskType&updatedBy="+myUser, { typeID:typeID, txtType:txtType, slcStatus:slcStatus }, function(data) {
		$("label.process").css("display", "none");
		if(data=="updated") {
			swal({
				title: "Success!",
				text: "You updated task type record successfully.",
				type: "success",
				timer: 1500,
				showConfirmButton: false
			},
				function() {
					window.location = "./workplace.php?role="+myRole+"&task=house&subtask=tasks";
				}
			);
		} else {
			swal("Oops!", data, "error");
			$("#btnUpdate").attr("disabled", false);
			$("#btnUpdate").html("Update");
		}
	});
	return false;
}
function delTypeTask (recID) {
	swal({
		title: "Are you sure?",
		text: "You going to delete this type name!",
		type: "warning",
		showCancelButton: true,
		confirmButtonColor: '#DD6B55',
		confirmButtonText: 'Yes, delete it!',
		closeOnConfirm: false
	},
	function() {
		$.post("./includes/houseKpg.php?action=deleteRec&dest=delTypeTask", { recID:recID }, function(data) {
			if(data=="deleted") {
				swal({
					title: "Success!",
					text: "Selected type name has been deleted.",
					type: "success",
					timer: 1500,
					showConfirmButton: false
				},
					function() {
						window.location = "./workplace.php?role="+myRole+"&task=house&subtask=tasks";
					}
				);
			} else {
				swal("Oops", data, "error");
			}
		});
	});
	return false;
}
/* -----------------------------------------------
----------------------Tasks------------------------ 
--------------------------------------------------*/

function loadTasks(pageLimit) {
	$("div.dynamicData.two #loading").css("display", "inline");
	var dataString = 'pageLimit='+ pageLimit+'&perPage='+15;
	$.ajax({
		type: "POST",
		url: "./includes/houseKpg.php?action=loadTable&dest=allTasks&myUser="+myUser+"&myRole="+myRole,
		data: dataString,
		cache: false,
		timeout: 2000,
		success: function(result){
			$("div.dynamicData.two #loading").css("display", "none");
			$("div.dynamicData.two .load_more_link").addClass('noneLink');
			$("div.dynamicData.two").html(result);
		}
	});
	return false;
}
function check_addTask() {
	var slcType    = $("#slcType").val();
	var txtName   = $("#txtName").val();
	var txtRemarks = $("#txtRemarks").val();
	
	if(slcType.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Please select type ");
		$("#slcType").css("border-color", "#FF0000");
		return false;
	} else {  clearErrors(); }
	
	if(txtName.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Provide task name");
		$("#txtName").css("border-color", "#FF0000");
		return false;
	} else { clearErrors(); }
	
	$("label.process").css("display", "inline");
	$("#btnSave").attr("disabled", true);
	$("#btnSave").html("Wait...");
	
	$.post("./includes/houseKpg.php?action=saveNew&dest=addTask&addedBy="+myUser, { slcType:slcType, txtName:txtName, 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 task record successfully.",
				type: "success",
				timer: 1500,
				showConfirmButton: false,
			},
				function() {
					window.location = "./workplace.php?role="+myRole+"&task=house&subtask=tasks\
					&action=showInfo&recID="+lastID;
				}
			);
		} else {
			swal("Oops!", data, "error");
		}
		$("#btnSave").attr("disabled", false);
		$("#btnSave").html("Save");
	});
	return false;	
}
function check_editTask() {
	var taskID 		= $("#taskID").val();
	var slcType 	= $("#slcType").val();
	var taskName 	= $("#taskName").val();
	var slcStatus 	= $("#slcStatus").val();
	var txtRemarks 	= $("#txtRemarks").val();
	
	if(slcType.length < 1) {
		$("#error1").css("display", "inline");
		$("#error1").html("Select type");
		$("#slcType").css("border-color", "#FF0000");
		return false;
	} else { clearErrors(); }
	
	if(taskName.length < 1) {
		$("#error2").css("display", "inline");
		$("#error2").html("Provide task name");
		$("#taskName").css("border-color", "#FF0000");
		return false;
	} else { clearErrors(); }
	
	$("label.process").css("display", "inline");
	$("#btnUpdate").attr("disabled", true);
	$("#btnUpdate").html("Wait...");
	
	$.post("./includes/houseKpg.php?action=updateExist&dest=editTask&updatedBy="+myUser, { taskID:taskID, slcType:slcType, taskName:taskName, txtRemarks:txtRemarks, slcStatus:slcStatus }, function(data) {
		$("label.process").css("display", "none");
		if(data=="updated") {
			swal({
				title: "Success!",
				text: "You updated task successfully.",
				type: "success",
				timer: 1500,
				showConfirmButton: false
			},
				function() {
					window.location = "./workplace.php?role="+myRole+"&task=house&subtask=tasks\
					&action=showInfo&recID="+taskID;
				}
			);
		} else {
			swal("Oops!", data, "error");
			$("#btnUpdate").attr("disabled", false);
			$("#btnUpdate").html("Update");
		}
	});
	return false;
}
function delTask(recID) {
	swal({
		title: "Are you sure?",
		text: "You going to delete this task!",
		type: "warning",
		showCancelButton: true,
		confirmButtonColor: '#DD6B55',
		confirmButtonText: 'Yes, delete it!',
		closeOnConfirm: false
	},
	function() {
		$.post("./includes/houseKpg.php?action=deleteRec&dest=delTask", { recID:recID }, function(data) {
			if(data=="deleted") {
				swal({
					title: "Success!",
					text: "Selected type name has been deleted.",
					type: "success",
					timer: 1500,
					showConfirmButton: false
				},
					function() {
						window.location = "./workplace.php?role="+myRole+"&task=house&subtask=tasks";
					}
				);
			} else {
				swal("Oops", data, "error");
			}
		});
	});
	return false;
}
function catchTask(recID) {
	$.post("./includes/houseKpg.php?action=get_recInfo&dest=getTask", { recID: recID, myUser: myUser, myRole: myRole }, function(data) {
		$("div.twoCols.two header span").html('[1 row]');
		$("div.dynamicData.two").html(data);
	});
	$("div.suggest.two").css("display", "none");
}

/* -----------------------------------------------
------------------Tasks---------------------------
--------------------------------------------------*/
function updateTaskMgtBar() {
	var perPage = $("#slcPerPage").val();
	var count = $("label#count").html();
	$.ajax({
		type: "POST",
		url: "./includes/houseKpg.php?action=setFoundNum&dest=taskMgt",
		cache: false,
		timeout: 2000,
		success: function(result){
			if(result != count){
				$("#refresh").html('records changed, <a onclick="return loadTaskMgtTwo(0,'+perPage+');">refresh now!</a>');
				(function blink(){
					$("span#refresh a").fadeOut(500).fadeIn(500, blink);
				})();
			}
			setTimeout(function(){
				updateTaskMgtBar();
			}, 60000);
		}
	});
}
function loadTaskMgt(pageLimit, perPage) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+pageLimit+'&perPage='+perPage;
	dataString += '&myUser='+myUser+"&myRole="+myRole;
	$.ajax({
		type: "POST",
		url: "./includes/houseKpg.php?action=loadTable&dest=allTaskMgt",
		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 loadTaskMgtTwo(pageLimit, perPage) {
	$("#dataTable div#loading").css("display", "inline");
	var dataString = 'pageLimit='+pageLimit+'&perPage='+perPage;
	dataString += '&myUser='+myUser+"&myRole="+myRole;
	$.ajax({
		type: "POST",
		url: "./includes/houseKpg.php?action=loadTable&dest=allTaskMgt",
		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 lmiddle">Task Details</div><div class="cave middle">Employee</div><div class="cave middle">Dates</div><div class="cave middle">Property</div><div class="cave short">Status</div></div>');
			$("#dataTable").append(result);
			
			(function (){
				//
				$.ajax({
					type: "POST",
					url: "./includes/houseKpg.php?action=setFoundNum&dest=taskMgt",
					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 add2List4Task() {
	var slcTasks	= $("#slcTasks").val(); 	var slcTasks2	= $("#slcTasks :selected").text();
	var slcEmp		= $("#slcEmp").val();		var slcEmp2		= $("#slcEmp :selected").text();
	var slcProp		= $("#slcProp").val();		var slcProp2	= $("#slcProp :selected").text();
	var dateIN		= $("#dateIN").val();
	var dateOut		= $("#dateOut").val();
	
	if(slcTasks.length < 1 || slcTasks== "--") {
		$("#error1").css("display", "inline");
		$("#error1").html("Please select task");
		$("#slcTasks").css("border-color", "#FF0000");
		return false;
	} else { clearErrors(); }
	
	if(slcEmp.length < 1 || slcEmp== "--") {
		$("#error2").css("display", "inline");
		$("#error2").html("select employee");
		$("#slcEmp").css("border-color", "#FF0000");
		return false;
	} else { clearErrors(); }
	
	if(slcProp.length < 1 || slcProp== "--") {
		$("#error3").css("display", "inline");
		$("#error3").html("select property");
		$("#slcProp").css("border-color", "#FF0000");
		return false;
	} else { clearErrors(); }
	
	if(dateIN.length < 1 ) {
		$("#error4").css("display", "inline");
		$("#error4").html("select date In");
		$("#dateIN").css("border-color", "#FF0000");
		return false;
	} else { clearErrors(); }
	
	if(dateOut.length < 1 ) {
		$("#error4").css("display", "inline");
		$("#error4").html("select date out");
		$("#dateOut").css("border-color", "#FF0000");
		return false;
	} else { clearErrors(); }
	
	// add new service to the list
	$("div.list").append('<div class="listItem num'+num+'">\
		<div class="cave serial"></div>\
		<div class="cave small">\
			<input type="hidden" name="taskID[]"  id="taskID" value="'+slcTasks+'" readonly="readonly" />\
			<input type="text" value="'+slcTasks2+'" readonly="readonly" />\
		</div>\
		<div class="cave small">\
			<input type="hidden" name="empID[]"  id="empID[]" value="'+slcEmp+'" readonly="readonly" />\
			<input type="text" value="'+slcEmp2+'" readonly="readonly" />\
		</div>\
		<div class="cave small">\
			<input type="hidden" name="propID[]"  id="propID[]" value="'+slcProp+'" readonly="readonly" />\
			<input type="text" value="'+slcProp2+'" readonly="readonly" />\
		</div>\
		<div class="cave small">\
			<input type="text" name="datIne[]"  id="datIne[]" value="'+dateIN+'" readonly="readonly" />\
		</div>\
		<div class="cave small">\
			<input type="text" name="dateOu[]"  id="dateOu[]" value="'+dateOut+'" readonly="readonly" />\
		</div>\
		<div class="cave last"><a id="remove" onclick="return removeItem('+num+');">remove</a></div>\
	</div>');
	num++;
	
	return false;	
}
function check_addTaskMngt() {
	var listItem		= $("div.theForm .listItem").height();
	
	if(listItem<1) {
	  $("#error5").css("display", "inline");
	  $("#error5").html("Please add at least one!");
	  return false;
	} else { clearErrors(); }
	
	$("label.process").css("display", "inline");
	$("#btnSave").attr("disabled", true);
	$("#btnSave").html("Wait...");
	return true;
}
function check_editTaskMngt() {
	var taskID 		= $("#taskID").val();
	var taskName 	= $("#taskName").val();
	var taskSumID 	= $("#taskSumID").val();
	var slcStatus 	= $("#slcStatus").val();
	var txtRemarks 	= $("#txtRemarks").val();
	
	if(taskName.length < 1 || taskID=="") {
		$("#error1").css("display", "inline");
		$("#error1").html("check task name");
		$("#taskName").css("border-color", "#FF0000");
		return false;
	} else { clearErrors(); }
	
	$("label.process").css("display", "inline");
	$("#btnUpdate").attr("disabled", true);
	$("#btnUpdate").html("Wait...");
	
	$.post("./includes/houseKpg.php?action=updateExist&dest=editTaskMngt&updatedBy="+myUser, { taskID:taskID, taskSumID:taskSumID, slcStatus:slcStatus, txtRemarks:txtRemarks }, function(data) {
		$("label.process").css("display", "none");
		if(data=="updated") {
			swal({
				title: "Success!",
				text: "You updated task successfully.",
				type: "success",
				timer: 1500,
				showConfirmButton: false
			},
				function() {
					window.location = "./workplace.php?role="+myRole+"&task=house&subtask=taskMgt\
					&action=showInfo&recID="+taskSumID;
				}
			);
		} else {
			swal("Oops!", data, "error");
			$("#btnUpdate").attr("disabled", false);
			$("#btnUpdate").html("Update");
		}
	});
	return false;
}
function catchTaskMngt(taskID) {
	$.post("./includes/houseKpg.php?action=get_recInfo&dest=getTaskMngt", { taskID: taskID, 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");
}
function delTaskMngt(num,taskDetID,taskSumID) {
	swal({
		title: "Are you sure to delete?",
		text: "You going to delete this task!\n\
		Once deleted, it will not exist any more.",
		type: "warning",
		showCancelButton: true,
		confirmButtonColor: '#DD6B55',
		confirmButtonText: 'Yes, delete it!',
		closeOnConfirm: false
	},
	function() {
		$.post("./includes/houseKpg.php?action=deleteRec&dest=delTaskMngt",{taskDetID:taskDetID,taskSumID:taskSumID}, function(data){
			if(data=="deleted") {
				$("div.list div.listItem.rec"+num).find("input").val("");
				$("div.list div.listItem.rec"+num).fadeOut(500, function(){ $(this).remove(); });
				swal({
					title: "Success",
					text: "Task you selected has been removed permenantly",
					type: "success",
					timer: 1500,
					showConfirmButton: false
				},
				function() {
					location.reload();
				}
				);
			} else {
				swal("Oops!", data, "error");
			}
		});
	});
	return false;
}

