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 (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;
 }
}
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 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);
}
//############################################
function go(lnk)
{
	window.location.href=lnk;
}

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'>Player<td>"+player+"</td></tr><tr><td width='117' align='center'>Population<td>"+population+"</td></tr><tr><td width='117' align='center'>Alliance<td>"+alliance+"</td></tr></table>";
}

function timer(data, lnk)
{
	dat=document.getElementById(data);
	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[data]); window.location.href=lnk; done=1;}
		}
	}
	if (!done)
	{
		h=''+time[0];
		m=''+time[1];
		s=''+time[2];
		if(m.length==1) m='0'+m;
		if(s.length==1) s='0'+s;
		if(h.length==1) h='0'+h;
		dat.innerHTML=h+":"+m+":"+s;
		id[data]=setTimeout("timer('"+data+"', '"+lnk+"')", 1000);
	}
}


function timeRun(elemId) {
  now5=new Date();

  addH = "00";
  addM = "00";
  addS = "01";
  
 id=document.getElementById(elemId);
 var time=(id.innerHTML).split(":");
  
  hour3 = parseInt(addH) + parseInt(time[0]);
  min3 = parseInt(addM)  + parseInt(time[1]);
  sec3 = parseInt(addS)  + parseInt(time[2]);

  if (sec3>=60) { sec3 = -(60 - sec3); min3 = parseInt(min3)+1; }
  if (min3>=60) { min3 = -(60 - min3); hour3 = parseInt(hour3)+1; }
  if (hour3>=24) { hour3 = -(24 - hour3); }
  if (sec3<=9) { sec3="0"+sec3; }
  if (min3<=9) { min3="0"+min3; }
  if (hour3<=9) { hour3="0"+hour3; }
  if ((addS>59) || (addM>59) || (addH>23)) { window.alert('Invalid Input!!'); return false; }
  if ((addS.length > 2) || (addM.length > 2) || (addH.length > 2)) { window.alert('Invalid Input!!'); return false; }
  if ((addS.indexOf('-') != -1) || (addM.indexOf('-') != -1) || (addH.indexOf('-') != -1)) { window.alert('Invalid Input!!'); return false; }

  id.innerHTML=hour3+":"+min3+":"+sec3;
  setTimeout("timeRun('"+elemId+"')", 1000);
}

