<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%><%@page import="com.fujitsu.iflow.common.UdaNameSanitizer"
%><%@page import="com.fujitsu.iflow.common.DataSourceConfParser"
%><%@page import="com.fujitsu.iflow.common.ErrorMessage"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.bpmcore.BPMSessionImpl"
%><%@page import="com.fujitsu.iflow.model.util.ModelException"
%><%@page import="com.fujitsu.iflow.model.util.QuickForm"
%><%@page import="com.fujitsu.iflow.model.util.QuickFormOperation"
%><%@page import="com.fujitsu.iflow.model.util.URLEncoder"
%><%@page import="com.fujitsu.iflow.model.workflow.DataItem"
%><%@page import="com.fujitsu.iflow.model.workflow.FormRef"
%><%@page import="com.fujitsu.iflow.model.workflow.NodeInstance"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.model.workflow.WorkItem"
%><%@page import="java.io.File"
%><%@page import="java.io.FileReader"
%><%@page import="java.io.PrintWriter"
%><%@page import="java.io.Reader"
%><%@page import="java.io.Writer"
%><%@page import="java.lang.Long"
%><%@page import="java.net.URL"
%><%@page import="java.text.ParseException"
%><%@page import="java.util.Hashtable"
%><%@page import="java.util.Properties"
%><%@page import="java.io.FileInputStream"
%><%@page import="java.util.StringTokenizer" 
%><%@page import="java.text.DateFormat"
%><%@page import="java.util.Date"
%><%@page import="java.util.Locale"
%><%@page import="javax.xml.parsers.DocumentBuilder"
%><%@page import="javax.xml.parsers.DocumentBuilderFactory"
%><%@page import="org.w3c.dom.Document"
%><%@page import="org.w3c.dom.Element"
%><%@page import="org.w3c.dom.Node"
%><%@page import="org.w3c.dom.NodeList"
%><%@page import="org.xml.sax.SAXException"
%><%@page import="org.xml.sax.SAXParseException"
%><%

    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;
    } 

    String webServerURL = bps.getServerOption("TWFServerBaseURL");
    if (webServerURL == null || webServerURL.length() <= 0) 
    {
        throw new Exception("Servlet Engine Configuration Error: Need to provide a value for 'TWFServerBaseURL'");
    }

    // Note: if you are developing forms, uncomment the following
    // line and you will always see the latest version of your form.
    // there is a performance penalty so this should not be done
    // in production situations.

    QuickForm.flush();

    String formName = request.getParameter("Forms");
    String formPath = "";
    String taskIdStr = request.getParameter("wid");
    if (taskIdStr == null)
    {
        //used to have a parameter taskId, changing to wid, but might be some left around.
        taskIdStr = request.getParameter("taskId");
    }
    if (taskIdStr == null) 
    {
        throw new Exception("WorkItem.jsp requires a 'taskId' parameter to specify which work item it should display.");
    }
    long taskId = Long.parseLong(taskIdStr);
    int completeWiIndex = -1;
    String isCompletedWI = request.getParameter("isCompleted");
    if (isCompletedWI != null) 
    {
        try 
        {
            completeWiIndex = Integer.parseInt(isCompletedWI);
        }
        catch (NumberFormatException nfe) 
        {
            completeWiIndex = -1;
        }
    }

    //added
    String fileName = application.getRealPath("/").trim();
    if (fileName.endsWith(File.separator))
    {
        fileName = fileName + "Config"+File.separator+"QuickFormCSSDef.xml";
    }
    else
    {
        fileName = fileName + File.separator + "Config"+File.separator+"QuickFormCSSDef.xml";

    }
    //end of addition.                     

    QuickForm qf = null;
    String quickFormWarningMessage = null;
    WorkItem task = null;
    if (completeWiIndex >= 0)
    {
        WorkItem[] tasks = (WorkItem[])session.getAttribute("completedTasks");
        if (tasks == null || tasks.length <= 0) 
        {
            throw new Exception("Missing the completed workitem information for work item '" + taskId + "'.");
        }
        task = tasks[completeWiIndex];
    }
    else 
    {
        try
        {
            task = bps.getWorkItem(taskId);
        }
        catch(Exception ex)
        {
            throw new Exception("The WorkItem with id '" + taskId + "' could not be found.  Reason: "+ex.toString());
        }
    }
    if (task == null) 
    {
        throw new Exception("The WorkItem with id '" + taskId + "' could not be found.");
    }

    request.setAttribute("task", task);

    String[] forms = null;

    if ( completeWiIndex >=0 ) 
    {
        forms = getFormNames(task);
    }
    else 
    {
        forms = task.getFormNames();
    }

    try 
    {
        if(forms == null || forms.length == 0) 
        {
            formName = null;
        }
        else 
        {
            boolean bValidForm = false;
            if (formName != null) 
            {
                for (int i=0; i<forms.length; ++i) 
                {
                    if (formName.equals(forms[i])) 
                    {
                        bValidForm = true;
                        break;
                    }
                }
            }
            if (!bValidForm)
            {
                formName = forms[0];
            }
        }

        if (formName != null && formName.trim().length() > 0) 
        {
            if ( completeWiIndex >=0 ) 
            {
                formPath = (String)formHash.get(formName);
            }
            else 
            {
                formPath = task.getFormFileName(formName);
            }

            String lcform = formPath;

            lcform = lcform.replace('/',File.separatorChar);
            lcform = lcform.replace('\\',File.separatorChar);

            if (lcform.endsWith(".jsp") && lcform.indexOf(application.getRealPath("/").trim()) >= 0)
            {
                String destUrl = formPath;
                int pos = formPath.lastIndexOf("\\");
                if (pos >= 0)
                {
                    destUrl = destUrl.substring(pos + 1);
                }
                else
                {
                    pos = formPath.lastIndexOf("/");
                    if(pos >= 0)
                    {
                        destUrl = destUrl.substring(pos + 1);
                    }
                }
                destUrl = destUrl + "?taskId=" + taskId;
                response.sendRedirect(destUrl);
                return;
            }

            if(formPath.indexOf("FormsRepository") >= 0)
            {
                String formRep = formPath.substring(formPath.lastIndexOf("FormsRepository")+16);
                int pos = formRep.indexOf("\\");
                webServerURL = webServerURL.trim();
                if (!webServerURL.endsWith("/"))
                {
                    webServerURL += "/";
                }
                StringBuffer encodedFormPath = new StringBuffer(webServerURL);
                encodedFormPath.append("QuickForms/");
                while (pos >= 0)
                {
                    urlEncode(encodedFormPath, formRep.substring(0, pos));
                    encodedFormPath.append("/");
                    formRep = formRep.substring(pos+1);
                    pos = formRep.indexOf("\\");
                }

                urlEncode(encodedFormPath, formRep);
                qf = QuickForm.getQuickForm(formName, encodedFormPath.toString());
            }
            else
            {
                qf = QuickForm.getQuickForm(formName, formPath);
            }
        }
        else
        {
            qf = QuickForm.getDefaultQuickForm();
        }

    }
    catch (Exception e) 
    {
        e.printStackTrace();
        quickFormWarningMessage = "WARNING:\n\nUsing the 'default' form above because system is unable to retrieve form '"
            +formName+"' from the file '"+formPath
            +"' for work item id '"+taskId+"' because: "+e.toString()
            +".\n\n   Correct this situation in order to see the form displayed properly.";
        qf = QuickForm.getDefaultQuickForm();
    }
    try
    {
        int size = qf.opList.length;
        DataItem[] udaData = task.getDataItems();
        int ulen = udaData.length;
        String uNames[] = new String[ulen];
        for (int i=0; i<ulen; i++) 
        {
            uNames[i] = udaData[i].getName();
        }
        Hashtable udaLookup = UdaNameSanitizer.getReverseLookup(uNames);
        for (int i=0; i<size; i++) 
        {
            QuickFormOperation op = qf.opList[i];
            String param = op.paramValue.trim();
            switch(op.opCode) 
            {
                case QuickFormOperation.WORKITEMHEADER:
                {

                    %>
                        <jsp:include page="WorkItemHeader.jsp" flush="true"/>
                        <style type="text/css">@import url(calendar/calendar-win2k-1.css);</style>
                        <script type="text/javascript" src="calendar/calendar.js"></script>
                        <script type="text/javascript" src="calendar/lang/calendar-en.js"></script>
                        <script type="text/javascript" src="calendar/calendar-setup.js"></script>
                    <%
                }
                    continue;
                case QuickFormOperation.PROCESSPANEL: 
                {
                    request.setAttribute("includeSet", param);
                    %>
                        <jsp:include page="ProcessPanel.jsp" flush="true"/>
                    <%
                }
                    continue;
                case QuickFormOperation.ATTACHMENTPANEL:
                {
                    %>
                        <jsp:include page="ShowAttachments.jsp" flush="true"/>
                    <%
                }
                    continue;
                case QuickFormOperation.DEFAULTUDAS:
                {
                    %>
                        <jsp:include page="DefaultUDAs.jsp" flush="true"/>
                    <%
                }
                    continue;
                // This has been added to handle the new functionality provided in IBPM ver 8 server.
                case QuickFormOperation.HTMLCONTROL: 
                {	Properties prop = op.parseAttributes();
                    handleControls(fileName,out,param,udaLookup,task,bps,prop);
                    continue;
                }
                case QuickFormOperation.TEMPLATESTART :
                {
                    continue;
                }
                case QuickFormOperation.TEMPLATEEND :
                {
                    continue;
                }

                case QuickFormOperation.HTMLSTRING:

                    if (isCompletedWI != null && op.paramValue.toLowerCase().indexOf("</form>")>=0) 
                    {
                        out.write("<input type=\"hidden\" name=\"isCompleted\" value=\"" + completeWiIndex +"\">");
                    }
                    out.write(op.paramValue);
                    continue;
            }

            // everything that falls through to here is a UDA value input

            String value = "";
            String attributeName = (String)udaLookup.get(param);
            String attrType = "STRING";

            // if a particular user defined attribute of the form is not found,
            // then simply output a null-string value.
            if (attributeName != null) 
            {
                value = task.getDataItemValue(attributeName);
                attrType = task.getDataItemType(attributeName);
                //if(attrType.equals(DataItem.TYPE_DATE) && !isDate(value)){
                //    Date d = new Date(Long.parseLong(value));
                //    DateFormat df = DateFormat.getDateTimeInstance(
                //          DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.getDefault());
                //    value = df.format(d);
                //}
            }

            switch(op.opCode)
            {
                case QuickFormOperation.FIELD:
                    QuickForm.writeHtmlWithLineBreaks(out, value);
                    break;
                case QuickFormOperation.INPUT:
                    showInputBox(out, attributeName, value, attrType);
                    break;
                case QuickFormOperation.HIDDEN:
                    out.write("<input type=\"hidden\" name=\"uda_");
                    QuickForm.writeHtml(out, param);
                    out.write("\" value=\"");
                    QuickForm.writeHtml(out, value);
                    out.write("\" />");
                    break;
                case QuickFormOperation.CALENDAR:
                    showInputBox(out, attributeName, value, attrType);
                    break;
                default:
                    throw new Exception("Invalid opcode {{?" + op.opCode + " " + param + "}}");
            }
        }
        if (quickFormWarningMessage != null) 
        {
            %>
                <center>
                    <table border="1" width="90%" cellpadding="20" cellspacing="0" bordercolor="#99CCFF" bgcolor="#FFFFFF">
                        <tr>
                             <td width="100%">
                                 <font face="Arial" color="#008080">
                                    <% QuickForm.writeHtmlWithLineBreaks(out, quickFormWarningMessage); %>
                                 </font>
                            </td>
                        </tr>
                    </table>
                </center>
            <%
        }
    }
    catch (Exception e) 
    {
        throw new Exception ("Unable to render form '"+formName+"' file '"+formPath
               +"' for work item id '"+taskId+"' because: "+e.toString());
    }
