﻿var prevOnloadFuncRef;
var onloadFuncRef;
var isViewMode = true;

function _mt_is_wlcontext() {
    return false; //mythings site is never
}

function _mt_convertLinks() {
    //do nothing
}
 
function addEvent(object, eventname, func) {
    if (window.addEventListener) { // Mozilla, Netscape, Firefox
        object.addEventListener(eventname, func, false);
    }
    else { // IE
        object.attachEvent('on' + eventname, func);
    }
}

function removeEvent(object, eventname, func) {
    if (window.removeEventListener) { // Mozilla, Netscape, Firefox
        object.removeEventListener(eventname, func, false);
    }
    else { // IE
        object.detachEvent('on' + eventname, func);
    }
}



function SetInnerText(el, value) {
    if (isIE()) {
        el.innerText = value;
    }
    else {
        el.textContent = value;
    }
}

function setIsClicked(hdnEl, result) {
    var hRes = true;
    //alert(hdnEl.value);
    if (hdnEl != null) {
        if (hdnEl.value == 0) {
            hdnEl.value = 1;
            return true;
        }
        else
            return false;
    }
    return true;
}

function setFocus(txtElem) {
    var el = document.getElementById(txtElem);
    try {
        if (el != null)
            el.focus();
    } catch (e) { }
}

function InitEditableControl(txtElem) {
    window.setTimeout("setFocus('" + txtElem + "');", 100);
}
function KeyPressEditableBehaviour(acceptFunc, cancelFunc, e) {
    var keyCode;
    var isAcceptEvnt = true;
    if (isIE()) {
        e = window.event;
        keyCode = e.keyCode;
        if (e.srcElement.rows != undefined)
            isAcceptEvnt = !e.srcElement.rows > 1;
    }
    else {
        if (e.target.rows != undefined)
            isAcceptEvnt = !e.target.rows > 1;
        keyCode = e.keyCode;
    }
    //debugger
    if (keyCode == 27)
        cancelFunc();
    if (keyCode == 13) {
        if (isAcceptEvnt) {
            acceptFunc();
            e.cancelBubble = true;
        }
    }
    return false;
}
function setEnableEditableDetails(control, result, attributeId, raiseFunc, hdnEditableAttributeId) {
    if (isViewMode) {
        if (result) {
            control.setAttribute("prevClass", control.className);
            control.align = "left";
            control.className = "editable-td-frame";
            if (typeof (strEditTitle) != 'undefined')
                control.title = strEditTitle;
            control.onclick = raiseFunc;
            document.getElementById(hdnEditableAttributeId).value = attributeId;
        }
        else {
            control.title = '';
            control.onclick = null;
            control.className = control.getAttribute("prevClass");
        }
    }
}
function setEnableEditable(control, result, raiseFunc) {
    if (isViewMode) {
        if (result) {
            prevClass = control.className;
            control.className += ' editable-label-frame';
            if (strEditTitle != "undefined")
                control.title = strEditTitle;
            control.onclick = raiseFunc;
        }
        else {
            control.onclick = null;
            control.className = "";
            if (prevClass != "")
                control.className = prevClass;
            control.title = '';
        }
    }
}

function enableCheckbox(chkbox, result) {
    if (isIE()) {
        if (chkbox.parentElement.disabled)
            chkbox.parentElement.disabled = !result;
    }
    else {
        if (chkbox.parentNode.disabled)
            chkbox.parentNode.disabled = !result;
    }
    chkbox.disabled = !result;
}

function $(obj) {
    return document.getElementById(obj);
}

//safely attaches window onload event
function attachOnloadEvent(funcRef) {
    if (window.onload) {
        prevOnloadFuncRef = window.onload
    }
    onloadFuncRef = funcRef;
    window.onload = __doOnLoad;
}

function __doOnLoad() {
    //if (prevOnloadFuncRef)prevOnloadFuncRef();
    if (onloadFuncRef) onloadFuncRef();
}

