
/********************/
function _(text, id) {
    return text;
}

/*** alert.js ***/
function sessionping(callback, cont) {
    if (dexconfig['ping.interval'] != null && dexconfig['ping.interval'] > 0) {
        window.setInterval(function() {
            $.ajax({
                method: 'POST',
                url: dexconfig['page.ping.name'],
                data: "control=" + cont + "&ez=10",
                dataType: 'text',
                complete: function(transport) {
                    var ct = transport.getResponseHeader('Content-Type');
                    var res = transport.responseText;
                    if (ct.match(/application\/x-redirect/)) {
                        location.href = res;
                    }
                    else if (ct.match(/text\/html/)) {
                        if (res.indexOf('Dexter.fw.login') > -1)
                            location.href = dexconfig['page.login.name'];
                    }
                    else {
                        callback(res);
                    }
                },
                error: function(request, textStatus, errorThrown) {

                }
            });
        }, dexconfig['ping.interval'] * 1000, callback);
    }
}

var isDexalertOpened = false;
function dexalert(message, title, callback, cparams) {
    var ret = true;
    if (!isDexalertOpened) {
        isDexalertOpened = true;
        if (title == null)
            title = 'Hiba';

        el = $(document.body).createAppend(
        'div', { title: title }, message).hide();

        $(el).dialog({
            bgiframe: true,
            //resizable: false,
            //show: 'slideDown',
            modal: true,
            overlay: {
                backgroundColor: '#000',
                opacity: 0.5
            },
            close: function(event, ui) {
                isDexalertOpened = false;
            },
            buttons: {
                Ok: function() {
                    $(this).dialog('destroy');
                    el.remove();
                    if (typeof (callback) != 'undefined')
                        callback(cparams);
                    isDexalertOpened = false;
                }
            }
        });
    }
    else {
        ret = false;
    }

    return ret;
}

/*** ajaxaction.js ***/
function $1(id) {
    return document.getElementById(id);
}

function mySubmit( url, updateId, form, xtraparams )
{
    if( typeof(updateId) == 'undefined' || updateId == null)
        updateId = 'semmi';
    
    if( typeof(form) == 'undefined' || form == null)
        form = null;
        
    if( typeof(xtraparams) == 'undefined' || xtraparams == null)
        xtraparams = null;
        
    //dexsubmit(url, form, xtraparams, null, updateId, null, null);
//    alert(form);
//    alert(xtraparams);
//    
    dexsubmit(url,form, xtraparams, null, updateId);
    
}

function dexsubmit(action, form, xtraparams, handler, contentid, method, async) {
    //alert(action + ":" + form + ":" + xtraparams + ":" + heandler + ":" + contentid + ":" +method+ ":" +async );
    
    //alert(action + ":" + form );
    
    var ajaxWorking = true;
    var opleasewait = $1('ajaxbusy');

    window.setTimeout(function() {
        if (ajaxWorking) {
            $(opleasewait).show();
        }
    }, 500);

    if (typeof (method) == 'undefined')
        method = 'POST';

    if (typeof (async) == 'undefined')
        async = true;

    var params = [];
    if (form) {
        if (typeof (form) == 'string') {
            params = $('#' + form).serializeArray();
        }
        else if (jQuery.isArray(form)) {
            jQuery.each(form, function() {
                params = jQuery.merge(params, $('#' + this).serializeArray());
            });
        }
        else {
            params = form.serializeArray();
        }
    }

    if (xtraparams) {
        for (var k in xtraparams)
            params.push({ name: k, value: xtraparams[k] });
    }

    params.push({ name: 'X-Requested-With', value: 'XMLHttpRequest' });

    url = action;

    var erroroccured = false;

    $.ajax({
        async: async,
        type: method,
        url: url,
        data: params,
        cache: false,
        processData: true,
        dataType: 'text',
        complete: function(transport) {
            if (erroroccured)
                return;

            var ct = transport.getResponseHeader('Content-Type');
            var res = transport.responseText;

            if (ct.match(/text\/errormsg-data/)) {
                eval('res = ' + res + ';');
                if (handler)
                    handler(res);
                else
                    dexalert('unhandled_error: ' + res);
            }
            else if (ct.match(/text\/errormsg/)) {
                dexalert(res);
            }
            else if (ct.match(/text\/html/)) {
                if (res.indexOf('Dexter.fw.login') > -1)
                    location.href = dexconfig['page.login.name'];
                else {
                    if (contentid && typeof (contentid) == 'string') {
                        $(contentid ? '#' + contentid : '#the_content').html(res);
                    }
                    else if (contentid) {
                        $(contentid).html(res);
                    }
                    else if (handler) {
                        handler(res);
                    }
                }
            }
            else if (ct.match(/application\/json/)) {
                eval('res = ' + res + ';');
                if (handler)
                    handler(res);
                else
                    handlejson(res);
            }
            else if (ct.match(/text\/dexalert/)) {
                eval('res = ' + res + ';');
                dexalert(res.Message, res.Title);
            }
            else if (ct.match(/text\/refresh/)) {
                location.reload(true);
            }
            else if (ct.match(/text\/run/)) {
                var funcStr = "function() { " + res + "}";
                var func = eval('[' + funcStr + ']')[0];
                func();
            }

            else if (ct.match(/application\/x-htmlpart/)) {
                try {
                    //alert(ct);
                    //var re = /<htmlpart[^>]*id="([^">]+)"[^>]*>((?:(?!<\/htmlpart)[\s\S])*)<\/htmlpart>/mig;
                    var re = /<htmlpart[^>]*id="([^">]+)"[^\/>]*(?:\/>|>((?:(?!<\/htmlpart)[\s\S])*)<\/htmlpart>)/mig;
                    var ms;
                    while ((ms = re.exec(transport.responseText))) {
                        //alert(ms[0]);
                        //var o = $('#' + ms[1]);
                        var o = $1(ms[1]);
                        var h = ms[2];
                        if (typeof (h) == 'undefined')
                            h = '';

                        if (o) {
                            $(o).html(h);

                        } else
                            alert('htmlpart missing: ' + ms[1]);
                        /*
                        if (o) {
                        o.innerHTML = '<div style="display:none;">ie-script-workaround</div>' + h;
                        var scripts = o.getElementsByTagName("script");
                        for (var lv = 0; lv < scripts.length; lv++) {
                        eval(scripts[lv].text);
                        }
                        */
                    }
                } catch (e) { alert('ERR:' + e.message); }
            } else if (ct.match(/application\/x-redirect/)) {
                location.href = res;
            }

            ajaxWorking = false;
            if (opleasewait) {
                $(opleasewait).hide();
                //opleasewait.remove();
            }
        },
        error: function(request, textStatus, errorThrown) {
            erroroccured = true;
            alert('AJAX error\n' + request.responseText);
            ajaxWorking = false;
            if (opleasewait) {
                $(opleasewait).hide();
                //opleasewait.remove();
            }
        }
    });
    return;
}

