// JavaScript Document
/*
Fix IE's bad implementation of getElementById()

This renames the old getElementById to oldGetElementById, compares the
id returned, and (if they differ) loops through all the elements to
find the correct element!
*/
/* Put on the backburner... need to find how to inherit the methods and properties of getElementById
document.xgetElementById = document.getElementById;
document.getElementById = function(idElement) {
	var element;
	if(element = document.xgetElementById(idElement)){
		if(element.id != idElement){
			var allElements = document.getElementsByTagName("*");
			for (var i=0; i < allElements.length; i++) {
				if (allElements[i].id == idElement) {
					return allElements[i];
				}
			}
		} else if((idElement != null) && (element != null)) {
			return element;
		} else {
			return null;
		}
	} else {
		return null;
	}
}
*/
/*
USAGE:
addLoadEvent(function_name); 

Where 'function_name' is the name of the function you wish to run onload

DESCRIPTION:
Adds a function to the onload call dynamically.
*/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/*
USAGE:
toggleDisplay(element_id, [new_element_state(show|hide)]);

Where 'elementId' is the id of the element you wish to change.
Optional: 'elementState' is the new state of the element.

DESCRIPTION:
This function sets the class of the selected element to "show" or "hide" depending on current state of the element OR the state given by 'elementState'.
The primary purpose of this function is to show or hide elements on a web page.
*/
function toggleDisplay (elementId, elementState, textElementId, elementText ){
	var element;
	if(element = document.getElementById(elementId)){
		var txtState;
		if(elementState == "show" || elementState == "hide"){ 
			element.className = element.className.replace(/ show\b/, "");
			element.className = element.className.replace(/ hide\b/, "");
			element.className = element.className.replace(/show\b/, "");
			element.className = element.className.replace(/hide\b/, "");
			switch(elementState){
				case 'show':
					element.className += " show";
					txtState = "hide";
				break;
				case 'hide':
				default:
					element.className += " hide";
					txtState = "show";
				break;
			}
		} else {
			var elementClassName = element.className.toLowerCase();
			element.className = element.className.replace(/ show\b/, "");
			element.className = element.className.replace(/ hide\b/, "");
			element.className = element.className.replace(/show\b/, "");
			element.className = element.className.replace(/hide\b/, "");
			if (elementClassName.indexOf("hide") != -1){
					element.className += " show";
					txtState = "hide";
			} else {
					element.className += " hide";
					txtState = "show";
			}
		}
		if(textElement = document.getElementById(textElementId)){
			textElement.innerHTML = elementText.replace("[state]", txtState);
		}
	}
}

function clone(elementId, refId){
	var element = document.getElementById(elementId);
	var ref = document.getElementById(refId);
	var div = element.childNodes[3].cloneNode(true);
	var del = document.createElement("INPUT");
	del.setAttribute("type", "button");
	del.setAttribute("value", "delete");
	del.onclick = function(){
		this.parentNode.parentNode.removeChild(this.parentNode);
	}
	div.insertBefore(del,div.getElementsByTagName('hr')[0]);
	element.insertBefore(div,ref);
}

//External Links Array
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}

addLoadEvent(externalLinks);