// The AJAX function...

function AJAX(){
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
return xmlHttp;
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
return xmlHttp;
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
return xmlHttp;
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}
}

// Timestamp for preventing IE caching the GET request (common function)

function fetch_unix_timestamp()
{
 return parseInt(new Date().getTime().toString().substring(0, 10))
}

////////////////////////////////
//
// Refreshing the DIV gameinfo
//
////////////////////////////////

function refreshdiv_gameinfo(){

// Customise those settings
var pid = location.search;
var seconds = 10;
var divid = "gameinfo";
var url = "./includes/gather_gameinfo.php";

// Create xmlHttp

var xmlHttp_one = AJAX();

// No cache

var timestamp = fetch_unix_timestamp();
var nocacheurl = url+ pid +"&t="+timestamp;

// The code...

xmlHttp_one.onreadystatechange=function(){
if(xmlHttp_one.readyState==4){
document.getElementById(divid).innerHTML=xmlHttp_one.responseText;
setTimeout('refreshdiv_gameinfo()',seconds*1000);
}
}
xmlHttp_one.open("GET",nocacheurl,true);
xmlHttp_one.send(null);
}

// Start the refreshing process

window.onload = function startrefresh(){
setTimeout('refreshdiv_gameinfo()',seconds*1000);
}

////////////////////////////////
//
// Refreshing the DIV playerslots
//
////////////////////////////////

function refreshdiv_playerslots(){

// Customise those settings
var pid = location.search;
var seconds = 3;
var divid = "playerslots";
var url = "./includes/gather_playerslots.php";

// Create xmlHttp

var xmlHttp_two = AJAX();

// No cache

var timestamp = fetch_unix_timestamp();
var nocacheurl = url+ pid +"&t="+timestamp;

// The code...

xmlHttp_two.onreadystatechange=function(){
if(xmlHttp_two.readyState==4){
document.getElementById(divid).innerHTML=xmlHttp_two.responseText;
setTimeout('refreshdiv_playerslots()',seconds*1000);
}
}
xmlHttp_two.open("GET",nocacheurl,true);
xmlHttp_two.send(null);
}

// Start the refreshing process

window.onload = function startrefresh(){
setTimeout('refreshdiv_playerslots()',seconds*1000);
}

////////////////////////////////
//
// Refreshing the DIV buttons
//
////////////////////////////////

function refreshdiv_buttons(){

// Customise those settings
var pid = location.search;
var seconds = 3;
var divid = "buttons";
var url = "./includes/gather_kick.php";
// Create xmlHttp

var xmlHttp_three = AJAX();

// No cache
var timestamp = fetch_unix_timestamp();
var nocacheurl = url+ pid +"&t="+timestamp;

// The code...

xmlHttp_three.onreadystatechange=function()
{

	if(xmlHttp_three.readyState==4)
	{		
		var kick = xmlHttp_three.responseText;
		if (kick==1)
		{
			Test(0,1);			
		}
		if (kick==2)
		{
			alert('kick ' + kick + '.');
		}
		document.getElementById(divid).innerHTML=xmlHttp_three.responseText;
		setTimeout('refreshdiv_buttons()',seconds*1000);
	}
}
xmlHttp_three.open("GET",nocacheurl,true);
xmlHttp_three.send(null);
}

// Start the refreshing process

var seconds;
window.onload = function startrefresh(){
	setTimeout('refreshdiv_buttons()',seconds*1000);
}

////////////////////////////////
//
// Button Ready
//
////////////////////////////////
var xmlHttp_five = AJAX();
function gatReady(pid) {
  var getdate = new Date();  //Used to prevent caching during ajax call
  if(xmlHttp_five) { 
    var ready = document.getElementById("ready");
    xmlHttp_five.open("POST","includes/gather_buttons.php",true);
    xmlHttp_five.onreadystatechange  = handleReady;
    xmlHttp_five.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp_five.send("ready=" + ready.value + "&pid=" + pid);
  }
}

