var textFields = {};

var scriptLoaders = [];



var $ = function(sElement) {

	return document.getElementById(sElement);

};



var elementHasClass = function(oElem, sSearchClass) {

	var sClass = oElem.className;

	var bHasClass = false;

	

	if(sClass != null) {

		var aClasses = sClass.split(new RegExp("/\s+/"));

		

		for(var i=0; i < aClasses.length; i++) {

			sClass = aClasses[i];

			

			if(sClass == sSearchClass) {

				bHasClass = true;

				break;

			}

		}

	}

	

	return bHasClass;

};



var getElementsByTagName = function(oParent, sSearchName, aMatches) {

	var oChild = oParent.firstChild;

	

	if(aMatches == null) {

		aMatches = [];

	}

	

	var stack = [];

	stack[0] = oChild;

	var iPos = 0;

	

	while(stack[0] != null) {

		oChild = stack[iPos];

		

		if(oChild == null) {

			stack[--iPos] = stack[iPos].nextSibling;

		} else{

			if(oChild.nodeType == 1) {

				if(oChild.nodeName.toLowerCase() == sSearchName.toLowerCase()) {

					aMatches = aMatches.concat([oChild]);

				}

			}

			

			if(++iPos < 1024) {

				stack[iPos] = oChild.firstChild;

				//aMatches = window.getElementsByClassValue(oChild, sSearchClass, aMatches);

			}

		}

	}

	

	return aMatches;

};



var scriptLoader = function() {

	// Find all text input elements with default values

	var oJoinListElem = $('joinList');

	

	if(oJoinListElem != null) {

		var elems = getElementsByTagName(oJoinListElem, 'input');

		// Iterate through each element

		for(var i=0; i < elems.length; i++) {

			var textField = elems[i];

			

			// Check for the existence of a "type" attribute

			var typeAttribute = textField.getAttributeNode('type');

			if(typeAttribute != null) {

				// Get the value of the "type" attribute"

				var fieldType = typeAttribute.nodeValue.toLowerCase();

				

				// If the value of the "type" attribute is "text", associate the default

				// value with the field ID

				if(fieldType == 'text') {

					// Get the field ID

					var textFieldID = textField.getAttribute('id');

					

					// Save the field ID and the label text in an array

					window.textFields[textFieldID] = {};

					window.textFields[textFieldID]['label'] = textField.value;

					

					// Attach events to the field so that the text disappears when the

					// field gets "focus" and reappears when the field loses "focus"

					textField.onfocus = function() {

						var textFieldID = this.getAttribute('id');

						var input = window.textFields[textFieldID]['input'];

						

						if(input == null || input == '') {

							this.value = '';

						}

					};

					

					textField.onblur = function() {

						var textFieldID = this.getAttribute('id');

						var labelText = '';

						

						if(this.value != null && this.value != '') {

							window.textFields[textFieldID]['input'] = this.value;

							labelText = this.value;

						} else {

							window.textFields[textFieldID]['input'] = '';

							labelText = window.textFields[textFieldID]['label'];

						}

						

						this.value = labelText;

					};

				}

			}

		}

	}

};



window.scriptLoaders = window.scriptLoaders.concat([scriptLoader]);



window.onload = function() {

	for(var i=0; i < window.scriptLoaders.length; i++) {

		window.scriptLoaders[i]();

	}

};