﻿function CreateClearDiv() {
    var div = document.createElement('div');
    div.className = "clear";

    return div;
}

function CloneArray(oldArr) {
    var newArr = new Array();
    for (var i = 0; i < oldArr.length; i++) {
        newArr[newArr.length] = oldArr[i];
    }

    return newArr;
}

function ContainsTag(el, tagName) {
    try {
        if (el.tagName.toLowerCase() == tagName.toLowerCase()) {
            return true;
        }
        else {
            for (var x = 0; x < el.childNodes.length; x++) {
                if (ContainsTag(el.childNodes[x], tagName)) {
                    return true;
                }
            }
        }
    }
    catch (e) {
        return false;
    }

    return false;
}

function DisableEnableAllElementsIn(el) {
    try {
        el.disabled = el.disabled ? false : true;
    }
    catch (E) { }

    if (el.childNodes && el.childNodes.length > 0) {
        for (var x = 0; x < el.childNodes.length; x++) {
            DisableEnableAllElementsIn(el.childNodes[x]);
        }
    }
}

function DisableAllElementsIn(el) {
    try {
        el.disabled = true;
    }
    catch (E) { }

    if (el.childNodes && el.childNodes.length > 0) {
        for (var x = 0; x < el.childNodes.length; x++) {
            DisableAllElementsIn(el.childNodes[x]);
        }
    }
}

function DisableEnableAllSelectsByServiceType(el, type) {
    try {
        if (el.tagName.toLowerCase() == "select") {
            if (el.attributes["servicetype"].value == type) {
                el.disabled = el.disabled ? false : true;
            }
        }
    }
    catch (E) { }

    if (el.childNodes && el.childNodes.length > 0) {
        for (var x = 0; x < el.childNodes.length; x++) {
            DisableEnableAllSelectsByServiceType(el.childNodes[x], type);
        }
    }
}

function ShowHideAllElementsInByTagName(el, tagName) {
    try {
        if (el.tagName != undefined && el.tagName.toLowerCase() == tagName) {
            el.style.visibility = el.style.visibility == 'hidden' ? '' : 'hidden';
        }
    }
    catch (E) { }

    if (el.childNodes && el.childNodes.length > 0) {
        for (var x = 0; x < el.childNodes.length; x++) {
            ShowHideAllElementsInByTagName(el.childNodes[x], tagName);
        }
    }
}

