// Prepare & load a function for a page
$(document).ready(function() {
getEmployeeDB();
getTimeClockDB();
});
// Automatically update the year
const copyrightYear = new Date();
let year = copyrightYear.getFullYear();
document.getElementById("copyright-year").innerHTML = "LTD Services © " + year;
// Query a list of employees from database in modifyemployee.html
function getEmployeeDB() {
$.ajax({
url: "https://x0t8oj4tr7.execute-api.us-east-1.amazonaws.com/query/queryemployee",
type: "GET",
success: function(data) {
$("#employeeData tr").remove();
let employee_data = "";
let record = data.recordset;
let rowId = 1;
$.each(record, function(key, value) {
employee_data += "
";
employee_data += "" + value.Username + " | ";
employee_data += "" + value.FirstName + " | ";
employee_data += "" + value.LastName + " | ";
employee_data += "" + value.RoleName + " | ";
employee_data += "" + value.Active + " | ";
employee_data += "
";
rowId = rowId + 1;
});
$("#employeeData").append(employee_data);
},
error: function(jqXhr, textStatus, errorMessage) {
$("#employeeData").text("Error: " + errorMessage);
}
});
}
// Get time clock from database in timeclock.html
function getTimeClockDB() {
var account = new Object();
account.username = sessionStorage.getItem("username");
$.ajax({
url: "https://is7hpdjha9.execute-api.us-east-1.amazonaws.com/timeclock/timeclock",
type: "POST",
crossDomain: true,
contentType: "application/json",
data: JSON.stringify(account),
success: function(data) {
let time_data = "";
let x = 1;
if (data == "false") {
$("#LastAction").text("Your account has no time clock activity.");
$("#ActionButton").prop("value", "Clock In");
} else {
$("#employeeClockData tr").remove();
$.each(data.recordset, function(key, value) {
let actionTime = new Date(value.ActionTime.toString());
let options = { year: "numeric", month: "long", day: "numeric", hour: "2-digit", minute: "2-digit"};
options.timeZoneName = "short";
if (x == 1) {
// Check the last user action and set the text of the button.
if (value.UserAction == "In") {
$("#ActionButton").prop("value", "Clock Out");
$("#LastAction").text("Clocked In - " + actionTime.toLocaleString("en-US", options));
} else {
$("#ActionButton").prop("value", "Clock In");
$("#LastAction").text("Clocked Out - " + actionTime.toLocaleString("en-US", options));
}
x = -1;
}
time_data += "";
time_data += "" + actionTime.toLocaleString("en-US", options) + " | ";
time_data += "" + value.UserAction + " | ";
time_data += "
";
});
$("#employeeClockData").append(time_data);
}
},
error: function(jqXhr, textStatus, errorMessage) {
$("#employeeClockData").text("Error: " + errorMessage);
}
});
}
// Clock In/Out in timeclock.html
function clockInOut() {
var account = new Object();
var buttonValue = $("#ActionButton").val();
account.username = sessionStorage.getItem("username");
account.action = buttonValue;
$.ajax({
url: "https://is7hpdjha9.execute-api.us-east-1.amazonaws.com/timeclock/clockin",
type: "POST",
crossDomain: true,
contentType: "application/json",
data: JSON.stringify(account),
success: function(data) {
if (data == "false") {
$("#LastAction").text("Action failed. Please try again later.");
} else {
getTimeClockDB();
}
},
error: function(jqXhr, textStatus, errorMessage) {
$("#LastAction").text("Error: " + errorMessage);
}
});
}
// Close the Modify Menu Screen in modifyemployee.html
function closeModifyMenuScreen() {
$(".modifyMenu").hide();
}
// Display individual employee's full info in the "Modify Menu Screen" at modifyemployee.html
function queryEmployeeInfo(rowId) {
$(".modifyMenu").show();
var info = new Object;
info.username = $("#Username" + rowId).text();
$.ajax({
url: "https://x0t8oj4tr7.execute-api.us-east-1.amazonaws.com/query/queryemployeeinfo",
type: "POST",
crossDomain: true,
contentType: "application/json",
data: JSON.stringify(info),
success: function(data) {
$.each(data.recordset, function(key, value) {
$("#username").text(value.Username);
$("#firstname").val(value.FirstName);
$("#midname").val(value.MidName);
$("#lastname").val(value.LastName);
$("#rolename").val(value.RoleName);
$("#address1").val(value.Address1);
$("#address2").val(value.Address2);
$("#city").val(value.City);
$("#statecode").val(value.StateCode);
$("#zipcode").val(value.ZipCode);
$("#phone").val(value.Phone);
$("#email").val(value.Email);
if (value.Active == 1) {
$("#active").prop("checked", true);
} else {
$("#active").prop("checked", false);
}
});
},
error: function(jqXhr, textStatus, errorMessage) {
$("#username").text("Error: " + errorMessage);
}
});
}
// Modifies individual employee's information
function modifyEmployeeInfo() {
var info = new Object();
info.username = $("#username").text();
info.firstname = $("#firstname").val();
info.lastname = $("#lastname").val();
info.rolename = $("#rolename").val();
info.address1 = $("#address1").val();
info.city = $("#city").val();
info.statecode = $("#statecode").val();
info.zipcode = $("#zipcode").val();
info.phone = $("#phone").val();
info.email = $("#email").val();
if ($("#midname").val() == "") {
info.midname = "Not given";
} else {
info.midname = $("#midname").val();
}
if ($("#address2").val() == "") {
info.address2 = "Not given";
} else {
info.address2 = $("#address2").val();
}
if ($("#active").is(":checked")) {
info.active = 1;
} else {
info.active = 0;
}
if ($("#rolename").val() == "WebAdmin") {
info.ltdadmin = 1;
} else {
info.ltdadmin = 0;
}
$.ajax({
url: "https://xpttpcamx8.execute-api.us-east-1.amazonaws.com/update/updateemployeeinfo",
method: "POST",
crossDomain: true,
contentType: "application/json",
data: JSON.stringify(info),
success: function(data) {
alert(data);
location.replace("modifyemployee.html");
},
error: function(jqXhr, textStatus, errorMessage) {
$("#username").text("Error: " + errorMessage + " // Please refresh and try again.");
}
});
}
// Search for a(n) employee in modifyemployee.html (Doesn't work currently)
/* function searchEmployee() {
if ($("#searchUsername").val() == null && $("#searchFirstName").val() == null && $("#searchLastName").val() == null) {
getEmployeeDB();
} else {
var info = new Object();
info.username = $("#searchUsername").val();
info.firstname = $("#searchFirstName").val();
info.lastname = $("#searchLastName").val();
$.ajax({
url: "",
method: "POST",
crossDomain: true,
contentType: "application/json",
data: JSON.stringify(info),
success: function(data) {
$("#employeeClockData tr").remove();
let employee_data = "";
let record = data.recordset;
let rowId = 1;
$.each(record, function (key, value) {
employee_data += "";
employee_data += "" + value.Username + " | ";
employee_data += "" + value.FirstName + " | ";
employee_data += "" + value.LastName + " | ";
employee_data += "" + value.RoleName + " | ";
employee_data += "" + value.Active + " | ";
employee_data += "
";
rowId = rowId + 1;
});
$("#employeeData").append(employee_data);
},
error: function(jqXhr, textStatus, errorMessage) {
$("#username").text("Error: " + errorMessage + " // Please refresh and try again.");
}
});
}
}*/
// Contact Form in index.html
function submitToAPI(e) {
e.preventDefault();
var URL = "https://5dykenthsg.execute-api.us-east-1.amazonaws.com/03/contact-us";
var Namere = /[A-Za-z]{1}[A-Za-z]/;
if (!Namere.test($("#materialContactFormName").val())) {
alert("Name can not less than 2 char");
return;
}
if ($("#materialContactFormEmail").val() == "") {
alert("Please enter your email address");
return;
}
var reeamil = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,6})?$/;
if (!reeamil.test($("#materialContactFormEmail").val())) {
alert("Please enter valid email address");
return;
}
var name = $("#materialContactFormName").val();
var email = $("#materialContactFormEmail").val();
var subject = $("#subjectOption :selected").text();
var message = $("#materialContactFormMessage").val();
var data = {
name: name,
email: email,
subject: subject,
message: message
};
$.ajax({
type: "POST",
url: "https://5dykenthsg.execute-api.us-east-1.amazonaws.com/03/contact-us",
dataType: "json",
crossDomain: "true",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(data),
success: function () {
// clear form and show a success message
alert("Successfull");
$("#contact-form").reset();
location.reload();
},
error: function () {
// show an error message
alert("UnSuccessfull");
}
});
}
// Format zip code (in US)
function formatZIPCode(value) {
if (!value) return value;
const zipCode = value.replace(/[^\d]/g, '');
const zipCodeLength = zipCode.length;
if (zipCodeLength < 7) return zipCode;
return `${zipCode.slice(0, 5)}-${zipCode.slice(5,8)}`;
}
function zipCodeFormatter() {
const inputField = document.getElementById('zipcode');
const formattedInputValue = formatZIPCode(inputField.value);
inputField.value = formattedInputValue;
}
// Format phone number
function formatPhoneNumber(value) {
if (!value) return value;
const phoneNumber = value.replace(/[^\d]/g, '');
const phoneNumberLength = phoneNumber.length;
if (phoneNumberLength < 4) return phoneNumber;
if (phoneNumberLength < 7) {
return `(${phoneNumber.slice(0, 3)}) ${phoneNumber.slice(3)}`;
}
return `(${phoneNumber.slice(0, 3)}) ${phoneNumber.slice(3,6)}-${phoneNumber.slice(6, 9)}`;
}
function phoneNumberFormatter() {
const inputField = document.getElementById('phone');
const formattedInputValue = formatPhoneNumber(inputField.value);
inputField.value = formattedInputValue;
}