function template(lnk, vars)
{
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)
 {
  con=document.getElementById('content'); 
  con.innerHTML="";
  data=xmlHttp.responseText; c=0;
	 if(typeof(prev) == "undefined") { prev=''; }
		con.innerHTML="[ <a class='q_link' href=\"javascript: template('"+prev+"', '')\"><font color='red'>previous page</font></a> ]" ; prev=lnk ; 
  con.innerHTML+="[<a class='q_link' target='blank_' href='about.php'> H e l p ! </a>]";
  con.innerHTML+="[<a class='q_link' target='blank_' href='refferal.php'> <font color='green'>! Get free GP ! </a></font>]</br>";
  if (data.indexOf("<script type='text/javascript'>")>-1)
   for (start=data.indexOf("<script type='text/javascript'>"); start>-1; start=data.indexOf("<script type='text/javascript'>"))
   {
    end=data.indexOf("</script>");
				scr=document.createElement("SCRIPT"); scr.type = "text/javascript"; scr.text=data.substring(start+31, end-1);
    con.innerHTML+=data.substring(0, start-1); con.appendChild(scr);
    if (data.length>end+9) data=data.substring(end+9); else data="";
   }
  con.innerHTML+=data;
 } gtime();
}
if (vars)
{
 xmlHttp.open("POST", lnk, true);
 xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
 xmlHttp.send(vars);
}
else
{
 xmlHttp.open("GET", lnk, true);
 xmlHttp.send(null);
}
}
//##########################################################
function gtime()
{
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)
 {
  con=document.getElementById('gtime'); 
  con.innerHTML=xmlHttp.responseText;
 }
}
 xmlHttp.open("GET", "now.php", true);
 xmlHttp.send(null);
}
//##########################################################
function chat(msg, sid)
{
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)
 {
  cbox=document.getElementById("cBox");
  cbox.value+=xmlHttp.responseText;
  cbox.scrollTop=cbox.scrollHeight;
 }
}
xmlHttp.open("POST","chat_.php?sid="+sid, true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
xmlHttp.send('msg='+msg);
}
//##########################################################3
function desc(town, player, population, alliance){
	document.getElementById("desc_town").innerHTML=town;
	document.getElementById("desc_player").innerHTML=player;
	document.getElementById("desc_population").innerHTML=population;
	document.getElementById("desc_alliance").innerHTML=alliance;
}

function timer(v, lnk)
{
	dat=document.getElementById(v);
	var time=(dat.innerHTML).split(":"); var done=0;
	if (time[2]>0) time[2]--;
	else
	{
		time[2]=59;
		if (time[1]>0) time[1]--;
		else
		{
			time[1]=59;
			if (time[0]>0) time[0]--;
			else { clearTimeout(id[v]); template(lnk, ''); done=1;}
		}
	}
	if (!done)
	{
		dat.innerHTML=time[0]+":"+time[1]+":"+time[2];
		clearTimeout(id[v]);
		id[v]=setTimeout("timer('"+v+"', '"+lnk+"')", 1000);
	}
}

function forgot()
{
	usr=document.getElementById("name");
	go("forgot.php?name="+usr.value);
}

