<<InsertPage page='CssStyles'>>

<<JOpen>>

<<JScript  includes='QuestionnaireFunctions/functions'

    //var title_link = 'AUDITIERUNG ERP-SYSTEM UND OPERATIVE PROZESSE';
    //neu aus E-Mail JLichtscheidel
    var title_link = "Digitalisierungscheck";
    var page_name = 'AUDITIERUNG ERP-SYSTEM ';

    jglobal.set('page_name',page_name);

    var outerTable = [[*
            [{JHtml

         <table class="outerTable" >
         <tr>

            }]
            *]];
    var mainFraim = [[*
            [{JHtml

            <td class='leftFraim'>

            }]
            *]];
    var closeFraim = [[*
            [{JHtml

            </td>

            }]
            *]];
    var rightFraim = [[*
            [{JHtml

            <td class='rightFraim'>

            }]
            *]];
    var closeTable = [[*
            [{JHtml

            </tr></table>

            }]
            *]];

    // get id progect from link
    var id_project = jpage.get("id");
    if(parseInt(id_project) > 0 ) {
        jglobal.clear();
        jglobal.set('id_project',id_project);
        jglobal.set("categoryIndex", 0);
        jglobal.set("category", 0);
    } else {
        if(jglobal.containsKey('id_project')) {
           var id_project = parseInt( jglobal.get('id_project'));
        }
    }

    // verify if the results page is accesed
    var results = parseInt(jpage.get('pageResults'));
    if(results == 1) {
        jglobal.set('pageResults',1);
        jumpToTopOfThePage();
    }

    try {
        var tcon = jrt.exclusiveCon( "PP.ShowEditProjects",false);
        // if link activate account is accesed
         if(jpage.get('ac') == 1){
            if(jpage.get('uuid') != null){
                // get user uuid from link
                var uuid = jpage.get('uuid');
                var querySelect = tcon.prepareStatement("SELECT * FROM clients WHERE  uuid='" + uuid +  "'").executeQuery();

                while(querySelect.next()) {
                    var email= querySelect.getString('email');
                    var clientId= querySelect.getInt('id_client');
                    jglobal.set('email',email);
                    jglobal.set('clientId',clientId);
                    var confirmed_email = querySelect.getBoolean('confirmed_email');

                    if(confirmed_email == true){
                        addMessage("information", "Willkommen zurück "+email);
                    } else {
                        var query = "UPDATE clients  SET confirmed_email = true WHERE uuid='"+uuid+"'" ;
                        var saveStatement = tcon.prepareStatement(query);
                        var executeResult = saveStatement.executeUpdate();
                        tcon.commit();
                        // save user details globaly
                        tcon.commit();
                        addMessage("information", "Ihr Konto wurde erfolgreich aktiviert.");
                    }
                }
            }
        }
    } catch(e) {
        if(tcon != null) tcon.rollback();
            throw "ShowEditProjects1 jreturn:" + e;
    } finally {
        if(tcon != null) {
            jrt.release(tcon);
        }
    }

    try {
       var tcon = jrt.exclusiveCon( "PP.ShowEditProjects2",false);
        // if link see results from email is active
        if(jpage.get('se') == 1) {
            if(jpage.get('uuid') != null) {
                var uuid = jpage.get('uuid');
                // save user details globaly
                var querySelect = tcon.prepareStatement("SELECT id_client, email FROM clients WHERE  uuid='" + uuid +  "'").executeQuery();
                if(querySelect.next()) {
                    var email= querySelect.getString('email');
                    var clientId= querySelect.getInt('id_client');
                    jglobal.set('email',email);
                    jglobal.set('clientId',clientId);

                    tcon.commit();
                    querySelect.close();
                    var queryUpdate = "UPDATE projects_clients set is_active  = true where fk_project = "+parseInt(id_project)+
                    " and fk_client = "+parseInt(clientId)+" ";
                    var queryStatement = tcon.createStatement();
                    queryStatement.executeUpdate(queryUpdate);
                    tcon.commit();
                    queryStatement.close();
                    var confirmation_message = displayMessagesByKey(tcon, 'welcome_projekt');

                    addMessage("information", confirmation_message);
                 } else {
                    var link_print = [[*
                      [{JHtml

                        <div id='headerLayout'>
                          <h1 class="page_title">$title_link</h1>
                        </div>
                        }]
                      *]];

                      var link_print = jutil.replace(link_print, "$title_link", title_link);

                      jout.println(link_print);

                      var user_deleted = displayMessagesByKey(tcon, 'user_deleted');
                      jout.println('|'+ user_deleted);
                      return;
                 }
            }
        }
      } catch(e) {
        if(tcon != null) {
          tcon.rollback();
        }
        throw "ShowEditProjrweects jreturn:" + e;
      } finally {
        if(tcon != null) {
            jrt.release(tcon);
        }
    }

    try {
        var tcon = jrt.exclusiveCon( "PP.ShowEditProjeects2",false);
        var queryProject = tcon.prepareStatement("SELECT id_project, name_project, fk_status FROM projects WHERE  id_project='" + id_project +     "'").executeQuery();
        if(queryProject.next()) {
            var    name_project= queryProject.getString("name_project");
            var    fk_status= queryProject.getInt("fk_status");
        } else {
            var    name_project= '';
            var    fk_status= 0;
        }

        jglobal.set('name_project',name_project);
        tcon.commit();
        queryProject.close();
    } catch(e) {
        if(tcon != null) {
          tcon.rollback();
        }
        throw "ShowEditPrewrwrwerojects jreturn:" + e;
    } finally {
        if(tcon != null) {
            jrt.release(tcon);
        }
    }
    // get all categories by current project
    try {
        var tcon = jrt.exclusiveCon("QuestionnaireFunctions.getSubcatogoriesByCatProject",false);
        tcon.commit();
        getCategoriesALLByProject(tcon, id_project);
        tcon.commit();
    } catch(e) {
        if(tcon != null) tcon.rollback();
        throw "getSubcatogoriesByCatProject jreturn:" + e;
    } finally {
        if(tcon != null) jrt.release(tcon);
    }

    try {
        var tcon = jrt.exclusiveCon("ProjectsPage.showCategories",false);
        var link_print = [[*
                [{JHtml

                    <div id='headerLayout'>
                        <h1 class="page_title">$title_link</h1>
                        <h3 class="page_title">$project_name</h3>
                    </div>
                    }]
                *]];

        var link_print = jutil.replace(link_print, "$title_link", title_link);
        var link_print = jutil.replace(link_print, "$project_name", name_project);
        jout.println(link_print);

         //   SHOW MESSAGES
        showMessage();

        tcon.commit();

        if(jglobal.containsKey('categoryIndex')) {
            var categoryIndex = parseInt(jglobal.get("categoryIndex"));
            jglobal.set("categoryIndex",categoryIndex);
        } else {
            jglobal.set("categoryIndex", 0);
        }

        // link category index prom right menu, categoryIndex
        var categoryIndex = jpage.get("category");
        if(categoryIndex != null) {
            jglobal.set("categoryIndex", categoryIndex);
            jumpToTopOfThePage();
        }
    } catch(e) {
        if(tcon != null) tcon.rollback();
        throw "showCategories jreturn:" + e;
    } finally {
        if(tcon != null){
          jrt.release(tcon);
        }
    }

    var showResults = 0;
    var pageResults = jglobal.get('pageResults');

     if(jpage.get("category") != null) {
        pageResults = 0;
        jglobal.set('pageResults', 0);
     }

    var sumAnswerd = 0;
    var sumTotal = 0;

    if(jglobal.containsKey('sumAnswerd')) {
            var sumAnswerd = parseInt(jglobal.get('sumAnswerd'));
    }
    if(jglobal.containsKey('sumTotal')) {
            var sumTotal =   parseInt(jglobal.get('sumTotal'));
    }
    // verify if all questions are answerd
    if(sumTotal > 0 && sumAnswerd == sumTotal) {
        if(parseInt(pageResults)>0) {
            showResults = 1;
        }
    }
    
    //new overwrite show_results
    var is_questionnaire_completed = isQuestionnaireCompleted();
    if(is_questionnaire_completed)
    {
        if(parseInt(pageResults)>0) {
            showResults = 1;
        }
    }
    
    // if project status is active
    if(fk_status == 1) {
        // if show results page is active
        if(showResults == 1) {
            try {
                var tcon = jrt.exclusiveCon("PP.showResults",false);
                tcon.commit();
                jout.println(outerTable);
                jout.println(mainFraim);

                // show results page function
                showResultsPage(tcon);

                jout.println(closeFraim);
                jout.println(rightFraim);
                jout.println('%%commentbox');

                if(jglobal.containsKey('require_pass') && jglobal.get('require_pass') == 1) {
                    showClientEmailAdressFieldsPass(tcon);
                } else {
                    showClientEmailAdressFields(tcon);
                }

                showInputFieldsUser(tcon);
                showCategoriesLinksProgress(tcon);

                jout.println(closeFraim);
                jout.println(closeTable);
                tcon.commit();
            } catch(e) {
                if(tcon != null){
                    tcon.rollback();
                }
                throw "showResults jreturn:" + e;
            } finally {
                if(tcon != null) jrt.release(tcon);
            }
        } else {

             try {
                var tcon = jrt.exclusiveCon("PP.showQuestions",false);
                jout.println(outerTable);
                jout.println(mainFraim);
                tcon.commit();

                showQuestions(tcon);

                tcon.commit();
                jout.println(closeFraim);
                jout.println(rightFraim);
                jout.println('%%commentbox');

                if(jglobal.containsKey('require_pass') && jglobal.get('require_pass') == 1) {
                    showClientEmailAdressFieldsPass(tcon);
                } else {
                    showClientEmailAdressFields(tcon);
                }

                tcon.commit();

                showInputFieldsUser(tcon);
                showCategoriesLinksProgress(tcon);

                tcon.commit();
                jout.println('== Projektdetails ');
                showProgressAnswerd();

                if(jglobal.containsKey('clientId')) {
                    showResultsButton();
                }

                tcon.commit();
                jout.println('%%');
                jout.println(closeFraim);
                jout.println(closeTable);
            } catch(e) {
                if(tcon != null) {
                    tcon.rollback();
                }
                throw "showQuestions jreturn:" + e;
            } finally {
                if(tcon != null){
                    jrt.release(tcon);
                }
            }
        }
    } else {
         try {
            var tcon = jrt.exclusiveCon("PP.active_project",false);

            sendMailInactiveProject(tcon);

            var active_project = displayMessagesByKey(tcon, 'active_project');
            jout.println('|'+ active_project);
        } catch(e) {
            if(tcon != null) tcon.rollback();
            throw "active_project jreturn:" + e;
        } finally {
            if(tcon != null) {
                jrt.release(tcon);
            }
        }
    }