%>

<%!
Hashtable formHash = new Hashtable();

public String[] getFormNames(WorkItem wi) throws Exception
{
    String[] formList = null;
    NodeInstance ni = wi.getNodeInstance();
    FormRef[] fref = ni.getForms();
    if (fref != null) 
    {
        formList = new String[fref.length];
        for (int i=0; i< fref.length ; i++) 
        {
            formHash.put(fref[i].getName(), fref[i].getPath());
            formList[i] = fref[i].getName();
        }
    }
    return formList;
}

public boolean isDate(String text)
{
    boolean isMatch = false;
    if (text != null && text.trim().length()>0)
    { 
        try
        {
            DateFormat df = DateFormat.getDateTimeInstance(
                    DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.getDefault());
            df.parse(text);
            isMatch = true;
        }
        catch (ParseException e)
        {
            try 
            {
                long l = Long.parseLong(text);
                new Date(l);
            }
            catch (NumberFormatException e1)
            {
            }
        }
    }
    return isMatch;
}

//The following is a patch to make quick forms work in Spain.
//the QuickForm.getQuickForm routine was reading the form using
//the default character set, and not using UTF-8 as it should have.
//Remove this once the model is fixed (v7.0)

private static Hashtable formCache = new Hashtable();

private QuickForm getQuickForm(String name,String path) throws Exception
{
    name = name.toLowerCase();
    QuickForm result = (QuickForm)formCache.get(name);
    if (result != null) 
    {
        return result;
    }
    if (path == null || path.trim().length() <= 0) 
    {
        return QuickForm.getDefaultQuickForm();
    }
    
    String form = null;
    Reader rr = null;
    if (path.toLowerCase().indexOf("http://") > -1 ) 
    {
        java.net.URL formUrl = new java.net.URL(path.trim());
        rr = new java.io.InputStreamReader(formUrl.openStream(), "UTF-8");
    }
    else 
    {
        rr = new FileReader(path);
    }
    result = new QuickForm(name, rr);
    formCache.put(name, result);
    return result;
}

