/**********************************************************
Author:
Adam Barry
Klestrup | partners
www.klestrup-partners.dk

Date: January 16 2008

© 2008 Adam Barry, all rights reserved

-----------------------------------------------------------

Name:
textFieldHandler script

-----------------------------------------------------------
Description:
Functions that enables dynamic style-switching when input
fields are filled.

-----------------------------------------------------------
Usage:
Simply place a link to the this script in the head-section
of the XHTML page. The script will then automatically
execute on page load.

<script type="text/javascript" src="textFieldHandler.js"></script>

<fieldset>
	<input class="text" type="text" />
</fieldset>

-----------------------------------------------------------
Dependencies:
This script depends on the windowOnLoad-script to execute

**********************************************************/

function controlInputFields() {
	if (!document.getElementsByTagName) return;

	var inputs = document.getElementsByTagName('input');

	if (!inputs) return;

	for (var i = 0; i < inputs.length; i++) {
		if (inputs[i].className.indexOf('text')==0) {
			var me = inputs[i];
			me.onkeyup = function() {
				toggleInputClass(this);
			}
		}
	}
}addLoadEvent(function(){controlInputFields();});


function toggleInputClass(element) {

	if (element.value.length > 0) {
		if (element.className.indexOf("filled") < 0) {
			element.className += " filled";
		}
	}
	else {
		element.className = element.className.replace(new RegExp("filled\\b"), "");
	}
}