﻿function setGreetingBasedOnUsersClock() {
    var d = new Date();
    if (d.getHours() >= 2 && d.getHours() < 12) {
        $(".spGreeting").html("Good Morning");
    } else if (d.getHours() >= 12 && d.getHours() < 17) {
        $(".spGreeting").html("Good Afternoon");
    } else if (d.getHours() >= 17 || d.getHours() < 2) {
        $(".spGreeting").html("Good Evening");
    }
}
function setupLogin(lnkLogInThenAddClientID) {
    var myLightbox = new CCHMC.Lightbox('#' + lnkLogInThenAddClientID, { zIndex: 887, type: 'show', width: 360, height: 200, directlightbox: $('#loginLightbox') });
    $('.lightbox_content .txtUserName1_Visible').focus();

}
function showLightbox(content, width, height, lightboxType, actLightBox) {
    var myLightbox = new CCHMC.Lightbox(content, { type: lightboxType, width: width, height: height, directlightbox: actLightBox });
    if (lightboxType != 'show') {
        myLightbox.getContentArea().append(content.html());
        myLightbox.open();
    }
};


function submitEnter(myfield, e, button) {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13) {
        button.click();
        return false;
    }
    else
        return true;
};

/* Favorites functionality */

function setButtonVisibility() {
    var jsonParameters = "{ 'favoriteName':'" + escape(document.title) + "',"
            + "'favoriteUrl':'" + escape(document.URL) + "' }";

    $.ajax({
        type: "POST",
        url: "/Services/Favorites.asmx/GetFavoriteId",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: jsonParameters,
        success: function(data) {
            if (data.d == "0") {
                $('#lnkFavoriteAdd').show();
                $('#addToFavoritesLabel').html('Add to MyPages');
                $('#lnkFavoriteRemove').hide();
            }
            else {
                $('#lnkFavoriteAdd').hide();
                $('#removeFromFavoritesLabel').html('Remove from MyPages');
                $('#lnkFavoriteRemove').show();
            }
        }
    });
};

function showDescriptionLightbox() {
    showLightbox($('#pagetools_lb_desc_container'), 500, 280, 'inline', null);
    $('.lightbox_content #spanPageTitle').html(document.title);
    $('.lightbox_content #btnSaveFavorite').attr('disabled', false);
    $('.lightbox_content #btnSaveAndGoToMyPages').attr('disabled', false);
    $('.lightbox_content #favoriteDescription').attr('disabled', false);
    $('.lightbox_content #favoriteDescription').val('');
    $('.lightbox_content #favoriteDescription').focus();
};

function closeDescriptionLightbox() {
    $('.close').click();
};

function doAjaxAddLink(redirectUrl) {
    $('.lightbox_content #spanDescriptionLabel').html('Saving...');
    $('.lightbox_content #favoriteDescription').attr('disabled', true);
    $('.lightbox_content #btnSaveFavorite').attr('disabled', true);
    $('.lightbox_content #btnSaveAndGoToMyPages').attr('disabled', true);
    $('.lightbox_content #btnCancelFavorite').val('Close');

    var jsonParameters = "{ 'directoryId':'0',"
            + "'favoriteUrl':'" + escape(document.URL) + "',"
            + "'favoriteName':'" + escape(document.title) + "',"
            + "'favoriteDescription':'" + escape($('.lightbox_content #favoriteDescription').val()) + "' }"

    $.ajax({
        type: "POST",
        url: "/Services/Favorites.asmx/AddLink",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: jsonParameters,
        success: function(data) {
            if (data.d == "" && redirectUrl != "") {
                location.href = redirectUrl;
            }
            else if (data.d == "Not logged in") {
                alert("For your security, your session with the web server timed out. Please click \"Add to MyPages\" again to log in.");
                location.href = location.href;
            }
            else {
                if (data.d == "Maximum link count reached") {
                    alert("You have reached the maximum number of favorites. Please remove another favorite first.");
                }
                else if (data.d != "") {
                    alert("Error adding favorite. Please refresh the page and try again.");
                }
                setButtonVisibility();
                closeDescriptionLightbox();
            }
        }
    });
};

