var Calculator = {

	day: new Array,
	memory: new Array,
	intervalObject: false,
	checkBoxObject: false,
	dayTotalSumm: new Array,
	NDS: 18,
	NDSArray: new Array,
	currentPrice: false,
	totalFree: new Array,

	setCheckbox: function (timestamp, time, price, object) {
		
		this.intervalObject = document.getElementById('interval');
	
		if( this.day[timestamp] == undefined ) 
			this.day[timestamp] = new Array;
		
		if( this.day[timestamp][time] == undefined )
			this.day[timestamp][time] = new Array;
		
		this.checkBoxObject = object;
		
		this.currentPrice = price;
		
		if ( this.isChecked() ) {
			this.setData(timestamp, time);
		}
		else {
			this.unsetData(timestamp, time);
		}
		
	},
	
	setMemory: function(timestamp, time) {
		
		if( this.memory[timestamp] == undefined )
			this.memory[timestamp] = new Array;
		if( this.memory[timestamp][time] == undefined )
			this.memory[timestamp][time] = new Array;
			
		this.memory[timestamp][time]['interval'] = this.intervalObject.value; 
		this.memory[timestamp][time]['price'] = this.currentPrice; 
		
		
	},
	
	getMemory: function(timestamp, time) {
		
		if( this.memory[timestamp] == undefined ) return 0;
		
		if( this.memory[timestamp][time] == undefined ) return 0;
		
		return this.memory[timestamp][time];
		
	},
	
	isChecked: function () {
		return this.checkBoxObject.checked;
	},
	
	recountDay: function( timestamp ) {
	
		var totalTime = 0;
		var summTime  = 0;
		var totalPrice = 0;
		
		for( var time in this.day[timestamp] ) {
			
			totalTime += parseInt(this.day[timestamp][time]);

			if( this.day[timestamp][time] != 0) {
				totalPrice += this.memory[timestamp][time]['interval'] * this.memory[timestamp][time]['price'] / 60;
				summTime++;
			}
			
		}
		
		this.NDSArray[timestamp] = totalPrice + (totalPrice * 18 / 100);
		
		var writeArray = new Array;
		writeArray[0] = summTime;
		writeArray[1] = totalTime;
		
		if( this.dayTotalSumm['quantity'] == undefined) 
			this.dayTotalSumm['quantity'] = new Array;
		
		if( this.dayTotalSumm['time'] == undefined ) 
			this.dayTotalSumm['time'] = new Array;
			
		

			this.dayTotalSumm['quantity'][timestamp] = summTime;
			this.dayTotalSumm['time'][timestamp] = totalTime;
			
			if(totalTime == 0) {
				this.writeVar(timestamp, 0);
			}
			else {
				this.writeData(timestamp, writeArray);
			}
		
		this.writeVar(timestamp + '_nds', this.NDSArray[timestamp]);
	
	},
	
	recountHour: function( time ) {
		
		var totalSumm = 0;
		var summTime = 0;
		
		for(var timestamp in this.day) {
			
			if( this.day[timestamp][time] != undefined ) {
				totalSumm += parseInt(this.day[timestamp][time]);
			
				if( this.day[timestamp][time] != undefined ) 
				if( this.day[timestamp][time] != 0 )
					summTime++;
			}
			
		}
	
		if(summTime == 0) {
			this.writeVar(time, 0);
		}else {
			var writeArray = new Array;
			writeArray[0] = summTime;
			writeArray[1] = totalSumm;
			this.writeData(time, writeArray);
		}


	
	},
	
	recountDays: function() {
	
		var totalSumm = 0;
		var totalTime = 0;

		for( timestamp in this.dayTotalSumm['quantity'] )
			totalSumm += this.dayTotalSumm['quantity'][timestamp];

		for( timestamp in this.dayTotalSumm['time'] )
			totalTime += this.dayTotalSumm['time'][timestamp];
	
			
			if(totalTime == 0) {				
				this.writeVar('total', 0);
				
			}else {
				var writeArray = new Array;
				writeArray[0] = totalSumm;
				writeArray[1] = totalTime;
				this.writeData('total', writeArray);	
			}
	},
	
	recountNDS: function() {
	
		totalNDS = 0;
		
		for(timestamp in this.NDSArray)
			totalNDS += parseInt(this.NDSArray[timestamp]);
		
		this.writeVar('total_nds', totalNDS);
		
	},
	
	recountFreeDay: function(timestamp) {
		
		var totalTime = 0;
		var totalPrice = 0;
		var free = 0;
		
		for( var time in this.day[timestamp] ) {
			
			totalTime += parseInt(this.day[timestamp][time]);

			if( this.day[timestamp][time] != 0)
				totalPrice += this.memory[timestamp][time]['interval'] * this.memory[timestamp][time]['price'] / 60;
		}
		
		if( totalTime >= 300 && totalTime <= 600 )
			free = 10;
		
		if( totalTime >= 601 && totalTime <= 1200 )
			free = 15;		
		
		if( totalTime >= 1201 && totalTime <= 1800 )
			free = 20;
		
		if( totalTime >= 1801 && totalTime <= 2400 )
			free = 25;

		if( totalTime >= 2401 && totalTime <= 3000 )
			free = 30;

		if( totalTime >= 3001 )
			free = 35;
			
		this.totalFree[timestamp] = totalPrice + ((totalPrice - (totalPrice * free / 100)) * this.NDS / 100);
		
		
		this.writeVar(timestamp + '_free', this.totalFree[timestamp]);
		
	},
	
	recountFree: function() {
	
		var totalFree = 0;
		
		for(timestamp in this.totalFree)
			totalFree += parseInt(this.totalFree[timestamp]);
		
		this.writeVar('total_free', totalFree);	
	
	},
	
	writeData: function( divID, writeHTML ) {
	
		document.getElementById(divID).innerHTML = writeHTML[0] + '/' + writeHTML[1];
	
	},
	
	writeVar: function(divID, writeHTML) {
		
		document.getElementById(divID).innerHTML = writeHTML;
		
	},
	
	setHiddenFiled: function(id) {
		document.getElementById(id).value = this.intervalObject.value;
	},
	
	setData: function(timestamp, time) {
		this.day[timestamp][time] = this.intervalObject.value;
		this.setMemory(timestamp, time);
		this.recountDay(timestamp);	
		this.recountHour(time);	
		this.recountDays();
		this.recountNDS();
		this.recountFreeDay(timestamp);	
		this.recountFree();
		this.setHiddenFiled(timestamp + '_' + time);
		return true;
	},
	
	unsetData: function(timestamp, time) {
		this.day[timestamp][time] = 0;
		this.recountDay(timestamp);
		this.recountHour(time);
		this.recountDays();
		this.recountNDS();
		this.recountFreeDay(timestamp);	
		this.recountFree();
	}

};