function handleReady() {
   if (xmlHttp_five.readyState == 4) {
     if(xmlHttp_five.status == 200) {

        if (document.myForm.readyb.value == "Ready") {
            document.myForm.readyb.value = "Not Ready";
            document.myForm.ready.value = "Waiting";
        } else {
            document.myForm.readyb.value = "Ready";
            document.myForm.ready.value = "Ready";
        }
     }
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
}


////////////////////////////////
//
// Button Leave/Delete
//
////////////////////////////////
var xmlHttp_six = AJAX();
function gatLeadel(pid) {
  var getdate = new Date();  //Used to prevent caching during ajax call
  if(xmlHttp_six) { 
    var leave = document.getElementById("leave");
    xmlHttp_six.open("POST","includes/gather_buttons.php",true); //calling testing.php using POST method
    xmlHttp_six.onreadystatechange  = handleLeave;
    xmlHttp_six.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp_six.send("leave=" + leave.value + "&pid=" + pid); //Posting ready to PHP File
  }
}

function handleLeave() {
   if (xmlHttp_six.readyState == 4) {
     if(xmlHttp_six.status == 200) {

     }
     else {
        alert("Error during function. Please try again");
     }
   }
}


////////////////////////////////
//
// Filter Search
//
////////////////////////////////
var xmlHttp_seven = AJAX();
function showUser(reset)
{
	if(reset) {
		document.filterz.empty.checked=false;
		document.filterz.full.checked=false;
		document.filterz.srv.checked=false;
		document.filterz.voice.checked=false;
		document.filterz.slots.value=0;
		document.filterz.skills.value=0;
	}

		if(document.getElementById('empty').checked){
			var empty = '1';
		}
		if(document.getElementById('full').checked){
			var full = '1';
		}
		if(document.getElementById('srv').checked){
			var srv = '1';
		}
		if(document.getElementById('voice').checked){
			var voice = '1';
		}		
		var slot=encodeURIComponent(document.getElementById("slots").value)
		var skill=encodeURIComponent(document.getElementById("skills").value)
	var action = document.getElementById('action').value;
	var room = document.getElementById('room').value;
	var url="./includes/gather_search.php";
	url=url+"?a="+slot+"&b="+skill+"&c="+empty+"&d="+full+"&e="+room+"&f="+srv+"&g="+voice+"&action="+action;
	url=url+"&sid="+Math.random();
	xmlHttp_seven.onreadystatechange=stateChanged;
	xmlHttp_seven.open("GET",url,true);
	xmlHttp_seven.send(null);
}

function stateChanged()
{
	if (xmlHttp_seven.readyState==4)
	{
		document.getElementById("search").innerHTML=xmlHttp_seven.responseText;
	}
	else 
	{
		document.getElementById("search").innerHTML = '<img src="/images/gather/loading.gif">';
	}
}

////////////////////////////////
//
// Button Change
//
////////////////////////////////
var xmlHttp_eight = AJAX();
function gatChange(pid) {
  var getdate = new Date();  //Used to prevent caching during ajax call
  if(xmlHttp_eight) { 
    var change = document.getElementById("change").value;
    xmlHttp_eight.open("POST","includes/gather_buttons.php",true); //calling testing.php using POST method
    xmlHttp_eight.onreadystatechange  = handleChange;
    xmlHttp_eight.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp_eight.send("change=" + change + "&pid=" + pid); //Posting ready to PHP File
  }
}

function handleChange() {
   if (xmlHttp_eight.readyState == 4) {
     if(xmlHttp_eight.status == 200) {

     }
     else {
        alert("Error during function. Please try again");
     }
   }
}


//////// prematch popup functions /////////////////////////////////////

function gatherleave() {

this.focus();

parent.opener = this;

parent.close();
}

function gatherclose() {

alert ("You have been kicked");

this.focus();

parent.opener = this;

parent.close();
}

function Test(pid,id) {

var wid = (screen.width - 600) / 2;

var hei = (screen.height - 300) / 2;
	if(pid > 0)
	{
		var prematch = window.open('gather_prematch.php?pid='+pid, 'Pre Match', 'width=800,height=730,toolbar=0,statusbar=0,scrollbars=0,menubar=0,left='+wid+',top='+hei);
	}
	if(id==1)
	{
		this.focus();
	
		alert('You have been kicked');	
		
		this.close();
	}
	if(id==2)
	{
		alert('You have been Banned');
		
		prematch.close();
	}
}
