annotate logintets/login_presentation.js @ 5:e91d6f908222 boosterpack

[svn r7] finished!
author robert
date Tue, 08 Sep 2009 06:55:23 -0400
parents 477258d09353
children
rev   line source
robert@0 1 var showingLoggedIn = false;
robert@0 2 var messageElement = false;
robert@0 3 var messageP = false;
robert@0 4
robert@0 5 // focusField method: called when username and password gain focus
robert@0 6 function focusField()
robert@0 7 {
robert@0 8 try {
robert@0 9 if (messageElement != false)
robert@0 10 {
robert@0 11 document.getElementById('message').removeChild(messageElement);
robert@0 12 }
robert@0 13 getSeed();
robert@0 14 }
robert@0 15 catch (e)
robert@0 16 { } // do nothing... hides an apparent firefox bug: https://bugzilla.mozilla.org/show_bug.cgi?id=236791
robert@0 17 }
robert@0 18
robert@0 19 // blurField method: called when username and password are blurred
robert@0 20 function blurField()
robert@0 21 {
robert@0 22 try {
robert@0 23 validateLogin();
robert@0 24 }
robert@0 25 catch (e)
robert@0 26 { } // hide bug https://bugzilla.mozilla.org/show_bug.cgi?id=236791
robert@0 27 }
robert@0 28
robert@0 29 // showLogin method: displays if necessariy that we are logged in
robert@0 30 function showLogin() {
robert@0 31 if (messageElement != false)
robert@0 32 {
robert@0 33 try {
robert@0 34 document.getElementById('message').removeChild(messageElement);
robert@0 35 }
robert@0 36 catch (e) { }
robert@0 37 }
robert@0 38 if (loggedIn)
robert@0 39 {
robert@0 40 showingLoggedIn = true;
robert@0 41
robert@0 42 loginPanel = document.getElementById('login');
robert@0 43 p = document.createElement('p');
robert@0 44 pre = document.createTextNode('Logged in as ');
robert@0 45 strong = document.createElement('strong');
robert@0 46 strong_text = document.createTextNode(fullname);
robert@0 47 strong.appendChild(strong_text);
robert@0 48 mid = document.createTextNode(' [');
robert@0 49 a = document.createElement('a');
robert@0 50 a.href='javascript:logout();';
robert@0 51 a_text = document.createTextNode('logout');
robert@0 52 a.appendChild(a_text);
robert@0 53 post = document.createTextNode(']');
robert@0 54
robert@0 55 p.appendChild(pre);
robert@0 56 p.appendChild(strong);
robert@0 57 p.appendChild(mid);
robert@0 58 p.appendChild(a);
robert@0 59 p.appendChild(post);
robert@0 60
robert@0 61 messageP = document.getElementById('message');
robert@0 62 loginPanel.removeChild(messageP);
robert@0 63 loginPanel.appendChild(p);
robert@0 64
robert@0 65 messageElement = p;
robert@0 66
robert@0 67 document.getElementById('username').disabled = 'disabled';
robert@0 68 document.getElementById('password').disabled = 'disabled';
robert@0 69
robert@0 70 document.getElementById('comments').focus();
robert@0 71 }
robert@0 72 else
robert@0 73 {
robert@0 74 messageElement = document.createElement('strong');
robert@0 75 messageElement.appendChild(document.createTextNode(' ' + messages));
robert@0 76 messageElement.style.color = '#ff0000';
robert@0 77 document.getElementById('message').appendChild(messageElement);
robert@0 78 }
robert@0 79 }
robert@0 80
robert@0 81 // logout method: prepares for a new login
robert@0 82 function logout()
robert@0 83 {
robert@0 84 resetLogin();
robert@0 85 username = document.getElementById('username');
robert@0 86 password = document.getElementById('password');
robert@0 87 loginPanel = document.getElementById('login');
robert@0 88 username.value = '';
robert@0 89 password.value = '';
robert@0 90 username.disabled = null;
robert@0 91 password.disabled = null;
robert@0 92
robert@0 93 loginPanel.removeChild(messageElement);
robert@0 94 loginPanel.appendChild(messageP);
robert@0 95
robert@0 96 messageElement = false;
robert@0 97 showingLoggedIn = false;
robert@0 98
robert@0 99 username.focus();
robert@0 100 }
robert@0 101
robert@0 102 // setupLogin method: to be called on page load, sets up the login script
robert@0 103 function setupLogin()
robert@0 104 {
robert@0 105 username = document.getElementById('username');
robert@0 106 password = document.getElementById('password');
robert@0 107 addEvent(username, 'focus', focusField);
robert@0 108 addEvent(username, 'blur', blurField);
robert@0 109 addEvent(password, 'focus', focusField);
robert@0 110 addEvent(password, 'blur', blurField);
robert@0 111 }
robert@0 112
robert@0 113
robert@0 114 // function to add events to the page in a cross-browser manner
robert@0 115 function addEvent(objObject, strEventName, fnHandler) {
robert@0 116 // DOM-compliant way to add an event listener
robert@0 117 if (objObject.addEventListener) {
robert@0 118 objObject.addEventListener(strEventName, fnHandler, false);
robert@0 119 }
robert@0 120 // IE/windows way to add an event listener
robert@0 121 else if (objObject.attachEvent) {
robert@0 122 objObject.attachEvent('on' + strEventName, fnHandler);
robert@0 123 }
robert@0 124 }
robert@0 125
robert@0 126 addEvent(window,'load',setupLogin);