function applyCSS(e, css) {
    e.className = css;
}

function ShowHideItemDetails(container, show, cookieName, itemID) {
    //alert(container.id + ':' + show + ':' + itemID);
    if (typeof (container) == "undefined")
        return;
    if (show)
        container.className = "item-show-details-container"
    else
        container.className = "item-hide-details-container"
    if (cookieName != "") {
        SetCookie(cookieName + itemID, show ? "1" : "0");
    }

    nuconomyTrack(show ? 1005 : 1006, 285, __CONTENTID);
}

function CollapseExpand(btnGroup, groupId, attributeId, imgCollapse) {
    var img_open = "cat_bullet_open";
    var img_close = "cat_bullet_closed";
    var isCollapse = true;
    tdGroup = btnGroup.parentNode;
    tblDetails = tdGroup.parentNode.parentNode;

    isCollapse = (tdGroup.getAttribute("expanded") == "True")

    var trColl = tblDetails.getElementsByTagName("tr");
    for (var i = 0; i < trColl.length; i++) {
        var trRow = trColl[i];
        var tdgroupId = null;
        var tdChilds = trRow.getElementsByTagName("td");
        if (tdChilds.length > 0) {
            tdgroupId = tdChilds[0].getAttribute("groupId");
        }
        if (tdgroupId == null || tdgroupId == 'undefined') {
            if (tdChilds.length > 1) {
                tdgroupId = tdChilds[1].getAttribute("groupId");
            }
        }

        if (tdgroupId == groupId) {
            if (isCollapse) {
                trRow.style.display = "none";
                btnGroup.innerHTML = btnGroup.getAttribute("showText");
                if (imgCollapse != "undefined") {
                    imgCollapse.src = imgCollapse.src.replace(img_open, img_close);
                }
            }
            else {
                trRow.style.display = "";
                btnGroup.innerHTML = btnGroup.getAttribute("hideText");
                if (imgCollapse != "undefined") {
                    imgCollapse.src = imgCollapse.src.replace(img_close, img_open);
                }
            }
        }
    }
    tdGroup.setAttribute("expanded", ((isCollapse) ? "False" : "True"));
}

function getWindowWidth() {
    var myWidth = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
    }
    return myWidth;
}

function getWindowHeight() {
    var myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myHeight = document.body.clientHeight;
    }
    return myHeight;
}



function findLeftOffset(elem) {
    var left = 0;
    while (elem.tagName != "BODY") {
        left += elem.offsetLeft;
        elem = elem.offsetParent;
    }
    return left;
}

function findTopOffset(elem) {
    var top = 0;
    while (elem.tagName != "BODY") {
        top += elem.offsetTop;
        elem = elem.offsetParent;
    }
    return top;
}


function grayScreen() {
    var wf = $(__WaitFrameID);
    scWidth = (document.documentElement.scrollWidth || document.body.scrollWidth);
    scHeight = (document.documentElement.scrollHeight || document.body.scrollHeight);
    if (wf != null) {
        wf.style.width = scWidth + "px";
        wf.style.height = (scHeight - 5) + "px";
    }
    var el = $("ScreenGrayer");
    el.style.width = scWidth + "px";
    el.style.height = (scHeight - 5) + "px";
    el.style.display = "";
}

function ungrayScreen() {
    $("ScreenGrayer").style.display = "none";
}

function isIE() {
    if (document.all)
        return true;
    return false;
}

function isIE6() {
    return isIE() && navigator.appVersion.indexOf("MSIE 6.0") > 0;
}

function SaveScrollPosition(controlId) {
    var control = document.getElementById(controlId);
    var strCook = document.cookie;
    var controlIndex = strCook.indexOf(controlId);

    if (control) {
        var intY = control.scrollTop;
        if (controlIndex != -1) {
            var intS = controlIndex;
            var intE = strCook.indexOf("~!", controlIndex);
            var strControl = strCook.substr(intS, intE - intS + 2);
            document.cookie = strCook.replace(strControl, '');
        }
        document.cookie = controlId + "=!~" + intY + "~!";
    }
}

