
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;
}


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();
    }
    else
    {
        expires = "";
    }
    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;
    }
    var yearv = year.value;
    var monthv = month.value;
    var dayv = day.value;
    if (yearv == 0)
    {
        year.value = '0000';
        month.value = '00';
        day.value = '00';
        return;
    }
    if (monthv == 0)
    {
        monthv = 1;
        month.value = '1';
    }
    if (dayv == 0)
    {
        dayv = 1;
        day.value = '1';
    }
    var date = new Date();
    date.setFullYear(yearv, monthv-1, 1);
    var dayofweek=date.getDay();
    var szoko = (yearv % 4 == 0 && (yearv % 100 != 0 || yearv % 400 == 0));
    var days = 30;
    if (monthv == 2)
    {
        if (szoko)
        {
            days = 29;
        }
        else
        {
            days = 28;
        }
    }
    else
    {
        if (monthv %2 == 1 && monthv <= 7)
        {
            days = 31;
        }
        else if (monthv % 2 == 0 && monthv >= 8)
        {
            days = 31;
        }
    }
    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>dayv)
    {
        day.value = dayv;
    }
    else
    {
        day.value=days;
    }
}


function select_innerHTML(objeto,innerHTML)
{
    /******
* select_innerHTML - corrige o bug do InnerHTML em selects no IE
* Veja o problema em: http://support.microsoft.com/default.aspx?scid=kb;en-us;276228
* Versão: 2.1 - 04/09/2007
* Autor: Micox - Náiron José C. Guimarães - micoxjcg@yahoo.com.br
* @objeto(tipo HTMLobject): o select a ser alterado
* @innerHTML(tipo string): o novo valor do innerHTML
*******/
    objeto.innerHTML = "";
    var selTemp = document.createElement("micoxselect");
    var opt;
    selTemp.id="micoxselect1";
    document.body.appendChild(selTemp);
    selTemp = document.getElementById("micoxselect1");
    selTemp.style.display="none";
    if(innerHTML.toLowerCase().indexOf("<option")<0)
    {//se não é option eu converto
        innerHTML = "<option>" + innerHTML + "</option>";
    }
    innerHTML = innerHTML.toLowerCase().replace(/<option/g,"<span").replace(/<\/option/g,"</span");
    selTemp.innerHTML = innerHTML;


    for(var i=0;i<selTemp.childNodes.length;i++)
    {
        var spantemp = selTemp.childNodes[i];

        if(spantemp.tagName)
        {
            opt = document.createElement("OPTION")

            if(document.all)
            { //IE
                objeto.add(opt);
            }
            else
            {
                objeto.appendChild(opt);
            }

            //getting attributes
            for(var j=0; j<spantemp.attributes.length ; j++)
            {
                var attrName = spantemp.attributes[j].nodeName;
                var attrVal = spantemp.attributes[j].nodeValue;
                if(attrVal)
                {
                    try
                    {
                        opt.setAttribute(attrName,attrVal);
                        opt.setAttributeNode(spantemp.attributes[j].cloneNode(true));
                    }
                    catch(e)
                    {
                    }
                }
            }
            //getting styles
            if(spantemp.style)
            {
                for(var y in spantemp.style)
                {
                    try
                    {
                        opt.style[y] = spantemp.style[y];
                    }
                    catch(e)
                    {}
                }
            }
            //value and text
            opt.value = spantemp.getAttribute("value");
            opt.text = spantemp.innerHTML;
            //IE
            opt.selected = spantemp.getAttribute('selected');
            opt.className = spantemp.className;
        }
    }
    document.body.removeChild(selTemp);
    selTemp = null;
}


/// form help tips

function show_help( path )
{
    var nw = window.open(path, "tooltip_window","width=300, height=300, scrollbars=yes, resizable=yes, location=no, toolbar=no, directories=no, status=no, menubar=no, copyhistory=no");
    nw.focus();
    return void(0);
}

function edit_help( path )
{
    var nw = window.open(path, "form_help_window","width=800 height=800, scrollbars=yes, resizable=yes, location=yes, toolbar=yes, directories=no, status=yes, menubar=yes, copyhistory=no");
    nw.focus();
    return void(0);
}

///  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]();
    }
};