>>


<<JAction name='login' includes='QuestionnaireFunctions/functions'

    var tcon = null;
    try {
        var tcon = jrt.exclusiveCon("QuestionnaireFunctions.Login",false);
        tcon.commit();

        loginWithEmailOnly(tcon);

        tcon.commit();
    } catch(e) {
        if(tcon != null) tcon.rollback();
        throw " Loginj return:" + e;
    } finally {
        if(tcon != null)jrt.release(tcon);
    }

>>

<<JAction name='loginPass' includes='QuestionnaireFunctions/functions'

    var tcon = null;
    try {
        var tcon = jrt.exclusiveCon("QuestionnaireFunctions.LoginPass",false);
        tcon.commit();

        loginUsingPass(tcon);

        tcon.commit();
    } catch(e) {
        if(tcon != null){
            tcon.rollback();
        }
        throw "jreturn:" + e;
    } finally {
        if(tcon != null){
            jrt.release(tcon);
        }
    }

>>

<<JAction name='onForgotPass' includes='QuestionnaireFunctions/functions'

    var tcon = null;
    try {
        var tcon = jrt.exclusiveCon("QuestionnaireFunctions.ForgotPass",false);
        tcon.commit();

        forgotPass(tcon);

        tcon.commit();
    } catch(e) {
        if(tcon != null){
            tcon.rollback();
        }
        throw "jreturn:" + e;
    } finally {
        if(tcon != null){
            jrt.release(tcon);
        }
    }

    jumpToTopOfThePage();

