annotate forum/Themes/default/script.js @ 82:6dd719d7c78b website

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