//alert(parseInt('09'));

var date_format = '%Y.%m.%d';
var cal_obj2 = null;
var cal_input = null;


function intVal(value)
{
    value = value.toString();
    if( value.charAt(0) == '0' )
        value = value.substr(1);
    value = parseInt(value);
    return value;
}

function reloadCalendar(date_from,date_to)
{
    var year = listGetSelectedValue( getEbyID('selCalendarYear') );
    var month = listGetSelectedValue( getEbyID('selCalendarMonth') );

    var axID = "axCalendar";
    if( (year) &&(month) )
        Ajax.call(axID,"index.php?area=ax:calendar",null,Ajax.OP_SETHTML,{nodeId: "bkCalendar", post: {"year":year,"month":month,"date_from":date_from,"date_to":date_to}});
}


function getDateSelectValue(yearSelID, monthSelID, daySelID)
{
    var selYear = getEbyID(yearSelID);
    var selMonth = getEbyID(monthSelID);
    var selDay = getEbyID(daySelID);

    if( (selYear) && (selMonth) && (selDay) )
    {
        var year = listGetSelectedValue(selYear);
        var month = listGetSelectedValue(selMonth);
        var day = listGetSelectedValue(selDay);

        if( (year) && (month) && (day) )
            return year + "-" + month + "-" + day;
    }

    return false;
}

function setEventFilter(key,value)
{
    var form = document.forms['frmEventFilter'];
    var mode = form.mode.value;

    switch( key )
    {
        case 'date' :
            if( value.indexOf(':') != -1)
            {
                tmp = value.split(':');
                form.date_from.value = tmp[0];
                form.date_to.value = tmp[1];
            }
            else
                form.date_from.value = form.date_to.value = value;
        break;
        case 'id_performer' :
            if( value == "" && mode == "quick")
            {
                alert(stringtable.ml76);
                return false;
            };
        break;
        case 'id_location' :
            if( value == "" && mode == "quick" )
            {
                alert(stringtable.ml77);
                return false;
            };
        break;
        case 'id_pass' :
            if( value == "" && mode == "quick" )
            {
                alert(stringtable.ml78);
                return false;
            };
        break;
        case 'event_date_from'  :
            date_from = getDateSelectValue('selEventDateFromYear','selEventDateFromMonth','selEventDateFromDay');
            if( date_from )
            {
                if( (form.max_date.value) && (date_from > form.max_date.value) )
                {
                    alert(stringtable.ml79);
                    return false;
                }
                form.date_from.value = date_from;
            }
            else
                return false;
        break;
        case 'event_date_to'  :
            date_to = getDateSelectValue('selEventDateToYear','selEventDateToMonth','selEventDateToDay');
            if( date_to  )
            {
                if( (form.min_date.value) && (date_to < form.min_date.value) )
                {
                    alert(stringtable.ml79);
                    return false;
                }

                form.date_to.value = date_to ;
            }
            else
                return false;
        break;
    }

    if( mode == "quick" )
    {
        if( form.id_performer )
            if( key != "id_performer" ) listSelectByValue(form.id_performer,"");
        if( form.id_performer )
            if( key != "id_location" ) listSelectByValue(form.id_location,"");
        if( form.id_pass )
            if( key != "id_pass" ) listSelectByValue(form.id_pass,"");
        if( form.fulltext )
            if( key != "fulltext" ) form.fulltext.value = "";
        if( (form.date_from) && (form.date_to) )
            if( key != "date" ) form.date_from.value = form.date_to.value = "";

        if( form.fulltext.value.length > 0 && form.fulltext.value.length < 3 )
        {
            alert(stringtable.ml80);
            return false;
        };

        //if( (form.event_date_from) && (form.event_date_to) )
        //    if( (key != "event_date_from") && (key != "event_date_to") )
        //        form.event_date_from.value = form.event_date_to.value = "";

        form.submit();
    }

    if( mode == "detail" )
    {
        if( form.fulltext.value == "keresett kifejezés(ek) (vesszővel elválasztva)")
            form.fulltext.value = "";

        if( key == "submit" )
        {
            if( form.fulltext.value.length > 0 && form.fulltext.value.length < 3 )
            {
                alert(stringtable.ml80);
                return false;
            };

            var date_from = form.date_from.value;
            var date_to = form.date_to.value;
            var min_date = form.min_date.value;
            var max_date = form.max_date.value;

            if(((date_from) && (!checkDate(form.date_from, null, 'yyyy.mm.dd'))) || ((date_to) && (!checkDate(form.date_to, null, 'yyyy.mm.dd'))))
                alert(stringtable.ml79);
            else
                if( (date_from) && (date_to) && (date_to < date_from) )
                    alert(stringtable.ml79);
                else
                    if((date_from) && (min_date) && (date_from < min_date))
                        alert(stringtable.ml81.replace(/%1/, min_date.replace(/-/g, '.')));
                    else
                        if((date_to) && (max_date) &&  (date_to > max_date))
                            alert(stringtable.ml82.replace(/%1/, max_date.replace(/-/g, '.')));
                        else
                            form.submit();
        }
    }

    if( mode == "archive" )
    {
        if( key == "submit" || key == "date" )
        {
            if( form.fulltext.value.length > 0 && form.fulltext.value.length < 3 )
            {
                alert(stringtable.ml80);
                return false;
            };

            form.submit();
        }
    }




    //if( mode == "quick" || mode == "archive" )
    //{
    //    switch( key )
    //    {
    //        case 'date'             :   form.date.value = value;
    //                                    var selYear = getEbyID('selEventDateFromYear');
    //                                    var selMonth = getEbyID('selEventDateFromMonth');
    //                                    var selDay = getEbyID('selEventDateFromDay');
    //                                    if( (selYear) && (selMonth) && (selDay) )
    //                                    {
    //                                        listSelectByValue(selYear,value.substr(0, 4));
    //                                        listSelectByValue(selMonth,value.substr(5, 2));
    //                                        listSelectByValue(selDay,value.substr(8, 2));
    //                                    }
    //                                    var selYear = getEbyID('selEventDateToYear');
    //                                    var selMonth = getEbyID('selEventDateToMonth');
    //                                    var selDay = getEbyID('selEventDateToDay');
    //                                    if( (selYear) && (selMonth) && (selDay) )
    //                                    {
    //                                        listSelectByValue(selYear,value.substr(0, 4));
    //                                        listSelectByValue(selMonth,value.substr(5, 2));
    //                                        listSelectByValue(selDay,value.substr(8, 2));
    //                                    }
    //                                    break;
    //        case 'event_date_from'  :   date_from = getDateSelectValue('selEventDateFromYear','selEventDateFromMonth','selEventDateFromDay');
    //                                    if( date_from )
    //                                    {
    //                                        if( (form.event_date_to.value) && (date_from > form.event_date_to.value) )
    //                                        {
    //                                            alert("Hibás dátum választás!");
    //                                            return false;
    //                                        }
    //                                        form.event_date_from.value = date_from;
    //                                    }
    //                                    else
    //                                        return false;
    //                                    break;
    //        case 'event_date_to'  :     date_to = getDateSelectValue('selEventDateToYear','selEventDateToMonth','selEventDateToDay');
    //                                    if( date_to  )
    //                                    {
    //                                        if( (form.event_date_from.value) && (date_to < form.event_date_from.value) )
    //                                        {
    //                                            alert("Hibás dátum választás!");
    //                                            return false;
    //                                        }
    //                                        form.event_date_to.value = date_to ;
    //                                    }
    //                                    else
    //                                        return false;
    //                                    break;
    //    }
    //
    //

}

