/*
  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)) {
    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();
                }
        });

};

