﻿// attaches an event on an element (ie. <body onclick="" />)
var isIE = document.all? true : false;

function attachEventHandler(theEl, theEvent, theHandler, theObject)
{
  theEvent = theEvent.toLowerCase();
  if (isIE)
  {
    if (theObject)
      theEl.attachEvent(theEvent, function() { theHandler.call(theObject) } );
    else
      theEl.attachEvent(theEvent, theHandler);
  }
  else
  {
    if (theEl == document.body)
      theEl = document;
    if (theEvent == "ondragstart")
      theEvent = "ondraggesture";
    theEl.addEventListener(theEvent.substring(2), theHandler, true);
  }
}


// gets an array of elements
function getElementsByTagNameAttributeValue(ancestorEl, tagName, attrName, attrValue)
{
  attrName = attrName.toLowerCase();
  if (typeof(attrValue) == 'undefined')
    attrValue = null;
  var els = [];
  var a = ancestorEl.getElementsByTagName(tagName);
  for (var i=0; i<a.length; i++)
  {
    var el = a[i];
    if (attrName == "classname" && isIE)
      var val = el.className;
    else if (attrName == "classname")
      var val = el.getAttribute("class");
    else
      var val = el.getAttribute(attrName);
    if ((val != null) && ((attrValue == null) || (val == attrValue)))
      els.push(el);
  }
  return els;
};



// gets a parent element
function getParentElementByTagNameAttributeValue(el, tagName, attrName, attrValue)
{
  if (typeof(attrValue) == 'undefined')
    attrValue = null;

  tagName = tagName.toLowerCase();

  while (el && el.getAttribute && el.parentNode)
  {
    if (el.tagName && (el.tagName.toLowerCase() == tagName))
    {
      if (attrName == "className")
        var val = el.className;
      else
        var val = el.getAttribute(attrName);
      if ((val != null) && ((attrValue == null) || (val == attrValue)))
        return el;
    }
    el = el.parentNode;
  }

  return null;
};



