﻿/// <reference path="jquery-1.4.4-vsdoc.js" />
var lastsel;
var sizes;

$(function () {
    $("body.subsub-none").find("a.create, a.read, a.update, a.delete, a.clone").click(function () {
        $.cookies.set("originUrl", location.href);
    });

    $('a.backtolist').click(function () {
        if ($.cookies.get("originUrl") != null) {
            location.href = $.cookies.get("originUrl");
        };
        return false;
    });


    $("#datagrid").jqGrid({
        url: '/nl/cms/products/DynamicGridData/' + $('#Product_Id').val(),
        datatype: 'json',
        mtype: 'GET',
        colNames: ['Id', 'Maat', 'Prijs', 'Oude Prijs', 'Voorraad', 'Verkocht', 'Referentie', 'Tonen'],
        colModel: [
          { name: 'Id', index: 'Id', width: 20, align: 'left', sortable: false, editable: false, hidden: true },
          { name: 'Maat', index: 'Maat', val: 'ggg', width: 100, align: 'left', sortable: false, editable: true, edittype: 'select' },
          { name: 'Prijs', index: 'Prijs', width: 100, align: 'left', sortable: false, editable: true },
          { name: 'PrijsOud', index: 'PrijsOud', width: 100, align: 'left', sortable: false, editable: true },
          { name: 'Voorraad', index: 'Voorraad', width: 100, align: 'left', sortable: false, editable: true, formatter: 'integer' },
          { name: 'Verkocht', index: 'Verkocht', width: 100, align: 'left', sortable: false, editable: false },
          { name: 'Referentie', index: 'Referentie', width: 200, align: 'left', sortable: false, editable: true, edittype: 'textarea', editoptions: { size: "20", maxlength: "500"} },
          { name: 'Tonen', index: 'Tonen', width: 100, align: 'left', sortable: false, editable: true, edittype: "checkbox", editoptions: { value: "True:False"}}],
        pager: jQuery('#pager'),
        rowNum: 20,
        rowList: [5, 10, 20, 50],
        sortname: 'Maat',
        sortorder: "asc",
        viewrecords: true,
        caption: 'Maten',
        height: 'auto',
        width: 'auto',
        closeAfterAdd: true,
        reloadAfterSubmit: true,
        beforeRequest: function () {
            //get all countries
            sizes = $.ajax({ url: '/nl/cms/products/getSizes/' + $('#Product_Id').val(), async: false, success: function (data, result) { if (!result) alert('Failure to retrieve the Sizes.'); } }).responseText;
        },
        onSelectRow: function (id) {
            if (id && id !== lastsel) {
                jQuery('#datagrid').jqGrid('saveRow', lastsel);
                jQuery('#datagrid').jqGrid('editRow', id, true);
                lastsel = id;
            }
        },
        loadComplete: function () {
            $('#datagrid').setColProp('Maat', { editoptions: { value: sizes.substring(1, sizes.length - 1)} });
        },
        editurl: '/nl/cms/products/SaveProductSize/' + $('#Product_Id').val()
    }).navGrid('#pager', { edit: false, add: true, del: false, search: false });



    $(".SendNewsletter").click(function () {
        var id = $(this).attr("id").replace("SendNewsletter-", "");
        $("#dialog:ui-dialog").dialog("destroy");

        $("#dialog-confirm-" + id).dialog({
            height: 160,
            modal: true,
            buttons: {
                "Doorgaan": function () {
                    $(this).dialog("close");
                    $("#SendNewsletter-" + id).parent("form").submit();
                },
                "Annuleren": function () {
                    $(this).dialog("close");
                }
            }
        });
    });

    $('#mailtemplates').change(function () {
        var selectedid = $(this).val();
        var subject = $('#mailtemplatesubject-' + selectedid).html();
        var body = $('#mailtemplatebody-' + selectedid).html();

        if (subject.length > 0) {
            $('#OrderEmail_Subject').val(subject);
        }
        if (body.length > 0) {
            $('#OrderEmail_Body').val(body);
        }
    });

});

function processAddEdit(response, postdata) {
    alert('');
    var success = true;
    var message = ""
    var json = eval('(' + response.responseText + ')');
    if (json.errors) {
        success = false;
        for (i = 0; i < json.errors.length; i++) {
            message += json.errors[i] + '<br/>';
        }
    }
    var new_id = "1";
    return [success, message, new_id];
}




