
function toggleLog()
{
    var logBox = document.getElementById('log');
    if (!logBox) return false;
    if (logBox.style.display == 'block')
        logBox.style.display = 'none';
    else
        logBox.style.display = 'block';
    return false;
}

function openWindow(url)
{
    window.open(url,'_blank');
}

var currentPage = Array();
var currentTitle = Array();

function divBookActivate( book, title )
{
    var obj = document.getElementById( 'divBook_'+book+'_'+title );
    var objT = document.getElementById( 'divBookT_'+book+'_'+title );
	
    if (obj)
    {
        if (currentPage[book])
        {
            currentPage[book].style.display = 'none';
        }
        if (currentPage[book] == obj)
        {
            currentPage[book] = false;
        } 
        else
        {
            currentPage[book] = obj;
            currentPage[book].style.display = 'block';
        }
		
        if (currentTitle[book])
        {
            currentTitle[book].className='divBookTitle';
        }
        if (currentTitle[book] == objT)
        {
            currentTitle[book] = false;
        } 
        else
        {
            currentTitle[book] = objT;
            currentTitle[book].className='divBookSelected';
        }
    }
}

function createCookie(name,value,days)
{
    var expires = "";
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires = "; expires="+date.toGMTString();
    }
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++)
    {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name)
{
    createCookie(name,"",-1);
}

/**
*
* URL encode / decode
* http://www.webtoolkit.info/
*
**/

var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

