function showHelp(id){
	if(document.getElementById(id) != null){
		var display = "block";
		if(arguments[1] != null){
			display = arguments[1];
		}
		var el = document.getElementById(id);
		el.style.display = el.style.display == display ? "none" : display;
	}
}

function $(id){
	return document.getElementById(id);
}
function a(id){
	return document.getElementById(id) == null ? false : document.getElementById(id);
}
// ** INTEGER SORT -- JØSSES ** //
function intSort(arr){
	var temp;
	for(i = 0; i < arr.length; i++){
		for(j = 0; j < arr.length; j++){
			if(parseInt(arr[j]) > parseInt(arr[j + 1])){
				temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}	
	}
	return arr;
}

// ** BODY ONLOAD FOCUS FUNCTION ** //
function focusThis(id){
	document.getElementById(id).focus();
}
// ** ADD card to textarea  ** //
function add_card(card){
	insertAtcursor(document.getElementById("threadtext"), "[" + card + "]")
//	document.getElementById('threadtext').value = document.getElementById('threadtext').value + "[" + card + "]";
	document.getElementById('threadtext').focus();
}
// ** SHOW card row ** //
function show_card_row(row){
	document.getElementById('hearts_row').style.display = 'none';
	document.getElementById('spades_row').style.display = 'none';
	document.getElementById('clubs_row').style.display = 'none';
	document.getElementById('diamonds_row').style.display = 'none';
	document.getElementById(row).style.display = 'block';
}
// ** CHANGE BACKGROUND OF INPUT TEXT FIELDS ON SELECT ** //
function changeInputBgOn(element){
	element.style.backgroundPosition = "0px -27px";
}
function changeInputBgOff(element){
	element.style.backgroundPosition = "0px 0px";
}
// ** CHANGE BACKGROUND OF BUTTONS ON SELECT ** //
function changeSubmitBgOn(element){
	element.style.backgroundPosition = "0px -29px";
}
function changeSubmitBgOff(element){
	element.style.backgroundPosition = "0px 0px";
} 
function changeContentSubmitBgOn(element){
	element.style.backgroundPosition = "0px -29px";
}
function changeContentSubmitBgOff(element){
	element.style.backgroundPosition = "0px 0px";
}

function openChat(id,team){
	if(team == 'team'){
		window.open("/buddychat.php?type=team&chat=" + id,"teamChat","height=500,width=502,resizable=no,scrollbars=no");
	}
	else{
		window.open("/buddychat.php?chat=" + id,"userChat","height=500,width=502,resizable=no,scrollbars=no");
	}
}
function chatUnfold(id) {
	elem = document.getElementById(id);
	if(elem.style.display != "block"){
		elem.style.display = "block";
		document.getElementById(id+"li").className = "unfold";
	}
	else{
		elem.style.display = "none";
		document.getElementById(id+"li").className = "fold";
	}
}
function chatUnfoldoffline(id) {
	elem = document.getElementById(id);
	if(elem.style.display != "block"){
		elem.style.display = "block";
		document.getElementById(id+"li").className = "offlineunfold";
	}
	else{
		elem.style.display = "none";
		document.getElementById(id+"li").className = "offlinefold";
	}
}
function buddylistOfflines(){
	var list = document.getElementById("buddylist-offline");
	var header = document.getElementById("buddylist-offline-head");
	list.style.display = list.style.display == "block" ? "none" : "block";
	header.style.backgroundPosition = list.style.display == "none" ? "2px 3px" : "2px -8px";
}
var updatenow = "";
function updatebuddies()
{
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
 				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
 				alert("Your browser does not support AJAX!");
				return false;
			}
 		}
	}
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			text = xmlHttp.responseText;
			var buddies = Array();
			var buddy = Array();
			buddies = text.split("||");
/*			var echo = "";
			for(var i = 0; i < buddies.length - 2; i++){
				echo += buddies[i];
			}*/
			var echo = buddies[0];
			if(buddies[1] != updatenow){
				document.getElementById("chat-list").innerHTML = echo;
			}
			updatenow = buddies[1];
