﻿var Doc = document;
function gid(id) { return Doc.getElementById(id); }
function gname(name) { return Doc.getElementByName ? Doc.getElementByName(name) : null; }
function gtag(tagName) { return Doc.getElementsByTagName ? Doc.getElementsByTagName(name) : new Array() }
function Trim(s) { if (s == null) return null; var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/); return (m == null) ? "" : m[1]; }
function ghtml(id, strHTML) { if (gid(id)) gid(id).innerHTML = strHTML; }
function gtext(id, strText) { if (gid(id)) gid(id).innerText = strText; }
function IsNumber(str) { return /^-?\d+$/.test(str); }
var Browser = {};
Browser.isIE = function () { return (document.all && window.ActiveXObject) ? true : false; };
Browser.isIE5Up = function () { return (Browser.isIE() && document.getElementById) ? true : false; };
Browser.isFirefox = function () { return (document.implementation && document.implementation.createDocument && !window.opera) ? true : false; };
Browser.isChrome = function () { return navigator.userAgent.toLowerCase().indexOf('chrome') != -1 };
Browser.isOpera = function () { return (window.opera) ? true : false; };

var __sto = setTimeout;
window.setTimeoutEx = function (callback, timeout, param) {
    var args = Array.prototype.slice.call(arguments, 2);
    var _cb = function () { callback.apply(null, args); }
    __sto(_cb, timeout);
}

