﻿PageController = function() {
    var handler = {
        config: {
            DefaultPanelID: 4
        },
        DisplayPleaseWait: function(MSG) {
            ITAPanelBox.ActionComplete(handler.config.DefaultPanelID);
            ITAPanelBox.SetProperty('height', '40px');
            ITAPanelBox.ActionStart(handler.config.DefaultPanelID, "Please wait");
            ITAPanelBox.UpdateStatus(MSG);
        },
        initialise: function() {
            try {
                //Initialise any objects;
                ITAPanelBox.Init();
                var n = Dom.get('txtYourName');
                var e = Dom.get('txtYourEmail');
                var m = Dom.get('txtQuickStory');
                Event.on(n, 'focus', function() {
                    if (n.value == "your name...") {
                        n.value = "";
                    }

                }, PageController, true);

                Event.on(e, 'focus', function() {
                    if (e.value == "your email...") {
                        e.value = "";
                    }
                }, PageController, true);

                Event.on(m, 'focus', function() {
                    if (m.value == "Quick story...") {
                        m.value = "";
                    }

                }, PageController, true);


            }
            catch (e) {
                SetStatusMessage("A problem occured initialising page." + e);
            }

        },
        sendStory: {
            send: function() {

                try {
                    var Name = FieldValue("txtYourName", "");
                    var Email = FieldValue("txtYourEmail", "");
                    var Story = FieldValue("txtQuickStory", "");
                    var callback = {
                        success: handler.sendStory.success,
                        failure: handler.sendStory.failure,
                        argument: [Name],
                        scope: this,
                        timeout: WebService.DefaultTimeout
                    };


                    var params = "Email=" + Url.encode(Email) + "&Name=" + Url.encode(Name) + "&Message=" + Url.encode(Story);

                    if ((Name != '' & Name != 'your name...') && (Email != '' & Email != 'your email...')) {
                        handler.DisplayPleaseWait("Sending email...");

                        // Perform the async request
                        WebService.Post(WebService.Methods.Email.SendEmail, callback, params);
                    }
                    else {
                        alert("Please enter your name and a return email when sending a quick note, thank you.");
                    }
                }
                catch (e) {
                    SetStatusMessage("A problem occured locating work item. [" + e + "].");
                }

            },

            success: function(obj) {

            var name = obj.argument[0];
                
                PageController.PanelCancelled();
                alert("Thank you " + name + ", your email has been sent, we will be in contact shortly.");
            },

            failure: function(obj) {
                PageController.PanelCancelled();
            }

        }
    }
    return {
        Variables: handler.config,
        Initialise: handler.initialise,
        SendStory: handler.sendStory.send,
        PanelCancelled: function() { ITAPanelBox.ActionComplete(PageController.Variables.DefaultPanelID); }

    }
} ();

//Initialise On dom load
Event.onDOMReady(PageController.Initialise);
