onload = function() {
   setInterval(serverStatus, 30000);
}

var filename = window.location.pathname.replace(/^.*[\\\/]/, '');
var xmlhttp;
var xmlhttp_2;
if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	xmlhttp_2=new XMLHttpRequest();
	}
else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	xmlhttp_2=new ActiveXObject("Microsoft.XMLHTTP");
	}

function showCollection(){
	document.getElementById('content_dir').style.display="inline";
	}

function hideCollection(){
	document.getElementById('content_dir').style.display="none";
	}

function searchImg(startrow){
	
	document.getElementById('search_loading').style.display="block";

	var search_all = document.getElementById("search_all").value;
	var search_game = document.getElementById("search_game").value;
	var search_type = document.getElementById("search_type").value;
	var per_page = document.getElementById("per_page").value;
	
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
		document.getElementById("search_result").innerHTML=xmlhttp.responseText;
		document.getElementById('search_loading').style.display="none";
		jQuery("#search_result a[rel^='lightbox']").slimbox();
		}
	  }
	xmlhttp.open("GET","ajax/ajax_img_search.php?search_all="+search_all+"&search_game="+search_game+"&search_type="+search_type+"&per_page="+per_page+"&startrow="+startrow,true);
	xmlhttp.send();
}

function searchOrg(startrow){
	
	document.getElementById('search_loading').style.display="block";

	var search_faction = document.getElementById("search_faction").value;
	var search_type = document.getElementById("search_type").value;
	var search_game = document.getElementById("search_game").value;
	var search_display = document.getElementById("search_display").value;
	var search_language = document.getElementById("search_language").value;
	var per_page = document.getElementById("per_page").value;
	var search_all = document.getElementById("search_all").value;	
	var pve_min = document.getElementById("pve_min").value;
	var pve_max = document.getElementById("pve_max").value;
	var pvp_min = document.getElementById("pvp_min").value;
	var pvp_max = document.getElementById("pvp_max").value;
	var rp_min = document.getElementById("rp_min").value;
	var rp_max = document.getElementById("rp_max").value;
	var raid_min = document.getElementById("raid_min").value;
	var raid_max = document.getElementById("raid_max").value;
	
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
		document.getElementById("search_result").innerHTML=xmlhttp.responseText;
		document.getElementById('search_loading').style.display="none";
		}
	  }
	xmlhttp.open("GET","ajax/ajax_org_search.php?search_faction="+search_faction+"&search_type="+search_type+"&search_game="+search_game+"&search_display="+search_display+"&search_language="+search_language+"&per_page="+per_page+"&search_all="+search_all+"&pve_min="+pve_min+"&pve_max="+pve_max+"&pvp_min="+pvp_min+"&pvp_max="+pvp_max+"&rp_min="+rp_min+"&rp_max="+rp_max+"&raid_min="+raid_min+"&raid_max="+raid_max+"&startrow="+startrow,true);
	xmlhttp.send();
}

function searchRef(startrow){
	
	document.getElementById('search_loading').style.display="block";

	var search_type = document.getElementById("search_type").value;
	var search_game = document.getElementById("search_game").value;
	var search_display = document.getElementById("search_display").value;
	var per_page = document.getElementById("per_page").value;
	var search_all = document.getElementById("search_all").value;
	var search_author = document.getElementById("search_author").value;
	var search_collection = document.getElementById("search_collection").value;
	
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
		document.getElementById("search_result").innerHTML=xmlhttp.responseText;
		document.getElementById('search_loading').style.display="none";
		}
	  }
	xmlhttp.open("GET","ajax/ajax_ref_search.php?search_type="+search_type+"&search_game="+search_game+"&search_display="+search_display+"&per_page="+per_page+"&search_all="+search_all+"&search_author="+search_author+"&search_collection="+search_collection+"&startrow="+startrow,true);
	xmlhttp.send();
}

function refreshCollection(){
	
	var search_type = document.getElementById("search_type").value;
	var search_game = document.getElementById("search_game").value;

	xmlhttp_2.onreadystatechange=function()
	  {
	  if (xmlhttp_2.readyState==4 && xmlhttp_2.status==200)
		{
		document.getElementById("search_collection").innerHTML=xmlhttp_2.responseText;
		}
	  }
	xmlhttp_2.open("GET","ajax/ajax_collection_refresh.php?search_type="+search_type+"&search_game="+search_game,true);
	xmlhttp_2.send();
}

