var postHTMLCache = new Array();
var rid = 0;

function quick_edit_title( topicID, forumID )
{
    if( !xmlhttpreqEnabled )
    {
        alert( 'Not supported by your browser' );
        return;
    }
    
    var xmlReq = new XMLRequest;
        
    var titleContainer = document.getElementById( 'tpicl' + topicID );
    var titleLink = document.getElementById( 'tpicc' + topicID );
        
    if( postHTMLCache[ topicID ] == null || postHTMLCache[ topicID ] == undefined || postHTMLCache[ topicID ] == '' ) postHTMLCache[ topicID ] = titleContainer.innerHTML;
        
    rid++;
        
    titleContainer.innerHTML = ' \
        <form action="#" method="get" onsubmit="return false" name="ttf' + rid + '"> \
        <input type="hidden" name="t" value="' + topicID +'" /> \
        <input type="hidden" name="f" value="' + forumID +'" /> \
        <input type="text" name="TopicTitle" size="50" maxlength="50" style="width: 250px" value="' + titleLink.innerHTML + '" onkeypress="res=handleEnter(this,event);if(!res)quick_save_title(this);return res" /> \
        <input type="button" name="save" value="&raquo;" onclick="quick_save_title(this)" /> \
        <input type="button" name="cancel" value="X" onclick="cancel_title_edit(this)" /> \
        </form>';
    
    if( document.forms['ttf' + rid].TopicTitle.focus )
    {
        document.forms['ttf' + rid].TopicTitle.focus()
        document.forms['ttf' + rid].TopicTitle.select()
    }
}

function handleEnter (field, event) {
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
if (keyCode == 13) {
return false;
} 
else
return true;
}  

function cancel_title_edit( obj )
{
    topicID = obj.form.t.value
    
    var titleContainer = document.getElementById( 'tpicl' + topicID );
    
    titleContainer.innerHTML = postHTMLCache[ topicID ];
    
    postHTMLCache[ topicID ] = null;
}

function quick_save_title( obj )
{
    forumID = obj.form.f.value
    topicID = obj.form.t.value
    
    var xmlReq = new XMLRequest;
    
    XMLRequest.onSuccessfulData = function()
    {
        var newTitle = XMLRequest.thread.responseText;
        
        var titleContainer = document.getElementById( 'tpicl' + topicID );
        titleContainer.innerHTML = postHTMLCache[ topicID ];
        postHTMLCache[ topicID ] = null;
        
        var titleLink = document.getElementById( 'tpicc' + parseInt( topicID ) );
        titleLink.innerHTML = newTitle;
    }
    XMLRequest.onFail = function()
    {
        alert( 'An error has occurred saving the topic title' );
    }
    XMLRequest.onError = function( message )
    {
        alert( message );
    }
    
    obj.form.save.disabled = true;
    obj.form.cancel.disabled = true;
    
    var data = new Array();
    data['TopicTitle'] = obj.form.TopicTitle.value;
    data['f'] = obj.form.f.value;
    data['t'] = obj.form.t.value;
    
    xmlReq.loadXML( 'index.php?act=XMLModCP&CODE=edittopictitle&f=' + forumID + '&t=' + topicID, 'POST', data );
}