/*
 * phpaga
 *
 * This file contains some core JavaScript functionality for phpaga.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: core.js 875 2007-10-21 19:31:31Z florian $
 *
 * Copyright (c) 2006, Florian Lanthaler <florian@phpaga.net>
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in
 *      the documentation and/or other materials provided with the
 *      distribution.
 *
 *    * Neither the name of Florian Lanthaler nor the names of his
 *      contributors may be used to endorse or promote products derived
 *      from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


function getEl(i) {
    return document.getElementById(i);
}

/* Interface helpers */

function showPageStatus() {
    document.getElementById("pagestatus").style.display = "block";
}

function hidePageStatus() {
    document.getElementById("pagestatus").style.display = "none";
}

function cleanHideElement(elname) {
    document.getElementById(elname).innerHTML = "";
    document.getElementById(elname).style.visibility = "hidden";
}

function hideElement(elname) {
    document.getElementById(elname).style.visibility = "hidden";
}

function showElement(elname) {
    document.getElementById(elname).style.visibility = "visible";
}

function show_box(linkid, boxid) {

    v = document.getElementById(linkid);
    x = findPosX(v);
    y = findPosY(v);

    u = document.getElementById(boxid);
    w = u.offsetWidth;

    if (x > w) {
        x = x - w;
    }

    if (y > u.offsetHeight) {
        y = y - u.offsetHeight + 20;
    }

    u.style.left = x+"px";
    u.style.top = y+"px";
    u.style.visibility = "visible";
}


/* Date and time */

function getCurrentTimeString() {

    var d = new Date();
    var hh = d.getHours();
    var mm = d.getMinutes();

    if (hh < 10)
        hh = '0'+hh;

    if (mm < 10)
        mm = '0'+mm;

    var time = hh+':'+mm;

    return time;
}

function setCurrentTime(id) {

    var el = document.getElementById(id);

    if (el)
        el.value = getCurrentTimeString();

}

function getCurrentDateString() {

    var d = new Date();

    var month = d.getMonth() + 1;
    var day = d.getDate();

    if (month < 10)
        month = '0'+month;

    if (day < 10)
        day = '0'+day;

    var da = d.getFullYear()+'-'+month+'-'+day;

    return da;
}

function setCurrentDateTime(did, tid) {

    var del = document.getElementById(did);
    var tel = document.getElementById(tid);

    if (del)
        del.value = getCurrentDateString();

    if (tel)
        tel.value = getCurrentTimeString();
}

function getDateTimeDiff(date2, date1) {
    var B, D, E, F = 0;
    var diff, hours, mins = 0;

    B = /^(\d{4})-(\d{1,2})-(\d{1,2}) (\d{1,2})[-:]{1}(\d\d)$/.test(date1);
    if (B) {
        with (RegExp) {
            D = new Date($1, $2 - 1, $3, $4, $5);
        }
    }

    E = /^(\d{4})-(\d{1,2})-(\d{1,2}) (\d{1,2})[-:]{1}(\d\d)$/.test(date2);
    if (E) {
        with (RegExp) {
            F = new Date($1, $2 - 1, $3, $4, $5);
        }
    }

    if (!D || !E)
        return('');
    else
    {

        diff = ((D.getTime() - F.getTime()) / 1000) / 60;
        hours = Math.floor(diff / 60);
        mins = diff % 60;

        if ((hours < 10) && (hours >= 0))
            hours = '0' + hours;

        if (mins < 10)
            mins = '0' + mins;

        return (hours + ':' + mins);
    }
}

function setmenu(mainmenu) {

    var items = '<ul>';
    var mitem;

    var selmenu = menu[mainmenu];
    for (mitem in selmenu)
    {
        if (mitem != 'each') {
            items += '<li><a href="'+ mitem + '">' + selmenu[mitem] + '</a></li>';
        }
    }

    items += '</ul>';

    document.getElementById("ctxtnav").innerHTML = items;


    var navlist=document.getElementById('navitems');

    if(navlist)
    {

        litems=navlist.getElementsByTagName('li');
        for(i=0;i<litems.length;i++)
        {

            if (litems[i].className.indexOf('active') != -1) {
                litems[i].className = '';
            }
        }

        document.getElementById('nav'+mainmenu).className = 'active';

    }
}

function clockTimerAction() {

    AjaxRequest.get(
                    {
                        'url':'service.php?saction=49',
                            'onSuccess':function(req){
                            document.getElementById("clock").innerHTML = req.responseText;
                        }
                    }
                    );
}

/* Various helper funtions */

function isNumeric(val) {

    var charpos = val.search("[^0-9.]");

    if(val.length > 0 &&  charpos >= 0)
        return false;
    else
        return true;
}


/* findPosX() and findPosY() by Peter-Paul Koch
 * taken from http://www.quirksmode.org/js/findpos.html */

function findPosX(obj)
{
    var curleft = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curleft += obj.offsetLeft
                obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curtop += obj.offsetTop
                obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}
