function get_object() {
	if (window.XMLHttpRequest) return new XMLHttpRequest();
	if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
	return null;
}
function change_content(div_id, page_url) {
	var xmlhttp = get_object();
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			document.getElementById(div_id).innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.open('GET', page_url, true);
	xmlhttp.send(null);
}
function post_comment(item_id, ajax_id, div_id, update_url) {
	var xmlhttp = get_object();
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.responseText == 'OK') {
				document.getElementById('action_errors').style.display = 'none';
				close_overlay();
				change_content(div_id, update_url);
			}
			else {
				document.getElementById('action_errors').innerHTML = xmlhttp.responseText;
				document.getElementById('action_errors').style.display = 'block';
			}
		}
	}
	var params = 'ajax=' + ajax_id + '&item_id=' + item_id + 
		'&topic_id=' + (document.getElementById('comment_topic_id') != undefined ? encodeURIComponent(document.getElementById('comment_topic_id').value) : '') + 
		'&quoted_from=' + (document.getElementById('comment_quoted_from') != undefined ? encodeURIComponent(document.getElementById('comment_quoted_from').value) : '') + 
		'&name=' + (document.getElementById('comment_name') != undefined ? encodeURIComponent(document.getElementById('comment_name').value) : '') + 
		'&email=' + (document.getElementById('comment_email') != undefined ? encodeURIComponent(document.getElementById('comment_email').value) : '') + 
		'&article=' + encodeURIComponent(document.getElementById('comment_article').value);
	xmlhttp.open('POST', location.href, true);
	xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	xmlhttp.setRequestHeader('Content-length', params.length);
	xmlhttp.setRequestHeader('Connection', 'close');
	xmlhttp.send(params);
}
function post_order(item_id, ajax_id, div_id) {
	var xmlhttp = get_object();
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			var resp = xmlhttp.responseText.toString();
			if (resp.substr(0, 5) != 'ERROR') {
				document.getElementById('basket_success_' + item_id).style.display = '';
				document.getElementById(div_id).innerHTML = resp;
				setTimeout(function(){
					hide_div('basket_success_' + item_id);
				}, 3000);
			}
			else {
				document.getElementById('basket_errors_' + item_id).innerHTML = resp.substr(6);
				document.getElementById('basket_errors_' + item_id).style.display = '';
			}
		}
	}
	var params = 'ajax=' + ajax_id + '&item_id=' + item_id +
		(document.getElementById('order_fields_2_' + item_id) != undefined ? '&fields[2]=' + encodeURIComponent(document.getElementById('order_fields_2_' + item_id).options[document.getElementById('order_fields_2_' + item_id).selectedIndex].value) : '') + 
		(document.getElementById('order_fields_3_' + item_id) != undefined ? '&fields[3]=' + encodeURIComponent(document.getElementById('order_fields_3_' + item_id).options[document.getElementById('order_fields_3_' + item_id).selectedIndex].value) : '') + 
		'&amount=' + encodeURIComponent(document.getElementById('order_amount_' + item_id).value);
	xmlhttp.open('POST', location.href, true);
	xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	xmlhttp.setRequestHeader('Content-length', params.length);
	xmlhttp.setRequestHeader('Connection', 'close');
	xmlhttp.send(params);
}
function post_category_change(item_id, ajax_id, cat_id, div_id) {
	var xmlhttp = get_object();
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.responseText == 'OK') {
				document.getElementById(div_id).style.display = '';
				setTimeout(function(){
					hide_div(div_id);
				}, 3000);
			}
		}
	}
	var params = 'ajax=' + ajax_id + '&item_id=' + item_id + '&cat_id=' + cat_id;
	xmlhttp.open('POST', location.href, true);
	xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	xmlhttp.setRequestHeader('Content-length', params.length);
	xmlhttp.setRequestHeader('Connection', 'close');
	xmlhttp.send(params);
}
function post_comment_report(div_id) {
	var xmlhttp = get_object();
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.responseText == 'OK') {
				document.getElementById('edit_action_errors').style.display = 'none';
				close_overlay();
				document.getElementById(div_id).style.display = '';
				setTimeout(function(){
					hide_div(div_id);
				}, 3000);
			}
			else {
				document.getElementById('edit_action_errors').innerHTML = xmlhttp.responseText;
				document.getElementById('edit_action_errors').style.display = 'block';
			}
		}
	}
	var params = 'ajax=11&comment_id=' + document.getElementById('report_comment_id').value + '&article=' + encodeURIComponent(document.getElementById('report_explanation').value);
	xmlhttp.open('POST', location.href, true);
	xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	xmlhttp.setRequestHeader('Content-length', params.length);
	xmlhttp.setRequestHeader('Connection', 'close');
	xmlhttp.send(params);
}
function delete_reports(comment_id, page_url) {
	var xmlhttp = get_object();
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			document.getElementById('comment_report_list_' + comment_id).style.display = 'none';
			document.getElementById('reports_span_id_' + comment_id).style.display = 'none';
			document.getElementById('reports_span_deleted_' + comment_id).style.display = '';
			setTimeout(function(){
				hide_div('reports_span_deleted_' + comment_id);
			}, 3000);
		}
	}
	xmlhttp.open('GET', page_url, true);
	xmlhttp.send(null);
}
function post_topic_edit(div_id, update_url) {
	var xmlhttp = get_object();
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.responseText == 'OK') {
				document.getElementById('edit_action_errors').style.display = 'none';
				close_overlay();
				change_content(div_id, update_url);
			}
			else {
				document.getElementById('edit_action_errors').innerHTML = xmlhttp.responseText;
				document.getElementById('edit_action_errors').style.display = 'block';
			}
		}
	}
	var params = 'ajax=9&item_id=' + document.getElementById('edit_topic_id').value + '&comment_id=' + document.getElementById('edit_comment_id').value + 
		'&name=' + (document.getElementById('edit_topic_name') != undefined ? encodeURIComponent(document.getElementById('edit_topic_name').value) : '') + 
		'&article=' + encodeURIComponent(document.getElementById('edit_topic_article').value);
	xmlhttp.open('POST', location.href, true);
	xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	xmlhttp.setRequestHeader('Content-length', params.length);
	xmlhttp.setRequestHeader('Connection', 'close');
	xmlhttp.send(params);
}
function hide_div(div_id) {
	document.getElementById(div_id).style.display = 'none';
}
function change_loc_string(new_id) {
	var reg = new RegExp('#.*$', 'gi');
	if (reg.test(String(document.location))) location.replace(String(document.location).replace(/^([^#]*)#.*$/, '$1#' + new_id));
	else location.replace(document.location + '#' + new_id);
}
function get_loc_string(page_url) {
	var reg = new RegExp(/^[^#]*#([0-9]+)/);
	var match = reg.exec(String(document.location));
	change_content('gallery_image_div', page_url + ',9,' + match[1]);
}
function get_page_size() {	
	var xScroll, yScroll, windowWidth, windowHeight;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.scrollWidth;
		yScroll = (this.isFrame ? parent.innerHeight : self.innerHeight) + (this.isFrame ? parent.scrollMaxY : self.scrollMaxY);
	}
	else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else {
		xScroll = document.getElementsByTagName("html").item(0).offsetWidth;
		yScroll = document.getElementsByTagName("html").item(0).offsetHeight;
		xScroll = (xScroll < document.body.offsetWidth) ? document.body.offsetWidth : xScroll;
		yScroll = (yScroll < document.body.offsetHeight) ? document.body.offsetHeight : yScroll;
	}
	if (self.innerHeight) {
		windowWidth = (this.isFrame) ? parent.innerWidth : self.innerWidth;
		windowHeight = (this.isFrame) ? parent.innerHeight : self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body) {
		windowWidth = document.getElementsByTagName("html").item(0).clientWidth;
		windowHeight = document.getElementsByTagName("html").item(0).clientHeight;
		windowWidth = (windowWidth == 0) ? document.body.clientWidth : windowWidth;
		windowHeight = (windowHeight == 0) ? document.body.clientHeight : windowHeight;
	}
	var pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;
	var pageWidth = (xScroll < windowWidth) ? windowWidth : xScroll;
	return new Array(pageWidth, pageHeight, windowWidth, windowHeight);
}
function display_overlay(content_id) {
	var isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
	var page_size = get_page_size();
	document.getElementById('overlay-bg').style.display = '';
	if (isIE6) document.getElementById('overlay-bg').style.height = page_size[1] + "px";
	document.getElementById('content').style.display = (content_id == 1 ? '' : 'none');
	document.getElementById('content_2').style.display = (content_id == 2 ? '' : 'none');
	if (document.getElementById('content_3')) document.getElementById('content_3').style.display = (content_id == 3 ? '' : 'none');
	var overlay = document.getElementById('overlay-holder');
	overlay.style.display = '';
	var overlay_width = overlay.offsetWidth;
	var left_margin = Math.round((page_size[2] - overlay_width) / 2);
	overlay.style.left = left_margin + "px";
	var iframes = document.getElementsByTagName('iframe');
	for (i = 0; i < iframes.length; i++) {
		iframes[i].parentNode.style.visibility = 'hidden';
	}
}
function close_overlay() {
	document.getElementById('overlay-bg').style.display = 'none';
	document.getElementById('overlay-holder').style.display = 'none';
	var iframes = document.getElementsByTagName('iframe');
	for (i = 0; i < iframes.length; i++) {
		iframes[i].parentNode.style.visibility = 'visible';
	}
}
var rotation_group_count = 0;
function do_rotation(ul_prefix, show_nr, auto) {
	if (show_nr > rotation_group_count) show_nr = 1;
	if (show_nr == 0) show_nr = rotation_group_count;
	document.getElementById(ul_prefix + 'current').innerHTML = show_nr;
	for (var i = 1; i <= rotation_group_count; i++) {
		if (i == show_nr) document.getElementById(ul_prefix + i).style.display = '';
		else document.getElementById(ul_prefix + i).style.display = 'none';
	}
	if (auto == 1) {
		var next_group = (show_nr == rotation_group_count ? 1 : show_nr + 1);
		setTimeout(function(){
			do_rotation(ul_prefix, next_group, 1);
		}, 7000);
	}
}
function load_avatar() {
	document.getElementById('forum_avatar_loading').style.display = '';
	return true;
}
function complete_avatar(response) {
	document.getElementById('forum_avatar_loading').style.display = 'none';
	if (response.substr(0, 5) != 'ERROR') {
		close_overlay();
		document.getElementById('avatar_ul').innerHTML = response;
		document.getElementById('forum_avatar_error').style.display = 'none';
		document.getElementById('forum_avatar_file').value = '';
	}
	else {
		document.getElementById('forum_avatar_error').innerHTML = response.substr(6);
		document.getElementById('forum_avatar_error').style.display = '';
	}
}
function autofill(obj, fields) {
	for (i = 0; i < fields.length; i++) {
		document.getElementById('fields_delivery_' + fields[i]).value = (obj.checked == true ? document.getElementById('fields_sender_' + fields[i]).value : '');
	}
}
function play_music(obj) {
	if (soundManager.supported() != false) {
		if (document.getElementById('music_status').innerHTML == '0') {
			var s = soundManager.createSound({id: 'tunnus', url: '/site/data/kelgukoerad.mp3'});
			s.play({onfinish: function() { s.play(); }});
			obj.style.background = 'url(/site/img/sound-on.png) no-repeat';
			document.getElementById('music_status').innerHTML = '1';
		}
		else {
			soundManager.stop('tunnus');
			obj.style.background = 'url(/site/img/sound-of.png) no-repeat';
			document.getElementById('music_status').innerHTML = '0';
		}
	}
}
function display_fieldset(obj, fields, sets) {
	for (i = 0; i < sets.length; i++) {
		document.getElementById(sets[i]).style.display = (fields[obj.selectedIndex] == i + 1 ? '' : 'none');
	}
}
function display_flex(obj, input_id, vals) {
	if (vals.length == 0) return false;
	var show_field = 0;
	for (i = 0; i < vals.length; i++) {
		if (i>0 && vals[i] == obj.selectedIndex + 1) show_field = 1;
	}
	if (show_field == 1) document.getElementById(input_id).style.display = '';
	else {
		document.getElementById(input_id).value = '';
		document.getElementById(input_id).style.display = 'none';
	}
}
function display_pic(pic_href) {
	display_overlay(1);
	document.getElementById('image_container').src = pic_href;
}
function open_printpage() {
	newWin = window.open('', 'print_window', 'height=470,width=700,top=40,left=40,scrollbars=yes');
	newWin.document.write('<html><head>');
	newWin.document.write(document.getElementsByTagName('head')[0].innerHTML);
	newWin.document.write('</head><body onload="window.print();" class="printpage">');
	newWin.document.write(document.getElementById('print_content').innerHTML);
	newWin.document.write('</body></html>');
	newWin.document.close();
}
function highlight_stars(div_prefix, pic_name, star_count, is_half) {
	for (var i = 1; i <= 5; i++) {
		if (i <= star_count) var pic_type = pic_name;
		else if (is_half == 1 && i == star_count + 1) var pic_type = 'half';
		else var pic_type = 'empty';
		document.getElementById(div_prefix + i).src = '/site/img/rating_' + pic_type + '.gif';
	}
}

