﻿/**
* This class is used in conjunction with the Onsharp.Web.UI.Controls.LogOnInterface class in the Onsharp.dll versions 1.9 and higher.
*
* Classes included in this file:
*     LogOnInterface
* This file requires the use of the following files:
*     base.js
*     base_classes.js
*     cls_ajax.js
*     cls_java_window.js
*     cls_ajax_processor_base.js
**/
function LogOnInterface(p_instanceName, p_ajax, p_id, p_redirectToOnLogOn)
{
    var v_url = Uri.Load();
    LogOnInterface.baseConstructor.call(this, p_instanceName, p_ajax, v_url.Path + v_url.File, p_redirectToOnLogOn);
    this.Id = p_id;
    return;
}
Extend(LogOnInterface, AjaxProcessorBase);
LogOnInterface.prototype.GetType = function() { return 'LogOnInterface'; }
LogOnInterface.prototype.DoLogOn = function(p_form)
{
    var v_redirect = true;
    var v_query = QueryString.Load();
    if(v_query.Contains('redirect')) { v_redirect = new Uri(decodeURIComponent(v_query.Item('redirect'))); }
    this.ShowStatusWindow('Please wait while the system attempts to log you in...', true);
    this.Execute(QueryString.Create({mode:'go',name:this.Name,username:p_form.username.value,password:p_form.password.value }), false, v_redirect, true, this.Id + '_login_request');
    return;
}
LogOnInterface.prototype.ShowStatusWindow = function(p_text, p_showLoadImage)
{
    if(p_showLoadImage) { p_text += '<br />' + this.GetLoadImage('padding-top: 5px;'); }
    var v_login_div = document.getElementById(this.Id);
    var v_status_text = document.getElementById(this.Id + '_login_error_text');
    v_login_div.style.cursor = 'wait';
    v_status_text.className = 'status';
    v_status_text.style.display = 'block';
    v_status_text.innerHTML = p_text;
    return;
}
LogOnInterface.prototype.ShowErrorWindow = function(p_text, p_object)
{
    var v_login_div = document.getElementById(this.Id);
    var v_status_text = document.getElementById(this.Id + '_login_error_text');
    v_login_div.style.cursor = 'default';
    v_status_text.className = 'error';
    v_status_text.style.display = 'block';
    v_status_text.innerHTML = p_text;
    return;
}
LogOnInterface.prototype.ShowForgotPasswordForm = function(p_by_un)
{
    var v_win = this.ShowWindow(350, 300, false, true, 'Password Reminder', '<div style="text-align: center; padding-top: 20px;">Please wait while the content is loaded...<br />' + this.GetLoadImage('padding-top: 5px;') + '</div>', true, false, null);
    if (p_by_un)
    {
        this.Execute(QueryString.Create({ mode: 'show_fp_form', name: this.Name, p_by_un: true }), false, __callback, true, this.Id + '_login_forgot_password_form', null);
    }
    else
    {
        this.Execute(QueryString.Create({ mode: 'show_fp_form', name: this.Name }), false, __callback, true, this.Id + '_login_forgot_password_form', null);
    } 
    function __callback(p_returnObject, p_identifier)
    {
        p_returnObject.CanClose = false;
        v_win.SetContent(p_returnObject.Html);
    }
    return;
}
LogOnInterface.prototype.SubmitForgotPassword = function(p_form, p_by_un)
{
    if (p_form.user_email != null)
    {
        var email = p_form.user_email.value;
        var userName = null;
    }
    if (p_form.user_name != null)
    {
        var email = null;
        var userName = p_form.user_name.value;
    }

    if (p_by_un && userName.length <= 0)
        {
            alert('Please enter your user name.'); 
            return;
        }
        else if (email != null && email.length <= 0)
        {
            alert('Please enter your email address.'); 
            return;
        }

        if (!p_by_un && !Utils.IsEmail(email))
        {
            alert('The email address you entered is not a valid email address.'); 
            return; 
        }

        var v_win = this.Window;
        this.Window.SetCursor('wait');
        this.Window.StatusBar.SetText(this.GetLoadImage() + ' Looking up your information...');
        if (p_by_un)
        {
            this.Execute(QueryString.Create({ mode: 'password_lookup_username', name: this.Name, user_name: userName }), false, __callback, true, this.Id + '_login_password_lookup', __fail);
        }
        else
        {
            this.Execute(QueryString.Create({ mode: 'password_lookup', name: this.Name, user_email: email }), false, __callback, true, this.Id + '_login_password_lookup', __fail);
        }
        function __callback(p_returnObject, p_identifier)
        {
            p_returnObject.CanClose = false;
            v_win.SetContent(p_returnObject.Html);
            v_win.StatusBar.SetText('');
            v_win.SetCursor('default');
        }
        function __fail(p_returnObject, p_identifier)
        {
            v_win.ResizeTo(400, 300);
            v_win.SetContent(p_returnObject);
            v_win.StatusBar.SetText('');
            v_win.SetCursor('default');
            return false;
        }
    return;
}
