﻿var is_ie = (/MSIE (\d+\.\d+);/.test(navigator.userAgent));

EmailAlerts = function() { };

EmailAlerts.prototype.USER_ID = 0;
EmailAlerts.prototype.USER_HAS_PASSWORD = false;

EmailAlerts.prototype.ALERTS_MESSAGE = null;
EmailAlerts.prototype.ALERTS_COUNT = 5;

EmailAlerts.prototype.TXT_SIGNUP_FIRSTNAME = null;
EmailAlerts.prototype.TXT_SIGNUP_LASTNAME = null;
EmailAlerts.prototype.TXT_SIGNUP_EMAIL = null;
EmailAlerts.prototype.TXT_LOGIN_EMAIL = null;
EmailAlerts.prototype.TXT_LOGIN_PASSWORD = null;
EmailAlerts.prototype.TXT_NEW_PASSWORD = null;

var _SERVICE_PATH = '/alerts/email/EmailAlertService.ashx?service=';
EmailAlerts.prototype.SIGNUP_URL = _SERVICE_PATH + 'signup';
EmailAlerts.prototype.LOGIN_URL = _SERVICE_PATH + 'login';
EmailAlerts.prototype.SAVE_URL = _SERVICE_PATH + 'save';
EmailAlerts.prototype.GET_URL = _SERVICE_PATH + 'get_alerts';

EmailAlerts.prototype.init = function() {
    this.ALERTS_MESSAGE = $('alerts-message');

    if (!is_ie)
        Dom.setStyle(this.ALERTS_MESSAGE, 'opacity', '0.0');
    else
        Dom.setStyle(this.ALERTS_MESSAGE, 'visibility', 'hidden');

    this.TXT_SIGNUP_FIRSTNAME = $('txtSignUp_FirstName');
    this.TXT_SIGNUP_LASTNAME = $('txtSignUp_LastName');
    this.TXT_SIGNUP_EMAIL = $('txtSignUp_Email');
    this.TXT_LOGIN_EMAIL = $('txtLogin_Email');
    this.TXT_LOGIN_PASSWORD = $('txtLogin_Password');
    this.TXT_NEW_PASSWORD = $('txtNewPassword');

    this.TXT_LOGIN_EMAIL.value = '';
    this.TXT_LOGIN_PASSWORD.value = '';
    this.TXT_NEW_PASSWORD.value = '';
    this.TXT_SIGNUP_EMAIL.value = '';
    this.TXT_SIGNUP_FIRSTNAME.value = '';
    this.TXT_SIGNUP_LASTNAME.value = '';

    this.goToStep1();
};

EmailAlerts.prototype.btnSignUp_Click = function() {
    if (this.TXT_SIGNUP_EMAIL.value == '' || this.TXT_SIGNUP_FIRSTNAME.value == '' || this.TXT_SIGNUP_LASTNAME == '') {
        this.set_alerts_message('Please enter your first name/ last name and email.');
    }
    else {
        this.set_alerts_message(null);
        
        var ajaxObjectUrl = this.SIGNUP_URL + '&firstname=' + this.TXT_SIGNUP_FIRSTNAME.value;
        ajaxObjectUrl += '&lastname=' + this.TXT_SIGNUP_LASTNAME.value;
        ajaxObjectUrl += '&email=' + this.TXT_SIGNUP_EMAIL.value;

        var ajaxObject = {};
        ajaxObject.scope = this;
        ajaxObject.url = ajaxObjectUrl;
        ajaxObject.success = function() {
            ajaxObject.scope.signup_process(arguments[0].responseText);
        };
        ajax.post(ajaxObject);
    }
    return false;
};

EmailAlerts.prototype.btnLogin_Click = function() {
    if (this.TXT_LOGIN_EMAIL.value == '' || this.TXT_LOGIN_PASSWORD.value == '') {
        this.set_alerts_message('Please enter your username/ password.');
    }
    else {
        this.set_alerts_message(null);

        var ajaxObjectUrl = this.LOGIN_URL + '&email=' + this.TXT_LOGIN_EMAIL.value;
        ajaxObjectUrl += '&password=' + this.TXT_LOGIN_PASSWORD.value;

        var ajaxObject = {};
        ajaxObject.scope = this;
        ajaxObject.url = ajaxObjectUrl;
        ajaxObject.success = function() {
            ajaxObject.scope.login_process(arguments[0].responseText);
        };
        ajax.post(ajaxObject);
    }
    return false;
};

EmailAlerts.prototype.signup_process = function() {
    if (arguments[0] == 'user_exists') {
        this.set_alerts_message('The details you provided already exist in our database. Please login '
            + 'using the form on the right. If you\'ve forgotten your password, please '
            + '<a href="password_reset.aspx?e=' + this.TXT_LOGIN_EMAIL.value + '">click here</a>');
    }
    else {
        this.USER_ID = arguments[0];
        this.USER_HAS_PASSWORD = false;

        var textObj = $('alerts-text-step-2');
        var innerHtml = 'Thanks for registering <strong>' + this.TXT_SIGNUP_FIRSTNAME.value + '</strong>! '
            + 'Now you can choose your alerts. From the list below, check the '
            + 'box next the alert type that you want to subscribe to.';

        textObj.innerHTML = innerHtml;
        this.goToStep2();
    }
};

