// JavaScript Document

//====================================
// Global
//====================================
var imageSize = [38,150,250,500,750,14,50,80,8,35,48,90,150,250];
var cameras = [];
var config = new Configuration();

window.addEventListener?window.addEventListener('load',init,false):window.attachEvent('onload',init);

function init() {	
	createCameras();
};

function createCameras() {	
	for(var x=0;x<14;x++) {
		cameras[x] = ['camera' + (x+1)];
		cameras['camera' + (x+1)] = [['num'],['fps'],['hrs'],['motion']];		
	};
};

function Configuration() {

	this.calculate = function() {
		this.getCameraValues();		
		var storage = this.calculateStorage();
		var utilizationPerServer = this.networkUtilizationPerServer();
		var utilizationPerCamera = this.networkUtilizationPerCamera(utilizationPerServer);
		var s = storage + ' TB of storage (estimated).';	
		var ns = utilizationPerServer + '% Gigabit Network Utilization for Server.';
		var nc = utilizationPerCamera + '% Gigabit Network Utilization for Camera.';
		this.updateText('storage-wrapper',s);
		this.updateText('networkPerServer-wrapper',ns);
		this.updateText('networkPerCamera-wrapper',nc);
		this.showObject('results',true);
	};

//====================================
// Util
//====================================
	this.$ = function(o) {
		return document.getElementById(o);
	};
	this.showObject = function(o, d) {
    	this.$(o).style.display = (d) ? 'block':'none';		
    };
	this.updateText = function(o,s) {
		var e = this.$(o);
		e.innerHTML = '';
		e.innerHTML = s;
	};
	this.resetForm = function(f) {
		var form = this.$(f);
		for(var x=0;x<form.elements.length;x++) {
			var o = form.elements[x];
			switch(o.type) {
				case 'text':
					o.value = '';
					break;
				case 'select-one':
					o.selectedIndex = 0;
					break;
			}
		};
	};
	this.camSettings = function(x) {
		var type = parseFloat(x);
		if(!isNaN(type)) {
			var form = this.$('calc');
			for(var i=0;i<form.elements.length;i++) {
				var o = form.elements[i];
				if(o.type == 'text') {
					if((o.id).search('fps') != -1) {
						o.value = (type == 1)?10:10;
					}if((o.id).search('hrs') != -1) {
						o.value = (type == 1)?24:18;
					}
					if((o.id).search('motion') != -1) {
						o.value = (type == 1)?10:20;
					}
				}
			}
		}
	};
	this.mbperday = function(n) {		
		var mb = 0;		
		var cam = cameras['camera'+(n+1)];
		try {
			var fps = cam['num']*cam['fps'];	
		} catch(err) {
			
		};	
		var sps = (imageSize[n]*fps)/1000;
		mb = ((sps*60)*60)*cam['hrs']*(cam['motion']/100);
		mbDay = mb/1000;
		if (isNaN(mbDay)) {
			return 0;
		} else {
			return mbDay;	
		}
	};
	this.totalgbperday = function() {
		var total = 0;
		for(var x=0;x<cameras.length;x++) {
			total += config.mbperday(x);	
		}
		return total;		
	};
	this.networkUtilizationPerCamera = function(s) {
		var totalCams = 0;
		for(var x=0;x<cameras.length;x++) {
			var cam = cameras['camera'+(x+1)];	
			try {
				totalCams += parseFloat(cam['num']);				
			} catch(err) {
				
			};	
		}		
		var perCam = s/totalCams;
		perCam = perCam.toFixed(2);
		return perCam;
	};
	this.networkUtilizationPerServer = function() {
		var total = 0;
		for(var x=0;x<cameras.length;x++) {
			var cam = cameras['camera'+(x+1)];	
			try {
				var fps = cam['num']*cam['fps'];				
			} catch(err) {
				
			};	
			var size = (fps * imageSize[x])*8;
			total += size;
		}
		total = Math.round(total/10000);
		return total;
	};
	this.calculateStorage = function() {
		var gb = this.totalgbperday();		
		var storage = this.$('storage').value;
		var total = (gb*storage)/1000;
		total = total.toFixed(1);
		return total;
	};
	this.validate = function() {
		var valid = true;
		var form = this.$('calc');
		var errorList = [];
		for(var x=0;x<form.elements.length;x++) {
			var o = form.elements[x];
			if (o.type == 'select-one') {
				if (o.selectedIndex == 0) {
					errorList[errorList.length] = 'Please select ' + o.title;
					valid = false;
				};
			};
			if (o.type == 'text') {
				if(o.id == 'storage' && o.value < 1) {
					errorList[errorList.length] = 'Please enter Desired Days of Storage';
					valid = false;
				}
			};
		};
		
		if (valid) {
			this.calculate();			
		} else {
			var e = '';
			for(var x=0;x<errorList.length;x++) {
				e += errorList[x] + '<br />';
			};
			this.updateText('errorList',e);
		};
	};
	this.getCameraValues = function() {
		for(var n=0;n<cameras.length;n++) {
			for(var x=0;x<cameras['camera'+(n+1)].length;x++) {
				cameras['camera'+(n+1)][cameras['camera'+(n+1)][x]] = [(config.$(cameras['camera'+(n+1)][x]+(n+1)).value=='')?0:config.$(cameras['camera'+(n+1)][x]+(n+1)).value];
			};
			
		};		
	};

};



