window.onload = initialise;

function initialise()
{
	if (!document.getElementsByTagName || !document.getElementById || !document.createElement || !document.createTextNode)
		return;
	
	var objAreas = document.getElementsByTagName('textarea');
	var objLabels = document.getElementsByTagName('label');
	var objSpan, strLabel;

	// Attach an event handler for each form
	for (var iCounter=0; iCounter<objAreas.length; iCounter++)
	{
		strLabel = getLabel(objAreas[iCounter], objLabels);
		objSpan = document.createElement('span');
		objSpan.className = 'increase'; // for IE
		objSpan.appendChild(document.createTextNode('['));
		objSpan.appendChild(addAnchor(objAreas[iCounter], strLabel, 1));
		objSpan.appendChild(document.createTextNode('] ['));
		objSpan.appendChild(addAnchor(objAreas[iCounter], strLabel, 2));
		objSpan.appendChild(document.createTextNode(']'));
		objAreas[iCounter].parentNode.insertBefore(objSpan, objAreas[iCounter]);
	}
}

function addAnchor(objArea, strLabel, iAction)
{
	var objAnchor = document.createElement('a');

	objAnchor.setAttribute('href', '#' + objArea.getAttribute('id'));
	objAnchor.onclick = function(event){return increaseArea(event, objArea, iAction);};
	objAnchor.onkeypress = function(event){return increaseArea(event, objArea, iAction);};
	if (iAction == 1)
		objAnchor.appendChild(document.createTextNode('Increase Area for ' + strLabel));
	else
		objAnchor.appendChild(document.createTextNode('Decrease Area for ' + strLabel));

	return objAnchor;
}

function getLabel(objField, objLabels)
{
	for (var iCounter=0; iCounter<objLabels.length; iCounter++)
		if (objLabels[iCounter].htmlFor == objField.id)
			return objLabels[iCounter].firstChild.nodeValue;

	return 'unknown';
}

function increaseArea(objEvent, objArea, iAction)
{
	// Allow keyboard navigation over links
	if (objEvent && objEvent.type == 'keypress')
		if (objEvent.keyCode != 13 && objEvent.keyCode != 32)
			return true;

	var iRows = parseInt(objArea.getAttribute('rows'), 10);
	
	if (iAction == 1)
	{
		if (iRows < 50)
			iRows += 5;
	}
	else
	{
		if (iRows > 5)
			iRows -= 5;
	}
	
	objArea.setAttribute('rows', iRows);
	return false;
}

