﻿// $(document).ready(...);
//$(function() {
//    $('ul.jd_menu').jdMenu();
//    // Add menu hiding on document click
//    $(document).bind('click', function() {
//        $('ul.jd_menu ul:visible').jdMenuHide();
//    });
//});

$(document).ready(function () {
    // Disable caching of AJAX responses
    $.ajaxSetup({ cache: false, async: true });

    /*
    $(".ajaxError").ajaxError(function () {
        $(this).show('slow');
    });

    $("div.ajaxError").click(function () {
        $(this).hide('slow');
    });
    */

    $('.ajaxLoadingSpinner').hide();

    $('.ajaxLoadingSpinner').ajaxStart(function () {
        $(this).show();
    }).ajaxStop(function () {
        $(this).hide();
    });
});

    function setBestBeforeToday(textBox) {
        var bestBeforeDate = Date.today();
        setTextBoxValue(textBox, bestBeforeDate);
    }
    
    function increaseBestBefore(textBox, addMonths, addDays) {

        var bestBeforeDate;

        // If empty use todays date, otherwise use the date in the text box.
        if (textBox.value == "" ) {
            bestBeforeDate = Date.today();
        } else {
            bestBeforeDate = Date.parse(textBox.value);
        }
        
        bestBeforeDate = bestBeforeDate.add( { days: addDays, months: addMonths } );
        
        setTextBoxValue(textBox, bestBeforeDate);
    }
    
    function setTextBoxValue(textBox, value) {
        textBox.value = value.toString("dd MMM yyyy");
    }


    // Date helpers.
    
    function setNoDate() {
        $(".dateEntry").val("");
    }

    function setDateToday() {
        // Set the best before date textbox to todays date.
        $(".dateEntry").val(getFormattedDate(Date.today()));
    }

    function setDateYesterday() {
        // Set the best before date textbox to yesterdaysdate.
        $(".dateEntry").val(getFormattedDate(Date.today().add(-1).days()));
    }

    function increaseDate(addYears, addMonths, addDays) {

        var bestBeforeDate;
        var bestBeforeDateText = $(".dateEntry").val();

        // If empty use todays date, otherwise use the date in the text box.
        if (bestBeforeDateText == "") {
            bestBeforeDate = Date.today();
        } else {
            bestBeforeDate = Date.parse(bestBeforeDateText);
        }

        if (addDays) {
            bestBeforeDate.add(addDays).days();
        }

        if (addMonths) {
            bestBeforeDate.add(addMonths).months();
        }

        if (addYears) {
            bestBeforeDate.add(addYears).years();
        }

        $(".dateEntry").val(getFormattedDate(bestBeforeDate));
    }

    function getFormattedDate(value) {
        return value.toString("dd MMM yyyy");
    }

    // Ajax request failed.
    function ajaxFailed(xhr, textStatus, errorThrown) {

        $("#loading").toggle();

        if (xhr.responseText) {

            var err = JSON.parse(xhr.responseText);

            if (err) {
                alert(err.Message);
            } else {
                alert({ Message: "Unknown server error." });
            }
        }

        return;
    }

