Figment.Import("Disney.WDPRO.IBC.GuestServices",Figment.getJSRoot() + "modules/");
Figment.Import("Figment.EventHandler",Figment.getJSRoot() + "_framework/");
Figment.Import("Figment.DOM",Figment.getJSRoot() + "_framework/");
Figment.Import("Figment.Net.Request",Figment.getJSRoot() + "_framework/");
Figment.Import("Figment.Cookies",Figment.getJSRoot() + "_framework/");

Figment.Namespace("Disney.WDPRO.IBC.GuestServices.Plugins.Login");

/**
 * The following assumptions will be made regarding the HTML structure of the
 * page:
 * 
 *  - This plug-in container will have:
 *      »   an id called "guest_services_login_plugin_id"
 *          (this id can be changed via the CONSTANTS)
 *  
 *  - Each plug-in state will container will have:
 *      »   an attribute called "plugin-state" identifies the plug-in state
 *          this container is used for
 *          (this attribute name can be changed via the CONSTANTS)
 *      
 *      »   a css class called "guest_services_login_plugin_state"
 *          (this class can be changed via the CONSTANTS)
 *  
 *  - The element that switches the state of this plug-in will have:
 *      »   an attribute called "plugin_state" identifies the plug-in state
 *          this element desires to make active
 *          (this attribute name can be changed via the CONSTANTS)
 *      
 *      »   a css class called "guest_services_login_plugin_change_state_button"
 *          (this is used to attach the events to element to change the 
 *          plug-in state.  You can create your own event for another element
 *          using the "changeState" method, if desired)
 *          (this class can be changed via the CONSTANTS)
 *  
 *  - A hidden input field called "guest_services_login_plugin_id" will be
 *    included in the plug-in and will have it's value set to the id assigned
 *    to the plug-in's container.
 *  
 * Some methods have been exposed as "public" so custom events can be created to
 * access these methods.  The methods are:
 *  
 *  - isLoggedIn()
 *  - logout()
 *  - login()
 *  - lookupAccount()
 *  - changeState()
 *  - defaultState()
 *  - getName()
 *  - addLoginParams()
 *  - removeLoginParam()
 * 
 * All other methods should be treated as "private" and should not be used 
 * outside of this plug-in.
 * 
 * @author Philip Jarrell
 * @since 2.2
 *
 * $Revision: #1 $
 * $Author: ahoyt $
 * $DateTime: 2008/10/17 08:03:58 $
 */
