var xmlHttp;

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}



function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("info").innerHTML=xmlHttp.responseText;
  } 
}

function middleInfoBox() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("MiddleInfoBox").innerHTML=xmlHttp.responseText;
  } 
}

function iconbar() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("icon_bar").innerHTML=xmlHttp.responseText;
  } 
}

function chat_window() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("chat_window").innerHTML=xmlHttp.responseText;
  } 
}

function fleet_loc() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("show_fleet").innerHTML=xmlHttp.responseText;
  } 
}

function astropedia() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("show_info").innerHTML=xmlHttp.responseText;
  } 
}


function navbar() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("black_bars_top").innerHTML=xmlHttp.responseText;
  } 
}

function regbox() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("index_box").innerHTML=xmlHttp.responseText;
  } 
}

function message_box() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("message_box").innerHTML=xmlHttp.responseText;
  } 
}

function replace_html(id, content){
	document.getElementById(id).innerHTML = content;
	}
	
function show_progressbar(id) {
			replace_html(id, '<img src="graphics/icons/4.gif" border="0" alt="Loading, please wait..." />');
		}
		
		

				
				
	

function get_example()
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
var url="control_tasks.php";
url=url+"?t="+struct;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}


function async(url)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
url=url+"&sid="+Math.random(); 
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}




function build(loc)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }

url = "build.php?z=1";
params = "&loc="+loc;
url=url+"&sid="+Math.random(); 
xmlHttp.onreadystatechange=iconbar;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send(params);
}

function build_detail_old(structure)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }

url = "asyncpages/build_details.php?z=1";
params = "&structure="+structure;
url=url+"&sid="+Math.random(); 
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send(params);
}




function nav()
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
 
var url = "nv.php?t=0";
url=url+"&sid="+Math.random(); 
xmlHttp.onreadystatechange=navbar;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function mission()
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 { 
 alert ("Browser does not support HTTP Request");
 return;
 }


url = "missions.php?z=1";

mission_type = document.getElementById("mission_type").value;
planet = document.getElementById("planet").value;
level = document.getElementById("ml").value;
fleet = document.getElementById("fleet_to_use").value; 

var params = "&ml="+level+"&p="+planet+"&mission_type="+mission_type+"&fleet_to_use="+fleet;

if (level == 3){
	if (mission_type == 6){
		var metal = document.getElementById("mtt").value;
		var crystal = document.getElementById("ctt").value;
		var exotic = document.getElementById("ett").value;
		
		params = params + "&metal="+metal+"&crystal="+crystal+"&exotic="+exotic;
						}
				}

				
url=url+"&sid="+Math.random(); 
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send(params);
}


function get_fleet(fleet)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
 
 mission_type = document.getElementById("mission_type").value;
 
var params = "&fleet_to_use="+fleet+"&mission="+mission_type;
//alert(params);
url = "get_fleet.php?z=1";
url=url+"&sid="+Math.random(); 
xmlHttp.onreadystatechange=fleet_loc;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send(params);
}

function mission_desc(mission)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }

var params = "&mission="+mission;
url = "mission_desc.php?z=1";
url=url+"&sid="+Math.random(); 
xmlHttp.onreadystatechange=fleet_loc;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send(params);
}


function check_res()
{

pm = parseInt(document.getElementById("pm").value);
pc = parseInt(document.getElementById("pc").value);
pe = parseInt(document.getElementById("pe").value);
max_space = parseInt(document.getElementById("max").value);


var metal = parseInt(document.getElementById("mtt").value);
var crystal = parseInt(document.getElementById("ctt").value);
var exotic = parseInt(document.getElementById("ett").value);

//stuff = "-"+metal+"-"+crystal+"-"+exotic;
//alert(stuff);



used = 0;

if (metal > (max_space - used)){metal = (max_space - used);}
if (metal > max_space) { metal = max_space;}
if (metal > pm) {metal = pm;}

used += metal;

//if (used == max_space){crystal = 0;}

if (crystal > (max_space - used)){ crystal = (max_space - used);}
if (crystal > pc) {crystal = pc;}

used += crystal;
//if (used == max_space){exotic = 0;}

if (exotic > (max_space - used)) {exotic = (max_space - used);}
if (exotic > pe){ exotic = pe;}

used += exotic;

document.getElementById("mtt").value = metal;
document.getElementById("ctt").value = crystal;
document.getElementById("ett").value = exotic;

}


function reg_box()
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }


url = "reg.php?z=1";
url=url+"&sid="+Math.random(); 
xmlHttp.onreadystatechange=regbox;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}



function info(id)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }


url = "info.php?z=1";
url=url+"&i="+id;
url=url+"&sid="+Math.random(); 
xmlHttp.onreadystatechange=regbox;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function structure_info(id)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
var params = "&st=" +id;
url = "struct_info.php";
url=url+"?sid="+Math.random(); 
xmlHttp.onreadystatechange=astropedia;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send(params);
}

function ship_info(id)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
var params = "&sh=" +id;
url = "ship_info.php";
url=url+"?sid="+Math.random(); 
xmlHttp.onreadystatechange=astropedia;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send(params);
}

function send_chat()
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
 

var user = document.getElementById("user").value;
var chat_text = document.getElementById("chat_text").value;
 
//document.getElementById("chat_text").value ='';
 
var params = "&chat_text=" + chat_text +"&user=" + user;
url = "send_chat.php";
url=url+"?sid="+Math.random(); 
//xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send(params);
document.getElementById("chat_text").value ="";
}


function show_chat()
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
 
var url = "chat.php?z=1";
 
url=url+"&sid="+Math.random(); 
xmlHttp.onreadystatechange=chat_window;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

var objDiv = document.getElementById("chat_window");
objDiv.scrollTop = objDiv.scrollHeight;


}

function toBottom(){

nDiv = document.getElementById('chat_window');
setTimeout("nDiv.scrollTop = nDiv.scrollHeight",1);
}

function checkspace(){
		var maxHold = parseInt(document.getElementById("maxhold").value);
		
		var metal = parseInt(document.getElementById("metal").value);
		var crystal	= parseInt(document.getElementById("crystal").value);
		var exotic = parseInt(document.getElementById("exotic").value);
		
		if (!metal) metal = 0;
		if (!crystal) crystal = 0;
		if (!exotic) exotic = 0;
		
		
		
		var maxMetal = parseInt(document.getElementById("maxmetal").value);
		var maxCrystal = parseInt(document.getElementById("maxcrystal").value);
		var maxExotic = parseInt(document.getElementById("maxexotic").value);
		
		var availableSpace = 0;
		
		if (metal > maxMetal) {metal = maxMetal;}
		if (crystal > maxCrystal) {crystal = maxCrystal;}
		if (exotic > maxExotic) {exotic	= maxExotic;}
		
		//update values in the resources table to make sure that they are not trying
		//to take more resources than they have...
		
		document.getElementById("metal").value = metal;
		document.getElementById("crystal").value = crystal;
		document.getElementById("exotic").value = exotic;
		
		availableSpace = maxHold - (metal + crystal + exotic);
		document.getElementById("availablespace").innerHTML = availableSpace;
		if (availableSpace < 0){
			document.getElementById("feedback").innerHTML = "<p class='Bold'>Fleet Overloaded, space available: " + availableSpace + "</p>";
			}
		
		else {
					document.getElementById("feedback").innerHTML = "<input type ='submit' value ='Send Mission'>";
			}
					
		
		}




function pausescript(millis) 
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); } 
while(curDate-date < millis);
} 




