<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Wierszowki.Models.MagazineItemViewData>" %>
<%@ Import namespace="Wierszowki.Core.Validation"%> 
<asp:Content ID="js" ContentPlaceHolderID="jsContent" runat="server">
    <script src="../../Scripts/jquery-1.3.1.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery-1.3.1.min.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.form.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.blockUI.js" type="text/javascript"></script>
    <script src="../../Scripts/date.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.datePicker.js" type="text/javascript"></script>
    <script src="../../Scripts/spiffy.js" type="text/javascript"></script>
    <script src="../../Scripts/jHelper.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.validate.js" type="text/javascript" ></script>
    <script src="../../Scripts/xVal.jquery.validate.js" type="text/javascript"></script>
</asp:Content>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Dodaj wierszówkę
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
        $(function() {
            
            $("#Magazine").change(function() {
                var magazineId = $("#Magazine > option:selected").attr("value");
                var urlAction = "<%= Url.Action("FindIssuesById", "Magazine") %>";
                urlAction = urlAction + "/" + magazineId;
                $.getJSON(urlAction, function(data) {
                    $("#IssueId").addItems(data);
                });
            }); 
            jQuery.validator.setDefaults({
	           success: "valid"
            });
            jQuery.validator.addMethod("DateCorrectFormat", function(data, element) {
                return data.match(/^(19|20)\d\d(-)(0[1-9]|1[012])$/);
	            }, "Podaj prawidłową datę i format (0000-00).");
            
            jQuery.validator.addMethod("RangePrice", function(cena, element) {
                return cena.match(/^((0)|(0[,]\d\d)|([1-9]\d[,]\d\d)|([1-9]\d\d[,]\d\d)|([1]{1}[0]{3}[,][0]{2}))$/);
                },"Wymagany format to 0,00 , a wartość nie może być większa niż 1000.");
            
            $("#myform").validate({
                rules: {
                     AuthorId:    { required: true },
                     IssueId:     { required: true, min:1 },
                     ItemTypeId:  { required: true },
                     Date:        { required: true, DateCorrectFormat: true },
                     Caption:     { required: true, maxlength: 255 },
                     Description: { maxlength: 255 },
                     Bonus:       { required: true, RangePrice: true},
                     Price:       { required: true, RangePrice: true}
                },
                messages: {
                     AuthorId:    { required: "Proszę wybrać autora." },
                     IssueId:     { required: "Proszę wybrać numer wydania.", min: "Prosze wybrać numer wydania. Musisz najpierw wybrać Gazetę." },
                     ItemTypeId:  { required: "Proszę wybrać typ wierszówki." },
                     Date:        { required: "Proszę podać miesiąc"},
                     Caption:     { required: "Proszę podać tytuł wierszówki.", maxlength: "Tytuł jest za długi." },
                     Description: { maxlength:"Opis jest za długi."},
                     Bonus:       { required: "Proszę wprowadzić bonus dla wierszówki."},
                     Price:       { required: "Proszę wprowadzić wycenę wierszówki."}
                }
             });
             $("#myform").change(function() {
                $("#myform").valid();
             });
         });
    </script>
    <style type="text/css">
        select,input { border: 1px solid black;}
	    select.error, input.error { border: 1px solid red; }
        br { clear: both; }

        label.error {
		    background: url('http://dev.jquery.com/view/trunk/plugins/validate/demo/images/unchecked.gif') no-repeat;
		    padding-left: 18px;
		    margin-left: 270px;
		    margin-top: 3px;
	    }
    </style>
    <h2>Dodaj wierszówkę</h2>
    <%= Html.ValidationSummary("Proszę poprawić błędy i spróbować ponownie.")%>
    <div>                      
        <% Html.BeginForm("Create", "MagazineItem", Model.MagazineItem, FormMethod.Post, new { id = "myform" }) ;
            %>
            <fieldset>
                <legend>Nowa wierszówka</legend>
                <div class="formitem">
                    <label for="MagazineItem.AuthorId">Autor:</label>
                    <%= Html.DropDownList("AuthorId", ViewData.Model.AuthorList, "-- proszę wybrać --")%>
                    <%= Html.ValidationMessage("AuthorId", "*")%>
                    <br />
                </div>
                <div class="formitem">
                    <label for="Magazine">Gazeta:</label>
                    <%= Html.DropDownList("Magazine", ViewData.Model.MagazineList, "-- proszę wybrać --")%>
                    <%= Html.ValidationMessage("Magazine", "*")%>
                    <br />
                </div>
                <div class="formitem">
                    <label for="MagazineItem.IssueId">Wydanie:</label>
                    <%= Html.DropDownList("IssueId", ViewData.Model.IssueList, "-- proszę wybrać --")%>
                    <%= Html.ValidationMessage("IssueId", "*")%>
                    <br />
                </div>
                <div class="formitem">
                    <label for="MagazineItem.ItemTypeId">Typ:</label>
                    <%= Html.DropDownList("ItemTypeId", ViewData.Model.ItemTypeList, "-- proszę wybrać --")%>
                    <%= Html.ValidationMessage("ItemTypeId", "*")%>
                     <br />
                </div>
                <div class="formitem">
                    <label for="MagazineItem.Date">Miesiąc:</label>
                    <%= Html.TextBox("Date", ViewData.Model.MagazineItem.Date, new { Class = "date-pick" })%>
                    <%= Html.ValidationMessage("Date", "*")%>
                    <br />
                </div>
                
                <div class="formitem">
                    <label for="MagazineItem.Caption">Tytuł:</label>
                    <%= Html.TextBox("Caption", ViewData.Model.MagazineItem.Caption, new { title = "Proszę wprowadzić tytuł artykułu." })%>
                    <%= Html.ValidationMessage("Caption", "*")%>
                    <br />
                </div>
                <div class="formitem">
                    <label for="MagazineItem.Description">Opis:</label>
                    <%= Html.TextArea("Description", ViewData.Model.MagazineItem.Description, new { title = "Proszę wprowadzić opis artykułu." })%>
                    <br />
                </div>
                <div class="formitem">
                    <label for="MagazineItem.Price">Wycena:</label>
                    <%= Html.TextBox("Price", ViewData.Model.MagazineItem.Price)%>
                    <%= Html.ValidationMessage("Price", "*")%> 
                    <br />
                </div>
                <div class="formitem">
                    <label for="MagazineItem.Bonus">Bonus:</label>
                    <%= Html.TextBox("Bonus", ViewData.Model.MagazineItem.Bonus)%>
                    <%= Html.ValidationMessage("Bonus", "*")%>
                    <br />
                </div>
                <div>                
                    <input type="submit" value="Dodaj" name="dodaj" id="dodaj"/>                
                </div>
            </fieldset>        
        <% Html.EndForm();%>               
    </div>
    <div>
        <%=Html.ActionLink("Powrót", "Index", "Home") %>
        <%=Html.ClientSideValidation<MagazineItemValidation>("MagazineItem")%>
    </div>
</asp:Content>