<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.workflow.Plan"
%><%@page import="com.fujitsu.iflow.model.workflow.ProcessInstantiator"
%><%@page import="com.fujitsu.iflow.model.workflow.WorkItem"
%><%@page import="java.util.Enumeration"
%><%
    request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1
    BPMSession bps = (BPMSession) session.getAttribute("BPMSession");
    if (bps == null) {
        %><jsp:include page="PasswordPanel.jsp" flush="true"/><%
        return;
    }
%>
<html>
<head><title>Interstage Business Process Manager | Adding an attachment</title></head>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>
<script>
    function backClicked(isWorkitem)
    {
        if (isWorkitem)
            location.href="WorkItem.jsp";
        else
            location.href="ProcessInstantiator.jsp";
    }
    function nullForm()
    {
        if (document.attachmentForm.documentTitle.value == "")
        {
            alert("You need to provide a title for the document!");
            document.attachmentForm.documentTitle.focus();
            return false;
        }
        if (document.attachmentForm.documentTitle.value == "")
        {
            alert("You need to provide a title for the document!");
            document.attachmentForm.documentTitle.focus();
            return false;
        }
        if (document.attachmentForm.yesNewAttachment.value == "")
        {
            alert("You need to choose the attachment type!");
            return false;
        }
        if (document.attachmentForm.document.value == "")
        {
            if (document.attachmentForm.yesNewAttachment.value == "true")
            {
                alert("You need to provide the local file path!");
                document.attachmentForm.document.focus();
                return false;
            }
        }
        if (document.attachmentForm.dmsPath.value == "")
        {
            alert("You need to provide the Dms file path!");
            document.attachmentForm.dmsPath.focus();
            return false;
        }
        if(document.attachmentForm.isNewAttachment[0].checked == true)
        {
			if (document.attachmentForm.dmsPath.value.lastIndexOf(".") == -1)
			{
				alert("You must specify the actual file name for the attachment!")
				document.attachmentForm.dmsPath.focus();
				return false;
			}
		}

    return true;
    }

    function enableAttControls(isNew)
    {
        if (isNew == "false") {
            attachmentForm.document.disabled = true;
            document.attachmentForm.dmsPath.disabled = false;
            document.attachmentForm.brwDms.disabled = false;
            document.attachmentForm.overwrite.disabled = true;


            document.getElementById("lclFileTxt").style.visibility = "hidden";
            document.getElementById("lclFileCap").style.visibility = "hidden";

            document.attachmentForm.yesNewAttachment.value = "false";
        }
        else
        {
            attachmentForm.dmsPath.disabled = false;
            document.attachmentForm.brwDms.disabled = false;
            attachmentForm.document.disabled = false;
            document.attachmentForm.overwrite.disabled = false;

            document.getElementById("lclFileTxt").style.visibility = "visible";
            document.getElementById("lclFileCap").style.visibility = "visible";


            document.attachmentForm.yesNewAttachment.value = "true";
        }
    }


    function openDmsBrowser(url)
    {

      var w = '450';
      var h = '400';
      var winl = (screen.width-w)/2;
      var wint = (screen.height-h)/2;
      var settings  ='height='+h+',';
          settings +='width='+w+',';
          settings +='top='+wint+',';
          settings +='left='+winl+',';
          settings +='scrollbars='+'yes'+',';
          settings +='resizable=no';
      if(document.attachmentForm.yesNewAttachment.value == "true")
          url = url + "?ShowFile=false";
      else
          url = url + "?ShowFile=true";
      var childWindow = window.open(url, "DmsBrowser",settings);
        if (childWindow.opener == null)
            childWindow.opener = self;
    }