function RestoreScrollPosition(controlId) {
    var control = document.getElementById(controlId);
    var strCook = document.cookie;
    var controlIndex = strCook.indexOf(controlId);
    if (controlIndex != -1 && control) {
        var intS = strCook.indexOf("!~", controlIndex);
        var intE = strCook.indexOf("~!", controlIndex);
        var strPos = strCook.substring(intS + 2, intE);
        if (!isNaN(strPos)) {
            document.getElementById(controlId).scrollTop = strPos;
        }
    }

    //    if(strCook.indexOf("!~") != -1 && control)
    //    {
    //        var intS = strCook.indexOf("!~");
    //        var intE = strCook.indexOf("~!");
    //        var strPos = strCook.substring(intS+2,intE);
    //        // alert("New position = " + strPos);
    //        document.getElementById(controlId).scrollTop = strPos;
    //    }
}

function SaveScrollPositionToHidden(divId, hiddenId) {
    var hidden = document.getElementById(hiddenId);
    var div = document.getElementById(divId);
    if (div && hidden) {
        var intY = div.scrollTop;
        hidden.value = intY;
    }
}

function RestoreScrollPositionFromHidden(divId, hiddenId, ratio) {
    var hidden = document.getElementById(hiddenId);
    var div = document.getElementById(divId);
    if (div && hidden) {
        if (ratio > 0) {
            hidden.value = (div.scrollHeight * ratio) + (div.clientHeight / 2);
        }

        if (hidden.value == '') {
            hidden.value = 0;
        }

        var intY = hidden.value;
        div.scrollTop = intY;
    }
}

function GoBack() {
    history.go(-1);
    return false;
}

function IsEnter(uniqueId) {
    if (event.which || event.keyCode) {
        if ((event.which == 13) || (event.keyCode == 13)) {
            var button = document.getElementById(uniqueId);
            if (button) {
                button.click();
            }
            return false;
        }
    }
    else {
        return true;
    }
}


//disable tabindex for elements recursively
function UntabifyByTagName(el, tagToUntabify) {
    var i;
    tags = el.getElementsByTagName(tagToUntabify);
    for (i = 0; i < tags.length; i++) {
        tags[i].tabIndex = -1;
    }
}

//disable tabindex for elements recursively
function Untabify(el) {
    var i;
    for (i = 0; i < el.childNodes.length; i++) {
        if (typeof (el.childNodes[i].tagName) != "undefined") {
            el.childNodes[i].tabIndex = -1;
            Untabify(el.childNodes[i]);
        }
    }
}

function HideMessage() {
    __lblNegativeMessage.style.display = "none";
    __lblPositiveMessage.style.display = "none";
}

function ShowNotification(str) {
    ShowPositiveMessage(str)
}

function ShowPositiveMessage(str) {
    __lblPositiveMessage.innerHTML = str;
    __lblNegativeMessage.style.display = "none";
    __lblPositiveMessage.style.display = "";
}

function ShowError(str) {
    ShowNegativeMessage(str)
}

function ShowNegativeMessage(str) {
    __lblNegativeMessage.innerHTML = str;
    __lblPositiveMessage.style.display = "none";
    __lblNegativeMessage.style.display = "";
}

//Limit text size for multiline text areas
function LimitTextLength(textbox) {
    if (textbox != null && textbox.getAttribute("maxlength") != "" && typeof (textbox.getAttribute("maxlength")) != "undefined") {
        if (textbox.value.length > parseInt(textbox.getAttribute("maxlength"))) {
            var txt = textbox.value;
            txt = txt.slice(0, parseInt(textbox.getAttribute("maxlength")));
            textbox.value = txt;
            return false;
        }
    }
    return true;
}

function GetIndexOfElement(element) {
    for (var i = 0; i < document.forms[0].elements.length; i++) {
        if (element == document.forms[0].elements[i]) {
            return i;
        }
    }
    return -1;
}