function handlemsg(res) {
    var title = _('Válasz üzenet');
    if (res.MessageType == 'Message') {
        title = _('Hiba');
    }

    dexalert(res);
}

function handlejson(res) {
    dexalert(res);
}

jQuery.download = function(url, form, method) {
    var params = [];
    if (form) {
        if (typeof (form) == 'string') {
            params = $('#' + form).serializeArray();
        }
        else if (jQuery.isArray(form)) {
            jQuery.each(form, function() {
                params = jQuery.merge(params, $('#' + this).serializeArray());
            });
        }
        else {
            params = form.serializeArray();
        }
    }

    data = params;

    //url and data options required
    if (url && data) {
        //data can be string of parameters or array/object
        data = typeof data == 'string' ? data : jQuery.param(data);
        //split params into form inputs
        var inputs = '';
        jQuery.each(data.split('&'), function() {
            var pair = this.split('=');
            inputs += '<input type="hidden" name="' + pair[0] + '" value="' + pair[1] + '" />';
        });
        //send request
        jQuery('<form action="' + url + '" method="' + (method || 'post') + '">' + inputs + '</form>')
		.appendTo('body').submit().remove();
    };
};


function naviPrint(id) {

    var a = $('#' + id + ' .table-sorted-desc');
    var desc = true;
    if (typeof (a.attr("id")) == 'undefined') {
        a = $(' #' + id + ' .table-sorted-asc');
        desc = false;
    }
    var rend = a.attr("id");
    var urlpost = "?_act=" + id + "/print&rendezes=" + rend + "&desc=" + desc;
    //var urlpost = "?_act=" + id + "/print";

    var url = location.href.replace(/#.*$/, '');
    url = url + urlpost;
    //alert(url);

    window.open(url, "Nyomtatás", "width=640,height=480,direktories=no,menubar=no,toolbar=no,scrollbars=yes");
    //location.href = url;

}

function dexpopupaction(url, title, width, height) {
    var href = url;

    el = $(document.body).createAppend(
        'div', { title: title }, '').hide();

    if (typeof (height) == 'undefined' || height == -1)
        height = 500;
    else if (height == -2)
        height = 'auto';
    if (typeof (width) == 'undefined' || width == -1)
        width = 780;

    dexsubmit(href, null, null, function(a) {
        $(el).html(a);
        $(el).dialog({
            bgiframe: true,
            modal: true,
            //show: 'fade',
            height: height,
            width: width,
            overlay: {
                backgroundColor: '#000',
                opacity: 0.5
            },
            close: function(event, ui) {
                $(this).dialog('destroy');
                el.remove();
            }
        });
    }, null, 'GET', true);

}

function dexconfirm(title, message, callback) {
    el = $(document.body).createAppend(
        'div', { title: title }, message).hide();

    $(el).dialog({
        bgiframe: true,
        resizeable: false,
        //show: 'fade',
        resizable: false,
        height: 140,
        modal: true,
        overlay: {
            backgroundColor: '#000',
            opacity: 0.8
        },
        buttons: {
            'Nem': function() {
                $(this).dialog('close');
            },
            'Igen': function() {
                $(this).dialog('close');
                callback();
            }
        }
    });
}





