home_webtop = {
    webBaseUrl: '',
    init: function() {
        $('#forgotPasswordDialog').hide();
        $('#forgotPasswordDialog').dialog({
            autoOpen: false,
            resizable: false,
            width: 450,
            height: 250,
            modal: true,
            buttons: {
                'Send': function() {
                    $('#passwordFailMessage').hide();
                    var email = $('#forgotPasswordEmail').val();
                    home_webtop.sendPassword(email);
                },
                'Cancel': function() {
                    $('#forgotPasswordDialog').dialog('close');
                }
            },
            open: function(event, ui) {
                $('#passwordFailMessage').hide();
            }
        });

        $('#loginDialog').hide();
        $('#loginDialog').dialog({
            autoOpen: false,
            resizable: false,
            width: 450,
            height: 300,
            modal: true,
            buttons: {
                'Sign In': function() {
                    var email = $('#loginPopupEmail').val();
                    var password = $('#loginPopupPassword').val();
                    home_webtop.loginService(email, password);
                },
                'Cancel': function() {
                    $('#loginDialog').dialog('close');
                }
            },
            open: function(event, ui) {
                $('#loginPopupUsername').attr('value','');
                $('#loginPopupPassword').attr('value', '');
                $('#loginError').hide();
            }
        });

        // Fix for #11914 - Add key handlers to catch enter/return to submit form in IE8

        $('#loginPopupEmail, #loginPopupPassword').keydown(function(event) {
            if(event.which == 13) {
                var email = $('#loginPopupEmail').val();
                var password = $('#loginPopupPassword').val();
                home_webtop.loginService(email, password);
                event.preventDefault();
            }
        });

        $('#settingUpDialog').hide();
        $('#settingUpDialog').dialog({
            autoOpen: false,
            resizable: false,
            width: 700,
            height: 300,
            modal: true
        });
        $('#monitoringDialog').hide();
        $('#monitoringDialog').dialog({
            autoOpen: false,
            resizable: false,
            width: 700,
            height: 300,
            modal: true
        });
        $('#optimizingDialog').hide();
        $('#optimizingDialog').dialog({
            autoOpen: false,
            resizable: false,
            width: 700,
            height: 300,
            modal: true
        });
        $('#measuringDialog').hide();
        $('#measuringDialog').dialog({
            autoOpen: false,
            resizable: false,
            width: 700,
            height: 300,
            modal: true
        });
        $('#videoDialog').hide();
        $('#videoDialog').dialog({
            autoOpen: false,
            resizable: false,
            width: 846,
            height: 548,
            modal: true
        });
        $('#settingUpVideoDialog').hide();
        $('#settingUpVideoDialog').dialog({
            autoOpen: false,
            resizable: false,
            width: 846,
            height: 548,
            modal: true
        });
        $('#monitoringVideoDialog').hide();
        $('#monitoringVideoDialog').dialog({
            autoOpen: false,
            resizable: false,
            width: 846,
            height: 548,
            modal: true
        });
        $('#optimizingVideoDialog').hide();
        $('#optimizingVideoDialog').dialog({
            autoOpen: false,
            resizable: false,
            width: 846,
            height: 548,
            modal: true
        });         
        $('#measuringVideoDialog').hide();
        $('#measuringVideoDialog').dialog({
            autoOpen: false,
            resizable: false,
            width: 846,
            height: 548,
            modal: true
        });
        $('#iphoneVideoDialog').hide();
        $('#iphoneVideoDialog').dialog({
            autoOpen: false,
            resizable: false,
            width: 846,
            height: 548,
            modal: true
        });


        $('#forgotPasswordLink').live('click', function(event) {
            $('#loginDialog').dialog('close');
            $('#forgotPasswordDialog').dialog('open');
            $.trackEvent('ForgotPassword', 'click');
            event.preventDefault();
        });
        $('#loginLink').click(function(event) {
            $('#loginDialog').dialog('open');
            $.trackEvent('Login', 'click');
            event.preventDefault();
        });

        $('#loginDialogForm').submit(function(event) {
            event.preventDefault();
        });
        
        $('.logoutLink').click(function(event) {
            home_webtop.logoutService();
            $.trackEvent('Logout', 'click');
            event.preventDefault();
        });

        $('#settingUpLink').click(function(event) {
            $.trackEvent('expand', 'click', 'setup');
            $('#settingUpDialog').dialog('open');
            event.preventDefault();
        });
        $('#monitoringLink').click(function(event) {
            $.trackEvent('expand', 'click', 'monitoring');
            $('#monitoringDialog').dialog('open');
            event.preventDefault();
        });
        $('#optimizingLink').click(function(event) {
            $.trackEvent('expand', 'click', 'optimizing');
            $('#optimizingDialog').dialog('open');
            event.preventDefault();
        });
        $('#measuringLink').click(function(event) {
            $.trackEvent('expand', 'click', 'measuring');
            $('#measuringDialog').dialog('open');
            event.preventDefault();
        });
        $('.videoLink').click(function(event) {
            $.trackEvent('video', 'click', 'overview');
            $('#videoDialog').dialog('open');
            event.preventDefault();
        });
        $('.iphoneVideoLink').click(function(event) {
            $.trackEvent('video', 'click', 'iphone');
            $('#iphoneVideoDialog').dialog('open');
            event.preventDefault();
        });
        $('.setupVideo').click(function(event) {
            $.trackEvent('video', 'click', 'setup');
            $('#settingUpVideoDialog').dialog('open');
            event.preventDefault();
        });
        $('.monitoringVideo').click(function(event) {
            $.trackEvent('video', 'click', 'monitoring');
            $('#monitoringVideoDialog').dialog('open');
            event.preventDefault();
        });
        $('.optimizingVideo').click(function(event) {
            $.trackEvent('video', 'click', 'optimizing');
            $('#optimizingVideoDialog').dialog('open');
            event.preventDefault();
        });
        $('.measuringVideo').click(function(event) {
            $.trackEvent('video', 'click', 'measuring');
            $('#measuringVideoDialog').dialog('open');
            event.preventDefault();
        });

        // init page functions
        $('#trialButton').click(function(event) {
            $.trackEvent('trialButton', 'click');
            home_webtop.requestTrial();
            event.preventDefault();
        });

        $('#createAccountButton').click(function(event) {
            $.trackEvent('createAccountButton', 'click');
            home_webtop.activateAccount();
            event.preventDefault();
        });
    },
    sendPassword: function(email) {
        $.ajax({
            type: 'POST',
            url: 'ws/sendPassword.json',
            data: { 'email' : email },
            dataType: 'json',
            cache: false,
            success: function(data) {
                $('#forgotPasswordDialog').dialog('close');
                alert('Your password has been sent');
            },
            error: function(response) {
                var message = 'There was an error sending your password, please try again later.';
                var errorJson = eval('(' + response.responseText + ')');
                if(errorJson.error == 'noEmail') {
                    message = 'Please enter your email.';
                } else if(errorJson.error == 'noUser') {
                    message = 'Sorry, we can\'t find a user with that email. Please try again.';
                }
                $('#passwordFailMessage').html(message);
                $('#passwordFailMessage').show();
            }
        });
    },
    loginService: function(email, password) {
        $('#loginError').hide();
        $.ajax({
            type: 'POST',
            url: 'ws/login.json',
            data: { 'email': email, 'password': password },
            dataType: 'json',
            cache: false,
            success: function() {
                window.location.href = home_webtop.webBaseUrl;
            },
            error: function(response) {
                var errorJson = eval('(' + response.responseText + ')');
                var errorMessage = home_webtop.getLoginErrorMsg(errorJson.error);
                $('#loginError').html('*** ' + errorMessage + ' ***');
                $('#loginError').show();
            }
        });
    },
    getLoginErrorMsg: function(errorType) {
        var errorMsg;
        if(errorType == 'emptyEmail') {
            errorMsg = 'Please enter your email address.';
        } else if(errorType == 'emptyPassword') {
            errorMsg = 'Please enter your password.';
        } else if(errorType == 'invalidCredentialsError') {
            errorMsg = 'Incorrect email/password combination. Please try again.';
        } else if(errorType == 'authenticationDownError') {
            errorMsg = 'The Twitter API is not responding. Please try again later.';
        } else if(errorType == 'authenticationError') {
            errorMsg = 'There was an error during login. Please try again.';
        } else if(errorType == 'deletedUserError') {
            errorMsg = 'Sorry, this user no longer exists.';
        } else if(errorType == 'accountExists') {
            errorMsg = 'That email address is already registered.';
        } else if(errorType == 'invalidEmail') {
            errorMsg = 'Please enter a valid email address.';
        } else if(errorType == 'invalidPassword') {
            errorMsg = 'Please enter a valid password.';
        } else {
            errorMsg = 'Sorry, there was an unknown error during login. Please try again.';
        }
        return errorMsg;
    },
    logoutService: function() {
        $.ajax({
            type: 'GET',
            url: 'ws/logout.json',
            dataType: 'json',
            cache: false,
            complete: function(data) {
                window.location.href = home_webtop.webBaseUrl;
            }
        });
    },
    requestTrial: function() {
        $('#clientRequestError').hide();
        $('#clientForm .errorTextColor').each(function() {
            $(this).removeClass('errorTextColor');
        });
        var fullName = $('#rtFullName').val();
        var companyName = $('#rtCompanyName').val();
        var title = $('#rtTitle').val();
        var email = $('#rtEmail').val();
        var phoneNumber = $('#rtPhoneNumber').val();
        var notes = $('#rtNotes').val();
        $.ajax({
            type: 'POST',
            url: 'ws/requestTrial.json',
            data: { 'fullName': fullName, 'companyName':companyName, 'title': title, 'email': email, 'phoneNumber': phoneNumber, 'notes': notes },
            dataType: 'json',
            cache: false,
            success: function() {
                window.location.href= 'request_trial_success.htm';
            },
            error: function(response) {
                home_webtop.handleTrialError(response);   
            }
        });
    },
    handleTrialError: function(response) {
        var errorMsg = '';
        if(response.status != 500) {
            var errorJson = eval('(' + response.responseText + ')');
            errorMsg = errorJson.error;
        }

        var msg = '';
        var titleId = '';
        if(errorMsg == 'noName') {
            msg = 'Please enter your full name';
            titleId = 'fnTitle';
        } else if(errorMsg == 'noCompany') {
            msg = 'Please enter your company\'s name';
            titleId = 'cnTitle';
        } else if(errorMsg == 'noTitle') {
            msg = 'Please enter your job title';
            titleId = 'tTitle';
        } else if(errorMsg == 'noEmail' || errorMsg == 'invalidEmail') {
            msg = 'Please enter a valid email';
            titleId = 'eTitle';
        } else if(errorMsg == 'noNumber' || errorMsg == 'invalidNumber') {
            msg = 'Please enter a valid phone number';
            titleId = 'pnTitle';
        } else if(errorMsg == 'noNotes') {
            msg = 'Please enter Twitter accounts';
            titleId = 'nTitle';
        } else if(errorMsg == 'clientExists') {
            msg = 'You have already requested a trial with this email address.';
        } else if(errorMsg == 'cannotSend') {
            msg = 'Sorry, we cannot seem to send a request at this time. Please try again later.';
        } else { 
            msg = 'Sorry, there was an unknown error sending your request. Please try again later.';
        }
        
        if(titleId != '') {
            $('#' + titleId).addClass('errorTextColor');
        }
        $('#clientRequestError').html(msg).show();
    },
    activateAccount: function() {
        $('#activationError').hide();
        $('#activationForm .errorTextColor').each(function() {
            $(this).removeClass('errorTextColor');
        });
        var fullName = $('#fullName').val();
        var email = $('#email').val();
        var password = $('#password').val();
        var confirm = $('#confirm').val();
        var timeZone = $('#timeZone').val();
        var followTap11 = '';
        if($('#followTap11').length > 0) {
            followTap11 = $('#followTap11').attr('checked');
        }
        
        $.ajax({
            type: 'POST',
            url: 'ws/activateAccount.json',
            data: { 'fullName': fullName, 'email': email, 'password': password, 'confirmPassword':confirm, 'timeZoneId':timeZone, 'followTap11': followTap11 },
            dataType: 'json',
            cache: false,
            success: function() {
                window.location.href = 'get_started.htm';
            },
            error: function(response) {
                home_webtop.handleActivateError(response);
            }
        });
    },
    handleActivateError: function(response) {
        var errorMsg = '';
        if(response.status != 500) {
            var errorJson = eval('(' + response.responseText + ')');
            errorMsg = errorJson.error;
        }

        var msg = '';
        var titleId = '';
        if(errorMsg == 'noUser') {
            msg = "You must be logged in to activate your account.";
        } else if(errorMsg == 'alreadyActivated') {
            msg = "You have already activated your account.";
        } else if(errorMsg == 'noName') {
            msg = "Please enter your name.";
            titleId = 'aFullName';
        } else if(errorMsg == 'noEmail' || errorMsg == 'invalidEmail') {
            msg = "Please enter a valid email address.";
            titleId = 'aEmail';
        } else if(errorMsg == 'emailAlreadyInUse') {
            msg = "Sorry, this email address is already in use. Please enter a different email address.";
            titleId = 'aEmail';
        } else if(errorMsg == 'noPassword' || errorMsg == 'minLengthPassword') {
            msg = 'Please enter a password a minimum of 6 characters in length.';
            titleId = 'aPassword';
        } else if(errorMsg == 'noConfirm') {
            msg = "Please confirm your password.";
            titleId = 'aConfirm';
        } else if(errorMsg == 'invalidConfirm') {
            msg = "Your password does not match your confirmation password.";
            $('#aConfirm').addClass('errorTextColor');
            $('#aPassword').addClass('errorTextColor');
        } else if(errorMsg == 'mustChangePassword') {
            msg = "Please choose a new password.";
            titleId = 'aPassword';
        } else if(errorMsg == 'noTimeZone') {
            msg = 'Please select your time zone';
            titleId = 'aTimeZone';
        } else if(errorMsg == 'invalidTimeZone') {
            msg = 'Invalid time zone';
            titleId = 'aTimeZone';
        } else { 
            msg = 'Sorry, there was an unknown error signing in. Please try again later.';
        }
        
        if(titleId != '') {
            $('#' + titleId).addClass('errorTextColor');
        }
        
        $('#activationError').html(msg).show();
    }
};