/**
 * FormInputExample()
 * 
 * Fill formfield with example string if no uservalue is given.
 * 
 * @param formField - input field
 * @param demoString - Demo string to show in field if it's empty
 * @param demoCSSClass - CSSClass of formField if empty, default id 'empty'
 */
function FormInputExample( formField, demoString, demoCSSClass )
{
	this.formField = formField;
	this.demoString = demoString;
	this.userChanged = false;
	if (!demoCSSClass ) this.demoCSSClass = 'empty';
	else this.demoCSSClass = demoCSSClass;
	
	if ( formField.value == '' )
		this.setDemoString();
	else
		this.userChanged = true;
	this.formField.formInputExample = this;
	var formField = this.formField;
	FormInputExample.addEvent(
				formField,
				'onfocus',
				function(e) {
					var target = window.event ? window.event.srcElement : e ? e.target : null;
					target.formInputExample.removeDemoString(); });
	FormInputExample.addEvent(this.formField,
				'onblur',
				function(e) {
					var target = window.event ? window.event.srcElement : e ? e.target : null;
					target.formInputExample.setDemoString(); });

	FormInputExample.addEvent(this.formField, 'onchange', function(e) {
		//alert("SELBER: "+this.value);
		var target = window.event ? window.event.srcElement : e ? e.target : null;
		target.formInputExample.userChanged = true;
		if ( this.value == '' )
		{
			this.formInputExample.userChanged = false;
			this.setDemoString();
		} });

	FormInputExample.addEvent(this.formField, "onkeyup", function(e) {
		var target = window.event ? window.event.srcElement : e ? e.target : null;
		target.formInputExample.userChanged = true; } );
	
	var current = this.formField.parentNode;
	while ( current )
	{
		if ( current.tagName.toLowerCase() == 'form' )
		{
			if (current.formInputExample) break;
			current.formInputExample = this;
			FormInputExample.addEvent(current,'submit', function() { this.formInputExample.onFormSubmit(); });
			break;
		}
		current = current.parentNode;
	}
}

FormInputExample.addEvent = function( obj, type, fnct )
{
	dojo.connect(obj, type, fnct );
}

FormInputExample.prototype.onFormSubmit = function()
{
	var current = this.formField.parentNode;
	while ( current )
	{
		if ( current.tagName.toLowerCase() == 'form' )
		{
			var inputs = current.getElementsByTagName('input');
			for ( var x=0; x<inputs.length; x++ )
			{
				if (inputs[x].formInputExample && !inputs[x].formInputExample.userChanged)
					inputs[x].formInputExample.removeDemoString();
			}
			break;
		}
		current = current.parentNode;
	}
}

FormInputExample.prototype.removeDemoString = function()
{
	var classes = this.formField.className.split(' ');
	var nc = new Array();
	for ( var x=0; x<classes.length; x++ ) {
		if ( classes[x] == this.demoCSSClass ) continue;
		nc.push(classes[x]);
	}
	this.formField.className = nc.join(' ');
	if ( this.formField.value != this.demoString ) return;
	this.formField.value='';
}

FormInputExample.prototype.setDemoString = function()
{
	if (this.userChanged) return;
	this.formField.className += ' '+this.demoCSSClass;
	this.formField.value=this.demoString;
}
