var childHandle;
var dateHandle;
var timeHandle;
var textHandle;

function openWindow(url, nimi, width, height) {
    var handle = window.open(url, nimi, "width="+width+",height="+height+",resizable=no,toolbar=no,status=no,location=no,titlebar=no,scrollbars=no,left=10,top=10");
    if (!handle.opener) handle.opener = self;
    childHandle = handle;
    return handle;
}

function openWindow2(url, nimi, width, height) {
    var handle = window.open(url, nimi, "width="+width+",height="+height+",resizable=no,toolbar=no,status=no,location=no,titlebar=no,scrollbars=yes,left=10,top=10");
    if (!handle.opener) handle.opener = self;
    childHandle = handle;
    return handle;
}

function openWindow3(url, nimi, width, height) {
    var handle = window.open(url, nimi, "width="+width+",height="+height+",resizable=no,toolbar=no,status=no,location=no,titlebar=no,scrollbars=yes,menubar=yes,left=10,top=10");
    if (!handle.opener) handle.opener = self;
    childHandle = handle;
    return handle;
}

function openWindow4(url, nimi, width, height) {
    var handle = window.open(url, nimi, "width="+width+",height="+height+",resizable=yes,toolbar=no,status=no,location=no,titlebar=no,scrollbars=no,menubar=no,left=10,top=10");
    if (!handle.opener) handle.opener = self;
    childHandle = handle;
    return handle;
}

function openForm(url, nimi) {
	var handle = openWindow(url, nimi, 750, 450);
	handle.focus();
	childHandle = handle;
	return false;
}

function openForm2(url, nimi) {
	var handle = openWindow2(url, nimi, 750, 450);
	handle.focus();
	childHandle = handle;
	return false;
}

function openFormTilit(url, nimi) {
	var handle = openWindow(url, nimi, 320, 270);
	handle.focus();
	childHandle = handle;
	return false;
}

function openRaportti(url, nimi) {
	var handle = openWindow3(url, nimi, 750, 700);
	handle.focus();
	childHandle = handle;
	return false;
}

function openRaporttiWide(url, nimi) {
	var handle = openWindow3(url, nimi, 950, 700);
	handle.focus();
	childHandle = handle;
	return false;
}

function openFormPTS(url, nimi) {
	var handle = openWindow(url, nimi, 350, 450);
	handle.focus();
	childHandle = handle;
	return false;
}

function openFormLomake(url, nimi) {
	var handle = openWindow(url, nimi, 750, 650);
	handle.focus();
	childHandle = handle;
	return false;
}

function openFormEsikatsele(url, nimi) {
	var handle = openWindow2(url, nimi, 950, 650);
	handle.focus();
	childHandle = handle;
	return false;
}

function openFormKorkea(url, nimi) {
	var handle = openWindow(url, nimi, 750, 700);
	handle.focus();
	childHandle = handle;
	return false;
}

function openFormKorkeaWide(url, nimi) {
	var handle = openWindow(url, nimi, 840, 700);
	handle.focus();
	childHandle = handle;
	return false;
}

function openFormKorkeaWide2(url, nimi) {
	var handle = openWindow4(url, nimi, 840, 700);
	handle.focus();
	childHandle = handle;
	return false;
}

function openWideForm(url, nimi) {
	var handle = openWindow(url, nimi, 840, 450);
	handle.focus();
	childHandle = handle;
	return false;
}

function openWiderForm(url, nimi) {
	var handle = openWindow(url, nimi, 972, 450);
	handle.focus();
	childHandle = handle;
	return false;
}

function openTextEditor(id) {
	var handle = openWindow("texteditor_frameset.html", "texteditor", 750, 450);
	handle.focus();
	textHandle = id;
	return false;
}

function openSmallForm(url, nimi) {
	var handle = openWindow(url, nimi, 400, 300);
	handle.focus();
	childHandle = handle;
	return false;
}

function openSmallForm2(url, nimi) {
	var handle = openWindow2(url, nimi, 400, 300);
	handle.focus();
	childHandle = handle;
	return false;
}

