//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// js/functions.js
// Some useful utility functions...
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

var cms_onload_functions = new Array();

/**
 * Sets a function to be called when the onload event occours.
 *
 * @param func The function that should be called on load.
 **/
function cms_add_onload_function(func)
{
    cms_onload_functions.push(func);
}

/**
 * This function is called when the onload event occours.
 * Never call directly!
 **/
function cms_onload(e)
{
    if (!e)
    {
        e = window.event;
    }

    for (var i in cms_onload_functions)
    {
        cms_onload_functions[i](e);
    }
}

/**
 * Redirect to the given url (last update: 27/03/2007)
 * This is just a wrapper function.
 *
 * @param url The url to redirect to.
 **/
function redirect(url)
{
    location.href = url;
}

/**
 * Searches value in an array. The array don't need to be sorted.
 *
 * @param value
 * @param array
 * @return Returns true if the value was found, false otherwise.
 **/
function array_search(value, array)
{
    for (var i = 0; i < array.length; i++)
    {
        if (array[i] == value)
        {
            return true;
        }
    }

    return false;
}

/**
 * Creates a loginhash by password, static key and authhash (last update: 16/06/2007)
 *
 * @param Uncrypted password.
 * @param Static key string.
 * @param Authhash of the session.
 * @return Loginhash that can be transmitted over the net.
 **/
function generateLoginHash(password, static_key, authhash)
{
    // Encrypt password
    password = hex_md5(password);

    // Create loginhash
    return hex_md5(password + "|" + static_key + "|" + authhash);
}

//------------------------------------------------------------------------------
// Node manipulation/information
//------------------------------------------------------------------------------

/**
 * Returns the html object with the given ID (last update: 06/04/07)
 * This is just s wrapper function.
 *
 * @param elementID The ID of the element.
 * @return The html element object.
 **/
function getElement(elementID)
{
    return document.getElementById(elementID);
}

/**
 * Returns the value of a html object (last update: 06/04/07)
 *
 * @param elementID The ID of the element.
 * @return The value of the html element.
 **/
function getElementValue(elementID)
{
    return getElement(elementID).value;
}

/**
 * Sets the value of a html object (last update: 18/04/07)
 *
 * @param elementID The ID of the element.
 * @param value The new value of the element.
 **/
function setElementValue(elementID, newValue)
{
    return getElement(elementID).value = newValue;
}

/**
 * Returns the selected text of a combo box (last update: 06/04/07)
 *
 * @param elementID The ID of the element.
 * @return The text of the selected option.
 **/
function getSelectedText(elementID)
{
    var element = getElement(elementID);
    return element.options[element.selectedIndex].text;
}

/**
 * Selects all options of a combo box (last update: 18/05/07)
 *
 * @param elementID The ID of the element.
 **/
function selectAllOptions(elementID)
{
    var element = getElement(elementID);

    for (var i = 0; i < element.options.length; i++)
    {
        element.options[i].selected = true;
    }
}

/**
 * Checks or unchecks all checkboxes with the target name.
 *
 * @param targets The ID of the elements to toggle.
 * @param checked True if the boxes should be checked, false otherwise.
 **/
function toggleCheckbox(targets, checked)
{
    var elements = document.getElementsByName(targets);

    for (var i = 0; i < elements.length; i++)
    {
        elements[i].checked = checked;
    }
}

/**
 * Returns the absolute position of an element node relative to the document.
 * See the link for a detailed explanation.
 *
 * @param elementID The ID of the element.
 * @return The position object of the element (retval.x, retval.y)
 * @link http://www.quirksmode.org/js/findpos.html
 **/
function getElementPositionAbsolute(elementID)
{
    var element = getElement(elementID);
    var x = element.offsetLeft, y = element.offsetTop;

    while (element = element.offsetParent)
    {
        x += element.offsetLeft;
        y += element.offsetTop;
    }

    return {"x" : x, "y" : y};
}

/**
 * Toggles the visibility of the given element (last update: 15/04/07)
 **/
function toggle(menuid)
{
    var display = getElement(menuid).style.display;

    // Display attribute is not initialised yet
    if (!display)
    {
        display = getElement(menuid).className == "togglecontainer expanded" ? "block" : "none";
    }

    switch (display)
    {
        case 'none':
            getElement(menuid).style.display = "block";
            break;

        default:
            getElement(menuid).style.display = "none";
    }
}

//------------------------------------------------------------------------------
// Window functions
//------------------------------------------------------------------------------

/**
 * Creates a new child window with default options (last update: 16/04/2007)
 *
 * @param url The url of the page to be loaded in the new window.
 * @param name Name of the window.
 * @return The window object
 **/
function createWindow(url, name)
{
    return window.open(url, name, "menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes");
}

/**
 * Resize a window to the maximum possible size (last update: 16/04/2007)
 *
 * @param The window to maximize.
 **/
function maximizeWindow(wnd)
{
    wnd.moveTo(0, 0);
    wnd.resizeTo(screen.availWidth, screen.availHeight);
}

/**
 * Returns the inner heigth of the window.
 *
 * @link http://www.quirksmode.org/viewport/compatibility.html#link2
 * @return The inner height of the window in px.
 **/
function getWindowInnerHeight()
{
    // all except Explorer
    if (window.innerHeight)
    {
    	return window.innerHeight;
    }
    // Explorer 6 Strict Mode
    else if (document.documentElement && document.documentElement.clientHeight)
    {
    	return document.documentElement.clientHeight;
    }
    // other Explorers
    else if (document.body)
    {
    	return document.body.clientHeight;
    }
}

//------------------------------------------------------------------------------
// Misc
//------------------------------------------------------------------------------

/**
 * Decrypts an email that was encrypted with the PHP function encrpyt_mail().
 * Opens the default mail application.
 * @see lib/functions.php
 *
 * @param str The encrypted email.
 **/
function decrypt_mail(str)
{
    redirect("mailto:" + strrev(rot13(str)));
}


//------------------------------------------------------------------------------
// Utilities
//------------------------------------------------------------------------------

/**
 * Encrypts a string using the rot13 algorithm.
 *
 * @param str The original string.
 * @return The rot13 encrypted string.
 **/
function rot13(str)
{
    var str_rot13 = "";

    for (var i = 0; i < str.length; i++)
    {
        var c = str.charCodeAt(i);

        // Lowercase
        if (c >= 97 && c <= 122)
        {
            c = c <= 109 ? c + 13 : c - 13;
        }
        // Uppercase
        else if (c >= 65 && c <= 90)
        {
            c = c <= 77 ? c + 13 : c - 13;
        }

        str_rot13 += String.fromCharCode(c);
    }

    return str_rot13;
}

/**
 * Reverses a string.
 *
 * @param str The original string.
 * @return The reversed string.
 **/
function strrev(str)
{
    var str_reverse = "";
    for (var i = 0; i < str.length; i++)
    {
        str_reverse = str.charAt(i) + str_reverse;
    }
    return str_reverse;
}