private void urlEncode(StringBuffer dest,String source)
{
    for(int i=0; i<source.length(); i++) 
    {
        char ch = source.charAt(i);
        if ( (ch >= '0' && ch <= '9') ||
                (ch >= 'a' && ch <= 'z') ||
                (ch >= 'A' && ch <= 'Z') ||
                (ch == '_') || (ch == '.')||
                (ch == '/')) 
        {
            dest.append(ch);
        }
        else
        {
            dest.append('%');
            int j = (int) ch;
            if (j<128) 
            {
                dest.append(Integer.toString((int)ch, 16));
            }
            // above 128 need to encode in UTF8 first!
            else if (j<2048) 
            {
                dest.append(Integer.toString(192 + ((j >> 6) % 32), 16));
                dest.append('%');
                dest.append(Integer.toString(128 + (j % 64), 16));
            }
            else if (j<2048) 
            {
                dest.append(Integer.toString(224 + ((j >> 12) % 16), 16));
                dest.append('%');
                dest.append(Integer.toString(128 + ((j>>6) % 64), 16));
                dest.append('%');
                dest.append(Integer.toString(128 + (j % 64), 16));
            }
        }
    }
}

//new Additions
public void handleControls(String fileName,Writer out,String param,Hashtable udaLookup,WorkItem task,BPMSession bps,java.util.Properties prop) 
    throws Exception
		{
		    //String s = null;
		    String type=null;
		    String uda = null;
		    String value = null;
		    String maxchar = null;
		    String maxwidth ="";
		    String cols = "";
		    String rows = "";
		    String property1 = "";
		    String size = "";
		    String style_name="";
		    boolean valid = false;
		    
		    type = prop.getProperty("type");
		    uda = prop.getProperty("uda");
		    
		    if(prop.getProperty("value")!=null){
		    	 value = prop.getProperty("value");
		    }
		    if(prop.getProperty("maxchar")!=null){
		    	 maxchar = prop.getProperty("maxchar");
		    }    
		    if (prop.getProperty("maxwidth") != null) {
		        maxwidth = prop.getProperty("maxwidth");
		    }
		    if (prop.getProperty("rows") != null) {
		        rows = prop.getProperty("rows");
		    }
		    if (prop.getProperty("cols") != null) {
		        cols = prop.getProperty("cols");
		    }
		    if (prop.getProperty("multiselection") != null) {
		        property1 = prop.getProperty("multiselection");
		    }
		    if (prop.getProperty("size") != null) {
		        size = prop.getProperty("size");
		    }
		    if (prop.getProperty("name") != null) {
		        style_name = prop.getProperty("name");
		    }
		    if (type != null) {
		        valid = true;
		    }
		
		    // after reading all the tokens
		    try
		    {
		        if (valid)
		        {
		            String input_value = "";
		            String attrType = "STRING";
		            String uda_type = "STRING";
		            if (uda != null) 
		            {
		                uda = (String)udaLookup.get(uda);
		                if(task != null)
		                {
		                    input_value = task.getDataItem(uda).getValue();
		                    uda_type = task.getDataItem(uda).getType();
		                }
		                else
		                {
		                    input_value ="";
		                }
		            }
		            if(uda_type == null)
		            {
		                uda_type = type;
		            }
		            if(input_value == null)
		            {
		                input_value = "";
		            }
		            if (type.equalsIgnoreCase("TEXT"))
		            {
		                showTextBox(out,uda,value,input_value,maxchar,maxwidth,uda_type,"TEXT");
		            }
		            else if ( type.equalsIgnoreCase("PASSWORD") )
		            {
		                showTextBox(out,uda,value,input_value,maxchar,maxwidth,uda_type,"PASSWORD");
		            }
		            else if ( type.equalsIgnoreCase("HIDDEN") )
		            {
		                showTextBox(out,uda,value,input_value,maxchar,maxwidth,uda_type,"HIDDEN");
		            }
		            else if ( type.equalsIgnoreCase("READONLY") )
		            {
		                showTextBox(out,uda,value,input_value,maxchar,maxwidth,uda_type,"READONLY");
		            }
		            else if ( type.equalsIgnoreCase("TEXTAREA") )
		            {
		                showTextArea(out,uda,value,input_value,rows,cols);
		            }
		            else if ( type.equalsIgnoreCase("RADIO") )
		            {
		                showRadioBox(out,uda,value,input_value);
		            }
		            else if ( type.equalsIgnoreCase("CHECK") )
		            {
		                showCheckBox(out,uda,value,input_value);
		            }
		            else if ( type.equalsIgnoreCase("COMBO") )
		            {
		                showListBox(out,uda,value,input_value,"Single",size);
		            }
		            else if ( type.equalsIgnoreCase("LIST") )
		            {
		                showListBox(out,uda,value,input_value,property1,size);
		            }
		            else if ( type.equalsIgnoreCase("CALENDAR") )
		            {
		                showCalendar(out,uda,input_value);
		            }
		            else if ( type.equalsIgnoreCase("DEFAULT") )
		            {
		                showCustomInputBox(out,uda,input_value,uda_type);
		            }
		            else if ( type.equalsIgnoreCase("STYLE") )
		            {
		                putStyle(fileName,out,style_name,bps);
		            }
		        }
		    }catch(Exception e)
		    {
		        throw new Exception("Exception occurred while parsing the HTMLCONTROL type Data"+e.toString());
		    }
}

