function xmlhttpPost(strURL, divID, displayTYPE, timeOUT) {
	if (window.allowAjax)
	{
		var xmlHttpReq = false;
		// Chat exception
		var chatQuery = '';
		if (strURL.indexOf('l1=chat') != -1)
		{
			chatQuery = '&' + 'last=' + window.lastChat;
		}
		// Mozilla/Safari
		if (window.XMLHttpRequest) {
			xmlHttpReq = new XMLHttpRequest();
		}
		// IE
		else if (window.ActiveXObject) {
			xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlHttpReq.open('GET', 'index.php?ajax=' + displayTYPE + '&' + strURL + chatQuery, true);
		xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHttpReq.onreadystatechange = function() {
			if (xmlHttpReq.readyState == 4) {
				var respo = xmlHttpReq.responseText;
				if (respo.indexOf('$doreloadthispage$') == -1)
				{
					updatepage(xmlHttpReq.responseText, divID);
				}
				else
				{
					window.location.reload();
				}
			}
		}
		xmlHttpReq.send(null);
	}
	if (0 < timeOUT)
	{
		eval("if (typeof this.to_" + divID + " != 'undefined') { clearTimeout(to_" + divID + "); }");
		eval("this.to_" + divID + " = setTimeout(\"xmlhttpPost('\" + strURL + \"', '\" + divID + \"', '\" + displayTYPE + \"', \" + timeOUT + \")\", timeOUT * 1000);");
	}
}

function topg_xmlhttpPost(strURL, divID, displayTYPE, timeOUT) {
	if (window.allowAjax)
	{
		var xmlHttpReq = false;
		// Mozilla/Safari
		if (window.XMLHttpRequest) {
			xmlHttpReq = new XMLHttpRequest();
		}
		// IE
		else if (window.ActiveXObject) {
			xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlHttpReq.open('GET', 'topgame.b.php?ajax=' + displayTYPE + '&' + strURL, true);
		xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHttpReq.onreadystatechange = function() {
			if (xmlHttpReq.readyState == 4) {
				var respo = xmlHttpReq.responseText;
				if (respo.indexOf('$doreloadthispage$') == -1)
				{
					updatepage(xmlHttpReq.responseText, divID);
				}
				else
				{
					window.location.reload();
				}
			}
		}
		xmlHttpReq.send(null);
	}
	if (0 < timeOUT)
	{
		eval("if (typeof this.to_" + divID + " != 'undefined') { clearTimeout(to_" + divID + "); }");
		eval("this.to_" + divID + " = setTimeout(\"xmlhttpPost('\" + strURL + \"', '\" + divID + \"', '\" + displayTYPE + \"', \" + timeOUT + \")\", timeOUT * 1000);");
	}
}

function olym_xmlhttpPost(strURL, divID, displayTYPE, timeOUT) {
	if (true)
	{
		var xmlHttpReq = false;
		// Mozilla/Safari
		if (window.XMLHttpRequest) {
			xmlHttpReq = new XMLHttpRequest();
		}
		// IE
		else if (window.ActiveXObject) {
			xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlHttpReq.open('GET', 'olympics.php?ajax=' + displayTYPE + '&' + strURL, true);
		xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHttpReq.onreadystatechange = function() {
			if (xmlHttpReq.readyState == 4) {
				var respo = xmlHttpReq.responseText;
				if (respo.indexOf('$doreloadthispage$') == -1)
				{
					updatepage(xmlHttpReq.responseText, divID);
				}
				else
				{
					window.location.reload();
				}
			}
		}
		xmlHttpReq.send(null);
	}
	if (0 < timeOUT)
	{
		eval("if (typeof this.to_" + divID + " != 'undefined') { clearTimeout(to_" + divID + "); }");
		eval("this.to_" + divID + " = setTimeout(\"xmlhttpPost('\" + strURL + \"', '\" + divID + \"', '\" + displayTYPE + \"', \" + timeOUT + \")\", timeOUT * 1000);");
	}
}

function updatepage(str, divID) {
	var objDiv = document.getElementById(divID);
	var cScroll = objDiv.scrollHeight - (objDiv.clientHeight + objDiv.scrollTop);
	str = str.replace(/^[\s\t]+/,'');
	if ('<!--lastChat' == str.substring(0, 12))
	{
		window.lastChat = parseInt(str.substring(13, 23));
		str = str.substring(26);
		if (document.getElementById(divID).innerHTML.indexOf('<script language="JavaScript">') == -1)
		{
			cScroll == 1;
			document.getElementById(divID).innerHTML += str;
		}
		else
		{
			document.getElementById(divID).innerHTML = str;
		}
	}
	else
	{
		document.getElementById(divID).innerHTML = str;
	}
	if (cScroll == 0)
	{
		setTimeout('document.getElementById("' + divID + '").scrollTop = document.getElementById("' + divID + '").scrollHeight;', 1000);
	}
}

function fixChatTabs(divID) {
	window.allowAjax = true;
	window.lastChat = 0;
	document.getElementById(divID).innerHTML = '<script language="JavaScript"></script>';
}

function sendChat() {
    var xmlHttpReq = false;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlHttpReq.open('POST', 'index.php?l1=chat&room=2&lang=est', true);
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttpReq.onreadystatechange = function() {
        if (xmlHttpReq.readyState == 4) {
            document.chatform.line.value = '';
        }
    }
    xmlHttpReq.send('submit=true&line=' + document.chatform.line.value);
	window.allowAjax = true;
	clearTimeout(window.aAtimeout);
	window.aAtimeout = setTimeout('killAjax();', 600000);
}

function sendOlym(ques, divi, ajaz) {
    var xmlHttpReq = false;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlHttpReq.open('POST', 'olympics.php?ajax=' + ajaz + '&q=' + ques, true);
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttpReq.onreadystatechange = function() {
        if (xmlHttpReq.readyState == 4) {
			updatepage(xmlHttpReq.responseText, divi);
        }
    }
    xmlHttpReq.send('asubmit=OK&answer=' + getCheckedValue(document.forms['ol' + ques].answer) + '&name=' + document.forms['ol' + ques].name.value + '&number=' + document.forms['ol' + ques].number.value);
	window.allowAjax = true;
	clearTimeout(window.aAtimeout);
	window.aAtimeout = setTimeout('killAjax();', 600000);
}

function conditionalReload(strURL, reloadURL, timeOUT) {
    var xmlHttpReq = false;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlHttpReq.open('GET', 'index.php?' + strURL, true);
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttpReq.onreadystatechange = function() {
        if (xmlHttpReq.readyState == 4) {
			var responseThurr = xmlHttpReq.responseText;
            if (responseThurr.indexOf('yes') != -1) {
				if (document.getElementById('gpline').value.length == 0)
				{
					window.location = 'index.php?' + reloadURL;
				}
				else
				{
					var yoursmess = '<span class="red">Sinu k&auml;ik!</span><br />';
					if (document.getElementById('gamechatcontent').innerHTML.indexOf(yoursmess) == -1)
					{
						document.getElementById('gamechatcontent').innerHTML = document.getElementById('gamechatcontent').innerHTML + yoursmess;
						document.getElementById('gamechatcontent').scrollTop = document.getElementById('gamechatcontent').scrollHeight;
					}
				}
			}
        }
    }
	if (0 < timeOUT)
	{
		eval("setTimeout(\"conditionalReload('\" + strURL + \"', '\" + reloadURL + \"', \" + timeOUT + \")\", timeOUT * 1000);");
	}
    xmlHttpReq.send(null);
}

function promptLogout() {
	document.getElementById('promptframe').style.visibility = 'visible';
	window.logtimer = setTimeout('runLogout();', 1000 * 30);
}

function runLogout() {
	window.location = 'index.php?option=logout';
}

function startPrompt() {
	if (document.getElementById('promptframe') != null) {
		setTimeout('promptLogout();', 1000 * 60 * 10);
	}
}

function resetPrompt() {
	document.getElementById('promptframe').style.visibility = 'hidden';
	clearTimeout(window.logtimer);
	startPrompt();
}


function asd(){
	return 1;
	var myDiv='';
	if(document.getElementById('chat')){
		myDiv='chat';
	}
	if(document.getElementById('inchat')){
		myDiv='inchat';
	}
	if(document.getElementById('gamechat-content')){
		myDiv='gamechat-content';
	}
	
	if(myDiv!=''){
		var objDiv = document.getElementById(myDiv);
		
		objDiv.scrollTop = objDiv.scrollHeight;
		setTimeout("asd()",1000);
	}
}
function aS(val){
	document.getElementById('chatroom-input-id').value=document.getElementById('chatroom-input-id').value+''+val;
}

function killAjax() {
	var divID = '';
	if (document.getElementById('chat'))
	{
		divID = 'chat';
	}
	if (document.getElementById('inchat'))
	{
		divID = 'inchat';
	}
	if (document.getElementById('gamechat-content'))
	{
		divID = 'gamechat-content';
	}
	if (divID!='')
	{
		document.getElementById(divID).innerHTML += ' <!--ajaxKill--> <br /> Paistab, et oled arvutist eemal. Su interneti&uuml;henduse kokkuhoiuks panime jututoa pausile. <a href="#" onClick="reviveAjax(); return false;">Kui soovid edasi lugeda, kl&otilde;psa siia.</a> <br /> <br /> ';
	}
	window.allowAjax = false;
}

function reviveAjax() {
	var divID = '';
	if (document.getElementById('chat'))
	{
		divID = 'chat';
	}
	if (document.getElementById('inchat'))
	{
		divID = 'inchat';
	}
	if (document.getElementById('gamechat-content'))
	{
		divID = 'gamechat-content';
	}
	if (divID!='')
	{
		var lio = document.getElementById(divID).innerHTML.lastIndexOf('<!--ajaxKill-->');
		if (lio != -1)
		{
			document.getElementById(divID).innerHTML = document.getElementById(divID).innerHTML.substring(0, lio);
		}
	}
	window.allowAjax = true;
	window.aAtimeout = setTimeout('killAjax();', 600000);
}

function getCheckedValue(radioObj) {
	if (!radioObj)
	{
		return '';
	}
	var radioLength = radioObj.length;
	if (radioLength == undefined)
	{
		if(radioObj.checked)
		{
			return radioObj.value;
		}
		else
		{
			return '';
		}
	}
	for (var i = 0; i < radioLength; i++)
	{
		if (radioObj[i].checked)
		{
			return radioObj[i].value;
		}
	}
	return '';
}

window.allowAjax = true;
window.aAtimeout = setTimeout('killAjax();', 600000);

// Mouseover code start

var cX = 0;
var cY = 0;
var rX = 0;
var rY = 0;

function UpdateCursorPosition(e) {
	cX = e.pageX;
	cY = e.pageY;
}

function UpdateCursorPositionDocAll(e) {
	cX = event.clientX;
	cY = event.clientY;
}

if (document.all)
{
	document.onmousemove = UpdateCursorPositionDocAll;
}
else
{
	document.onmousemove = UpdateCursorPosition;
}

function AssignPosition(d) {
	if (self.pageYOffset)
	{
		rX = self.pageXOffset;
		rY = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		rX = document.documentElement.scrollLeft;
		rY = document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		rX = document.body.scrollLeft;
		rY = document.body.scrollTop;
	}
	if (document.all)
	{
		cX += rX; 
		cY += rY;
	}
	d.style.left = (cX+10) + "px";
	d.style.top = (cY+10) + "px";
}

function HideContent(d) {
	if(d.length < 1)
	{
		return;
	}
	document.getElementById(d).style.display = "none";
}

function ShowContent(d) {
	if (d.length < 1)
	{
		return;
	}
	var dd = document.getElementById(d);
	AssignPosition(dd);
	dd.style.display = "block";
}

function ReverseContentDisplay(d) {
	if (d.length < 1)
	{
		return;
	}
	var dd = document.getElementById(d);
	AssignPosition(dd);
	if (dd.style.display == "none")
	{
		dd.style.display = "block";
	}
	else
	{
		dd.style.display = "none";
	}
}

function CreateContentDiv(id) {
	var divid = 'pop' + id;
	if (document.getElementById(divid) === null)
	{
		var newdiv = document.createElement('div');
		newdiv.setAttribute('id', divid);
		newdiv.setAttribute('class', 'userpopup');
		newdiv.style.display = 'none';
		newdiv.style.position = 'absolute';
		newdiv.innerHTML = ' <div style="background: white; border: 1px solid black; width: 100px; padding: 5px;"> <img src="loader.gif" alt=" " align="absmiddle" width="16" height="16" /> laeb... </div> ';
		document.body.appendChild(newdiv);
		ShowContent(divid);
		xmlhttpPost('l1=profile&id=' + id, divid, 'popup', 0);
	}
	else
	{
		ShowContent(divid);
	}
}

// Mouseover code end

// Tab scroll code start

scrollStep = 2;
scrollTotal = 0;
timerLeft = '';
timerRight = '';

function scrollDivLeft(id) {
	clearTimeout(timerRight);
	document.getElementById(id).scrollLeft += scrollStep;
	timerRight = setTimeout("scrollDivLeft('" + id + "')", 10);
	scrollTotal += 10;
	if (scrollTotal > 500)
	{
		clearTimeout(timerRight);
		scrollTotal = 0;
	}
}

function scrollDivRight(id) {
	clearTimeout(timerLeft);
	document.getElementById(id).scrollLeft -= scrollStep;
	timerLeft = setTimeout("scrollDivRight('" + id + "')", 10);
	scrollTotal += 10;
	if (scrollTotal > 500)
	{
		clearTimeout(timerLeft);
		scrollTotal = 0;
	}
}

// Tab scroll code end