function ltrim(str)
{
  var charList = "\\s";
  if (arguments.length > 1)
    charList = arguments[1];
  var regExp = new RegExp('^['+charList+']*');
  return str.replace(regExp, '');
}

function rtrim(str)
{
  var charList = "\\s";
  if (arguments.length > 1)
    charList = arguments[1];
  var regExp = new RegExp('['+charList+']*$');
  return str.replace(regExp, '');
}

function trim(str)
{
  var charList = "\\s";
  if (arguments.length > 1)
    charList = arguments[1];
  return ltrim(rtrim(str, charList), charList);
}

function isEmailFormat(objInput){
         var str = fAllTrim(objInput.value);
         var supported = 0;
          if (window.RegExp) {
            var tempStr = "a";
            var tempReg = new RegExp(tempStr);
            if (tempReg.test(tempStr)) supported = 1;
          }
          if (!supported)
            return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
          var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
          var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
          var r3 = new RegExp("\\*");
          return (!r1.test(str) && r2.test(str) && !r3.test(str));
}


function indexOf(obj, key)
{
    //showProperties(obj)
  for (var i in obj){
    if (obj[i] == key)
      return i;
  }
  return false;

   /*  
     for (i=0; i<arr.length; i++){
        alert(arr[i])
      if (arr[i] == key)
      return i;
     }
     return false;
     */
}


function $() {
   if (arguments.length == 1) return get$(arguments[0]);
   var elements = [];
   $c(arguments).each(function(el){
      elements.push(get$(el));
   });
   return elements;

   function get$(el){
      if (typeof el == 'string') el = document.getElementById(el);
      return el;
   }
}

function showProperties(obj, objName)
{
      var result = "";
      for (var i in obj)
          result += objName + "." + i + " = " + obj[i] + "\n";
      alert(result);
}

function addLoadEvent(func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
                window.onload = func;
        }
        else {
                window.onload = function() {
                        oldonload();
                        func();
                }
        }
}

function encodeNumeric(s)
{
  var i, l, e, o = '', c;
  
  for (i=0, l=s.length; i<l; i++) {
    c = s.charCodeAt(i);

    if (c > 127 || c == 60 || c == 62 || c == 38 || c == 39 || c == 34)
      o += '&#' + c + ";";
    else
      o += String.fromCharCode(c);
  }
  return o;
}

function getElementsByIdMask(node, mask)
{
  var elements = new Array();
  var regExp = new RegExp(mask);
  var id = (node.id ? ''+node.id : '');
  if (id.match(regExp))
    elements[elements.length] = node;
  if (node.nodeType == 1)    // 1: ELEMENT_NODE
    if (node = node.firstChild)
      do {
        var newElements = getElementsByIdMask(node, mask);
        elements = elements.concat(newElements);
      } while (node = node.nextSibling);
  return elements;
}

// css+
function btn_press(btn)
{
  btn.className = btn.className.replace(/ (rev|usu)/,'');
  btn.className += ' rev';
  //alert(btn.className)
}

function btn_release(btn)
{
  btn.className = btn.className.replace(/ (rev|usu)/,'');
  btn.className += ' usu';
  //alert(btn.className)
}

function preventPropagate(e)
{
	if (!e) e = window.event;
	e.cancelBubble = true;
	e.returnValue = false;
	if (e.stopPropagation)
		e.stopPropagation();
}

function openTranslatePopup(lang, key)
{
  window.open('/?popup=translate&l='+lang+'&k='+key, 'translate_popup', 'width=500,height=400,resizable=1,location=1,menubar=1');
}

// reloads page and adds url parameter for noncacheable result
function myPageReload()
{
  // at first removing 'nc' parameter if exists
  var url = document.location.href.replace(/\?|&nc=[^&]+/, '');
  url += (url.indexOf('?') == -1 ? '?' : '&') + 'nc=' + Math.random();
  document.location.href = url;
  return false;
}