function SetFocusByIndex(index) {
    try {
        document.forms[0][(index + 1) % document.forms[0].length].focus();
    }
    catch (e) {
        SetFocusByIndex(index + 1);
    }
}

function ApplySmartTab(element) {
    try {
        var index = GetIndexOfElement(element);
        if (!isNaN(index)) {
            SetFocusByIndex(index);
        }
    }
    catch (e) { }
}

function ResetVisibility(control, visible) {
    try {
        if (visible) {
            control.style.display = 'block';
        }
        else {
            control.style.display = 'none';
        }
    }
    catch (e) {

    }
};

/*Input text validation*/
// **************************************************************************************
// KeyPressValidation - enable typing only chars of the given format.
// Formats: POSNUMERIC, NUMERIC, POSINTEGER, INTEGER, STRING
// **************************************************************************************
function KeyPressValidation(Mask, DecimalSeperator, e) {

    // =========================================================	
    // Choose Mask of allowed/notAllowes chars
    // =========================================================	
    var AllowChars = '';
    var BlockChars = '';

    switch (Mask) {
        case "POSNUMERIC":
            AllowChars = '0123456789' + DecimalSeperator;
            break;
        case "POSINTEGER":
            AllowChars = '0123456789';
            break;

    }

    // =========================================================	
    // Capture the char that the user typed
    // =========================================================	
    //var e = window.event; 
    var ch;

    if (isIE()) {
        e = window.event;
        ch = String.fromCharCode(e.keyCode);
    }
    else {
        ch = String.fromCharCode(e.which);
        //debugger
        if (e.which >= 0 && e.which <= 8)
            ch = '0';
    }
    var ret = true;
    // =========================================================	
    // Check that user's char is in list of chars
    // =========================================================	
    if (AllowChars.length > 0)
        if (AllowChars.indexOf(ch) == -1) {
        ret = false;
    }

    if (BlockChars.length > 0)
        if (BlockChars.indexOf(ch) != -1) {
        ret = false;
    }

    if (isIE()) {
        event.returnValue = ret;
    }
    else {
        if (!ret) { e.preventDefault(); }
    }
}

/**********************************************************************
function FnChkDragStr( RegExpMaskInstr, fAllowQte, excludeChar1)
to be used captureing the 'onDrop' event 
- e.g.: onDrop="FnChkPstStr('[^-. \\d]', false);" will allow only -0.123456789 chars
	
Params-
'RegExpMaskInstr' 	- a string that contains regular expression of all permitted chars. 
don't enter qoute (") sign in this expression.
'fAllowQte'		- true/false - qoutes(") enabled.
'excludeChar1'		- invalid char
	
Output- 
If the dragged data string contains only chars from the mask- String.  
else - nothing. 						  

**********************************************************************/

function FnChkDragStr(RegExpMaskInstr, fAllowQte, excludeChar1, dash) {
    if (dash)
        if (CheckPaste(RegExpMaskInstr, event.dataTransfer.getData("TEXT"), fAllowQte, excludeChar1))
        eval(dash)
    event.returnValue = CheckPaste(RegExpMaskInstr, event.dataTransfer.getData("TEXT"), fAllowQte, excludeChar1);
}

/**********************************************************************
function FnChkPstStr( RegExpMaskInstr, fAllowQte, excludeChar1)
check the clipboard data to fit the 'RegExpMaskInstr' mask.
to be used captureing the 'onPaste' event 
- e.g.: onPaste="FnChkPstStr('[^-. \\d]', false);" will allow only -0.123456789 chars
	
Params-
'RegExpMaskInstr' 	- a string that contains regular expression of all permitted chars. 
don't enter qoute (") sign in this expression.
'fAllowQte'		- true/false - qoutes(") enabled.
'excludeChar1'		- invalid char
	
Output- 
If the dragged data string contains only chars from the mask- String.  
else - nothing. 						  

**********************************************************************/