//			document.getElementById("buddy-test").innerHTML = buddies.length;
//			document.getElementById("buddy-test").innerHTML = text;
		}
	}

	xmlHttp.open("GET","/includes/buddies_ajax.php",true);
	xmlHttp.send(null);
}
var updatenowteam = "";
function updateteammembers()
{
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
 				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
 				alert("Your browser does not support AJAX!");
				return false;
			}
 		}
	}
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			text = xmlHttp.responseText;
			var buddies = Array();
			var buddy = Array();
			buddies = text.split("||");
/*			var echo = "";
			for(var i = 0; i < buddies.length - 2; i++){
				echo += buddies[i];
			}*/
			var echo = buddies[0];
			if(buddies[1] != updatenowteam){
				document.getElementById("team-list").innerHTML = echo;
			}
			updatenowteam = buddies[1];
//			document.getElementById("buddy-test").innerHTML = buddies.length;
//			document.getElementById("buddy-test").innerHTML = text;
		}
	}

	xmlHttp.open("GET","/includes/team_member_ajax.php",true);
	xmlHttp.send(null);
}

var heads_up;
function openTable(id){
	if(!heads_up || heads_up.closed){
		heads_up = window.open("/heads_up/headsuppoker.php?game=" + id,"HeadsUp","height=530,width=900,resizable=no,scrollbars=no");
	}
}

function evenHeights(){
	/*var h1,h2,h3,h4;
	h1 = parseInt(document.getElementById("leftcontent").offsetHeight);
	h2 = h3 = h4 = 0;
	if(document.getElementById("content") != null){
		h2 = parseInt(document.getElementById("content").offsetHeight);
	}
	if(document.getElementById("content-wide") != null){
		h3 = parseInt(document.getElementById("content-wide").offsetHeight);
	}
	else{
	h4 = parseInt(document.getElementById("rightcontent").offsetHeight);
	}
	var arr = new Array(h1,h2,h3,h4);
	arr = intSort(arr);
	arr.reverse();
	document.getElementById("leftcontent").style.height = arr[0] + "px";
	if(document.getElementById("content") != null){
		document.getElementById("content").style.height = arr[0] + "px";
	}
	if(document.getElementById("content-wide") != null){
		document.getElementById("content-wide").style.height = arr[0] + "px";
	}
	else{
	document.getElementById("rightcontent").style.height = arr[0] + "px";
	}*/
}
function tablespacing(){
	var tables = document.getElementsByTagName("table");
	for (var i = 0; i < tables.length ;i++ ){
		if(tables[i].className != "nojs-spacing"){
			tables[i].cellSpacing = 0;
			tables[i].cellPadding = 0;
		}
	}
}

// ** INSERT STRING AT CURSOR ** //
function insertAtcursor(myField, myValue) {
	//IE support
	if (document.selection) {
		myField.focus();

		sel = document.selection.createRange();
		sel.text = myValue;
	}

	//Mozilla/Firefox/Netscape 7+ support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
}

function bbctime() {
  var elm_sekund = document.getElementById('tid_sekund').innerHTML;
  document.getElementById('tid_sekund').innerHTML = (elm_sekund==59)? bbctime_minut() : twoDigits(++elm_sekund);
  setTimeout('bbctime()',1000);
}

function bbctime_minut() {
  var elm_minut = document.getElementById('tid_minut').innerHTML;
  document.getElementById('tid_minut').innerHTML = (elm_minut==59)? bbctime_time() : twoDigits(++elm_minut);
  return "0" + 0;
}

function bbctime_time() {
      var elm_time = document.getElementById('tid_time').innerHTML;
      document.getElementById('tid_time').innerHTML = (elm_time==23)? "00" : twoDigits(++elm_time);
      return "0" + 0;
}

function twoDigits(num){
  return num>9 ? num : "0" + num;
}

