<%@page import="com.csc.library.utilities.CheckNull"%>
<%@page import="com.csc.library.entry.UIManager"%>
<%@page contentType="text/html; charset=UTF-8"%>
<jsp:useBean id="APS_NEW_006" class="com.csc.library.system.Task" scope="page"/>
<%
  APS_NEW_006.setChannel(request,response);
  APS_NEW_006.checkProfile();
  UIManager screen = (UIManager) APS_NEW_006.process("SingleEntry","APS_NEW_006");
  String lang = screen.getUProfile().get("lang");
  screen.referLangOff();
  CheckNull chk = new CheckNull();
  String answer = chk.chkNullString(request.getParameter("__answer"),screen.getDataLabel("answer"));
  String answer_format = chk.chkNullString(request.getParameter("__answer_format"),screen.getDataLabel("answer_format"));
%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- Bootstrap -->
        <link href="../JS/QueryBuilder/bootstrap-3.3.5/css/bootstrap.css" rel="stylesheet">

        <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
          <script src="../JS/html5shiv.min.js"></script>
          <script src="../JS/respond.min.js"></script>
        <![endif]-->
        <link href="../CSS/TISCO.css" rel="stylesheet" type="text/css">
        <script src='../JS/SCREEN.js'></script>
        <script src="../JS/HOTKEY.js"></script>
        <script src="../JS/ICONSMENU.js"></script>
        <script src="../JS/VIEWCALENDAR.js"></script>
        <script src="../JS/SWAPLANG_FREEDOM.js"></script>
        <script src="../JS/HOTKEY.js"></script>
        <script src="../JS/exam/CKEDITOR/ckeditor.js"></script>
        <script src="../JS/exam/CKEDITOR/sample.js"></script>
        <script src="../JS/exam/CKEDITOR/extend_plugin.js"></script>
        <script src="../JS/jquery-1.11.3.min.js"></script>
        <script src="../JS/bootstrap.min.js"></script>
        <script src="../JS/exam/VIDEO4ALL/video4all.js"></script>
        <!-- <script src="../AUDIOJS/audio.min.js"></script> -->
        <script>setLangSwapLang("<%=lang%>");</script>
        <script>setLang('<%=lang%>');</script>
        <script>getTitleName();</script>

        <style>
            img[height="20"] {
                padding: 0px;
            }

            .audiojs .play {
                background: url("../AUDIOJS/player-graphics.gif") -8px -1px no-repeat;
            }

            .audiojs .pause {
                background: url("../AUDIOJS/player-graphics.gif") -8px -91px no-repeat;
            }
        </style>
    </head>
    <body>
        <div id="toolTipLayer" style="position:absolute; visibility: hidden"></div>
        <script>initToolTips();</script>
        <form name="cscform" method="post" role="form" action="APS_NEW_006.jsp">
            <!----------BEGIN CSC INITIAL ZONE ------------------------>
            <%=screen.InitialVariable()%>
            <!----------END CSC INITIAL ZONE -------------------------->
            <input type="hidden" name="__screen" value="APS_NEW_006">
            <input type="hidden" name="__help" value="">
            <input type="hidden" name="__fixCon" value="">
            <input type="hidden" name="__cmd" value="">
            <input type="hidden" name="__language" value='<%=lang%>'>
            <input type="hidden" name="__companyid" value="<%=screen.getUProfile().get("companyid")%>">
            <input type="hidden" name="__notNull" value="<%=screen.getRequire()%>">
            <input type="hidden" name="__user" value="<%=screen.getUProfile().getUserId()%>">
            <input type="hidden" name="__referPage" value="">
            <div align="center">
                <script>
                    document.write(Icons(""));
                </script>
                <br>
                <table style = "border-spacing: 0;border-collapse: 0" class="medium">
                    <tr>
                        <td class="header" colspan="2"><script>swLang('APS_NEW_006');</script>&nbsp;</td>
                    </tr>
                    <tr>
                        <td class="blankspace" colspan="2"></td>
                    </tr>
                    <tr>
                        <td class="columnlabel" ><strong><script>swLang('ANSWERID');</script></strong>&nbsp;</td>
                        <td class="columnobject1" ><%=screen.getInput("answerid")%></td>
                    </tr>
                    <tr>
                        <td class="columnlabel"><script>swLang('ANSWER');</script>&nbsp;</td>
                        <td align="left">
                            <textarea name="editor" id="editor" rows="10" cols="80"></textarea>
                            <script>
                                var editor = CKEDITOR.replace('editor', {
                                    customConfig: '../CKEDITOR/config_for_new_aps.js',
                                    width: 700,
                                    height: 150,
                                    language: 'en'
                                });
                                CKEDITOR.instances['editor'].on('instanceReady', function () {
                                    this.document.on('keyup', my_function);
                                });
                            </script>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" class="text-center" style="padding-top: 10px;">
                            <button type="button" id="openDialog" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal">
                                <span class="glyphicon glyphicon-play"></span> Preview
                            </button>

                            <!-- Modal -->
                            <div class="modal fade" id="myModal" role="dialog">
                                <div class="modal-dialog modal-lg">
                                    <!-- Modal content-->
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                                            <h2 style="font-size: 20px;" class="modal-title" style="color: #000000;">Preview</h2>
                                        </div>
                                        <div class="modal-body text-left"></div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td class="blankspace" colspan="2"></td>
                    </tr>
                    <tr>
                        <td class="bottom" colspan="2"></td>
                    </tr>
                </table>
                <div style="display: none;"onclick="result();">Result</div>
                <div>
                    <textarea style="display: none;" id="showResult" rows="7" cols="50"></textarea>
                </div>
                <textarea style="display: none;" id="__answer" name="__answer"><%=answer %></textarea>
                <textarea style="display: none;" id="__answer_format" name="__answer_format"><%=answer_format %></textarea>
            </div>

            <!----------BEGIN CSC ENDING ZONE ------------------------>
            <%=screen.endJSP()%>
            <!----------END CSC ENDING ZONE -------------------------->


        </form>

        <script>
            // ---------- Preview ----------
            $('#openDialog').click(function () {
                var answer = removeLineBreak(editor.getData());
                answer = setReplacePreview(answer);
                answer = chkFloatLeft(answer);
                answer = chkFloatRight(answer);
                $('.modal-body').html('<strong style="color: #000000;font-size: 150%;">' + getSwapLang('ANSWER') + '</strong><br><br><span style="color: #000000;">' + answer);
            });

            function GoSave() {
                var answer_format = editor.element["$"].nextSibling.childNodes[1].childNodes[1].childNodes[1].contentDocument.childNodes[1].childNodes[1].innerHTML;
                answer_format = setReplaceGoSave(answer_format);
                $('#__answer_format').val(answer_format);
                var answer = removeLineBreak(editor.getData());
                answer = chkFloatLeft(answer);
                answer = chkFloatRight(answer);
                $('#__answer').val(answer);
                confirmSave();
            }

            function removeLineBreak(msg) {
                return msg.replace(/(\r\n|\n|\r)/gm, '');
            }

            function setReplacePreview(msg) {
                msg = msg.replace(/\&#3618;&#160;/g, '&nbsp;');
                msg = msg.replace(/\&#3618;/g, '&nbsp;');
                msg = msg.replace(/\<p> <\/p>/g, '');
                return msg;
            }

            function setReplaceGoSave(msg) {
                msg = msg.replace(/\<br>/g, '&nbsp;');
                msg = msg.replace(/\<p> <\/p>/g, '');
                msg = msg.replace(/\<p>&#3618;&#160;<\/p>/g, '');
				msg = msg.replace('"true"></p>','"true">&nbsp;</p>');
                return msg;
            }

            function chkFloatLeft(answer) {
                var $first = '';
                var $last = '';
                while (answer.indexOf('float:left') > -1) {
                    $first = answer.substring(0, answer.indexOf('style=\"float:left\"'));
                    if ($first.indexOf('<p><img') > -1) {
                        $first = $first.replace('<p><img', '<p style="text-align: left;"><img');
                    } else if ($first.indexOf('<p style="text-align: center"><img') > -1) {
                        $first = $first.replace('<p style="text-align: center"><img', '<p style="text-align: left;"><img');
                    }
                    $last = answer.slice(answer.indexOf('style=\"float:left\"'));
                    $last = $last.replace('style=\"float:left\"', '');
                    answer = $first + $last;
                }
                return answer;
            }

            function chkFloatRight(answer) {
                var $first = '';
                var $last = '';
                while (answer.indexOf('float:right') > -1) {
                    $first = answer.substring(0, answer.indexOf('style=\"float:right\"'));
                    if ($first.indexOf('<p><img') > -1) {
                        $first = $first.replace('<p><img', '<p style="text-align: right;"><img');
                    } else if ($first.indexOf('<p style="text-align: center"><img') > -1) {
                        $first = $first.replace('<p style="text-align: center"><img', '<p style="text-align: right;"><img');
                    }
                    $last = answer.slice(answer.indexOf('style=\"float:right\"'));
                    $last = $last.replace('style=\"float:right\"', '');
                    answer = $first + $last;
                }
                return answer;
            }

            function GoUpdate() {
                update();
            }

            function GoDelete() {
                edelete();
            }

            function GoSearch() {
                search();
            }

            function result() {
                $('#showResult').val(editor.getData() + editor2.getData());
            }

            function setBlankData() {
                $('#__answer').val('');
                $('#__answer_format').val('');
            }

            function edelete() {
                if (nullCheck()) {
                    with (document.cscform) {
                        bCon = confirm(MyCode[4]);
                        if (bCon == true) {
                            __cmd.value = "delete";
                            setSysIsEdit("");
                            setBlankData();
                            submit();
                        } else {
                            if ((typeof __actioned) != "undefined") {
                                __actioned.value = "";
                            }
                        }
                    }
                }
            }

            function GoClean() {
                with (document.cscform) {
                    if (((typeof __cmd) != "undefined") && ((typeof __confirm) != "undefined")) {
                        __cmd.value = "save";
                        __confirm.value = "c";
                        if ((typeof window.parent.leftFrame) != "undefined") {
                            with (window.parent.leftFrame.document.cscform) {
                                __sysisedit.value = "";
                            }
                        }
                        $('#__answer').val('');
                        $('#__answer_format').val('');
                        submit();
                    }
                }
            }

            audiojs.events.ready(function () {
                var as = audiojs.createAll();
            });

            $(function () {
                var $answer_format = $('#__answer_format').val();
                if ($answer_format !== '') {
                    editor.setData($answer_format);
                }
            });

            function my_function() {
                var answer = editor.element["$"].nextSibling.childNodes[1].childNodes[1].childNodes[1].contentDocument.childNodes[1].childNodes[1].innerHTML;
                if (answer.indexOf('๏ป') > -1) {
                    answer = answer.replace(/\๏ป/g, '&nbsp;');
                    answer = answer.replace(/\<P>&nbsp;<\/P>/g, '&nbsp;');
                    editor.element["$"].nextSibling.childNodes[1].childNodes[1].childNodes[1].contentDocument.childNodes[1].childNodes[1].innerHTML = answer;
                }
            }

            function linkHelp(help, input) {
                with (document.cscform) {
                    obj = eval("document.cscform." + input);
                    var param = "PRU084_NEW_APS.jsp?__helpName=" + help + "&__fieldSearch=" + input + "&__pageCall=" + __screen.value + "&__fixCon=" + __fixCon.value + "&tdeq=1";
                    if (obj.value == "") {
                        param = "PRU084_NEW_APS.jsp?__helpName=" + help + "&__fieldSearch=" + input + "&__pageCall=" + __screen.value + "&__fixCon=" + __fixCon.value + "&tdeq=1";
                    }
                    win = window.open(param, "Help", "left=300,top=150,width=700,height=400,resizable=yes,scrollbars=yes,toolbar=no,status=yes");
                    if (window.focus) {
                        win.focus();
                    }
                }
            }

        </script>
    </body>
</html>
<%screen.referLangOff();%>