
var rteInited = false;

function lax(vars, ids, options) {

    async = 0;

    if (typeof ids != "object" || ids.length == 0  ) {
        ids = {
            0 : 'main-content'
        };
    }

    if (typeof options != "object") {
        options = {};
    }

    if (typeof vars != "object") {
        vars = {};
    }

    if (options.confirm) {

        if (!confirm(options.confirm)) {
            return false;
        }
    }

    if (!options.leaveRTE) {

        if (typeof tinyMCE == "object") {
            var t = tinyMCE.editors, e;

            for (e in t) {
                if (t[e].remove) {
                    t[e].remove();
                }
            }
        }
    }

    var url = '?';

    for(var key in vars) {
        url += '&' + key + '=' + vars[key];
    }

    for(var i in ids) {

        var str_url =  encodeURI(url+'&ajax='+ids[i]);
        str_url = str_url.replace(/\'/g,'\\\'');

        $('#'+ids[i]).load(str_url);
    }

    rteInited = false;
    return true;
}

function pax(form, vars, ids, options) {

    if (typeof ids != "object") {
        return false;
    }

    if (typeof options != "object") {
        options = {};
    }

    if (typeof vars != "object") {
        vars = {};
    }

    if (options.confirm) {
        if (!confirm(options.confirm)) {
            return false;
        }
    }

    if (!options.leaveRTE) {

        if (typeof tinyMCE == "object") {
            var t = tinyMCE.editors, e;

            for (e in t) {
                if (t[e].remove) {
                    t[e].remove();
                }
            }
        }
    }
    rteInited = 1;

    // check for files
    post_target = '';

    $('#form :input').each(function(id, element) {

        if (element.type != "file") {
            return;
        }

        if ($('#bd_fileupload')) {
            $('#bd_fileupload').remove();
        }

        var iframe = $('<iframe>');
        iframe.attr({id:'bd_fileupload', name:'bd_fileupload'});
        iframe.css({display:'none'});

        iframe.bind('load', function() {

            var output = $('#bd_fileupload').contents().find("body").html();
            $('#'+ids[0]).html(output);
        });

        $('#app-frame').append(iframe);

        post_target = "bd_fileupload";

    });


    var url = '?';

    for(var key in vars) {
        url += '&' + key + '=' + vars[key];
    }

    for(var i in ids) {

        if (post_target) {

            var str_url =  encodeURI(url+'&ajax='+ids[i]);
            document.forms[form].action = str_url;
            document.forms[form].target = post_target;
            document.forms[form].submit();


        } else {

            var str_url =  encodeURI(url+'&ajax='+ids[i]);
            document.forms[form].action = str_url;

            $.post(str_url, $('#'+form).serialize(), function(data){
                $('#'+ids[i]).html(data);
            });
        }
    }

    rteInited = false;
    return true;
}


function initRTE () {
    if (rteInited) return false;
    tinyMCE.init({
        mode : "specific_textareas",
        editor_selector : "mceEditor",
        theme : "advanced",
        theme_advanced_buttons1 : "bold,italic,underline,separator,strikethrough,bullist,numlist,undo,redo,link,unlink,seperator,cut,copy,paste,pastetext,pasteword,removeformat,code,cleanup",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        //theme_advanced_resizing : true,
        plugins : "table,contextmenu,paste",
        extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style],p,img[!src|border:0|alt|title|width|height|style]a[name|href|target|title|onclick]",
        /*invalid_elements : "div",*/
        cleanup_on_startup : false,
        entity_encoding : "raw"
    });
    rteInited = true;
}