function GetElementAbsoluteTop(oElement) {
    var iReturnValue = 0;
    while (oElement != null) {
        iReturnValue += oElement.offsetTop;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

function GetElementAbsoluteLeft(oElement) {
    var iReturnValue = 0;
    while (oElement != null) {
        iReturnValue += oElement.offsetLeft;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

function ScrollDivContent(div, element) {
    var topPos = GetElementTop(element);
    div.scrollTop = 0;
    div.scrollTop = parseInt(topPos);
}

function Trim(value) {
    return value.replace(/^\s*/, "").replace(/\s*$/, "");
}

function CharIsNumber(sign) {
    var validChars = "0123456789";
    if (validChars.indexOf(sign) != -1) {
        return true;
    }

    return false;
}

function GetElementTop(elem) {
    var yPos = elem.offsetTop;

    return yPos;
}

function GetElementLeft(elem) {
    var xPos = elem.offsetLeft;

    return xPos;
}

function getScreenCenterY() {
    var y = 0;
    y = getScrollOffset() + (getInnerHeight() / 2);

    return y;
}

function getScreenCenterX() {
    return document.body.clientWidth / 2;
}

function getInnerHeight() {
    var y;
    if (self.innerHeight) // all except Explorer
    {
        y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    // Explorer 6 Strict Mode
    {
        y = document.documentElement.clientHeight;
    }
    else if (document.body) // other Explorers
    {
        y = document.body.clientHeight;
    }
    return (y);
}

function getScrollOffset() {
    var y;
    if (self.pageYOffset) // all except Explorer
    {
        y = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
    {
        y = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
        y = document.body.scrollTop;
    }
    return y;
}

function GetWindowHieght() {
    var windowHeight;
    if (window.innerHeight) {
        windowHeight = window.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        windowHeight = document.documentElement.clientHeight;
    }
    else {
        windowHeight = document.body.clientHeight;
    }

    return windowHeight;
}

function posTop() {
    return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

function InactivateElements(parent, shadowID, className, important) {
    var shadowDiv = document.createElement("div");

    shadowDiv.id = shadowID;
    shadowDiv.className = className;
    shadowDiv.style.zIndex = 9999998 + important;
    shadowDiv.style.width = parent.offsetWidth + 'px';
    shadowDiv.style.height = parent.offsetHeight + 'px';
    shadowDiv.style.position = "absolute";

    parent.appendChild(shadowDiv);

    Sys.UI.DomElement.setLocation(shadowDiv, GetElementAbsoluteLeft(parent), GetElementAbsoluteTop(parent));
}

function ActivateElements(parentDiv, shadowID) {
    var shadowDiv = $get(shadowID);
    parentDiv.removeChild(shadowDiv);
}

function ShowObjectAndInactiveBackgroundInBrowserCenter(inactiveDiv, divForElement, element, className, shadowID, parentID, important) {
    divForElement.appendChild(element);
    element.style.display = '';
    element.style.zIndex = 9999999 + important;
    element.style.position = "absolute";
    var x = Math.round(getScreenCenterX()) - Math.round(element.clientWidth / 2);
    var y = Math.round(getScreenCenterY()) - Math.round(element.clientHeight / 2);
    Sys.UI.DomElement.setLocation(element, x, y);

    var shadowDiv = document.createElement("div");
    var parentIDHidden = document.createElement("input");

    shadowDiv.id = shadowID;
    shadowDiv.className = className;
    shadowDiv.style.zIndex = 9999998 + important;
    shadowDiv.style.width = '100%';
    shadowDiv.style.height = window.innerHeight > document.body.offsetHeight ? window.innerHeight + 'px' : document.body.offsetHeight + 'px';
    shadowDiv.style.position = "absolute";

    parentIDHidden.setAttribute("type", "hidden");
    parentIDHidden.value = parentID;
    parentIDHidden.id = shadowID + "parentID";

    shadowDiv.appendChild(parentIDHidden);

    Sys.UI.DomElement.setLocation(shadowDiv, GetElementLeft(inactiveDiv), GetElementTop(inactiveDiv));
    inactiveDiv.appendChild(shadowDiv);
}

function HideObjectAndInactiveBackground(inactiveDiv, element, shadowID) {
    element.style.display = 'none';
    element.style.zIndex = 0;
    var shadowDiv = $get(shadowID);
    var parentIDHidden = $get(shadowID + "parentID");
    if (parentIDHidden != undefined && parentIDHidden != null && parentIDHidden.value != "") {
        var parent = $get(parentIDHidden.value);
        document.body.removeChild(element);
        parent.appendChild(element);
    }
    try {
        shadowDiv.parentNode.removeChild(shadowDiv);
    }
    catch (err) {
        //inactiveDiv.removeChild(shadowDiv);
        //shadowID.parentNode.removeChild(shadowDiv);
    }
}

function ShowObjectAndInactiveBackgroundYCenter(inactiveDiv, divForElement, element, className, shadowID) {
    divForElement.appendChild(element);
    element.style.display = '';
    element.style.zIndex = 9999999;
    var x = Math.round(divForElement.clientWidth / 2) - Math.round(element.clientWidth / 2);
    var y = Math.round(getScreenCenterY()) - Math.round(element.clientHeight / 2);

    Sys.UI.DomElement.setLocation(element, x, y);

    var shadowDiv = document.createElement("div");
    shadowDiv.id = shadowID;
    shadowDiv.className = className;
    shadowDiv.style.zIndex = 9999998;
    shadowDiv.style.width = inactiveDiv.offsetWidth + 'px';
    shadowDiv.style.height = window.innerHeight > document.body.offsetHeight ? window.innerHeight + 'px' : document.body.offsetHeight + 'px';
    shadowDiv.style.position = "absolute";
    Sys.UI.DomElement.setLocation(shadowDiv, GetElementLeft(inactiveDiv), GetElementTop(inactiveDiv));
    inactiveDiv.appendChild(shadowDiv);
}

function ShowObjectAndInactiveBackground(inactiveDiv, divForElement, element, className, shadowID) {
    element.style.position = "absolute";

    RecenterObject(divForElement, element);

    var shadowDiv = document.createElement("div");
    shadowDiv.id = shadowID;
    shadowDiv.className = className;
    shadowDiv.style.zIndex = 9999998;
    shadowDiv.style.width = inactiveDiv.offsetWidth + 'px';
    shadowDiv.style.height = inactiveDiv.offsetHeight + 'px';
    shadowDiv.style.position = "absolute";
    inactiveDiv.appendChild(shadowDiv);
    Sys.UI.DomElement.setLocation(shadowDiv, GetElementAbsoluteLeft(inactiveDiv), GetElementAbsoluteTop(inactiveDiv));
}

function ShowInnerObjectAndInactiveBackground(inactiveDiv, divForElement, element, className, shadowID) {
    divForElement.appendChild(element);
    element.style.position = 'absolute';
    element.style.display = '';
    element.style.zIndex = 9999999;
    var x = Math.round((divForElement.offsetWidth / 2) - (element.offsetWidth / 2));
    var y = Math.round((divForElement.offsetHeight / 2) - (element.offsetHeight / 2));

    Sys.UI.DomElement.setLocation(element, x, y);

    var shadowDiv = document.createElement("div");
    shadowDiv.id = shadowID;
    shadowDiv.className = className;
    shadowDiv.style.zIndex = 9999998;
    shadowDiv.style.width = inactiveDiv.offsetWidth + 'px';
    shadowDiv.style.height = inactiveDiv.offsetHeight + 'px';
    shadowDiv.style.position = "absolute";
    Sys.UI.DomElement.setLocation(shadowDiv, GetElementAbsoluteLeft(inactiveDiv), GetElementAbsoluteTop(inactiveDiv));
    inactiveDiv.appendChild(shadowDiv);
    if (GetElementAbsoluteLeft(shadowDiv) != GetElementAbsoluteLeft(inactiveDiv) || GetElementAbsoluteTop(shadowDiv) != GetElementAbsoluteTop(inactiveDiv)) {
        Sys.UI.DomElement.setLocation(shadowDiv, GetElementLeft(inactiveDiv), GetElementTop(inactiveDiv));
    }
}

function RecenterObject(divForElement, element) {
    divForElement.appendChild(element);
    element.style.display = '';
    element.style.zIndex = 9999999;
    if (typeof (elementLeft) == "undefined") {
        elementLeft = element.offsetLeft;
    }
    var x = Math.round((GetElementAbsoluteLeft(divForElement) + (divForElement.offsetWidth / 2)) - (element.offsetWidth / 2));
    var y = Math.round(GetElementAbsoluteTop(divForElement) + (divForElement.offsetHeight / 2)) - (element.offsetHeight / 2);

    var windowTop = getScrollOffset();
    var windowBottom = GetWindowHieght() + windowTop;
    var divForElementTop = GetElementAbsoluteTop(divForElement);
    var divForElementBottom = GetElementAbsoluteTop(divForElement) + divForElement.offsetHeight;
    if (y < windowTop || y < divForElementTop) {
        y = Math.round(windowTop + (GetWindowHieght() / 2)) - (element.clientHeight / 2);
        if (y < divForElementTop) {
            y = Math.round(divForElementTop + 20) - (element.clientHeight / 2);
        }
    }

    if (y + element.clientHeight > windowBottom || y + element.clientHeight > divForElementBottom) {
        y = Math.round(windowTop + (GetWindowHieght() / 2)) - (element.clientHeight / 2);
        if (y + element.clientHeight > divForElementBottom) {
            y = divForElementBottom - element.clientHeight - 5;
        }
        if (y < divForElementTop) {
            y = divForElementTop + 20;
        }
    }
    Sys.UI.DomElement.setLocation(element, x, y);
}

function ShowImageLoaderObject(divForElement, element) {
    divForElement.appendChild(element);
    element.style.display = '';
    element.style.position = 'absolute';
    element.style.zIndex = 9999999;
    var x = Math.round(((GetElementLeft(divForElement) + divForElement.clientWidth) / 2) - (element.clientWidth / 2));
    var y = Math.round(((GetElementTop(divForElement) + divForElement.clientHeight) / 2) - (element.clientHeight / 2));

    Sys.UI.DomElement.setLocation(element, x, y);
}

function ShowObjectAndInactiveAllBackground(divForElement, element, className, shadowID) {
    divForElement.appendChild(element);
    element.style.display = '';
    element.style.zIndex = 9999999;
    var x = Math.round(divForElement.clientWidth / 2) - (element.clientWidth / 2);
    var y = Math.round(divForElement.clientHeight / 2) - (element.clientHeight / 2);

    Sys.UI.DomElement.setLocation(element, x, y);

    var shadowDiv = document.createElement("div");
    shadowDiv.id = shadowID;
    shadowDiv.className = className;
    shadowDiv.style.zIndex = 9999998;
    shadowDiv.style.width = document.body.offsetWidth + 'px';
    shadowDiv.style.height = document.body.offsetHeight + 'px';
    shadowDiv.style.position = "absolute";
    //shadowDiv.style.margin = "auto";
    Sys.UI.DomElement.setLocation(shadowDiv, GetElementLeft(document.body), GetElementTop(document.body));
    document.body.appendChild(shadowDiv);
}

function HideObjectAndActiveBackground(inactiveDiv, element, shadowID) {
    element.style.display = 'none';
    element.style.zIndex = 0;
    var shadowDiv = $get(shadowID);
    var parentIDHidden = $get(shadowID + "parentID");
    if (parentIDHidden != undefined && parentIDHidden != null && parentIDHidden.value != "") {
        var parent = $get(parentIDHidden.value);
        //document.body.removeChild(element);
        parent.appendChild(element);
    }
    else {
    try {
        inactiveDiv.appendChild(element);
    }
    catch (err) {
    }
    }
    try {
        shadowDiv.parentNode.removeChild(shadowDiv);
    }
    catch (err) {
        //inactiveDiv.removeChild(shadowDiv);
        //shadowID.parentNode.removeChild(shadowDiv);
    }
}

function HideObjectAndActiveBackgroundAndDestroyElement(element, shadowID) {
    element.style.display = 'none';
    element.style.zIndex = 0;
    var shadowDiv = $get(shadowID);
    try {
        shadowDiv.parentNode.removeChild(shadowDiv);
    }
    catch (err) {

    }
    try{
        element.parentNode.removeChild(element);
    }
    catch (err) {
        ;
    }
}

function CreateLoader(loaderID, text) {
    var loaderDiv = document.createElement("div");
    loaderDiv.className = "div_loading_catalog";
    loaderDiv.id = loaderID;
    var textSpan = document.createElement("span");
    textSpan.innerHTML = text;
    var lf = document.createElement('br');
    var image = document.createElement('img');
    image.src = "app/img/loader.gif";
    image.alt = text;
    image.title = text;
    loaderDiv.appendChild(textSpan);
    loaderDiv.appendChild(lf);
    loaderDiv.appendChild(image);

    return loaderDiv;
}

function CreateImageLoader(loaderID) {
    var image = document.createElement('img');
    image.src = "app/img/imageLoader.gif";
    image.alt = "Nahrávám...";
    image.title = "Nahrávám...";
    image.id = loaderID;

    return image;
}

function PreloadImages(imagesDiv) {
    var images = imagesDiv.getElementsByTagName('img');

    for (var i = 0; i < images.length; i++) {
        var img = new Image();
        img.scr = images[i].src;
    }
}

function HideObjectAndActiveAllBackground(element, shadowID) {
    element.style.display = 'none';
    element.style.zIndex = 0;
    var shadowDiv = $get(shadowID);
    var parentIDHidden = $get(shadowID + "parentID");
    if (parentIDHidden != undefined && parentIDHidden != null && parentIDHidden.value != "") {
        var parent = $get(parentIDHidden.value);
        document.body.removeChild(element);
        parent.appendChild(element);
    }
    try {
        shadowDiv.parentNode.removeChild(shadowDiv);
    }
    catch (err) {
        //inactiveDiv.removeChild(shadowDiv);
        //shadowID.parentNode.removeChild(shadowDiv);
    }
}

function HideObjectAndActiveBackgroundAndReturnObjectToParent(inactiveDiv, element, shadowID, parent) {
    element.style.display = 'none';
    element.style.zIndex = 0;
    var shadowDiv = $get(shadowID);
    inactiveDiv.removeChild(shadowDiv);
    parent.appendChild(element);
}

function ShiftElementAfterWaitTime(element, direction, shiftLong) {
    var shift = direction == "left" ? 1 : -1;
    for (var i = 0; i < shiftLong; i++) {
        Sys.UI.DomElement.setLocation(element, GetElementLeft(element) + shift, GetElementTop(element));
        PauseComp(20);
    }
}

function PauseComp(millis) {
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while (curDate - date < millis);
}

function ConfirmDialog(msg) {
    return window.confirm(msg)
}

//skryje nebo ukaze objekt dle jeho id
//nejprve hleda serverovy objekt, pote klientsky
function ShowHide(id) {

    var o = document.getElementById(id_prefix + id);
    if (!o) o = document.getElementById(id);
    if (!o) {
        return;
    }

    if (o.style.display == 'none' || o.style.display == 'NONE')
        o.style.display = 'block';
    else
        o.style.display = 'none';
}

function ShowHideMsg(id) {

    var o = document.getElementById(id_prefix + id);
    if (!o) o = document.getElementById(id);
    if (!o) {
        alert('Control not found: ' + id);
        return;
    }

    if (o.style.display == 'none' || o.style.display == 'NONE')
        o.style.display = 'block';
    else
        o.style.display = 'none';

}

function GetMyElementById(id) {

    var o = document.getElementById(id_prefix + id);
    if (!o) o = document.getElementById(id);
    return o;

}


var windowShowTreeSimple = "";

function ShowTreeSimple(clientId) {

    var url = "TreeSimple.aspx?targetId=" + clientId;

    if (!windowShowTreeSimple.close && windowShowTreeSimple.location) {
        windowShowTreeSimple.location.href = url;
    }
    else {
        windowShowTreeSimple = window.open(url, "_blank", "height=500,width=400,resizable=no,toolbar=no,menubar=no,titlebar=no,status=no,scrollbars=yes", "false");
        if (!windowShowTreeSimple.opener) { windowShowTreeSimple = self; }
    }

    if (window.focus) { windowShowTreeSimple.focus() }
    return false;
}

var windowShowTreeAction = "";

function ShowTreeAction(clientId, akce) {

    var url = "TreeAction.aspx?targetId=" + clientId + "&akce=" + akce;

    if (!windowShowTreeAction.close && windowShowTreeAction.location) {
        windowShowTreeAction.location.href = url;
    }
    else {
        windowShowTreeAction = window.open(url, "_blank", "height=500,width=400,resizable=no,toolbar=no,menubar=no,titlebar=no,status=no,scrollbars=yes", "false");
        if (!windowShowTreeAction.opener) { windowShowTreeAction = self; }
    }

    if (window.focus) { windowShowTreeAction.focus() }
    return false;
}

function GoToLink(id) {

    var obj = GetMyElementById(id);
    if (obj) {
        obj.scrollIntoView(true);
    }

}



function MyRemoveSelectItems(o) {
    for (var i = o.options.length - 1; i > 0; i--) {
        if (o.options.remove)
            o.options.remove(i) //IE
        else
            o.options[i] = null; //firefox
    }
}

function MyAddSelectItem(o, text) {
    var item = document.createElement('option');
    o.options.add(item);
    item.text = text;
    return item;
}

function MyAddSelectItem2(o, text, val) {
    var item = document.createElement('option');
    o.options.add(item);
    item.text = text;
    item.value = val;
    return item;
}
