var maxRows = 20,
    notifyColor = '#f88';

window.onload = function(e)
{
	var divs = document.getElementsByTagName('div');
	for (var i = 0; i < divs.length; ++i) {
		if (divs[i].className.match(/\btoggle\b/)) {
			makeToggleable(divs[i]);
		}
		if (divs[i].className.match(/\bfile_toggle\b/)) {
			makeFileToggleable(divs[i]);
		}
	}
	var tables = document.getElementsByTagName('table');
	for (var i = 0; i < tables.length; ++i) {
		if (tables[i].className.match(/\bpaged\b/)) {
			makeTabs(tables[i]);
		}
		if (tables[i].className.match(/\bsortable\b/)) {
			makeSortable(tables[i]);
		}
	}
	var forms = document.getElementsByTagName('form');
	for (var i = 0; i < forms.length; ++i) {
		if (forms[i].className.match(/\bnotify\b/)) {
			changeNotify(forms[i]);
		}
		if (forms[i].className.match(/\bconfirm\b/)) {
			makeConfirm(forms[i]);
		}
	}
	if (document.getElementById('files')) {
		filesForm(document.getElementById('files'));
	}
	if (document.getElementById('accCont')) {
		accCont(document.getElementById('accCont'));
	}
	if (document.getElementById('rejCont')) {
		rejCont(document.getElementById('rejCont'));
	}
	if (document.getElementById('manAcc')) {
		manAcc(document.getElementById('manAcc'));
	}
}

function makeTabs(table)
{
	var ol = document.createElement('ol');
	ol.className = 'pageList';
	for (var page = 1; page <= table.tBodies.length; ++page) {
		var a = document.createElement('a');
		a.href = '';
		a.onclick = function(i)
		{
			return function(e)
			{
				curLi.className = '';
				curLi = ol.childNodes[i];
				curLi.className = 'current';
				curTbody.className = '';
				curTbody = table.tBodies[i];
				curTbody.className = 'current';
				return false;
			};
		}(page - 1);
		ol.appendChild(document.createElement('li'))
			.appendChild(a)
			.appendChild(document.createTextNode(page));
	}
	var curLi = ol.firstChild,
	    curTbody = table.tBodies[0];
	curLi.className = 'current';
	if (page > 2) {
		titleTabs(table.tBodies, ol, 0);
		table.parentNode.insertBefore(ol, table);
	}
}

function makeSortable(table)
{
	table.rowArray = [];
	table.asc = true;
	table.sortedTh = table.rows[0].cells[0];
	for (var i = 1; i < table.rows.length; ++i) {
		table.rowArray.push(table.rows[i]);
	}
	var ths = table.getElementsByTagName('th');
	for (var i = 0; i < ths.length; ++i) {
		if (ths[i].className.match(/\bsortable\b/)) {
			ths[i].title = 'Sort by ' + getText(ths[i]);
			ths[i].onclick = function(i)
			{
				return function(e)
				{
					if (table.asc && table.sortedTh == this) {
						table.rowArray.sort(function(a, b)
						{
							at = getText(a.cells[i]).toLowerCase();
							bt = getText(b.cells[i]).toLowerCase();
							if (at < bt) {
								return 1;
							}
							if (at > bt) {
								return -1;
							}
							return 0;
						});
						table.asc = false;
					} else {
						table.rowArray.sort(function(a, b)
						{
							at = getText(a.cells[i]).toLowerCase();
							bt = getText(b.cells[i]).toLowerCase();
							if (at > bt) {
								return 1;
							}
							if (at < bt) {
								return -1;
							}
							return 0;
						});
						table.asc = true;
					}
					table.sortedTh = this;
					resortRows(table, table.rowArray);
					if (table.previousSibling.nodeName.toLowerCase() == 'ol') {
						titleTabs(table.tBodies, table.previousSibling, i);
					}
				}
			}(i);
		}
	}
}

