//
// Some useful functions
//

// jscript trim function
function atrim (s) {
    s = s.replace(/ /g, ' ');
    return s.replace(/(^\s+)|(\s+$)/g, "");
}
    
// encode as sort of urlencode
function encode_str (s) {
    r = "";
    for (i=0; i<s.length; i++) r = r + "~" + (""+(s.charCodeAt(i)));
    return r;
}
    
// flip element 
function show_hide_element ( id )
{
    var el = document.getElementById(id);
    if (!el) return;
    if (el.style.display == "none") el.style.display = "block"; else el.style.display = "none";
}
    
// XMLHttp function
function getXmlHttp() {
  var xmlhttp;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}

// Base URL
function getBaseURL() {
    var url = location.pathname;
    return url.substring(0, url.lastIndexOf('/'));
}

// IE can't use indexOf() - so we will use custom (other browsers are OK)
//
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