EmailAlerts.prototype.login_process = function() {
    if (arguments[0] == 'fail') {
        this.set_alerts_message('Your login failed. Please try again. If you\'ve forgotten your '
            + 'password, please <a href="password_reset.aspx?' + this.TXT_LOGIN_EMAIL.value + '">'
            + 'click here</a>');
    }
    else {
        this.USER_ID = arguments[0];
        this.USER_HAS_PASSWORD = true;

        var textObj = $('alerts-text-step-2');
        var innerHtml = 'Welcome back! You can configure the alerts you received by checking/ unchecking '
            + 'the boxes below. When you have finished, simply hit \'SAVE\' at the bottom.';

        textObj.innerHTML = innerHtml;
        this.alert_get_user();
    }
};

EmailAlerts.prototype.btnSaveAlerts_Click = function() {
    var continueSave = true;

    if (!this.USER_HAS_PASSWORD) {
        if (this.TXT_NEW_PASSWORD.value == '') {
            this.set_alerts_message('Please enter a new password.');
            continueSave = false;
        }
        else {
            continueSave = true;
        }
    }

    if (continueSave) {
        this.set_alerts_message(null);

        var ajaxObjectUrl = this.SAVE_URL;
        ajaxObjectUrl += this.alert_get_checked();
        ajaxObjectUrl += '&user_id=' + this.USER_ID;

        if (!this.USER_HAS_PASSWORD)
            ajaxObjectUrl += '&password=' + this.TXT_NEW_PASSWORD.value;

        var ajaxObject = {};
        ajaxObject.scope = this;
        ajaxObject.url = ajaxObjectUrl;
        ajaxObject.success = function() {
            ajaxObject.scope.goToStep3();
        };
        ajax.post(ajaxObject);
    }

    return false;
};

EmailAlerts.prototype.alert_check = function() {
    var checkObj = $('alert-check-' + arguments[0]);

    if (checkObj != null) {
        checkObj.checked = (checkObj.checked ? false : true);
    }

    return false;
};

EmailAlerts.prototype.alert_get_checked = function() {
    var url = '';

    for (var i = 1; i <= this.ALERTS_COUNT; i++) {
        var checkObj = $('alert-check-' + i);
        if (checkObj != null) {
            url += '&alert' + i + '=' + (checkObj.checked);
        }
    }

    return url;
};

EmailAlerts.prototype.alert_get_user = function() {
    var ajaxObject = {};
    ajaxObject.url = this.GET_URL + '&user_id=' + this.USER_ID;
    ajaxObject.scope = this;
    ajaxObject.success = function() {
        if (arguments[0].responseText != '[]') {
            var json = eval("(" + arguments[0].responseText + ")");
            for (var i = 0; i < json.length; i++) {
                var checkObj = $(json[i].alert);
                if (checkObj != null)
                    checkObj.checked = true;
            }
        }
        ajaxObject.scope.goToStep2();
    };
    ajax.post(ajaxObject);
};

EmailAlerts.prototype.set_alerts_message = function() {
    if (arguments[0] != null) {
        this.ALERTS_MESSAGE.innerHTML = '<br />' + arguments[0];
        Dom.setStyle(this.ALERTS_MESSAGE, 'display', 'block');

        if (!is_ie) {
            var animationAttributes = { opacity: { to: 1} };
            var animation = new YAHOO.util.Anim(this.ALERTS_MESSAGE, animationAttributes);

            animation.duration = 0.4;
            animation.animate();
        }
        else {
            Dom.setStyle(this.ALERTS_MESSAGE, 'visibility', 'visible');
        }
    }
    else {
        if (!is_ie)
            Dom.setStyle(this.ALERTS_MESSAGE, 'opacity', '0.0');
        else
            Dom.setStyle(this.ALERTS_MESSAGE, 'visibility', 'hidden');
    }
};

EmailAlerts.prototype.goToStep1 = function() {
    $('alerts-text-step-1').style.display = 'block';
    $('alerts-step-1').style.display = 'block';
};

EmailAlerts.prototype.goToStep2 = function() {
    $('alerts-text-step-1').style.display = 'none';
    $('alerts-text-step-2').style.display = 'block';

    if (!this.USER_HAS_PASSWORD)
        $('alerts-new-password').style.display = 'block';

    this.fadeOut('alerts-step-1');
    this.fadeIn('alerts-step-2');
};

EmailAlerts.prototype.fadeIn = function() {
    var obj = $(arguments[0]);

    if (!is_ie) {
        Dom.setStyle(obj, 'opacity', '0.0');
        Dom.setStyle(obj, 'display', 'block');
    }
    else {
        Dom.setStyle(obj, 'visibility', 'hidden');
    }

    if (!is_ie) {
        var animationAttributes = { opacity: { to: 1} };
        var animation = new YAHOO.util.Anim(obj, animationAttributes);

        animation.duration = 0.4;
        animation.animate();
    }
    else {
        Dom.setStyle(obj, 'visibility', 'visible');
        Dom.setStyle(obj, 'display', 'block');
    }
};

EmailAlerts.prototype.goToStep3 = function() {
    $('alerts-text-step-2').style.display = 'none';

    this.fadeOut('alerts-step-2');

    if (this.USER_HAS_PASSWORD)
        this.fadeIn('alerts-text-step-3');
    else
        this.fadeIn('alerts-text-step-3-new');
};

/* not strictly a fade out, just goes nicely with 'fadeIn' and 'fadeOut' */
EmailAlerts.prototype.fadeOut = function() {
    var obj = $(arguments[0]);
    Dom.setStyle(obj, 'display', 'none');
};