var attachmentCurrentElement = null;
var attachmentAjax = new spwChat( "ajaxrpc.php", '' );

function attachmentDrag( el ) {
    if( el.id == "noAttachment" && attachmentCurrentElement != null ) {
        attachmentCurrentElement.style.border = "none";
        attachmentCurrentElement = null; 
        document.getElementById('uploadfileHelp').innerHTML = '';
        return;
    }
    if( el.id == "noAttachment" ) {
        return;
    }
    if( attachmentCurrentElement != null ) {
        attachmentCurrentElement.style.border = "none";
    }
    attachmentCurrentElement = el;
    attachmentCurrentElement.style.border = "2px solid red";
    document.getElementById('uploadfileHelp').innerHTML = 'Auf ein Textfeld klicken um die Datei dort hinzulegen.'
}

function attachmentDrop( el ) {
    if( attachmentCurrentElement != null ) {
        el2 = document.getElementById("in_"+el.id);
        el2.value += attachmentCurrentElement.title;
        attachmentCurrentElement.style.border = "none";
        attachmentCurrentElement = null;
        document.getElementById('uploadfileHelp').innerHTML = '';
        attachmentExecuteTextfield( el2, el.id+"show" );
    }
}

function attachmentExecuteAllFields( ) {
   attachmentExecuteTextfield( document.getElementById("in_text2"), "text2show" );
   attachmentExecuteTextfield( document.getElementById("in_text4"), "text4show" );
   attachmentExecuteTextfield( document.getElementById("in_text6"), "text6show" );
   attachmentExecuteTextfield( document.getElementById("in_text8"), "text8show" );
}

function attachmentExecuteTextfield( el, out ) {
    if( el.value != "" ) {
        try {
            document.getElementById(out).innerHTML = "";
            var matchRule = /\[localimg\](\d+)\[\/localimg\]/g;
            //alert( el.value );
            var localattachments;
            while( localattachments = matchRule.exec( el.value ) ) {
                if( localattachments ) {
                    document.getElementById(out).innerHTML += '<a href="javascript:attachmentRemoveFromTextfield(\''+el.id.substr(3)+'\', \''+RegExp.$1+'\');"><img src="attachment.php?att='+RegExp.$1+'&amp;type=img&amp;resize=120" /></a>'; 
    //                alert( "Test 2: Fund " + i + " - "+localattachments[i]+" - " + RegExp.$1+" - " + RegExp.$2);
                }
            }
            var matchRule = /\[localattachment\](\d+)\[\/localattachment\]/g;
            //alert( el.value );
            var localattachments;
            while( localattachments = matchRule.exec( el.value ) ) {
                if( localattachments ) {
                    document.getElementById(out).innerHTML += '<div onclick="javascript:attachmentRemoveFromTextfield(\''+el.id.substr(3)+'\', \''+RegExp.$1+'\');" class="localattachment">Attachment Nr.<br />'+RegExp.$1+'</div>'; 
    //                alert( "Test 2: Fund " + i + " - "+localattachments[i]+" - " + RegExp.$1+" - " + RegExp.$2);
                }
            }
        } catch(e) {}
    }
}

function attachmentRemoveFromTextfield( elid, id ) {
    el2 = document.getElementById( "in_"+elid );
    out = elid+"show";
    if( el2.value != "" ) {
        document.getElementById(out).innerHTML = "";
        var matchRule = /\[([^\]]+)\](\d+)\[\/[^\]]+\]/g;
        var localattachments;
        var newList = "";
        while( localattachments = matchRule.exec( el2.value ) ) {
            if( localattachments ) {
                if( RegExp.$2 != id ) {
                    newList += "["+RegExp.$1+"]"+RegExp.$2+"[/"+RegExp.$1+"]";
                } else {
                    if( RegExp.$1 == "localimg" ) {
                        document.getElementById( "newAttachments" ).innerHTML += '<img id="imgM'+RegExp.$2+'" title="[localimg]'+RegExp.$2+'[/localimg]" onclick="attachmentDrag(this);" src="attachment.php?att='+RegExp.$2+'&amp;type=img&amp;resize=120" />';
                    } else 
                    if( RegExp.$1 == "localattachment" ) {
                        document.getElementById( "newAttachments" ).innerHTML += '<div id="imgM'+RegExp.$2+'" title="[localattachment]'+RegExp.$2+'[/localattachment]" onclick="attachmentDrag(this);" class="localattachmentedit">Attachment Nr. '+RegExp.$2+'</div>';
                    }         
                }
            }
        }
        el2.value = newList;
        attachmentExecuteTextfield( el2, out );
    }
}

function attachmentRemove( subid, el, att ) {
    var in_el = document.getElementById( "in_"+el );
    var out = el+"show";
    if( in_el.value != "" ) {
        var matchRule = /\[([^\]]+)\](\d+)\[\/[^\]]+\]/g;
        var localattachments;
        var newList = "";
        while( localattachments = matchRule.exec( in_el.value ) ) {
            if( localattachments ) {
                if( RegExp.$2 != att ) {
                    newList += "["+RegExp.$1+"]"+RegExp.$2+"[/"+RegExp.$1+"]";
                } else {
                }
            }
        }
        in_el.value = newList;
        artikelSaveByAjax( subid, el );
    }
    attachmentDelete( att );
}

function attachmentDelete( attid ) {
    attachmentAjax.spw_chat_addRequest( "?sid="+sid+"&t=attachments&a=ajax&do=delete&id="+attid, 
                         attachmentDeleteResponse,
                         "" 
                       );
    attachmentAjax.spw_chat_sndReq();
}

function attachmentDeleteResponse() {
}