function openAverigeForm(url, nimi) {
	var handle = openWindow(url, nimi, 500, 375);
	handle.focus();
	childHandle = handle;
	return false;
}

function openNarrowForm(url, nimi) {
	var handle = openWindow(url, nimi, 300, 450);
	handle.focus();
	childHandle = handle;
	return false;
}

function openNarrowForm2(url, nimi) {
	var handle = openWindow(url, nimi, 600, 450);
	handle.focus();
	childHandle = handle;
	return false;
}

function openDatePickerOLD(pvmFieldId, timeFieldId, d, disbefday, disaftday) {
	dateHandle = pvmFieldId;
	timeHandle = timeFieldId;
	var handle = openWindow("paivavalikko.aspx?paiva=" + d + "&amp;disbefday=" + disbefday + "&amp;disaftday=" + disaftday, "paivavalikko", 230, 220);
	handle.focus();
	childHandle = handle;
	return false;
}

function setDateField(newValue) {
	var dateForm = document.getElementById(dateHandle);
	var timeForm = document.getElementById(timeHandle);
	if (!dateForm) return;
	dateForm.value = newValue;
	if (!timeForm) return;
	timeForm.value = "";
}

function openHelp(url) {
    var handle = window.open(url, "popup", "width=500,height=450,resizable=yes,toolbar=no,scrollbars=yes,status=no,location=no,titlebar=no");
    if (!handle.opener) handle.opener = self;
    handle.focus();
	return false;
}

function closeChildWindows() {
	if (childHandle != null) childHandle.close();
}

function reloadParentAndClose(){
	var o = opener;
	try {
		opener.location.href = opener.location.href;
	}
	catch (ex) {}
	self.close();
}

// Animated screens
function setUp(id) {
	var obj = document.getElementById(id);
	obj.style.width = obj.getAttribute("minwidth");
	obj.style.height = obj.getAttribute("minheight");
	animateOpen(id);
}

function animateOpen(id) {
	var obj = document.getElementById(id);
	if (obj.getAttribute("cancel") == "1") return;
	var currentWidth = parseInt(obj.style.width);
	var currentHeight = parseInt(obj.style.height);
	var maxWidth = parseInt(obj.getAttribute("maxwidth"));
	var maxHeight = parseInt(obj.getAttribute("maxheight"));
	if (currentWidth < maxWidth || currentHeight < maxHeight) {
		var widthDiff = (maxWidth-currentWidth)/6;
		var heightDiff = (maxHeight-currentHeight)/6;
		if (widthDiff > 1 || heightDiff > 1) {
			if (widthDiff > 1) obj.style.width = currentWidth + widthDiff;
			if (heightDiff > 1) obj.style.height = currentHeight + heightDiff;
			setTimeout("animateOpen('" + id + "')", 20);}
		else {
			obj.style.width = maxWidth;
			obj.style.height = maxHeight;
			setTimeout("quickClose('" + id + "')", 5000);}
	}
	else {
		setTimeout("quickClose('" + id + "')", 5000);
	}
}

function quickClose(id) {
	var obj = document.getElementById(id); // Viitataan boxiin
	if (obj.getAttribute("cancel") == "1") return;
	obj.style.width = obj.getAttribute("minwidth");
	obj.style.height = obj.getAttribute("minheight");
}

function quickOpen(id) {
	var obj = document.getElementById(id); // Viitataan boxiin
	obj.style.width = obj.getAttribute("maxwidth");
	obj.style.height = obj.getAttribute("maxheight");
	obj.cancel = "1";
}

function quickToggle(id) {
	var obj = document.getElementById(id); // Viitataan boxiin
	if (parseInt(obj.style.width) == parseInt(obj.getAttribute("maxwidth")) && parseInt(obj.style.height) == parseInt(obj.getAttribute("maxheight"))) {
		obj.style.width = obj.getAttribute("minwidth");
		obj.style.height = obj.getAttribute("minheight");
	}
	else {
		obj.style.width = obj.getAttribute("maxwidth");
		obj.style.height = obj.getAttribute("maxheight");
	}
	obj.cancel = "1";
}