<%@ page extends="oneit.servlets.jsp.JSPInclude" %>

<%@ include file="/inc/stdimports50.jsp" %><%-- This is in cougar --%>
<%@ include file="/inc/stdcms.jsp" %><%-- This is in cougar --%>
<%@ include file="/extensions/performa/inc/stdimports.jsp" %>

<oneit:dynIncluded>
    <div class="form-page-section">
    <script type="text/javascript">
        
        function moveImportanceSection(elementRating){
            
            var importanceDiv   =   $(elementRating).closest('.criteria_section').find('.work-rating');
            var radioGroup      =   $("[name='" + $(elementRating).attr('name') +"']");
            
            if(radioGroup.is(':checked')){
                importanceDiv.insertAfter($("[name='" + $(elementRating).attr('name') +"']:checked").parent());
                importanceDiv.show();
            }
            else{
                importanceDiv.hide();
            }
        }
        
        $(document).ready(function(){
                        
            $(".element_rating_radio").each(function (){
                moveImportanceSection(this);
            });
            
            $(".element_rating_radio").change(function(){
                moveImportanceSection(this);
            });
            
            $(".importance_radio").change(function(){
                var importanceGroup =   $(this).closest('.rate-toggle');
                
                importanceGroup.find('li.active').removeClass("active" );
                importanceGroup.find("input[type='radio']:checked").closest('li').addClass("active" );
            });
            
            $("select[name$='CultureTemplate']").change(function(){
                $("button[name$='loadCultureFromTemplate']").click();
            });
                        
            var showError = true;
            addPostRecalcHandler (function ($fieldThatChanged) {
                if(showError) {
                    showError = false;
                    $('.errorField input, .errorField select, .errorField textarea, .errorField .assocObjDesc').eq(0).focus();
                }
                recalcFunction();
            });
        });
        
    </script>
    <%
        Job                     job         =   (Job) getData(request, "Job"); 
        CultureCriteriaTemplate template    =   (CultureCriteriaTemplate) getData(request, "CultureCriteriaTemplate");

        FormTag     jobForm     = FormTag.getActiveFormTag(request);
        FormBuilder formBuilder = jobForm.getFormBuilder();

        Debug.assertion(job != null || template != null , "Job or Template is null");

        for(CultureClass cultureClass : CultureClass.getCultureClassArray())
        {
    %>
            <div class="form-group">
                <div class="work-belonging">
                    <oneit:toString value="<%= cultureClass %>" mode="EscapeHTML"/>
                </div>
                <div class="critarea-desc"><%= cultureClass.getClassDescription() %></div>
                
            <%
                for(CultureCriteria criteria : (job != null ? job.getCultureCriteriasForClass(cultureClass) : template.getCultureCriteriasForClass(cultureClass)))
                {
                    String          optionKey   =   WebUtils.getRadioSingleAssocKey(request, criteria, CultureCriteria.SINGLEREFERENCE_CultureElementRating);
                    String          formValue   =   formBuilder.fieldValue (optionKey, criteria.getCultureElementRating() == null ? "" : String.valueOf(criteria.getCultureElementRatingID()));
                    CultureElement  element     =   criteria.getCultureElement();
            %>
                    <div class="criteria_section culture-margin-bottom">
                        <fieldset >
                            <legend>
                                <oneit:toString value="<%= element %>" mode="EscapeHTML"/>
                                <a href="#" title='<%= element.getDetailedDescription() %>' class="info-icon-left"><img src="images/info-icon.png" /></a>
                            </legend>

                            <label class="switch">
                                <oneit:recalcClass  htmlTag="span"  classScript="criteria.getIsApplicable() ? 'checkbox checked': 'checkbox unchecked'"    criteria="<%= criteria %>">
                                    <oneit:ormInput obj="<%= criteria %>"    attributeName="IsApplicable"   type="checkbox"/>
                                </oneit:recalcClass>
                                <div class="slider round"></div>
                            </label>

                        <oneit:recalcClass  htmlTag="div"   classScript="criteria.getIsApplicable() ? 'show': 'hide'"  criteria="<%= criteria %>">
                        <%
                            for (CultureElementRating rating : element.getRatingsSet())
                            {
                                String  ratingId    = String.valueOf(rating.getID().longID());
                                String  selectedStr = (CollectionUtils.equals(ratingId, formValue) ? "checked" : "");
                        %>
                                <div class="<%= "radio radio-primary workplace-radio " %>">
                                    <input type="radio" name="<%= optionKey %>" id="<%= ratingId %>"    class="element_rating_radio"    value="<%= ratingId %>" <%= selectedStr %> >
                                    <label for="<%= ratingId %>">
                                        <oneit:toString value="<%= rating %>"   mode="EscapeHTML"/>
                                    </label>
                                </div>
                        <%
                            }
                        %>
                        </oneit:recalcClass>
                        <div class="work-rating">
                            <div class="rate-background">
                                <span class="arrow-up-gray"></span>
                                <span class="rate-label">
                                    <oneit:label GUIName="Rate Importance" />
                                </span>
                                <span class="rate-toggle">
                                    <ul class="nav nav-pills nav-justified">
                                    <%
                                        String  importanceKey   =   WebUtils.getInputKey(request, criteria, CultureCriteria.FIELD_Importance);
                                        String  importanceValue =   formBuilder.fieldValue (importanceKey, criteria.getImportance() == null ? "" : criteria.getImportance().getName());

                                        for(Importance importance : Utils.getImportancesForCulture())
                                        {
                                            boolean isSelected      =   CollectionUtils.equals(importanceValue, importance.getName());
                                            String  importanceId    =   String.valueOf(criteria.getID().longID()) + "_importance_" + importance.getName();
                                            String  selected        =   isSelected ? "checked" : "";
                                            String  activeClass     =   isSelected ? "active" : "";
                                    %>
                                            <li class="<%= activeClass%>">
                                                <a href="javascript:void(0)" class="importance">

                                                    <input type="radio" name="<%= importanceKey %>" id="<%= importanceId %>"    class="importance_radio"    value="<%= importance.getName() %>" <%= selected %>/>
                                                    <label for="<%= importanceId %>"><oneit:toString value="<%= importance.getDescription() %>" mode="EscapeHTML" /></label>
                                                </a>
                                            </li>
                                    <%
                                        }
                                    %>
                                    </ul>
                                </span>
                            </div>
                        </div>
                        </fieldset>
                    </div>
                    <!--<div class="form-brack-line-sub"></div>-->
            <%
                }
            %>
            </div>
    <%
        }
    %>
    </div> 
</oneit:dynIncluded>