var BrowserDetect = {
    init: function () {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
        || this.searchVersion(navigator.appVersion)
        || "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
        for (var i=0;i<data.length;i++)	{
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function (dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
    },
    dataBrowser: [
    {
        string: navigator.userAgent,
        subString: "OmniWeb",
        versionSearch: "OmniWeb/",
        identity: "OmniWeb"
    },
    {
        string: navigator.vendor,
        subString: "Apple",
        identity: "Safari"
    },
    {
        prop: window.opera,
        identity: "Opera"
    },
    {
        string: navigator.vendor,
        subString: "iCab",
        identity: "iCab"
    },
    {
        string: navigator.vendor,
        subString: "KDE",
        identity: "Konqueror"
    },
    {
        string: navigator.userAgent,
        subString: "Firefox",
        identity: "Firefox"
    },
    {
        string: navigator.vendor,
        subString: "Camino",
        identity: "Camino"
    },
    {		// for newer Netscapes (6+)
        string: navigator.userAgent,
        subString: "Netscape",
        identity: "Netscape"
    },
    {
        string: navigator.userAgent,
        subString: "MSIE",
        identity: "Explorer",
        versionSearch: "MSIE"
    },
    {
        string: navigator.userAgent,
        subString: "Gecko",
        identity: "Mozilla",
        versionSearch: "rv"
    },
    { 		// for older Netscapes (4-)
        string: navigator.userAgent,
        subString: "Mozilla",
        identity: "Netscape",
        versionSearch: "Mozilla"
    }
    ],
    dataOS : [
    {
        string: navigator.platform,
        subString: "Win",
        identity: "Windows"
    },
    {
        string: navigator.platform,
        subString: "Mac",
        identity: "Mac"
    },
    {
        string: navigator.platform,
        subString: "Linux",
        identity: "Linux"
    }
    ]

};
BrowserDetect.init();

var TSElements=Array();

function printTree(tree)
{
    var out = "";
    for (var i=0; i<tree.length; i++)
    {
        out += tree[i] + '.';
        if (tree[tree[i]]!=undefined && tree[tree[i]].length)
        {
            out += '['+printTree(tree[tree[i]])+']';
        }
    }
    return out;
}

function TreeAddElement( tree, element )
{
    var e = element.split('/');
    for (var i=0; i<e.length; i++)
    {
        if (tree[e[i]]==undefined)
        {
            tree.push(e[i]);
            tree[e[i]] = Array();
        }
        tree = tree[e[i]];
    }
}

function TreeBuildSelect( tree, path )
{
    var e = path.split('/');
    for (var i=0; i<e.length; i++)
        tree = tree[e[i]];
    if (tree==undefined || !tree.length) return undefined;
	
    var select = document.createElement("select");
    select.name = e[0]+'-'+ e.length;
    select.id = e[0]+'-'+e.length;
    var ia = 0;
    if (e.length>1)
    {
        select.options[0] = new Option('---', 0);
        ia = 1;
    }
    for (var i=0; i<tree.length; i++)
    {
        select.options[ia+i] = new Option(tree[i], tree[i]);
    }
    select.selectedIndex = 0;
    select.onchange=function()
    {
        TreeSelected( this );
    };
    return select;
}

function TreeSelected( select )
{
    var name = select.name;
    var pos = name.lastIndexOf('-');
    var level = name.substr(pos+1);
    name = name.substr(0, pos);
    var path = name;
    for (var i = 1; i<=level; i++)
    {
        var s = document.getElementById(name+'-'+i);
        if (i>1 && s.selectedIndex == 0) break;
        path += '/'+s.options[s.selectedIndex].value;
    }
    var parent = select.parentNode;
    while (s=document.getElementById(name+'-'+i))
    {
        parent.removeChild(s);
        i++;
    }
    var s = TreeBuildSelect(TSElements, path);
    if (s)
        parent.appendChild(s, null);
	
    var s = document.getElementById(name);
    var p = s.options.length-1;
    while (p && name+'/'+s.options[p].text!=path) p--;
	
    s.selectedIndex = p;
}

function TreeSelectValue( path )
{
    var e = path.split('/');
    for (var i=1; i<e.length; i++)
    {
        var s = document.getElementById(e[0]+'-'+i);
        var p=0;
        for (var j = 0; j<s.options.length; j++)
            if (s.options[j].value==e[i]) p=j;
        s.selectedIndex = p;
        TreeSelected(s);
    }
}

function InitTreeSelect( treename )
{
    var select = document.getElementById(treename);
    if (!select) return;

	
    var parent = select.parentNode;
    TSElements.push(treename);
    TSElements[treename] = Array();
    for (var i=0; i<select.options.length; i++)
    {
        TreeAddElement(TSElements[treename], select.options[i].text);
    }
    //	alert(printTree(TSElements[treename]));
    var lvl0 = TreeBuildSelect( TSElements, treename );
    parent.appendChild(lvl0);
    TreeSelectValue( treename+'/'+select.options[select.selectedIndex].text );
    select.style.display='none';
}

function addHoverFunction(navRoot)
{
    if (!navRoot) return;
    for (var i=0; i<navRoot.childNodes.length; i++)
    {
        var node = navRoot.childNodes[i];
        if (node.nodeName=="LI")
        {
            node.onmouseover=function()
            {
                this.className+=" over";
                this.style.zIndex = 2222;
            }
            node.onmouseout=function()
            {
                this.className=this.className.replace(" over", "");
                this.style.zIndex = 999;
            }
            var uls = node.getElementsByTagName("UL");
            for (var j=0; j<uls.length; j++)
            {
                addHoverFunction(uls[j]);
            }
        }
    }
}


var myDays = ["vasárnap","hétfő","kedd","szerda","csütörtök","péntek","szombat","vasárnap"];

function updateDate( name )
{
    var year = document.getElementById(name+'_year');
    var month = document.getElementById(name+'_month');
    var day = document.getElementById(name+'_day');
    if (!year || !month || !day) return;
    year = year.value;
    month = month.value;
    dayvalue = day.value;
    var date = new Date();
    date.setFullYear(year, month-1, 1);
    var dayofweek=date.getDay();
    var szoko = (year % 4 == 0 && (year%100!=0 || year%400==0));
    if (month == 2)
    {
        if (szoko) days = 29;
        else days = 28;
    }
    else
    {
        days = 30;
        if (month %2 == 1 && month <= 7) days++;
        else if (month % 2 == 0 && month >= 8) days++;
    }
    while (day.length>1)
    {
        day.remove(day.length-1);
    }
    while (day.length <= days)
    {
        var newday = document.createElement('option');
        newday.text = (day.length)+" ("+myDays[dayofweek % 7]+")";
        newday.value = day.length;
        try
        {
            day.add(newday, null);
        }
        catch(ex)
        {
            day.add(newday);
        }
        dayofweek++;
    }
    if (days>dayvalue)
        day.value = dayvalue;
    else
        day.value=days;
}

function setMonthInterval(from, to, month)
{
    fyear = document.getElementById(from+'_year');
    fmonth = document.getElementById(from+'_month');
    fday = document.getElementById(from+'_day');

    tyear = document.getElementById(to+'_year');
    tmonth = document.getElementById(to+'_month');
    tday = document.getElementById(to+'_day');
    if (!fyear || !fmonth || !fday) return;
    if (!tyear || !tmonth || !tday) return;

    fmonth.value = month+1;
    if (month < 11)
    {
        tyear.value = fyear.value;
        tmonth.value = month+2;
    }
    else
    {
        tyear.value = parseInt(fyear.value)+1;
        tmonth.value = 1;
    }
    fday.value = 1;
    tday.value = 1;
    updateDate(from);
    updateDate(to);
}

function setYearInterval(from, to)
{
    fyear = document.getElementById(from+'_year');
    fmonth = document.getElementById(from+'_month');
    fday = document.getElementById(from+'_day');

    tyear = document.getElementById(to+'_year');
    tmonth = document.getElementById(to+'_month');
    tday = document.getElementById(to+'_day');
    if (!fyear || !fmonth || !fday) return;
    if (!tyear || !tmonth || !tday) return;

    fmonth.value = 1;
    fmonth.value = 1;
    fday.value = 1;

    tyear.value = parseInt(fyear.value)+1;
    tmonth.value = 1;
    tday.value = 1;

    updateDate(from);
    updateDate(to);
}


var JSLog = false;
function myLog( msg )
{
    if (!JSLog)
    {
        JSLog = document.createElement('div');
        JSLog.setAttribute('id', 'JSLog');
        JSLog.setAttribute('style', '\
position: absolute; \n\
top: 10px; \n\
right: 10px; \n\
width: 240px; \n\
height: 120px; \n\
padding: 0.5em; \n\
background-color: #800000; \n\
color: orange; \n\
border: 1px solid red; \n\
overflow: auto; \n\
font-size: 10pt;');
        document.body.appendChild(JSLog);
    }
    if (JSLog)
    {
        var div = document.createElement('div');
        div.setAttribute('style', 'border-bottom: 1px solid black; padding-bottom: 4px; margin-bottom: 4px;');
        var text = document.createTextNode(msg);
        div.appendChild(text);
        JSLog.insertBefore(div, JSLog.firstChild);
    }
}

function displayHint( id )
{
    var obj = document.getElementById(id);
    if (!obj) return;
    obj.style.display='block';
}

function hideHint( id )
{
    var obj = document.getElementById(id);
    if (!obj) return;
    obj.style.display='none';
}

function showHide( id, state )
{
    var obj = document.getElementById(id);
    if (!obj) return false;
    if (state)
    {
        obj.style.display='';
    }
    else
    {
        obj.style.display='none';
    }
    var b1 = document.getElementById(id+"_show");
    var b2 = document.getElementById(id+"_hide");
    if (!b1 || !b2)
    {
        return false;
    }
    if (state)
    {
        b1.style.display='none';
        b2.style.display='';
    }
    else
    {
        b1.style.display='';
        b2.style.display='none';
    }
    return false;
}


var tipFields = new Array();
function enterTipField(field, password)
{
    if (!field) return;
    var value = field.value;
    var name = field.name;
    if ((!tipFields[name])|| (value==tipFields[name]))
    {
        tipFields[name] = value;
        field.value = '';
        if (password)
        {
            field.type = 'password';
        }
    }
    //myLog("Field: "+field+", value="+field.value);
}

function leaveTipField(field, password)
{
    if (!field) return;
    var value = field.value;
    var name = field.name;
    if (tipFields[name] && value=='')
    {
        field.value = tipFields[name];
        if (password)
        {
            field.type = 'text';
        }
    }
}

/**
 *  Set two div's height to their maximums.
 *  @param idlist is an array of IDs of the blocks
 */
function alignColumnsHeight( idlist )
{
    var hmax = 0;
    var olist = new Array();
    for (var i=0; i < idlist.length; i++)
    {
        var o = document.getElementById(idlist[i]);
        if (!o)
        {
            alert("No layer: "+idlist[i])
            continue;
        }
        olist[olist.length] = o;
        var h = o.offsetHeight;
        if (hmax < h)
        {
            hmax = h;
        }
    }
    for (var i=0; i<olist.length; i++)
    {
        olist[i].style.height = hmax+"px";
    }
}


if (!String.prototype.trim)
{
    String.prototype.trim = function()
    {
        var	str = this.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
    }
}


///  On load functions


var onloadTaskList = new Array();

onloadHover = function ()
{
    /* Add :hover support for IE 5 & 6*/
    if (document.getElementById && BrowserDetect.browser=='Explorer' && parseFloat(BrowserDetect.version)<7.0) {
        addHoverFunction( document.getElementById("pulldown") );
        addHoverFunction( document.getElementById("sidemenu") );
    }
}

onloadTaskList[onloadTaskList.length] = onloadHover;


window.onload=function()
{
    for(var i=0; i< onloadTaskList.length; i++)
    {
        onloadTaskList[i]();
    }
};


