﻿function DocumentItemSelect(linkValue) {
    document.forms[0].action = linkValue;
    __doPostBack('__Page', linkValue);
}

function SaveMenuState() //tato funkce se vola primo z adapteru, kam byla dopsana do funkce ExpandCollapse__AspNetTreeView(sourceElement)
{
    var state = '';
    state = GetViewState__AspNetTreeView('ctl00_ContentPlaceHolder1_ctl00_tvLeftMenu_UL');  //funkce z adapteru, ktera sestavuje stav menu (id samotneho menu)
    SetCookie("TreeViewState", state, 3600);
}

function Redirect(path, script) {
    SetCookie("JavaScript", script, 5);
    window.location.href = path;
}

function SetJavaScript() {
    $(document).ready(function() {
        SetCookie("JavaScript", '1', 5);
    });
}

function RestoreMenu(restore, clear) {
    $(document).ready(function() {
        RestoreMenuState('ctl00_ContentPlaceHolder1_ctl00_tvLeftMenu_UL', restore, clear);
    });
}

function RestoreMenuState(menu, restore, clear) {
    var menuNode = document.getElementById(menu);
    var state = "";
    if (restore) {
        var cookies = document.cookie.split(";");
        for (var cookie in cookies) {
            if (cookies[cookie].indexOf("TreeViewState") > -1) {
                state = cookies[cookie].split("=")[1];
            }
        }
    }
    RestoreTreeViewState__AspNetTreeView(menuNode, state)
    //var menuChilds = $("ul.AspNet-TreeView-Show", menuNode);
    /*
    for (var item in menuChilds) {
        menuChilds[item].className = "AspNet-TreeView-Hide";
    }
    */
    if (clear) {
        SetCookie("TreeViewState", "", -1);
    }
}

// Save TreeView state to Cookie
function SetCookie(cookieName, cookieValue, nSeconds) {
    var today = new Date();
    var expire = new Date();
    if (nSeconds == null || nSeconds == 0) nSeconds = 1;
    expire.setTime(today.getTime() + nSeconds * 1000);      //  3600000 * 24 * nDays);
    document.cookie = cookieName + "=" + escape(cookieValue)
                 + ";expires=" + expire.toGMTString();
}

/*
var menuFirstLevelWithSubs = $("li.AspNet-TreeView-Root").not("li.AspNet-TreeView-Leaf").get();
state += SaveStateOnLevel(menuFirstLevelWithSubs);

function SaveStateOnLevel(menu)
{
    var state = '';
    for (var menuItem in menu) {
        var menuChilds = $("ul.AspNet-TreeView-Show", menu[menuItem]);
        if (menuChilds.length > 0) {
            state += 'e';
            var childMenu = $("li.AspNet-TreeView-Parent", menu[menuItem]).get();
            if (childMenu.length > 0)
                state += SaveStateOnLevel(childMenu);
        }
        else
            state += 'n';
    }
    return state;
}
*/

function RestoreTreeViewState__AspNetTreeView2(element, state) {
    var child = element.firstChild;
    var bConsiderChildren = true;

    //  The following line must be changed if you alter the TreeView adapters generation of
    //  markup such that the first child within the LI no longer is the expand/collapse <span>.
    if ((element.tagName == "LI") && (child != null)) {
        var expandCollapseSPAN = null;
        var expandedUL = null;
        var currentChild = child;
        while (currentChild != null) {
            if ((currentChild.tagName == "SPAN") &&
                (currentChild.className != null) &&
                ((currentChild.className.indexOf(collapseClass) > -1) ||
                 (currentChild.className.indexOf(expandClass) > -1))) {
                expandCollapseSPAN = currentChild;
            }
            if ((currentChild.tagName == "UL") &&
                (currentChild.className != null))
            {
                expandedUL = currentChild;
                break;
            }
            currentChild = currentChild.nextSibling;
        }

        if (expandedUL != null) {
            if (state.substring(0, 1) != 'e') {
                expandCollapseSPAN.className = expandClass;
                expandedUL.className = "AspNet-TreeView-Hide";
                bConsiderChildren = false;
            }
            else {
                expandCollapseSPAN.className = collapseClass;
                expandedUL.className = "AspNet-TreeView-Show";
            }
            state = state.substring(1);
            expandCollapseSPAN = null;
        }
    }

    if ((bConsiderChildren || state.length == 0) && (child != null)) {
        state = RestoreTreeViewState__AspNetTreeView2(child, state);
    }

    if (element.nextSibling != null) {
        state = RestoreTreeViewState__AspNetTreeView2(element.nextSibling, state);
    }
    return state;
}

function ValidateActionData() {
    //Page_IsValid = false;
    for (var val in Page_Validators) {
        ValidatorValidate(Page_Validators[val]);
    }
    if (Page_IsValid) {
        theForm.submit();    
    }
}

function SetSearchType(ctrlName) {
    var hdnSearchFocus = document.getElementById(hdnSearchFocusId);
    if (ctrlName.length == 0) {
        hdnSearchFocus.value = "";
    }
    else {
        hdnSearchFocus.value = ctrlName;
    }
}
