/**
 * File:     home.js
 * Created:  Mar 4, 2007
 * Author:   Optaros - Steve Zimmerman
 * Desc:     Event module functionality.
 */

/***********************************************************
 *  Assign events via Behaviour.js
 *  Author: Optaros - Steve Zimmerman
 *  Created:  Mar 4, 2007
 */
var eventsAssign = {
    '#lnkForgot' : function(el){
        el.onclick = function () {
            Modalbox.show(el.href,{
                title:el.title,
                width: 500,
                height: 190,
                afterLoad : function () {
                    initForgotPasswordForm();
                }
            });
            return false;
        }
    },
    '#lnkInvitedBy' : function(el){
        el.onclick = function () {
            Modalbox.show(el.href,{
                title:el.title,
                width:913,
                afterLoad: function() {// display the appropriate form, and set the width line up under the text
                    Liberty.Signup.showDivInvitedBy()  // located in common.js, so the access gate can call this function
                }
            });
            return false;
        }
    },
    '#lnkAbout' : function(el){
        el.onclick = function () {
            Modalbox.show(el.href,{
                title:el.title,
                width:800,
                height: 500,
                afterLoad: function() {
                    Liberty.Signup.initWaitingListLink();
                }
            });
            return false;
        }
    },
    '#lnkWaitList' : function(el){
        el.onclick = function () {
            Modalbox.show(el.href,{
                title:el.title,
                width:913,
                afterLoad: function() { // display the appropriate form, and set the width line up under the text
                    $('divReferredBy').style.display = 'block';
                    $('divBecomeMember').style.display = 'block';
                }
            });
            return false;
        }
    },
    '.joinLink' : function(el){
        el.onclick = function () {
            Modalbox.show(el.href, {title:el.title,
                width:913,
                afterLoad: function() {
                    $('divReferredBy').style.display = 'block';
                    $('divBecomeMember').style.display = 'block';
                }
            });
            return false;
        }
    }
};

/* register the events */
Behaviour.register(eventsAssign);

function checkValue() {
    if(($('txtEmailLogin').value == '') || ($('txtPass').value == '')) {
        new Effect.BlindDown('errorHomeClient', {duration: .2});;
        $('errorHomeClient').innerHTML = 'Both Member Email and Password are required to sign in to the Rue.';
        return false;
    };
}

function formCallback(result, form) {
    if(result == false) {
        if($('homeNotifications')) $('homeNotifications').style.display = 'none';
        $('errorHomeClient').style.display = 'block';
        $('errorHomeClient').innerHTML = "Please enter a valid member email and password to gain access.";
    }
}

var loginValid = null;

function initLoginForm() {
    loginValid = new Validation('loginHome', { onsubmit: false, onFormValidate: formCallback });
  
    if ($('txtEmailLogin').getValue() == '') {
        $('txtEmailLogin').focus();
    } else {
        $('txtPass').focus();
    }
    var notices = $('homeNotifications');
    if (notices && notices.childElements().length > 0) {
        notices.show();
    }
    try {
        if (Liberty.Browser.ie) {
            Event.observe(document, 'keypress', function(e){
                try {
                  	var code, character;
                  	if (!e) var e = window.event;
                  	if (e.keyCode) code = e.keyCode;
                  	else if (e.which) code = e.which;
                  	if (code === 13 && loginValid.validate()) { //enter button
                  	   document.forms.loginHome.submit();
                  	}
              	} catch (e) {}
            });
        }
    } catch (e) {};
}

function initLoginModal(value){
    Modalbox.show(value, {
        title:'&nbsp;', 
        width:600, 
        slideDownDuration:0,
        onShow: function() {
            $('MB_window').style.top = '15%'
        }
    });
}

function initForgotPasswordForm() {
    var forgotPasswordForm = $('frmForgotPassword');
    if (forgotPasswordForm) {
        forgotPasswordForm.observe('submit', function(event) {
            Liberty.Common.toggleBttn('forgotEmailSubmit');
            event.stop();
            if (!window.modalValidator.validate()) {
                Liberty.Common.toggleBttn('forgotEmailSubmit');
            } else {
                forgotPasswordForm.stopObserving('submit');
                new Ajax.Updater(
                  'passwordContainer', 
                  '/access/sendPasswordReminder', 
                  {
                    asynchronous : true, 
                    evalScripts  : true, 
                    onComplete   : function(request, json){
                        if ($('error')) {
                            $('MB_window').setStyle({height: '310px'});
                            $('MB_content').setStyle({height: '290px'});
                        }
                        initForgotPasswordForm();
                    }, 
                    parameters   : Form.serialize(forgotPasswordForm)
                  }
                );
            }
        });
    }
}

