window.onload = function() {
	document.trimming.onsubmit = function() {
		el = document.trimming.elements;
		for (i=0; i<el.length; i++) {
			if (el[i].type == 'text' && !(el[i].value > 0)) {
				alert('数値を正しく入力してください。');
				return false;
			}
		}
		focusLength = document.trimming.focusLength.value;
		x = ratio(new Array(document.trimming.a1.value, document.trimming.a2.value));
		y = ratio(new Array(document.trimming.b1.value, document.trimming.b2.value));
		alert(Math.round(Math.max(x,y)/Math.min(x,y)*focusLength)+'mm');
	}
}

function ratio (r) {
	var l = Math.max(r[0],r[1]);
	var s = Math.min(r[0],r[1]);
	if (document.trimming.base[0].checked) {
		s = s/l;
		l = l/l;
	}
	else {
		l = l/s;
		s = s/s;
	}
	return Math.sqrt(Math.pow(l, 2)+Math.pow(s, 2));
}
