<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>

<script type="text/javascript">

    $(document).ready(function() {
        var ajaxLoading = false;
        $("input[name='saveDefaults']").bind("click", function() {
            if (!ajaxLoading) {
                $("#waiting").show();
                ajaxLoading = true;
                var defParamValue = "";
                var selectedModel = $("input[name='selectedModel']").val();
                var checkedParam = false;
                $("input[name='eachDefault']").each(function(index) {
                    if ($(this).attr("checked")) {
                        checkedParam = true;
                        var paramId = $(this).parent().find("input[name='eachDefaultId']").val();
                        var parValue = $(this).parent().find(".defaultValue").val();
                        var eachParam = paramId + "@" + parValue;

                        if (defParamValue != "") {
                            defParamValue += "," + eachParam;
                        } else {
                            defParamValue = eachParam;
                        }
                    }
                });
                if (!checkedParam) {
                    alert("To save you need to select some parameters!!!");
                } else {
                    if (confirm("Are you shure that the values entered right ?")) {
                        $.ajax({
                            url: "setDefault.action",
                            data:{
                                defParamValues:defParamValue,
                                selectedModel:selectedModel
                            },
                            type:"post",
                            success:function(result) {

                                if (result == "success") {
                                    alert("Process has been completed successfully!");
                                          $("#modelsParams").html(null);


                                    $.ajax({
                                        url: "viewModelParams.action",
                                        data:{
                                            selectedModel:selectedModel
                                        },
                                        type:"post",
                                        success:function(data) {


                                            $("#modelsParams").html(data);
                                            return false;
                                        }
                                    });
                                } else if (result == "error") {
                                    alert("Sorry error occurred.Please try again!");
                                }
                            }
                        });
                    }
                }
                ajaxLoading = false;
                $("#waiting").hide();

            }
//             $("input[name='saveDefaults']").unbind("click");
        });
        var ii=1;
//        setInterval(function(){
//         if(ii%2==0){
////             $("#colorChange").css("backgroundColor","green");
//             $("#colorChange").css("color","green");
//         } else {
////             $("#colorChange").css("backgroundColor","red");
//             $("#colorChange").css("color","red");
//         }
//            ii++;
//        },500)
    });
</script>
<style type="text/css">

    .defaults {
        width: 290px;
        float: left;
        margin-top:10px;
    }

    #saveDefaults {
        margin-top: 50px;
        width: 900px;
        float: left;
    }

    #grayBox {
        z-index: 100;
    }
    /*#colorChange{*/
        /*background-image:url('images/settings/attention.png');*/
    /*}*/
</style>
<div id="waiting"></div>
<%--<div style="margin-top: 20px;">--%>
    <%--&lt;%&ndash;<s:text name="label.modelParams"/>&ndash;%&gt;--%>
<%--</div>--%>
<br/>

<s:if test="%{!isParamsEmpty()}">
    <s:iterator value="parameters" var="parameter" status="s_parameter">
        <div class="defaults">
            <input type="checkbox" name="eachDefault">
            <s:property value="#parameter.visualName"/>
            <s:if test="%{#parameter.defparam==1}">
                <img src="<%=request.getContextPath()%>/images/settings/default.png" alt="default" width="15"
                     height="15"/>
            </s:if>
            <br/>
            <s:if test="%{#parameter.writable==0}">
                <input type="text" value="<s:property value="#parameter.defaultValue" />" class="defaultValue"
                       disabled="disabled"/>
            </s:if>
            <s:else>
                <input type="text" value="<s:property value="#parameter.defaultValue" />" class="defaultValue"/>
            </s:else>
            <input type="hidden" value="<s:property value="#parameter.id" />" name="eachDefaultId"/>
        </div>
    </s:iterator>

    <input type="hidden" name="selectedModel" value="<s:property value='selectedModel' />"/>

    <div id='saveDefaults'>
        <div style="float:left;width:900px;">
            <input type='submit' value='Save' name='saveDefaults' style="line-height:27px;border:none; margin-left: 350px;background: url('images/button/button.png') no-repeat scroll left top transparent; height: 27px;width: 60px; "/>
        </div>



    </div>
    <div style="color:red;font-size:13px;margin:70px 10px 0px;float:left;display:block;width:960px;">
            <div id="colorChange" style="float:left;">
                <img src="<%=request.getContextPath()%>/images/settings/attention.png" alt=""  width="12"
                     height="12" style="margin-top:2px;margin-right:10px;"/>
            </div><div style="float:left;width: 880px;" >If you check the parameters and don't enter the values in the appropriate fields or enter
            wrong values and save them then devices will start working not correctly!!!   </div>
        </div>
</s:if>
<s:else>
    <s:text name="msg.nothaving.params"/>
</s:else>

