// Sisältää toimintoja helpottavia javascriptejä...
menu = function() {
	if (document.getElementById && document.getElementById("navi")) {
		var items = document.getElementById("navi").getElementsByTagName("LI");
		for (var i=0; i<items.length; i++) {
			items[i].onmouseover = function() { this.className += " hover";	}
			items[i].onmouseout = function() { this.className = this.className.replace(new RegExp(" hover\\b"), ""); }
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", menu);

function openscript( url, width, height ) {
	window.open(url,'popup','width='+ width +',height='+ height +',scrollbars=yes,menubar=no,resize=yes,toolbars=no');
}
function Calendar( year, month ) {
	var now = new Date();
	if (!year) {
		// get current year
		year = now.getFullYear();
	}
	if (!month) {
		// get current month
		month = now.getMonth() + 1;
	}
	var url = "/cgi-bin/ajax.pl";
	url = url + "?action=calendar";					// add action to URL
	url = url + "&y=" + year;					// add year to URL
	url = url + "&m=" + month;					// add month to URL
	url = url + "&rnd=" + Math.ceil(Math.random() * 99999999 + 1);	// random number to URL, for preventing script for caching...
	http.open("get", url, true);
	http.onreadystatechange = function() {
		if (http.readyState == 4 && http.status == 200) {
			// Insert response into a innerHTML
			document.getElementById("calendar").innerHTML = http.responseText;

			// Clear CalendarEvents HTML...
			document.getElementById("calendarevents").innerHTML = "";
		}
	}
	http.send(null);
}
function CalendarEvents( date ) {
	var url = "/cgi-bin/ajax.pl";
	url = url + "?action=calendarevent";				// add action to URL
	url = url + "&date=" + date;					// add date to URL
	url = url + "&rnd=" + Math.ceil(Math.random() * 99999999 + 1);	// random number to URL, for preventing script for caching...
	http.open("get", url, true);
	http.onreadystatechange = function() {
		if (http.readyState == 4 && http.status == 200) {
			// Insert response into a innerHTML
			document.getElementById("calendarevents").innerHTML = http.responseText;
		}
	}
	http.send(null);
}
function closeCalendarEvents() {
	document.getElementById("calendarevents").innerHTML = "";
}
function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	@else
	xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object

function textCounter( field, countfield, maxlimit ) {
	countfield.value = maxlimit - field.value.length;
}
function insertAtCursor( myField, myValue ) {
	myField.focus();
	
	// IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		if (sel.text.length > 0) {
			// Tekstiä valittu, luo aloitus- ja lopetustagit tekstin ympärille...
			sel.text = '['+ myValue +']'+ sel.text +'[/'+ myValue +']';
		} else {
			// Ei tekstiä valittuna, lisää aloitus- ja lopetustagit...
			sel.text = '['+ myValue +'][/'+ myValue +']';
		}
		myField.focus();
	}
	// MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var cursorPos = endPos;
		var scrollTop = myField.scrollTop;
		if (startPos != endPos) {
			myField.value = myField.value.substring(0, startPos)
                                      + '['+ myValue +']'
                                      + myField.value.substring(startPos, endPos)
                                      + '[/'+ myValue +']'
                                      + myField.value.substring(endPos, myField.value.length);
			cursorPos += 6;
		} else {
			myField.value = myField.value.substring(0, startPos)
                                      + '['+ myValue +'][/'+ myValue +']'
                                      + myField.value.substring(endPos, myField.value.length);
			cursorPos = startPos + 3;
		}
		myField.focus();
		myField.selectionStart = cursorPos;
		myField.selectionEnd = cursorPos;
		myField.scrollTop = scrollTop;
	} else {
		myField.value += '['+ myValue +'][/'+ myValue +']';
		myField.focus();
	}
}
	
function lisaaLihavointi( form ) {
	insertAtCursor(form, 'b');
}
function lisaaAlleviivaus( form ) {
	insertAtCursor(form, 'u');
}
function lisaaKursivointi( form ) {
	insertAtCursor(form, 'i');
}

