";
var addressString = "
";
const $ = {
a:[],
selector:{
selector1:['selector here with options']
}
}
const conversation = {
stage: 0,
applicantInfo: {
firstName: "",
lastName: "",
middleName: "",
age: "",
email: "",
phoneNumber: "",
address: "",
code: ""
}
};
// Define a global variable to store the fetched replies
let replies = {};
setTimeout(() => {
repp(`
Hello
SELECT
Services
`)
}, 1200);
const started = (selector) => {
selector = document.getElementById('select-start');
switch (selector.value) {
case "Services":
repp(`
`)
break;
default:
break;
}
}
// Function to fetch replies from JSON file
function fetchReplies() {
fetch('replies.json')
.then(response => response.json())
.then(data => {
replies = data;
})
.catch(error => {
console.error('Error fetching replies:', error);
});
}
// Call fetchReplies() to load replies when the script starts
fetchReplies();
// Function to set last seen time
function setLastSeen() {
var date = new Date();
var lastSeen = document.getElementById("lastseen");
lastSeen.innerText = "last seen today at " + date.getHours() + ":" + date.getMinutes()
}
// Function to close full-screen display picture
function closeFullDP() {
var x = document.getElementById("fullScreenDP");
if (x.style.display === 'flex') {
x.style.display = 'none';
} else {
x.style.display = 'flex';
}
}
// Function to open full-screen display picture
function openFullScreenDP() {
var x = document.getElementById("fullScreenDP");
if (x.style.display === 'flex') {
x.style.display = 'none';
} else {
x.style.display = 'flex';
}
}
// Function to check if enter key is pressed
function isEnter(event) {
if (event.keyCode == 13) {
sendMsg();
}
}
// Function to send message
function sendMsg() {
var input = document.getElementById("inputMSG");
var ti = input.value;
if (input.value == "") {
return;
}
var date = new Date();
var myLI = document.createElement("li");
var myDiv = document.createElement("div");
var crDiv = document.createElement("div");
var dateLabel = document.createElement("label");
dateLabel.innerText = date.getHours() + ":" + date.getMinutes();
myDiv.setAttribute("class", "sent");
crDiv.setAttribute("class", "green");
crDiv.innerText = input.value;
myDiv.appendChild(crDiv);
myLI.appendChild(myDiv);
crDiv.appendChild(dateLabel);
document.getElementById("listUL").appendChild(myLI);
var s = document.getElementById("chatting");
s.scrollTop = s.scrollHeight;
setTimeout(function () { wNreply(ti) }, 2000);
input.value = "";
playSound();
}
// Function to generate unique code
function generateUniqueCode() {
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var code = '';
for (var i = 0; i < 12; i++) {
code += characters.charAt(Math.floor(Math.random() * characters.length));
}
return code;
}
// Function to play sent message sound
function playSound() {
audio.play();
}
// Function to send response
function sendResponse() {
setTimeout(setLastSeen, 2000);
var date = new Date();
var myLI = document.createElement("li");
var myDiv = document.createElement("div");
var crDiv = document.createElement("div");
var dateLabel = document.createElement("label");
dateLabel.innerText = date.getHours() + ":" + date.getMinutes();
myDiv.setAttribute("class", "received");
crDiv.setAttribute("class", "grey");
dateLabel.setAttribute("class", "dateLabel");
crDiv.innerText = "";
myDiv.appendChild(crDiv);
myLI.appendChild(myDiv);
crDiv.appendChild(dateLabel);
document.getElementById("listUL").appendChild(myLI);
var s = document.getElementById("chatting");
s.scrollTop = s.scrollHeight;
playSound();
}
// Function to process user input and reply
function wNreply(inputText) {
var lastSeen = document.getElementById("lastseen");
lastSeen.innerText = "typing...";
var name = "";
let user = inputText.toLowerCase();
if (user.includes("my name is")) {
name = inputText.substring(inputText.indexOf("is") + 2);
if (name.toLowerCase().includes(name)) {
repp("Ohh! That's my name too" + name);
return
}
inputText = "input";
} else if (user.includes("gusto") || user.includes('applying')) {
repp("Im glad that you're interested for a Customer Service Representative Position. To get started, please enter your first name.");
conversation.stage = 1;
return;
} else if (user.includes('fb')) {
repp(DATA.form.form1);
return;
} else if (user.startsWith('a-')) {
checkApplicationStatus(user.substr(2)); // Extract the code after "A-"
return;
}
switch (user.trim()) {
case "help":
repp("help");
break;
case "resume":
repp(resumeString);
break;
case "intro":
repp("intro");
break;
case "i want to apply":
case "applicant":
case "apply":
case "applying for customer service position":
repp("To get started, please type your first name.");
conversation.stage = 1;
break;
case "clear":
clearChat();
break;
case "time":
var date = new Date();
repp("Current time is " + date.getHours() + ":" + date.getMinutes());
break;
case "date":
var date = new Date();
repp("Current date is " + date.getDate() + "/" + date.getMonth() + "/" + date.getFullYear());
break;
default:
if (conversation.stage > 0) {
// Handle collecting applicant information
processApplicantInfo(user.trim());
} else {
repp('Im quite not sure I understand that. You can try other term for that or just type "help" to see the other available commands.');
}
break;
}
}
// Function to process applicant information
function processApplicantInfo(input) {
switch (conversation.stage) {
case 1:
conversation.applicantInfo.firstName = input;
conversation.stage = 2;
repp("Next, please type your last name.");
break;
case 2:
conversation.applicantInfo.lastName = input;
conversation.stage = 3;
repp("Please provide your middle name (if any), or type 'none'.");
break;
case 3:
if (input.toLowerCase() === "none") {
conversation.applicantInfo.middleName = "None";
} else {
conversation.applicantInfo.middleName = input;
}
conversation.stage = 4;
repp("What is your age?");
break;
case 4:
conversation.applicantInfo.age = input;
conversation.stage = 5;
repp("Please enter your email address.");
break;
case 5:
conversation.applicantInfo.email = input;
conversation.stage = 6;
repp("Next, please provide your phone number.");
break;
case 6:
conversation.applicantInfo.phoneNumber = input;
conversation.stage = 7;
repp("Finally, type your present address.");
break;
case 7:
conversation.applicantInfo.address = input;
conversation.applicantInfo.code = generateUniqueCode();
saveApplicantInfo(conversation.applicantInfo); // Call a function to save this information to the database
conversation.stage = 0; // Reset the conversation stage
repp("Thank you! Your application has been submitted. To track the status of your application, here is a unique code: A-" + conversation.applicantInfo.code + ". Just send 'A-" + conversation.applicantInfo.code + "' to check.");
break;
default:
repp("I'm sorry, I didn't understand that.");
break;
}
}
// Function to clear chat
function clearChat() {
document.getElementById("listUL").innerHTML = "";
wNreply('intro');
}
// Function to fetch replies from JSON file
function repp(textKey) {
setTimeout(setLastSeen, 2000);
var date = new Date();
var myLI = document.createElement("li");
var myDiv = document.createElement("div");
var crDiv = document.createElement("div");
var dateLabel = document.createElement("label");
dateLabel.setAttribute("id", "sentlabel");
dateLabel.id = "sentlabel";
dateLabel.innerText = date.getHours() + ":" + date.getMinutes();
myDiv.setAttribute("class", "received");
crDiv.setAttribute("class", "grey");
// Check if the textKey exists in fetched replies, otherwise use the textKey itself
const replyText = replies[textKey] ? replies[textKey] : textKey;
crDiv.innerHTML = replyText;
myDiv.appendChild(crDiv);
myLI.appendChild(myDiv);
crDiv.appendChild(dateLabel);
document.getElementById("listUL").appendChild(myLI);
var s = document.getElementById("chatting");
s.scrollTop = s.scrollHeight;
playSound();
}
// Function to save applicant information
function saveApplicantInfo(applicantInfo) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "save_applicant_info.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log(xhr.responseText); // Log server response
}
};
var data = "firstName=" + encodeURIComponent(applicantInfo.firstName) +
"&lastName=" + encodeURIComponent(applicantInfo.lastName) +
"&middleName=" + encodeURIComponent(applicantInfo.middleName) +
"&age=" + encodeURIComponent(applicantInfo.age) +
"&email=" + encodeURIComponent(applicantInfo.email) +
"&phoneNumber=" + encodeURIComponent(applicantInfo.phoneNumber) +
"&address=" + encodeURIComponent(applicantInfo.address) +
"&code=" + encodeURIComponent(applicantInfo.code);
xhr.send(data);
}
// Function to check application status
function checkApplicationStatus(code) {
repp("Checking application status for code: " + code);
var xhr = new XMLHttpRequest();
xhr.open("POST", "get_interview_schedule.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
if (response.success) {
var scheduleDate = response.scheduleDate;
var link = response.link;
repp("Your application is currently scheduled for an Initial Interview on " + scheduleDate + ". Click here for more details: " + link);
} else {
repp("No schedule found for the provided code.");
}
}
};
xhr.send("code=" + encodeURIComponent(code));
}
// Function to get interview schedule
function getInterviewSchedule(code, callback) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "get_schedule.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
if (response.success) {
callback(response.scheduleDate, response.link);
} else {
callback(null, null);
}
}
};
xhr.send("code=" + encodeURIComponent(code));
}