function ExplainInput( oElement )
{
	this._element = oElement;
	this._explain = oElement.value;
	this._element._parent = this;
	this._element.onfocus = this.__focus;
	this._element.onblur  = this.__blur;
};
ExplainInput.prototype.__focus = function()
{
	this._parent.onfocus();
};
ExplainInput.prototype.__blur = function()
{
	this._parent.onblur();
};
ExplainInput.prototype.onfocus = function()
{
	this._element.className = this._element.className.replace( /\s?explain/, "" );
	if ( this._element.value == this._explain )
		this._element.value = "";
};
ExplainInput.prototype.onblur = function()
{
	if ( this._element.value == "" )
		this._element.value = this._explain;
	if ( this._element.value == this._explain )
		this._element.className += " explain";
};


function Button( oElement )
{
	if ( typeof oElement.src != "undefined" )
	{
		//  Create normal (current) and hover src strings
		oElement._normal = oElement.src;
		oElement._hover  = oElement.src.replace( /\.(gif|jpg|png)/, "_over.$1" );
		//  Preload the hover image
		( new Image() ).src = oElement._hover;
		//  Assign event handlers
		oElement.onmouseover = this.__mouseover;
		oElement.onmouseout  = this.__mouseout;
	}
};
Button.prototype.__mouseover = function()
{
	this.src = this._hover;
};
Button.prototype.__mouseout = function()
{
	this.src = this._normal;
};


function initFormFieldExplain()
{
	var aInput = document.getElementsByTagName( "input" );
	for ( var i = 0; i < aInput.length; ++i )
		if ( aInput[ i ].className.match( /explain/ ) )
			new ExplainInput( aInput[ i ] );
};

function initButtons()
{
	var aImage = document.getElementsByTagName( "img" );
	for ( var i = 0; i < aImage.length; ++i )
		if ( aImage[ i ].className.match( /button/ ) )
			new Button( aImage[ i ] );
};




function RadioTriggerDisplay( sRadioGroup, sDisplay, aShowIfSelected )
{
	this._radio   = new Array();
	this._display = document.getElementById( sDisplay );
	this._trigger = aShowIfSelected;

	var aInput = document.getElementsByTagName( "input" );
	for ( var i = 0; i < aInput.length; ++i )
		if ( aInput[ i ].type == "radio" && aInput[ i ].name == sRadioGroup )
			this._radio[ this._radio.length ] = aInput[ i ];

	if ( this._radio.length > 0 )
		this._assignEventHandlers();
};
RadioTriggerDisplay.prototype._assignEventHandlers = function()
{
	for ( var i = 0; i < this._radio.length; ++i )
	{
		this._radio[ i ]._parent  = this;
		this._radio[ i ].onchange = this.__toggle;
		this._radio[ i ].onclick  = this.__toggle;
	}
};
RadioTriggerDisplay.prototype.__toggle = function()
{
	this._parent.toggle();
};
RadioTriggerDisplay.prototype.inTriggerList = function( sID )
{
	for ( var i = 0; i < this._trigger.length; ++i )
		if ( this._trigger[ i ] == sID )
			return true;
	return false;
};
RadioTriggerDisplay.prototype.toggle = function()
{
	var bDisplay = false;
	for ( var i = 0; i < this._radio.length; ++i )
	{
		if ( this._radio[ i ].checked == true && this.inTriggerList( this._radio[ i ].id ) )
			bDisplay = true;
	}
	this.contentDisplay( bDisplay );
};
RadioTriggerDisplay.prototype.contentDisplay = function( bDisplay )
{
	if ( this._display )
	{
		if ( document.all && document.getElementById( 'paymentblock' ) )
		{
			document.getElementById( 'paymentblock' ).parentNode.style.display = "none";
			setTimeout( "document.getElementById( 'paymentblock' ).parentNode.style.display='block';", 15 );
		}
		this._display.style.display = bDisplay ? "block" : "none";
	}
};



