/*
********************************************************************************************************************************************************************
* Name              :
*
* Description       :
*
* Author(S)         : JSHUMAKER
*
* Creation Date     : 3/12/2007
*
* Version           : 1.0
*
*
* Modifications:
*    MOD ID          Date              Developer Name                   Description
*    ------------    --------------    -----------------------------    ----------------------------------------------------
*
* VSS Info:
*      $Archive:
*      $Author:
*              $Date:
*              $Revision:
*
*  COPYRIGHT 2007 TDCI Inc.
********************************************************************************************************************************************************************
*/

LoginForm = Class.create();
LoginForm.prototype = 
{
    initialize: function(OuterDivID)
    {
	    // state variables
	    

        // object variables
        this.OuterDiv = null;
        this.Forgot = null;
        this.Login = null;
        this.Collapse = null;
        this.TopNotify = null;
        this.BottomNotify = null;
        this.Messages = null;
        this.Username = null;
        this.Password = null;
        

        //Assign references to the object variables
        this.OuterDiv = $(OuterDivID);
        this.ClientID = OuterDivID;
        this.ReferenceStructure();

        //Change VB true/false values to javascript true/false values
        this.ParseInitialValues();

        //Make sure the client on click is cleared
        //  This functionality will be executed during the mouse down event.
        //  This simplifies the event handling and toggling.
        this.OuterDiv.onclick = null;

        this.InitializeButtons();

        return true;
    },

    InitializeButtons: function()
    {
        this.Forgot = eval(this.Forgot);
        this.OuterDiv.OnForgot = this.Forgot.GetClientClick();
        this.Forgot.SetClientClickInContext(this, "this.ClientForgot();");
    
        if (this.Login != null && this.Login.length > 0)
        {
            this.Login = eval(this.Login);
            this.Login.SetClientClickInContext(this, "this.ClientLogin();");
        }
        
        if (this.Collapse != null && this.Collapse.length > 0)
        {
            this.Collapse = eval(this.Collapse);
            this.OuterDiv.OnCollapse = this.Collapse.GetClientClick();
            this.Collapse.SetClientClickInContext(this, "this.CollapseClick();");
        }
    },

    SetLoginInContext: function(Object, Command)
    {
        this.OuterDiv.OnEnabledClick = Command;
        this.OuterDiv.OnEnabledClickInContext = Object;
        this.OuterDiv.OnEnabledClickInContextFunction = function(Username, Password)
        {
            return eval(Command);
        };
        this.OuterDiv.OnEnabledClickInContextPointer = this.OuterDiv.OnEnabledClickInContextFunction.bind(Object);
    },

    SetCollapseInContext: function(Object, Command)
    {
        this.OuterDiv.OnEnabledCollapse = Command;
        this.OuterDiv.OnEnabledCollapseInContext = Object;
        this.OuterDiv.OnEnabledCollapseInContextFunction = function()
        {
            return eval(Command);
        };
        this.OuterDiv.OnEnabledCollapseInContextPointer = this.OuterDiv.OnEnabledCollapseInContextFunction.bind(Object);
    },

    CollapseClick: function()
    {
        if (this.OuterDiv.OnEnabledCollapseInContextPointer != null)
        {
            this.OuterDiv.OnEnabledCollapseInContextPointer();
        }
        else
        {
            eval(this.OuterDiv.OnCollapse);
        }
    },

    ClientLogin: function()
    {
        if (LoadingOn(this.ClientLogin.bind(this)))
        {
            this.ClearNotifications();
            
            var Valid = true;
            var Status = false;
            
            var Username = this.Username.value;
            var Password = this.Password.value;
        
            if (Username.length <= 0)
            {
                Valid = false;
                this.NotifyUsername(true);
            }
            
            if (Password.length <= 0)
            {
                Valid = false;
                this.NotifyPassword(true);
            }
            
            if (Valid)
            {
                if (this.OuterDiv.OnEnabledClickInContextPointer != null)
                {
                    Status = this.OuterDiv.OnEnabledClickInContextPointer(Username, Password);
                }
                else
                {
                    Status = eval(this.OuterDiv.OnLogin);
                }
                
                if (Status == true)
                {
                    Valid = true;
                }
                else
                {
                    this.NotifyLogin();
                }
            }
            
            LoadingOff();
        }
    },
    
    ClientForgot: function()
    {
        this.ClearNotifications();

        var Username = this.Username.value;

        if (Username.length > 0)
        {
            eval(this.OuterDiv.OnForgot);
        }
        else
        {
            this.NotifyUsername(true);
        }
    },
    
    NotifyUsername: function(UseMessage)
    {
        if (Element.hasClassName(this.TopNotify, 'Hidden'))
        {
            Element.toggleClassName(this.TopNotify, 'Hidden');
        }
        
        if (UseMessage)
        {
            this.CompatSetText(this.Messages, this.CompatGetText(this.Messages) + "\n" + this.OuterDiv.InvalidUsername);
        }
    },
    
    NotifyPassword: function(UseMessage)
    {
        if (Element.hasClassName(this.BottomNotify, 'Hidden'))
        {
            Element.toggleClassName(this.BottomNotify, 'Hidden');
        }
        
        if (UseMessage)
        {
            this.CompatSetText(this.Messages, this.CompatGetText(this.Messages) + "\n" + this.OuterDiv.InvalidPassword);
        }
    },
    
    NotifyLogin: function()
    {
        this.NotifyUsername(false);
        this.NotifyPassword(false);
        
        this.CompatSetText(this.Messages, this.CompatGetText(this.Messages) + "\n" + this.OuterDiv.InvalidLogin);
    },
    
    ClearNotifications: function()
    {
        if (!Element.hasClassName(this.TopNotify, 'Hidden'))
        {
            Element.toggleClassName(this.TopNotify, 'Hidden');
        }
        if (!Element.hasClassName(this.BottomNotify, 'Hidden'))
        {
            Element.toggleClassName(this.BottomNotify, 'Hidden');
        }
        
        this.CompatSetText(this.Messages, "");
    },

    GetClientID: function()
    {
        return this.ClientID;
    },


//Setup Helper Functions**************
    ParseInitialValues: function()
    {
        this.Forgot = this.OuterDiv.getAttribute("ForgotPassword");
        this.Login = this.OuterDiv.getAttribute("Login");
        this.Collapse = this.OuterDiv.getAttribute("Collapse");
        this.OuterDiv.InvalidUsername = this.OuterDiv.getAttribute("InvalidUsername");
        this.OuterDiv.InvalidPassword = this.OuterDiv.getAttribute("InvalidPassword");
        this.OuterDiv.InvalidLogin = this.OuterDiv.getAttribute("InvalidLogin");
        this.OuterDiv.OnLogin = this.OuterDiv.getAttribute("OnLogin");
    },
    
    ReferenceStructure: function()
    {
        var Content = null;
        var Field = null;
        Content = this.ChildNodes(this.OuterDiv, 3);
        
        Field = this.ChildNodes(Content, 0);
        this.TopNotify = this.ChildNodes(Field, 0);
        this.Username = this.ChildNodes(Field, 3);
        
        Field = this.ChildNodes(Content, 1);
        this.BottomNotify = this.ChildNodes(Field, 0);
        this.Password = this.ChildNodes(Field, 3);
        
        this.Messages = this.ChildNodes(this.OuterDiv, 4);
    },

//Browser Compatability Helpers**********
    CompatGetText: function(Target)
    {
        if (Target.innerText == null)
        {
            return Target.textContent;
        }
        
        return Target.innerText;
    },
    
    CompatSetText: function(Target, Text)
    {
        if (Target.innerText == null && Target.textContent != null)
        {
            Target.textContent = Text;
        }
        else if (Target.innerText != null && Target.textContent == null)
        {
            Target.innerText = Text;
        }
        else
        {
            Target.innerText = Text;
            Target.textContent = Text;
        }
    },
    
    ChildNodes: function(Object, Index)
    {
        var Pos = 0;
        var RealPos = -1;
        
        for (Pos = 0; Pos < Object.childNodes.length; Pos++)
        {
            if (Object.childNodes.item(Pos).nodeType != 3)
            {
                RealPos++;
            }
            
            if (RealPos == Index)
            {
                return Object.childNodes.item(Pos);
            }
        }
        
        return null;
    },
    
    NumChildNodes: function(Object)
    {
        var Pos = 0;
        var RealPos = 0;
        
        for (Pos = 0; Pos < Object.childNodes.length; Pos++)
        {
            if (Object.childNodes.item(Pos).nodeType != 3)
            {
                RealPos++;
            }
        }
        
        return RealPos;
    },
    
    ValidateEmail: function(value)
    {
        if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(value))
        {
            return true;
        }
        
        return false;
    }
};