>>

<<JAction name='next' includes='QuestionnaireFunctions/functions'

    if(jglobal.containsKey('clientId')){
        categories = jglobal.get("categories");
        var index =  parseInt(jglobal.get('categoryIndex'));
        if(index != null && index < categories.size()) {
            jglobal.set('categoryIndex', ++index);
        } else {
            jglobal.set('categoryIndex', 1);
        }

        runFunctionWithCon(function(tcon) {

          editUserSubquestionComment(tcon);
          save(tcon);

        }, 'QuestionnaireFunctions.next');

    } else {
        var confirmation_message ="Bitte melden Sie sich zuerst an!";
        addMessage("error", confirmation_message);
    }
    jumpToTopOfThePage();
>>

<<JAction name='previous' includes='QuestionnaireFunctions/functions'

    if(jglobal.containsKey('clientId')) {
        var index = jglobal.get('categoryIndex');

        if(index != null && index > 0) {
            jglobal.set('categoryIndex', --index);
        } else {
            jglobal.set('categoryIndex', 1);
        }

        runFunctionWithCon(function(tcon) {

          editUserSubquestionComment(tcon);
          save(tcon);

        }, 'QuestionnaireFunctions.previous');
    } else {
        var confirmation_message ="Bitte melden Sie sich zuerst an!";
        addMessage("error", confirmation_message);
    }
    jumpToTopOfThePage();
>>

