﻿function initAttachList(attachdata) {
    loadAttachItem(attachdata);

}
function loadAttachItem(filelist) {
    if (filelist != "") {
        var arAttach = filelist.split("||");
        var file;
        for (var i = 0; i < arAttach.length; i++) {
            file = arAttach[i];
            if (file != "") {
                var attachitem = new AttachItem();
                attachitem.setValue(file);
                addAttachItem(attachitem);
            }
        }
    }
}
function addAttachItem(attachitem) {
    
    if (attachitem.path == "") {
        divAttachList.innerHTML += "<span><span id=spAttachItem class=clAttachItem><nowrap>&nbsp;" + attachitem.name + "<img src=\"/aimages/buttons/idelete.gif\" onclick='delIndex(this," + attachitem.index + ")'/></nowrap></span>&nbsp;</span>";
    } else {
        var path = attachitem.path.replace(/\\/g, "\\\\");
        divAttachList.innerHTML += "<span><span id=spAttachItem class=clAttachItem><nowrap>&nbsp;" + attachitem.name + "<img src=\"/aimages/buttons/idelete.gif\" onclick=\"delPath(this,'" + path + "')\"/></nowrap></span>&nbsp;</span>";
    }
    
}

function delIndex(obj, index) {
    var filelist = objAttachData.value;
    var newlist = "";
    if (filelist != "") {
        var arAttach = filelist.split("||");
        var file;
        for (var i = 0; i < arAttach.length; i++) {
            file = arAttach[i];
            if (file != "") {
                var attachitem = new AttachItem();
                attachitem.setValue(file);
                if (attachitem.index != index) {
                    if (newlist == "") {
                        newlist = file;
                    } else {
                        newlist += "||" + file;
                    }

                }
            }
        }
    }
    objAttachData.value = newlist;
    obj.parentElement.parentElement.style.display = "none";
}
function delPath(obj, path) {
    var filelist = objAttachData.value;
    var newlist = "";
    if (filelist != "") {
        var arAttach = filelist.split("||");
        var file;
        for (var i = 0; i < arAttach.length; i++) {
            file = arAttach[i];
            if (file != "") {
                var attachitem = new AttachItem();
                attachitem.setValue(file);
                if (attachitem.path != path) {
                    if (newlist == "") {
                        newlist = file;
                    } else {
                        newlist += "||" + file;
                    }

                }
            }
        }
    }
    objAttachData.value = newlist;
    obj.parentElement.parentElement.style.display = "none";
}