function trade_options(type, subtype, data)
{
	t=document.getElementById(type);
	st=document.getElementById(subtype);
	if (t.value==0) st.innerHTML="<select name='"+subtype.substr(0, subtype.length-1)+"'><option value='0'>Crop</option><option value='1'>Lumber</option><option value='2'>Stone</option><option value='3'>Iron</option><option value='4'>Gold</option></select>";
	else st.innerHTML="<select name='"+subtype.substr(0, subtype.length-1)+"'><option value='0'>"+data[0]+"</option><option value='1'>"+data[1]+"</option><option value='2'>"+data[2]+"</option><option value='3'>"+data[3]+"</option><option value='4'>"+data[4]+"</option><option value='5'>"+data[5]+"</option><option value='6'>"+data[6]+"</option><option value='7'>"+data[7]+"</option><option value='8'>"+data[8]+"</option><option value='9'>"+data[9]+"</option><option value='10'>"+data[10]+"</option></select>";
}
//misc functions
function login()
{
 vars="name="+document.getElementById("name").value+"&pass="+document.getElementById("pass").value;
 template("login_.php", vars);
}
function sit()
{
 vars="account="+document.getElementById("account").value+"&name="+document.getElementById("sname").value+"&pass="+document.getElementById("spass").value;
 template("sit.php", vars);
}
function profile_edit()
{
 vars="email="+document.getElementById("email").value+"&desc="+document.getElementById("desc").value+"&sitter="+document.getElementById("sitter").value+"&grpath="+document.getElementById("grpath").value+"&pass="+document.getElementById("pass").value;
 template("profile_edit.php", vars);
}
function pass_edit()
{
 vars="pass="+document.getElementById("opass").value+"&pass_="+document.getElementById("npass").value+"&pass__="+document.getElementById("npass_").value;
 template("pass.php", vars);
}
function del_acc()
{
 vars="pass="+document.getElementById("dapass").value;
 template("delacc.php", vars);
}
function map_go()
{
 vars="x="+document.getElementById("x").value+"&y="+document.getElementById("y").value;
 template("map.php", vars);
}
function write_msg()
{
 vars="recipient="+document.getElementById("recipient").value+"&subject="+document.getElementById("subject").value+"&contents="+document.getElementById("contents").value;
 template("writemsg_.php", vars);
}
function create_town(is_cap)
{
 vars="x="+document.getElementById("x").value+"&y="+document.getElementById("y").value+"&name="+document.getElementById("name").value;
 template("create_.php?is_cap="+is_cap, vars);
}
function ch_capital()
{
 vars="name="+document.getElementById("name").value+"&pass="+document.getElementById("pass").value;
 template("ch_capital_.php", vars);
}
function set_workers(town, s, b)
{
 vars="workers="+document.getElementById("workers").value;
 template("bslot.php?town="+town+"&s="+s+"&b="+b, vars);
}
function set_vars(town)
{
 vars="rations="+document.getElementById("rations").value+"&taxes="+document.getElementById("taxes").value;
 template("vars.php?town="+town, vars);
}
function send_r(town)
{
 vars="sQ="+document.getElementById("s_sQ").value+"&sType="+document.getElementById("s_sType").value+"&name="+document.getElementById("s_name").value+"&x="+document.getElementById("s_x").value+"&y="+document.getElementById("s_y").value+"&maxTime="+document.getElementById("s_maxTime").value;
 template("trade.php?town="+town+"&type=1", vars);
}
function sell_npc(town)
{
 vars="sQ="+document.getElementById("n_sQ").value+"&sType="+document.getElementById("n_sType").value+"&bType="+document.getElementById("n_bType").value;
 template("npc_trade.php?town="+town, vars);
}
function post_offer(town)
{
 vars="sQ="+document.getElementById("o_sQ").value+"&sType="+document.getElementById("o_sType").value+"&bQ="+document.getElementById("o_bQ").value+"&bType="+document.getElementById("o_bType").value+"&maxTime="+document.getElementById("o_maxTime").value;
 template("trade.php?town="+town+"&type=0", vars);
}
function find_offers(town)
{
 vars="sType="+document.getElementById("b_sType").value+"&bType="+document.getElementById("b_bType").value;
 template("offers.php?town="+town+"&page=0", vars);
}
function a_create(town)
{
 vars="name="+document.getElementById("a_name").value;
 template("a_create.php?town="+town, vars);
}
function invite(town)
{
 vars="name="+document.getElementById("i_name").value;
 template("invite.php?town="+town, vars);
}
function p_pact(town)
{
 vars="name="+document.getElementById("p_name").value;
 template("pact.php?town="+town+"&type=0", vars);
}
function w_pact(town)
{
 vars="name="+document.getElementById("w_name").value;
 template("pact.php?town="+town+"&type=1", vars);
}
function a_edit(town)
{
 vars="name="+document.getElementById("al_name").value+"&desc="+document.getElementById("al_desc").value;
 template("a_edit.php?town="+town, vars);
}
function a_del()
{
 vars="pass="+document.getElementById("pass").value;
 template("a_del.php", vars);
}
function act_forum(town)
{
 vars="a="+document.getElementById("f_a").value+"&id="+document.getElementById("f_id").value+"&parent="+document.getElementById("f_parent").value+"&name="+document.getElementById("f_name").value+"&desc="+document.getElementById("f_desc").value;
 template("forums_.php?town="+town, vars);
}
function act_thread(town, forum)
{
 vars="a="+document.getElementById("t_a").value+"&id="+document.getElementById("t_id").value+"&name="+document.getElementById("t_name").value+"&desc="+document.getElementById("t_desc").value+"&content="+document.getElementById("t_content").value;
 template("thread_.php?town="+town+"&forum="+forum, vars);
}
function act_post(town, thread)
{
 vars="a="+document.getElementById("p_a").value+"&id="+document.getElementById("p_id").value+"&desc="+document.getElementById("p_desc").value+"&content="+document.getElementById("p_content").value;
 template("posts_.php?town="+town+"&thread="+thread, vars);
}
function train(town, type)
{
 vars="q="+document.getElementById("q"+type).value;
 template("train.php?town="+town+"&type="+type, vars);
}
function create_hero(town)
{
 vars="name="+document.getElementById("name").value;
 template("hero_create_.php?town="+town, vars);
}
function send_t(town)
{
 vars="type="+document.getElementById("type").value+"&target="+document.getElementById("target").value+"&hero="+document.getElementById("hero").value+"&st="+document.getElementById("st").value+"&q_0="+document.getElementById("q_0").value+"&q_1="+document.getElementById("q_1").value+"&q_2="+document.getElementById("q_2").value+"&q_3="+document.getElementById("q_3").value+"&q_4="+document.getElementById("q_4").value+"&q_5="+document.getElementById("q_5").value+"&q_6="+document.getElementById("q_6").value+"&q_7="+document.getElementById("q_7").value+"&q_8="+document.getElementById("q_8").value+"&q_9="+document.getElementById("q_9").value+"&q_10="+document.getElementById("q_10").value+"&q_11="+document.getElementById("q_11").value;
 template("sendt.php?town="+town, vars);
}
function send_csim_data(town)
{
 //defender data
 vars="type="+document.getElementById("type").value+"&d_faction="+document.getElementById("d_faction").value;
 vars+="&d_army_0="+document.getElementById("d_army_0").value+"&d_army_1="+document.getElementById("d_army_1").value+"&d_army_2="+document.getElementById("d_army_2").value+"&d_army_3="+document.getElementById("d_army_3").value+"&d_army_4="+document.getElementById("d_army_4").value+"&d_army_5="+document.getElementById("d_army_5").value+"&d_army_6="+document.getElementById("d_army_6").value+"&d_army_7="+document.getElementById("d_army_7").value+"&d_army_8="+document.getElementById("d_army_8").value+"&d_army_9="+document.getElementById("d_army_9").value+"&d_army_10="+document.getElementById("d_army_10").value+"&d_army_11="+document.getElementById("d_army_11").value;
 vars+="&d_hero_0="+document.getElementById("d_hero_0").value+"&d_hero_1="+document.getElementById("d_hero_1").value+"&d_hero_2="+document.getElementById("d_hero_2").value+"&d_hero_3="+document.getElementById("d_hero_3").value+"&d_hero_4="+document.getElementById("d_hero_4").value+"&d_hero_5="+document.getElementById("d_hero_5").value;
 vars+="&d_hero_talents_0="+document.getElementById("d_hero_talents_0").value+"&d_hero_talents_1="+document.getElementById("d_hero_talents_1").value+"&d_hero_talents_2="+document.getElementById("d_hero_talents_2").value+"&d_hero_talents_3="+document.getElementById("d_hero_talents_3").value+"&d_hero_talents_4="+document.getElementById("d_hero_talents_4").value+"&d_hero_talents_5="+document.getElementById("d_hero_talents_5").value+"&d_hero_talents_6="+document.getElementById("d_hero_talents_6").value;
 vars+="&d_hero_active_spells="+document.getElementById("d_hero_active_spells").value;
 vars+="&d_tech_0="+document.getElementById("d_tech_0").value+"&d_tech_1="+document.getElementById("d_tech_1").value+"&d_tech_2="+document.getElementById("d_tech_2").value+"&d_tech_3="+document.getElementById("d_tech_3").value+"&d_tech_4="+document.getElementById("d_tech_4").value+"&d_tech_5="+document.getElementById("d_tech_5").value+"&d_tech_6="+document.getElementById("d_tech_6").value+"&d_tech_7="+document.getElementById("d_tech_7").value+"&d_tech_8="+document.getElementById("d_tech_8").value;
 vars+="&d_buildings_0="+document.getElementById("d_buildings_0").value+"&d_buildings_1="+document.getElementById("d_buildings_1").value+"&d_buildings_2="+document.getElementById("d_buildings_2").value+"&d_buildings_3="+document.getElementById("d_buildings_3").value+"&d_buildings_4="+document.getElementById("d_buildings_4").value+"&d_buildings_5="+document.getElementById("d_buildings_5").value+"&d_buildings_6="+document.getElementById("d_buildings_6").value+"&d_buildings_7="+document.getElementById("d_buildings_7").value+"&d_buildings_8="+document.getElementById("d_buildings_8").value+"&d_buildings_9="+document.getElementById("d_buildings_9").value+"&d_buildings_10="+document.getElementById("d_buildings_10").value+"&d_buildings_11="+document.getElementById("d_buildings_11").value+"&d_buildings_12="+document.getElementById("d_buildings_12").value+"&d_buildings_13="+document.getElementById("d_buildings_13").value+"&d_buildings_14="+document.getElementById("d_buildings_14").value+"&d_buildings_15="+document.getElementById("d_buildings_15").value+"&d_buildings_16="+document.getElementById("d_buildings_16").value+"&d_buildings_17="+document.getElementById("d_buildings_17").value+"&d_buildings_18="+document.getElementById("d_buildings_18").value+"&d_buildings_19="+document.getElementById("d_buildings_19").value+"&d_buildings_20="+document.getElementById("d_buildings_20").value+"&d_buildings_21="+document.getElementById("d_buildings_21").value+"&d_buildings_22="+document.getElementById("d_buildings_22").value;
 vars+="&d_resources_0="+document.getElementById("d_resources_0").value+"&d_resources_1="+document.getElementById("d_resources_1").value+"&d_resources_2="+document.getElementById("d_resources_2").value+"&d_resources_3="+document.getElementById("d_resources_3").value+"&d_resources_4="+document.getElementById("d_resources_4").value+"&d_resources_5="+document.getElementById("d_resources_5").value+"&d_resources_6="+document.getElementById("d_resources_6").value;
 vars+="&d_wall_w="+document.getElementById("d_wall_w").value;
 //attacker data
 vars+="&type="+document.getElementById("type").value+"&a_faction="+document.getElementById("a_faction").value;
 vars+="&a_army_0="+document.getElementById("a_army_0").value+"&a_army_1="+document.getElementById("a_army_1").value+"&a_army_2="+document.getElementById("a_army_2").value+"&a_army_3="+document.getElementById("a_army_3").value+"&a_army_4="+document.getElementById("a_army_4").value+"&a_army_5="+document.getElementById("a_army_5").value+"&a_army_6="+document.getElementById("a_army_6").value+"&a_army_7="+document.getElementById("a_army_7").value+"&a_army_8="+document.getElementById("a_army_8").value+"&a_army_9="+document.getElementById("a_army_9").value+"&a_army_10="+document.getElementById("a_army_10").value+"&a_army_11="+document.getElementById("a_army_11").value;
 vars+="&a_hero_0="+document.getElementById("a_hero_0").value+"&a_hero_1="+document.getElementById("a_hero_1").value+"&a_hero_2="+document.getElementById("a_hero_2").value+"&a_hero_3="+document.getElementById("a_hero_3").value+"&a_hero_4="+document.getElementById("a_hero_4").value+"&a_hero_5="+document.getElementById("a_hero_5").value;
 vars+="&a_hero_talents_0="+document.getElementById("a_hero_talents_0").value+"&a_hero_talents_1="+document.getElementById("a_hero_talents_1").value+"&a_hero_talents_2="+document.getElementById("a_hero_talents_2").value+"&a_hero_talents_3="+document.getElementById("a_hero_talents_3").value+"&a_hero_talents_4="+document.getElementById("a_hero_talents_4").value+"&a_hero_talents_5="+document.getElementById("a_hero_talents_5").value+"&a_hero_talents_6="+document.getElementById("a_hero_talents_6").value;
 vars+="&a_hero_active_spells="+document.getElementById("a_hero_active_spells").value;
 vars+="&a_tech_0="+document.getElementById("a_tech_0").value+"&a_tech_1="+document.getElementById("a_tech_1").value+"&a_tech_2="+document.getElementById("a_tech_2").value+"&a_tech_3="+document.getElementById("a_tech_3").value+"&a_tech_4="+document.getElementById("a_tech_4").value+"&a_tech_5="+document.getElementById("a_tech_5").value+"&a_tech_6="+document.getElementById("a_tech_6").value+"&a_tech_7="+document.getElementById("a_tech_7").value+"&a_tech_8="+document.getElementById("a_tech_8").value;
 template("csim.php?town="+town, vars);
}
function mission(town)
{
 vars="q="+document.getElementById("q").value+"&type="+document.getElementById("type").value+"&target="+document.getElementById("target").value;
 template("missions.php?town="+town, vars);
}
function bonuses(a)
{
 vars="hero="+document.getElementById("hero").value+"&q="+document.getElementById("q").value;
 template("bonuses.php?a="+a, vars);
}
function stats_rank(town, t)
{
 rank=document.getElementById("rank").value; rank--;
 vars="rank="+rank;
 template("stats.php?town="+town+"&t="+t, vars);
}
function stats_name(town, t)
{
 vars="name="+document.getElementById("name").value;
 template("stats.php?town="+town+"&t="+t, vars);
}
function desc(town, player, population, alliance)
{
	document.getElementById("descriptor").innerHTML="<table class='q_table_desc' style='border-collapse: collapse' width='250' border='1'><tr><td colspan='2' align='center'>"+town+"</tr><tr><td width='117' align='center'><b>Player</b><td>"+player+"</td></tr><tr><td width='117' align='center'><b>Population</b><td>"+population+"</td></tr><tr><td width='117' align='center'><b>Alliance</b><td>"+alliance+"</td></tr></table>";
}
function map()
{
 var vars="x="+document.getElementById("x").value+"&y="+document.getElementById("y").value;
 template('map.php', vars);
}
function update_town(town)
{
 vars="name="+document.getElementById("name").value+"&desc="+document.getElementById("desc").value;
 template("town_edit_.php?town="+town, vars);
}