function flipInfTable(postfix)
{
    var nodeLink = getEbyID('linkFlipArrow_' + postfix);
    var nodeContent = getEbyID('bkFlipContent_' + postfix);

    if( isVisible(nodeContent) )
    {
        showBlock(nodeContent,false);
        nodeLink.innerHTML = "&gt;&gt;";
    }
    else
    {
        showBlock(nodeContent,true);
        nodeLink.innerHTML = "&lt;&lt;";
    }
}

function refreshMonths(monthSelID,yearSelID,date_from,date_to,selMonthValue)
{
    var selMonth = getEbyID(monthSelID);
    var selYear = getEbyID(yearSelID);
    var selDay = getEbyID( monthSelID.replace('Month','Day') );
    var orig_date_from = date_from;
    var orig_date_to = date_to;

    if( (selMonth) && (selYear) )
    {
        var year = listGetSelectedValue(selYear);
        date_from = date_from.substring(0,7);
        date_to = date_to.substring(0,7);

        listDelOption(selMonth,false);
        listAddOption(selMonth,"","-");
        if(selDay)
        {
            listDelOption(selDay,false);
            listAddOption(selDay,"","-");
        }

        for(var i=0; i < listMonthName.length;i++)
        {
            var date = year + '-' + listMonthName[i][0];

            switch( true )
            {
                case ((date_from != "") && (date_to != "")) :   if( (date >= date_from) && (date <= date_to) )
                                                                    listAddOption(selMonth,listMonthName[i][0],listMonthName[i][1]);
                                                                break;
                case (date_from != "")                      :   if( date >= date_from )
                                                                    listAddOption(selMonth,listMonthName[i][0],listMonthName[i][1]);
                                                                break;
                case (date_to != "")                        :   if( date <= date_to )
                                                                    listAddOption(selMonth,listMonthName[i][0],listMonthName[i][1]);
                                                                break;
            }
        }

        if( selMonthValue != undefined )
            listSelectByValue(selMonth,selMonthValue);

        if( (year) && (selMonthValue) && (selDay) )
        {
            date_from = orig_date_from != "" ? orig_date_from.substring(0,10) : "";
            date_to = orig_date_to != "" ? orig_date_to.substring(0,10) : "";

            days = daysInMonth(parseInt(selMonthValue, 10), parseInt(year, 10));
            for(var i=1;i <= days;i++)
            {
                day = i < 10 ? ("0" + i) : i;
                var date = year + '-' + selMonthValue + '-' + day;
                switch( true )
                {
                    case ((date_from != "") && (date_to != "")) :   if( (date >= date_from) && (date <= date_to) )
                                                                        listAddOption(selDay,day,day);
                                                                    break;
                    case (date_from != "")                      :   if( date >= date_from )
                                                                        listAddOption(selDay,day,day);
                                                                    break;
                    case (date_to != "")                        :   if( date <= date_to )
                                                                        listAddOption(selDay,day,day);
                                                                    break;
                }
            }
        }
    }
}

