function helpWin(url)
{
    window.open(url, "_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=yes, width=520, height=500")
}

function openWin(url)
{
    var height = 650                    //Set height
    var width = 500                     //Set width
    var name = "winname"                //Set window name
    var top = 20                        //Set distance from top
    var left = 20                       //Set distance from bottom
    newwin = window.open(url, name, "fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,directories=no,location=no,width=" + width + ",height=" + height + ",left=" + left + ",top=" + top);
}

function openWin(url, name) {
    var height = 650                    //Set height
    var width = 500                     //Set width
    var top = 20                        //Set distance from top
    var left = 20                       //Set distance from bottom
    newwin = window.open(url, name, "fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,directories=no,location=no,width=" + width + ",height=" + height + ",left=" + left + ",top=" + top);
}

function fknOrder(str1, str2, str3)
{
    alert("Best&auml;llfunktionen har ej tagits i drift");
}

function toggleTree()
{
    HideFrame(); return;
}

//kind of a hack which gets the treestate, toggles the switch, then executes a treetoggle, to preserve users's previous state
function ensureTreeState()
{
    if (top.document.getElementById("colset") != null)
    {
        var flag = getTreeFlag();
        flag = Math.abs(flag - 1);
        document.cookie = "treeflag=" + String(flag);
        toggleTree();
    }
}

function toggle(theid, show)
{
    var x = document.getElementById(theid);
    if (x == null)
    {
        alert("null ref on: " + theid);
        return;
    }

    if (show == null)
        x.style.display = x.style.display == "none" ? "block" : "none";
    else
        x.style.display = show ? "block" : "none";
}

function toggleInline(theid)
{
    var x = document.getElementById(theid);
    if (x == null)
    {
        alert("null ref on: " + string(theid));
        return;
    }
    x.style.display = x.style.display == "none" ? "inline" : "none";
}

function HideFrame()
{
    var treeflag = getTreeFlag();
    if (treeflag == 0)
    {
        treeflag = 1;
        top.document.getElementById("colset").cols = "0,*";
        document.getElementById("toggleLink").innerHTML = "Visa&nbsp;&ndash;&gt;"; // tr&auml;det";
        document.getElementById("uppdateraLink").innerHTML = "";
        document.getElementById("treeControlDivider").innerHTML = "";
    }
    else
    {
        treeflag = 0;
        top.document.getElementById("colset").cols = "30%,*";
        document.getElementById("toggleLink").innerHTML = "&lt;&ndash;&nbsp;D&ouml;lj"; // tr&auml;det";
        document.getElementById("uppdateraLink").innerHTML = "Uppdatera";
        document.getElementById("treeControlDivider").innerHTML = ", ";
    }

    setCookie('treeflag', String(treeflag));
}

function getTreeFlag()
{
    var flag = getCookie("treeflag");
    if (flag == null)
        flag = 0;

    return flag;
}

/**
* http://www.netspade.com/articles/javascript/cookies.xml
* Sets a Cookie with the given name and value.
*
* name       Name of the cookie
* value      Value of the cookie
* [expires]  Expiration date of the cookie (default: end of current session)
* [path]     Path where the cookie is valid (default: path of calling document)
* [domain]   Domain where the cookie is valid
*              (default: domain of calling document)
* [secure]   Boolean value indicating if the cookie transmission requires a
*              secure transmission
*/
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
* Deletes the specified cookie.
*
* name      name of the cookie
* [path]    path of the cookie (must be same as path used to create cookie)
* [domain]  domain of the cookie (must be same as domain used to create cookie)
*/
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

/* Used mostly for assisting the tabs on the search pages when user clicks on tablecell instead of link inside tablecell . */
function followLink(link)
{
    window.location = link;
}

/* toggles cookie to keep search params displayed or hidden */
function setKeepOpen()
{
    if (document.getElementById("chkKeepOpen").checked)
        setCookie('searchKeepOpen', '1');
    else
        setCookie('searchKeepOpen', '0');
}

/*determines which search page to go to depending on a set cookie*/
function newSearch()
{
    var currentSearchPage = getCookie("currentSearchPage");
    if (currentSearchPage == null)
        currentSearchPage = "auth";

    var location = "search_" + currentSearchPage + ".aspx";

    if (self == top)
        parent.location = location;
    else
        top.location = location;
}

function redirectWithQueryString(newPage)
{
    if (window.top == window.self)
    {
        var query = this.location.search.substring(1);
        window.location = newPage + "?" + query;
    }
}

function getUrlParam(name)
{
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

function trim(str, trimChar)
{
    if (trimChar === undefined)
        trimChar = '';

    return str.replace(/^\s+|\s+$/g, trimChar);
}

function getSelectIndex(selectId, text)
{
    for (i = 0; i < getElementInMaster(selectId).length; i++)
        if (text.toLowerCase() == getElementInMaster(selectId).options[i].text.toLowerCase())
            return i;
}