function DateDisplay( oInput )
{
	this.init( oInput );
};
DateDisplay.prototype.init = function( oInput )
{
	this._input = oInput;
	this._input._parent  = this;
	this._input.onchange = this.__onchange;
};
DateDisplay.prototype.__onchange = function()
{
	this._parent.parse();
};
DateDisplay.prototype.parse = function()
{
	var sValue   = this._input.value;
	var aSegment = sValue.match( /([0-9]+)/g );
	if ( aSegment.length == 3 )
		return this._createDate( aSegment[ 2 ], aSegment[ 1 ], aSegment[ 0 ] );
	else if ( this._input.className.match( /yearonly/ ) && aSegment.length == 1 )
		return this._createDate( aSegment[ 0 ], 1, 1 )
};
DateDisplay.prototype._createDate = function( nYear, nMonth, nDay )
{
	var nCorrection = 1;
	var oNow  = new Date();
	if ( ( nYear + "" ).length < 4 )
	{
		nYear = this._sanitizeNumber( nYear, true );
		nYear = ( nYear < 1000 && nYear >= 100 ? 0 : ( 2000 + nYear > oNow.getFullYear() ? 1900 : 2000 ) ) + nYear;
	}
	else if ( ( nYear + "" ).length > 4 )
	{
		return 0;
	}

	nMonth = this._sanitizeNumber( nMonth ) - 1;
	if ( nMonth > 11 || nMonth < 0 )
	{
		++nCorrection;
		nMonth = Math.max( Math.min( 11, nMonth ), 0 );
	}

	nDay = this._sanitizeNumber( nDay );
	oNow.setUTCFullYear( nYear, nMonth, nDay );

	while ( oNow.getMonth() != nMonth )
	{
		++nCorrection;
		oNow.setUTCFullYear( nYear, nMonth, --nDay );
	}

	var sDay   = ( "00" + oNow.getDate() );
	var sMonth = ( "00" +  ( oNow.getMonth() + 1 ) );
	var sYear  = oNow.getFullYear();

	if ( sDay.length > 2 )
		sDay = sDay.substr( sDay.length - 2, 2 );
	if ( sMonth.length > 2 )
		sMonth = sMonth.substr( sMonth.length - 2, 2 );

	this._input.value = sDay + "-" + sMonth + "-" + sYear;
	return nCorrection;
};
DateDisplay.prototype._sanitizeNumber = function( sNumber, bAllowZero )
{
	var sReturn = ( sNumber + "" ).replace( /^0+/, "" );
	return sReturn == "" ? ( bAllowZero ? 0 : 1 ) : parseInt( sReturn );
};


function initDateDisplay()
{
	var aInput = document.getElementsByTagName( "input" );
	for ( var i = 0; i < aInput.length; ++i )
		if ( aInput[ i ].className.match( /datedisplay/ ) )
			new DateDisplay( aInput[ i ] );
}



function toggleImage( oLink )
{
	if ( typeof oLink._example == "undefined" )
	{
		oLink._example = document.createElement( "div" );
		oLink._image = document.createElement( "img" );
		oLink._image.style.width  = "350px";
		oLink._image.style.height = "278px";
		oLink._image.src          = oLink.href;
		oLink._example._link      = oLink;
		oLink._example.onclick    = function()
		{
			this.parentNode.removeChild( this );
			this._link.parentNode.style.display = "";
		};
		oLink._example.appendChild( oLink._image );
	}
	oLink.parentNode.style.display = "none";
	oLink.parentNode.parentNode.insertBefore( oLink._example, oLink.parentNode );
}

function previewYoutube( sURL )
{
	if ( sURL.indexOf( "youtube.com" ) > 0 )
	{
		var oImage = document.getElementById( "youtubepreview" );
		var aMatch = sURL.match( /v=([a-zA-Z0-9-]+)/ );
		oImage.style.backgroundImage = ( aMatch.length > 0 ) ? "url(http://img.youtube.com/vi/" + aMatch[ 1 ] + "/default.jpg)" : "none";
	}
}