</script>
<body class="bodyClass" onLoad="goforit(); enableAttControls('false');">
<center>
<%
    boolean overwrite = false;
    String title="";
    String ss = request.getParameter("attOverwrite");
    if (ss != null && ss.equals("true")) {
        overwrite = true;
        title="value=\""+request.getParameter("attTitle")+"\"";
    }
    boolean isWorkitem = false;
    Plan plan = null;
    String splanId = null;
    ProcessInstantiator processInstantiator = null;
    String processName = null;

    String taskName = null;
    String sTaskId = request.getParameter("taskId");
    if (sTaskId != null) {
        long taskId = Long.parseLong(sTaskId);
        WorkItem task = bps.getWorkItem(taskId);
        isWorkitem = true;
        taskName = task.getName();
%>
        <jsp:include page="WorkItemHeader.jsp" flush="true"/>
<%
    }
    else {
        plan = (Plan)session.getAttribute("plan");
        splanId = (String)session.getAttribute("planID");
        processInstantiator = (ProcessInstantiator)session.getAttribute("processInstantiator");
        processName = processInstantiator.getName();
%>
        <jsp:include page="ProcessHeader.jsp" flush="true"/>
<%
    }

    String[] bcolor = {"evenrow","oddrow"};
    int nextColor = 0;
    String formName = request.getParameter("Forms");
    if (formName == null) {
        formName = "";
    }
    String actionURL = "AddAttachmentAction.jsp?isWorkitem="+isWorkitem+"&Forms="+formName;
    if (sTaskId != null) {
        actionURL = actionURL + "&taskId="+sTaskId;
    }
%>
        <br/><br/>
        <form name="attachmentForm" enctype="multipart/form-data" method="post" action="<%= actionURL %>" onSubmit="return nullForm();">
        <TABLE border="0" cellpadding="0" cellspacing="1" class="tableClass" width="650" align="center" bgcolor="#CCCCCC">
            <tr>
                <td>
                    <table width="100%" border="0" cellpadding="4" cellspacing="0">
                    <tr class="headrow">
                        <td colspan="2" align="center">
<%
                    if (isWorkitem) {
%>
                        Adding attachment to Task: <%= taskName %>
<%
                    }
                    else {
%>
                        Adding attachment to Process Instance: <%= processName %>
<%
                    }
%>
                        </td>
                            </tr>
                        <tr class="oddrow">
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            </tr>
                        <tr class="oddrow">
                            <td class="prompt" align="right"> Document Title: </td>
                            <td> <input type="text" name="documentTitle" <%=title%> size="60"> </td>
                            </tr>
                        <tr class="oddrow">
                            <td class="prompt" align="right"> Document Source: </td>
                            <td> <input type="radio" name="isNewAttachment" value="false" width="300" onClick="enableAttControls(this.value)" checked>Browse existing Attachments</input></td>
                            </tr>
                        <tr class="oddrow">
                            <td class="prompt" align="right"></td>
                            <td> <input type="radio" name="isNewAttachment" value="true" width="300" onClick="enableAttControls(this.value)" >Add a new Attachment</input></td>
                            </tr>
                        <tr class="oddrow">
                            <td class="prompt" align="right"><Div id="lclFileCap"> Local File: </Div></td>
                            <td><Div id="lclFileTxt"> <input type="file" name="document" size="60" disabled="true"> </Div></td>
                            </tr>
                        <tr class="oddrow">
                            <td class="prompt" align="right"><Div id="remFileCap"> DMS File Path:</Div></td>
                            <td>
                                <Div id="remFileTxt">
                                <input type="text" name="dmsPath" value="" size="60"/>
                                <input type="button" name="brwDms" value="Browse.." onClick="openDmsBrowser('DmsBrowser.jsp')"/>
                                </Div>
                            </td>
                            </tr>
                        <tr class="oddrow">
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            </tr>
                        <tr class="evenrow">
                            <td class="prompt"colspan="2"> <input name="overwrite" type="checkbox" value="true" >
                            OK to overwrite document
                            </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
            <center><br/>
                    <input type="submit" value="Attach document" class="submitClass">
        </center>
            <center><br/>
                    <input type="hidden" name="yesNewAttachment" value="">
        </center>

            </form>
</center>
</body>
</html>