/*
+--------------------------------------------------------------------------
|   Bug Tracker v1.3 for IPB v2.2.x and v2.3.x
|   =============================================
|   by Keith Rocheck
|   (c) 2005 - 2006 Simna Web Services
|   http://www.simnaweb.com
|   =============================================
|   Web: http://mods.invisionize.com/db/index.php/f/6995
+--------------------------------------------------------------------------
|
|   > JS XML Edit Script
|   > Script written by Keith Rocheck
|   > Date started: 31th December 2005
|   > Module version: 10001
|
+--------------------------------------------------------------------------
*/

//-----------------------------------------
// Some variables
//-----------------------------------------
var reply_cache = new Array()
var ajax_loaded = 1

/*-------------------------------------------------------------------------*/
// Resize the edit window
/*-------------------------------------------------------------------------*/
function ajax_std_window_resize( pix, rid )
{
    var box = document.getElementById( 'reply-edit-' + rid )
    var h1 = parseInt( box.style.height ) ? parseInt( box.style.height ) : 300
    var h2 = h1 + pix
    
    if( h2 > 0 )
    {
        box.style.height = h2 + "px"
    }
    
    return false
}

/*-------------------------------------------------------------------------*/
// Cancel XML edit
/*-------------------------------------------------------------------------*/
function ajax_cancel_for_edit( ci )
{
    if( reply_cache[ ci ] != "" )
    {
        document.getElementById( 'reply-' + ci ).innerHTML = reply_cache[ ci ]
    }
    return false
}

/*-------------------------------------------------------------------------*/
// Save XML edit
/*-------------------------------------------------------------------------*/
function ajax_save_for_edit( ei, ci, pi, event )
{
    var url = ipb_var_bugtracker_url + 'req=doxmledit&bid=' + ei + '&rid=' + ci + '&pid=' + pi;
    var fields = new Array();
    fields['md5check'] = ipb_md5_check;
    fields['bid'] = ei;
    fields['rid'] = ci;
    fields['pid'] = pi;
    fields['req'] = 'doxmledit';
    fields['Post'] = document.getElementById( 'reply-edit-' + ci ).value;
    fields['std_used'] = 1;
    var post_check=fields['Post'];
    if(post_check.replace( /^\s*|\s*$/g,"")=="")
    {
        alert(js_no_empty_post);
        return false;
    }
    do_request_function=function()
    {
        if(!xmlobj.readystate_ready_and_ok())
        {
            xmlobj.show_loading();
            return;
        }
        xmlobj.hide_loading();
        var html=xmlobj.xmlhandler.responseText;
        if(html=='nopermission')
        {
            alert(js_error_no_permission);
            document.getElementById( 'reply-' + ci ).innerHTML = reply_cache[ci];
        }
        else if( html != 'error' )
        {
            document.getElementById( 'reply-' + ci ).innerHTML = html;
            xmlobj.execute_javascript(html);
            fix_linked_image_sizes();
        }
    };
    
    xmlobj=new ajax_request();
    xmlobj.onreadystatechange(do_request_function);
    var xmlreturn=xmlobj.process(url,'POST',xmlobj.format_for_post(fields));
    reply_cache[ ci ] = '';
    return false;
}

/*-------------------------------------------------------------------------*/
// Prepare for an XML edit (a.k.a. change to the edit box)
/*-------------------------------------------------------------------------*/
function ajax_prep_for_edit( ei, ci, pi, event )
{
    global_cancel_bubble( event, true );
    var reply_main_obj = document.getElementById( 'reply-main-' + ci );
    var reply_box_top = _get_obj_toppos( reply_main_obj );
    var url = ipb_var_bugtracker_url + 'req=xmledit&bid=' + ei + '&rid=' + ci + '&pid=' + pi;
    
    reply_cache[ ci ] = document.getElementById( 'reply-' + ci ).innerHTML;
    
    try
    {
        menu_action_close();
    }
    catch(e){}
    
    do_request_function=function()
    {
        if(!xmlobj.readystate_ready_and_ok())
        {
            xmlobj.show_loading();
            return;
        }
        xmlobj.hide_loading();
        var html=xmlobj.xmlhandler.responseText;
        
        if (html=='nopermission')
        {
            alert(js_error_no_permission);
        }
        else if( html != 'error' )
        {
            if( reply_box_top )
            {
                scroll( 0, reply_box_top - 30 );
            }
            document.getElementById( 'reply-' + ci ).innerHTML = html;
            IPS_Lite_Editor[ci]=new ips_text_editor_lite(ci);
            IPS_Lite_Editor[ci].init();
        }
        else
        {
            alert( html )
        }
    };
    
    xmlobj=new ajax_request();
    xmlobj.onreadystatechange(do_request_function);
    xmlobj.process(url);
    return false;
}
