function doDetail(category, typeid) {
	var myWidth = 200;
	if ((detail_window) && (!detail_window.closed)) {
		if (navigator.userAgent.indexOf("Safari") == -1) {
			detail_window.close();
		}
	}
	if ((category == "1") && (typeid == "1")) {
		myWidth = 700;
	} else if ((category == "1") && (typeid == "2")) {
		myWidth = 625;
	} else if ((category == "2") && (typeid == "1")) {
		myWidth = 475;
	} else if ((category == "2") && (typeid == "2")) {
		myWidth = 625;
	} else if ((category == "A") && (typeid == "1")) {
		myWidth = 750;
	} else if ((category == "A") && (typeid == "2")) {
		myWidth = 750;
	} else if ((category == "B") && (typeid == "1")) {
		myWidth = 475;
	} else if ((category == "B") && (typeid == "2")) {
		myWidth = 925;
	} else if ((category == "C") && (typeid == "1")) {
		myWidth = 700;
	} else if ((category == "C") && (typeid == "2")) {
		myWidth = 975;
	} else if ((category == "D") && (typeid == "1")) {
		myWidth = 750;
	} else if ((category == "D") && (typeid == "2")) {
		myWidth = 525;
	} else if ((category == "E") && (typeid == "1")) {
		myWidth = 575;
	} else {
		myWidth = 700;
	}
	detail_window = openWindow("/calculator/detail" + category + "_" + typeid + ".html", "calc_detail", 400, myWidth);
}

function checkStep1() {
	if (chkCheckBox(document.calculator["locations[]"]) == false) {
		alert("Please select at least one region for comparison");
		return false;
	} else {
		return true;
	}
}

function checkStep2() {
	if (parseInt(document.calculator.total.value, 10) != 100) {
		alert("Category total most be 100.");
		return false;
	} else {
		if ((results_window) && (!results_window.closed)) {
			results_window.close();
		}
		var myHeight;
		if (document.calculator.typeid.value == 1) {
			myHeight = (navigator.userAgent.indexOf("Safari") > -1) ? 360 : 385;
		} else {
			myHeight = (navigator.userAgent.indexOf("Safari") > -1) ? 325 : 350;
		}
		results_window = openWindow("", "results", myHeight, 400);
		window.myAllow = true;
		return true;
	}
}

function saveFactors() {
	if (document.calculator && document.calculator.elements && (document.calculator.elements.length > 0)) {
		var myQuery = new Array();
		var newidx = 0;
		for (var idx=0; idx < document.calculator.elements.length; idx++) {
			if (document.calculator.elements[idx].name.substr(0, 5) == "txt__") {
				myQuery[newidx] = document.calculator.elements[idx].name + "=" + document.calculator.elements[idx].value;
				newidx++;
			}
		}
		if (myQuery.length > 0) {
			var req = createRequest();
			req.open("POST", "/ajax.php", false);
			req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			req.send("todo=SAVE_FACTORS&typeid=" + document.calculator.typeid.value + "&" + myQuery.join("&"));
		}
	}
}

function arrowOver(object, todo) {
	if (myImages[object.name] && myImages[object.name][todo]) {
		object.src = myImages[object.name][todo].src;
	}
}

function doDecrease(field) {
	var myField = document.calculator[field];
	if (myField != null) {
		currentField = myField;
		setValue(-1);
	}
}

function doIncrease(field) {
	var myField = document.calculator[field];
	if (myField != null) {
		currentField = myField;
		setValue(1);
	}
}

function setValue(add_sub) {
	var newValue = parseInt(currentField.value, 10) + add_sub;
	if ((newValue >= 0) && (newValue <= 100)) {
		currentField.value = newValue;
		var arrFieldName = currentField.name.split("_");
		if ((arrFieldName.length > 1) && (arrFieldName[0] == "txt")) {
			if (arrFieldName[arrFieldName.length-1] == "0") {
				calcTotal();
			} else {
				calcSibling(currentField);
			}
		}
		myTimer = setTimeout("setValue(" + add_sub + ");", 250);
	}
}

function doStop() {
	if (myTimer) {
		clearTimeout(myTimer);
		currentField = null;
	}
}

function calcTotal() {
	var newTotal = 0;
	var arrFieldName;
	for (var idx=0; idx < document.calculator.elements.length; idx++) {
		arrFieldName = document.calculator.elements[idx].name.split("_");
		if ((arrFieldName.length > 1) && (arrFieldName[0] == "txt")) {
			if (arrFieldName[arrFieldName.length-1] == "0") {
				newTotal += parseInt(document.calculator.elements[idx].value, 10);
			}
		}
	}
	document.calculator.total.value = newTotal;
	if (newTotal == 100) {
		document.calculator.total.className = "factor";
		document.calculator.next.disabled = false;
	} else {
		document.calculator.total.className = "factor verplicht";
		document.calculator.next.disabled = true;
	}
}

function calcSibling(object) {
	var arrCatField = object.name.split("_");
	var oldIndex = arrCatField[arrCatField.length-1];
	arrCatField[arrCatField.length-1] = 0;
	if (document.calculator[arrCatField.join("_")].parentNode.rowSpan == 2) {
		for (var idx=document.calculator.elements.length-1; idx > -1; idx--) {
			if (document.calculator.elements[idx].name == object.name) break;
		}
		if (idx >= 0) {
			if (oldIndex == "1") {
				idx++;
			} else {
				idx--;
			}
			document.calculator.elements[idx].value = 100 - parseInt(object.value, 10);
		}
	}
}

function doResize() {
	if (opener.window.myAllow) {
		var myButtons = getElement("buttons");
		if (myButtons != null) {
			var neededWidth = 40 + myButtons.clientWidth;
			if (neededWidth > 400) {
				window.moveBy(Math.floor((neededWidth - 400)/2)*-1, 0)
				window.resizeBy(neededWidth - 400, 0);
			}
		}
		opener.window.myAllow = false;
	}
}

detail_window = null;
results_window = null;
currentField = null;
myTimer = null;
myImages = new Array();
myImages["up"] = new Array();
myImages["up"]["over"] = new Image();
myImages["up"]["over"].src = "images/site/arrowUp-over.jpg";
myImages["up"]["out"] = new Image();
myImages["up"]["out"].src = "images/site/arrowUp.jpg";
myImages["down"] = new Array();
myImages["down"]["over"] = new Image();
myImages["down"]["over"].src = "images/site/arrowDown-over.jpg";
myImages["down"]["out"] = new Image();
myImages["down"]["out"].src = "images/site/arrowDown.jpg";

