var width 	= [],
    height	= [];

// A0
width[0]	= "840";
height[0]	= "1188";

// A1
width[1]	= "594";
height[1]	= "840";

// A2
width[2]	= "420";
height[2]	= "594";

// A3
width[3]	= "297";
height[3]	= "420";

// A4
width[4]	= "210";
height[4]	= "297";

// A5
width[5]	= "149";
height[5]	= "210";

// A6
width[6]	= "105";
height[6]	= "149";

// A7
width[7]	= "74";
height[7]	= "53";

// anderes Format
width[8]	= "";
height[8]	= "";

function getRadioValue(node) {
    var i;

	for (i = 0; i <= node.length; ++i) {
		if (node[i].checked) {
			return node[i].value;
		}
	}

	return null;
}

function onFormatChange(f, d, n) {
	var dir = getRadioValue(d);

	if (1 == parseInt(dir)) {
		f.width.value	= width[n];
		f.height.value	= height[n];
	} else if (2 == parseInt(dir)) {
		f.width.value	= height[n];
		f.height.value	= width[n];
	} else {
		f.width.value	= "";
		f.height.value	= "";
	}
}

function onDirectionChange(f, d) {
	/*
	 * PORTRAIT = 1
	 * LANDSCAPE = 2
	 */

	var w = parseInt(f.width.value, 10),
        h = parseInt(f.height.value, 10),
        t;

	if (!isNaN(w) && !isNaN(h) && w != h) {
		if (1 === parseInt(d.value, 10)) {
			if (h < w) {
				t = h;
				h = w;
				w = t;
			}
		} else if (2 === parseInt(d.value, 10)) {
			if (h > w) {
				t = h;
				h = w;
				w = t;
			}
		}

		f.width.value = w;
		f.height.value = h;
	}
}

function errorMsg(message, element) {
	alert(message);
	element.focus();
	element.select();
}

function isInt(val) {
	var chkZ = 1;

	for (i = 0; i < val.length; ++i) {
		if (val.charAt(i) < "0" || val.charAt(i) > "9") {
		   		chkZ = -1;
		}
	}

	if (chkZ == -1) {
		return false;
	}

	return true;
}

function isFieldCorrect(textfield, name) {
	if (textfield.value == "") {
		errorMsg("Bitte " + name +" eingeben!", textfield);
		return false;
	} else if (!isInt(textfield.value)) {
		errorMsg("Bitte nur Millimeter in ganzen Zahlen größer 0 eingeben!", textfield);
		return false;
	} else if (parseInt(textfield.value) <= 0) {
		errorMsg("Bitte ganzen Zahlen größer als 0 eingeben!", textfield);
		return false;
	}

	return true;
}

function isEntriesCorrect(form) {
	if (!isFieldCorrect(form.height, "Höhe")) {
		return false;
	}

	if (!isFieldCorrect(form.width, "Breite")) {
		return false;
	}

	if (!isFieldCorrect(form.leftborder, "linken Rand")) {
		return false;
	}

	return true;
}