function timeEval(elemId){

 today=new Date();

 id=document.getElementById(elemId);
 var time=(id.innerHTML);

  t=time.split(":");

  addH =  t[0];
  addM =  t[1];
  addS =  t[2];

  hour3 = parseInt(addH)  + parseInt(today.getHours());
  min3  = parseInt(addM)  + parseInt(today.getMinutes());
  sec3  = parseInt(addS)  + parseInt(today.getSeconds());

  if (sec3>=60) { sec3 = -(60 - sec3); min3 = parseInt(min3)+1; }
  if (min3>=60) { min3 = -(60 - min3); hour3 = parseInt(hour3)+1; }
  if (hour3>=24) { hour3 = -(24 - hour3); }
  if (sec3<=9) { sec3="0"+sec3; }
  if (min3<=9) { min3="0"+min3; }
  if (hour3<=9) { hour3="0"+hour3; }
  
  //if ((addS>59) || (addM>59) || (addH>23)) { window.alert('Invalid Input!!'); return false; }
  //if ((addS.length > 2) || (addM.length > 2) || (addH.length > 2)) { window.alert('Invalid Input!!'); return false; }
  //if ((addS.indexOf('-') != -1) || (addM.indexOf('-') != -1) || (addH.indexOf('-') != -1)) { window.alert('Invalid Input!!'); return false; }

 id.innerHTML=hour3+":"+min3+":"+sec3;
  setTimeout("timeRun('"+elemId+"')", 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>";
}

function showtext(building)
{
	if (!document.getElementById)
	return;
        textcontainerobj=document.getElementById("label");
        document.getElementById("label").innerHTML=building;
}

function startres()
{
		res_sec0=res_ph0/3600;
        res_sec1=res_ph1/3600;
        res_sec2=res_ph2/3600;
        res_sec3=res_ph3/3600;
        //res_sec4=res_ph4/3600;

        res_start0=res_start0+res_sec0; if (res_start0 > res_limit0) res_start0=res_limit0;
        res_start1=res_start1+res_sec1; if (res_start1 > res_limit1) res_start1=res_limit1;
        res_start2=res_start2+res_sec2; if (res_start2 > res_limit2) res_start2=res_limit2;
        res_start3=res_start3+res_sec3; if (res_start3 > res_limit3) res_start3=res_limit3;
        //res_start4=res_start4+res_sec4; if (res_start4 > res_limit4) res_start4=res_limit4;

        res_show0=Math.round(res_start0);
        res_show1=Math.round(res_start1);
        res_show2=Math.round(res_start2);
        res_show3=Math.round(res_start3);
        //res_show4=Math.round(res_start4);

        document.getElementById("res0").innerHTML=res_show0;
        document.getElementById("res1").innerHTML=res_show1;
        document.getElementById("res2").innerHTML=res_show2;
        document.getElementById("res3").innerHTML=res_show3;
        
		
		//document.getElementById("res4").innerHTML=res_show4;

        setTimeout('startres()',1000);
}

function inittabs() {

	// Grab the tab links and content divs from the page
	var tabListItems = document.getElementById('tabs').childNodes;
	for ( var i = 0; i < tabListItems.length; i++ ) {
	    if ( tabListItems[i].nodeName == "LI" ) {
        	var tabLink = getFirstChildWithTagName( tabListItems[i], 'A' );
        	var id = getHash( tabLink.getAttribute('href') );
        	tabLinks[id] = tabLink;
        	contentDivs[id] = document.getElementById( id );
	    }
	}

	// Assign onclick events to the tab links, and
	// highlight the first tab
	var i = 0;

	for ( var id in tabLinks ) {
	    tabLinks[id].onclick = showTab;
	    tabLinks[id].onfocus = function() { this.blur() };
	    if ( i == 0 ) tabLinks[id].className = 'selected';
	    i++;
	}

	// Hide all content divs except the first
	var i = 0;

	for ( var id in contentDivs ) {
	    if ( i != 0 ) contentDivs[id].className = 'tabContent hide';
	    i++;
        }
}

function showTab() {
	var selectedId = getHash( this.getAttribute('href') );

	// Highlight the selected tab, and dim all others.
	// Also show the selected content div, and hide all others.
	for ( var id in contentDivs ) {
	    if ( id == selectedId ) {
        	tabLinks[id].className = 'selected';
        	contentDivs[id].className = 'tabContent';
	    } else {
        	tabLinks[id].className = '';
        	contentDivs[id].className = 'tabContent hide';
	    }
	}

	// Stop the browser following the link
	return false;
}

function getFirstChildWithTagName( element, tagName ) {
	for ( var i = 0; i < element.childNodes.length; i++ ) {
	    if ( element.childNodes[i].nodeName == tagName ) return element.childNodes[i];
	}
}

function getHash( url ) {
	var hashPos = url.lastIndexOf ( '#' );
	return url.substring( hashPos + 1 );
}
//function map()
//{
// //alert(document.getElementById("x").value+" @@@ "+document.getElementById("y").value);
// var vars="x="+document.getElementById("x").value+"&y="+document.getElementById("y").value;
// template('view_.php', vars);
//}


function map()
{
 //alert(document.getElementById("x").value+" @@@ "+document.getElementById("y").value);
 var vars="x="+document.getElementById("x").value+"&y="+document.getElementById("y").value;
 location.replace('view.php?x='+document.getElementById("x").value+'&y='+document.getElementById("y").value);
}

Array.prototype.max = function() {
var max = this[0];
var len = this.length;
for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];
return max;
}
Array.prototype.min = function() {
var min = this[0];
var len = this.length;
for (var i = 1; i < len; i++) if (this[i] < min) min = this[i];
return min;
}

function fillLimit(resId,value){
		document.getElementById(resId).value=Math.round(value);
		document.getElementById(resId).focus();
}

function validateLimit(minVal, maxVal, id){
	elemVal = document.getElementById(id).value;
	if((elemVal>maxVal)||(elemVal<minVal)){
		alert( " please Verify your input | max allowed : " + maxVal);
		return false;
	}
}




function createXHR()
{
 var request = false;
 
	 try {
		 request = new ActiveXObject('Msxml2.XMLHTTP');
	 }

	 catch (err2) {
	 
		 try {
			request = new ActiveXObject('Microsoft.XMLHTTP');
		 }
		 catch (err3) {
		 
			 try {
				request = new XMLHttpRequest();
			 }
			 catch (err1)
			 {
				request = false;
			 }
		 }
 	}
 return request;
}

 function showDatassss(x,y){

	 	var xhr=createXHR();

 			 url = 'getdatamap.php?x='+x+'&y='+y;
			
			 xhr.open("GET", url,true);
			 xhr.onreadystatechange=function()
			 {
				if (xhr.readyState == 4)
				 {
					 if (xhr.status != 404)
						{
								
						 var data=eval("(" + xhr.responseText + ")");
					     document.getElementById('datafrom').innerHTML=data;
						}

				}
			 }
			xhr.send(null);
 
 }	