Disney.WDPRO.IBC.GuestServices.Plugins.Login = {

    CONSTANTS: {
        PROCESSOR:                 "en_US/_framework/components/ajaxProcessor",
        PLUGIN_ID:                 "GuestServices_Login_PlugIn",
        CHANGE_STATE_BUTTON_CLASS: "guest_services_login_plugin_change_state_button",
        PLUGIN_STATE_CLASS:        "guest_services_login_plugin_state",
        
        LOGIN_ACTION_BUTTON_ID:    "guest_services_login_action_button",
        LOOKUP_ACTION_BUTTON_ID:   "guest_services_lookup_action_button",
        LOGIN_DESCRIPTION_ID:      "guest_services_login_intro_description",
        MEMBER_NAME_INPUT_ID:      "guest_services_login_member_name",      
        PASSWORD_INPUT_ID:         "guest_services_login_password",     
        DEFAULT_STATE:             "login",
        
        SERVER_ERROR_CODE:         "E1800018",
        
        SERVER_TIMEOUT:            20000    // 20 seconds
    },
    
    _TIMEOUT_ID: null,
    
    _PARAGRAPH_TEXT: null,

    _EXTRA_LOGIN_PARAMS: new Figment.HashTable(),
    
    addLoginParams: function(key, value) {
        Disney.WDPRO.IBC.GuestServices.Plugins.Login._EXTRA_LOGIN_PARAMS.put(key, value);
    },

    removeLoginParam: function(key) {
        Disney.WDPRO.IBC.GuestServices.Plugins.Login._EXTRA_LOGIN_PARAMS.remove(key);
    },
    
    getClassName: function()
    {
        return "Disney.WDPRO.IBC.GuestServices.Plugins.Login";
    },
    
    setToDefaultState: function()
    {
        Disney.WDPRO.IBC.GuestServices.Plugins.Login.setToState(Disney.WDPRO.IBC.GuestServices.Plugins.Login.CONSTANTS.DEFAULT_STATE);
    },

    setToState: function(state) {
        Disney.WDPRO.IBC.GuestServices.Plugins.Login._restoreMessage();
        Disney.WDPRO.IBC.GuestServices.changeState(state, YAHOO.util.Dom.getElementsByClassName(Disney.WDPRO.IBC.GuestServices.Plugins.Login.CONSTANTS.PLUGIN_STATE_CLASS));

    },
        
    isLoggedIn: function()
    {
        // According to WDIG Engineering, if the "BLUE" cookie exists then we 
        // should assume the guest is logged in
        return ( Figment.Cookies.readCookie("BLUE") !== null ) ? true : false;
    },
    
    logout: function(sourceName)
    {
        try
        {
            var fields = new Figment.HashTable();
            fields.put("action","logout");
            
            // Create the new Net requestor
            var requestor = new Figment.Net.Request();
            var callBack = function(requestor)
            {
                window.clearTimeout(Disney.WDPRO.IBC.GuestServices.Plugins.Login._TIMEOUT_ID);
                var source = sourceName;
                var responseXML = Disney.WDPRO.IBC.GuestServices.Plugins.Login._getRequestXML(requestor);
                var eventResponse = Disney.WDPRO.IBC.GuestServices.Plugins.Login._sendEvent(responseXML, "logout", source);
                Disney.WDPRO.IBC.GuestServices.dispatchEvent(eventResponse);
                return;
            };
            var timeOutCallBack = function()
            {
                try
                {
                    requestor.requestor.request.conn.abort();
                } catch(e) {}
                callBack(null);
            };
            Disney.WDPRO.IBC.GuestServices.Plugins.Login._sendRequest(requestor, callBack, timeOutCallBack, fields);
        }
        catch(exception)
        {
            Disney.WDPRO.IBC.GuestServices.Plugins.Login._handleException(exception);
        }
    },
    
    login: function(memberName, password, sourceName)
    {
        try
        {
            var fields = new Figment.HashTable();
            fields.putAll(Disney.WDPRO.IBC.GuestServices.Plugins.Login._EXTRA_LOGIN_PARAMS);
            fields.put("memberName",memberName);
            fields.put("password",password);
            fields.put("action","login");
            
            // Create the new Net requestor
            var requestor = new Figment.Net.Request();
            var callBack = function(requestor)
            {
                window.clearTimeout(Disney.WDPRO.IBC.GuestServices.Plugins.Login._TIMEOUT_ID);
                var source = sourceName;
                var responseXML = Disney.WDPRO.IBC.GuestServices.Plugins.Login._getRequestXML(requestor);
                Disney.WDPRO.IBC.GuestServices.Plugins.Login._handleErrors(responseXML, "login");
                var eventResponse = Disney.WDPRO.IBC.GuestServices.Plugins.Login._sendEvent(responseXML, "login", source);
                if(eventResponse.getResult())
                {
                    Disney.WDPRO.IBC.GuestServices.close();
                } else  { //response false
                    //re-enable login button so guest can try again
                    var loginActionButton = document.getElementById(Disney.WDPRO.IBC.GuestServices.Plugins.Login.CONSTANTS.LOGIN_ACTION_BUTTON_ID);
                    loginActionButton.disabled = false;
                }
                Disney.WDPRO.IBC.GuestServices.Shared.logger.debug("Request failed");
                return;
            };
            var timeOutCallBack = function()
            {
                alert("timeout reached");
                try
                {
                    requestor.requestor.request.conn.abort();
                } catch(e) {}
                callBack(null);
            };
            Disney.WDPRO.IBC.GuestServices.Plugins.Login._sendRequest(requestor, callBack, timeOutCallBack, fields);
        }
        catch(exception)
        {
            Disney.WDPRO.IBC.GuestServices.Plugins.Login._handleException(exception);
        }
    },
    
    lookupAccount: function(memberName, lastName, birthMonth, birthDay, birthYear, sourceName)
    {
        try
        {
            var fields = new Figment.HashTable();
            fields.put("memberName",memberName);
            fields.put("lastName",lastName);
            fields.put("birthMonth",birthMonth);
            fields.put("birthDay",birthDay);
            fields.put("birthYear",birthYear);
            fields.put("action","lookup");
            
            var errors = Disney.WDPRO.IBC.GuestServices.Plugins.Login._validateDate(birthMonth, birthDay, birthYear);
            if(errors.length > 0)
            {
                var xml = Disney.WDPRO.IBC.GuestServices.Plugins.Login._createXMLDocument(errors);
                Disney.WDPRO.IBC.GuestServices.Plugins.Login._handleErrors(xml, "lookup");
                Disney.WDPRO.IBC.GuestServices.Plugins.Login._sendEvent(errors, "lookup", sourceName);
            }
            else
            {
                // Create the new Net requestor
                var requestor = new Figment.Net.Request();
                var callBack = function(requestor)
                {
                    window.clearTimeout(Disney.WDPRO.IBC.GuestServices.Plugins.Login._TIMEOUT_ID);
                    var source = sourceName;
                    var responseXML = Disney.WDPRO.IBC.GuestServices.Plugins.Login._getRequestXML(requestor);
                    Disney.WDPRO.IBC.GuestServices.Plugins.Login._handleErrors(responseXML, "lookup");
                    Disney.WDPRO.IBC.GuestServices.Plugins.Login._changeMessage(responseXML);
                    Disney.WDPRO.IBC.GuestServices.Plugins.Login._sendEvent(responseXML, "lookup", sourceName);
                    Disney.WDPRO.IBC.GuestServices.Shared.logger.debug("Request failed");
                    return;
                };
                var timeOutCallBack = function()
                {
                    try
                    {
                        requestor.requestor.request.conn.abort();
                    } catch(e) {}
                    callBack(null);
                };
                Disney.WDPRO.IBC.GuestServices.Plugins.Login._sendRequest(requestor, callBack, timeOutCallBack, fields);
            }
        }
        catch(exception)
        {
            Disney.WDPRO.IBC.GuestServices.Plugins.Login._handleException(exception);
        }
    },
    
    _validateDate: function(birthMonth, birthDay, birthYear)
    {
        var errors = [];
        if(birthMonth === "" || birthDay === "" || birthYear === "")
        {
            errors.push(Disney.WDPRO.IBC.GuestServices.Plugins.Register.CONSTANTS.INVALID_DATE_ERROR_CODE);
        }
        return errors;
    },
    
    _restoreMessage: function()
    {
        var description = document.getElementById(Disney.WDPRO.IBC.GuestServices.Plugins.Login.CONSTANTS.LOGIN_DESCRIPTION_ID);
        var message = Disney.WDPRO.IBC.GuestServices.Plugins.Login._PARAGRAPH_TEXT;
        if(description !== null && message !== null)
        {
            description.innerHTML = message;
        }
    },
    
    _changeMessage: function(xml)
    {
        var messagesResponse = xml.getElementsByTagName("Paragraph");
        if(messagesResponse.length > 0)
        {
            var message = messagesResponse[0].firstChild.nodeValue.trim();
            if(message !== null)
            {
                var description = document.getElementById(Disney.WDPRO.IBC.GuestServices.Plugins.Login.CONSTANTS.LOGIN_DESCRIPTION_ID);
                if(description !== null)
                {
                    Disney.WDPRO.IBC.GuestServices.Plugins.Login._PARAGRAPH_TEXT = description.innerHTML;
                    description.innerHTML = message;
                    Disney.WDPRO.IBC.GuestServices.changeState("login", YAHOO.util.Dom.getElementsByClassName(Disney.WDPRO.IBC.GuestServices.Plugins.Login.CONSTANTS.PLUGIN_STATE_CLASS));
                }
            }
        }
    },
    
    _getRequestXML: function(requestor)
    {
        var responseXML = null;
        var contentType = null;
        if(requestor !== null)
        {
            contentType = requestor.response.getResponseHeader["Content-Type"];
            if(contentType.indexOf("text/xml") > - 1)
            {
                responseXML = requestor.getResponseXML();
            }
        }
        if(responseXML === null)
        {
            responseXML = Disney.WDPRO.IBC.GuestServices.Plugins.Login._createXMLDocument([Disney.WDPRO.IBC.GuestServices.Plugins.Login.CONSTANTS.SERVER_ERROR_CODE]);
        }
        return responseXML; 
    },
    
    _createXMLDocument: function(errorCodes)
    {
        var doc;
        var xml = "<?xml version=\"1.0\"?>\n";
        xml += "<Message>\n";
            xml += "<Errors id=\"ErrorList\">\n";
            for(var i=0; i < errorCodes.length; i++)
            {
                var errorCode = errorCodes[i];
                var message = Disney.WDPRO.IBC.GuestServices.getError(errorCode);
                if(typeof message === "undefined" || message === null)
                {
                    message = "Unknown error occurred";
                }
                xml += "<Error code=\"" + errorCode + "\">\n";
                    xml += message + "\n";
                xml += "</Error>\n";
            }
            xml += "</Errors>\n";
            xml += "<Response>\n";
                xml += "<Result id=\"result\">\n";
                    xml += "false\n";
                xml += "</Result>\n";
            xml += "</Response>\n";
        xml += "</Message>\n";
        // W3C
        if( document.implementation.createDocument )
        {
            Disney.WDPRO.IBC.GuestServices.Shared.logger.debug("Creating XML document in W3C-compliant browser");
            var parser = new DOMParser();
            doc = parser.parseFromString(xml, "text/xml");
        // MSIE
        }
        else if( window.ActiveXObject )
        {
            Disney.WDPRO.IBC.GuestServices.Shared.logger.debug("Creating XML document in IE browser");
            doc = new ActiveXObject("Microsoft.XMLDOM");
            doc.async = "false";
            doc.loadXML(xml);
        }
        return doc;
    },
    
    _sendEvent: function(xml, eventName, sourceName)
    {
        var eventResponse = new Disney.WDPRO.IBC.GuestServices.Event();
        eventResponse.setEvent(eventName);
        eventResponse.setSource(sourceName);
        var errorsResponse = xml.getElementsByTagName("Error");
        for(var i=0; i < errorsResponse.length; i++)
        {
            var errorResponse = errorsResponse[i];
            var code = errorResponse.getAttribute("code");
            eventResponse.addError(code);
        }
        var results = xml.getElementsByTagName("Result");
        if(results.length > 0)
        {
            var result = results[0].firstChild.nodeValue.trim();
            eventResponse.setResult(result === "true" ? true: false);
        }
        var children = xml.getElementsByTagName("Child");
        if(children.length > 0)
        {
            var child = children[0].firstChild.nodeValue.trim();
            eventResponse.addData("child",child === "true" ? true: false);
        }
        Disney.WDPRO.IBC.GuestServices.dispatchEvent(eventResponse);
        return eventResponse;
    },
    
    _loginEvent: function(e)
    {
        // disable the button after first click:
        var loginActionButton = document.getElementById(Disney.WDPRO.IBC.GuestServices.Plugins.Login.CONSTANTS.LOGIN_ACTION_BUTTON_ID);
        loginActionButton.disabled = true;
        // Set-up id constants used to get the member name and password field
        // elments from the DOM
        var CONSTANTS = {
            MEMBER_NAME_ID: "guest_services_login_member_name",
            PASSWORD_ID: "guest_services_login_password"
        };
        
        var memberName = null;
        var password = null;
        
        // Get the member name form the login state
        var memberNameField = document.getElementById(CONSTANTS.MEMBER_NAME_ID);
        if(memberNameField !== null)
        {
            memberName = memberNameField.value;
        }
        
        // Get the password from the login state
        var passwordField = document.getElementById(CONSTANTS.PASSWORD_ID);
        if(passwordField !== null)
        {
            password = passwordField.value;
        }
        
        var windowEvent = Figment.EventHandler.getEvent(e);
        
        var strHBXPageViewCode = Disney.WDPRO.IBC.GuestServices.getHBXPNCode(windowEvent);
        var strHBXMLCCode = Disney.WDPRO.IBC.GuestServices.getHBXMLCCode(windowEvent);
        var strSCPNCode = Disney.WDPRO.IBC.GuestServices.getSCPNCode(windowEvent);
        var strSCHierCode = Disney.WDPRO.IBC.GuestServices.getSCHierCode(windowEvent);
        if (strHBXPageViewCode !== null && strHBXMLCCode !== null){
            Disney.WDPRO.IBC.GuestServices.EVENT_HBX_Event_PageView(strHBXPageViewCode,strHBXMLCCode,strSCPNCode,strSCHierCode);
        }
        
        var sourceName = Disney.WDPRO.IBC.GuestServices.getSourceName();
        
        // Send the call to login now
        Disney.WDPRO.IBC.GuestServices.Plugins.Login.login(memberName, password, sourceName);
    },
    
    _logoutEvent: function(e)
    {
        var sourceName = Disney.WDPRO.IBC.GuestServices.getSourceName();
        Disney.WDPRO.IBC.GuestServices.Plugins.Login.logout(sourceName);
    },
    
    _lookupAccountEvent: function(e)
    {
        // Set-up id constants used to get the member name and password field
        // elments from the DOM
        var CONSTANTS = {
            MEMBER_NAME_ID: "guest_services_lookup_member_name",
            LAST_NAME_ID: "guest_services_lookup_last_name",
            BIRTH_MONTH_ID: "guest_services_lookup_birth_month",
            BIRTH_DAY_ID: "guest_services_lookup_birth_day",
            BIRTH_YEAR_ID: "guest_services_lookup_birth_year"
        };
        var memberName = null;
        var lastName = null;
        var birthMonth = 0;
        var birthDay = 0;
        var birthYear = 0;
        
        var memberNameField = document.getElementById(CONSTANTS.MEMBER_NAME_ID);
        if(memberNameField !== null)
        {
            memberName = memberNameField.value;
        }
        
        var lastNameField = document.getElementById(CONSTANTS.LAST_NAME_ID);
        if(lastNameField !== null)
        {
            lastName = lastNameField.value;
        }
        
        var birthMonthField = document.getElementById(CONSTANTS.BIRTH_MONTH_ID);
        if(birthMonthField !== null)
        {
            birthMonth = birthMonthField.value;
        }
        
        var birthDayField = document.getElementById(CONSTANTS.BIRTH_DAY_ID);
        if(birthDayField !== null)
        {
            birthDay = birthDayField.value;
        }
        
        var birthYearField = document.getElementById(CONSTANTS.BIRTH_YEAR_ID);
        if(birthYearField !== null)
        {
            birthYear = birthYearField.value;
        }
        
        var windowEvent = Figment.EventHandler.getEvent(e);
        
        var strHBXPageViewCode = Disney.WDPRO.IBC.GuestServices.getHBXPNCode(windowEvent);
        var strHBXMLCCode = Disney.WDPRO.IBC.GuestServices.getHBXMLCCode(windowEvent);
        var strSCPNCode = Disney.WDPRO.IBC.GuestServices.getSCPNCode(windowEvent);
        var strSCHierCode = Disney.WDPRO.IBC.GuestServices.getSCHierCode(windowEvent);
        if (strHBXPageViewCode !== null && strHBXMLCCode !== null){
            Disney.WDPRO.IBC.GuestServices.EVENT_HBX_Event_PageView(strHBXPageViewCode,strHBXMLCCode,strSCPNCode,strSCHierCode);
        }
        
        var sourceName = Disney.WDPRO.IBC.GuestServices.getSourceName();
        
        // Send the call to lookup account now
        Disney.WDPRO.IBC.GuestServices.Plugins.Login.lookupAccount(memberName, lastName, birthMonth, birthDay, birthYear, sourceName);
    },
    
    _changeStateEvent: function(e)
    {
        var event = Figment.EventHandler.getEvent(e);
        var element = event.element;
        var stateName = element.getAttribute(Disney.WDPRO.IBC.GuestServices.CONSTANTS.STATE_ATTRIBUTE_NAME);
        
        var windowEvent = Figment.EventHandler.getEvent(e);
        
        var strHBXPageViewCode = Disney.WDPRO.IBC.GuestServices.getHBXPNCode(windowEvent);
        var strHBXMLCCode = Disney.WDPRO.IBC.GuestServices.getHBXMLCCode(windowEvent);
        var strSCPNCode = Disney.WDPRO.IBC.GuestServices.getSCPNCode(windowEvent);
        var strSCHierCode = Disney.WDPRO.IBC.GuestServices.getSCHierCode(windowEvent);
        if (strHBXPageViewCode !== null && strHBXMLCCode !== null){
            Disney.WDPRO.IBC.GuestServices.EVENT_HBX_Event_PageView(strHBXPageViewCode,strHBXMLCCode,strSCPNCode,strSCHierCode);
        }
        
        if(stateName !== null)
        {
            Disney.WDPRO.IBC.GuestServices.changeState(stateName, YAHOO.util.Dom.getElementsByClassName(Disney.WDPRO.IBC.GuestServices.Plugins.Login.CONSTANTS.PLUGIN_STATE_CLASS));
        }
    },
    
    _handleException: function(exception)
    {
        Disney.WDPRO.IBC.GuestServices.Shared.logger.debug("Exception thrown: " + exception.message);
    },
    
    _handleErrors: function(xml, state)
    {
        var CONSTANTS = {
            ERROR_CODE_LIST_IN_LOGN_STATE_ID: "guest_services_login_error_list",
            ERROR_CODE_LIST_IN_LOOKUP_STATE_ID: "guest_services_lookup_error_list"
        };
        var errorListContainer = null;
        if(state === "login")
        {
            errorListContainer = document.getElementById(CONSTANTS.ERROR_CODE_LIST_IN_LOGN_STATE_ID);
        }
        else if(state === "lookup")
        {
            errorListContainer = document.getElementById(CONSTANTS.ERROR_CODE_LIST_IN_LOOKUP_STATE_ID);
        }
        if(errorListContainer !== null)
        {
            var errorLists = errorListContainer.getElementsByTagName("UL");
            var bAddToDOM = false;
            var errorList = null;
            if(errorLists.length === 0)
            {
                errorList = document.createElement("UL");
                bAddToDOM = true;
            }
            else
            {
                errorList = errorLists[0];
                // Clear the list
                while(errorList.hasChildNodes())
                {
                    errorList.removeChild(errorList.firstChild);
                }
            }
            
            if(errorList !== null && xml !== null)
            {
                var errorsResponse = xml.getElementsByTagName("Error");
                for(var i=0; i < errorsResponse.length; i++)
                {
                    var errorResponse = errorsResponse[i];
                    var code = errorResponse.getAttribute("code");
                    var message = Figment.DOM.getFirstChildElement(errorResponse).nodeValue.trim();
                    if(code === "E1800004") {
		        message = "Last Name does not match our records";
                    }
                    Disney.WDPRO.IBC.GuestServices.Shared.logger.debug("Error: " + code + " ==> " + message);
                    var error = document.createElement("LI");
                    error.appendChild(document.createTextNode(message));
                    errorList.appendChild(error);
                }
            
                if(bAddToDOM)
                {
                    errorListContainer.appendChild(errorList);
                }
            }
        }
    },
    
    getPluginElement: function()
    {
        return document.getElementById(Disney.WDPRO.IBC.GuestServices.Plugins.Login.CONSTANTS.PLUGIN_ID);
    },
    
    _sendRequest: function(requestor, callBack, timeOutCallBack, fields)
    {
        if(requestor !== null)
        {
            Disney.WDPRO.IBC.GuestServices.Plugins.Login._TIMEOUT_ID = window.setTimeout(timeOutCallBack, Disney.WDPRO.IBC.GuestServices.Plugins.Login.CONSTANTS.SERVER_TIMEOUT);
            if(Disney.WDPRO.IBC.GuestServices.Shared.isNotEmptyOrNull(fields))
            {
                var iterator = fields.iterator();
                var field;
                while(iterator.hasNext())
                {
                    field = iterator.next();
                    requestor.addParameter(field.key,field.value);
                }
            }
            
            var plugin = Disney.WDPRO.IBC.GuestServices.Plugins.Login.getPluginElement();
            if(plugin !== null)
            {
                requestor.addParameter("strModuleName",plugin.id);
                // Change the request to be a POST
                requestor.getOptions().setMethod("POST");
                
                // Set the call back method
                if(Disney.WDPRO.IBC.GuestServices.Shared.isNotEmptyOrNull(callBack))
                {
                    requestor.getOptions().setUseCallbacks(true);
                    requestor.setCompleteCallBack( callBack );
                }
                
                // Now send the request
                requestor.send(Figment.getWebRoot() + Disney.WDPRO.IBC.GuestServices.Plugins.Login.CONSTANTS.PROCESSOR);
                return requestor;
            }
            throw new Exception("Plugin not found");
        }
        return null;
    },

    /**
     * Allows users to hit the <ENTER> key to submit the login form.
     * Calls the click event for the login button.
     */
    _submitIfEnterPressed: function(e) {
        
        var keynum = '';
        
        if (window.event) { // IE
            keynum = e.keyCode;
        } else {
            keynum = e.which; // Netscape/FireFox/Opera
        }
        
        if (keynum === 13) {
            document.getElementById(Disney.WDPRO.IBC.GuestServices.Plugins.Login.CONSTANTS.LOGIN_ACTION_BUTTON_ID).click();   
        }
    },

    main: function()
    {
        Disney.WDPRO.IBC.GuestServices.registerPlugin(Disney.WDPRO.IBC.GuestServices.Plugins.Login);
        
        YAHOO.util.Dom.getElementsByClassName(
        		Disney.WDPRO.IBC.GuestServices.Plugins.Login.CONSTANTS.CHANGE_STATE_BUTTON_CLASS, 
        		null, null, function(element) {
                	YAHOO.util.Dom.addClass(element, Disney.WDPRO.IBC.GuestServices.CONSTANTS.PREVENT_ASYNC_FROM_PLEASEWAIT);
                	YAHOO.util.Event.addListener(element,"click",Disney.WDPRO.IBC.GuestServices.Plugins.Login._changeStateEvent);
                });
        
        var loginActionButton = YAHOO.util.Dom.get(Disney.WDPRO.IBC.GuestServices.Plugins.Login.CONSTANTS.LOGIN_ACTION_BUTTON_ID);
        if(loginActionButton !== null)
        {
        	YAHOO.util.Dom.addClass(loginActionButton, Disney.WDPRO.IBC.GuestServices.CONSTANTS.PREVENT_ASYNC_FROM_PLEASEWAIT);
        	YAHOO.util.Event.addListener(loginActionButton, "click", Disney.WDPRO.IBC.GuestServices.Plugins.Login._loginEvent );
        }
        
        var lookupActionButton = YAHOO.util.Dom.get(Disney.WDPRO.IBC.GuestServices.Plugins.Login.CONSTANTS.LOOKUP_ACTION_BUTTON_ID);
        if(lookupActionButton !== null)
        {
        	YAHOO.util.Dom.addClass(lookupActionButton, Disney.WDPRO.IBC.GuestServices.CONSTANTS.PREVENT_ASYNC_FROM_PLEASEWAIT);
        	YAHOO.util.Event.addListener(lookupActionButton, "click", Disney.WDPRO.IBC.GuestServices.Plugins.Login._lookupAccountEvent);
        }

        // Simulate clicking the login button by hitting the ENTER key:
        YAHOO.util.Event.addListener(Disney.WDPRO.IBC.GuestServices.Plugins.Login.CONSTANTS.MEMBER_NAME_INPUT_ID,
        		'keyup', Disney.WDPRO.IBC.GuestServices.Plugins.Login._submitIfEnterPressed);
        YAHOO.util.Event.addListener(Disney.WDPRO.IBC.GuestServices.Plugins.Login.CONSTANTS.PASSWORD_INPUT_ID, 
        		'keyup', Disney.WDPRO.IBC.GuestServices.Plugins.Login._submitIfEnterPressed);
    }
    
};

Figment.EntryPoint.add(Disney.WDPRO.IBC.GuestServices.Plugins.Login);

