function unixtimetodate(value) {
	var theDate = new Date(value*1000);
	var dateString = theDate.toLocaleString();
	return dateString;
}

function UpdateTimee() {
	uvalue = uvalue + 1;
//	var upval = (uvalue-upt)/60.0/60.0;
	document.getElementById('timeszar').innerHTML = "ServerTime: " + unixtimetodate(uvalue) + " --- Uptime: " + /*upval.toFixed(2) + " ora"  */ CTimeStamp(uvalue-upt);
	window.setTimeout('UpdateTimee()',1000);
}

function CTimeStamp(secs)
{
	var sztring = '', szint;
	if (secs > 60*60*24) {
		var days = Math.floor(secs/60/60/24);
		secs = secs%(60*60*24*days);
		sztring = days.toString() + 'd ';
	}
	if (secs > 60*60) {
		var hours = Math.floor(secs/60/60);
		secs = secs%(60*60*hours);
		sztring = sztring + hours.toString() + 'h ';
	}
	if (secs > 60) {
		var mins = Math.floor(secs/60);
		secs = secs%(60*mins);
		sztring = sztring + mins.toString() + 'm ';
	}
	if (secs > 0 ) {
		sztring = sztring + secs.toString() + 's';
	}

	return sztring;
}




function BodyOnLoad()
{
	UpdateTimee();
	//	CheckChatBox();
	window.setTimeout('CheckChatBox()',2000);

}

function gup( name )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
	return "";
	else
	return results[1];
}

var xmlhttp=false;
try {
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
	try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (E) {
		xmlhttp = false;
	}
}
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp = false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp = false;
	}
}

function CheckChatBox()
{
	if (chatboxid == 0) { return; }
	xmlhttp.open("GET", "getlastcb.php",true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState == 4) {
			var rt = xmlhttp.responseText;
			var cbarr = rt.split("\n");
			var chatboxid2 = parseInt(cbarr[0]);
			if (chatboxid2 > chatboxid)
			{
				chatboxid = chatboxid2;
				createCookie("chatbox",chatboxid.toString(),5000);
				alert('uj chatbox uzenet ' + cbarr[1] + ' altal (' + unixtimetodate(cbarr[2]) + ')');
			}
		}
	}
	xmlhttp.send(null)
	window.setTimeout('CheckChatBox()',30000);

}

function UpdateContent(divt, url, limit)
{
	var x = document.players.orderby.selectedIndex;
	var mit = document.players.orderby.options[x].value;
	document.getElementById(divt).innerHTML = "<center> ... loading ... </center>";

	var banned = document.players.banned.checked;
	var mitkeres = document.players.kereso.value;
	if (mitkeres.length > 0)
	{
		if (mitkeres.length < 2) {
			document.getElementById(divt).innerHTML = "<center> legalabb 2 karaktert adj meg!</center>";
			return;
		}
	
	}
	if (mitkeres == undefined) mitkeres = "";
	if (limit == undefined) limit = 0;

	xmlhttp.open("GET", url + "?orderby="+ mit + "&limit=" + limit + "&filter=" + mitkeres + "&banned=" + banned, true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState == 4) { SetContent(divt,xmlhttp.responseText); }
	}
	xmlhttp.send(null);

}

function SetContent(divt,mit)
{
	document.getElementById(divt).innerHTML = mit;
	CB_Init();
}


function keypress() {
	if (window.mytimeout) window.clearTimeout(window.mytimeout);
	window.mytimeout = window.setTimeout("UpdateContent('playerslist','listplayers.php')", 500);
	return true;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";

	document.cookie = name+"="+value+expires+"; path=/";
}


function GetURL(url) {
	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState == 4) { alert(xmlhttp.responseText); window.location.reload(); }
	}
	xmlhttp.send(null);
}


var g_kick = 1 << 0;
var g_invite = 1 << 1;
var g_color = 1 << 2;
var g_access = 1 << 3;
var g_bank = 1 << 4;
var g_house = 1 << 5;




var joglista = 0;
function Jogok(formid)
{
        joglista = 0;
        if (document.getElementById(formid).kick.checked) joglista |=  g_kick;
        if (document.getElementById(formid).invite.checked) joglista |= g_invite;
        if (document.getElementById(formid).color.checked) joglista |= g_color;
        if (document.getElementById(formid).access.checked) joglista |= g_access;
        if (document.getElementById(formid).bank.checked) joglista |= g_bank;
        if (document.getElementById(formid).house.checked) joglista |= g_house;

	return joglista;
	
}

function hideshow(id) {
	if (document.getElementById(id).style.display == 'none')
	{
		document.getElementById(id).style.display = 'block';
	} else {
		document.getElementById(id).style.display = 'none';
	}
}

function getselected(honnet)
{
var dropdownIndex = document.getElementById(honnet).selectedIndex;
//alert(dropdownIndex);
var dropdownValue = document.getElementById(honnet)[dropdownIndex].value;
//document.write(dropdownValue);
//	alert(dropdownValue);
	return dropdownValue;

}


function EditIdea(divid,iid)
{
	var divt = document.getElementById(divid).innerHTML;
	if (divt.indexOf('textarea') == -1)
	{
		divt = divt.replace("<br>","\n");
		document.getElementById(divid).innerHTML = '<textarea id="ideaedit' + iid.toString() +'" name="ideaedit" cols="70" rows="3">' + divt +' </textarea><input type="button" value="Change" class="button" onClick="javascript:HideUpdate(\'' + iid +'\',\''+ divid +'\');" >';
	} /* else {
		var divtt = document.getElementById('ideaedit').innerHTML;
		document.getElementById(divid).innerHTML = divtt.replace("\n","<br>");
	}
	*/
}
function HideUpdate(iid, divid)
{
	var divtt = document.getElementById('ideaedit' + iid.toString()).value;
	divtt = divtt.replace("\n","<br>");
//	divtt = divtt.replace("\r","<br>");
	GetURL('ideas.php?&a=e&i=' + iid.toString() + '&newidea=' + divtt.toString());
	document.getElementById(divid).innerHTML = divtt;

}
