profile.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. var localTime = new Date();
  2. function autoDetectTimeOffset(currentTime)
  3. {
  4. if (typeof(currentTime) != 'string')
  5. var serverTime = currentTime;
  6. else
  7. var serverTime = new Date(currentTime);
  8. // Something wrong?
  9. if (!localTime.getTime() || !serverTime.getTime())
  10. return 0;
  11. // Get the difference between the two, set it up so that the sign will tell us who is ahead of who.
  12. var diff = Math.round((localTime.getTime() - serverTime.getTime())/3600000);
  13. // Make sure we are limiting this to one day's difference.
  14. diff %= 24;
  15. return diff;
  16. }
  17. // Prevent Chrome from auto completing fields when viewing/editing other members profiles
  18. function disableAutoComplete()
  19. {
  20. if (is_chrome && document.addEventListener)
  21. document.addEventListener("DOMContentLoaded", disableAutoCompleteNow, false);
  22. }
  23. // Once DOMContentLoaded is triggered, call the function
  24. function disableAutoCompleteNow()
  25. {
  26. for (var i = 0, n = document.forms.length; i < n; i++)
  27. {
  28. var die = document.forms[i].elements;
  29. for (var j = 0, m = die.length; j < m; j++)
  30. // Only bother with text/password fields?
  31. if (die[j].type == "text" || die[j].type == "password")
  32. die[j].setAttribute("autocomplete", "off");
  33. }
  34. }
  35. function calcCharLeft()
  36. {
  37. var oldSignature = "", currentSignature = document.forms.creator.signature.value;
  38. var currentChars = 0;
  39. if (!document.getElementById("signatureLeft"))
  40. return;
  41. if (oldSignature != currentSignature)
  42. {
  43. oldSignature = currentSignature;
  44. var currentChars = currentSignature.replace(/\r/, "").length;
  45. if (is_opera)
  46. currentChars = currentSignature.replace(/\r/g, "").length;
  47. if (currentChars > maxLength)
  48. document.getElementById("signatureLeft").className = "error";
  49. else
  50. document.getElementById("signatureLeft").className = "";
  51. if (currentChars > maxLength && !$("#profile_error").is(":visible"))
  52. ajax_getSignaturePreview(false);
  53. else if (currentChars <= maxLength && $("#profile_error").is(":visible"))
  54. {
  55. $("#profile_error").css({display:"none"});
  56. $("#profile_error").html('');
  57. }
  58. }
  59. setInnerHTML(document.getElementById("signatureLeft"), maxLength - currentChars);
  60. }
  61. function ajax_getSignaturePreview (showPreview)
  62. {
  63. showPreview = (typeof showPreview == 'undefined') ? false : showPreview;
  64. $.ajax({
  65. type: "POST",
  66. url: smf_scripturl + "?action=xmlhttp;sa=previews;xml",
  67. data: {item: "sig_preview", signature: $("#signature").val(), user: $('input[name="u"]').attr("value")},
  68. context: document.body,
  69. success: function(request){
  70. if (showPreview)
  71. {
  72. var signatures = new Array("current", "preview");
  73. for (var i = 0; i < signatures.length; i++)
  74. {
  75. $("#" + signatures[i] + "_signature").css({display:""});
  76. $("#" + signatures[i] + "_signature_display").css({display:""}).html($(request).find('[type="' + signatures[i] + '"]').text() + '<hr />');
  77. }
  78. }
  79. if ($(request).find("error").text() != '')
  80. {
  81. if (!$("#profile_error").is(":visible"))
  82. $("#profile_error").css({display: "", position: "fixed", top: 0, left: 0, width: "100%"});
  83. var errors = $(request).find('[type="error"]');
  84. var errors_html = '<span>' + $(request).find('[type="errors_occurred"]').text() + '</span><ul class="reset">';
  85. for (var i = 0; i < errors.length; i++)
  86. errors_html += '<li>' + $(errors).text() + '</li>';
  87. errors_html += '</ul>';
  88. $(document).find("#profile_error").html(errors_html);
  89. }
  90. else
  91. {
  92. $("#profile_error").css({display:"none"});
  93. $("#profile_error").html('');
  94. }
  95. return false;
  96. },
  97. });
  98. return false;
  99. }
  100. function changeSel(selected)
  101. {
  102. if (cat.selectedIndex == -1)
  103. return;
  104. if (cat.options[cat.selectedIndex].value.indexOf("/") > 0)
  105. {
  106. var i;
  107. var count = 0;
  108. file.style.display = "inline";
  109. file.disabled = false;
  110. for (i = file.length; i >= 0; i = i - 1)
  111. file.options[i] = null;
  112. for (i = 0; i < files.length; i++)
  113. if (files[i].indexOf(cat.options[cat.selectedIndex].value) == 0)
  114. {
  115. var filename = files[i].substr(files[i].indexOf("/") + 1);
  116. var showFilename = filename.substr(0, filename.lastIndexOf("."));
  117. showFilename = showFilename.replace(/[_]/g, " ");
  118. file.options[count] = new Option(showFilename, files[i]);
  119. if (filename == selected)
  120. {
  121. if (file.options.defaultSelected)
  122. file.options[count].defaultSelected = true;
  123. else
  124. file.options[count].selected = true;
  125. }
  126. count++;
  127. }
  128. if (file.selectedIndex == -1 && file.options[0])
  129. file.options[0].selected = true;
  130. showAvatar();
  131. }
  132. else
  133. {
  134. file.style.display = "none";
  135. file.disabled = true;
  136. document.getElementById("avatar").src = avatardir + cat.options[cat.selectedIndex].value;
  137. document.getElementById("avatar").style.width = "";
  138. document.getElementById("avatar").style.height = "";
  139. }
  140. }
  141. function showAvatar()
  142. {
  143. if (file.selectedIndex == -1)
  144. return;
  145. document.getElementById("avatar").src = avatardir + file.options[file.selectedIndex].value;
  146. document.getElementById("avatar").alt = file.options[file.selectedIndex].text;
  147. document.getElementById("avatar").alt += file.options[file.selectedIndex].text == size ? "!" : "";
  148. document.getElementById("avatar").style.width = "";
  149. document.getElementById("avatar").style.height = "";
  150. }
  151. function previewExternalAvatar(src)
  152. {
  153. if (!document.getElementById("avatar"))
  154. return;
  155. var tempImage = new Image();
  156. tempImage.src = src;
  157. if (maxWidth != 0 && tempImage.width > maxWidth)
  158. {
  159. document.getElementById("avatar").style.height = parseInt((maxWidth * tempImage.height) / tempImage.width) + "px";
  160. document.getElementById("avatar").style.width = maxWidth + "px";
  161. }
  162. else if (maxHeight != 0 && tempImage.height > maxHeight)
  163. {
  164. document.getElementById("avatar").style.width = parseInt((maxHeight * tempImage.width) / tempImage.height) + "px";
  165. document.getElementById("avatar").style.height = maxHeight + "px";
  166. }
  167. document.getElementById("avatar").src = src;
  168. }