function resortRows(table, rowArray)
{
	var tbs = [],
	    row = 0;
	for (var i = 0; i < table.tBodies.length; ++i) {
		tbs.push(table.tBodies[i].rows.length);
	}
	for (var i = 0; i < table.tBodies.length; ++i) {
		for (var j = 0; j < tbs[i]; j++) {
			table.tBodies[i].appendChild(rowArray[row++]);
		}
	}
}

function titleTabs(tBodies, ol, col)
{
	for (var i = 0; i < tBodies.length; ++i) {
		ol.childNodes[i].getElementsByTagName('a')[0].title
			= getText(tBodies[i].rows[0].cells[col])
			  + (tBodies[i].rows.length > 1 ? ' ... ' + getText(tBodies[i].rows[tBodies[i].rows.length-1].cells[col]) : '');
	}
}

function changeNotify(form)
{
	form.onreset = function(e)
	{
		for (var i = 0; i < this.elements.length; ++i) {
			this.elements[i].style.backgroundColor = '';
		}
	}
	for (var i = 0; i < form.elements.length; ++i) {
		form.elements[i].onkeypress = function(e)
		{
			this.style.backgroundColor = notifyColor;
		}
	}
}

function makeConfirm(form)
{
	form.onsubmit = function(e)
	{
		return confirm("Are you sure?");
	}
}

function makeToggleable(div)
{
	var a = document.createElement('a');
	a.href = '';
	a.appendChild(document.createTextNode('Hide'));
	a.onclick = function(e)
	{
		if (div.style.display) {
			div.style.display = '';
			this.firstChild.nodeValue = 'Hide';
		} else {
			div.style.display = 'none';
			this.firstChild.nodeValue = 'Show';
		}
		return false;
	}
	div.parentNode.insertBefore(a, div);
}

function makeFileToggleable(div)
{
	var a = document.createElement('a');
	a.href = '';
	a.appendChild(document.createTextNode('Show archive'));
	a.onclick = function(e)
	{
		if (div.style.display) {
			div.style.display = '';
			this.firstChild.nodeValue = 'Hide archive';
		} else {
			div.style.display = 'none';
			this.firstChild.nodeValue = 'Show archive';
		}
		return false;
	}
	div.parentNode.insertBefore(document.createTextNode(' | '), div.previousSibling);
	div.parentNode.insertBefore(a, div.previousSibling);
	div.style.display = 'none';
}

function getText(el)
{
	var text = '';
	for (var i = 0; i < el.childNodes.length; ++i) {
		if (el.childNodes[i].nodeType == 3) {
			text += el.childNodes[i].nodeValue;
		} else if (el.childNodes[i].nodeType == 1) {
			text += getText(el.childNodes[i]);
		}
	}
	return text;
}

function filesForm(form)
{
	form.onsubmit = function(e)
	{
		var file = document.getElementById('file').value.match(/[\/\\]([^\/\\]+)$/)[1],
		    select = document.getElementById('update'),
		    updates = [],
		    matchI = -1;
		for (var i = 0; i < select.options.length; ++i) {
			updates[i] = getText(select.options[i]);
			if (updates[i] == file) {
				matchI = i;
			}
		}
		if (select.value == '' && matchI > -1) {
			return confirm('Are you sure you want to upload ' +
			               file + ' as a new file instead of an update?');
		} else if (select.value != '' && updates[select.selectedIndex] != file) {
			return confirm('Are you sure you want to update ' +
			               updates[select.selectedIndex] +
			               ' with ' + file + '?');
		}
	}
}

function accCont(form)
{
	form.onsubmit = function(e)
	{
		return confirm('This is a legally binding contract. Please keep a copy for your records. Are you sure you accept?');
	}
}

function rejCont(form)
{
	form.onsubmit = function(e)
	{
		return confirm('Are you sure you wish to reject the contract and be unassigned from this entry?');
	}
}

function manAcc(form)
{
	form.onsubmit = function(e)
	{
		return confirm('Are you sure you want to manually accept this contract?');
	}
}
