/// <reference path="jquery-1.6.1-vsdoc.js" />
/// <reference path="json2.js" />
/// <reference path="knockout-1.2.1.debug.js" />
/// <reference path="knockout.mapping-latest.debug.js" />

var viewModel;
$(function () {


    viewModel = {
        isEditing: ko.observable(false),
        isPaying: ko.observable(false),
        isSearching: ko.observable(false),
        id: ko.observable(''),
        reg: ko.observable(''),
        state: ko.observable('search'),
        isPrintingOfferLetter: ko.observable(false),
        search: function () {
            search();
        },
        edit: function () {
            var args = {
                'id': viewModel.id(),
                'registration': viewModel.reg()
            };
            if (!args.id || !args.registration) {

                $('<div></div').html('sila masukan no daftar anda').dialog({
                    modal: true,
                    title: "Perhatian",
                    buttons: {
                        "OK": function () {
                            $(this).dialog("destroy");
                            $('#registrationTextBox').focus();
                        }
                    }
                });
                return;
            }

            viewModel.isEditing(true);
            $.ajax({
                type: "POST",
                url: "/Application/GetEditApplicationUrl",
                data: JSON.stringify(args),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                error: function (xhr, message, ex) { alert("error : " + xhr.statusText); },
                success: function (msg) {
                    if (msg.OK) {
                        window.location = msg.Url;
                    } else {
                        $('<div></div').html(msg.Message).dialog({
                            modal: true,
                            title: "Perhatian",
                            buttons: {
                                "OK": function () {
                                    $(this).dialog("destroy");
                                    $('#registrationTextBox').focus();
                                }
                            }
                        });
                    }
                    viewModel.isEditing(false);
                } // end success
            }); // end ajax
        },
        pay: function () {
            var registration = viewModel.reg();
            if (!registration) { alert('sila masukan no daftar anda'); return; }
            var args = {
                registration: registration
            };
            viewModel.isPaying(true);
            $.ajax({
                type: "POST",
                url: "/Invoice/ProcessForPayment",
                data: JSON.stringify(args, null, 4),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                error: function (xhr) { alert("error : " + xhr.statusText); },
                success: function (msg) {
                    if (msg.OK) {
                        window.location = msg.Url;
                    } else {
                        $('<div></div').html(msg.Message).dialog({
                            modal: true,
                            title: "Perhatian",
                            buttons: {
                                "OK": function () {
                                    $(this).dialog("destroy");
                                    $('#registrationTextBox').focus();
                                }
                            }
                        });
                        viewModel.isPaying(false);
                    }
                } // end success
            }); // end ajax
        }
    };
    viewModel.canSearch = ko.dependentObservable(function () {
        return this.id() && this.id().length > 5 && !this.isSearching();
    }, viewModel);
    viewModel.canEdit = ko.dependentObservable(function () {
        return this.reg() && this.reg().length == 10 && !this.isEditing();
    }, viewModel);


    viewModel.canEdit.subscribe(function (edit) {
        $('#editLink').button({ disabled: !edit });
    });
    viewModel.isPaying.subscribe(function (pay) {
        $('#depositLink').button({ disabled: pay });
    });
    viewModel.canSearch.subscribe(function (search) {
        $('#searchButton').button({ disabled: !search });
    });
    $('#searchButton').button({ disabled: true });
    $('#editLink').button({ disabled: true });
    ko.applyBindings(viewModel);

    $('#loadingPanel').hide();
    $('#searchPanel').show();
    $(':text').css('width', '250px');

    if ($('#idTextBox').val().length > 0) search();



    $('#printOfferLetterLink').click(function () {
        var registration = $('#registrationTextBox').val();
        if (!registration.length) { alert('sila masukan no daftar anda'); return; }
        var args = {};
        args.registration = registration;

        $('#loadingPanel').show();
        $.ajax({
            type: "POST",
            url: "/Application/GetOfferLetterUrl",
            data: JSON.stringify(args, null, 4),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function (xhr) { alert("error : " + xhr.statusText); },
            success: function (msg) {
                if (msg.OK) {
                    window.location = msg.Url;
                } else {
                    alert(msg.Message);
                }
                $('#loadingPanel').hide();
            } // end success
        }); // end ajax

    });



    $('#idTextBox').focus();
});



function search() {

    var args = {
        id: $('#idTextBox').val()
    };
    if ($('#idTextBox').val().length === 0) {
        alert('sila masukan no kp anda');
        $('#idTextBox').focus();
        return;
    }
    viewModel.isSearching(true);
    $.ajax({
        type: "POST",
        url: "/Application/Search",
        data: JSON.stringify(args, null, 4),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function (xhr, message, ex) { alert("error : " + xhr.statusText); },
        success: function (msg) {
            if (msg.OK) {
                $('#statusLabel').text(msg.Status);
                $('#appMessageLabel').text(msg.Message);
                $('#searchPanel').hide();
                $('#appMessagePanel').slideDown('slow', null);

                if (msg.CanPay) {
                    $('#depositLabel').show();
                    $('#depositAmountLabel').text(msg.DepositAmount);
                }

                if (msg.Quarters.length > 0)
                    $('#quartersLabel').show().html(msg.Quarters);

            } else {
                $('#noticeLabel').text(msg.Message);
                $('#searchButton').button("enabled");
            }
            viewModel.isSearching(false);
            $('#registrationTextBox').focus();
        } // end success
    });   // end ajax

}

