﻿/* <![CDATA[
/* Helper functions */

/// <summary>
///        Returns the appointments in the specified period filtered by the specified resource.
/// </summary>
/// <param name="scheduler">
///        RadScheduler's client-side object
/// </param>
/// <param name="start" type="Date">
///        The start of the period
/// </param>
/// <param name="end" type="Date">
///        The end of the period
/// </param>
/// <param name="resource">
///        The resource (room or user) to filter by
/// </param>
/// <param name="appointment">
///        The current appointment
/// </param>
function getAppointmentsInRangeByResource(scheduler, start, end, resource, appointment) {
    //Get all appointments within the specified time period
    var result = scheduler.get_appointments().getAppointmentsInRange(start, end);

    //If specified remove the appointment from the appointment list
    if (appointment)
        result.remove(appointment);

    //Filter the appointments based on the resource
    return result.findByResource(resource);
}

/// <summary>
///        Checks if the user associated with the specified appointment has another
///        appointment in the specified time period
/// </sumary>
/// <param name="scheduler">
///        RadScheduler's client-side object
/// </param>
/// <param name="start" type="Date">
///        The start of the period
/// </param>
/// <param name="end" type="Date">
///        The end of the period
/// </param>
/// <param name="appointment">
///        The current appointment
/// </param>
function isUserOccupied(scheduler, start, end, appointment) {
    //get the "User" resource associated with the appointment
    var currentUser = appointment.get_resources().getResourcesByType("User").getResource(0);
    //get all appointments in this period which are associated with this resource
    var appointmentsForThisUser = getAppointmentsInRangeByResource(scheduler, start, end, currentUser, appointment);
    //if the list of appointments is not empty the user has other appointments besides the specified in this time period
    return appointmentsForThisUser.get_count() > 0;
}
/// <summary>
///        Checks if there are other appointments within the specified time slot
/// </sumary>
/// <param name="scheduler">
///        RadScheduler's client-side object
/// </param>
/// <param name="start" type="Date">
///        The start of the period
/// </param>
/// <param name="end" type="Date">
///        The end of the period
/// </param>
/// <param name="slot">
///        The time slot to check
/// </param>            
function isRoomOccupied(scheduler, start, end, slot, appointment) {
    //get the "Room" resource associated with the time slot
    var currentRoom = slot.get_resource();
    //get all appointments for this "room" in the specified period
    var appointmentsForThisRoom = getAppointmentsInRangeByResource(scheduler, start, end, currentRoom, appointment);
    //if the list of appointments is not empty there are other appointments in this slot
    return appointmentsForThisRoom.get_count() > 0;
}

/// <summary>
///        Checks if the specified time slot is occupied and warns to prevent appointment rescheduling.
///        Called by the resizeEnd and moveEnd client-side event handlers.
/// </sumary>
function warnIfOccupied(start, end, sender, args) {
    var slot = args.get_targetSlot();
    var appointment = args.get_appointment();
       
    if (isUserOccupied(sender, start, end, appointment)) {
        alert("This user is occupied in this time period.");
        args.set_cancel(true);
    }
    else if (isRoomOccupied(sender, start, end, slot, appointment)) {
        alert("This room is not available in this time period.");
        args.set_cancel(true);
    }

    appointment.get_element().style.border = "";
}
/// <summary>
///        Checks if the specified time slot is occupied and visually shows it to the user.
///        Called by the resizeing and moving client-side event handlers.
/// </sumary>
function highlightIfOccupied(start, end, sender, args) {
    var appointment = args.get_appointment();
    var slot = args.get_targetSlot();

    if (isUserOccupied(sender, start, end, appointment) || isRoomOccupied(sender, start, end, slot, appointment)) {
        appointment.get_element().style.border = "1px solid red";
        return;
    }

    appointment.get_element().style.border = "";
}

function onAppointmentResizing(sender, args) {
    var start = args.get_appointment().get_start();
    var end = args.get_targetSlot().get_endTime();

    highlightIfOccupied(start, end, sender, args);
}

function onAppointmentResizeEnd(sender, args) {
    var start = args.get_appointment().get_start();
    var end = args.get_targetSlot().get_endTime();

    warnIfOccupied(start, end, sender, args);

    var slotElement = $telerik.$(args.get_targetSlot().get_domElement());
    if (slotElement.is(".Disabled") || slotElement.parent().is(".Disabled")) {
        // Prevent appointment move to disabled timeslots.
        alert("This venue is not available in this time period.");
        args.set_cancel(true);
    }
}

function onAppointmentMoving(sender, args) {
    var start = args.get_targetSlot().get_startTime();
    var end = new Date(start.getTime() + args.get_appointment().get_duration());
    highlightIfOccupied(start, end, sender, args);      
}

function onAppointmentMoveEnd(sender, args) {
    var start = args.get_targetSlot().get_startTime();
    var end = new Date(start.getTime() + args.get_appointment().get_duration());

    var slotElement = $telerik.$(args.get_targetSlot().get_domElement());
    if (slotElement.is(".Disabled") || slotElement.parent().is(".Disabled")) {
        // Prevent appointment move to disabled timeslots.
        alert("This venue is not available in this time period.");
        args.set_cancel(true);
    }
        
    warnIfOccupied(start, end, sender, args);
}

function onAppointmentInserting(sender, args) {
    var slot = args.get_targetSlot();
    var start = slot.get_startTime();
    var end = slot.get_endTime();

    var slotElement = $telerik.$(args.get_targetSlot().get_domElement());
    if (slotElement.is(".Disabled") || slotElement.parent().is(".Disabled")) {
        // Prevent appointment inserting on holidays.
        return false;
        //args.set_cancel(true);
    }
    else if (isRoomOccupied(sender, start, end, slot)) {
        alert("This room is not available in this time period.");
        return false;
        //args.set_cancel(true);
    }
}

function insertAppointment(sender, eventArgs) {
    var slot = eventArgs.get_targetSlot();
    var start = slot.get_startTime();
    var end = slot.get_endTime();
    var slotElement = $telerik.$(eventArgs.get_targetSlot().get_domElement());
    if (slotElement.is(".Disabled") || slotElement.parent().is(".Disabled")) {
        // Prevent appointment inserting on holidays.
        // eventArgs.set_cancel(true);
    }
    else if (isRoomOccupied(sender, start, end, slot)) {
        alert("This room is not available in this time period.");
        return false;
        // args.set_cancel(true);
    }
    else {
        sender.showInsertFormAt(slot);
    }
}

function editAppointment(sender, e) {
    var apt = e.get_appointment();
    sender.editAppointmentWithConfirmation(apt);
}

function pageLoad() {
    $telerik.$(".Disabled").bind("contextmenu", function (e) {
        return false;
    });
} 