private Document getQuickFormCssDoc(BPMSession bps) throws ModelException
{
    Document doc = null;
    try
    {            
        doc = DataSourceConfParser.getXmlConfigFileDocument(((BPMSessionImpl)bps)._wfs, "QuickFormCSSDef.xml");
    }
    catch(Exception ex)
    {
        //ErrorMessage Could not complete the XML parsing. = 10017 = XML_PARSING_FAILED
        //Log.println(Log.LEVEL0,"getQuickFormCssDoc: "+ex);
        throw new ModelException(ErrorMessage.XML_PARSING_FAILED,ex);
    }
    return doc;
}

private String getStyleSheetsPath(String fileName,Writer out,String style_name,BPMSession bps) 
    throws Exception
{
    String path = null;
    try
    {
        Document doc = getQuickFormCssDoc(bps);
        NodeList listOfStyles = doc.getElementsByTagName("style");
        int totalStyles = listOfStyles.getLength();
        //styleSheetName = new String[totalStyles];
        for(int s=0; s<listOfStyles.getLength() ; s++)
        {
            Node firstStyleNode = listOfStyles.item(s);
            if(firstStyleNode.getNodeType() == Node.ELEMENT_NODE)
            {
                Element styleElement = (Element)firstStyleNode;
                NodeList styleNameList = styleElement.getElementsByTagName("name");
                Element styleNameElement = (Element)styleNameList.item(0);
                NodeList textSNList = styleNameElement.getChildNodes();
                String name = ((Node)textSNList.item(0)).getNodeValue().trim();
                if(name.equals(style_name))
                {
                    NodeList pathList = styleElement.getElementsByTagName("path");
                    Element pathVal = (Element)pathList.item(0);
                    NodeList pathNameList = pathVal.getChildNodes();
                    path= ((Node)pathNameList.item(0)).getNodeValue().trim();
                    return path;
                }
            }//end of if clause
        }//end of for loop with s var
        //return path;
    }catch (Exception e)
    {
        throw new Exception("  Error getting Style sheet "+ fileName,e);
    }catch (Throwable t)
    {
        throw new Exception("  Error getting Style sheet "+ fileName,t);
    }
    return null;
}

