
function xb_get_element(thing)
{
  if (typeof(thing) == 'undefined' || !thing)
  {
    return null;
  }
  else if (typeof(thing.tagName) == 'undefined' || !thing.tagName)
  {
    if (document.all)
      el = document.getElementById(thing) ? document.getElementById(thing) : document.all[thing];
    else
      el = document.getElementById(thing);

    if (el)
      return el;
    else
      return null;
  }
  else if (thing.tagName)
  {
    return thing;
  }
  else
  {
    return null;
  }
}


function addLoadEvent(func)
{
  if (typeof(func) != 'function')
    return;

  var oldonload = window.onload;

  if (typeof window.onload != 'function')
    window.onload = func;
  else
  {
    window.onload = function()
    {
      if (oldonload)
        oldonload();

      func();
    }
  }
}


function unhide_selects()
{
  if (!is_IE() || is_IE7())
    return;

  var iframe = xb_get_element('ie_iframe');

  if (iframe)
    iframe.parentNode.removeChild(iframe);
  else
  {
    var s = document.getElementsByTagName('select');
    for (var i = 0; i < s.length; ++i)
      s[i].style.display = '';
  }
}

/**
Checks if an element has a class
@param  el              element to check. Can be the id or the actual element
@param  searched_class  class to checked

@return boolean
**/
function has_class(el, searched_class)
{
  if (typeof(searched_class) == 'undefined')
    return false;

  // check valid parameter
 el = xb_get_element(el);

  if (!el)
    return false;

  var class_array = el.className.split(' ');

  for(var i in class_array)
  {
    if (class_array[i] == searched_class)
      return true;
  }
  return false;
}

/**
Adds a class to an element
@param  el            element whose class to add. Can be the id or the actual element
@param  add_class     class to be added

@return nothing
**/
function add_class(el, added_class)
{
  if (typeof(added_class) == 'undefined')
    return false;

  // check valid parameter
  el = xb_get_element(el);

  // if element doesn't exist, or class already there, or no class to add
  if (!el || has_class(el, added_class))
      return false;

  el.className = el.className + ' ' + added_class;

  return true;
}

/**
Removes a class from an element
@param  el            element whose class to remove. Can be the id or the actual element
@param  remove_class  class to be removed

@return nothing
**/
function remove_class(el, removed_class)
{
  if (typeof(removed_class) == 'undefined')
    return false;

  // check valid parameter
  el = xb_get_element(el);

  // if element doesn't exist, or class not there, or no class to remove
  if (!el)
    return false;

  var class_array = el.className.split(' ');
  var class_changed = false;

  for (var i in class_array)
  {
    if (class_array[i] == removed_class)
    {
      // delete removed_class
      class_array.splice(i, 1);
      class_changed = true;
    }
  }

  if (class_changed)
    el.className = class_array.join(' ');

  return class_changed;
}

/**
Changes class of element
@param  el            element whose class to toggle. Can be the id or the actual element
@param  add_class     class to be added
@param  remove_class  clas to be removed

@return nothing
**/
// function toggle_class(el, added_class, removed_class)
// {
//   if (added_class)
//     add_class(el, added_class);
//
//   if (removed_class)
//     remove_class(el, removed_class);
// }

// new function to be tested
function toggle_class(el, added_class, removed_class)
{
  // check if adding
  add = false;
  if (typeof(added_class) != 'undefined' && added_class)
    add = true;

  // check if removing
  remove = false;
  if (typeof(removed_class) != 'undefined' && removed_class)
    remove = true;

  // if not adding or removing return
  if (!add && !remove )
    return false;

  el = xb_get_element(el);

  // if element doesn't exist
  if (!el)
    return false;

  var class_array = el.className.split(' ');
  var class_changed = false;

  for (var i in class_array)
  {
    if (remove && class_array[i] == removed_class)
    {
      // delete removed_class
      class_array.splice(i, 1);
      class_changed = true;
    }

    if (add && class_array[i] == added_class)
      add = false;
  }

  if (add)
  {
    class_array[class_array.length] = added_class;
    class_changed = true;
  }

  if (class_changed)
    el.className = class_array.join(' ');

  return class_changed;
}

function is_IE()
{
  return (navigator.appName.indexOf("Microsoft Internet Explorer") > -1);
};

function is_IE6()
{
  return (navigator.appName.indexOf("Microsoft Internet Explorer") > -1 && !window.XMLHttpRequest)
}

function is_IE7()
{
  return (navigator.appName.indexOf("Microsoft Internet Explorer") > -1 && window.XMLHttpRequest)
}

// clones an event and positioning attributes
// @param   obj   the object to clone
// @return  the cloned object
function clone_obj(obj)
{
  if (typeof(obj) != 'object')
    return null;

  try
  {
    var new_obj = new Object;

    if (typeof(obj.pageX) != 'undefined')
      new_obj['pageX'] = obj.pageX;

    if (typeof(obj.pageY) != 'undefined')
      new_obj['pageY'] = obj.pageY;

    if (typeof(obj.clientX) != 'undefined')
      new_obj['clientX'] = obj.clientX;

    if (typeof(obj.clientY) != 'undefined')
      new_obj['clientY'] = obj.clientY;

    return new_obj;
  }
  catch (e)
  {
    return null;
  }
}
// DOMUtils namespace
function DOMUtils() {};