<<JAction name='onSave' includes='QuestionnaireFunctions/functions'

    if(jglobal.containsKey('clientId')) {
        var tcon = '';
        try{
            var tcon = jrt.exclusiveCon("QuestionnaireFunctions.sendMail",false);
            tcon.commit();

            save(tcon);

            tcon.commit();

            getCategoriesALLByProject(tcon,1);
            var isCompleted = isQuestionnaireCompleted(tcon);
            if(isCompleted == false) {
                var page_name = jglobal.get("page_name");
                var error_open_frage = displayMessagesByKey(tcon, 'error_open_frage');
                addMessage("error", error_open_frage  );
            }
            tcon.commit();
        } catch(e) {
            if(tcon != null) {
                tcon.rollback();
            }
            throw "sendMail jreturn:" + e;
        } finally {
            if(tcon != null) {
                jrt.release(tcon);
            }
        }

    } else {
        var confirmation_message ="Bitte melden Sie sich zuerst an!";
        addMessage("error", confirmation_message);
    }

>>

<<JAction name='onShowResults'  includes='QuestionnaireFunctions/functions'

    if(jglobal.containsKey('clientId')) {
        var tcon = jrt.exclusiveCon("QuestionnaireFunctions.saveUserShowResults",false);
        tcon.commit();
        save(tcon);
        tcon.commit();
        jglobal.set('pageResults',1);
        var welcome_projekt = displayMessagesByKey(tcon, 'welcome_projekt');

        addMessage("information", welcome_projekt);
    } else {
        var confirmation_message ="Bitte melden Sie sich zuerst an!";
        addMessage("error", confirmation_message);
    }

    jumpToTopOfThePage();

>>

<<JAction name='onReturnToProject' includes='QuestionnaireFunctions/functions'

    jglobal.set('pageResults',0);
    jumpToTopOfThePage();

>>


<<JAction name='saveUserInfo' includes='QuestionnaireFunctions/functions'


   var css_input =[[*
       [{JCss

           input[type="input"][value=""]:not(#fpara_remarks){
               border: 1px solid red;
            }
        }]
    *]];
    jout.println(css_input);

    var tcon = null;
    try {
        if(jglobal.containsKey('clientId')) {
            var tcon = jrt.exclusiveCon("QuestionnaireFunctions.saveUserInfo",false);
            tcon.commit();
            save(tcon);
            saveUserInfo(tcon);
            tcon.commit();
            var client_information_update =displayMessagesByKey(tcon, 'client_information_update');

            addMessage("information", client_information_update);
        } else {
            var confirmation_message ="Bitte melden Sie sich zuerst an!";
            addMessage("error", confirmation_message);
        }
    } catch(e) {
        if(tcon != null) {
            tcon.rollback();
        }
        throw "saveUserInfo jreturn:" + e;
    } finally {
        if(tcon != null) {
            jrt.release(tcon);
        }
    }

>>


<<JAction name='onSendMail' includes='QuestionnaireFunctions/functions'

    var tcon = '';
    try{
        var tcon = jrt.exclusiveCon("QuestionnaireFunctions.sendMail",false);
        tcon.commit();

        save(tcon);

        tcon.commit();

        getCategoriesALLByProject(tcon,1);
        var is_quest_completed = checkInputFields(tcon);

        if(is_quest_completed == 1) {
            var isCompleted = isQuestionnaireCompleted(tcon);
            if(isCompleted == true) {
                sendMail(tcon);
            } else {
                var page_name = jglobal.get("page_name");
                var error_check_questions= displayMessagesByKey(tcon, 'error_check_questions');
                addMessage("error",  error_check_questions);
            }
        } else {
            var error_complete_userinfo = displayMessagesByKey(tcon, 'error_complete_userinfo');
            addMessage("error", error_complete_userinfo);

            var css_input =[[*
                [{JCss

                    input[type="input"][value=""]:not(#fpara_remarks){
                        border: 1px solid red;
                    }
                }]
            *]];
            jout.println(css_input);
        }

        tcon.commit();
    } catch(e) {
        if(tcon != null) {
            tcon.rollback();
        }
        throw "sendMail jreturn:" + e;
    } finally {
        if(tcon != null) {
            jrt.release(tcon);
        }
    }

    jumpToTopOfThePage();

>>

<<JAction name='onEditUserComments' includes='QuestionnaireFunctions/functions'

  runFunctionWithCon(function(tcon) {
    editUserComments(tcon);
  }, 'QuestionnaireFunctions.onEditUserComments');

>>

<<JAction name='onEditUserQubquestionComments' includes='QuestionnaireFunctions/functions'

  runFunctionWithCon(function(tcon){
    editUserSubquestionComment(tcon);
    save(tcon);
  }, 'QuestionnaireFunctions.onEditUserQubquestionComments');

>>

<<JClose>>