function FnChkPstStr(RegExpMaskInstr, fAllowQte, excludeChar1, dash) {
    if (dash) {
        if (CheckPaste(RegExpMaskInstr, clipboardData.getData("TEXT"), fAllowQte, excludeChar1))
            eval(dash)
    }
    event.returnValue = CheckPaste(RegExpMaskInstr, clipboardData.getData("TEXT"), fAllowQte, excludeChar1);
}

function CheckPaste(RegExpMaskInstr, userInput, fAllowQte, excludeChar1) {
    var validInput = userInput;
    var re = new RegExp(RegExpMaskInstr);

    userInput = ReplaceAll(userInput, '"', '')


    if (fAllowQte)
        validInput = ReplaceAll(userInput, '"', '')

    if (excludeChar1)
        validInput = ReplaceAll(validInput, excludeChar1, '')

    return (validInput == ReplaceAll(userInput, re, ""));
}

/*****************************************************************
* replace all substring in input text (support regular expression)
*****************************************************************/
function ReplaceAll(theString, searchChar, newChar) {
    return theString.split(searchChar).join(newChar);
}


/* Client Date Time Offset */
function GetUserDateTimeOffset() {
    var currentDate = new Date();
    // obtain local UTC offset
    var localOffset = currentDate.getTimezoneOffset();
    if (!isNaN(localOffset)) {
        localOffset = localOffset * -1;
    }
    return localOffset;
}

function LoadApplet(containerID, appletTag) {
    document.getElementById(containerID).innerHTML = appletTag;
}

function ChangeVisibility(controlId) {
    var control = document.getElementById(controlId);
    if (control) {
        if (control.style.display == 'none') {
            control.style.display = '';
        }
        else {
            control.style.display = 'none';
        }
    }
}

function ToggleVisibility(controlId) {
    var control = document.getElementById(controlId);
    if (control) {
        control = document.getElementById(controlId);
        if (control) {
            control = new Sys.UI.Control(control);
            control.set_visible(!control.get_visible());
        }
    }
}

function BeginRequestHandler(sender, args) {
    var scrollTop = document.documentElement.scrollTop;
    if (scrollTop > 95 && $("ProgressIndicator") != null) {
        $("ProgressIndicator").style.top = document.documentElement.scrollTop + "px";
        $("ProgressIndicator").className = "progress-area-float";
    }
    else {
        $("ProgressIndicator").style.top = "";
        $("ProgressIndicator").className = "progress-area";
    }
}


function EndRequestHandler(sender, args) {
    if (typeof (__MessageContainer) != "undefined") {
        var scrollTop = document.documentElement.scrollTop;
        if (scrollTop > 95) {
            __MessageContainer.style.top = document.documentElement.scrollTop + "px";
            __MessageContainer.className = "message-float";
        }
        else {
            __MessageContainer.style.top = "";
            __MessageContainer.className = "";
        }
    }


    if (typeof (__goTopOfPage) != "undefined" && __goTopOfPage && isIE6()) {
        document.documentElement.scrollTop = 0;
        __goTopOfPage = false;
    }

    if (args.get_error() != null) {
        var errorName = args.get_error().name;
        if (errorName.length > 0) {
            alert(args.get_error().message);
            args.set_errorHandled(true);
        }
    }

    __IsFullyLoaded = true;
}

function processScroll() {
    if (typeof (__MessageContainer) != "undefined" && __MessageContainer.className != "") {
        __MessageContainer.style.top = "";
        __MessageContainer.className = "";
    }
}

function disableEnter(e) {
    var eventCode = e ? e.which : window.event.keyCode;
    var targetControl = null;
    if (isIE()) {
        e = window.event;
        targetControl = e.srcElement;
    }
    else
        targetControl = e.target;
    if (targetControl.tagName == 'TEXTAREA')
        return true;
    return eventCode != 13;
}

