comparison forum/Themes/Vamp/script.js @ 76:e3e11437ecea website

Add forum code
author Chris Cannam
date Sun, 07 Jul 2013 11:25:48 +0200
parents
children
comparison
equal deleted inserted replaced
75:72f59aa7e503 76:e3e11437ecea
1 var smf_formSubmitted = false;
2
3 // Define document.getElementById for Internet Explorer 4.
4 if (typeof(document.getElementById) == "undefined")
5 document.getElementById = function (id)
6 {
7 // Just return the corresponding index of all.
8 return document.all[id];
9 }
10 // Define XMLHttpRequest for IE 5 and above. (don't bother for IE 4 :/.... works in Opera 7.6 and Safari 1.2!)
11 else if (!window.XMLHttpRequest && window.ActiveXObject)
12 window.XMLHttpRequest = function ()
13 {
14 return new ActiveXObject(navigator.userAgent.indexOf("MSIE 5") != -1 ? "Microsoft.XMLHTTP" : "MSXML2.XMLHTTP");
15 };
16
17 // Some older versions of Mozilla don't have this, for some reason.
18 if (typeof(document.forms) == "undefined")
19 document.forms = document.getElementsByTagName("form");
20
21 // Load an XML document using XMLHttpRequest.
22 function getXMLDocument(url, callback)
23 {
24 if (!window.XMLHttpRequest)
25 return false;
26
27 var myDoc = new XMLHttpRequest();
28 if (typeof(callback) != "undefined")
29 {
30 myDoc.onreadystatechange = function ()
31 {
32 if (myDoc.readyState != 4)
33 return;
34
35 if (myDoc.responseXML != null && myDoc.status == 200)
36 callback(myDoc.responseXML);
37 };
38 }
39 myDoc.open('GET', url, true);
40 myDoc.send(null);
41
42 return true;
43 }
44
45 // Send a post form to the server using XMLHttpRequest.
46 function sendXMLDocument(url, content, callback)
47 {
48 if (!window.XMLHttpRequest)
49 return false;
50
51 var sendDoc = new window.XMLHttpRequest();
52 if (typeof(callback) != "undefined")
53 {
54 sendDoc.onreadystatechange = function ()
55 {
56 if (sendDoc.readyState != 4)
57 return;
58
59 if (sendDoc.responseXML != null && sendDoc.status == 200)
60 callback(sendDoc.responseXML);
61 else
62 callback(false);
63 };
64 }
65 sendDoc.open('POST', url, true);
66 if (typeof(sendDoc.setRequestHeader) != "undefined")
67 sendDoc.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
68 sendDoc.send(content);
69
70 return true;
71 }
72
73 function textToEntities(text)
74 {
75 var entities = "";
76 for (var i = 0; i < text.length; i++)
77 {
78 if (text.charCodeAt(i) > 127)
79 entities += "&#" + text.charCodeAt(i) + ";";
80 else
81 entities += text.charAt(i);
82 }
83
84 return entities;
85 }
86
87 // Open a new window.
88 function reqWin(desktopURL, alternateWidth, alternateHeight, noScrollbars)
89 {
90 if ((alternateWidth && self.screen.availWidth * 0.8 < alternateWidth) || (alternateHeight && self.screen.availHeight * 0.8 < alternateHeight))
91 {
92 noScrollbars = false;
93 alternateWidth = Math.min(alternateWidth, self.screen.availWidth * 0.8);
94 alternateHeight = Math.min(alternateHeight, self.screen.availHeight * 0.8);
95 }
96 else
97 noScrollbars = typeof(noScrollbars) != "undefined" && noScrollbars == true;
98
99 window.open(desktopURL, 'requested_popup', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=' + (noScrollbars ? 'no' : 'yes') + ',width=' + (alternateWidth ? alternateWidth : 480) + ',height=' + (alternateHeight ? alternateHeight : 220) + ',resizable=no');
100
101 // Return false so the click won't follow the link ;).
102 return false;
103 }
104
105 // Remember the current position.
106 function storeCaret(text)
107 {
108 // Only bother if it will be useful.
109 if (typeof(text.createTextRange) != "undefined")
110 text.caretPos = document.selection.createRange().duplicate();
111 }
112
113 // Replaces the currently selected text with the passed text.
114 function replaceText(text, textarea)
115 {
116 // Attempt to create a text range (IE).
117 if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
118 {
119 var caretPos = textarea.caretPos;
120
121 caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
122 caretPos.select();
123 }
124 // Mozilla text range replace.
125 else if (typeof(textarea.selectionStart) != "undefined")
126 {
127 var begin = textarea.value.substr(0, textarea.selectionStart);
128 var end = textarea.value.substr(textarea.selectionEnd);
129 var scrollPos = textarea.scrollTop;
130
131 textarea.value = begin + text + end;
132
133 if (textarea.setSelectionRange)
134 {
135 textarea.focus();
136 textarea.setSelectionRange(begin.length + text.length, begin.length + text.length);
137 }
138 textarea.scrollTop = scrollPos;
139 }
140 // Just put it on the end.
141 else
142 {
143 textarea.value += text;
144 textarea.focus(textarea.value.length - 1);
145 }
146 }
147
148 // Surrounds the selected text with text1 and text2.
149 function surroundText(text1, text2, textarea)
150 {
151 // Can a text range be created?
152 if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
153 {
154 var caretPos = textarea.caretPos, temp_length = caretPos.text.length;
155
156 caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;
157
158 if (temp_length == 0)
159 {
160 caretPos.moveStart("character", -text2.length);
161 caretPos.moveEnd("character", -text2.length);
162 caretPos.select();
163 }
164 else
165 textarea.focus(caretPos);
166 }
167 // Mozilla text range wrap.
168 else if (typeof(textarea.selectionStart) != "undefined")
169 {
170 var begin = textarea.value.substr(0, textarea.selectionStart);
171 var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
172 var end = textarea.value.substr(textarea.selectionEnd);
173 var newCursorPos = textarea.selectionStart;
174 var scrollPos = textarea.scrollTop;
175
176 textarea.value = begin + text1 + selection + text2 + end;
177
178 if (textarea.setSelectionRange)
179 {
180 if (selection.length == 0)
181 textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
182 else
183 textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
184 textarea.focus();
185 }
186 textarea.scrollTop = scrollPos;
187 }
188 // Just put them on the end, then.
189 else
190 {
191 textarea.value += text1 + text2;
192 textarea.focus(textarea.value.length - 1);
193 }
194 }
195
196 // Checks if the passed input's value is nothing.
197 function isEmptyText(theField)
198 {
199 // Copy the value so changes can be made..
200 var theValue = theField.value;
201
202 // Strip whitespace off the left side.
203 while (theValue.length > 0 && (theValue.charAt(0) == ' ' || theValue.charAt(0) == '\t'))
204 theValue = theValue.substring(1, theValue.length);
205 // Strip whitespace off the right side.
206 while (theValue.length > 0 && (theValue.charAt(theValue.length - 1) == ' ' || theValue.charAt(theValue.length - 1) == '\t'))
207 theValue = theValue.substring(0, theValue.length - 1);
208
209 if (theValue == '')
210 return true;
211 else
212 return false;
213 }
214
215 // Only allow form submission ONCE.
216 function submitonce(theform)
217 {
218 smf_formSubmitted = true;
219 }
220 function submitThisOnce(form)
221 {
222 // Hateful, hateful fix for Safari 1.3 beta.
223 if (navigator.userAgent.indexOf('AppleWebKit') != -1)
224 return !smf_formSubmitted;
225
226 if (typeof(form.form) != "undefined")
227 form = form.form;
228
229 for (var i = 0; i < form.length; i++)
230 if (typeof(form[i]) != "undefined" && form[i].tagName.toLowerCase() == "textarea")
231 form[i].readOnly = true;
232
233 return !smf_formSubmitted;
234 }
235
236 // Set the "inside" HTML of an element.
237 function setInnerHTML(element, toValue)
238 {
239 // IE has this built in...
240 if (typeof(element.innerHTML) != 'undefined')
241 element.innerHTML = toValue;
242 // Otherwise, try createContextualFragment().
243 else
244 {
245 var range = document.createRange();
246 range.selectNodeContents(element);
247 range.deleteContents();
248 element.appendChild(range.createContextualFragment(toValue));
249 }
250 }
251
252 // Set the "outer" HTML of an element.
253 function setOuterHTML(element, toValue)
254 {
255 if (typeof(element.outerHTML) != 'undefined')
256 element.outerHTML = toValue;
257 else
258 {
259 var range = document.createRange();
260 range.setStartBefore(element);
261 element.parentNode.replaceChild(range.createContextualFragment(toValue), element);
262 }
263 }
264
265 // Get the inner HTML of an element.
266 function getInnerHTML(element)
267 {
268 if (typeof(element.innerHTML) != 'undefined')
269 return element.innerHTML;
270 else
271 {
272 var returnStr = '';
273 for (var i = 0; i < element.childNodes.length; i++)
274 returnStr += getOuterHTML(element.childNodes[i]);
275
276 return returnStr;
277 }
278 }
279
280 function getOuterHTML(node)
281 {
282 if (typeof(node.outerHTML) != 'undefined')
283 return node.outerHTML;
284
285 var str = '';
286
287 switch (node.nodeType)
288 {
289 // An element.
290 case 1:
291 str += '<' + node.nodeName;
292
293 for (var i = 0; i < node.attributes.length; i++)
294 {
295 if (node.attributes[i].nodeValue != null)
296 str += ' ' + node.attributes[i].nodeName + '="' + node.attributes[i].nodeValue + '"';
297 }
298
299 if (node.childNodes.length == 0 && in_array(node.nodeName.toLowerCase(), ['hr', 'input', 'img', 'link', 'meta', 'br']))
300 str += ' />';
301 else
302 str += '>' + getInnerHTML(node) + '</' + node.nodeName + '>';
303 break;
304
305 // 2 is an attribute.
306
307 // Just some text..
308 case 3:
309 str += node.nodeValue;
310 break;
311
312 // A CDATA section.
313 case 4:
314 str += '<![CDATA' + '[' + node.nodeValue + ']' + ']>';
315 break;
316
317 // Entity reference..
318 case 5:
319 str += '&' + node.nodeName + ';';
320 break;
321
322 // 6 is an actual entity, 7 is a PI.
323
324 // Comment.
325 case 8:
326 str += '<!--' + node.nodeValue + '-->';
327 break;
328 }
329
330 return str;
331 }
332
333 // Checks for variable in theArray.
334 function in_array(variable, theArray)
335 {
336 for (var i = 0; i < theArray.length; i++)
337 {
338 if (theArray[i] == variable)
339 return true;
340 }
341 return false;
342 }
343
344 // Find a specific radio button in its group and select it.
345 function selectRadioByName(radioGroup, name)
346 {
347 if (typeof(radioGroup.length) == "undefined")
348 return radioGroup.checked = true;
349
350 for (var i = 0; i < radioGroup.length; i++)
351 {
352 if (radioGroup[i].value == name)
353 return radioGroup[i].checked = true;
354 }
355
356 return false;
357 }
358
359 // Invert all checkboxes at once by clicking a single checkbox.
360 function invertAll(headerfield, checkform, mask)
361 {
362 for (var i = 0; i < checkform.length; i++)
363 {
364 if (typeof(checkform[i].name) == "undefined" || (typeof(mask) != "undefined" && checkform[i].name.substr(0, mask.length) != mask))
365 continue;
366
367 if (!checkform[i].disabled)
368 checkform[i].checked = headerfield.checked;
369 }
370 }
371
372 // Keep the session alive - always!
373 var lastKeepAliveCheck = new Date().getTime();
374 function smf_sessionKeepAlive()
375 {
376 var curTime = new Date().getTime();
377
378 // Prevent a Firefox bug from hammering the server.
379 if (smf_scripturl && curTime - lastKeepAliveCheck > 900000)
380 {
381 var tempImage = new Image();
382 tempImage.src = smf_scripturl + (smf_scripturl.indexOf("?") == -1 ? "?" : "&") + "action=keepalive;" + curTime;
383 lastKeepAliveCheck = curTime;
384 }
385
386 window.setTimeout("smf_sessionKeepAlive();", 1200000);
387 }
388 window.setTimeout("smf_sessionKeepAlive();", 1200000);
389
390 // Set a theme option through javascript.
391 function smf_setThemeOption(option, value, theme, cur_session_id)
392 {
393 // Compatibility.
394 if (cur_session_id == null)
395 cur_session_id = smf_session_id;
396
397 var tempImage = new Image();
398 tempImage.src = smf_scripturl + (smf_scripturl.indexOf("?") == -1 ? "?" : "&") + "action=jsoption;var=" + option + ";val=" + value + ";sesc=" + cur_session_id + (theme == null ? "" : "&id=" + theme) + ";" + (new Date().getTime());
399 }
400
401 function smf_avatarResize()
402 {
403 var possibleAvatars = document.getElementsByTagName ? document.getElementsByTagName("img") : document.all.tags("img");
404
405 for (var i = 0; i < possibleAvatars.length; i++)
406 {
407 if (possibleAvatars[i].className != "avatar")
408 continue;
409
410 var tempAvatar = new Image();
411 tempAvatar.src = possibleAvatars[i].src;
412
413 if (smf_avatarMaxWidth != 0 && tempAvatar.width > smf_avatarMaxWidth)
414 {
415 possibleAvatars[i].height = (smf_avatarMaxWidth * tempAvatar.height) / tempAvatar.width;
416 possibleAvatars[i].width = smf_avatarMaxWidth;
417 }
418 else if (smf_avatarMaxHeight != 0 && tempAvatar.height > smf_avatarMaxHeight)
419 {
420 possibleAvatars[i].width = (smf_avatarMaxHeight * tempAvatar.width) / tempAvatar.height;
421 possibleAvatars[i].height = smf_avatarMaxHeight;
422 }
423 else
424 {
425 possibleAvatars[i].width = tempAvatar.width;
426 possibleAvatars[i].height = tempAvatar.height;
427 }
428 }
429
430 if (typeof(window_oldAvatarOnload) != "undefined" && window_oldAvatarOnload)
431 {
432 window_oldAvatarOnload();
433 window_oldAvatarOnload = null;
434 }
435 }
436
437 function hashLoginPassword(doForm, cur_session_id)
438 {
439 // Compatibility.
440 if (cur_session_id == null)
441 cur_session_id = smf_session_id;
442
443 if (typeof(hex_sha1) == "undefined")
444 return;
445 // Are they using an email address?
446 if (doForm.user.value.indexOf("@") != -1)
447 return;
448
449 // Unless the browser is Opera, the password will not save properly.
450 if (typeof(window.opera) == "undefined")
451 doForm.passwrd.autocomplete = "off";
452
453 doForm.hash_passwrd.value = hex_sha1(hex_sha1(doForm.user.value.php_to8bit().php_strtolower() + doForm.passwrd.value.php_to8bit()) + cur_session_id);
454
455 // It looks nicer to fill it with asterisks, but Firefox will try to save that.
456 if (navigator.userAgent.indexOf("Firefox/") != -1)
457 doForm.passwrd.value = "";
458 else
459 doForm.passwrd.value = doForm.passwrd.value.replace(/./g, "*");
460 }
461
462 function hashAdminPassword(doForm, username, cur_session_id)
463 {
464 // Compatibility.
465 if (cur_session_id == null)
466 cur_session_id = smf_session_id;
467
468 if (typeof(hex_sha1) == "undefined")
469 return;
470
471 doForm.admin_hash_pass.value = hex_sha1(hex_sha1(username.toLowerCase() + doForm.admin_pass.value) + cur_session_id);
472 doForm.admin_pass.value = doForm.admin_pass.value.replace(/./g, "*");
473 }
474
475 function ajax_indicator(turn_on)
476 {
477 var indicator = document.getElementById("ajax_in_progress");
478 if (indicator != null)
479 {
480 if (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf("MSIE 7") == -1)
481 {
482 indicator.style.top = document.documentElement.scrollTop;
483 }
484 indicator.style.display = turn_on ? "block" : "none";
485 }
486 }