if (!NG.getElementsByClassName) {
	NG.getElementsByClassName = function(node, classname) {
		var results = [];
		if (NG.hasClass(node, classname)) results[results.length] = node;
		for (var i = 0; i < node.childNodes.length; i++) {
			results = results.concat(NG.getElementsByClassName(node.childNodes[i], classname));
		}
		return results;
	}
}

TBR = function() {}
TBR.loggedIn = false;
TBR.Init = function(loggedIn) {
	var form = document.getElementById('login');
	if (form) {
		var e = NG.getElementsByClassName(form, 'email')[0];
		var p = NG.getElementsByClassName(form, 'password')[0];
		var pf = NG.getElementsByClassName(form, 'password-fake')[0];
		if (e) {
			NG.addEventListener(e, 'focus', function() { if (e.value == 'Email') e.value = ''; });
			NG.addEventListener(e, 'blur',  function() { if (e.value == '') e.value = 'Email'; });
		}
		if (pf) {
			NG.addEventListener(pf, 'focus', function() {
				if (pf.value == 'Password') {
					p.style.display = 'inline';
					p.focus();
					pf.style.display = 'none';
				}
			});
		}
		if (p) {
			NG.addEventListener(p, 'blur',  function() {
				if (p.value == '') {
					pf.style.display = 'inline';
					p.style.display = 'none';
				}
			});
		}
	}
	this.loggedIn = loggedIn;
}

TBR.DropdownDependancy = function(node, dependancyChain, data, defaultid) {
	var tmp = this;
	if (isStr(node)) node = document.getElementById(node);
	this.node = node;
	this.dependancyChain = dependancyChain;
	this.defaultid = defaultid;
	this.data = data;
	for (var i = 0; i < this.dependancyChain.length; i++) {
		if (!isArr(this.dependancyChain[i])) {
			this.dependancyChain[i] = [this.dependancyChain[i]];
		}
		elem = this.dependancyChain[i];
		for (var j = 0; j < elem.length; j++) {
			var node = document.getElementById(elem[j]);
			while (node.tagName != 'TR' && node.parentNode != null) node = node.parentNode;
			if (this.defaultOpt == null) this.defaultOpt = '(Select a ' + node.firstChild.innerHTML + ')';
			NG.addEventListener(document.getElementById(elem[j]), 'change', function() { tmp.Change(); });
		}
	}
}
TBR.DropdownDependancy.prototype.Change = function() {
	var o = this.data;
	var value = [];
	finished:
	for (var i = 0; i < this.dependancyChain.length; i++) {
		for (var j = 0; j < this.dependancyChain[i].length; j++) {
			var k = document.getElementById(this.dependancyChain[i][j]).value;
			if (k == '' && j < (this.dependancyChain[i].length - 1)) continue;
			value.push(k);
			if (o[k]) {
				o = o[k].data;
				break;
			} else {
				o = null;
				break finished;
			}
		}
	}
	if (value != this.current) {
		while (this.node.options.length > 0) { this.node.remove(0); }
		if (o != null) {
			for (var k in o) {
				if (isFunc(o[k])) continue;
				n = document.createElement('option');
				n.value = k;
				n.text = o[k].name;
				if (k == this.defaultid) n.selected = true;
				try { this.node.add(n, null); }
				catch (e) { this.node.add(n); }
			}
		} else {
			n = document.createElement('option');
			n.value = '';
			n.text = this.defaultOpt;
			try { this.node.add(n, null); }
			catch (e) { this.node.add(n); }
		}
		this.current = value;
	}
}

TBR.Register = function(node) {
	var tmp = this;
	if (isStr(node)) node = document.getElementById(node);
	var visa = document.getElementById('STUDENTVISATYPEID');
	if (visa.options[visa.selectedIndex].text != 'Distance Education') NG.getElementsByClassName(node, 'institution-state')[0].style.display = 'none';
	NG.addEventListener(NG.getElementsByClassName(node, 'button-submit')[0], 'click', function() { tmp.Validate(); });
	NG.addEventListener(document.getElementById('STUDENTVISATYPEID'), 'change', function() { NG.getElementsByClassName(node,'institution-state')[0].style.display = (this.options[this.selectedIndex].text == 'Distance Education' ? '' : 'none') });
	this.node = node;
}
TBR.Register.prototype.AddWhy = function(name, description) {
	var tmp = this;
	if (description == '') return false;
	var node = document.getElementById(name);
	if (!node) return false;
	while (node.tagName != null && node.tagName != 'TR') {
		node = node.parentNode;
	}
	if (node.tagName != 'TR') return false;
	node.appendChild(document.createElement('TD'));
	node.lastChild.appendChild(document.createElement('span'));
	node.lastChild.lastChild.innerHTML = 'Why';
	NG.addClass(node.lastChild.lastChild, 'why');
	node.lastChild.lastChild.appendChild(document.createElement('span'));
	NG.addClass(node.lastChild.lastChild.lastChild, 'tooltip');
	node.lastChild.lastChild.lastChild.innerHTML = description;
	node.lastChild.lastChild.lastChild.style.display = 'none';
	NG.addEventListener(node.lastChild.lastChild, 'click', function() { tmp.SelectWhy(node.lastChild.lastChild); });
}
TBR.Register.prototype.SelectWhy = function(node) {
	var ts = NG.getElementsByClassName(this.node, 'tooltip');
	var mode = (node.lastChild.style.display == 'block' ? 'none' : 'block');
	for (var i = 0; i < ts.length; i++) ts[i].style.display = 'none';
	node.lastChild.style.display = mode;
}
TBR.Register.prototype.GetInputs = function() {
	var inputs;
	var r = [];
	inputs = this.node.getElementsByTagName('input');
	for (var i = 0; i < inputs.length; i++) r.push(inputs[i]);
	inputs = this.node.getElementsByTagName('select');
	for (var i = 0; i < inputs.length; i++) r.push(inputs[i]);
	return r;
}
TBR.Register.prototype.Validate = function() {
	var errors = []; var alerts = [];
	if (TBR.loggedIn) var skip = ['Password','Password2','Faculty2ID','Institution_StateID'];
	else var skip = ['Faculty2ID','Institution_StateID'];
	
	var inputs = this.GetInputs();
	for (var i = 0; i < inputs.length; i++) {
		if (inputs[i].type == 'button') continue;
		if ((inputs[i].value == '') || (inputs[i].value == 0) || (inputs[i].value == null)) {
			if (skip.search(inputs[i].name) >= 0) continue;
			errors.push(inputs[i].name);
		}
	}
	if (errors.length > 0) {
		this.RenderErrors(errors);
		this.node.firstChild.style.borderCollapse = 'collapse';
		alerts.push('You have left some fields incomplete');
	}
	if (this.node.Password.value != this.node.Password2.value) {
		alerts.push('Passwords do not match');
	}
	if (alerts.length > 0) {
		alert('* ' + alerts.join('\n* '));
		return false;
	}
	
	this.node.submit();
}
TBR.Register.prototype.RenderErrors = function(errors) {
	var inputs = this.GetInputs();
	for (var i = 0; i < inputs.length; i++) {
		if (errors.search(inputs[i].name) >= 0) {
			var n = inputs[i];
			while (n != null && n.tagName != 'TR') { n = n.parentNode; }
			if (n != null) NG.addClass(n, 'invalid');
		}
	}
}