کاربر:Shervinafshar/EsfahbodTools.js

از ویکی‌پدیا، دانشنامهٔ آزاد.

نکته: پس از ذخیره‌سازی ممکن است برای دیدن تغییرات نیاز باشد که حافظهٔ نهانی مرورگر خود را خالی کنید. موزیلا / فایرفاکس / Safari: کلید Shift را نگه‌دارید و روی دکمهٔ Reload کلیک کنید، یا کلید‌های Ctrl-Shift-R را با هم فشار دهید (در رایانه‌های اپل مکینتاش کلید‌های Cmd-Shift-Rاینترنت اکسپلورر: کلید Ctrl نگه‌دارید و روی دکمهٔ Refresh کلیک‌ کنید، یا کلید‌های Ctrl-F5 را با هم فشار دهید؛ Konqueror: روی دکمهٔ Reload کلیک کنید و یا کلید F5 را فشار دهید؛ اُپرا: کاربران اُپرا ممکن است لازم باشد که بطور کامل حافظهٔ نهانی مرورگر را در منوی Tools→Preferences خالی کنند.

/*<pre>*/
/* tools.js  Some random JavaScript tools
 * Copyright (C) 2003  Behdad Esfahbod <js@behdad.org>
 * Copyright (C) 2003  Behnam Esfahbod <behnam@esfahbod.info>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You can receive a copy of GNU Lesser General Public License at the
 * World Wide Web address <http://www.gnu.org/licenses/lgpl.html>.
 *
 * For licensing issues, contact The FarsiWeb Project Group,
 * Computing Center, Sharif University of Technology,
 * PO Box 11365-8515, Tehran, Iran, or contact us the
 * email address <FWPG@sharif.edu>.
 */

function setFocusToFirstInput () {
  for (var i = 0;  i < document.forms.length;  i++)
    for (var j = 0;  j < document.forms[i].elements.length;  j++)
      if (   document.forms[i].elements[j].type != 'hidden'
          && document.forms[i].elements[j].style.display != 'none'
          && document.forms[i].elements[j].style.visibility != 'hidden'
          && document.forms[i].elements[j].className == 'wrong'
         ) {
        document.forms[i].elements[j].focus();
	if (   document.forms[i].elements[j].type == 'text'
            || document.forms[i].elements[j].type == 'password'
           )
          document.forms[i].elements[j].select();
        return true;
      }

  for (var i = 0;  i < document.forms.length;  i++)
    for (var j = 0;  j < document.forms[i].elements.length;  j++)
      if (   document.forms[i].elements[j].type != 'hidden'
          && document.forms[i].elements[j].style.display != 'none'
          && document.forms[i].elements[j].style.visibility != 'hidden'
         ) {
        document.forms[i].elements[j].focus();
	if (   document.forms[i].elements[j].type == 'text'
            || document.forms[i].elements[j].type == 'password'
           )
          document.forms[i].elements[j].select();
        return true;
      }
  return true;
}


function fromEntityToUtf8 (obj) {
  
  obj.value = obj.value.replace(/&#([0-9]+);/g,
    function(s, n, ofs, all) {
      return String.fromCharCode(n);
    }
  );
}

function fromUtf8ToEntity (obj_id) {
  var obj = document.getElementById (obj_id);
  obj.value = obj.value.replace(/([^\x00-\x7f])/g,
    function(s, c, ofs, all) {
      c = String(c);
      return "&#"+c.charCodeAt(0)+";";
    }
  );
}

function escapeHTMLEntities (obj_id) {
  var obj = document.getElementById (obj_id);
  s = obj.value;
  s = s.replace(/&/g, '&amp;');
  s = s.replace(/</g, '&lt;');
  s = s.replace(/>/g, '&gt;');
  s = s.replace(/'/g, '&apos;');
  s = s.replace(/"/g, '&quot;');
  obj.value = s;
}

function unescapeHTMLEntities (obj_id) {
  var obj = document.getElementById (obj_id);
  s = obj.value;
  s = s.replace(/&lt;/g, '<');
  s = s.replace(/&gt/g, '<');
  s = s.replace(/&apos;/g, '\'');
  s = s.replace(/&quot;/g, '"');
  s = s.replace(/&amp;/g, '&');
  obj.value = s;
}

/*</pre>*/