/*
  parseUri 1.2.1
  (c) 2007 Steven Levithan <stevenlevithan.com>
  MIT License
*/

function parseUri (str) {
    var o   = parseUri.options,
        m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
        uri = {},
        i   = 14;

    while (i--) uri[o.key[i]] = m[i] || "";

    uri[o.q.name] = {};
    uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
        if ($1) uri[o.q.name][$1] = $2;
    });

    return uri;
};

parseUri.options = {
    strictMode: false,
    key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
    q:   {
        name:   "queryKey",
        parser: /(?:^|&)([^&=]*)=?([^&]*)/g
    },
    parser: {
        strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
        loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
    }
};

$.dr.wysiwyg.setDefaultConfig({
    Width: '100%',
    Height: '600',
    BasePath:DR.FCK_BASE_PATH,
    Config:{
        CustomConfigurationsPath:('ems'==parseUri(window.location).directory.substr(1,3))?'/ems/js/fck/config.js':'/ems/js/fck/config_limited.js'
    }
});

var DISABLE_DUPLICATE_SESSION_CHECKER = false;
function session_error() {
    if (typeof window.DISABLE_DUPLICATE_SESSION_CHECKER == 'undefined' || window.DISABLE_DUPLICATE_SESSION_CHECKER != true) {
        top.location.href="/two/phase2/echo2.0/error_session.php";
    } else {
        CHECKSESS = setInterval("checksess()", 5000);
    }
}
function checksess() {
    var cookie_session_id = get_cookie('PHPSESSID');
    if (cookie_session_id != SESSION_ID && (typeof window.DISABLE_DUPLICATE_SESSION_CHECKER == 'undefined' || window.DISABLE_DUPLICATE_SESSION_CHECKER != true)) {
        clearInterval(CHECKSESS);
        setTimeout("session_error()", 5000);
    }
}
function get_cookie(cookieName) {
    var results = document.cookie.match ( '(^|;) ?' + cookieName + '=([^;]*)(;|$)' );
    if (results){
        return (unescape(results[2]));
    } else {
        return null;
    }
}

var basePath = parseUri(window.location).directory;
if ('ems/auth/' != basePath.substr(1,9) && 'admin/auth/' != basePath.substr(1,11) && 'wizard' != basePath.substr(1,6)) {
    var CHECKSESS;
    $(document).ready(function(){
        var sessionTimeout = setTimeout("ModalOverlay.open('/two/phase2/echo2.0/countdown.htm', 400, 300, 'Session Expiration')",2400000);
        if (typeof SESSION_ID != 'undefined' && (typeof window.DISABLE_DUPLICATE_SESSION_CHECKER == 'undefined' || window.DISABLE_DUPLICATE_SESSION_CHECKER != true)) {
            CHECKSESS = setInterval("checksess()", 5000);
        }
    });
    $(window).bind('beforeunload', function(){
        window.DISABLE_DUPLICATE_SESSION_CHECKER = true;
        clearInterval(CHECKSESS);
    });
}


/*
 * Setup the path variables
 */
DR.FRAMEWORK_PATH = '/drui/';
DR.IMAGE_PATH = '/drui/images/';
DR.BLANK_PAGE_URL = '/drui/html/Blank.html';
DR.FCK_BASE_PATH = '/drui/external/fckeditor/';

var userLocale = "en_US"; 

$.fn.insertAtCaret = function (myValue) {
        return this.each(function(){
                //IE support
                if (document.selection) {
                        this.focus();
                        sel = document.selection.createRange();
                        sel.text = myValue;
                        this.focus();
                }
                //MOZILLA/NETSCAPE support
                else if (this.selectionStart || this.selectionStart == '0') {
                        var startPos = this.selectionStart;
                        var endPos = this.selectionEnd;
                        var scrollTop = this.scrollTop;
                        this.value = this.value.substring(0, startPos)
                                      + myValue
                              + this.value.substring(endPos,
this.value.length);
                        this.focus();
                        this.selectionStart = startPos + myValue.length;
                        this.selectionEnd = startPos + myValue.length;
                        this.scrollTop = scrollTop;
                } else {
                        this.value += myValue;
                        this.focus();
                }
        });

};
/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
$.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
