/* <![CDATA[ */
function ToggleArea( form, button, field ) {
	if( $( form ).style.display == 'block' ) {
		if( $( button ) ) {
			$( button ).value = $( button ).value.replace( /-/, '+' );
		}
		$( form ).style.display = 'none';
	}
	else {
	    $( form ).style.display = 'block';
		if( $( button ) ) {
			$( button ).value = $( button ).value.replace( /\+/, '-' );
		}
		if( typeof( field ) != 'undefined' ) {
			$( field ).focus();
			new Effect.Highlight( field );
		}
	}
}

function ShowMessage() {
	var id = arguments[ 1 ] || 'message';
	$( id ).innerHTML = unescape( arguments[ 0 ] );
	new Effect.Appear( id, { duration: 0.1 } );
	new Effect.Highlight( id );
	new Interval( function() { new Effect.Fade( id ) } );
}

/* class Interval - my prototype based interval implementation */
var Interval = Class.create();
Interval.prototype = {
	/// handler, timeout in seconds || 5, repeat count || 1
	initialize: function( handler, timeout, count ) {
			if( typeof handler == 'function' ) {
			var count = 0;
			var handle = window.setInterval(
				function() {
					try {
						handler();
					}
					catch( e ){
					}
					if( ++count == ( count || 1 ) ) {
						window.clearInterval( handle );
					}
				}, 																							
				( timeout || 5 ) * 1000
			)
		};
	}
}
/* end class Interval */

function $RS( ids ) {
	if( typeof ids == 'array' ) {
		for( var k=0; k<ids.length; k++ ) {
			$( ids[ k ] ).value = '';
		}
	}
	else {
		$( ids ).value = '';
	}
}
/*function $F( id ) {
	return( escape( $F( id ) ) );
}*/
function $L( id ) {
	return( $F( id ).length );
}
function $E( id ) {
	return( escape( $F( id ) ) );
}

function IsRadioChecked() {
	for( var k=0; k<arguments.length; k++ ) {
		if( $( arguments[ k ] ).checked ) {
			return( true );
		}
	}
}
function UncheckRadio() {
	for( var k=0; k<arguments.length; k++ ) {
		$( arguments[ k ] ).checked = false;
	}
}
function GetRadio() {
	for( var k=0; k<arguments.length; k++ ) {
		if( $( arguments[ k ] ).checked ) {
			return( $F( arguments[ k ] ) );
		}
	}
}

function GetRadioValue() {

	var args = typeof( arguments[ 0 ] ) == 'object' ? arguments[ 0 ] : arguments;
	for( var k=0; k<args.length; k++ ) {
		if( $( args[ k ] ).checked ) {
			return( $( args[ k ] ).value );
		}
	}
}

function SetFocus() {
	var element = typeof( arguments[ 0 ] ) == 'object' ? arguments[ 0 ] : $( arguments[ 0 ] );

	if( element ) {
		element.focus();
	}
}

function GetInt() {
	var element = typeof( arguments[ 0 ] ) == 'object' ? arguments[ 0 ] : $( arguments[ 0 ] );
	var value = parseInt( Trim( element.value.replace( /,/, '.' ) ) );
	return( isNaN( value ) ? 0 : value );
}

function Trim() {
	return( arguments[ 0 ].replace( /^\s+|\s+$/g, '' ) );
}
function CastFloat() {
	var value = parseFloat( Trim( arguments[ 0 ].replace( /,/, '.' ) ) );

	return( isNaN( value ) ? 0.0 : value );
}
function GetFloat() {
	
	var element = typeof( arguments[ 0 ] ) == 'object' ? arguments[ 0 ] : $( arguments[ 0 ] );

	var value = parseFloat( Trim( element.value.replace( /,/, '.' ) ) );

	return( isNaN( value ) ? 0.0 : value );
}

function GetSelect() {
	return( element.options[ element.selectedIndex ].value );
}

function Set() {
	var element = $( arguments[ 0 ] );

	switch( typeof( element ) )
	{
		case 'radio':
		{
			element.checked = arguments[ 1 ] || '';
			break;
		}
	}
}

function FormField( id, data_type, ids, message, validation ) {
	this.id = arguments[ 0 ];
	this.data_type = arguments[ 1 ];
	this.ids = arguments[ 2 ];
	this.message = arguments[ 3 ];
	this.validation = arguments[ 4 ] || null;
	this.value = null;

	if( this.ids ) {
		this.object = null;
		this.field_type = $( this.ids[ 0 ] ).type;
	}
	else {
		this.object = $( arguments[ 0 ] );
		this.field_type = this.object.type;
	}
}

FormField.prototype = {

	Check : function () {
		if( this.field_type == 'radio' ) {
			if( this.data_type == 'float' ) {
				this.value = CastFloat( GetRadioValue( this.ids ) );
			}
			else {
				this.value = GetRadioValue( this.ids );
			}
		}
		else {
			if( this.data_type == 'float' ) {
				this.value = CastFloat( $( this.id ).value );
			}
			else {
				this.value = $( this.id ).value;
			}
			
			if( this.validation ) {
				if( !this.validation() ) {
					ShowMessage( this.message );
					SetFocus( this.id );
					return( false );
				}
			}
		}
		return( true );
	}
}

function FormCalculate( method, fields, callback ) {
	var params = [ 'method=' + method  ];
	for( var k = 0; k<fields.length; k++ ) {
		if( !fields[ k ].Check() ) {
			return( false );
		}
		else {
			params.push( fields[ k ].id + '=' + fields[ k ].value );
		}
	}

//	alert( params.join( ',' ) );

	new Ajax.Request(
		'/rechner/ajax.php', {
			method: 'post', 
			parameters: params.join( '&' ),
			onComplete: function( response ) {
//				alert(response.responseText );
				var result = eval( '(' + response.responseText + ')' );
				callback( result );
			}
		}
	);
}
/* ]]> */
