﻿var _usefulldatepattern = "dd/mm/yy";

$(function () {
    globalSetDateMask();
    globalSetAmountMask();
    $('table.form-table').attr('cellpadding', '5px');
    $('input[type=text].form-input').css("width", "100%");
});

function globalSetDateMask() {

    $('input.date').datepicker({
        dateFormat: _usefulldatepattern,
        duration: 0,
        selectOtherMonths: true,
        showAnim: '',
        showButtonPanel: true
    });

    $('input.date').live('blur', function (event) {
        if (!validDate($(this))) {
            $(this).datepicker('hide');
            $(this).datepicker('disable');
            globalAlert('Invalid Date', '<strong>\'' + $(this).val() + '\'</strong> is not a valid date. Please select a date from the calendar.', 2);
            event.preventDefault();
            $(this).datepicker('enable')
            $(this).focus();
        }

    });
};


function globalSetAmountMask() {
    $('input.amount').numericinput();
};


// global alert popup
function globalAlert(title, msg, type, buttons) {
    if (title == undefined || title == null || title == '') {
        title = '';
    }
    if (msg == undefined || msg == null || msg == '') {
        return;
    }
    if (type == undefined || type == null || isNaN(type) || type < 0 || type > 2) {
        type = 0;
    }
    var icon = type == 0 ? 'url("/Content/images/notifications/ok.png")' :
                   type == 1 ? 'url("/Content/images/notifications/warn.png")' :
                               'url("/Content/images/notifications/error.png")';
    if (buttons == undefined || buttons == null) {
        buttons = { 'Close': function () { $(this).dialog('close'); } };
    }
    $('<div><div style="margin-left: 60px;">' + msg + '</div></div>')
                .css('background-image', icon)
                .css('background-repeat', 'no-repeat')
                .css('background-position', '10px 10px')
                .css('padding-top', '10px')
                .css('padding-bottom', '10px')
                .dialog({
                    modal: true,
                    autoOpen: true,
                    title: title,
                    resizable: false,
                    width: 380,
                    buttons: buttons
                });
};

Array.prototype.addUnique = function (valueToAdd) {
    if (this == undefined)
        return [valueToAdd];
    for (var i = 0; i < this.length; i++) {
        if (this[i] == valueToAdd) {
            return;
        }
    }
    this.push(valueToAdd);
    return;
};

function validDate(element, pattern) {
    if (element == undefined ||
        element == null) return false;

    if (pattern == undefined || pattern == null || pattern == '') {
        pattern = _usefulldatepattern;
    }

    // date is expected in the 'pattern' pattern, or empty
    var proposedDate = $(element).val();

    if (proposedDate == undefined ||
        proposedDate == null ||
        proposedDate.length == 0) return true;

    if (proposedDate.length < pattern.length) return false;

    var idxDay = pattern.indexOf("dd", 0),
        idxMonth = pattern.indexOf("mm", 0),
        idxYear = pattern.indexOf("yy", 0);

    var valDay = proposedDate.substr(idxDay, 2),
        valMonth = proposedDate.substr(idxMonth, 2),
        valYear = proposedDate.substr(idxYear, 4);

    var intDay = parseInt(valDay, 10),
        intMonth = parseInt(valMonth, 10),
        intYear = parseInt(valYear, 10);

    if (!isNaN(intDay) && intDay > 0 && intDay < 32 &&
        !isNaN(intMonth) && intMonth > 0 && intMonth < 13 &&
        !isNaN(intYear) && intYear > 1900 && intYear < 3000) {

        return (intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) ||
               (intMonth == 2 && intDay < 29) ||
               (intMonth == 2 && ((intYear % 4 == 0) || (intYear % 100 == 0) || (intYear % 400 == 0)) && intDay < 30) ||
               (intMonth != 2 && intDay < 31);
    }
    return false;
};