function goPrevCalendarMonth(monthSelID,yearSelID,date_from,date_to,autoshow)
{
    if( autoshow == undefined )
        autoshow = true;
    var selYear = getEbyID(yearSelID);
    var selMonth = getEbyID(monthSelID);
    if( (selMonth) && (selYear) )
    {
        var year = intVal(selYear.options[selYear.selectedIndex].value);
        var month = intVal(selMonth.options[selMonth.selectedIndex].value);
        month -= 1;
        if( month == 0 )
        {
            month = 12;
            year -= 1;
        }
        if( month < 10 )
            month = '0' + month;

        tmp = year + "-" + month;
        if( (!date_from || tmp >= date_from.substr(0,7)) && listInList(selYear,false,year) )
        {
            listSelectByValue(selYear,year);
            refreshMonths(monthSelID,yearSelID,date_from,date_to);
            if( listInList(selMonth,false,month) )
                listSelectByValue(selMonth,month);
            reloadCalendar(date_from,date_to);
            if( autoshow )
                showMonthFirstDay(monthSelID,yearSelID);
        }
    }
}

function goNextCalendarMonth(monthSelID,yearSelID,date_from,date_to,autoshow)
{
    if( autoshow == undefined )
        autoshow = true;
    var selYear = getEbyID(yearSelID);
    var selMonth = getEbyID(monthSelID);
    if( (selMonth) && (selYear) )
    {
        var year = intVal(selYear.options[selYear.selectedIndex].value);
        var month = intVal(selMonth.options[selMonth.selectedIndex].value);
        month += 1;
        if( month == 13 )
        {
            month = 1;
            year += 1;
        }
        if( month < 10 )
            month = '0' + month;

        tmp = year + "-" + month;
        if( (!date_to || tmp <= date_to.substr(0,7)) && listInList(selYear,false,year) )
        {
            listSelectByValue(selYear,year);
            refreshMonths(monthSelID,yearSelID,date_from,date_to);
            if( listInList(selMonth,false,month) )
                listSelectByValue(selMonth,month);
            reloadCalendar(date_from,date_to);
            if( autoshow )
                showMonthFirstDay(monthSelID,yearSelID);
        }
    }

}

function showMonthFirstDay(monthSelID,yearSelID)
{
//
    var selYear = getEbyID(yearSelID);
    var selMonth = getEbyID(monthSelID);
    if( (selMonth) && (selYear) )
    {
        var year = listGetSelectedValue( selYear );
        var month = listGetSelectedValue( selMonth );
        var date = year + "-" + month + "-01";
        setEventFilter('date',date);
    }
}

function daysInMonth(month,year)
{
    var m = [31,28,31,30,31,30,31,31,30,31,30,31];
    if (month != 2) return m[month - 1];
    if (year%4 != 0) return m[1];
    if (year%100 == 0 && year%400 != 0) return m[1];
    return m[1] + 1;
}

