﻿

    function GetSelectedItemValue(FormName) {

        var TargetObj       = document.forms[FormName].AttachFileList;
        var SelectedIndex   = TargetObj.selectedIndex;

        if(SelectedIndex < 0) return "";

        return TargetObj[SelectedIndex].value;

    }





    function InsertFileList(FormName, ItemText, ItemValue, SubAction) {

        var TargetObj = document.forms[FormName].AttachFileList;
        var ItemValueForSearch, ItemLength, LoopCount;

        ItemValueForSearch = ItemValue.toUpperCase();
        ItemLength         = TargetObj.length;

        TargetObj[ItemLength] = new Option("", "", false, false);

        for (LoopCount = (ItemLength - 1); LoopCount >= 0; LoopCount--) {

            if (TargetObj[LoopCount].value.toUpperCase() < ItemValueForSearch) {
                TargetObj[LoopCount + 1].text     = ItemText;
                TargetObj[LoopCount + 1].value    = ItemValue;
                break;

            } else {
                TargetObj[LoopCount + 1].text  = TargetObj[LoopCount].text;
                TargetObj[LoopCount + 1].value = TargetObj[LoopCount].value;

            }

        }

        if (LoopCount < 0) {
            TargetObj[0].text     = ItemText;
            TargetObj[0].value    = ItemValue;

        }

    }





    function BindAttachFileList(FormName) {

        var TargetObj = document.forms[FormName];

        if((TargetObj.AttFileName).value == "") return;

        var FileName = (TargetObj.AttFileName).value.split("|");
        var FileSize = (TargetObj.AttFileSize).value.split("|");

        var LoopCount;


        if (FileName.length > 0) {
            for (LoopCount = 0; LoopCount < FileName.length; LoopCount++) {
                InsertFileList(FormName, (FileName[LoopCount] + " (" + FileSize[LoopCount] + "KB)"), FileName[LoopCount], false)
            }
        }

    }





    function CopyFilePath(FormName) {

        var FileName = GetSelectedItemValue(FormName);

        if(FileName == "") {
            alert("링크 주소를 복사할 파일을 선택하세요.");
            return;
        }

        var TargetObj   = document.forms[FormName];
        var FilePath    = (TargetObj.AttFilePath).value + FileName;

        window.clipboardData.setData("text", FilePath); 
        alert("파일의 링크 주소를 클립보드에 복사하였습니다.\n\n사용할 입력창에 붙여넣기(Ctrl+V) 하세요.");

    }





    function FileDownload(FormName, ActionTargetObj) {

        var FileName = GetSelectedItemValue(FormName);

        if(FileName == "") {
            alert("전송받을 파일을 선택하세요.");
            return;
        }

        var TargetObj   = document.forms[FormName];
        var ContentCode = (TargetObj.AttContentCode).value;
        var SerialNo    = (TargetObj.AttSerialNo).value;


        ActionTargetObj.location.href =   "/FileControl/FileDownload.aspx"
                                        + "?ContentCode=" + escape(ContentCode)
                                        + "&SerialNo="    + escape(SerialNo)
                                        + "&FileName="    + escape(FileName)

    }



