// JavaScript Document
/*
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 'element_id' is the id of the element you wish to change.
Optional: 'new_element_state' 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 'new_element_state'.
The primary purpose of this function is to show or hide elements on a web page.
*/
function toggleDisplay (elementId, elementState, textElementId, elementText ){
	var element = document.getElementById(elementId);
	var txtState;
	if(elementState == "show" || elementState == "hide"){ 
		switch(elementState){
			case 'show':
				element.className = "show";
				txtState = "hide";
			break;
			case 'hide':
			default:
				element.className = "hide";
				txtState = "show";
			break;
		}
	} else {
		switch(element.className.toLowerCase()){
			case 'hide':
				element.className = "show";
				txtState = "hide";
			break;
			case 'show':
			default:
				element.className = "hide";
				txtState = "show";
			break;
		}
	}
	if(textElement = document.getElementById(textElementId)){
		textElement.innerHTML = elementText.replace("[state]", txtState);
	}
}

function clone(elementId, exclude){
	var element = document.getElementById(elementId);
	var div = element.childNodes[3].cloneNode(true);
	function delHidden(element, exclude){
		for(i=0; element.childNodes.length; i++){
			if (element.childNodes[i].type = exclude){
				element.removeChild(element.childNodes[i]);
			}
			if (element.childNodes[i].childNodes.length != 0){
				delHidden(div.childNodes[i]);
			}
		}
	}
	var label = document.createElement("LABEL");
	var del = document.createElement("INPUT");
	del.setAttribute("type", "button");
	del.setAttribute("value", "delete");
	del.onclick = function(){
		this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
	}
	var p = document.createElement("P");
	p.appendChild(label);
	p.appendChild(del);
	div.appendChild(p);
	function del_noClone(element){
		for(var i=0; i < element.childNodes.length; i++){
			if(element.childNodes[i].className == "noClone"){
				element.childNodes[i].parentNode.removeChild(element.childNodes[i]);
			} else if(element.childNodes[i].hasChildNodes()){
				del_noClone(element.childNodes[i]);
			}
		}
	}
	del_noClone(div);
	element.insertBefore(div,element.getElementsByTagName('p')[(element.getElementsByTagName('p').length - 1)]);
}

function disable(element){
	for (var i=0; i < element.childNodes.length; i++){
		if ((element.childNodes[i].tagName == "INPUT" && element.childNodes[i].type != "button") || element.childNodes[i].tagName == "TEXTAREA"){
			element.childNodes[i].setAttribute("disabled", "disabled");
		}
		if (element.childNodes[i].tagName == "INPUT" && element.childNodes[i].name.substr(0, 3) == "rem"){
			element.childNodes[i].removeAttribute("disabled");
		}
		if (element.childNodes[i].tagName == "INPUT" && element.childNodes[i].name == "delete"){
			element.childNodes[i].value = element.childNodes[i].value.replace("delete", "undelete");
			element.childNodes[i].setAttribute("onclick","enable(this.parentNode.parentNode);");
		}
		if (element.childNodes[i].childNodes.length != 0){
			disable(element.childNodes[i]);
		}
	}
}

function enable(element){
	for (var i=0; i < element.childNodes.length; i++){
		if ((element.childNodes[i].tagName == "INPUT" && element.childNodes[i].type != "button") || element.childNodes[i].tagName == "TEXTAREA"){
			element.childNodes[i].removeAttribute("disabled");
		}
		if (element.childNodes[i].tagName == "INPUT" && element.childNodes[i].name.substr(0, 3) == "rem"){
			element.childNodes[i].setAttribute("disabled", "disabled");
		}
		if (element.childNodes[i].tagName == "INPUT" && element.childNodes[i].name == "delete"){
			element.childNodes[i].value = element.childNodes[i].value.replace("undelete", "delete");
			element.childNodes[i].setAttribute("onclick","disable(this.parentNode.parentNode);");
		}
		if (element.childNodes[i].childNodes.length != 0){
			enable(element.childNodes[i]);
		}
	}
}

//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);