function Prefill()
{
	this.init();
	window[ ( this._self = "$_PrefillObject" ) ] = this;
};

Prefill.prototype.init = function()
{
	this._self = this;
	this._fields = this._getFields();
	this._forms = this._getForms();
	this._hijackSubmitHandlers();
};

Prefill.prototype._getFields = function()
{
	var aReturn = Array();
	var aInput = document.getElementsByTagName( "input" );

	for ( var i = 0; i < aInput.length; i++ )
	{
		if ( aInput[ i ].className.match( /\s?prefill/gi ) )
		{
			var oSelect = document.getElementById( "prefill_" + aInput[ i ].name );
			if ( typeof oSelect != "undefined" )
			{
				this.setPrefill( aInput[ i ], false );
				this._attachEventListeners( aInput[ i ] );
				aReturn.push( aInput[ i ] );
			}
		}
	}

	return aReturn;
};

Prefill.prototype._getForms = function()
{
	var aReturn = Array();
	var aFields = this._fields;

	for ( var i = 0; i < aFields.length; i++ )
	{
		var oForm = aFields[ i ].form;
		if ( typeof oForm != "undefined" && typeof oForm.onsubmit == "function" )
			aReturn.push( oForm )
	}

	aReturn = this.unique( aReturn );
	return aReturn;
};

Prefill.prototype.setPrefill = function( oInput, bFocus )
{
	sValue = oInput.value;
	sPrefillValue = oInput.title;

	oInput.className = oInput.className.replace( /\s?prefillvalue/gi, "" );

	if ( sValue == "" || sValue == sPrefillValue )
	{
		oInput.className += " prefillvalue";
		oInput.value = bFocus ? "": sPrefillValue;
	}

	if ( bFocus && oInput.parentNode.nodeName == "FIELDSET" )
	{
		oInput.parentNode.className = oInput.parentNode.className.replace( /\?serror/gi, "" );
	}
};

Prefill.prototype._attachEventListeners = function( oInput )
{
	if ( typeof oInput == "undefined" )
		return false;

	oInput._prefillOnfocus = oInput.onfocus;
	oInput.onfocus = function()
	{
		if ( typeof this._prefillOnfocus == "function" )
			this._prefillOnfocus();
		
		$_PrefillObject.setPrefill( this, true );
	};

	oInput._prefillOnkeyup = oInput.onkeyup;
	oInput.onkeyup = function()
	{
		if ( typeof this._prefillOnkeyup == "function" )
			this._prefillOnkeyup();
		
		$_PrefillObject.setPrefill( this, true );
	};
	
	oInput._prefillOnblur = oInput.onblur;
	oInput.onblur = function()
	{
		if ( typeof this._prefillOnblur == "function" )
			this._prefillOnblur();
			
		$_PrefillObject.setPrefill( this, false );
	};

	return true;
};

Prefill.prototype._hijackSubmitHandlers = function()
{
    for ( var i = 0; i < this._forms.length; i++ )
    {
    	this._forms[ i ].onsubmitPrefill = this._forms[ i ].onsubmit;
    	this._forms[ i ].onsubmit = function()
    	{
    		// Clear prefill values
    		var aInput = this.getElementsByTagName( "input" );
    		for ( var i = 0; i < aInput.length; i++ )
    			if ( aInput[ i ].className.match( /\s?prefill/gi ) )
    				if ( aInput[ i ].value == aInput[ i ].title )
    					aInput[ i ].value = "";

    		// Resume hijacked onsubmit
    		var bResult = this.onsubmitPrefill();

		// Reset prefill values
		for ( var i = 0; i < aInput.length; i++ )
			if ( aInput[ i ].className.match( /\s?prefill/gi ) )
				if ( aInput[ i ].value == "" )
					aInput[ i ].value = aInput[ i ].title;

    		return bResult;
    	}
    }
};

Prefill.prototype.unique = function( aInput )
{
	var aReturn = [];
	var l = aInput.length;

	for( var i=0; i < l; i++ )
	{
		for( var j=i+1; j < l; j++ )
			if ( aInput[ i ] === aInput[ j ] )
				j = ++i;

		aReturn.push( aInput[ i ] );
	}

	return aReturn;
};
