var klix = { };

klix.dialog = {

    _instance: null,

    _getInstance: function() {
        if (klix.dialog._instance == null) {
            document.getElementById('weiterempfehlen-dialog').style.display = 'block';
            klix.dialog._instance = new YAHOO.widget.Dialog('weiterempfehlen-dialog', {
                width : '520px',
                fixedcenter : true,
                visible : false,
                constraintoviewport : true,
                modal: true,
                zIndex: 10,
                buttons : [
                    { text:"Senden", handler: klix.dialog.handleSubmit, isDefault: true },
                    { text:"Abbrechen", handler: klix.dialog.handleCancel }
                ]
            });
            klix.dialog._instance.callback = {
                success: klix.dialog.successCallback,
                failure: function() { }
            };
            klix.dialog._instance.render();
        }
        return klix.dialog._instance;
    },

    show: function()
    {
        klix.dialog._getInstance().show();
        klix.dialog.nameOnKeyUp();
    },

    handleSubmit: function() {
        this.submit();
    },

    handleCancel: function() {
        this.cancel();
    },

    successCallback: function(o) {
        var res = YAHOO.lang.JSON.parse(o.responseText);
        if (res.statusType == 'error') {
            document.getElementById('errorOutput').innerHTML = res.message;
            klix.dialog.show();
        } else {
            mySimpleDialog = new YAHOO.widget.SimpleDialog("successDialog", {
                width: "350px",
                fixedcenter: true,
                modal: true,
                visible: true,
                draggable: false
            });
            mySimpleDialog.setHeader("Weiterempfehlung gesendet");
            mySimpleDialog.setBody(res.message);
            mySimpleDialog.cfg.setProperty("icon", YAHOO.widget.SimpleDialog.ICON_INFO);
            mySimpleDialog.cfg.queueProperty("buttons", [
                { text: 'Ok', handler: function() { this.hide(); } }
            ]);
            mySimpleDialog.render(document.body);

            document.getElementById('weiterempfehlenForm').reset();
        }
    },

    nameOnKeyUp: function()
    {
        var el = document.getElementById('fromName');
        var mailContent = document.getElementById('empfehlenMailContent');
        if (!mailContent.rawContent) {
            mailContent.rawContent = mailContent.innerHTML;
        }
        var newContent = mailContent.rawContent.replace('{$row.from_name}', el.value ? el.value : '...');
        newContent = newContent.replace('{$weiterempfehlenLink}', 'http://www.konicaminolta.at/klix');
        mailContent.innerHTML = newContent;
    }

};