public void putStyle(String fileName,Writer out,String style_name,BPMSession bps)
    throws Exception
{
    Properties props = new Properties();
    try
    {
        String style = null;
        if(style_name == null || style_name.length() == 0)
        {
            style = "default";
        }
        else
        {
            style = style_name;
        }
        String path = null;
        path = getStyleSheetsPath(fileName,out,style_name,bps);
        if(path==null)
        {
            throw new Exception("  Style sheet not found or invalid "+ fileName);
        }
        
        out.write("<LINK rel=\"stylesheet\" href=\"");
        QuickForm.writeHtml(out, path);
        out.write("\" type=\"text/css\">\n");
        out.write("<style type=\"text/css\">@import url(calendar/calendar-win2k-1.css);</style>\n");
        out.write("<script type=\"text/javascript\" src=\"calendar/calendar.js\"></script>\n");
        out.write("<script type=\"text/javascript\" src=\"calendar/lang/calendar-en.js\"></script>\n");
        out.write("<script type=\"text/javascript\" src=\"calendar/calendar-setup.js\"></script>\n");
    }
    catch(Exception e)
    {
        throw new Exception("  Style sheet not found or invalid ",e);
    }
    
}

public String getContolType(String type)
{
    if ( type.equalsIgnoreCase("\"TEXT\""))
    {
        return "TEXT";
    }
    else if ( type.equalsIgnoreCase("\"PASSWORD\"") )
    {
        return "PASSWORD";
    }
    else if ( type.equalsIgnoreCase("\"HIDDEN\"") )
    {
        return "HIDDEN";
    }
    else if ( type.equalsIgnoreCase("\"READONLY\"") )
    {
        return "READONLY";
    }
    else if ( type.equalsIgnoreCase("\"TEXTAREA\"") )
    {
        return "TEXTAREA";
    }
    else if ( type.equalsIgnoreCase("\"RADIO\"") )
    {
        return "RADIO";
    }
    else if ( type.equalsIgnoreCase("\"CHECK\"") )
    {
        return "CHECK";
    }
    else if ( type.equalsIgnoreCase("\"COMBO\"") )
    {
        return "COMBO";
    }
    else if ( type.equalsIgnoreCase("\"LIST\"") )
    {
        return "LIST";
    }
    else if ( type.equalsIgnoreCase("\"Calendar\"") )
    {
        return "CALENDAR";
    }
    else if ( type.equalsIgnoreCase("\"Default\"") )
    {
        return "DEFAULT";
    }
    else if ( type.equalsIgnoreCase("\"style\"") )
    {
        return "STYLE";
    }
    return null;
}

public void showTextArea(Writer out,String uda,String value,String input_value,String rows,String cols) 
    throws Exception
{
    try
    {
    String udaIdentifier = UdaNameSanitizer.getSanitizedString(uda);
        value = input_value;
        out.write("<textarea name=\"uda_");
        QuickForm.writeHtml(out,udaIdentifier);
        out.write("\" cols=\""+cols+ "\" rows=\""+rows+"\">");
        QuickForm.writeHtml(out,input_value);
        out.write("</textarea>");
        
    } 
    catch(Exception e)
    {  
        QuickForm.writeHtml(out, e.toString());
    }   
}

public void showRadioBox(Writer out,String uda,String value,String input_value)
    throws Exception
{
    try
    {
        String udaIdentifier = UdaNameSanitizer.getSanitizedString(uda);
        String[] tokens = null;
        if(value!=null)
        {
            tokens = value.split("&sep;");
        }
        String name = "";
        String val = "";
        String select[]=null;
        boolean flag = false;
        if(input_value!=null )
        {
            select = input_value.split("&sep;");
        }
        if(tokens!= null && tokens.length!=0)
        {
            for(int i_token=0;i_token<tokens.length;i_token++)
            {
                out.write("\n");
                name = "";
                val = "";
                name = tokens[i_token];
                val = name.substring(name.indexOf("=")+1);
                name = name.substring(0,name.indexOf("="));
                if(select != null && select.length !=0)
                {
                    for(int index=0;index<select.length;index++)
                    {
                        if ( val.trim().equals(select[index].trim()))
                        {
                            flag = true;
                        }
                    }
                }
                out.write("<input type=\"radio\" name=\"uda_");
                QuickForm.writeHtml(out,udaIdentifier);
                out.write("\" value=\"");
                QuickForm.writeHtml(out,val);
                if(flag)
                {
                    out.write("\" checked>");
                }
                else
                {
                    out.write("\">");
                }
                QuickForm.writeHtml(out,name);
                out.write("<br>");
                flag = false;
            }//end of for loop
        }//end of if
    }catch(Exception e)
    { 
        QuickForm.writeHtml(out, e.toString());
    }
}


public void showCheckBox(Writer out,String uda,String value,String input_value)
    throws Exception
{
    try
    {
    String udaIdentifier = UdaNameSanitizer.getSanitizedString(uda);
        String name="";
        String val ="";
        String[] tokens= null;
        if(value != null)
        {
            tokens= value.split("&sep;");
        }
        String select[] = null;
        if(input_value!=null )
        {
            select = input_value.split("&sep;");
        }
        boolean flag = false;
        if(tokens != null  && tokens.length!=0)
        {
            for(int i_token=0;i_token<tokens.length;i_token++)
            {
                out.write("\n");
                name = "";
                val = "";
                name = tokens[i_token];
                val = name.substring(name.indexOf("=")+1);
                name = name.substring(0,name.indexOf("="));
                String option = "option";
                if(select !=null && select.length!=0)
                {
                    for(int index=0;index<select.length;index++)
                    {
                        if ( val.trim().equals(select[index].trim()))
                        {
                            flag = true;  
                        }
                    }
                }
                out.write("<input type=\"checkbox\" name=\"uda_");
                QuickForm.writeHtml(out,udaIdentifier);
                out.write("\" value=\"");
                QuickForm.writeHtml(out,val);
                if(flag)
                {
                    out.write("\" checked />");
                }
                else
                {
                    out.write("\" />");
                }
                
                flag = false;                        
                QuickForm.writeHtml(out,name);
            }
        }//end of if
    } catch(Exception e)
    {   
        QuickForm.writeHtml(out, e.toString());
    }
}

