// AJAX Scripts

var xmlreqs=new Array();
function CXMLReq(freed) {
this.freed=freed;
this.xmlhttp=false;
if (window.XMLHttpRequest) {
this.xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject) {
this.xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}}
function xmlreqGET(url,op) {
var pos=-1;
for (var i=0;i<xmlreqs.length;i++) {
if (xmlreqs[i].freed==1) {
pos=i;break;
}}
if (pos==-1) {
pos=xmlreqs.length;
xmlreqs[pos]=new CXMLReq(1);
}
if (xmlreqs[pos].xmlhttp) {
xmlreqs[pos].freed=0;
xmlreqs[pos].xmlhttp.open("GET",url,true);
xmlreqs[pos].xmlhttp.onreadystatechange=function() {
if (typeof(xmlhttpChange)!='undefined') {
xmlhttpChange(pos,op);
}}
if (window.XMLHttpRequest) {
xmlreqs[pos].xmlhttp.send(null);
}
else if (window.ActiveXObject) {
xmlreqs[pos].xmlhttp.send();
}}}
function xmlreqPOST(url,data,op) {
var pos=-1;
for (var i=0;i<xmlreqs.length;i++) {
if (xmlreqs[i].freed==1) {
pos=i;
break;
}}
if (pos==-1) {
pos=xmlreqs.length;
xmlreqs[pos]=new CXMLReq(1);
}
if (xmlreqs[pos].xmlhttp) {
xmlreqs[pos].freed=0;
xmlreqs[pos].xmlhttp.open("POST",url,true);
xmlreqs[pos].xmlhttp.onreadystatechange=function() {
if (typeof(xmlhttpChange)!='undefined') {
xmlhttpChange(pos,op);
}}
xmlreqs[pos].xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlreqs[pos].xmlhttp.send(data);
}}
function xmlhttpChange(pos,op) {
if (typeof(xmlreqs[pos])!='undefined' && xmlreqs[pos].freed==0 && xmlreqs[pos].xmlhttp.readyState == 4) {
if (xmlreqs[pos].xmlhttp.status==200||xmlreqs[pos].xmlhttp.status==304) {
//handle_response(xmlreqs[pos].xmlhttp.responseXML,op);
handle_response(xmlreqs[pos].xmlhttp.responseText,op);
}
else {handle_error(op);}
xmlreqs[pos].freed = 1;
}}

function handle_response(text,op) {
if (op==1) {document.getElementById("civ2port_data").innerHTML=text;}
else if (op==2) {
var u=text.substr(0,1);
var v=text.substr(1).split("#");
//alert(u);
//alert(v);
if (u!="0") {//alert("lala");
var div=document.createElement("div");div.className="host0";
/*if (document.getElementById("civ2port_active").firstChild) {
var t=document.getElementById("civ2port_active").firstChild;
t.insertBefore(div);
}
else {*/
document.getElementById("civ2port_active").appendChild(div);
//}
//var t=document.createTextNode(v);
var t0=document.createElement("div");t0.className="host0";
var t1=document.createElement("div");t1.className="host1";
var t2=document.createElement("div");t2.className="host2";
var t3=document.createElement("div");t3.className="host3";
var t4=document.createElement("div");t4.className="host4";
var t5=document.createElement("div");t5.className="host5";
var t6=document.createElement("div");t6.className="clear nosize";
x1=document.createTextNode(v[0]);x2=document.createTextNode(v[1]);x3=document.createTextNode(v[2]);x4=document.createTextNode(v[3]);x5=document.createTextNode(v[4]);
t1.appendChild(x1);t2.appendChild(x2);t3.appendChild(x3);t4.appendChild(x4);t5.appendChild(x5);
t0.appendChild(t1);t0.appendChild(t2);t0.appendChild(t3);t0.appendChild(t4);t0.appendChild(t5);t0.appendChild(t6);
document.getElementById("civ2port_active").firstChild.appendChild(t0);
}
else { // bad host
}}
else if (op==3) {
if (text==1) {document.getElementById('civ2port_added').innerHTML="<span style=\"font-weight:bold\" class=\"civ2port_add_serv\">Your Civ2 Multiplayer host has been added, and other users may see it. <br />If you want to invite other players, launch a MP Civ2 game and click below.</span><br /><br /><input type=\"submit\" class=\"submit\" value=\"Alert the whole Civ2 players community\" onclick=\"notify_players();return false;\" />";}
else {document.getElementById('civ2port_added').innerHTML="<span class=\"civ2port_add_serv\">"+text+"</span>";}
}
else if (op==4) {
if (text==1) {document.getElementById('civ2port_added').innerHTML="<span style=\"font-weight:bold\" class=\"civ2port_add_serv\">Your game has been notified to the Civ2Academy players.</span>";}
else {document.getElementById('civ2port_added').innerHTML="<span class=\"civ2port_add_serv\">"+text+"</span>";}
}
else if (op==5) {
if (text==1) {document.getElementById('add_e_status').innerHTML="<span style=\"font-weight:bold\" class=\"civ2port_add_serv\">Your email address has been added.</span>";document.getElementById('email_a').value="";}
else {document.getElementById('add_e_status').innerHTML="<span class=\"civ2port_add_serv\">"+text+"</span>";}
document.getElementById('add_e_status').style.display="block";
}
//alert(text+"!!"+op);
}

function handle_error(op) {}

function civ2port_load() {
xmlreqGET("handler.php?action=1",1);
setTimeout("get_civ2port_hosts()",3000);
}

function get_civ2port_hosts() {
if (document.getElementById("civ2port_data").innerHTML=="") {setTimeout("get_civ2port_hosts()",3000);}
var chosts=document.getElementById("civ2port_data").innerHTML.split("#");
document.getElementById("civ2port_data").innerHTML="";
//alert(chosts);
for(i=1;i<(chosts.length);i++) {
var k=chosts[i].split("|");
//alert(k[0]+"//"+k[1]);
test_civ2port(k[0],k[1],k[2]);
}
document.getElementById("civ2port_status").innerHTML="<span style=\"font-weight:bold\">All Civ2 servers listed below are active now.</span>";
}

function test_civ2port(ip,user,date) {
xmlreqGET("handler.php?action=2&ip="+ip+"&user="+user+"&date="+date,2);
}

function add_server(nick,ip) {
xmlreqGET("handler.php?action=3&nick="+nick+"&ip="+ip,3);
}

function notify_players () {
xmlreqGET("handler.php?action=4",4);
}

function add_email(email) {
xmlreqGET("handler.php?action=5&email="+email,5);
}

