var included = [];
function include_once(script) {
  var script = jsDir + script + '.js';
  var append = true;
  for(var i=0; i<included.length; i++) {
    if(included[i] == script) {
      append = false;
    };
  };
  if(append) {
    var doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script);
    doc.appendChild(js);
    included.push(script);
    return false;
  };
};

//isdefined( object, variable )
function isdefined(o,v) {
  return (typeof(eval(o)[v]) != "undefined");
};

//addOnLoad(function name, include filename)
function addOnLoad(func,include) {
  if(include != null) {
    include_once(include);
  };
  if(window.onload) {
    var oldFunc = window.onload;
    window.onload = function() {
      oldFunc();
      eval(func);
    };
  };
};

//open url in current window (u:url)
function openURL(u) { document.location = u; };

//change background color on of element (e:element, c:color)
function bg_color(e,c) { e.style.backgroundColor=c; };

//toggle div display/block - toggleClass controls which class to hide
function toggleDiv (div, toggleClass) {
	var divs = document.getElementsByTagName('div');
	for (var x=0; x < divs.length; x++) {
		if (divs[x].className == toggleClass) {
			divs[x].style.display = "none";
		}
	}
	var tempDiv = document.getElementById(div);
	if (tempDiv.style.display == "block") {
		tempDiv.style.display = "none";
	} else {
		tempDiv.style.display = "block";
	}
}

function toggleActive (listItem, listItemParent) {
		var tempListItemParent = document.getElementById(listItemParent);
		for (x=0; x <tempListItemParent.childNodes.length; x++) {
			if (tempListItemParent.childNodes[x].className == 'active') {
				tempListItemParent.childNodes[x].className = '';
			} else if (tempListItemParent.childNodes[x].className == 'first active') {
				tempListItemParent.childNodes[x].className = 'first';
			} else if (tempListItemParent.childNodes[x].className == 'last active') {
				tempListItemParent.childNodes[x].className = 'last';
			}
		}
		var tempListItem = document.getElementById(listItem);
		if (tempListItem.className == '') {
			tempListItem.className = 'active';
		} else if (tempListItem.className == 'first') {
			tempListItem.className = 'first active';
		} else if (tempListItem.className == 'last') {
			tempListItem.className = 'last active';
		}
}

function countWords(content){
	var i=0;
	var numberofwords=1;
	while(i<=content.length) {
		if (content.substring(i,i+1) == " ") {
			numberofwords++;
			i++; 
			// extra i++ makes it skip double spaces, or space/return
		}
		if (content.substring(i,i+1) == "\n") {
			numberofwords++;
			i++;
			// extra i++ makes it skip double spaces, or space/return
		}
		i++;
	}
	if (content.length == 0) { numberofwords = 0; }
	return numberofwords;
}

function checkLength(content, maxLength, description){
	if(countWords(content) > maxLength){
		alert("You can only enter " + maxLength + " words into the " + description + " textbox. Please correct.");
	}
}

function updateWordCount(spanID, inputValue) {
	document.getElementById(spanID).innerHTML=countWords(inputValue);
}