﻿
// default, app-wide DisplayDate format, can be overridden as needed.
var DDFORMAT = "mmm dd yyyy";


// due to quirks in Netscape the xAddEventListener for GlobalPageLoad function
// is at the bottom of the ContentPages.aspx file.
//
var isNetscape = (navigator.userAgent.toLowerCase().indexOf("netscape") > -1);
function GlobalPageLoad() {
    // this function is invoked on the initial page load (!Page.IsPostBack)
    if ($get('ctl00_skipJump').value == 'true')
        SetFormFieldFocus();
    $get('ctl00_skipJump').value = 'true';
    if (isNetscape)
        CheckForResize();
}
function SetFormFieldFocus(fieldId) {
    try {
        if (!fieldId) {
            for (f = 0; f < document.forms.length; f++) {
                for (e = 0; e < document.forms[f].length; e++) {
                    var field = document.forms[f][e];
                    if (field.type == "text" && field.disabled == false) {
                        field.focus();
                        return;
                    }
                }
            }
        }
        else if (typeof fieldId == "string")
            $get(fieldId).focus();
        else
            fieldId.focus();
    }
    catch (err) {
        // IGNORE ANY ERRORS...
    }
}
function CheckForResize() {
    var content = $get("ctl00_upMainContent");
    content.style.height = 'auto';
    var w = content.offsetWidth;
    var h = content.offsetHeight;
    content.style.width = w + "px";
    content.style.height = h + "px";
    //      window.resizeTo(window.offsetWidth, window.offsetHeight);
    setTimeout("CheckForResize()", 1000);
}

//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 xAddEventListener(e, eT, eL, cap) {
    if (!(e = xGetElementById(e)))
        return;
    eT = eT.toLowerCase();
    if (e.addEventListener)
        e.addEventListener(eT, eL, cap || false);
    else if (e.attachEvent)
        e.attachEvent('on' + eT, eL);
    else {
        var o = e['on' + eT];
        e['on' + eT] = typeof o == 'function' ? function(v) { o(v); eL(v); } : eL;
    }
}
function xGetElementById(e) {
    if (typeof (e) == "string") {
        if (document.getElementById) e = document.getElementById(e);
        else if (document.all)
            e = document.all[e];
        else
            e = null;
    }
    return e;
}
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 EnableUI(state) {
    if (!state) {
        _backgroundElement.style.display = '';
        _backgroundElement.style.position = 'absolute';
        _backgroundElement.style.left = '0px';
        _backgroundElement.style.top = '0px';

        var clientBounds = this._getClientBounds();
        var clientWidth = clientBounds.width;
        var clientHeight = clientBounds.height;
        _backgroundElement.style.width = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientWidth) + 'px';
        _backgroundElement.style.height = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientHeight) + 'px';
        _backgroundElement.style.zIndex = 10000;
        _backgroundElement.className = 'modalWait';
    }
    else {
        _backgroundElement.style.display = 'none';
    }
}
function _getClientBounds() {
    var clientWidth;
    var clientHeight;
    switch (Sys.Browser.agent) {
        case Sys.Browser.InternetExplorer:
            clientWidth = document.documentElement.clientWidth;
            clientHeight = document.documentElement.clientHeight;
            break;
        case Sys.Browser.Safari:
            clientWidth = window.innerWidth;
            clientHeight = window.innerHeight;
            break;
        case Sys.Browser.Opera:
            clientWidth = Math.min(window.innerWidth, document.body.clientWidth);
            clientHeight = Math.min(window.innerHeight, document.body.clientHeight);
            break;
        default:  // Sys.Browser.Firefox, etc.
            clientWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
            clientHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
            break;
    }
    return new Sys.UI.Bounds(0, 0, clientWidth, clientHeight);
}
