function $1(id) 
{
    return document.getElementById(id);
}

var isFwAlertOpened = false;
function fwAlert(message, title, callback, cparams) 
{
    var ret = true;
    if( !isFwAlertOpened ) 
    {
        isFwAlertOpened = 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) {
                isFwAlertOpened = false;
            },
            buttons: {
                Ok: function() {
                    $(this).dialog('destroy');
                    el.remove();
                    if (typeof (callback) != 'undefined')
                        callback(cparams);
                    isFwAlertOpened = false;
                }
            }
        });
    }
    else {
        ret = false;
    }

    return ret;
}

function fwConfirm(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();
            }
        }
    });
}

function fwPopupAction(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;

    fwSubmit(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 fwSubmit_2( 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;
        
    fwSubmit(url,form, xtraparams, null, updateId);
}
//Ajaxos framework javacsript része
function fwSubmit(url, form, xtraparams, handler, contentid, method, async) {
    
    var ajaxWorking = true;
    var opleasewait = $1('ajaxbusy');

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

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

    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
                    fwAlert('unhandled_error: ' + res);
            }
            else if (ct.match(/text\/errormsg/)) 
            {
                fwAlert(res);
            }
            else if (ct.match(/text\/html/)) 
            {
                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\/fwAlert/)) 
            {
                eval('res = ' + res + ';');
                fwAlert(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-redirect/)) 
            {
                location.href = res;
            }

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