/* Javascript Library

Table of Contents:
void   addClass(element,value)                      - add a class to an element dynamically
void   addLoadEvent(fxn)                            - add a function to the window onload event
                                                    - (pass arguments with function(){fxn("me.com")})
array  getElementsByClass(searchClass,[node],[tag]) - gets elements by class optionally restricted by node or tag
void   insertAfter(newElement,targetElement)        - insert a new element after the specified target element 
object nextElement(node)                            - recursive function to get next element (analogous
                                                    - to nextSibling excluding text nodes)
void   prepareAnchors([link])                       - sets outside links to open in a new window
void   malert()                                     - creates a multi-line alert from additional arguments
void   seeObject(obj)                               - lists the property=value pairs of an (DOM) object
                                                    - in human readable form (useful for debugging)

*/

window.onload = function()
{ //runs after document is completed loaded into the browser window
   if (!document.getElementById || !document.getElementsByTagName || !document.createElement || !document.createTextNode) return false;
}

function addClass(element,value)
{ //add a class to an element dynamically
   if (!element.className)
      element.className = value;
   else
      element.className += " " + value;
}

function addLoadEvent(fxn,string)
{ //add a function to the window onload event (pass arguments with function(){fxn("me.com")})
   var onload = window.onload;
   if (typeof window.onload!="function")
      window.onload = fxn;
   else
      window.onload = function() {
         onload();
         fxn();
      }
}

function getElementsByClass(searchClass,node,tag)
{ //gets elements by class optionally restricted by node or tag
   node = (node==null)?document:node;
   tag = (tag==null)?"*":tag;
	 var elements = node.getElementsByTagName(tag);
	 var classElements = Array();
   var classes = searchClass.split(" ");
   var regexpString = "(^|\\s)" + classes[0];
   for (var i=1; i<classes.length; i++)
      regexpString += "|" + classes[i];
   regexpString += "(\\s|$)";
	 var classRegExp = RegExp(regexpString);
	 for (var i=0, j=0; i<elements.length; i++)
		   if (classRegExp.test(elements[i].className)) {
			   classElements[j] = elements[i];
			   j++;
		   }
	 return classElements;
}

function insertAfter(newElement,targetElement)
{ //insert a new element after the specified target element 
  var parent = targetElement.parentNode;
  if (parent.lastChild==targetElement)
    parent.appendChild(newElement);
  else
    parent.insertBefore(newElement,targetElement.nextSibling);
}

function nextElement(node)
{ //recursive function to get next element (analogous to nextSibling excluding text nodes)
   node = node.nextSibling;
   if (node)
      if (node.nodeType==1)
         return node;
      else
         return nextElement(node);
   else
      return null;
}

function prepareAnchors(website)
{ //sets outside links to open in a new window
   var anchors = document.getElementsByTagName("a");
   website = (website==null)?/repucci\.org/:RegExp(website.replace(/\./g,"\\."));
   for (var i=0; i<anchors.length; i++)
      if (!website.test(anchors[i].href))
         anchors[i].onclick = function() {
            window.open(this.href);
            return false;
         }
}

//functions useful for debugging
function malert(text)
{ //creates a multi-line alert from additional arguments
   for (var i=1; i<arguments.length; i++)
      text += "\n" + arguments[i];
   alert(text);
}

function seeObject(obj)
{ //lists the property=value pairs of an (DOM) object in human readable form
   if (obj) {
      var propList = Array();
      for (var prop in obj)
         propList = propList.concat(prop);
      propList = propList.sort();
      for (var i=0, propValues=""; i<propList.length; i++)
         propValues += propList[i] + " = " + obj[propList[i]] + "\n";
      propValues = propValues.replace(/(\{\s+)/g,"{");
      propValues = propValues.replace(/(\s+\})/g,"}");
      
      var seeObjectText = document.getElementById("seeObjectText");
      if (seeObjectText==null) {
         seeObjectText = document.createElement("pre");
         seeObjectText.style.position = "absolute";
         seeObjectText.style.top = "5px";
         seeObjectText.style.left = "20px";
         seeObjectText.style.background = "#CCCCCC";
         seeObjectText.style.border = "solid #333333 2px";
         document.body.appendChild(seeObjectText);
         
         var button = document.createElement("button");
         button.id = "seeObjectButton";
         button.style.position = "absolute";
         button.style.top = "5px";
         button.style.right = "5px";
         button.style.height = "20px";
         button.style.width = "20px";
         button.style.margin = "0px";
         button.style.padding = "0px";
         button.style.background = "#DD2222";
         button.style.color = "#FFFFFF";
         button.style.font = "10px/100% Arial Black, Impact, Arial";
         button.onclick = function() {
            this.parentNode.style.display = "none";
         }
         button.appendChild(document.createTextNode("X"));
         seeObjectText.appendChild(button);
      } else
         seeObjectText.style.display = "block";
      seeObjectText.appendChild(document.createTextNode(obj + " property = value\n\n" + propValues));
   }
   else
      alert("Object undefined.");
}