/***************************************************************************************/
/* FUNCTIONS FOR THE ASPECT RATIO CALCULATOR
**/
function calculateDimensions() {
	// Get vars from textfields
	var w = document.getElementById('txtWidth').value.replace(',','.');
	var h = document.getElementById('txtHeight').value.replace(',','.');
	var d = document.getElementById('txtDiameter').value.replace(',','.');
	var ratio = document.getElementById('selRatio').value;
	
	// If width, height and diameter aren't empty but not numeric, display error
	if ((!isEmpty(w) && !isNumeric(w)) || (!isEmpty(h) && !isNumeric(h)) || (!isEmpty(d) && !isNumeric(d))){
		alert('error no numeric values');
	}	
	
	// Is width and height set -> calculate diagonal and aspect ratio
	if ((!isEmpty(w) && isNumeric(w)) && (!isEmpty(h) && isNumeric(h))){
		// Calculate diameter
		d = Math.sqrt((w*w)+(h*h));
		// Calculate ratio
		calculatedRatio = calculateRatio(w,h);
		
		// Fill fields
		fillFields(w, h, d, calculatedRatio);
	}	
	// If ratio is set and width is set -> calculate dimensions
	else if ((!isEmpty(w) && isNumeric(w)) && (!isEmpty(ratio))){
		// Calculate heigth based on width and ratio
		h = w/getRatioDivision(ratio);
		// Calculate diameter
		d = Math.sqrt((w*w)+(h*h));
		// Fill fields
		fillFields(w, h, d, ratio);
	}	
	// If ratio is set and height is set -> calculate dimensions
	else if ((!isEmpty(h) && isNumeric(h)) && (!isEmpty(ratio))){
		// Calculate width based on height and ratio
		w = h*getRatioDivision(ratio);
		// Calculate diameter
		d = Math.sqrt((w*w)+(h*h));
		// Fill fields
		fillFields(w, h, d, ratio);
	}
	// If diagonal and ratio are set -> calculate height and width
	else if ((!isEmpty(d) && isNumeric(d)) && !isEmpty(ratio)) {
		ratioDivision = getRatioAsArray(ratio);
		// For example ratio 16:9 -> x=16; y = 9
		x = ratioDivision[0];
		y = ratioDivision[1];
		w = x*d/(Math.sqrt((x*x)+(y*y)));
		h = y*d/(Math.sqrt((x*x)+(y*y)));
		fillFields(w, h, d, ratio);
	}
	// Error, no calculation can be made
	else {
		// Do nothing
	}
	//document.getElementById('aspectRatioScreenTitle').innerHTML = document.getElementById('selUnits').value + ', ' + document.getElementById('selRatio').options[document.getElementById('selRatio').selectedIndex].text;
}

/*
* Calculate the ratio from the valid ratios array.
*
* @param w		width
* @param h		height
* @param margin	Max difference between ratio from array and calculated ratio 
**/
function calculateRatio(w, h, margin) {
	if (!margin) {
		margin = 0;
	}
	var arrayRatios = new Array(new Array('4:3',4/3),new Array('16:9',16/9),new Array('16:10',16/10),new Array('2,35:1',2.35/1));
	var arrayLength = arrayRatios.length;
	
	var calculatedRatio = w/h;
	
	// Loop through possible ratio's until the correct one is found
	for(var i=0;i<arrayLength;i++) {
		leftBoundary = (arrayRatios[i][1])-margin;
		rightBoundary = (arrayRatios[i][1])+margin;
		if ((calculatedRatio >= leftBoundary) && (calculatedRatio <= rightBoundary)) {
			return arrayRatios[i][0];
		}
	}
	// No correct one found
	return 0;
}

function fillFields(w, h, d, ratio) {
	document.getElementById('txtWidth').value = roundNumber(w,2);
	document.getElementById('txtHeight').value = roundNumber(h,2);
	document.getElementById('txtDiameter').value = roundNumber(d,2);
	document.getElementById('selRatio').value = ratio;
}

function resetFields() {
	document.getElementById('txtWidth').value = '';
	document.getElementById('txtHeight').value = '';
	document.getElementById('txtDiameter').value = '';
	document.getElementById('selRatio').value = 0;
}

/*
* Calculate width/height
*
* input: '16:9'
* output: 1.7777777777777777777777777777778
*
**/
function getRatioDivision(strRatio) {
	var arrRatios = strRatio.split(':');
	return arrRatios[0] / arrRatios[1];
}

/*
* Get ratio as array
*/
function getRatioAsArray(strRatio) {
	return strRatio.split(':');
}

/***************************************************************************************/
/* FUNCTIONS FOR THE LUX CALCULATOR
**/
function calculateLux() {
	// Get vars from textfields
	var ansilumen = document.getElementById('txtAnsilumen').value.replace(',','.');
	var units = document.getElementById('selUnits').value.replace(',','.');
	var w = document.getElementById('txtWidth').value.replace(',','.');
	var h = document.getElementById('txtHeight').value.replace(',','.');
	var reflectionValue = document.getElementById('selFabricType').value.replace(',','.');
	
	// Calculate surface in m2
	// Do we need to convert inches to cm?
	if (units == 'inch') {
		w = w * 2.54;
		h = h * 2.54;
	}
	surface = (w/100)*(h/100);	//surface is now in m2
	
	// Calculate: lux = (ANSI Lumen/surface screen in square meters)*reflectionValue
	lux = roundNumber(((ansilumen/surface)*reflectionValue),2);
	
	// Put in field
	document.getElementById('luxValue').innerHTML = lux;
}

function setWidthAndHeightUnits() {
	document.getElementById('widthUnits').innerHTML = document.getElementById('selUnits').value;
	document.getElementById('heightUnits').innerHTML = document.getElementById('selUnits').value;
}

/***************************************************************************************/
/* COMMON FUNCTIONS
**/
function isNumeric(n) {
	return !isNaN(n);
}

function isEmpty(input) {
	if (input == null || input == undefined || input == '' || input == 0) {
		return true;
	}
	return false;
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