function tourney_countdown(divid,showdays){
	var el = document.getElementById(divid);
	var dayarr,timearr,sec,min,hour,day;
	if(showdays){
		dayarr = el.innerHTML.split("days ");
		timearr = dayarr[1].split(":");
		day = dayarr[0];
	}
	else{
		timearr = el.innerHTML.split(":");
		day = "0";
	}
	min = timearr[1];
	hour = timearr[0];
	if(timearr[2] == "00"){
		sec = "59";
		if(timearr[1] == "00"){
			min = "59";
			if(timearr[0] == "00" && showdays){ 
				hour = "23";
				day = --dayarr[0];
			}
			else{
				hour = twoDigits(--timearr[0]);
			}
		}
		else{
			min = twoDigits(--timearr[1]);
		}
	}
	else{
		sec = twoDigits(--timearr[2]);
	}
	if(day <= "0" && hour <= "00" && min == "00" && sec == "00"){
		el.innerHTML = "Closed";
	}
	else{
		if(showdays){
			el.innerHTML = day + "days " + hour + ":" + min + ":" + sec;
		}
		else{
			el.innerHTML = hour + ":" + min + ":" + sec;
		}
		setTimeout("tourney_countdown('"+divid+"',"+showdays+")",1000);
	}
}
var latest_chat_id = 0;
function get_color(userid){
	var user_colors = new Array();
	user_colors = ["99ffff","ff99ff","ffff99","ff9999","99ff99","9999ff","ffcc66","66ccff","ccff66","cc66ff","ff66cc","66ffcc"];
	return user_colors[userid%user_colors.length];
}
function update_chatroom(place,div)
{
	if(place == null){
		place = "normal";
	}
	if(div == null){
		div = "chatroom";
	}
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
 				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
 				alert("Your browser does not support AJAX!");
				return false;
			}
 		}
	}
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			var text = xmlHttp.responseText;
//			a("chatgeterror").innerHTML = text;
			var chat = text.split("@");
				// users
			if(place == "normal"){
				var users = chat[1].split("|");
				var user,userstr;
				for(var i=0;i<users.length;i++){
					user = users[i].split(",");
					userstr = '<a href="/home/?show='+user[0]+'" style="color:#'+get_color(parseInt(user[0]))+'">'+user[1]+'</span><br />';
					if(i == 0){
						a("chatroom_users").innerHTML = userstr;
					}
					else{
						a("chatroom_users").innerHTML += userstr;
					}
				}
			}
//			if(parseInt(chat[0]) != latest_chat_id){
				latest_chat_id = parseInt(chat[0]);
				var chat2,str;
				for(var i=2;i<chat.length;i++){
					chat2 = chat[i].split("|");
					if(chat2[1] == null){
						str = "Say something...<br />";
					}
					else{
						if(chat2[0] == "time"){
							str = '<div class="center">-- BBC Time: ' + chat2[1] + ' --</div>';
						}
						else{
							str = "<span style='color:#"+get_color(parseInt(chat2[0]))+"'>"+chat2[1]+":</span> "+chat2[2]+"<br />";
						}
					}
					if(i == 2){
						a(div).innerHTML = str;
					}
					else{
						a(div).innerHTML += str;
					}
				}
				a(div).scrollTop = a(div).scrollHeight;
//			}
		}
	}
	xmlHttp.open("GET","/ajax/chatroom_get.php?place="+place,true);
	xmlHttp.send(null);
}
function chatroom_post(user,name,place)
{
	var string = a("chatroom-text").value;
	if(string != ""){
		var xmlHttp;
		try {
			// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		} catch (e) {
			// Internet Explorer
			try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
		}
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				var text = xmlHttp.responseText;
	//			a("chatposterror").innerHTML = text;
			}
		}
		if(place == null){
			chat = "chatroom";
		}
		else if(place == "top"){
			chat = "top-chatroom-chat";
		}
		a("chatroom-text").value = "";
		var chatroom = a(chat);
		xmlHttp.open("GET","/ajax/chatroom_post.php?user="+user+"&text="+string,true);
		string = string.replace("<","&lt;");
		string = string.replace(">","&gt;");
		chatroom.innerHTML += "<span style='color:#"+get_color(parseInt(user))+"'>"+name+":</span> "+string+"<br />";
		chatroom.scrollTop = chatroom.scrollHeight;
		xmlHttp.send(null);
	}
}
