﻿$(function() {
    scripts.init();
});

scripts = {
    $_modal: {},
    $_modalIFrame: {},
    init: function() {
        $('.collapsable-area-header').each(function() {
            $(this).prepend('<span></span>');
            $(this).click(function() {
                $(this).find('span').toggleClass('collapse-arrow-close');
                $(this).next('.collapsable-area-content').toggle('fast');
                return false;
            });
        });

        /* tabs */
        $('.jquery-tabs').tabs({
            show: function() {
                var sel = $(this).tabs('option', 'selected');
                $hidSelectedTab = $('input[id$=hidSelectedTab]');
                if ($hidSelectedTab.length > 0) {
                    $hidSelectedTab.val(sel);
                }
            },
            selected: $('input[id$=hidSelectedTab]').val()
        });

        /* datepickers */
        $('.datepicker').datepicker();
        $('.datepicker-future').datepicker({ minDate: new Date() });
        $('.datepicker-textbox').datepicker({ showOn: 'both', buttonImage: '/images/calendar.gif', buttonImageOnly: true });
        $('.datepicker-textbox-future').datepicker({ minDate: new Date(), showOn: 'both', buttonImage: '/images/calendar.gif', buttonImageOnly: true });

        /* modals */
        scripts.$_modal = $('.modal').dialog({ closeText: '', resizable: false, modal: true, autoOpen: false, dialogClass: 'modal' });
        $('a.modal-link').live('click', function() { scripts.$_modal.dialog('open'); });
        /* iframe modals */
        scripts.$_modalIFrame = $('.modal-iframe').dialog({ closeText: '', resizable: false, modal: true, autoOpen: false, dialogClass: 'modal modal-iframe' });
        $('a.modal-iframe-link').live('click', function() {
            var href = this.href;
            var width = 768;
            var height = ($(window).height() < 600 ? $(window).height() - 17 : 600);
            scripts.$_modalIFrame.html('<iframe src="' + href + '" scrolling="' + (height < 600 ? 'yes' : 'no') + '" frameborder="0" width="' + width + '" height="' + height + '"><p>Please update your browser to use this feature.</p></iframe>');
            scripts.$_modalIFrame.dialog({
                autoOpen: true,
                width: width,
                height: height
            });
            return false;
        });
    }
}

function RedirectToLogin() {
    window.location = "/membership/memberlogin.aspx";
}

function EnterKeyEvent(clientId, e) {
    var evtobj = window.event ? event : e;
    var keycode = evtobj.keyCode ? evtobj.keyCode : evtobj.charCode;
    if (keycode == 13) {
        $('#' + clientId).click();
        evtobj.returnValue = false;
    }
}

function ClearOnFocus(clientId, text) {
    $control = $('#' + clientId)
    if ($control.val() == text) {
        $control.val('');
    }
}

function ClearOnBlur(clientId, text) {
    $control = $('#' + clientId);
    if ($control.val() == '' || $control.val() == text) {
        $control.val(text);
    }
}

function parseJSONDates(v) {
    if (v != null) {
        if (v.constructor == String) {
            if (v.startsWith('/Date')) {
                v = new Date(parseInt(v.replace(/\/Date\((-?\d+)\)\//gi, "$1")));
            }
        }
        else if (v.constructor == Object || v.constructor == Array) {
            $.each(v, function(k, x) {
                v[k] = k != "__type" ? parseJSONDates(x) : x;
            });
        }
    }
    return v;
};

function getDayOfWeek(v) {
    var weekday = new Array(7);
    weekday[0] = "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Tuesday";
    weekday[3] = "Wednesday";
    weekday[4] = "Thursday";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";
    return weekday[(new Date(v)).getDay()];
};

$.strPadLeft = function(i, l, s) {
    var o = i.toString();
    if (!s) { s = '0'; }
    while (o.length < l) {
        o = s + o;
    }
    return o;
};
$.strPadRight = function(i, l, s) {
    var o = i.toString();
    if (!s) { s = '0'; }
    while (o.length < l) {
        o = o + s;
    }
    return o;
};

String.prototype.trim = function() {
    return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
}

String.prototype.startsWith = function(str) {
    return (this.match("^" + str) == str);
}

String.prototype.endsWith = function(str) {
    return (this.match(str + "$") == str);
}
