﻿
var WinDetails;

var popWDefault = 650;
var popW = screen.width / 3;
var popHDefault = 650;
var popH = screen.height * 4 / 5;

/**
* Opens a file in a new window with the passed width, height and scrollbars-options.
*/
function NewWindow(filename, w, h, scrollbars) {
    var scroll = "no";
    if (scrollbars) {
        scroll = scrollbars;
    }
    WinDetails = window.open(filename, "Win1", "dependent=yes,resizable=yes,status=yes,scrollbars=" + scroll + ",width=" + w + ",height=" + h);
    WinDetails.focus();
}

/**
* A short method for document.getElementById(id)
* <remark>Doubt it is such a big profit.</remark>
*/
function $(id) {
    return document.getElementById(id);
}

/**
* Shows or HIDES the element with the id equals toggleId. An element with the id <code>toggleId+"_toggle"</code> is used for status-icons, if it exists.
* Never used!
*/
function showContent(toggleId) {
    if ($(toggleId).style.display == "none") {
        $(toggleId).style.display = "block";
        if ($(toggleId + "_toggle")) {
            $(toggleId + "_toggle").innerHTML = '<img src="/App_Themes/Default/images/up.png" />';
        }
    } else {
        $(toggleId).style.display = "none";
        if ($(toggleId + "_toggle")) {
            $(toggleId + "_toggle").innerHTML = '<img src="/App_Themes/Default/images/down.png" />';
        }
    }
}
/**
* Deletes all whitespace chars in front and at end of the string(value).
*/
function trim(value) {
    return value.replace(/^\s+/, '').replace(/\s+$/, '');
}

/**
* Deletes all HTML-Tags form a string.
* <remark>used only once in LayerManager can be moved to LayerManager.</remark>
*/
function removeHTMLTags(value) {
    var strInputCode = value;
    strInputCode = strInputCode.replace(/&(lt|gt);/g, function(strMatch, p1) {
        return (p1 == 'lt') ? '<' : '>';
    });
    var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, '');
    return (strTagStrippedText);
}

/**
* Returns the value of the parameter with the passed name from the querystring.
* @param queryString A string, should be in formated like an Url with GET-Parameters
* @param parameterName The name of the parameter searched in the queryString
*/
function getParameter(queryString, parameterName) {
    var parameterName = parameterName + "=";
    if (queryString.length > 0) {
        begin = queryString.indexOf(parameterName);
        if (begin != -1) {
            begin += parameterName.length;
            end = queryString.indexOf('&', begin);
            if (end == -1) {
                end = queryString.length;
            }
            return unescape(queryString.substring(begin, end));
        } else {
            return '';
        }
    }
}

/**
* Checks if the container is a parent of containee.
* Never used! expect in the two methods below, which are also never used.
*/
function containsDOM(container, containee) {
    var isParent = false;
    do {
        if ((isParent = container == containee))
            break;
        containee = containee.parentNode;
    }
    while (containee != null);
    return isParent;
}

/**
* Checks if the mouse enters a container at an event.
* Never used! Not verified that it works correct in all browsers and situations
*/
function checkMouseEnter(element, evt) {
    if (element.contains && evt.fromElement) {
        return !element.contains(evt.fromElement);
    }
    else if (evt.relatedTarget) {
        return !containsDOM(element, evt.relatedTarget);
    }
}

/**
* Checks if the mouse leaves a container at an event.
* Never used! Not verified that it works correct in all browsers and situations
*/
function checkMouseLeave(element, evt) {
    if (element.contains && evt.toElement) {
        return !element.contains(evt.toElement);
    }
    else if (evt.relatedTarget) {
        return !containsDOM(element, evt.relatedTarget);
    }
}

// *** cookie handling

/**
* Returns the value of the cookie with the passed name, if exists.
*/
function getCookieEx(name) {
    var cCookie = document.cookie;
    var index = cCookie.indexOf(name + "=");

    if (index == -1) return null;

    index = cCookie.indexOf("=", index) + 1;
    var endstr = cCookie.indexOf(";", index);
    if (endstr == -1) endstr = cCookie.length;
    return unescape(cCookie.substring(index, endstr));
}

/**
* Creates a new cookie with the passed name and value. The cookie will expire one year from creation.
*/
function setCookieEx(name, value) {
    expireDate = new Date();
    expireDate.setDate(expireDate.getDate() + 365);
    document.cookie = name + "=" + escape(value) + ";expires=" + expireDate.toGMTString() + ";path=/";
}

/**
* Deletes the cookie with the passed name.
*/
function deleteCookieEx(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = GetCookieEx(name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