function showMonthTotal(monthSelID,yearSelID)
{
    var selYear = getEbyID(yearSelID);
    var selMonth = getEbyID(monthSelID);
    if( (selMonth) && (selYear) )
    {
        var year = listGetSelectedValue( selYear );
        var month = listGetSelectedValue( selMonth );

        var days = daysInMonth( parseInt(month, 10), parseInt(year, 10));
        var date = year + "-" + month + "-01" + ":" + year + "-" + month + "-" + days;
        setEventFilter('date',date);
    }
}


function postRegOrgan()
{
    var form = document.forms['frmRegOrgan'];

    if( checkInput(form.company,CHK_LENGTH,"A cégnév megadása kötelező!",3) )
        if( checkInput(form.tel,CHK_TEXT,"A telefonszám megadása kötelező!") )
        {
            if( form.fax.value == "" && form.email.value == "" )
            {
                alert("Fax vagy e-mail cím megadása kötelező!");
                return false;
            }

            if( form.fax.value != "" )
                if(! checkInput(form.fax,CHK_TEXT,"A faxszám megadása kötelező!") )
                    return false;

            if( form.email.value != "" )
                if(! checkInput(form.email,CHK_EMAIL,"Az e-mail cím megadása kötelező!") )
                    return false;

            if( checkInput(form.username,CHK_LENGTH,"A bejelentkezési név megadása kötelező!",3) )
                if( checkInput(form.password,CHK_LENGTH,"A jelszó megadása kötelező!",3) )
                    if( checkPassword(form.password,form.passwordchk,"") )
                        return true;
        }

    return false;
}

