Chris@76: var smf_formSubmitted = false; Chris@76: Chris@76: // Define document.getElementById for Internet Explorer 4. Chris@76: if (typeof(document.getElementById) == "undefined") Chris@76: document.getElementById = function (id) Chris@76: { Chris@76: // Just return the corresponding index of all. Chris@76: return document.all[id]; Chris@76: } Chris@76: // Define XMLHttpRequest for IE 5 and above. (don't bother for IE 4 :/.... works in Opera 7.6 and Safari 1.2!) Chris@76: else if (!window.XMLHttpRequest && window.ActiveXObject) Chris@76: window.XMLHttpRequest = function () Chris@76: { Chris@76: return new ActiveXObject(navigator.userAgent.indexOf("MSIE 5") != -1 ? "Microsoft.XMLHTTP" : "MSXML2.XMLHTTP"); Chris@76: }; Chris@76: Chris@76: // Some older versions of Mozilla don't have this, for some reason. Chris@76: if (typeof(document.forms) == "undefined") Chris@76: document.forms = document.getElementsByTagName("form"); Chris@76: Chris@76: // Load an XML document using XMLHttpRequest. Chris@76: function getXMLDocument(url, callback) Chris@76: { Chris@76: if (!window.XMLHttpRequest) Chris@76: return false; Chris@76: Chris@76: var myDoc = new XMLHttpRequest(); Chris@76: if (typeof(callback) != "undefined") Chris@76: { Chris@76: myDoc.onreadystatechange = function () Chris@76: { Chris@76: if (myDoc.readyState != 4) Chris@76: return; Chris@76: Chris@76: if (myDoc.responseXML != null && myDoc.status == 200) Chris@76: callback(myDoc.responseXML); Chris@76: }; Chris@76: } Chris@76: myDoc.open('GET', url, true); Chris@76: myDoc.send(null); Chris@76: Chris@76: return true; Chris@76: } Chris@76: Chris@76: // Send a post form to the server using XMLHttpRequest. Chris@76: function sendXMLDocument(url, content, callback) Chris@76: { Chris@76: if (!window.XMLHttpRequest) Chris@76: return false; Chris@76: Chris@76: var sendDoc = new window.XMLHttpRequest(); Chris@76: if (typeof(callback) != "undefined") Chris@76: { Chris@76: sendDoc.onreadystatechange = function () Chris@76: { Chris@76: if (sendDoc.readyState != 4) Chris@76: return; Chris@76: Chris@76: if (sendDoc.responseXML != null && sendDoc.status == 200) Chris@76: callback(sendDoc.responseXML); Chris@76: else Chris@76: callback(false); Chris@76: }; Chris@76: } Chris@76: sendDoc.open('POST', url, true); Chris@76: if (typeof(sendDoc.setRequestHeader) != "undefined") Chris@76: sendDoc.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); Chris@76: sendDoc.send(content); Chris@76: Chris@76: return true; Chris@76: } Chris@76: Chris@76: function textToEntities(text) Chris@76: { Chris@76: var entities = ""; Chris@76: for (var i = 0; i < text.length; i++) Chris@76: { Chris@76: if (text.charCodeAt(i) > 127) Chris@76: entities += "&#" + text.charCodeAt(i) + ";"; Chris@76: else Chris@76: entities += text.charAt(i); Chris@76: } Chris@76: Chris@76: return entities; Chris@76: } Chris@76: Chris@76: // Open a new window. Chris@76: function reqWin(desktopURL, alternateWidth, alternateHeight, noScrollbars) Chris@76: { Chris@76: if ((alternateWidth && self.screen.availWidth * 0.8 < alternateWidth) || (alternateHeight && self.screen.availHeight * 0.8 < alternateHeight)) Chris@76: { Chris@76: noScrollbars = false; Chris@76: alternateWidth = Math.min(alternateWidth, self.screen.availWidth * 0.8); Chris@76: alternateHeight = Math.min(alternateHeight, self.screen.availHeight * 0.8); Chris@76: } Chris@76: else Chris@76: noScrollbars = typeof(noScrollbars) != "undefined" && noScrollbars == true; Chris@76: Chris@76: 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: Chris@76: // Return false so the click won't follow the link ;). Chris@76: return false; Chris@76: } Chris@76: Chris@76: // Remember the current position. Chris@76: function storeCaret(text) Chris@76: { Chris@76: // Only bother if it will be useful. Chris@76: if (typeof(text.createTextRange) != "undefined") Chris@76: text.caretPos = document.selection.createRange().duplicate(); Chris@76: } Chris@76: Chris@76: // Replaces the currently selected text with the passed text. Chris@76: function replaceText(text, textarea) Chris@76: { Chris@76: // Attempt to create a text range (IE). Chris@76: if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) Chris@76: { Chris@76: var caretPos = textarea.caretPos; Chris@76: Chris@76: caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text; Chris@76: caretPos.select(); Chris@76: } Chris@76: // Mozilla text range replace. Chris@76: else if (typeof(textarea.selectionStart) != "undefined") Chris@76: { Chris@76: var begin = textarea.value.substr(0, textarea.selectionStart); Chris@76: var end = textarea.value.substr(textarea.selectionEnd); Chris@76: var scrollPos = textarea.scrollTop; Chris@76: Chris@76: textarea.value = begin + text + end; Chris@76: Chris@76: if (textarea.setSelectionRange) Chris@76: { Chris@76: textarea.focus(); Chris@76: textarea.setSelectionRange(begin.length + text.length, begin.length + text.length); Chris@76: } Chris@76: textarea.scrollTop = scrollPos; Chris@76: } Chris@76: // Just put it on the end. Chris@76: else Chris@76: { Chris@76: textarea.value += text; Chris@76: textarea.focus(textarea.value.length - 1); Chris@76: } Chris@76: } Chris@76: Chris@76: // Surrounds the selected text with text1 and text2. Chris@76: function surroundText(text1, text2, textarea) Chris@76: { Chris@76: // Can a text range be created? Chris@76: if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) Chris@76: { Chris@76: var caretPos = textarea.caretPos, temp_length = caretPos.text.length; Chris@76: Chris@76: caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2; Chris@76: Chris@76: if (temp_length == 0) Chris@76: { Chris@76: caretPos.moveStart("character", -text2.length); Chris@76: caretPos.moveEnd("character", -text2.length); Chris@76: caretPos.select(); Chris@76: } Chris@76: else Chris@76: textarea.focus(caretPos); Chris@76: } Chris@76: // Mozilla text range wrap. Chris@76: else if (typeof(textarea.selectionStart) != "undefined") Chris@76: { Chris@76: var begin = textarea.value.substr(0, textarea.selectionStart); Chris@76: var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart); Chris@76: var end = textarea.value.substr(textarea.selectionEnd); Chris@76: var newCursorPos = textarea.selectionStart; Chris@76: var scrollPos = textarea.scrollTop; Chris@76: Chris@76: textarea.value = begin + text1 + selection + text2 + end; Chris@76: Chris@76: if (textarea.setSelectionRange) Chris@76: { Chris@76: if (selection.length == 0) Chris@76: textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length); Chris@76: else Chris@76: textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length); Chris@76: textarea.focus(); Chris@76: } Chris@76: textarea.scrollTop = scrollPos; Chris@76: } Chris@76: // Just put them on the end, then. Chris@76: else Chris@76: { Chris@76: textarea.value += text1 + text2; Chris@76: textarea.focus(textarea.value.length - 1); Chris@76: } Chris@76: } Chris@76: Chris@76: // Checks if the passed input's value is nothing. Chris@76: function isEmptyText(theField) Chris@76: { Chris@76: // Copy the value so changes can be made.. Chris@76: var theValue = theField.value; Chris@76: Chris@76: // Strip whitespace off the left side. Chris@76: while (theValue.length > 0 && (theValue.charAt(0) == ' ' || theValue.charAt(0) == '\t')) Chris@76: theValue = theValue.substring(1, theValue.length); Chris@76: // Strip whitespace off the right side. Chris@76: while (theValue.length > 0 && (theValue.charAt(theValue.length - 1) == ' ' || theValue.charAt(theValue.length - 1) == '\t')) Chris@76: theValue = theValue.substring(0, theValue.length - 1); Chris@76: Chris@76: if (theValue == '') Chris@76: return true; Chris@76: else Chris@76: return false; Chris@76: } Chris@76: Chris@76: // Only allow form submission ONCE. Chris@76: function submitonce(theform) Chris@76: { Chris@76: smf_formSubmitted = true; Chris@76: } Chris@76: function submitThisOnce(form) Chris@76: { Chris@76: // Hateful, hateful fix for Safari 1.3 beta. Chris@76: if (navigator.userAgent.indexOf('AppleWebKit') != -1) Chris@76: return !smf_formSubmitted; Chris@76: Chris@76: if (typeof(form.form) != "undefined") Chris@76: form = form.form; Chris@76: Chris@76: for (var i = 0; i < form.length; i++) Chris@76: if (typeof(form[i]) != "undefined" && form[i].tagName.toLowerCase() == "textarea") Chris@76: form[i].readOnly = true; Chris@76: Chris@76: return !smf_formSubmitted; Chris@76: } Chris@76: Chris@76: // Set the "inside" HTML of an element. Chris@76: function setInnerHTML(element, toValue) Chris@76: { Chris@76: // IE has this built in... Chris@76: if (typeof(element.innerHTML) != 'undefined') Chris@76: element.innerHTML = toValue; Chris@76: // Otherwise, try createContextualFragment(). Chris@76: else Chris@76: { Chris@76: var range = document.createRange(); Chris@76: range.selectNodeContents(element); Chris@76: range.deleteContents(); Chris@76: element.appendChild(range.createContextualFragment(toValue)); Chris@76: } Chris@76: } Chris@76: Chris@76: // Set the "outer" HTML of an element. Chris@76: function setOuterHTML(element, toValue) Chris@76: { Chris@76: if (typeof(element.outerHTML) != 'undefined') Chris@76: element.outerHTML = toValue; Chris@76: else Chris@76: { Chris@76: var range = document.createRange(); Chris@76: range.setStartBefore(element); Chris@76: element.parentNode.replaceChild(range.createContextualFragment(toValue), element); Chris@76: } Chris@76: } Chris@76: Chris@76: // Get the inner HTML of an element. Chris@76: function getInnerHTML(element) Chris@76: { Chris@76: if (typeof(element.innerHTML) != 'undefined') Chris@76: return element.innerHTML; Chris@76: else Chris@76: { Chris@76: var returnStr = ''; Chris@76: for (var i = 0; i < element.childNodes.length; i++) Chris@76: returnStr += getOuterHTML(element.childNodes[i]); Chris@76: Chris@76: return returnStr; Chris@76: } Chris@76: } Chris@76: Chris@76: function getOuterHTML(node) Chris@76: { Chris@76: if (typeof(node.outerHTML) != 'undefined') Chris@76: return node.outerHTML; Chris@76: Chris@76: var str = ''; Chris@76: Chris@76: switch (node.nodeType) Chris@76: { Chris@76: // An element. Chris@76: case 1: Chris@76: str += '<' + node.nodeName; Chris@76: Chris@76: for (var i = 0; i < node.attributes.length; i++) Chris@76: { Chris@76: if (node.attributes[i].nodeValue != null) Chris@76: str += ' ' + node.attributes[i].nodeName + '="' + node.attributes[i].nodeValue + '"'; Chris@76: } Chris@76: Chris@76: if (node.childNodes.length == 0 && in_array(node.nodeName.toLowerCase(), ['hr', 'input', 'img', 'link', 'meta', 'br'])) Chris@76: str += ' />'; Chris@76: else Chris@76: str += '>' + getInnerHTML(node) + ''; Chris@76: break; Chris@76: Chris@76: // 2 is an attribute. Chris@76: Chris@76: // Just some text.. Chris@76: case 3: Chris@76: str += node.nodeValue; Chris@76: break; Chris@76: Chris@76: // A CDATA section. Chris@76: case 4: Chris@76: str += ''; Chris@76: break; Chris@76: Chris@76: // Entity reference.. Chris@76: case 5: Chris@76: str += '&' + node.nodeName + ';'; Chris@76: break; Chris@76: Chris@76: // 6 is an actual entity, 7 is a PI. Chris@76: Chris@76: // Comment. Chris@76: case 8: Chris@76: str += ''; Chris@76: break; Chris@76: } Chris@76: Chris@76: return str; Chris@76: } Chris@76: Chris@76: // Checks for variable in theArray. Chris@76: function in_array(variable, theArray) Chris@76: { Chris@76: for (var i = 0; i < theArray.length; i++) Chris@76: { Chris@76: if (theArray[i] == variable) Chris@76: return true; Chris@76: } Chris@76: return false; Chris@76: } Chris@76: Chris@76: // Find a specific radio button in its group and select it. Chris@76: function selectRadioByName(radioGroup, name) Chris@76: { Chris@76: if (typeof(radioGroup.length) == "undefined") Chris@76: return radioGroup.checked = true; Chris@76: Chris@76: for (var i = 0; i < radioGroup.length; i++) Chris@76: { Chris@76: if (radioGroup[i].value == name) Chris@76: return radioGroup[i].checked = true; Chris@76: } Chris@76: Chris@76: return false; Chris@76: } Chris@76: Chris@76: // Invert all checkboxes at once by clicking a single checkbox. Chris@76: function invertAll(headerfield, checkform, mask) Chris@76: { Chris@76: for (var i = 0; i < checkform.length; i++) Chris@76: { Chris@76: if (typeof(checkform[i].name) == "undefined" || (typeof(mask) != "undefined" && checkform[i].name.substr(0, mask.length) != mask)) Chris@76: continue; Chris@76: Chris@76: if (!checkform[i].disabled) Chris@76: checkform[i].checked = headerfield.checked; Chris@76: } Chris@76: } Chris@76: Chris@76: // Keep the session alive - always! Chris@76: var lastKeepAliveCheck = new Date().getTime(); Chris@76: function smf_sessionKeepAlive() Chris@76: { Chris@76: var curTime = new Date().getTime(); Chris@76: Chris@76: // Prevent a Firefox bug from hammering the server. Chris@76: if (smf_scripturl && curTime - lastKeepAliveCheck > 900000) Chris@76: { Chris@76: var tempImage = new Image(); Chris@76: tempImage.src = smf_scripturl + (smf_scripturl.indexOf("?") == -1 ? "?" : "&") + "action=keepalive;" + curTime; Chris@76: lastKeepAliveCheck = curTime; Chris@76: } Chris@76: Chris@76: window.setTimeout("smf_sessionKeepAlive();", 1200000); Chris@76: } Chris@76: window.setTimeout("smf_sessionKeepAlive();", 1200000); Chris@76: Chris@76: // Set a theme option through javascript. Chris@76: function smf_setThemeOption(option, value, theme, cur_session_id) Chris@76: { Chris@76: // Compatibility. Chris@76: if (cur_session_id == null) Chris@76: cur_session_id = smf_session_id; Chris@76: Chris@76: var tempImage = new Image(); Chris@76: 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: } Chris@76: Chris@76: function smf_avatarResize() Chris@76: { Chris@76: var possibleAvatars = document.getElementsByTagName ? document.getElementsByTagName("img") : document.all.tags("img"); Chris@76: Chris@76: for (var i = 0; i < possibleAvatars.length; i++) Chris@76: { Chris@76: if (possibleAvatars[i].className != "avatar") Chris@76: continue; Chris@76: Chris@76: var tempAvatar = new Image(); Chris@76: tempAvatar.src = possibleAvatars[i].src; Chris@76: Chris@76: if (smf_avatarMaxWidth != 0 && tempAvatar.width > smf_avatarMaxWidth) Chris@76: { Chris@76: possibleAvatars[i].height = (smf_avatarMaxWidth * tempAvatar.height) / tempAvatar.width; Chris@76: possibleAvatars[i].width = smf_avatarMaxWidth; Chris@76: } Chris@76: else if (smf_avatarMaxHeight != 0 && tempAvatar.height > smf_avatarMaxHeight) Chris@76: { Chris@76: possibleAvatars[i].width = (smf_avatarMaxHeight * tempAvatar.width) / tempAvatar.height; Chris@76: possibleAvatars[i].height = smf_avatarMaxHeight; Chris@76: } Chris@76: else Chris@76: { Chris@76: possibleAvatars[i].width = tempAvatar.width; Chris@76: possibleAvatars[i].height = tempAvatar.height; Chris@76: } Chris@76: } Chris@76: Chris@76: if (typeof(window_oldAvatarOnload) != "undefined" && window_oldAvatarOnload) Chris@76: { Chris@76: window_oldAvatarOnload(); Chris@76: window_oldAvatarOnload = null; Chris@76: } Chris@76: } Chris@76: Chris@76: function hashLoginPassword(doForm, cur_session_id) Chris@76: { Chris@76: // Compatibility. Chris@76: if (cur_session_id == null) Chris@76: cur_session_id = smf_session_id; Chris@76: Chris@76: if (typeof(hex_sha1) == "undefined") Chris@76: return; Chris@76: // Are they using an email address? Chris@76: if (doForm.user.value.indexOf("@") != -1) Chris@76: return; Chris@76: Chris@76: // Unless the browser is Opera, the password will not save properly. Chris@76: if (typeof(window.opera) == "undefined") Chris@76: doForm.passwrd.autocomplete = "off"; Chris@76: Chris@76: 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: Chris@76: // It looks nicer to fill it with asterisks, but Firefox will try to save that. Chris@76: if (navigator.userAgent.indexOf("Firefox/") != -1) Chris@76: doForm.passwrd.value = ""; Chris@76: else Chris@76: doForm.passwrd.value = doForm.passwrd.value.replace(/./g, "*"); Chris@76: } Chris@76: Chris@76: function hashAdminPassword(doForm, username, cur_session_id) Chris@76: { Chris@76: // Compatibility. Chris@76: if (cur_session_id == null) Chris@76: cur_session_id = smf_session_id; Chris@76: Chris@76: if (typeof(hex_sha1) == "undefined") Chris@76: return; Chris@76: Chris@76: doForm.admin_hash_pass.value = hex_sha1(hex_sha1(username.toLowerCase() + doForm.admin_pass.value) + cur_session_id); Chris@76: doForm.admin_pass.value = doForm.admin_pass.value.replace(/./g, "*"); Chris@76: } Chris@76: Chris@76: function ajax_indicator(turn_on) Chris@76: { Chris@76: var indicator = document.getElementById("ajax_in_progress"); Chris@76: if (indicator != null) Chris@76: { Chris@76: if (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf("MSIE 7") == -1) Chris@76: { Chris@76: indicator.style.top = document.documentElement.scrollTop; Chris@76: } Chris@76: indicator.style.display = turn_on ? "block" : "none"; Chris@76: } Chris@76: }