public void showListBox(Writer out,String uda,String value,String input_value,
        String property1,String size) throws Exception
{
    try
    {
    String udaIdentifier = UdaNameSanitizer.getSanitizedString(uda);
        String[] tokens=null;
        String prop = property1;
        if(value !=null)
        {
            tokens = value.split("&sep;");
        }
        String name = "";
        String val = "";
        boolean flag = false;
        String[] select = input_value.split("&sep;");
        out.write("\n");
        if(prop == null) 
        {
            prop = "Single";
        }
        out.write("<select " + prop + " name=\"uda_");
        QuickForm.writeHtml(out,udaIdentifier);
        out.write("\" size=\""+size+"\">");
        if(tokens!=null && tokens.length!=0)
        {
            for(int i_token=0;i_token<tokens.length;i_token++)
            {
                out.write("\n");
                name = "";
                val = "";
                name = tokens[i_token];
                val = name.substring(name.indexOf("=")+1);
                name = name.substring(0,name.indexOf("="));
                if(select !=null && select.length!=0)
                {
                    for(int index=0;index<select.length;index++)
                    {
                        if( val.trim().equals(select[index].trim()))
                        {
                            flag = true;
                        }
                    }
                }
                if(flag) 
                {
                    out.write("<option value=\"");
                    QuickForm.writeHtml(out,val);
                    out.write("\" selected>" );
                    QuickForm.writeHtml(out,name);
                    out.write("</option>");
                }
                else
                {
                    out.write("<option value=\"");
                    QuickForm.writeHtml(out,val);
                    out.write("\" >");
                    QuickForm.writeHtml(out,name);
                    out.write("</option>");
                }
                flag = false;
            }//end of for loop
            out.write("</select");
        }//end of if
    } catch(Exception e)
    {   
        QuickForm.writeHtml(out, e.toString());
    }
}

public void showTextBox(Writer out,String uda,String value,String input_value,String maxchar,
    String maxwidth,String udaItemType,String type) throws Exception
{
    try
    {
    String udaIdentifier = UdaNameSanitizer.getSanitizedString(uda);
        value = input_value;
        if(type.equals("TEXT"))
        {
            out.write("<input type=\"text\" name=\"uda_");
            QuickForm.writeHtml(out,udaIdentifier);
            out.write("\" size=\""+maxwidth + "\" value=\"");
            QuickForm.writeHtml(out,value);
            out.write("\" maxlength=\"" + maxchar + "\"/>");
        }
        else if(type.equals("HIDDEN"))
        {
            out.write("<input type=\"HIDDEN\" name=\"uda_");
            QuickForm.writeHtml(out,udaIdentifier);
            out.write("\" size=\""+maxwidth + "\" value=\"");
            QuickForm.writeHtml(out,value);
            out.write("\" maxlength=\"" + maxchar + "\"/>");
        }
        else if(type.equals("PASSWORD"))
        {
            out.write("<input type=\"PASSWORD\" name=\"uda_");
            QuickForm.writeHtml(out,udaIdentifier);
            out.write("\" size=\""+maxwidth + "\" value=\"");
            QuickForm.writeHtml(out,value);
            out.write("\" maxlength=\"" + maxchar + "\"/>");
        }
        else if(type.equals("READONLY"))
        {
            if(udaItemType.equals(DataItem.TYPE_DATE)) 
            {
                out.write("\n<input type=\"hidden\" name=\"uda_");
                QuickForm.writeHtml(out, udaIdentifier);
                out.write("\" size=\"14\" value=\"");
                QuickForm.writeHtml(out, value);
                out.write("\" />");
                //add input box
                out.write("\n<input type=\"text\" name=\"xxx_");
                QuickForm.writeHtml(out, udaIdentifier);
                out.write("\" size=\"40\" READONLY onChange=\"validateDate(wiForm.uda_");
                QuickForm.writeHtml(out,udaIdentifier);
                out.write(",wiForm.xxx_");
                QuickForm.writeHtml(out, udaIdentifier);
                out.write(");\"/>");
                out.write("\n<script type=\"text/javascript\">");
                out.write("\nwiForm.xxx_");
                QuickForm.writeHtml(out, udaIdentifier);
                out.write(".value = formatDate(wiForm.uda_");
                QuickForm.writeHtml(out,udaIdentifier);
                out.write(".value); ");
                out.write("\n</script>");
            }
            else 
            {
                out.write("<input type=\"TEXT\" name=\"uda_");
                QuickForm.writeHtml(out,udaIdentifier);
                out.write("\" size=\""+maxwidth + "\"  READONLY value=\"");
                QuickForm.writeHtml(out,value);
                out.write("\" maxlength=\"" + maxchar + "\"/>");
            }
        }
        out.write("\n");
    } 
    catch(Exception e)
    {   
        QuickForm.writeHtml(out, e.toString());
    }
}

