﻿
// IE has a bug that BasicDatePicker uncovers where the 'onbeforeunload' window event is
// triggered whenever a day is clicked in the BDP calendar. The ready function below is a
// patch that detects and mitigates this bug in IE only.

var skipUnload = false;
$().ready(function() {
    if ($.browser.msie) {
        $('.bdpDay a').live('click', function() {
            skipUnload = true;
        });
    };
});
window.onbeforeunload = confirmExit;
function confirmExit() {
    if (!skipUnload) {
        var changesMade = $('#ctl00_changesMade') && $('#ctl00_changesMade').val() == 'true';
        if (changesMade)
            return "If you leave this page any changes not saved will be lost. Click 'Cancel' to return to the page and then 'Save' the data.";
        EnableUI(false);
    };
    skipUnload = false;
};
Sys.Application.add_init(initHandler);
Sys.Application.add_load(pageLoadedHandler);

function initHandler(sender) {
    if (typeof Sys.WebForms != 'undefined') {
        var manager = Sys.WebForms.PageRequestManager.getInstance();
        manager.add_beginRequest(beginRequestHandler);
    };
};
function beginRequestHandler(sender, args) {
    EnableUI(false);
};
function pageLoadedHandler(sender, args) {
    var setFocus = true;
    if (typeof Sys.WebForms != 'undefined') {
        var manager = Sys.WebForms.PageRequestManager.getInstance();
        if (manager && manager._postBackSettings && manager._postBackSettings.async)
            setFocus = false;
    };
    EnableUI(true);
    if ($('#ctl00_skipJump').val() == 'true') {
        if (setFocus) {
            $('input:text:visible:enabled:first').focus();
        };
    }
    else {
        window.scrollTo(0, 0);
        $('#ctl00_skipJump').val('true');
    };
};

// There is a bug in all modern browsers (except FF) that doesn't change the cursor after an async event
// until the mouse is moved. The workaround I've found is to NOT change the cursor during blocking...
//
var uiIsEnabled = true;
var uiIsBlocked = false;

function EnableUI(state) {
    if (state == true && uiIsBlocked == true) {
        $.unblockUI({ fadeOut: 0 });
    };
    uiIsEnabled = state;
    if (uiIsEnabled == true)
        return;        
    setTimeout(function() {
        if (uiIsEnabled)
            return;
        uiIsBlocked = true;
        $.blockUI({
            message: "<div style='font-family: Verdana; font-size: 16px; padding-top: 5px; padding-bottom: 5px;'>Processing ... Please Wait</div>",
            fadeIn: 0,
            css: {
                color: '#000',
                cursor: 'default'
            },
            overlayCSS: {
                cursor: 'default',
                backgroundColor: '#BEBEBE'
            },
            onUnblock: function() {
                uiIsBlocked = false;
            }
        });
    }, 500);
};

// default, app-wide DisplayDate format, can be overridden as needed.
var DDFORMAT = "mmm dd yyyy";


// Left and Right trim functions...
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
};
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
};

//Repeat a string 'many' times
String.prototype.repeat = function(many) {
    var s = '';
    var t = this.toString();
    while (--many >= 0)
        s += t;
    return s;
};
//Left fill a number with zero so that the result has a length of 'many'
Number.prototype.addZero = function(many) {
    return ('0'.repeat(many) + this.toString()).substring(('0'.repeat(many) + this.toString()).length - many);
};
//Returns a formatted date from a string input
//string must be Date.parse compatible or 'yyyymmdd'
String.prototype.display = function(format) {
    return DisplayDate(this.toString(), format);
};
Date.prototype.display = function(format) {
    return DisplayDate(this, format);
};
// DON'T USE 'DisplayDate' FUNCTION DIRECTLY, USE A '.display()' WITH DATES (string or Date types)
// EXAMPLE FOR CURRENT DATE WOULD BE '(new Date()).display()'
// CAN OPTIONALLY PASS A FORMAT (rules below)
// Valid formats are: mmmm = full month name, mmm = 3 char month abrev, mm = 2 digit month, m = 1/2 digit month
//                    dd = 2 digit day, d = 1/2 digit day
//                    yyyy = 4 digit year, yy = last 2 digits of year
//                    other characters left 'as is'

var DDMONTHS = "January|February|March|April|May|June|July|August|September|October|November|December".split("|");

function DisplayDate(date, format) {
    var datein;
    if (typeof date == "undefined" ||
           (date.toString().toLowerCase() == "now"))
        datein = new Date();
    else if (typeof date == "string") {
        if (date.length == 8)
            datein = new Date(date.substring(0, 4),
                                      date.substring(4, 6) - 1,
                                      date.substring(6));
        else
            datein = new Date(date);
    }
    else if (date.constructor == Date)
        datein = date;
    else {
        alert("DisplayDate(date, format) function received an invalid 'date' parameter.\n\n" +
                  "This parameter is optional, but when supplied must be 'Now' (for current date),\n" +
                  "a 'dateParse' compatible string (ex., 'Jul 4, 1776' or a Date() object.\n\n" +
                  " An invalid typeof(" + typeof (date) + ") was received.\n");
        return "undefined";
    }
    var day = datein.getDate();
    var month = datein.getMonth() + 1;
    var year = datein.getFullYear();

    if (typeof format == "undefined")
        format = DDFORMAT;
    var isMonthUpperCase = (format.indexOf("M") > -1);
    format = format.toLowerCase();

    var m2use = month;
    if (format.indexOf("mmmm") > -1)
        m2use = DDMONTHS[month - 1];
    else if (format.indexOf("mmm") > -1)
        m2use = DDMONTHS[month - 1].substring(0, 3);
    else if (format.indexOf("mm") > -1)
        m2use = month.addZero(2);

    var d2use = day;
    if (format.indexOf("dd") > -1)
        d2use = day.addZero(2);

    var y2use;
    if (format.indexOf("yyyy") > -1)
        y2use = year;
    else
        y2use = (year + "").substring(2);

    format = format.replace("yyyy", "y");
    format = format.replace("yyy", "y");
    format = format.replace("yy", "y");
    format = format.replace("y", y2use);
    format = format.replace("dd", "d");
    format = format.replace("d", d2use);
    format = format.replace("mmmm", "m");
    format = format.replace("mmm", "m");
    format = format.replace("mm", "m");
    format = format.replace("m", (isMonthUpperCase ? m2use.toUpperCase() : m2use));

    return format;
};
function fckClass() {
    this.UpdateEditorFormValue = function() {
        for (i = 0; i < parent.frames.length; ++i)
            if (parent.frames[i].FCK) {
            parent.frames[i].FCK.UpdateLinkedField();
            alert("Just did FCK.UpdateLinkedField(), parent.frames[" + i + "]");
        }
    }
};
// instantiate the class
var fckObject = new fckClass();

function addCommas(nStr) {
    nStr += '';
    var x = nStr.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    };
    return x1 + x2;
};
