register.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. function smfRegister(formID, passwordDifficultyLevel, regTextStrings)
  2. {
  3. this.addVerify = addVerificationField;
  4. this.autoSetup = autoSetup;
  5. this.refreshMainPassword = refreshMainPassword;
  6. this.refreshVerifyPassword = refreshVerifyPassword;
  7. var verificationFields = new Array();
  8. var verificationFieldLength = 0;
  9. var textStrings = regTextStrings ? regTextStrings : new Array();
  10. var passwordLevel = passwordDifficultyLevel ? passwordDifficultyLevel : 0;
  11. // Setup all the fields!
  12. autoSetup(formID);
  13. // This is a field which requires some form of verification check.
  14. function addVerificationField(fieldType, fieldID)
  15. {
  16. // Check the field exists.
  17. if (!document.getElementById(fieldID))
  18. return;
  19. // Get the handles.
  20. var inputHandle = document.getElementById(fieldID);
  21. var imageHandle = document.getElementById(fieldID + '_img') ? document.getElementById(fieldID + '_img') : false;
  22. var divHandle = document.getElementById(fieldID + '_div') ? document.getElementById(fieldID + '_div') : false;
  23. // What is the event handler?
  24. var eventHandler = false;
  25. if (fieldType == 'pwmain')
  26. eventHandler = refreshMainPassword;
  27. else if (fieldType == 'pwverify')
  28. eventHandler = refreshVerifyPassword;
  29. else if (fieldType == 'username')
  30. eventHandler = refreshUsername;
  31. else if (fieldType == 'reserved')
  32. eventHandler = refreshMainPassword;
  33. // Store this field.
  34. var vFieldIndex = fieldType == 'reserved' ? fieldType + verificationFieldLength : fieldType;
  35. verificationFields[vFieldIndex] = Array(6);
  36. verificationFields[vFieldIndex][0] = fieldID;
  37. verificationFields[vFieldIndex][1] = inputHandle;
  38. verificationFields[vFieldIndex][2] = imageHandle;
  39. verificationFields[vFieldIndex][3] = divHandle;
  40. verificationFields[vFieldIndex][4] = fieldType;
  41. verificationFields[vFieldIndex][5] = inputHandle.className;
  42. // Keep a count to it!
  43. verificationFieldLength++;
  44. // Step to it!
  45. if (eventHandler)
  46. {
  47. createEventListener(inputHandle);
  48. inputHandle.addEventListener('keyup', eventHandler, false);
  49. eventHandler();
  50. // Username will auto check on blur!
  51. inputHandle.addEventListener('blur', autoCheckUsername, false);
  52. }
  53. // Make the div visible!
  54. if (divHandle)
  55. divHandle.style.display = '';
  56. }
  57. // A button to trigger a username search?
  58. function addUsernameSearchTrigger(elementID)
  59. {
  60. var buttonHandle = document.getElementById(elementID);
  61. // Attach the event to this element.
  62. createEventListener(buttonHandle);
  63. buttonHandle.addEventListener('click', checkUsername, false);
  64. }
  65. // This function will automatically pick up all the necessary verification fields and initialise their visual status.
  66. function autoSetup(formID)
  67. {
  68. if (!document.getElementById(formID))
  69. return false;
  70. var curElement, curType;
  71. for (var i = 0, n = document.getElementById(formID).elements.length; i < n; i++)
  72. {
  73. curElement = document.getElementById(formID).elements[i];
  74. // Does the ID contain the keyword 'autov'?
  75. if (curElement.id.indexOf('autov') != -1 && (curElement.type == 'text' || curElement.type == 'password'))
  76. {
  77. // This is probably it - but does it contain a field type?
  78. curType = 0;
  79. // Username can only be done with XML.
  80. if (curElement.id.indexOf('username') != -1 && window.XMLHttpRequest)
  81. curType = 'username';
  82. else if (curElement.id.indexOf('pwmain') != -1)
  83. curType = 'pwmain';
  84. else if (curElement.id.indexOf('pwverify') != -1)
  85. curType = 'pwverify';
  86. // This means this field is reserved and cannot be contained in the password!
  87. else if (curElement.id.indexOf('reserve') != -1)
  88. curType = 'reserved';
  89. // If we're happy let's add this element!
  90. if (curType)
  91. addVerificationField(curType, curElement.id);
  92. // If this is the username do we also have a button to find the user?
  93. if (curType == 'username' && document.getElementById(curElement.id + '_link'))
  94. {
  95. addUsernameSearchTrigger(curElement.id + '_link');
  96. }
  97. }
  98. }
  99. return true;
  100. }
  101. // What is the password state?
  102. function refreshMainPassword(called_from_verify)
  103. {
  104. if (!verificationFields['pwmain'])
  105. return false;
  106. var curPass = verificationFields['pwmain'][1].value;
  107. var stringIndex = '';
  108. // Is it a valid length?
  109. if ((curPass.length < 8 && passwordLevel >= 1) || curPass.length < 4)
  110. stringIndex = 'password_short';
  111. // More than basic?
  112. if (passwordLevel >= 1)
  113. {
  114. // If there is a username check it's not in the password!
  115. if (verificationFields['username'] && verificationFields['username'][1].value && curPass.indexOf(verificationFields['username'][1].value) != -1)
  116. stringIndex = 'password_reserved';
  117. // Any reserved fields?
  118. for (var i in verificationFields)
  119. {
  120. if (verificationFields[i][4] == 'reserved' && verificationFields[i][1].value && curPass.indexOf(verificationFields[i][1].value) != -1)
  121. stringIndex = 'password_reserved';
  122. }
  123. // Finally - is it hard and as such requiring mixed cases and numbers?
  124. if (passwordLevel > 1)
  125. {
  126. if (curPass == curPass.toLowerCase())
  127. stringIndex = 'password_numbercase';
  128. if (!curPass.match(/(\D\d|\d\D)/))
  129. stringIndex = 'password_numbercase';
  130. }
  131. }
  132. var isValid = stringIndex == '' ? true : false;
  133. if (stringIndex == '')
  134. stringIndex = 'password_valid';
  135. // Set the image.
  136. setVerificationImage(verificationFields['pwmain'][2], isValid, textStrings[stringIndex] ? textStrings[stringIndex] : '');
  137. verificationFields['pwmain'][1].className = verificationFields['pwmain'][5] + ' ' + (isValid ? 'valid_input' : 'invalid_input');
  138. // As this has changed the verification one may have too!
  139. if (verificationFields['pwverify'] && !called_from_verify)
  140. refreshVerifyPassword();
  141. return isValid;
  142. }
  143. // Check that the verification password matches the main one!
  144. function refreshVerifyPassword()
  145. {
  146. // Can't do anything without something to check again!
  147. if (!verificationFields['pwmain'])
  148. return false;
  149. // Check and set valid status!
  150. var isValid = verificationFields['pwmain'][1].value == verificationFields['pwverify'][1].value && refreshMainPassword(true);
  151. var alt = textStrings[isValid == 1 ? 'password_valid' : 'password_no_match'] ? textStrings[isValid == 1 ? 'password_valid' : 'password_no_match'] : '';
  152. setVerificationImage(verificationFields['pwverify'][2], isValid, alt);
  153. verificationFields['pwverify'][1].className = verificationFields['pwverify'][5] + ' ' + (isValid ? 'valid_input' : 'invalid_input');
  154. return true;
  155. }
  156. // If the username is changed just revert the status of whether it's valid!
  157. function refreshUsername()
  158. {
  159. if (!verificationFields['username'])
  160. return false;
  161. // Restore the class name.
  162. if (verificationFields['username'][1].className)
  163. verificationFields['username'][1].className = verificationFields['username'][5];
  164. // Check the image is correct.
  165. var alt = textStrings['username_check'] ? textStrings['username_check'] : '';
  166. setVerificationImage(verificationFields['username'][2], 'check', alt);
  167. // Check the password is still OK.
  168. refreshMainPassword();
  169. return true;
  170. }
  171. // This is a pass through function that ensures we don't do any of the AJAX notification stuff.
  172. function autoCheckUsername()
  173. {
  174. checkUsername(true);
  175. }
  176. // Check whether the username exists?
  177. function checkUsername(is_auto)
  178. {
  179. if (!verificationFields['username'])
  180. return false;
  181. // Get the username and do nothing without one!
  182. var curUsername = verificationFields['username'][1].value;
  183. if (!curUsername)
  184. return false;
  185. if (!is_auto)
  186. ajax_indicator(true);
  187. // Request a search on that username.
  188. checkName = curUsername.php_to8bit().php_urlencode();
  189. getXMLDocument(smf_prepareScriptUrl(smf_scripturl) + 'action=register;sa=usernamecheck;xml;username=' + checkName, checkUsernameCallback);
  190. return true;
  191. }
  192. // Callback for getting the username data.
  193. function checkUsernameCallback(XMLDoc)
  194. {
  195. if (XMLDoc.getElementsByTagName("username"))
  196. isValid = XMLDoc.getElementsByTagName("username")[0].getAttribute("valid");
  197. else
  198. isValid = true;
  199. // What to alt?
  200. var alt = textStrings[isValid == 1 ? 'username_valid' : 'username_invalid'] ? textStrings[isValid == 1 ? 'username_valid' : 'username_invalid'] : '';
  201. verificationFields['username'][1].className = verificationFields['username'][5] + ' ' + (isValid == 1 ? 'valid_input' : 'invalid_input');
  202. setVerificationImage(verificationFields['username'][2], isValid == 1, alt);
  203. ajax_indicator(false);
  204. }
  205. // Set the image to be the correct type.
  206. function setVerificationImage(imageHandle, imageIcon, alt)
  207. {
  208. if (!imageHandle)
  209. return false;
  210. if (!alt)
  211. alt = '*';
  212. var curImage = imageIcon ? (imageIcon == 'check' ? 'field_check.gif' : 'field_valid.gif') : 'field_invalid.gif';
  213. imageHandle.src = smf_images_url + '/icons/' + curImage;
  214. imageHandle.alt = alt;
  215. imageHandle.title = alt;
  216. return true;
  217. }
  218. }
  219. function updateAuthMethod()
  220. {
  221. // What authentication method is being used?
  222. if (!document.getElementById('auth_openid') || !document.getElementById('auth_openid').checked)
  223. currentAuthMethod = 'passwd';
  224. else
  225. currentAuthMethod = 'openid';
  226. // No openID?
  227. if (!document.getElementById('auth_openid'))
  228. return true;
  229. document.forms.registration.openid_url.disabled = currentAuthMethod == 'openid' ? false : true;
  230. document.forms.registration.smf_autov_pwmain.disabled = currentAuthMethod == 'passwd' ? false : true;
  231. document.forms.registration.smf_autov_pwverify.disabled = currentAuthMethod == 'passwd' ? false : true;
  232. document.getElementById('smf_autov_pwmain_div').style.display = currentAuthMethod == 'passwd' ? '' : 'none';
  233. document.getElementById('smf_autov_pwverify_div').style.display = currentAuthMethod == 'passwd' ? '' : 'none';
  234. if (currentAuthMethod == 'passwd')
  235. {
  236. verificationHandle.refreshMainPassword();
  237. verificationHandle.refreshVerifyPassword();
  238. document.forms.registration.openid_url.style.backgroundColor = '';
  239. document.getElementById('password1_group').style.display = '';
  240. document.getElementById('password2_group').style.display = '';
  241. document.getElementById('openid_group').style.display = 'none';
  242. }
  243. else
  244. {
  245. document.forms.registration.smf_autov_pwmain.style.backgroundColor = '';
  246. document.forms.registration.smf_autov_pwverify.style.backgroundColor = '';
  247. document.forms.registration.openid_url.style.backgroundColor = '#FFF0F0';
  248. document.getElementById('password1_group').style.display = 'none';
  249. document.getElementById('password2_group').style.display = 'none';
  250. document.getElementById('openid_group').style.display = '';
  251. }
  252. return true;
  253. }
  254. function onCheckChange()
  255. {
  256. if (document.forms.postForm.emailActivate.checked || document.forms.postForm.password.value == '')
  257. {
  258. document.forms.postForm.emailPassword.disabled = true;
  259. document.forms.postForm.emailPassword.checked = true;
  260. }
  261. else
  262. document.forms.postForm.emailPassword.disabled = false;
  263. }