/*******************************************************************************************
 * hideLabel
 * Written by Craig Francis
 * If there are any labels on the page with a 'jsAutoHide' class, then remove them and
 * enter their values into the relevant text fields. But do not password fields, as they
 * are usually paired with a username field.
 *******************************************************************************************/

	var hideLabel = new function () {

		//--------------------------------------------------
		// Do not allow older browsers to run this script

			if (!document.getElementById || !document.getElementsByTagName) {
				return;
			}

		//--------------------------------------------------
		// Initialisation function used for setup

			this.init = function () {

				//--------------------------------------------------
				// Get the labels text from the form - remember,
				// ECMA script (JS) is for functionality, not content.

					var labels = document.getElementsByTagName('label');
					for (var k=(labels.length - 1); k >= 0; k--) {
						if (cssjs('check', labels[k], 'jsAutoHide')) {
							hideLabel.setup(labels[k]);
						}
					}

			}

		//--------------------------------------------------
		// Function to setup the labels

			this.setup = function (labelRef) {

				//--------------------------------------------------
				// Get the text for the label

					if (labelRef.textContent) {
						var labelText = labelRef.textContent; // Good DOM 3 browsers
					} else {
						var labelText = labelRef.innerText; // Old browsers
					}

				//--------------------------------------------------
				// Strip off any non-characters

					labelText = labelText.replace(/:[^a-z]*/g, '');

				//--------------------------------------------------
				// Get the input field this label is for

					var labelFor = document.getElementById(labelRef.htmlFor);
					if (labelFor && labelFor.type == 'text') {

						//--------------------------------------------------
						// Give the textfield the hideLabel property, so
						// in future it knows what the text value should be

							labelFor.hideLabel = labelText;

						//--------------------------------------------------
						// Attach the relevent event handlers

							labelFor.onfocus = function () {
								if (this.value == this.hideLabel) {
									this.value = '';
								}
							};

							labelFor.onblur = function () {
								if (this.value == '') {
									this.value = this.hideLabel;
								}
							};

						//--------------------------------------------------
						// If the field does not already have a value, then
						// give it a defult (the label)

							if (labelFor.value == '') {
								labelFor.value = labelText;
							}

					} else {
						hideLabel.restoreLabels(labelRef);
					}

			}

		//--------------------------------------------------
		// If things go horribly wrong, then this function
		// will restore everything back to normal.

			this.restoreLabels = function (labelRef) {

				//--------------------------------------------------
				// If a labelRef has been provided, then do that one,
				// otherwise do all of them.

					if (labelRef === null) {
						var labels = document.getElementsByTagName('label');
						for (var k=(labels.length - 1); k >= 0; k--) {
							if (cssjs('check', labels[k], 'jsAutoHide')) {
								labels[k].style.position = 'static';
							}
						}
					} else {
						labelRef.style.position = 'static';
					}

			}

		//--------------------------------------------------
		// Set JS specific styles ready for page load.

			addCssRule('label.jsAutoHide { position: absolute; left: -5000px; }');

		//--------------------------------------------------
		// When the page has loaded, run the init function

			addLoadEvent (function() {
				hideLabel.init();
			});

	}