function doAjaxRemoveLink(favoriteIdToRemove) {
    $('#removeFromFavoritesLabel').html('Removing from MyPages...');

    var jsonParameters = "{ 'favoriteUrl':'" + escape(document.URL) + "',"
            + "'favoriteName':'" + escape(document.title) + "' }";

    $.ajax({
        type: "POST",
        url: "/Services/Favorites.asmx/RemoveLink",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: jsonParameters,
        success: function(data) {
            if (data.d == "") {
                setButtonVisibility();
            }
            else {
                alert('Error removing favorite. Please refresh the page and try again.');
            }
        }
    });
};

/* Sharing functionality */

function doAjaxSendEmail() {
    doClearErrorMessages();
    $('.lightbox_content #btn_send').attr('disabled', true);
    $('.lightbox_content .form_status').html("Sending...");

    var jsonParameters = "{ 'fromName':'" + escape($('.lightbox_content #form_from_name').val()) + "',"
            + "'fromAddress':'" + escape($('.lightbox_content #form_from_email').val()) + "',"
            + "'toAddress':'" + escape($('.lightbox_content #form_to_email').val()) + "',"
            + "'urlToShare':'" + escape(window.location.href) + "',"
            + "'contentId':'" + escape($('#contentId').val()) + "',"
            + "'token':'" + escape($('#serviceToken').val()) + "' }";

    $.ajax({
        type: "POST",
        url: "/Services/FavoritesMailer.asmx/ShareContentByEmail",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: jsonParameters,
        success: function(data) {
            var fromNameIsValid = data.d.FromNameIsValid;
            var fromAddressIsValid = data.d.FromAddressIsValid;
            var toAddressIsValid = data.d.ToAddressIsValid;
            var statusDescription = data.d.Description;

            $('.lightbox_content #form_from_name_error').css('visibility', fromNameIsValid ? 'hidden' : 'visible');
            $('.lightbox_content #form_from_email_error').css('visibility', fromAddressIsValid ? 'hidden' : 'visible');
            $('.lightbox_content #form_to_email_error').css('visibility', toAddressIsValid ? 'hidden' : 'visible');

            if (statusDescription == "Success" | statusDescription == "E-mail already sent") {
                $('.lightbox_content #form_status').html("Your e-mail has been sent.");
                doResetEmailForm();
                doClearErrorMessages();
            }
            else {
                if (statusDescription == "Invalid user input") {
                    $('.lightbox_content #form_status').html("Please fill in the required fields.");
                }
                else {
                    $('.lightbox_content #form_status').html("There was an error sending your e-mail. Please wait a moment and try again.");
                }
            }
            $('.lightbox_content #btn_send').attr("disabled", false);
        }
    });
};

function doResetEmailForm() {
    $('.lightbox_content #form_from_name').val('');
    $('.lightbox_content #form_from_email').val('');
    $('.lightbox_content #form_to_email').val('');
    $('.lightbox_content #btn_send').attr("disabled", false);
};

function doClearErrorMessages() {
    $('.lightbox_content #form_from_name_error').css('visibility', 'hidden');
    $('.lightbox_content #form_from_email_error').css('visibility', 'hidden');
    $('.lightbox_content #form_to_email_error').css('visibility', 'hidden');
};

function showEmailForm() {
    showLightbox($('#pagetools_lb_sharing_container'), 500, 280, 'inline', null);
    doResetEmailForm();
    doClearErrorMessages();
    $('.lightbox_content #form_status').html('');
    $('.lightbox_content #form_to_email').focus();
    return false;
};

/* Login functionality */

function showLogin(clearErrors, goToMyPages, lnkLogInThenAddClientID) {
    if (goToMyPages == true) {
        $('.nextAction').val('goToMyPages');
    }
    else {
        $('.nextAction').val('');
    }

    showLightbox('#' + lnkLogInThenAddClientID, 360, 200, 'show', $('.directlightbox'));
    $('.lightbox_content .txtUserName1_Visible').focus();
    if (clearErrors == true) {
        $('.lightbox_content .errormessage').toggle(false);
    }
};
