﻿var loaderLogic = function(Sys) {
    Sys.require([Sys.components.watermark, Sys.components.modalPopup, Sys.scripts.jQuery], function() {
        Type.registerNamespace("ETG.UI");
        ETG.UI.SubscribeWidget = function(clientID, mode) {
            var _clientId = clientID;
            var _mode = (mode) ? mode : "Popup";
            var _$ = function(id) {
                return _clientId + "_" + id;
            };
            var _$w_getText = function(element) {
                var b = Sys.UI.Behavior.getBehaviorsByType(element, Sys.Extended.UI.TextBoxWatermarkBehavior)[0];
                if (b) {
                    return b.get_Text();
                } else {
                    return '';
                }
            };

            var _$w_setText = function(element, text) {
                var b = Sys.UI.Behavior.getBehaviorsByType(element, Sys.Extended.UI.TextBoxWatermarkBehavior)[0];
                if (b) {
                    return b.set_Text(text);
                } else {
                    return '';
                }
            };

            var _gup = function(name) {
                name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
                var regexS = "[\\?&]" + name + "=([^&#]*)";
                var regex = new RegExp(regexS);
                var results = regex.exec(window.location.href);

                if (results == null)
                    return "";
                else
                    return unescape(results[1]);
            };

            var _ref = function() {
                var temp = _gup("ref");
                if (temp == "") {
                    if (!document.referrer.toLowerCase().match("thinketg.com")) {
                        temp = document.referrer;
                    }
                }
                return temp;
            } ();

            this.subscribeCallback = function(result) {
                if (result) {
                    $find("uiConfirm").show();
                }
            };

            this.listNewsCallback = function(result) {
                var options = '';
                for (var i = 0; i < result.length; i++) {
                    options += '<option value="' + result[i].Value + '">' + result[i].Name + '</option>';
                }
                $(".ddlNewsletter").html(options);
            };

            this.uiPopup_onOkay = function(e) {
                var email = _$w_getText(
                        $(
                            '.txtEmail',
                            $(
                                '#' + _$('uiSubscribePopup')
                            )
                        )[0]
                    );
                var fName = _$w_getText(
                        $(
                            '.txtFirstName',
                            $(
                                '#' + _$('uiSubscribePopup')
                            )
                        )[0]
                    );
                var lName = _$w_getText(
                        $(
                            '.txtLastName',
                            $(
                                '#' + _$('uiSubscribePopup')
                            )
                        )[0]
                    );
                var news = $('.ddlNewsletter', $('#' + _$('uiSubscribePopup')))[0].value;
                email = email.trim();
                var eml = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;

                if (eml.test(email)) {
                    $(".lblError", $("#" + _$("uiSubscribePopup"))).text("");
                    if (_ref == "") {
                        SubscriptionService.SubscribeUser(fName, lName, email, news, this.subscribeCallback, null, this);
                    } else {
                        SubscriptionService.SubscribeUserRef(fName, lName, email, news, _ref, this.subscribeCallback, null, this);
                    }
                } else {
                    $(".lblError", $("#" + _$("uiSubscribePopup"))).text("Please enter a valid email address");
                    if (e.preventDefault) {
                        e.preventDefault();
                    }
                    if (_mode == "Popup") {
                        var btn = $get(_$("btnSubscribe"));
                        if (btn) {
                            var _modal = Sys.UI.Behavior.getBehaviorsByType(btn,
                            Sys.Extended.UI.ModalPopupBehavior)[0];
                            _modal.show();
                        }
                    }
                }
            };

            var _uiPopup_onOkay = Function.createDelegate(this, this.uiPopup_onOkay);

            this.uiPopup_onCancel = function() {
                _$w_setText(
                        $(
                            '.txtEmail',
                            $(
                                '#' + _$('uiSubscribePopup')
                            )
                        )[0],
                        ''
                    );
                _$w_setText(
                        $(
                            '.txtFirstName',
                            $(
                                '#' + _$('uiSubscribePopup')
                            )
                        )[0],
                        ''
                    );
                _$w_setText(
                        $(
                            '.txtLastName',
                            $(
                                '#' + _$('uiSubscribePopup')
                            )
                        )[0],
                        ''
                    );
                $(".lblError", $("#" + _$("uiSubscribePopup"))).text("");
            };
            var _uiPopup_onCancel = Function.createDelegate(this, this.uiPopup_onCancel);
            this.uiPopup_onShowing = function(eventArgs) {
                var email =
                    _$w_getText(
                        $(
                            '.txtEmail',
                            $(
                                '#' + _$('uiSubscribeWidget')
                            )
                        )[0]
                    );
                _$w_setText(
                        $(
                            '.txtEmail',
                            $(
                                '#' + _$('uiSubscribePopup')
                            )
                        )[0],
                        email
                    );

            };
            var _uiPopup_onShowing = Function.createDelegate(this, this.uiPopup_onShowing);

            $(".txtEmail").watermark("Enter your email.");
            $(".txtFirstName").watermark("Enter your first name.");
            $(".txtLastName").watermark("Enter your last name.");
            $("#" + _$("uiSubscribeConfirm")).modalPopup({
                id: "uiConfirm",
                PopupControlID: _$("uiSubscribeConfirm"),
                OKControlID: _$("btnConfirm"),
                CancelControlID: _$("btnConfirm"),
                BackgroundCssClass: "modalBackground"
            });

            $clearHandlers($get(_$("uiSubscribeConfirm")));
            $addHandler($get(_$("btnConfirm")), "click", function() {
                $find("uiConfirm").hide();
            });

            SubscriptionService.ListNewsletters(this.listNewsCallback);

            switch (_mode) {
                case 'Popup':
                    var _popup = $("#" + _$("btnSubscribe")).modalPopup({
                        PopupControlID: _$("uiSubscribePopup"),
                        OkControlID: _$("btnOk"),
                        CancelControlID: _$("btnCancel"),
                        BackgroundCssClass: "modalBackground",
                        OnOkScript: _uiPopup_onOkay,
                        OnCancelScript: _uiPopup_onCancel
                    });
                    var btn = $get(_$("btnSubscribe"));
                    if (btn) {
                        var _modal = Sys.UI.Behavior.getBehaviorsByType(btn,
                            Sys.Extended.UI.ModalPopupBehavior)[0];
                        _modal.add_showing(_uiPopup_onShowing);
                    }

                    break;
                case 'Static':
                    $addHandler($get(_$("btnOk")), "click", _uiPopup_onOkay);
                    $addHandler($get(_$("btnCancel")), "click", _uiPopup_onCancel);
                    _$w_setText(
                        $(
                            '.txtEmail',
                            $(
                                '#' + _$('uiSubscribePopup')
                            )
                        )[0],
                        _gup("email")
                    );
                    break;
            }
        };
    });
};

if (typeof (pageLoad) == 'undefined') {
    pageLoad = function(sender, args) {
        loaderLogic(Sys);
    };
} else {
    var oldPageLoad = pageLoad;
    pageLoad = function(sender, args) {
        oldPageLoad(sender, args);
        loaderLogic(Sys);
    };
}