function chatUsersUpdate(){
	
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
		document.getElementById("chat_users").innerHTML=xmlhttp.responseText;
		}
	  }
	xmlhttp.open("GET","ajax/ajax_chat_users_update.php",true);
	xmlhttp.send();	
}

function chatUpdate(){

	var chat_pos = document.getElementById("chat_hold").value;
	
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
		//document.getElementById("chat_area").innerHTML=xmlhttp.responseText;
		if (xmlhttp.responseText != "") {
			$('input#chat_hold').replaceWith(xmlhttp.responseText);
			chatScrollD();
			if (document.getElementById('chat_alert').checked == true) {playSound('audio/gongs.wav')};
			}
		}
	  }
	//xmlhttp.open("GET","content/root/chat_buffer.htm",true);
	xmlhttp.open("GET","ajax/ajax_chat_update.php?chat_pos="+chat_pos,true);
	xmlhttp.send();
}

function chatScrollD(){
	if (document.getElementById('scroll_lock').checked == true){
		document.getElementById('chat_area').scrollTop = document.getElementById('chat_area').scrollHeight;
	}
}

function chatSend(text) {

	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
		//document.getElementById("chat_area").innerHTML=xmlhttp.responseText;
		//NOTE: replaceWith is jquery
		//$('div#chat_area').replaceWith(xmlhttp.responseText);
		}
	  }
	
	var parameters="text="+text;
	
	xmlhttp.open("POST", "proc/proc_chat_send.php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send(parameters);
}

function playSound(soundfile) {
	if (navigator.appName=="Microsoft Internet Explorer") {
		document.getElementById("dummy").innerHTML="<bgsound src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"0\" />";
	}
	else {
		document.getElementById("dummy").innerHTML="<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
	}
}

function serverStatus(){
	
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
		document.getElementById("server_status").innerHTML=xmlhttp.responseText;
		}
	  }
	xmlhttp.open("GET","content/fetch/serverstatus.htm");
	xmlhttp.send();
}

//jquery AJAX

// RETIRE
function refresh_type() {
	//$('#search_result').html('It works');
	$.get("inc/inc_form_select_entity_type.php", {category: ""+$('#category').val()+"", game: ""+$('#game').val()+""}, function(data){
		if(data.length >0) {
			$('#type').html(data);
			//$('#type').html(data);
		}
	});
}

function search_entity(startrow) {
	$.get("ajax/ajax_search_entity.php", {game: ""+$('#game').val()+"", category: ""+$('#category').val()+"", faction: ""+$('#faction').val()+"", display: ""+$('#display').val()+"" , per_page: ""+$('#per_page').val()+"", author: ""+$('#select_id_author').val()+"", owner: ""+$('#select_id_owner').val()+"", search_text: ""+$('#search_text').val()+"", startrow: ""+startrow+""}, function(data){
		if(data.length >0) {
			$('#search_result').html(data);
			//$('#type').html(data);
		}
	});
}	

//jquery auto-suggest

function lookup_entity(inputString) {
	if(inputString.length == 0) {
		// Hide the suggestion box.
		$('#suggestions_entity').hide();
	} else {
		// post data to our php processing page and if there is a return greater than zero
		// show the suggestions box
		$.post("ajax/ajax_autosuggest_entity.php", {mysearchString: ""+inputString+""}, function(data){
			if(data.length >0) {
				$('#suggestions_entity').show();
				$('#autoSuggestionsList_entity').html(data);
			}
		});
	}
} //end

function fill_entity(name) {
	$('#entity_name').val(name);
	setTimeout("$('#suggestions_entity').hide();", 200);
}

function lookup_entity_select(inputString,type) {
	if(inputString.length == 0) {
		// Hide the suggestion box.
		$('#suggestions_entity_'+type).hide();
	} else {
		// post data to our php processing page and if there is a return greater than zero
		// show the suggestions box
		$.post("ajax/ajax_autosuggest_entity_select.php", {mysearchString: ""+inputString+"", type: ""+type+""}, function(data){
			if(data.length >0) {
				$('#suggestions_entity_'+type).show();
				$('#autoSuggestionsList_entity_'+type).html(data);
			}
		});
		// Trigger search if on a search page
	}
} //end

function fill_entity_select(name,id,type) {
	$('#select_name_'+type).val(name);
	$('#select_id_'+type).val(id);
	$('#suggestions_entity_'+type).hide();
	if (filename == 'library_search.php') search_entity(0);
}
