/****************************************************************
 * Class-handling helper functions
 ****************************************************************
 */
var aCachedREs = new Array();
function classCacheRE(sClass) {
	if(!aCachedREs[ sClass ])
		aCachedREs[ sClass ] = new RegExp('^\\s*(.*)\\b('+ sClass +')\\b(.*)\\s*$');
	return( aCachedREs[ sClass ] );
}

function classMatch(oObject, sClass) {
	if(oObject && oObject.className)
		return(classCacheRE(sClass).exec(oObject.className));
	return(false);
}

function classAdd(oObject, sClass) {
	if(oObject && !classMatch(oObject, sClass))
		oObject.className += ' '+ sClass;
}

function classDel(oObject, sClass) {
	var aMatches = classMatch(oObject, sClass);
	if(aMatches)
		oObject.className = aMatches[1] + aMatches[ aMatches.length - 1 ];
}

/****************************************************************
 * Lanbel-handling helper functions
 ****************************************************************
 */
function initOverLabels () {
  if (!document.getElementById) return;

  var labels, id, field;

  // Set focus and blur handlers to hide and show
  // labels with 'overlabel' class names.
  labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {

	if(classMatch(labels[i], 'sys_overlabel')) {

	  // Skip labels that do not have a named association
	  // with another field.
	  id = labels[i].htmlFor || labels[i].getAttribute ('for');
	  if (!id || !(field = document.getElementById(id))) {
		continue;
	  }

	  // Change the applied class to hover the label
	  // over the form field.
	  classAdd(labels[i], 'sys_overlabel-apply');

	  // Hide any fields having an initial value.
	  //if (field.value !== '') {
	  //hideLabel(field.getAttribute('id'), true);
	  //}

	  //update - actually blank the field
	  field.value='';

	  // Set handlers to show and hide labels.
	  field.onfocus = function () {
		hideLabel(this.getAttribute('id'), true);
	  };
	  field.onblur = function () {
		if (this.value === '') {
		  hideLabel(this.getAttribute('id'), false);
		}
	  };

	  // Handle clicks to label elements (for Safari).
	  labels[i].onclick = function () {
		var id, field;
		id = this.getAttribute('for');
		if (id && (field = document.getElementById(id))) {
		  field.focus();
		}
	  };

	}
  }
};

function hideLabel (field_id, hide) {
  var field_for;
  var labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
	field_for = labels[i].htmlFor || labels[i].getAttribute('for');
	if (field_for == field_id) {
	  labels[i].style.textIndent= (hide) ? '-10000em' : '0em';
	  return true;
	}
  }
}

/****************************************************************
 * Calendar WeekView helper functions
 ****************************************************************
 */
// Mouse-over/out functions
function mouseOverRow()	{ classAdd(this.oTR, 'sys_hover'); }
function mouseOutRow()	{ classDel(this.oTR, 'sys_hover'); }

// Initialiser
function initSysCalendar() {
	if(!document.getElementsByTagName) return;

	// Get all tables, look for those with class 'sys_calendar'
	var aoTables = document.getElementsByTagName('table');
	for(var iTable = 0; iTable < aoTables.length; iTable++) {
		if(!classMatch(aoTables[ iTable ], 'sys_weekview')) continue;

		// Find all <tbody/>s within the table
		var aoTBodies = aoTables[ iTable ].getElementsByTagName('tbody');
		for(var iTBody = 0; iTBody < aoTBodies.length; iTBody++) {

			// Find all <tr/>s within the <tbody/>
			var aoTRows = aoTBodies[ iTBody ].getElementsByTagName('tr');
			for(var iTRow = 0; iTRow < aoTRows.length; iTRow++) {

				// Find all <td/>s within the <tr/>
				var aoTCells = aoTRows[ iTRow ].getElementsByTagName('td');
				for(var iTCell = 0; iTCell < aoTCells.length; iTCell++) {
					// Reference the parent <tr/> and assign the event-handlers
					aoTCells[ iTCell ].oTR = aoTRows[ iTRow ];
					aoTCells[ iTCell ].onmouseover = mouseOverRow;
					aoTCells[ iTCell ].onmouseout = mouseOutRow;
				}
			}
		}
	}
}

window.onload = function () {
  setTimeout(initOverLabels, 50);
  setTimeout(initSysCalendar, 50);
}