function HandleHideShow(link, handled, cookieName, evt, analyticsTag) {
    handledControl = document.getElementById(handled);
    if (handledControl) 
    {
        if (handledControl.style.display == 'none')
        {
            handledControl.style.display = '';
            if (typeof(analyticsTag)!="undefined" && analyticsTag!="")
            {
               gaTrack(analyticsTag);
            }
        }
        else
            handledControl.style.display = 'none';
    }
    if (m_ImageMode) 
    {
        var linkImageControl = document.getElementById(link + "_Image");
        if (linkImageControl) {
            if (handledControl.style.display == 'none')
                linkImageControl.src = m_ImgShow;
            else
                linkImageControl.src = m_ImgHide;
        }
    }
    else 
    {
        var linkControl = document.getElementById(link);
        if (linkControl) {
            if (linkControl.innerHTML == m_TextHide)
                linkControl.innerHTML = m_TextShow;
            else
                linkControl.innerHTML = m_TextHide;
        }
    }
    if (cookieName != "") {
        SetCookie(cookieName, handledControl.style.display == "none" ? "none" : "block");
    }
    var e = (evt) ? evt : window.event;
    if (window.event) {
        e.cancelBubble = true;
    } else {
        e.stopPropagation();
    }
}


function logTBClick() {
    var xmlhttp = null;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XmlHttp");
    }
    if (xmlhttp != null) {
        url = "services/ActivityLog.asmx/LogToolBarDownloadClick?source=Homepage";
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    }
}

function SetCookie(sName, sValue) {
    date = new Date();
    document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 2099 23:59:59 GMT;";
}


function setFrameHeight(elID, height) {
    $(elID).height = height + 10;
    $(elID).style.display = "";
}


//Navigation submenu
var activeMenuID = "";

function showSubmenu(el, menuID) {
    if (activeMenuID != "") {
        $(activeMenuID).style.display = "none";
    }
    activeMenuID = menuID;
    $(menuID).style.display = "block";
    $(menuID).style.top = (findTopOffset(el) + el.clientHeight) + "px";
    $(menuID).style.left = (findLeftOffset(el)) + "px";
    $(menuID).style.width = (el.clientWidth - 4) + "px";
    addEvent(document, "click", hideSubMenu);
}

function stopEvent(evt) 
{
    if (window.event) 
    {
        window.event.cancelBubble = true;
    }
    else
    {
        evt.stopPropagation()
    }
}

function hideSubMenu() {
    if (activeMenuID != "") 
    {
        $(activeMenuID).style.display = "none"; activeMenuID = ""
        removeEvent(document, "click", hideSubMenu);
    };
}


//Suggester

var isAdditionalRequest = false;
var isNavigation = false;
var isSuggesterDisabled = false;


function OnClientItemsRequesting(sender) {
    //return false;

    if (isNavigation || isSuggesterDisabled) {
        return false;
    }
    else {
        document.getElementById(sender.InputID).className += " suggester-loading";
        if (sender.Items.length == 0) {
            sender.HideDropDown();
        }
    }
}

function OnClientItemsRequested(sender, eventArgs) {
    document.getElementById(sender.InputID).className = "rcbInput";
    if (!isAdditionalRequest) {
        isAdditionalRequest = true;
        //sender.RequestItems(sender.GetText(),true);
    }
    else {
        isAdditionalRequest = false;
    }

    if (sender.Items.length == 0) {
        sender.HideDropDown();
    }
    else {
        sender.ShowDropDown();
    }
}

function OnClientDropDownOpening(sender) {
    //return false;

    if (isSuggesterDisabled) {//sender.GetText().length < 1 || 
        return false;
    }
}

function AfterClientCallBackError(combo) {
    return false;   //to hide the error message
}


function DisableSuggester(combo) {
    isSuggesterDisabled = true;
    if (typeof (combo) != "undefined") {
        document.getElementById(combo.InputID).className = "rcbInput";
        combo.HideDropDown();
    }
} 