function showData(x,y)
{
//var url="getdatamap.php";
//url=url+"?x="+x;
//url=url+"&y="+y;
//url=url+"&sid="+Math.random();
//
//if (window.XMLHttpRequest)
//  {// code for IE7+, Firefox, Chrome, Opera, Safari
//  xmlhttp=new XMLHttpRequest();
//  }
//else
//  {// code for IE6, IE5
//  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
//  }
//xmlhttp.onreadystatechange=function()
//  {
//  if (xmlhttp.readyState==4 && xmlhttp.status==200)
//    {
//    document.getElementById("datafrom").innerHTML=xmlhttp.responseText;
//    }
//  }
//xmlhttp.open("GET",url,true);
//xmlhttp.send();
}

function quickInfo(x,y,alliance,name,population,madina,distance,lastAattack)
{
	$("#popupUsername").html(name);
	$("#popupAlliance").html(alliance);
	$("#popupPopulation").html(population);
	$("#madina").html(madina);
	$("#popupDistance").html(distance);
	$("#lastAttack").html(lastAattack);
}

function wahaInfo(info,name)
{
	$("#popupUsername").html(name);
	$("#popupAlliance").html("-");
	$("#popupPopulation").html("-");
	$("#lastAttack").html("-");
	$("#madina").html(info);
}


/* Cross-Browser Split 1.0.1
(c) Steven Levithan <stevenlevithan.com>; MIT License
An ECMA-compliant, uniform cross-browser split method */

var cbSplit;

// avoid running twice, which would break `cbSplit._nativeSplit`'s reference to the native `split`
if (!cbSplit) {

cbSplit = function (str, separator, limit) {
    // if `separator` is not a regex, use the native `split`
    if (Object.prototype.toString.call(separator) !== "[object RegExp]") {
        return cbSplit._nativeSplit.call(str, separator, limit);
    }

    var output = [],
        lastLastIndex = 0,
        flags = (separator.ignoreCase ? "i" : "") +
                (separator.multiline  ? "m" : "") +
                (separator.sticky     ? "y" : ""),
        separator = RegExp(separator.source, flags + "g"), // make `global` and avoid `lastIndex` issues by working with a copy
        separator2, match, lastIndex, lastLength;

    str = str + ""; // type conversion
    if (!cbSplit._compliantExecNpcg) {
        separator2 = RegExp("^" + separator.source + "$(?!\\s)", flags); // doesn't need /g or /y, but they don't hurt
    }

    /* behavior for `limit`: if it's...
    - `undefined`: no limit.
    - `NaN` or zero: return an empty array.
    - a positive number: use `Math.floor(limit)`.
    - a negative number: no limit.
    - other: type-convert, then use the above rules. */
    if (limit === undefined || +limit < 0) {
        limit = Infinity;
    } else {
        limit = Math.floor(+limit);
        if (!limit) {
            return [];
        }
    }

    while (match = separator.exec(str)) {
        lastIndex = match.index + match[0].length; // `separator.lastIndex` is not reliable cross-browser

        if (lastIndex > lastLastIndex) {
            output.push(str.slice(lastLastIndex, match.index));

            // fix browsers whose `exec` methods don't consistently return `undefined` for nonparticipating capturing groups
            if (!cbSplit._compliantExecNpcg && match.length > 1) {
                match[0].replace(separator2, function () {
                    for (var i = 1; i < arguments.length - 2; i++) {
                        if (arguments[i] === undefined) {
                            match[i] = undefined;
                        }
                    }
                });
            }

            if (match.length > 1 && match.index < str.length) {
                Array.prototype.push.apply(output, match.slice(1));
            }

            lastLength = match[0].length;
            lastLastIndex = lastIndex;

            if (output.length >= limit) {
                break;
            }
        }

        if (separator.lastIndex === match.index) {
            separator.lastIndex++; // avoid an infinite loop
        }
    }

    if (lastLastIndex === str.length) {
        if (lastLength || !separator.test("")) {
            output.push("");
        }
    } else {
        output.push(str.slice(lastLastIndex));
    }

    return output.length > limit ? output.slice(0, limit) : output;
};

cbSplit._compliantExecNpcg = /()??/.exec("")[1] === undefined; // NPCG: nonparticipating capturing group
cbSplit._nativeSplit = String.prototype.split;

} // end `if (!cbSplit)`

// for convenience...
String.prototype.split = function (separator, limit) {
    return cbSplit(this, separator, limit);
};


