// ***** ----------------- *****
// ***** apptSelect.js *****
// ***** ----------------- *****
$(document).ready(function(){
$(".apptSelectButton").click(function(){
$btn = $(this);
var btnId = $btn.attr('id');
var calId = btnId.split('_')[1];
$txtDate = $("#DATE_" + calId);
$lstAppt = $("#APPOINTMENT_" + calId);
$hdnResId = $("#RESERVATION_" + calId);
$hdnApptKey = $("#APPTKEY_" + calId);
$hdnSelDate = $("#SELDATE_" + calId);
$hdnAdminAppt = $("#ADMINAPPT_" + calId);
$apptKey = $lstAppt.val();
if(($txtDate.val() == '') || ($lstAppt.val() == '')) {
alert('Choose a date and timeslot first');
return;
}
if ($(this).val() == "Cancel") {
if($hdnAdminAppt.val() == '1') {
$id = $hdnApptKey.val();
} else {
$id = $hdnResId.val();
}
} else {
$id = $lstAppt.val();
}
var isTest = ($("input[name='TEST_" + calId + "']").val() == "1" ? true : false);
var useNew = ($("input[name='NEW_" + calId + "']").val() == "1" ? true : false);
var mydata = {"action": $(this).val(), optlist: 'ADMIN.APPT="' + $hdnAdminAppt.val() + '"', "targetId" : $id};
if(isTest) {
mydata["TEST"] = '1';
}
if(useNew) {
mydata["NEW"] = '1';
}
$.ajax({
url: "/apptSelect/action",
data: mydata,
type: "POST",
success: function(data) {
data = $.trim(data);
var duration = $("#DURATION_" + calId).val();
if(data == 'Canceled') {
$btn.val('Reserve');
window.clearInterval(ptrTimerArr[$btn.data("timerIndex")]);
$txtDate.val('');
$txtDate.attr("disabled", false);
$lstAppt.val('');
$lstAppt.attr("style", 'width:150px;');
$ticker = $("#TIMER_" + calId);
$ticker.html('');
$hdnResId.val('');
$hdnApptKey.val('');
$hdnSelDate.val('');
$hdnAdminAppt.val('');
} else if(data == 'ReserveError') {
alert('Reserve Problems')
} else if(data == 'CommunicationsError') {
alert('Problems')
} else {
$btn.val('Cancel');
ptrTimerArr.push(null);
var index = ptrTimerArr.length - 1;
ptrTimerArr[index] = window.setInterval(function(){tick(calId, index, duration)}, 1000);
$btn.data("timerIndex", index);
$hdnResId.val(data);
$hdnApptKey.val($apptKey);
$hdnSelDate.val($txtDate.val());
$txtDate.attr("disabled", true);
$lstAppt.attr("disabled", true);
}
},
error: function() {
alert("Unable to complete the task due to server error. Please try again later.")
$txtDate.val('');
$lstAppt.val('');
$lstAppt.attr("disabled", true);
$lstAppt.attr("style", 'width:150px;');
}
});
});
});
var ptrTimerArr = [];
var cntTickArr = [];
function tick(calId, index, duration) {
cntTickArr[index] = (cntTickArr[index] ? (cntTickArr[index] + 1000) : 1000);
var dt = new Date(duration - cntTickArr[index]);
var mins = ('0' + dt.getMinutes()).slice(-2);
var secs = ('0' + dt.getSeconds()).slice(-2);
$ticker = $("#TIMER_" + calId);
$ticker.html(mins + ":" + secs);
if(cntTickArr[index] == duration) {
window.clearInterval(ptrTimerArr[index]);
alert("Your temporary reservation date has expired. However, it may still be available. Please choose again.");
$ticker.html('');
$txtDate = $("#DATE_" + calId);
$txtDate.val('');
$txtDate.attr("disabled", false);
//I will probably need to use ajax to fetch a new textbox that will be formatted again for available dates.
//maybe it would be possible to just disable dates that have become unavailable since we started using the tool.
//
$lstAppt = $("#APPOINTMENT_" + calId);
$lstAppt.val('');
$lstAppt.attr("style", 'width:150px;');
$("#BUTTON_" + calId).val('Reserve');
}
}
// This is called in the beforeShowDay event
// So called multiple times for each date before the datepicker pops up
//It is looking through each date and underlining certain ones (only for admins)
function beforeShowApptDates(dt, calId) {
var isAdmin = ($("input[name='ADMIN_" + calId + "']").val() == "1" ? true : false);
var d = ("0" + dt.getDate()).slice(-2);
var m = ("0" + (dt.getMonth() + 1)).slice(-2);
var y = dt.getFullYear();
var res = [false, ''];
if(isAdmin) {
res = [true, ''];
}
var datestr = m + "-" + d + "-" + y;
var apptDays = $("#HDNDATES_" + calId).val().split(',');
//console.log(datestr);
//console.log('-------');
if($.inArray(datestr, apptDays) != -1) {
if(isAdmin) {
res = [true, 'underline'];
} else {
res = [true, ''];
}
}
return res;
}
// This is called in the onChangeMonthYear event
function setApptDates(calendarId, year, month) {
var isTest = ($("input[name='TEST_" + calendarId + "']").val() == "1" ? true : false);
var useNew = ($("input[name='NEW_" + calendarId + "']").val() == "1" ? true : false);
var mydata = {apptYear:year, apptMonth:month, calId:calendarId};
if(isTest) {
mydata["TEST"] = '1';
}
if(useNew) {
mydata["NEW"] = '1';
}
$.ajax({
url: "/apptSelect/dates",
data: mydata,
type: "POST",
async: false,
success: function(data) {
data = $.trim(data);
$("#HDNDATES_" + calendarId).val(data);
}
});
}
// This is called in the onSelect event
function fetchApptsAndNotes(dateText, ctrl) {
var calId = ctrl.id.split('_')[1];
var isTest = ($("input[name='TEST_" + calId + "']").val() == "1" ? true : false);
var useNew = ($("input[name='NEW_" + calId + "']").val() == "1" ? true : false);
var mydata = {seldate: dateText, "calId" : calId};
if(isTest) {
mydata["TEST"] = '1';
}
if(useNew) {
mydata["NEW"] = '1';
}
$.ajax({
url: "/apptSelect/fetch",
data: mydata,
type: "POST",
success: function(data) {
data = $.trim(data);
if(!data) {
return;
}
var arr = data.split('^^');
var notes = arr[0];
if(notes) {
notes = notes.split('>|<');
} else {
notes = [];
}
var isAdmin = ($("input[name='ADMIN_" + calId + "']").val() == "1" ? true : false);
var appts = arr[1];
if(appts) {
appts = appts.split('>|<');
} else {
appts = [];
}
$lst = $("#APPOINTMENT_" + calId);
var notepane = $("#NOTEPANE_" + calId);
var sHtml = '';
if(notes.length > 0) {
sHtml = '