1<!-- lwsgs rewrites the below $vars $v $v into the correct values on the fly -->
2
3var lwsgs_user = "$lwsgs_user";
4var lwsgs_auth = "$lwsgs_auth";
5var lwsgs_email = "$lwsgs_email";
6
7var lwsgs_html = '\
8	<div id="dlogin" class="hidden"> \
9        <form action="lwsgs-login" method="post"> \
10         <input type="hidden" name="admin" value="needadmin/admin-login.html"> \
11         <input type="hidden" name="good" value="index.html"> \
12         <input type="hidden" name="bad" value="failed-login.html"> \
13         <input type="hidden" name="forgot-good" value="sent-forgot-ok.html"> \
14         <input type="hidden" name="forgot-bad" value="sent-forgot-fail.html">\
15         <input type="hidden" name="forgot-post-good" value="post-forgot-ok.html">\
16         <input type="hidden" name="forgot-post-bad" value="post-forgot-fail.html">\
17	 <table class="r">\
18          <tr>\
19           <td>User Name\
20            <input type="text" size="10" id="username" name="username"></td>\
21           <td>Password\
22            <input type="password" id="password" size="10" name="password"><div id="pw1"></div></td>\
23	     </tr><tr>\
24	   <td colspan="2" class="c">\
25	<input type="submit" id="login" name="login" value="Login" class="em">\
26      &nbsp;<input type="submit" id="forgot" name="forgot" value="Forgot password">\
27           &nbsp;<input id="doreg" type="button" value="Sign up"></td>\
28          </tr>\
29         </table>\
30        </form>\
31       </div>\
32\
33       <div id="dlogout" class="hiddenr">\
34        <form action="lwsgs-logout" method="post" class="r">\
35         <input type="hidden" name="good" value="index.html">\
36         <table class="r">\
37          <tr><td><table><tr><td><span id=grav></span></td></tr><tr><td>\
38	<a href="#" id="clink">\
39	<span id="curuser"></span></a></td></tr></table></td>\
40           <td class="tac"><input type="submit" name="logout" value="Logout"></td>\
41          </tr></table></td></tr>\
42         </table>\
43        </form></div>\
44\
45	<div id="dregister" class="hidden">\
46	 <form action="lwsgs-login" method="post">\
47	  <input type="hidden" name="admin" value="needadmin/admin-login.html">\
48	  <input type="hidden" name="good" value="successful-login.html">\
49	  <input type="hidden" name="bad" value="failed-login.html">\
50	  <input type="hidden" name="reg-good" value="post-register-ok.html">\
51	  <input type="hidden" name="reg-bad" value="post-register-fail.html">\
52	  <input type="hidden" name="forgot-good" value="sent-forgot-ok.html">\
53	  <input type="hidden" name="forgot-bad" value="sent-forgot-fail.html">\
54	  <input type="hidden" name="forgot-post-good" value="post-forgot-ok.html">\
55	  <input type="hidden" name="forgot-post-bad" value="post-forgot-fail.html">\
56	  <table class="l">\
57	     <tr>\
58	      <td colspan=2 align=center>\
59		<span id="curuser"></span>\
60	       <b>Please enter your details to register</b>:\
61	      </td>\
62	     </tr>\
63	    <tr><td align=right>\
64	     User Name:</td>\
65	     <td><input type="text" size="10" id="rusername" name="username" &nbsp;<span id=uchk></span></td>\
66	    </tr>\
67	    <tr>\
68	     <td align=right>Password:</td>\
69	     <td><input type="password" size="10" id="rpassword" name="password">&nbsp;<span id="rpw1"></span></td>\
70	    </tr>\
71	    <tr>\
72	    </tr>\
73	    <tr>\
74	     <td align=right><span id="pw2">Password (again):</span></td>\
75	     <td><input type="password" size="10" id="password2" name="password2">&nbsp;<span id="match"></span></td>\
76	    </tr>\
77	    <tr>\
78	     <td align=right>Email:</td>\
79	     <td><input type="email" size="10" id="email" name="email"\
80	          placeholder="me@example.com" &nbsp;<span id=echk></span></td>\
81	    </tr>\
82	    <tr>\
83	     <td colspan=2 align=center>\
84<input type="submit" id="register" name="register" value="Register" >\
85<input type="submit" id="rforgot" name="forgot" value="Forgot Password" class="hidden">\
86<input type="button" id="cancel" name="cancel" value="Cancel">\
87	     </td>\
88	    </tr>\
89         </table>\
90        </form>\
91       </div>\
92       \
93       <div id="dchange" class="hidden">\
94        <form action="lwsgs-change" method="post">\
95         <input type="hidden" id="cusername" name="username">\
96         <input type="hidden" name="admin" value="needadmin/admin-login.html">\
97         <input type="hidden" name="good" value="index.html">\
98         <input type="hidden" name="bad" value="failed-login.html">\
99         <input type="hidden" name="reg-good" value="post-register-ok.html">\
100         <input type="hidden" name="reg-bad" value="post-register-fail.html">\
101         <input type="hidden" name="forgot-good" value="sent-forgot-ok.html">\
102         <input type="hidden" name="forgot-bad" value="sent-forgot-fail.html">\
103         <input type="hidden" name="forgot-post-good" value="post-forgot-ok.html">\
104         <input type="hidden" name="forgot-post-bad" value="post-forgot-fail.html">\
105         <table class="l">\
106	     <tr>\
107	      <td colspan=2 align=center>\
108		<span id="ccuruser"></span>\
109	       <b>Please enter your details to change</b>:\
110	      </td>\
111	     </tr>\
112	    <tr><td align=right id="ccurpw_name">\
113	     Current Password:</td>\
114	     <td><input type="password" size="10" id="ccurpw" name="curpw"\
115	         >&nbsp;<span id=cuchk></span></td>\
116	    </tr>\
117	    <tr>\
118	     <td align=right>Password:</td>\
119	     <td><input type="password" size="10" id="cpassword" name="password"\
120	          &nbsp;<span id="cpw1"></span></td>\
121	    </tr>\
122	    <tr>\
123	     <td align=right><span id="pw2">Password (again)</span></td>\
124	     <td><input type="password" size="10" id="cpassword2" name="password2"\
125	         >&nbsp;<span id="cmatch"></span></td>\
126	    </tr>\
127	<!-- not supported yet\
128	    <tr>\
129	     <td align=right id="cemail_name">Email:</td>\
130	     <td><input type="email" size="10" id="cemail" name="email"\
131	     	  placeholder="?" \
132	     	  &nbsp;<span id=cechk></span></td>\
133	    </tr> -->\
134	    <tr>\
135	     <td colspan=2 align=center>\
136	      <input type="submit" id="change" name="change"\
137	       value="Change" class="wide">\
138	      <input type="submit" id="cforgot" name="forgot"\
139	       value="Forgot Password" class="wide hidden">\
140	      <input type="button" id="cancel2" name="cancel"\
141	       value="Cancel" class="wide">\
142	     </td>\
143	    </tr>\
144	    <tr>\
145	     <td colspan=2>\
146	      <input type="checkbox" id="showdel" name="showdel"\
147	       > Show Delete&nbsp;\
148	      <input type="submit" id="delete" name="delete" \
149	       value="Delete Account" class="wide hidden">\
150	     </td>\
151	    </tr>\
152         </table>\
153        </form>\
154       </div>\
155       \
156       <div id="dadmin" class="hidden">\
157         Admin settings TBD\
158       </div>\
159';
160
161/*-- this came from
162  -- https://raw.githubusercontent.com/blueimp/JavaScript-MD5/master/js/md5.min.js
163  -- under MIT license */
164!function(n){"use strict";function t(n,t){var r=(65535&n)+(65535&t),e=(n>>16)+(t>>16)+(r>>16);return e<<16|65535&r}function r(n,t){return n<<t|n>>>32-t}function e(n,e,o,u,c,f){return t(r(t(t(e,n),t(u,f)),c),o)}function o(n,t,r,o,u,c,f){return e(t&r|~t&o,n,t,u,c,f)}function u(n,t,r,o,u,c,f){return e(t&o|r&~o,n,t,u,c,f)}function c(n,t,r,o,u,c,f){return e(t^r^o,n,t,u,c,f)}function f(n,t,r,o,u,c,f){return e(r^(t|~o),n,t,u,c,f)}function i(n,r){n[r>>5]|=128<<r%32,n[(r+64>>>9<<4)+14]=r;var e,i,a,h,d,l=1732584193,g=-271733879,v=-1732584194,m=271733878;for(e=0;e<n.length;e+=16)i=l,a=g,h=v,d=m,l=o(l,g,v,m,n[e],7,-680876936),m=o(m,l,g,v,n[e+1],12,-389564586),v=o(v,m,l,g,n[e+2],17,606105819),g=o(g,v,m,l,n[e+3],22,-1044525330),l=o(l,g,v,m,n[e+4],7,-176418897),m=o(m,l,g,v,n[e+5],12,1200080426),v=o(v,m,l,g,n[e+6],17,-1473231341),g=o(g,v,m,l,n[e+7],22,-45705983),l=o(l,g,v,m,n[e+8],7,1770035416),m=o(m,l,g,v,n[e+9],12,-1958414417),v=o(v,m,l,g,n[e+10],17,-42063),g=o(g,v,m,l,n[e+11],22,-1990404162),l=o(l,g,v,m,n[e+12],7,1804603682),m=o(m,l,g,v,n[e+13],12,-40341101),v=o(v,m,l,g,n[e+14],17,-1502002290),g=o(g,v,m,l,n[e+15],22,1236535329),l=u(l,g,v,m,n[e+1],5,-165796510),m=u(m,l,g,v,n[e+6],9,-1069501632),v=u(v,m,l,g,n[e+11],14,643717713),g=u(g,v,m,l,n[e],20,-373897302),l=u(l,g,v,m,n[e+5],5,-701558691),m=u(m,l,g,v,n[e+10],9,38016083),v=u(v,m,l,g,n[e+15],14,-660478335),g=u(g,v,m,l,n[e+4],20,-405537848),l=u(l,g,v,m,n[e+9],5,568446438),m=u(m,l,g,v,n[e+14],9,-1019803690),v=u(v,m,l,g,n[e+3],14,-187363961),g=u(g,v,m,l,n[e+8],20,1163531501),l=u(l,g,v,m,n[e+13],5,-1444681467),m=u(m,l,g,v,n[e+2],9,-51403784),v=u(v,m,l,g,n[e+7],14,1735328473),g=u(g,v,m,l,n[e+12],20,-1926607734),l=c(l,g,v,m,n[e+5],4,-378558),m=c(m,l,g,v,n[e+8],11,-2022574463),v=c(v,m,l,g,n[e+11],16,1839030562),g=c(g,v,m,l,n[e+14],23,-35309556),l=c(l,g,v,m,n[e+1],4,-1530992060),m=c(m,l,g,v,n[e+4],11,1272893353),v=c(v,m,l,g,n[e+7],16,-155497632),g=c(g,v,m,l,n[e+10],23,-1094730640),l=c(l,g,v,m,n[e+13],4,681279174),m=c(m,l,g,v,n[e],11,-358537222),v=c(v,m,l,g,n[e+3],16,-722521979),g=c(g,v,m,l,n[e+6],23,76029189),l=c(l,g,v,m,n[e+9],4,-640364487),m=c(m,l,g,v,n[e+12],11,-421815835),v=c(v,m,l,g,n[e+15],16,530742520),g=c(g,v,m,l,n[e+2],23,-995338651),l=f(l,g,v,m,n[e],6,-198630844),m=f(m,l,g,v,n[e+7],10,1126891415),v=f(v,m,l,g,n[e+14],15,-1416354905),g=f(g,v,m,l,n[e+5],21,-57434055),l=f(l,g,v,m,n[e+12],6,1700485571),m=f(m,l,g,v,n[e+3],10,-1894986606),v=f(v,m,l,g,n[e+10],15,-1051523),g=f(g,v,m,l,n[e+1],21,-2054922799),l=f(l,g,v,m,n[e+8],6,1873313359),m=f(m,l,g,v,n[e+15],10,-30611744),v=f(v,m,l,g,n[e+6],15,-1560198380),g=f(g,v,m,l,n[e+13],21,1309151649),l=f(l,g,v,m,n[e+4],6,-145523070),m=f(m,l,g,v,n[e+11],10,-1120210379),v=f(v,m,l,g,n[e+2],15,718787259),g=f(g,v,m,l,n[e+9],21,-343485551),l=t(l,i),g=t(g,a),v=t(v,h),m=t(m,d);return[l,g,v,m]}function a(n){var t,r="";for(t=0;t<32*n.length;t+=8)r+=String.fromCharCode(n[t>>5]>>>t%32&255);return r}function h(n){var t,r=[];for(r[(n.length>>2)-1]=void 0,t=0;t<r.length;t+=1)r[t]=0;for(t=0;t<8*n.length;t+=8)r[t>>5]|=(255&n.charCodeAt(t/8))<<t%32;return r}function d(n){return a(i(h(n),8*n.length))}function l(n,t){var r,e,o=h(n),u=[],c=[];for(u[15]=c[15]=void 0,o.length>16&&(o=i(o,8*n.length)),r=0;16>r;r+=1)u[r]=909522486^o[r],c[r]=1549556828^o[r];return e=i(u.concat(h(t)),512+8*t.length),a(i(c.concat(e),640))}function g(n){var t,r,e="0123456789abcdef",o="";for(r=0;r<n.length;r+=1)t=n.charCodeAt(r),o+=e.charAt(t>>>4&15)+e.charAt(15&t);return o}function v(n){return unescape(encodeURIComponent(n))}function m(n){return d(v(n))}function p(n){return g(m(n))}function s(n,t){return l(v(n),v(t))}function C(n,t){return g(s(n,t))}function A(n,t,r){return t?r?s(t,n):C(t,n):r?m(n):p(n)}"function"==typeof define&&define.amd?define(function(){return A}):"object"==typeof module&&module.exports?module.exports=A:n.md5=A}(this);
165
166if (lwsgs_user.substring(0, 1) == "$") {
167	alert("lwsgs.js: lws generic sessions misconfigured and not providing vars");
168}
169function lwsgs_san(s)
170{
171	if (s.search("<") != -1)
172		return "invalid string";
173
174	return s;
175}
176
177function lwsgs_update()
178{
179	var en_login = 1, en_forgot = 1;
180
181	if (document.getElementById('password').value.length &&
182	    document.getElementById('password').value.length < 8)
183		en_login = 0;
184
185	if (!document.getElementById('username').value ||
186	    !document.getElementById('password').value)
187		en_login = 0;
188
189	if (!document.getElementById('username').value ||
190	     document.getElementById('password').value)
191		en_forgot = 0;
192
193	document.getElementById('login').disabled = !en_login;
194	document.getElementById('forgot').disabled = !en_forgot;
195
196	if (lwsgs_user)
197		document.getElementById("curuser").innerHTML = lwsgs_san(lwsgs_user);
198
199	if (lwsgs_user === "")
200		document.getElementById("dlogin").style.display = "inline";
201	else
202		document.getElementById("dlogout").style.display = "inline";
203 }
204
205function lwsgs_open_registration()
206{
207	document.getElementById("dadmin").style.display = "none";
208	document.getElementById("dlogin").style.display = "none";
209	document.getElementById("dlogout").style.display = "none";
210	document.getElementById("dchange").style.display = "none";
211	document.getElementById("dregister").style.display = "inline";
212}
213
214function lwsgs_cancel_registration()
215{
216	document.getElementById("dadmin").style.display = "none";
217	document.getElementById("dregister").style.display = "none";
218	document.getElementById("dchange").style.display = "none";
219
220	if (lwsgs_user === "")
221		document.getElementById("dlogin").style.display = "inline";
222	else
223		document.getElementById("dlogout").style.display = "inline";
224}
225
226function lwsgs_select_change()
227{
228	document.getElementById("dlogin").style.display = "none";
229	document.getElementById("dlogout").style.display = "none";
230	document.getElementById("dregister").style.display = "none";
231	if (lwsgs_auth & 2) {
232		document.getElementById("dadmin").style.display = "inline";
233		document.getElementById("dchange").style.display = "none";
234	} else {
235		document.getElementById("dadmin").style.display = "none";
236		document.getElementById("dchange").style.display = "inline";
237	}
238
239	event.preventDefault()
240}
241
242var lwsgs_user_check = '0';
243var lwsgs_email_check = '0';
244
245function lwsgs_rupdate()
246{
247	var en_register = 1, en_forgot = 0, op;
248
249	if (document.getElementById('rpassword').value ==
250	    document.getElementById('password2').value) {
251		if (document.getElementById('rpassword').value.length)
252			document.getElementById('match').innerHTML =
253				"<b class=\"green\">\u2713</b>";
254		else
255			document.getElementById('match').innerHTML = "";
256		document.getElementById('pw2').style = "";
257	} else {
258		if (document.getElementById('password2').value ||
259		    document.getElementById('email').value) { // ie, he is filling in "register" path and cares
260			document.getElementById('match').innerHTML =
261				"<span class=\"bad\">\u2718 <b>Passwords do not match</b></span>";
262		} else
263			document.getElementById('match').innerHTML =
264				"<span class=\"bad\">\u2718 Passwords do not match</span>";
265
266		en_register = 0;
267	}
268
269	if (document.getElementById('rpassword').value.length &&
270	    document.getElementById('rpassword').value.length < 8) {
271		en_register = 0;
272		document.getElementById('rpw1').innerHTML = "Need 8 chars";
273	} else
274		if (document.getElementById('rpassword').value.length)
275			document.getElementById('rpw1').innerHTML = "<b class=\"green\">\u2713</b>";
276		else
277			document.getElementById('rpw1').innerHTML = "";
278
279	if (!document.getElementById('rpassword').value ||
280	    !document.getElementById('password2').value ||
281	    !document.getElementById('rusername').value ||
282	    !document.getElementById('email').value ||
283	    lwsgs_email_check === '1'||
284	    lwsgs_user_check === '1')
285		en_register = 0;
286
287	document.getElementById('register').disabled = !en_register;
288	document.getElementById('rpassword').disabled = lwsgs_user_check === '1';
289	document.getElementById('password2').disabled = lwsgs_user_check === '1';
290	document.getElementById('email').disabled = lwsgs_user_check === '1';
291
292	if (lwsgs_user_check === '0') {
293		var uc = document.getElementById('uchk');
294
295		if (uc) {
296			if (document.getElementById('rusername').value)
297				uc.innerHTML = "<b class=\"green\">\u2713</b>";
298			else
299				uc.innerHTML = "";
300		}
301	} else {
302		if (document.getElementById('uchk'))
303			ocument.getElementById('uchk').innerHTML = "<b class=\"red\">\u2718 Already registered</b>";
304		en_forgot = 1;
305	}
306
307	if (lwsgs_email_check === '0') {
308		var ec = document.getElementById('echk');
309
310		if (ec) {
311			if (document.getElementById('email').value)
312				ec.innerHTML = "<b class=\"green\">\u2713</b>";
313			else
314				ec.innerHTML = "";
315		}
316	} else {
317		if (document.getElementById('echk'))
318			document.getElementById('echk').innerHTML = "<b class=\"red\">\u2718 Already registered</b>";
319		en_forgot = 1;
320	}
321
322	if (en_forgot)
323		document.getElementById('rforgot').style.display = "inline";
324	else
325		document.getElementById('rforgot').style.display = "none";
326
327	if (lwsgs_user_check === '1')
328		op = '0.5';
329	else
330		op = '1.0';
331	document.getElementById('rpassword').style.opacity = op;
332 	document.getElementById('password2').style.opacity = op;
333	document.getElementById('email').style.opacity = op;
334 }
335
336function lwsgs_cupdate()
337{
338	var en_change = 1, en_forgot = 1, pwok = 1, op;
339
340	if (lwsgs_auth & 8) {
341		document.getElementById('ccurpw').style.display = "none";
342		document.getElementById('ccurpw_name').style.display = "none";
343	} else {
344		if (!document.getElementById('ccurpw').value ||
345		    document.getElementById('ccurpw').value.length < 8) {
346			en_change = 0;
347			pwok = 0;
348			document.getElementById('cuchk').innerHTML = "<b class=\"red\">\u2718</b>";
349		} else {
350			en_forgot = 0;
351			document.getElementById('cuchk').innerHTML = "";
352		}
353		document.getElementById('ccurpw').style.display = "inline";
354		document.getElementById('ccurpw_name').style.display = "inline";
355	}
356
357	if (document.getElementById('cpassword').value ==
358	    document.getElementById('cpassword2').value) {
359		if (document.getElementById('cpassword').value.length)
360			document.getElementById('cmatch').innerHTML = "<b class=\"green\">\u2713</b>";
361		else
362			document.getElementById('cmatch').innerHTML = "";
363		document.getElementById('pw2').style = "";
364	} else {
365		if (document.getElementById('cpassword2').value //||
366		    //document.getElementById('cemail').value
367		) { // ie, he is filling in "register" path and cares
368			document.getElementById('cmatch').innerHTML =
369				"<span class=\"red\">\u2718 <b>Passwords do not match</b></span>";
370		} else
371			document.getElementById('cmatch').innerHTML = "<span class=\"red\">\u2718 Passwords do not match</span>";
372
373		en_change = 0;
374	}
375
376	if (document.getElementById('cpassword').value.length &&
377	    document.getElementById('cpassword').value.length < 8) {
378		en_change = 0;
379		document.getElementById('cpw1').innerHTML = "Need 8 chars";
380	} else {
381		var cpw = document.getElementById('cpw1');
382
383		if (cpw) {
384			if (document.getElementById('cpassword').value.length)
385				cpw.innerHTML = "<b class=\"green\">\u2713</b>";
386			else
387				cpw.innerHTML = "";
388		}
389	}
390
391	if (!document.getElementById('cpassword').value ||
392	    !document.getElementById('cpassword2').value ||
393	    pwok === 0)
394		en_change = 0;
395
396	if (document.getElementById('showdel').checked)
397		document.getElementById('delete').style.display = "inline";
398	else
399		document.getElementById('delete').style.display = "none";
400
401	document.getElementById('change').disabled = !en_change;
402	document.getElementById('cpassword').disabled = pwok === 0;
403	document.getElementById('cpassword2').disabled = pwok === 0;
404	document.getElementById('showdel').disabled = pwok === 0;
405	document.getElementById('delete').disabled = pwok === 0;
406	//document.getElementById('cemail').disabled = pwok === 0;
407
408	/*
409	if (lwsgs_auth & 8) {
410		document.getElementById('cemail').style.display = "none";
411		document.getElementById('cemail_name').style.display = "none";
412	} else {
413		document.getElementById('cemail').style.display = "inline";
414		document.getElementById('cemail_name').style.display = "inline";
415		if (lwsgs_email_check === '0'  &&
416		    document.getElementById('cemail').value != lwsgs_email) {
417			if (document.getElementById('cemail').value)
418				document.getElementById('cechk').innerHTML = "<b style=\"color:green\">\u2713</b>";
419			else
420				document.getElementById('cechk').innerHTML = "";
421		} else {
422			document.getElementById('cechk').innerHTML = "<b style=\"color:red\">\u2718 Already registered</b>";
423			en_forgot = 1;
424		}
425	} */
426
427	if (lwsgs_auth & 8)
428		en_forgot = 0;
429
430	if (en_forgot)
431		document.getElementById('cforgot').style.display = "inline";
432	else
433		document.getElementById('cforgot').style.display = "none";
434
435	if (pwok === 0)
436		op = '0.5';
437	else
438		op = '1.0';
439	document.getElementById('cpassword').style.opacity = op;
440 	document.getElementById('cpassword2').style.opacity = op;
441	// document.getElementById('cemail').style.opacity = op;
442 }
443
444function lwsgs_check_user()
445{
446    var xmlHttp = new XMLHttpRequest();
447    xmlHttp.onreadystatechange = function() {
448        if (xmlHttp.readyState === 4 && xmlHttp.status === 200) {
449            lwsgs_user_check = xmlHttp.responseText;
450	    lwsgs_rupdate();
451        }
452    }
453    xmlHttp.open("GET", "lwsgs-check/username="+document.getElementById('rusername').value, true);
454    xmlHttp.send(null);
455}
456
457function lwsgs_check_email(id)
458{
459    var xmlHttp = new XMLHttpRequest();
460    xmlHttp.onreadystatechange = function() {
461        if (xmlHttp.readyState === 4 && xmlHttp.status === 200) {
462            lwsgs_email_check = xmlHttp.responseText;
463	    lwsgs_rupdate();
464        }
465    }
466    xmlHttp.open("GET", "lwsgs-check/email="+document.getElementById(id).value, true);
467    xmlHttp.send(null);
468}
469
470function rupdate_user()
471{
472	lwsgs_rupdate();
473	lwsgs_check_user();
474}
475
476function rupdate_email()
477{
478	lwsgs_rupdate();
479	lwsgs_check_email('email');
480}
481
482function cupdate_email()
483{
484	lwsgs_cupdate();
485	lwsgs_check_email('cemail');
486}
487
488
489function lwsgs_initial()
490{
491	document.getElementById('lwsgs').innerHTML = lwsgs_html;
492
493	if (lwsgs_user) {
494		document.getElementById("curuser").innerHTML =
495			"currently logged in as " + lwsgs_san(lwsgs_user) + "</br>";
496
497		document.getElementById("ccuruser").innerHTML =
498		  "<span class=\"gstitle\">Login settings for " +
499		  lwsgs_san(lwsgs_user) + "</span></br>";
500	}
501
502	document.getElementById('username').oninput = lwsgs_update;
503	document.getElementById('username').onchange = lwsgs_update;
504	document.getElementById('password').oninput = lwsgs_update;
505	document.getElementById('password').onchange = lwsgs_update;
506	document.getElementById('doreg').onclick = lwsgs_open_registration;
507	document.getElementById('clink').onclick = lwsgs_select_change;
508	document.getElementById('cancel').onclick =lwsgs_cancel_registration;
509	document.getElementById('cancel2').onclick =lwsgs_cancel_registration;
510 	document.getElementById('rpassword').oninput = lwsgs_rupdate;
511 	document.getElementById('password2').oninput = lwsgs_rupdate;
512	document.getElementById('rusername').oninput = rupdate_user;
513	document.getElementById('email').oninput  = rupdate_email;
514	document.getElementById('ccurpw').oninput = lwsgs_cupdate;
515	document.getElementById('cpassword').oninput = lwsgs_cupdate;
516	document.getElementById('cpassword2').oninput = lwsgs_cupdate;
517<!--	document.getElementById('cemail').oninput = cupdate_email;-->
518	document.getElementById('showdel').onchange = lwsgs_cupdate;
519
520	if (lwsgs_email)
521		document.getElementById('grav').innerHTML =
522			"<img class='av' " +
523			"src=\"https://www.gravatar.com/avatar/" +
524			md5(lwsgs_email) +
525			"?d=identicon\">";
526	//if (lwsgs_email)
527		//document.getElementById('cemail').placeholder = lwsgs_email;
528	document.getElementById('cusername').value = lwsgs_user;
529	lwsgs_update();
530	lwsgs_cupdate();
531}
532
533window.addEventListener("load", function() {
534	lwsgs_initial();
535	document.getElementById("nolog").style.display = !!lwsgs_user ? "none" : "inline-block";
536	document.getElementById("logged").style.display = !lwsgs_user ? "none" : "inline-block";
537
538	document.getElementById("msg").onkeyup = mupd;
539	document.getElementById("msg").onchange = mupd;
540
541	var ws;
542
543	function mb_format(s)
544	{
545		var r = "", n, wos = 0;
546
547		for (n = 0; n < s.length; n++) {
548			if (s[n] == ' ')
549				wos = 0;
550			else {
551				wos++;
552				if (wos === 40) {
553					wos = 0;
554					r = r + ' ';
555				}
556			}
557			if (s[n] == '<') {
558				r = r + "&lt;";
559				continue;
560			}
561			if (s[n] == '\n') {
562				r = r + "<br>";
563				continue;
564			}
565
566			r = r + s[n];
567		}
568
569		return r;
570	}
571
572	function add_div(n, m)
573	{
574		var q = document.getElementById(n);
575		var d = new Date(m.time * 1000), s = d.toTimeString(), t;
576
577		t = s.indexOf('(');
578		if (t)
579			s = s.substring(0, t);
580
581		q.innerHTML = "<br><div class=\"group2\"><table class=\"fixed\"><tr><td>" +
582			"<img class=\"av\" src=\"https://www.gravatar.com/avatar/" + md5(m.email) +
583			"?d=identicon\"><br>" +
584			"<b>" + lwsgs_san(m.username) + "</b><br>" +
585			"<span class=\"small\">" + d.toDateString() +
586			  "<br>" + s + "</span><br>" +
587			"IP: " + lwsgs_san(m.ip) +
588			"</td><td class=\"ava\"><span>" +
589			mb_format(m.content) +
590			"</span></td></tr></table></div><br>" + q.innerHTML;
591	}
592
593	function get_appropriate_ws_url()
594	{
595		var pcol;
596		var u = document.URL;
597
598		if (u.substring(0, 5) == "https") {
599			pcol = "wss://";
600			u = u.substr(8);
601		} else {
602			pcol = "ws://";
603			if (u.substring(0, 4) == "http")
604				u = u.substr(7);
605		}
606		u = u.split('/');
607
608		return pcol + u[0] + "/xxx";
609	}
610
611	if (lwsgs_user) {
612
613		ws = new WebSocket(get_appropriate_ws_url(),
614					   "protocol-lws-messageboard");
615
616		try {
617			ws.onopen = function() {
618				document.getElementById("debug").textContent = "ws opened";
619			}
620			ws.onmessage =function got_packet(msg) {
621				add_div("messages", JSON.parse(msg.data));
622			}
623			ws.onclose = function(){
624			}
625		} catch(exception) {
626			alert('<p>Error' + exception);
627		}
628	}
629
630	function mupd()
631	{
632		document.getElementById("send").disabled = !document.getElementById("msg").value;
633	}
634}, false);
635