public void showCalendar(Writer out,String uda,String udaValue)
    throws Exception
{
//  first rationalize the date into a long value
//  because apparently people do it different ways.
    try{
        String udaIdentifier = UdaNameSanitizer.getSanitizedString(uda);
        long lval = 0;
        try 
        {
            lval = Long.parseLong(udaValue);
        }
        catch (Exception e2) 
        {
            // parse the date
            // assign lval
        }
        //add hidden uda for value
        out.write("\n<input type=\"hidden\" name=\"uda_");
        QuickForm.writeHtml(out, udaIdentifier);
        out.write("\" size=\"14\" value=\"");
        QuickForm.writeHtml(out, Long.toString(lval));
        out.write("\" />");
        //add input box
        out.write("\n<input type=\"text\" name=\"xxx_");
        QuickForm.writeHtml(out, udaIdentifier);
        out.write("\" size=\"40\" onChange=\"validateDate(wiForm.uda_");
        QuickForm.writeHtml(out, udaIdentifier);
        out.write(",wiForm.xxx_");
        QuickForm.writeHtml(out, udaIdentifier);
        out.write(");\"/>");
        //Add image button
        out.write("\n<input type=\"image\" name=\"button_");
        QuickForm.writeHtml(out, udaIdentifier);
        out.write("\" src=\"calendar/cal.gif\" width=\"16\" height=\"15\"/>");
        //Add Script
        out.write("\n<script type=\"text/javascript\">");
        out.write("\nwiForm.xxx_");
        QuickForm.writeHtml(out, udaIdentifier);
        out.write(".value = formatDate(wiForm.uda_");
        QuickForm.writeHtml(out, udaIdentifier);
        out.write(".value); ");
        out.write("\nCalendar.setup({inputField  : \"xxx_");
        QuickForm.writeHtml(out, udaIdentifier);
        out.write("\", button      : \"button_");
        QuickForm.writeHtml(out, udaIdentifier);
        out.write("\"});");
        out.write("\nshowTimeZone();");
        out.write("\n</script>");
    }
    catch (Exception e) 
    {
        QuickForm.writeHtml(out, e.toString());
    }
}