function writeRightMenu(postfix)
{
    document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="95" height="638" id="menu" align="middle">');
    document.write('<param name="allowScriptAccess" value="sameDomain" />');
    document.write('<param name="movie" value="flash/menu.swf?menuprovider=xmls/menuitems_'+postfix+'.xml&amp;textsize=10" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="flash/menu.swf?menuprovider=xmls/menuitems_'+postfix+'.xml&textsize=10" quality="high" bgcolor="#ffffff" width="95" height="638" name="menu" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
    document.write('</object>');
}

function writeSplash()
{
    document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="100%" height="100%" id="splash" align="middle">');
    document.write('<param name="allowScriptAccess" value="sameDomain" />');
    document.write('<param name="movie" value="flash/splash.swf?splashprovider=xmls/splash.xml" /><param name="quality" value="high" /><param name="scale" value="noscale" /><param name="bgcolor" value="#ffffff" /><embed src="flash/splash.swf?splashprovider=xmls/splash.xml" quality="high" scale="noscale" bgcolor="#ffffff" width="100%" height="100%" name="splash" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
    document.write('</object>');
}

function writeSplashNoFlash()
{
    document.write('<div style="width:100%;height:100%;background: #fff url(\'images/bg.splash.gif\') repeat-x fixed center center;"><div style="width:200px;height:500px;margin:0 auto;position:relative;top:50%;margin-top:-200px;"><a href="muzsika/"><img src="images/pic.mu.gif" alt="" style="display:block;margin-bottom:150px;border:none;" /></a><a href="koncertkalendarium/"><img src="images/pic.kk.gif" alt="" style="display:block;margin-top:250px;border:none;" /></a></div></div>');

}

function postSearchIssue()
{
    var form = document.forms['frmSearchIssue'];

    if( checkSelect(form.year,"Kérem válassza ki az évet!","") )
        return true;

    return false;
}

function postSearchLetter()
{
    var form = document.forms['frmSearchLetter'];

    if( checkSelect(form.author,null,"") || checkSelect(form.title,null,"") || checkSelect(form.subject,null,"") )
    {
        if( checkSelect(form.month_from,null,"") && (!checkSelect(form.year_from,null,"")) )
        {
            alert('Kérem válassza ki ki az évet!');
            return false;
        }

        if( checkSelect(form.month_to,null,"") && (!checkSelect(form.year_to,null,"")) )
        {
            alert('Kérem válassza ki ki az évet!');
            return false;
        }

        var date_from = '';
        var date_to = '';

        date_from += checkSelect(form.year_from,null,"") ? form.year_from.options[form.year_from.selectedIndex].value : '0000';
        date_from += checkSelect(form.month_from,null,"") ? form.month_from.options[form.month_from.selectedIndex].value : '00';

        date_to += checkSelect(form.year_to,null,"") ? form.year_to.options[form.year_to.selectedIndex].value : '9999';
        date_to += checkSelect(form.month_to,null,"") ? form.month_to.options[form.month_to.selectedIndex].value : '12';

        if( date_to < date_from )
        {
            alert('Kérem helyesen adja meg a dátumok sorrendjét!');
            return false;
        }

        return true;
    }
    else
        alert('Válasszon kezdőbetűt!');

    return false;
}

function onChangeSearchLetter(sel)
{
    var form = document.forms['frmSearchLetter'];

    if( sel.name != form.elements['author'].name)
        form.author.selectedIndex = 0;
    if( sel.name != form.elements['title'].name)
        form.title.selectedIndex = 0;
    if( sel.name != form.elements['subject'].name)
        form.subject.selectedIndex = 0;
}

function postSearchFT()
{
    var form = document.forms['frmSearchFullText'];

    if( (!checkInput(form.fulltextsearch,CHK_LENGTH,null,"3")) && (!checkSelect(form.year,null,"")) && (!checkSelect(form.month,null,""))  )
    {
        alert("Kérem adjon meg legalább 3 karaktert vagy válasszon dátumot!");
        return false;
    }

    return true;
}

function filterSearchResultBySource(source)
{
    window.location = 'index.php?area=searchres&source=' + source;
}

function orderSearchResult(sel)
{
    var orderby = sel.options[sel.selectedIndex].value;
    window.location = 'index.php?area=searchres&orderby=' + orderby;
}

function updateSearchFTStates(chk)
{
    var form = document.forms['frmSearchFullText'];

    if( chk.name == "all" )
    {
        if( form.all.checked )
        {
            form.title.checked = false;
            form.author.checked = false;
            form.article.checked = false;
            form.reper.checked = false;
            form.colname.checked = false;
        }
    }
    else
    {
        if( form.title.checked || form.author.checked || form.article.checked || form.reper.checked || form.colname.checked )
            form.all.checked = false;
    }
}

function posImprintBox()
{
    var nodeImprint = getEbyID('nodeImprint');
    var nodeImprintPosH = getEbyID('nodeImprintPosH');
    nodeImprint.style.top = (getScrollHeight() - getOffsetHeight(nodeImprint)) + "px";
}

function postNLetterPref()
{
    var form = document.forms['frmNLetterPref'];

    form.lids_location.value = list_getvalue(form.list_location,false,";");
    form.lids_pass.value = list_getvalue(form.list_pass,false,";");
    form.lids_getter.value = list_getvalue(form.list_getter,false,";");
    form.lids_performer.value = list_getvalue(form.list_performer,false,";");

    if((form.lids_location.value) ||
       (form.lids_pass.value) ||
       (form.lids_getter.value) ||
       (form.lids_performer.value) ||
       (form.date_from.value) ||
       (form.date_to.value) )
    {
        return true;
    }


    alert("Válasszon ki legalább egy keresési feltételt!");

    return false;
}

function delNLetterPref()
{
    if( confirm("Biztos benne ?") )
        window.location = "update.php?action=newsletter:delpref";
}

function addNLetterPrefItem(listid)
{
    var form = document.forms['frmNLetterPref'];
    var list_src = form.elements['registered_' + listid];
    var list_dest = form.elements['list_' + listid];

    if( (form) && (list_src) && (list_dest) )
    {
        var id_entity = list_src.options[list_src.selectedIndex].value;
        var title = list_src.options[list_src.selectedIndex].text;

        if( !list_inlist(list_dest,false,id_entity) )
        {
            list_sortaddtext(list_dest, id_entity, title);
            list_selectSinglevalue(list_dest, id_entity);
            list_selectSinglevalue(list_src, "");
        }
    }
}

function removeNLetterPrefItem(listid)
{
    var form = document.forms['frmNLetterPref'];
    var list_src = form.elements['registered_' + listid];
    var list_dest = form.elements['list_' + listid];

    if( (form) && (list_src) && (list_dest) )
    {
        list_del(list_dest,true);
    }
}


function pickDate(frmName,inpName)
{
    var form = document.forms[frmName];
    var text_field = form.elements[inpName];

	cal_obj2 = new RichCalendar();
	cal_obj2.start_week_day = 1;
	cal_obj2.show_time = false;
	cal_obj2.user_onchange_handler = cal2_on_change;

	cal_obj2.parse_date(text_field.value,date_format );
	cal_obj2.show_at_element(text_field, "adj_right-top");

    cal_input = text_field;

}

// user defined onchange handler
function cal2_on_change(cal, object_code)
{
	if (object_code == 'day')
    {
		cal_input.value = cal.get_formatted_date(date_format);
		cal.hide();
		cal_obj2 = null;
        cal_input = null;
	}
}