function Hide(id) {
    if (typeof (id) != "object") id = gid(id);
    if (id) id.style.display = "none";
}
function Show(id) {
    if (typeof (id) != "object") id = gid(id);
    if (id) id.style.display = "";
}
function HtmlEncode(str) {
    str = Trim(str);
    str = str.replace(/&/g, "&amp;");
    str = str.replace(/</g, "&lt;");
    str = str.replace(/>/g, "&gt;");
    str = str.replace(/"/g, "&quot;");
    return str;
}
function HtmlDecode(text) {
    var re = { '&lt;': '<', '&gt;': '>', '&amp;': '&', '&quot;': '"' };
    for (var i in re) text = text.replace(new RegExp(i, 'g'), re[i]);
    return text;
}
function realLength(val) { return val.replace(/[^\x00-\xff]/g, "**").length; }
function IsShow(obj) { return (obj.style.display == "" || obj.style.display == "block"); }

function GetParent() {
    //    if (Browser.isIE())
    //        return window.dialogArguments;
    //    else
    //        return opener;
    return GetOpener();
}
function GetFrameIndex(obj, name) {
    if (obj == null) return -1;
    var j = obj.frames.length;
    for (var i = 0; i < j; i++) {
        if (obj.frames[i].name == name) {
            return i;
        }
    }
    return -1;
}

function OpenModalDialog(strurl, width, height, addrdm, needscroll) {
    var w;
    if (typeof (top.openWindow) == "function") {
        var url = strurl;
        if (addrdm) {
            if (strurl.indexOf("?") > 0)
                url = strurl + "&random=" + Math.random();
            else
                url = strurl + "?random=" + Math.random();
        }

        height += 60;
        width += 8;

        if (typeof (needscroll) == "undefined") {
            //            top.openWindow('auto','',strurl,width,height,'auto',window);
            w = top.openWindow('auto', '', strurl, width, height, 'auto', window);
        }
        else if (needscroll) {
            w = top.openWindow('auto', '', strurl, width, height, 'yes', window);
        }
        else {
            w = top.openWindow('auto', '', strurl, width, height, 'no', window);
        }
    }
    else {
        w = OpenModalDialog_Old(strurl, width, height, addrdm);
    }

    return w;
}

function OpenModalDialog_Old(strurl, width, height, addrdm) {
    var url = strurl;
    if (typeof (addrdm) == 'undefined' || addrdm == true) {
        if (strurl.indexOf("?") > 0)
            url = strurl + "&random=" + Math.random();
        else
            url = strurl + "?random=" + Math.random();
    }
    var str = "";
    height += 30;
    var r;
    if (Browser.isIE()) {
        str = "dialogHeight: " + height + "px; dialogWidth: " + width + "px; edge: Raised; center: Yes; status: No; resizable: no; help: no;resizable:yes;";
        r = window.showModalDialog(url, window, str);
    }
    else {
        var ah = screen.availHeight;
        var aw = screen.availWidth;
        var xc = (aw - width) / 2;
        var yc = (ah - height) / 2;

        if (typeof (window.showModalDialog) == "function") {
            str = "dialogHeight: " + height + "px; dialogWidth: " + width + "px;" + "px; dialogTop: " + yc + "px; dialogLeft: " + xc + "px;";
            r = window.showModalDialog(url, this, str);
        }
        else {
            str = "width=" + width + "px,height=" + height + "px,top=" + yc + "px,left=" + xc + "px,menubar=no,toolbar=no,location=no,center=yes,scrollbars=yes,resizable=no,status=no,modal=yes";
            r = window.open(url, '', str);
        }
    }
    if (r == "NoRefresh") return false;
    return r;
}

function OpenNoResizeModalDialog(strurl, width, height, addrdm, needscroll) {
    OpenModalDialog(strurl, width, height, addrdm, true);
    //    var url=strurl;
    //    if (typeof(addrdm)=='undefined' || addrdm==true) 
    //    {    
    //        if (strurl.indexOf("?")>0)
    //            url= strurl +"&random=" + Math.random();
    //        else
    //            url= strurl +"?random=" + Math.random();
    //    }    
    //    var str = ""; 
    //    height += 30;
    //    var r;
    //    if (Browser.isIE())
    //    { 
    //        str = "dialogHeight: " + height + "px; dialogWidth: " + width + "px; edge: Raised; center: Yes; status: No; resizable: no; help: no;resizable:no;";			
    //        r = window.showModalDialog(url, window, str);
    //    }
    //    else
    //    {
    //        var ah = screen.availHeight; 
    //        var aw = screen.availWidth; 
    //        var xc = (aw - width) / 2; 
    //        var yc = (ah - height) / 2; 
    //        
    //        if(typeof(window.showModalDialog)=="function")
    //        {
    //            str = "dialogHeight: " + height + "px; dialogWidth: " + width + "px;" + "px; dialogTop: " + yc + "px; dialogLeft: " + xc + "px; resizable: no;";
    //            r = window.showModalDialog(url,this,str);
    //        }
    //        else
    //        {
    //            str = "width=" + width + "px,height=" + height + "px,top="+yc+"px,left="+xc+"px,menubar=no,toolbar=no,location=no,center=yes,scrollbars=yes,resizable=no,status=no,modal=yes"; 
    //            r = window.open(url,'',str);
    //        }
    //    }
    //    if (r=="NoRefresh") return false;
    //    return r;
}

function OpenModalessDialog(strurl, width, height) {
    var url;
    if (strurl.indexOf("?") > 0)
        url = strurl + "&random=" + Math.random();
    else
        url = strurl + "?random=" + Math.random();

    var str = "";
    height += 30;
    var r;
    if (Browser.isIE()) {
        str = "dialogHeight: " + height + "px; dialogWidth: " + width + "px; edge: Raised; center: Yes; status: No; resizable: No; dialogHide:Yes; help: no;";
        r = window.showModelessDialog(url, window, str);
    }
    else {
        return PopupWindow(strurl, width, height);
    }
    return r;
}

function PopupWindow(strurl, width, height, addrdm) {
    var url = strurl;
    if (typeof (addrdm) == 'undefined' || addrdm == true) {
        if (strurl.indexOf("?") > 0)
            url = strurl + "&random=" + Math.random();
        else
            url = strurl + "?random=" + Math.random();
    }
    var str = "";
    height += 30;
    var r;
    var ah = screen.availHeight;
    var aw = screen.availWidth;
    var xc = (aw - width) / 2;
    var yc = (ah - height) / 2;
    if (Browser.isIE()) {
        str = "width=" + width + "px,height=" + height + "px,top=" + yc + "px,left=" + xc + "px,menubar=no,toolbar=no,location=no,center=yes,scrollbars=yes,status=no,modal=no";
        r = window.open(url, '_blank', str);
        if (r != null) {
            try { r.focus(); } catch (ef) { }
        }
    }
    else {
        str = "width=" + width + "px,height=" + height + "px,top=" + yc + "px,left=" + xc + "px,menubar=no,toolbar=no,location=no,center=yes,scrollbars=yes,status=no,modal=no";
        r = window.open(url, '', str);
    }
    return r;
}
function _attachEvent(obj, evt, func) {
    if (obj == null) return;
    if (obj.addEventListener) {
        obj.addEventListener(evt, func, true);
    } else if (obj.attachEvent) {
        obj.attachEvent("on" + evt, func);
    } else {
        eval("var old" + func + "=" + obj + ".on" + evt + ";");
        eval(obj + ".on" + evt + "=" + func + ";");
    }
}
function _detachEvent(obj, evt, func) {
    if (obj == null) return;
    if (obj.removeEventListener) {
        obj.removeEventListener(evt, func, true);
    } else if (obj.detachEvent) {
        obj.detachEvent("on" + evt, func);
    } else {
        eval(obj + ".on" + evt + "=old" + func + ";");
    }
}
function _cancelBubble(e) {
    if (e.stopPropagation) e.stopPropagation();
    else e.cancelBubble = true;
}
function _cancelDefault(e) {
    if (e.preventDefault) e.preventDefault();
    else e.returnValue = false;
}
function AddOption(objID, text, value) {
    var oOption = document.createElement("OPTION");
    oOption.text = text;
    oOption.value = value;
    gid(objID).add(oOption);
}
function SelectOption(selectBox, selectValue) {
    var arOptions = selectBox.options;
    for (var i = 0, j = arOptions.length; i < j; i++) {
        if (selectValue == arOptions[i].value) {
            selectBox.options[i].selected = true;
            break;
        }
    }
}

function AdjustSessionID(url) {
    if (typeof (SessionID) != "undefined" && url.indexOf("SessionID") < 0) {
        if (url.indexOf("?") < 0)
            url += "?SessionID=" + SessionID;
        else
            url += "&SessionID=" + SessionID;
    }
    return url;
}
function AdjustUserID(url) {
    if (typeof (UserID) != "undefined" && url.indexOf("UserID") < 0) {
        if (url.indexOf("?") < 0)
            url += "?UserID=" + UserID;
        else
            url += "&UserID=" + UserID;
    }
    return url;
}
function AdjustProjectID(url) {
    if (typeof (ProjectID) != "undefined" && url.indexOf("ProjectID") < 0) {
        if (url.indexOf("?") < 0)
            url += "?ProjectID=" + ProjectID;
        else
            url += "&ProjectID=" + ProjectID;
    }
    return url;
}
function Adjust3ID(url) {
    url = AdjustSessionID(url);
    url = AdjustUserID(url);
    url = AdjustProjectID(url);
    return url;
}
function ReplaceUrlParam(url, key, value) {
    var re = new RegExp("(\\?|&)" + key + "=\\d*", "ig");
    url = url.replace(re, "$1");
    url = url.replace("?&", "?");
    url = url.replace("&&", "&");
    if (url.indexOf("?") > 0)
        url += "&" + key + "=" + value;
    else
        url += "?" + key + "=" + value;
    return url;
}
function GetParamFormUrl(url, key) {
    var index = url.toLowerCase().indexOf(key.toLowerCase());
    url = url.substr(index + key.length + 1);
    index = url.indexOf("&")
    if (index > -1) {
        url = url.substr(0, index);
    }
    return url;
}
function GotoLogin() {
    var a = parent;
    while (a.location.href != a.parent.location.href) {
        a = a.parent;
    }
    if (a.location.href.toLowerCase().indexOf("devspec") > 0) {
        a.location.href = 'DSLogin.aspx';
    }
    else {
        a.location.href = 'Login.aspx';
    }
}

function GetFrameByName(name) {
    for (var i = 0; i < window.frames.length; i += 1) {
        try {
            if (window.frames[i].name == name) { return window.frames[i]; }
        } catch (e) { }
    }
    return null;
}


function GlobalRegisterOnload(fun) {
    _attachEvent(window, "load", fun);
}
function GlobalRegisterOnresize(fun) {
    _attachEvent(window, "resize", fun);
}
function StringBuilder(txt) {
    this._parts = (typeof (txt) !== 'undefined' && initialText !== null && txt !== '') ? [txt.toString()] : [];
    this._value = {}; this._len = 0;
    this.append = function (text) { this._parts[this._parts.length] = text; }
    this.appendFormat = function (format, args) { this._parts[this._parts.length] = String._toFormattedString(false, arguments); }
    this.appendLine = function (text) { this._parts[this._parts.length] = ((typeof (text) === 'undefined') || (text === null) || (text === '')) ? '\r\n' : text + '\r\n'; }
    this.clear = function () { this._parts = []; this._value = {}; this._len = 0; }
    this.isEmpty = function () { if (this._parts.length === 0) return true; return this.toString() === ''; }
    this.toString = function (p) {
        p = p || ''; var parts = this._parts;
        if (this._len !== parts.length) { this._value = {}; this._len = parts.length; } var val = this._value;
        if (typeof (val[p]) === 'undefined') {
            if (p !== '') {
                for (var i = 0; i < parts.length; ) {
                    if ((typeof (parts[i]) === 'undefined') || (parts[i] === '') || (parts[i] === null)) parts.splice(i, 1);
                    else i++;
                }
            } val[p] = this._parts.join(p);
        } return val[p];
    }
}
String.prototype.endsWith = function String$endsWith(suffix) { return (this.substr(this.length - suffix.length) === suffix); }
String.prototype.startsWith = function String$startsWith(prefix) { return (this.substr(0, prefix.length) === prefix); }
String.prototype.trim = function String$trim() { return this.replace(/^\s+|\s+$/g, ''); }
String.prototype.trimEnd = function String$trimEnd() { return this.replace(/\s+$/, ''); }
String.prototype.trimStart = function String$trimStart() { return this.replace(/^\s+/, ''); }
String.format = function String$format(format, args) { return String._toFormattedString(false, arguments); }
String.localeFormat = function String$localeFormat(format, args) { return String._toFormattedString(true, arguments); }
String._toFormattedString = function String$_toFormattedString(useLocale, args) {
    var result = ''; var format = args[0];
    for (var i = 0; ; ) {
        var open = format.indexOf('{', i); var close = format.indexOf('}', i); if ((open < 0) && (close < 0)) { result += format.slice(i); break; }
        if ((close > 0) && ((close < open) || (open < 0))) { result += format.slice(i, close + 1); i = close + 2; continue; }
        result += format.slice(i, open); i = open + 1; if (format.charAt(i) === '{') { result += '{'; i++; continue; }
        var brace = format.substring(i, close); var colonIndex = brace.indexOf(':'); var argNumber = parseInt((colonIndex < 0) ? brace : brace.substring(0, colonIndex)) + 1;
        var argFormat = (colonIndex < 0) ? '' : brace.substring(colonIndex + 1); var arg = args[argNumber];
        if (typeof (arg) === "undefined" || arg === null) { arg = ''; }
        if (arg.toFormattedString) { result += arg.toFormattedString(argFormat); }
        else if (useLocale && arg.localeFormat) { result += arg.localeFormat(argFormat); }
        else if (arg.format) { result += arg.format(argFormat); }
        else result += arg.toString();
        i = close + 1;
    } return result;
}

function callWikiOnlineHelp(helpurl, bookname, identify) {
    if (bookname == "") {
        alert("Please call web administrator to setup a wiki online help book for current view.");
        return;
    }
    var url = helpurl + "?WikiBook=" + bookname + "&topic=" + identify;
    window.open(url, '', 'height=600, width=900, resizable=yes, location=yes, scrollbars=yes');
}

function getPosition(obj) { if (null == obj) return new Array(); var ary = new Array(); ary[0] = obj.offsetLeft; ary[1] = obj.offsetTop; while (obj = obj.offsetParent) { ary[0] += obj.offsetLeft; ary[1] += obj.offsetTop; } return ary; }

function GetOpener() {
    var objOpener = null;

    if (window.name != "" && top.document.getElementById(window.name)) {
        if (typeof (top.getWindowOpener) != 'undefined')
            objOpener = top.getWindowOpener(window.name);
    }
    else if (window.opener == null)
        objOpener = window.dialogArguments;
    else
        objOpener = opener;

    return objOpener;
}
function CloseWindow() {
    if (window.name != "" && top.document.getElementById(window.name)) {
        if (typeof (top.closeWindow) != 'undefined') {
            top.closeWindow(window.name);
            return true;
        }
    }
    else {
        window.close();
    }
    return false;
}
function Window_ResizeTo(width, height) {
    if (window.name != "" && top.document.getElementById(window.name)) {
        if (typeof (top.windowResizeTo) != 'undefined')
            top.windowResizeTo(window.name, width, height);
    }
    else {
        window.resizeTo(width, height);
    }
}

function Window_ResizeBy(width, height) {
    if (window.name != "" && top.document.getElementById(window.name)) {
        if (typeof (top.windowResizeBy) != 'undefined')
            top.windowResizeBy(window.name, width, height);
    }
    else {
        window.resizeBy(width, height);
    }
}

function Window_MoveTo(x, y) {
    if (window.name != "" && top.document.getElementById(window.name)) {
        if (typeof (top.windowMoveTo) != 'undefined')
            top.windowMoveTo(window.name, x, y);
    }
    else {
        window.moveTo(x, y);
    }
}
function AttachDragDopEvent(doc) {
    if (top.KWDragDrop)
        top.KWDragDrop.AttachEvent(doc);
}
function EachPageActive() {
    if (typeof (top.resetTimerCount) == "function") {
        top.resetTimerCount();
    }
}
function CheckFrameLogOff(w, depth) {
    AddEventToFrame(w, depth);
    for (var i = 0, j = w.frames.length; i < j; i++) {
        AddEventToFrame(w.frames[i]);
        if (depth < 5) {
            CheckFrameLogOff(w.frames[i], ++depth);
        }
    }
}
function AddEventToFrame(w) {
    try {
        if (typeof (w) == "undefined" || typeof (w.document.body) == "undefined" || !w.document.body) {
            return;
        }
        _attachEvent(w.document.body, "click", EachPageActive);
        _attachEvent(w.document.body, "keypress", EachPageActive);
    }
    catch (ex) { }
}

// 0: primary/left mouse button
// 1: middle mouse button
// 2: secondary/right mouse button
function getMouseButton(e) {
    if (document.implementation.hasFeature('MouseEvents', '2.0')) {
        return e.button;
    } else {
        switch (e.button) {
            case 0:
            case 1:
            case 3:
            case 5:
            case 7: return 0;
            case 2:
            case 6: return 2;
            case 4: return 1;
        }
    }
}

function encodeURIComponentEx(str) {
    var s = encodeURIComponent(str);
    s = s.replace("\'", "%27");
    s = s.replace("+", "%2B");
    return s;
}

function checkStartDueDate(startId, dueId) {
    if ($('#' + startId).length < 1 || $('#' + dueId).length < 1) return;
    $('#' + startId + ', #' + dueId).bind($.browser.msie ? 'propertychange' : 'change', function (e) {
        var startDateString = $('#' + startId).val(),
            endDateString = $('#' + dueId).val();
        var startDate = StringToDate(startDateString, startDateFormat),
            endDate = StringToDate(endDateString, finishDateFormat);
        if (startDateString != '' && endDateString != '' && (endDate - startDate < 0)) {
            $(this).val('');
            alert(LBacklog.PlannedTimeError);
        }
    });
}