public void showCustomInputBox(Writer out,String udaName,String udaValue,String udaType)
    throws Exception
{
    try {
        String udaIdentifier = UdaNameSanitizer.getSanitizedString(udaName);
        if(udaType.equals(DataItem.TYPE_DATE)) 
        {
            // first rationalize the date into a long value
            // because apparently people do it different ways.
            long lval = 0;
            try 
            {
                lval = Long.parseLong(udaValue);
            }
            catch (Exception e2) 
            {
                // parse the date
                // assign lval
            }
            //add hidden uda for value
            out.write("\n<input type=\"hidden\" name=\"uda_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" size=\"14\" value=\"");
            QuickForm.writeHtml(out, Long.toString(lval));
            out.write("\" />");
            //add input box
            out.write("\n<input type=\"text\" name=\"xxx_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" size=\"40\" onChange=\"validateDate(wiForm.uda_"+udaIdentifier+",wiForm.xxx_"+udaIdentifier+");\"/>");
            //Add image button
            out.write("\n<input type=\"image\" name=\"button_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" src=\"calendar/cal.gif\" width=\"16\" height=\"15\"/>");
            //Add Script
            out.write("\n<script type=\"text/javascript\">");
            out.write("\nwiForm.xxx_"+udaIdentifier+".value = formatDate(wiForm.uda_"+udaIdentifier+".value); ");
            out.write("\nCalendar.setup({inputField  : \"xxx_"+udaIdentifier+"\", button      : \"button_"+udaIdentifier+"\"});");
            out.write("\nshowTimeZone();");
            out.write("\n</script>");
        } 
        else if (udaType.equals(DataItem.TYPE_STRING))
        {
            //add input box
            if(udaValue == null)
            {
                udaValue = "";
            }
            out.write("<textarea name=\"uda_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" cols=\"68\" rows=\"2\">");
            QuickForm.writeHtml(out, udaValue);
            out.write("</textarea>");
        } 
        else if (udaType.equals(DataItem.TYPE_INTEGER)) 
        {
            //add input box
            if(udaValue == null)
            {
                udaValue = "0";
            }
            out.write("<input type=\"text\" name=\"uda_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" size=\"30\" value=\"");
            QuickForm.writeHtml(out, udaValue);
            out.write("\" onChange=\"validateInt(wiForm.uda_"+udaIdentifier+");\"/>");
        } 
        else if (udaType.equals(DataItem.TYPE_LONG)) 
        {
            if(udaValue == null)
            {
                udaValue = "0";
            }
            //add input box
            out.write("<input type=\"text\" name=\"uda_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" size=\"30\" value=\"");
            QuickForm.writeHtml(out, udaValue);
            out.write("\" onChange=\"validateLong(wiForm.uda_"+udaIdentifier+");\"/>");
        } 
        else if (udaType.equals(DataItem.TYPE_FLOAT))
        {
            if(udaValue == null)
            {
                udaValue = "0.0";
            }
            //add input box
            out.write("<input type=\"text\" name=\"uda_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" size=\"30\" value=\"");
            QuickForm.writeHtml(out, udaValue);
            out.write("\" onChange=\"validateFloat(wiForm.uda_"+udaIdentifier+");\"/>");
        }
        else if (udaType.equals(DataItem.TYPE_BIGDECIMAL))
        {
            //add input box
            if(udaValue == null)
            {
                udaValue = "0";
            }
            out.write("<input type=\"text\" name=\"uda_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" size=\"30\" value=\"");
            QuickForm.writeHtml(out, udaValue);
            out.write("\" onChange=\"validateBig(wiForm.uda_"+udaIdentifier+");\"/>");
        } 
        else if (udaType.equals(DataItem.TYPE_BOOLEAN)) 
        {
            //add input box
            if(udaValue == null)
            {
                udaValue = "false";
            }
            boolean isTrue = udaValue.equals("true");
            out.write("<input type=\"radio\" name=\"uda_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" ");
            if (isTrue) 
            {
                out.write("checked ");
            }
            out.write("value=\"true\" /> true ");
            out.write("<input type=\"radio\" name=\"uda_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" ");
            if (!isTrue) 
            {
                out.write("checked ");
            }
            out.write("value=\"false\" /> false ");
        }
        else
        {
            //add input box
            out.write("<input type=\"text\" name=\"uda_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" size=\"30\" value=\"");
            QuickForm.writeHtml(out, udaValue);
            out.write("\"/>");
        }
    }catch (Exception e) 
    {
        out.write("Unable to understand '");
        QuickForm.writeHtml(out, udaValue);
        out.write("' because: ");
        QuickForm.writeHtml(out, e.toString());
    }
    
}

//end of Additions



public void showInputBox(Writer out,String udaName,String udaValue,String udaType)
    throws Exception
{
    try 
    {
        String udaIdentifier = UdaNameSanitizer.getSanitizedString(udaName);
        if(udaType.equals(DataItem.TYPE_DATE)) 
        {
            // first rationalize the date into a long value
            // because apparently people do it different ways.
            long lval = 0;
            try 
            {
                lval = Long.parseLong(udaValue);
            }
            catch (Exception e2) 
            {
                // parse the date
                // assign lval
            }
            //add hidden uda for value
            out.write("\n<input type=\"hidden\" name=\"uda_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" size=\"14\" value=\"");
            QuickForm.writeHtml(out, Long.toString(lval));
            out.write("\" />");
            //add input box
            out.write("\n<input type=\"text\" name=\"xxx_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" size=\"40\" onChange=\"validateDate(wiForm.uda_"+udaIdentifier+",wiForm.xxx_"+udaIdentifier+");\"/>");
            //Add image button
            out.write("\n<input type=\"image\" name=\"button_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" src=\"calendar/cal.gif\" width=\"16\" height=\"15\"/>");
            //Add Script
            out.write("\n<script type=\"text/javascript\">");
            out.write("\nwiForm.xxx_"+udaIdentifier+".value = formatDate(wiForm.uda_"+udaIdentifier+".value); ");
            out.write("\nCalendar.setup({inputField  : \"xxx_"+udaIdentifier+"\", button      : \"button_"+udaIdentifier+"\"});");
            out.write("\nshowTimeZone();");
            out.write("\n</script>");
        } 
        else if (udaType.equals(DataItem.TYPE_STRING))
        {
            //add input box
            out.write("<textarea name=\"uda_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" cols=\"68\" rows=\"2\">");
            QuickForm.writeHtml(out, udaValue);
            out.write("</textarea>");
        } 
        else if (udaType.equals(DataItem.TYPE_INTEGER))
        {
            //add input box
            out.write("<input type=\"text\" name=\"uda_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" size=\"30\" value=\"");
            QuickForm.writeHtml(out, udaValue);
            out.write("\" onChange=\"validateInt(wiForm.uda_"+udaIdentifier+");\"/>");
        }
        else if (udaType.equals(DataItem.TYPE_LONG)) 
        {
            //add input box
            out.write("<input type=\"text\" name=\"uda_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" size=\"30\" value=\"");
            QuickForm.writeHtml(out, udaValue);
            out.write("\" onChange=\"validateLong(wiForm.uda_"+udaIdentifier+");\"/>");
        } 
        else if (udaType.equals(DataItem.TYPE_FLOAT)) 
        {
            //add input box
            out.write("<input type=\"text\" name=\"uda_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" size=\"30\" value=\"");
            QuickForm.writeHtml(out, udaValue);
            out.write("\" onChange=\"validateFloat(wiForm.uda_"+udaIdentifier+");\"/>");
        } 
        else if (udaType.equals(DataItem.TYPE_BIGDECIMAL)) 
        {
            //add input box
            out.write("<input type=\"text\" name=\"uda_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" size=\"30\" value=\"");
            QuickForm.writeHtml(out, udaValue);
            out.write("\" onChange=\"validateBig(wiForm.uda_"+udaIdentifier+");\"/>");
        } 
        else if (udaType.equals(DataItem.TYPE_BOOLEAN)) 
        {
            //add input box
            boolean isTrue = udaValue.equals("true");
            out.write("<input type=\"radio\" name=\"uda_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" ");
            if (isTrue) 
            {
                out.write("checked ");
            }
            out.write("value=\"true\" /> true ");
            out.write("<input type=\"radio\" name=\"uda_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" ");
            if (!isTrue) 
            {
                out.write("checked ");
            }
            out.write("value=\"false\" /> false ");
        } 
        else 
        {
            //add input box
            out.write("<input type=\"text\" name=\"uda_");
            QuickForm.writeHtml(out, udaIdentifier);
            out.write("\" size=\"30\" value=\"");
            QuickForm.writeHtml(out, udaValue);
            out.write("\"/>");
        }
    }
    catch (Exception e) 
    {
        out.write("Unable to understand '");
        QuickForm.writeHtml(out, udaValue);
        out.write("' because: ");
        QuickForm.writeHtml(out, e.toString());
    }
}

%>