// assigns inner HTML to element by id, and does error checking
DOMUtils.fill_element = function(id, html)
{
  var el = xb_get_element(id);
  if (el && typeof(html) != 'undefined' && html != null)
    el.innerHTML = html;
}
// returns inner html of an element
DOMUtils.get_inner_html = function(id)
{
  var el = xb_get_element(id);
  if (el && typeof(el.innerHTML) != 'undefined')
    return el.innerHTML;
  return '';
}
// removes from dom
DOMUtils.remove_element = function(id)
{
  var el = xb_get_element(id);
  if (el && el.parentNode)
    el.parentNode.removeChild(el);
}
// disables a button
DOMUtils.disable_button = function(btn, dis)
{
  if (typeof(btn) != 'undefined' && btn.disabled)
    btn.disabled = dis;
}
// sets the value of an element
DOMUtils.set_value = function(elm, val)
{
  var element = xb_get_element(elm);
  if (element && typeof element.value !== 'undefined')
  {
    element.value = val;
    return true;
  }
  return false;
}
// focuses an element
DOMUtils.focus = function(elm)
{
  var element = xb_get_element(elm);
  if (typeof(element.focus) === 'function')
    element.focus();
}
// hides selects under element for ie
// @param el  the element under which to hide the selects
// @param left    optional. the left position of the element
// @param top     optional. the top position of the element
// @param width   optional. the width of the element
// @param height  optional. the height of the element
DOMUtils.hide_selects_by_el = function(el, left, top, width, height)
{
  if (!is_IE() || is_IE7())
    return;

  var iframe_id = 'ie_iframe';

  var el = xb_get_element(el);
  if (el)
  {
    var iframe = xb_get_element(iframe_id);

    if (iframe)
      iframe.parentNode.removeChild(iframe);

    if (typeof(left) == 'undefined' || !left)
    {
      var displacement = 0;

      // the inside of the popups are left -3px so we need to move the
      // iframe by that much so that it doesn't peep through the shadow
      // but we don't want that for the top nav iframe or pulldown menu classes
      if (el.parentNode.id.indexOf('nav_') < 0 && el.id.indexOf('f_')!=0)
        var displacement = -3;
      if (el.id=='autocomplete_panel') // autocomplete popup
        var displacement = 1;

      var left = el.style.left;

      if (typeof(left) == 'undefined' || !left)
        left = findPosX(el) + displacement + 'px';
      else
        left = parseInt(left) + displacement + 'px';//Number(left.substring(0, left.length - 2)) - 3 + 'px';
    }

    if (typeof(top) == 'undefined' || !top)
    {
      var displacement = 0;

      if (el.id=='autocomplete_panel') // autocomplete popup
        var displacement = 1;

      var top = el.style.top;

      if (typeof(top) == 'undefined' || !top)
        top = findPosY(el) + displacement + 'px';
    }

    if (typeof(width) == 'undefined' || !width || typeof(height) == 'undefined' || !height)
    {
      var dim = get_object_dimensions(el);
      var width = dim.width + 'px';
      var height = dim.height + 'px';
    }

    var top_z_index = el.style.zIndex;
    if (typeof(top_z_index) == 'undefined' || !top_z_index)
      top_z_index = 995;

    // alert(el.parentNode.tagName);
    iframe = document.createElement('iframe');

    iframe.id = iframe_id;
    iframe.style.position = 'absolute';
    iframe.style.left = left;
    iframe.style.top = top;
    iframe.style.height = height;
    iframe.style.width = width;
    iframe.style.zIndex = top_z_index - 1;
    el.style.zIndex = top_z_index;
    // iframe.style.display = 'block';
    // iframe.style.filter = 'mask()';

    var parent = el.parentNode;

    if (el.id=='autocomplete_panel') // autocomplete popup
      parent = xb_get_element('nav_newsfeed');

    if (typeof(parent) == 'undefined' || !parent || !parent.tagName)
    {
      document.appendChild(iframe);
    }
    else if (parent.tagName == 'LI')
    {
      parent = parent.parentNode;

      if (typeof(parent) == 'undefined' || !parent)
        document.appendChild(iframe);
      else
        parent.appendChild(iframe);
    }
    else
    {
      parent.appendChild(iframe);
    }

    // var left = findPosX(el);
    // var top = findPosY(el);
    // var dim = get_object_dimensions(el);
    // var width = dim.width;
    // var height = dim.height;
    //
    // hide_selects(left, top, width, height);
    //
    // // if we hid the element's selects
    // if (el.getElementsByTagName('select'))
    // {
    //   unhide_el_selects(el);
    // }
  }
}


