
var invoicesClass = Class.create();

invoicesClass.prototype = {
	
	tarifTable 		: {},
	hoursTable 		: {},
	oDays 			: {},
	oOptions2		: {},
	
	fTotal			: 0,
	
	
	sTariffPrefix 	: 'tariff_',
	sHoursPrefix 	: 'hours_',
	sTotalPrefix 	: 'total_',
	
	initialize : function( tarifTable, hoursTable )
	{
		this.tarifTable = tarifTable;
		this.hoursTable = hoursTable;
	},
	
	setTarif2 : function( day, place, value )
	{
//		$( this.sTariffPrefix + day + '_2_' + place ).value = 0;

		for( i = 0; i < 4; i ++ )
		{
			$( this.sTariffPrefix + day + '_2_' + place + '_' + i ).value = 0;
			$( this.sTariffPrefix + day + '_2_' + place + '_' + i ).style.display = 'none';
		}

		
		$( this.sTariffPrefix + day + '_2_' + place + '_' + value ).style.display = 'block';
		$( this.sHoursPrefix + day + '_' + place ).style.display = 'none';
		$( this.sHoursPrefix + day + '_' + place ).value = 1;
		
//		for( i = 1; i <= 16; i ++ )
//		{
//			if( this.tarifTable[value] && this.tarifTable[value][i] && $( this.sTariffPrefix + day + '_2_' + place + '_' + i ) )
//			{
//				$( this.sTariffPrefix + day + '_2_' + place + '_' + i ).style.display = 'block';
//			}
//			else
//			{
//				$( this.sTariffPrefix + day + '_2_' + place + '_' + i ).style.display = 'none';
//			}
//		}

		this.recalculete();
	},
	
	checkTable : function( day, place, value )
	{
		var t0 = $( this.sTariffPrefix + day + '_1_' + place ).value;
		var t1 = $( this.sTariffPrefix + day + '_2_' + place + '_' + t0 ).value;
		if( t0 == value && value == 0 ) return;
		
		if( this.isHours( t0, t1 ) )
			$( this.sHoursPrefix + day + '_' + place ).style.display = 'block';
		else
		{ 
			$( this.sHoursPrefix + day + '_' + place ).style.display = 'none';
			$( this.sHoursPrefix + day + '_' + place ).value = 1;
		}
		
		if( t1 != 0 && ( !this.tarifTable[t0] || !this.tarifTable[t0][value] ) )
		{
			$( 'errormsg_' + day ).innerHTML = 'You can not select this combinations of tariffs';
			$( 'errormsg_' + day ).style.display = 'block';
			setTimeout( "Effect.Fade( 'errormsg_" + day + "', { duration : 0.5 } );", 2000 );
			$( this.sTariffPrefix + day + '_2_' + place ).value = 0;
		}
		
		this.recalculete();
	},
	
	isHours : function( t0, t1 )
	{
		for( i in this.hoursTable[t0] )
			if( this.hoursTable[t0][i] == t1 ) return true;
		return false;
	},
	
	recalculete : function()
	{
		for( di = 1; di <= 7; di ++ )
		{
			this.oDays[ di ] = 0;
			for( ci = 1; ci <= 3; ci ++ )
			{
				var amount = 0;
				var t0 = $( this.sTariffPrefix + di + '_1_' + ci ).value;
				var t1 = $( this.sTariffPrefix + di + '_2_' + ci + '_' + t0 ).value;
				if( this.tarifTable[t0] && this.tarifTable[t0][t1] )
				{
					if( this.isHours( t0, t1 ) )
						amount = new Number( this.tarifTable[t0][t1] ) * new Number( $( this.sHoursPrefix + di + '_' + ci ).value );
					else
						amount = new Number( this.tarifTable[t0][t1] );
				}
				this.oDays[ di ] += amount;
			}
		}
		this.showTotals();
	},
	
	showTotals : function()
	{
		this.fTotal = 0;
		for( i in this.oDays )
		{
			this.fTotal += new Number( this.oDays[i] );
			$( this.sTotalPrefix + i ).value = this.oDays[i];
		}
		$( 'totaal' ).value = this.fTotal;
	}
	
};
