var disappeardelay=250;
var enableanchorlink=0; 
var hidemenu_onclick=1; 
var ie5=document.all;
var ns6=document.getElementById&&!document.all;
var ghost_var = "";
var which_tab = "";

function ajaxContent(address,divID) {
  var xmlHttp;
  // Firefox, Opera 8.0+, Safari, SeaMonkey
  try {    
    xmlHttp=new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
    {
      try
      {
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      }
      catch (e) {}
    }
  }

  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
      // initiate server access
      xmlHttp.open("GET", address, true);
      xmlHttp.send(null);
    }
    // display the error in case of failure
    catch (e)
    {
      alert("ERROR: Cannot connect to server:\n" + e.toString());
    }
  }
  xmlHttp.onreadystatechange=function() {
    //The request is complete == state 4
    if (xmlHttp.readyState==4 && xmlHttp.status==200) {
	document.getElementById(divID).innerHTML=xmlHttp.responseText;
    }
  }
  
}

function addP() {
	try {
		window.external.AddSearchProvider('http://c-sro.com/search.xml');
	}
	catch(e) {
		alert('Search engine integration requires either Firefox 2 or Internet Explorer 7.');
	}
}

function addD() {
	try {
		window.external.AddSearchProvider('http://c-sro.com/direct.xml');
	}
	catch(e) {
		alert('Search engine integration requires either Firefox 2 or Internet Explorer 7.');
	}
}


function filtergear(z, r, s) {
  xx = document._gear._filter[document._gear._filter.selectedIndex].value;
  window.location.href = '/score/' + z + '/' + r + '/' + xx;
}

function filterstats(z, r, s) {
  xx = document._stats._filter[document._stats._filter.selectedIndex].value;
  window.location.href = '/stats/' + z + '/' + r + '/' + xx;
}

function filterbrowse(url) {
  window.location.href = url + document._browse._sort[document._browse._sort.selectedIndex].value;
}

function StripParam(url, param) {
  var start = url.indexOf(param);
  if (start == -1) return url;
  var end = start + param.length;

  var charBefore = url.charAt(start-1);
  if (charBefore != '?' && charBefore != '&') return url;

  var charAfter = (url.length >= end+1) ? url.charAt(end) : '';
  if (charAfter != '' && charAfter != '&' && charAfter != '#') return url;
  return url.substring(0, start-1) + url.substring(end);
}

function switchlang(lang) {
  var url = document.URL;
  url = StripParam(url, "lang=1");
  url = StripParam(url, "lang=0");
  var add_param = url.indexOf("?");
  if(add_param == -1) {
    window.location.href = url + '?lang=' + lang;
  } else {
    window.location.href = url + '&lang=' + lang;
  }
}

function togmenu() {
  if (document.getElementById('my_block').style.visibility == 'visible') {
    document.getElementById('darkBackgroundLayer').style.visibility = 'hidden';   
    document.getElementById('darkBackgroundLayer').style.display = 'none';

    document.getElementById('my_block').style.visibility = 'hidden';   
    document.getElementById('my_block').style.display = 'none';
    document.getElementById(which_tab).className = 'box_on';
    document.getElementById('my_nav').className = 'my_off';
  } else {
    document.getElementById('darkBackgroundLayer').style.visibility = 'visible';   
    document.getElementById('darkBackgroundLayer').style.display = '';

    document.getElementById('my_block').style.visibility = 'visible';   
    document.getElementById('my_block').style.display = '';
    which_tab = find_nav();
    clear_nav();
    document.getElementById('my_nav').className = 'my_on';
  }
}

function mnu_on(i) {
  document.getElementById(i).className = 'my_ele_on';
}

function mnu_off(i) {
  document.getElementById(i).className = 'my_ele';
}

function forumjump() {
  xi = document._forum._jump[document._forum._jump.selectedIndex].value;
  window.location.href = '/forum/?t=' + xi;
}

function stat_show(iid,hid) {
  if (document.getElementById(iid).style.visibility == 'visible') {
    document.getElementById(iid).style.visibility = 'hidden';   
    document.getElementById(iid).style.display = 'none';
    document.getElementById(hid).className = 'stats_head_off';   
  } else {
    document.getElementById(iid).style.visibility = 'visible';   
    document.getElementById(iid).style.display = '';
    document.getElementById(hid).className = 'stats_head_on';   
  }
}

function menu_swap(iid,hid) {
  if (document.getElementById(iid).style.display == '') {
    document.getElementById(iid).style.visibility = 'hidden';   
    document.getElementById(iid).style.display = 'none';
    document.getElementById(hid).className = 'menu_header_off';   
  } else {
    document.getElementById(iid).style.visibility = 'visible';   
    document.getElementById(iid).style.display = '';
    document.getElementById(hid).className = 'menu_header';   
  }
}

function ghost_txt_out (fld, def, id) {
	if (fld.value == def) {
		fld.value = def;
	}
	if (fld.value == "") {
		if (ghost_var == def) {
			fld.value = def;
		} else {
			fld.value = ghost_var;
			ghost_var = "";
		}
	}
	fld.id = id;
}

function ghost_txt_inc (fld, def, id) {
	ghost_var = fld.value;
	fld.value = "";
	fld.id = id;
}

function do_csro_build(pag) {
  if (pag == "server") {
    xx = document._csro._server[document._csro._server.selectedIndex].value;
    cc = xx.split("_");
    document._csro._servername.value = cc[0];
    document._csro._name.value = "Character Name";
    document.getElementById("csro_link").innerHTML = "<a href='http://" + location.host + "/player.php?server=" + document._csro._servername.value.toLowerCase() + "&name=" + document._csro._name.value.toLowerCase() + "'>http://c-sro.com/" + document._csro._servername.value.toLowerCase() + "/" + document._csro._name.value.toLowerCase() + "</a>";
  }
  if (pag == "user") {
    document.getElementById("csro_link").innerHTML = "<a href='http://" + location.host + "/player.php?server=" + document._csro._servername.value.toLowerCase() + "&name=" + document._csro._name.value.toLowerCase() + "'>http://c-sro.com/" + document._csro._servername.value.toLowerCase() + "/" + document._csro._name.value.toLowerCase() + "</a>";
  }
}

function getposOffset(what, offsettype) {
  var totaloffset=(offsettype=="left")? what.offsetLeft+5 : what.offsetTop-10;
  var parentEl=what.offsetParent;
  while (parentEl!=null) {
    totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
    parentEl=parentEl.offsetParent;
  }
  return totaloffset;
}

function showhide(obj, e, visible, hidden) {
  if (ie5||ns6) {
    dropmenuobj.style.left=dropmenuobj.style.top=-500;
  }
  if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover") {
    obj.visibility=visible;
  } else if (e.type=="click") {
    obj.visibility=hidden;
  }
}

function iecompattest() {
  return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function clearbrowseredge(obj, whichedge) {
  var edgeoffset=0;
  if (whichedge=="rightedge") {
    var windowedge=ie5 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15;
    dropmenuobj.contentmeasure=dropmenuobj.offsetWidth;
    if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure) {
      edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth;
    }
  } else {
    var topedge=ie5 && !window.opera? iecompattest().scrollTop : window.pageYOffset;
    var windowedge=ie5 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18;
    dropmenuobj.contentmeasure=dropmenuobj.offsetHeight;
    if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure) {
      edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight;
      if ((dropmenuobj.y-topedge)<dropmenuobj.contentmeasure) {
        edgeoffset=dropmenuobj.y+obj.offsetHeight-topedge;
      }
    }
  }
  return edgeoffset;
}

function dropdownmenu(obj, e, dropmenuID) {
  if (window.event) {
    event.cancelBubble=true;
  } else if (e.stopPropagation) {
    e.stopPropagation();
  }
  
  if (typeof dropmenuobj!="undefined") {
    dropmenuobj.style.visibility="hidden";
  }

  clearhidemenu();
  
  if (ie5||ns6) {
    obj.onmouseout=delayhidemenu;
    dropmenuobj=document.getElementById(dropmenuID);
    
    if (hidemenu_onclick) {
      dropmenuobj.onclick=function(){dropmenuobj.style.visibility='hidden'};
    }
    dropmenuobj.onmouseover=clearhidemenu;
    dropmenuobj.onmouseout=ie5? function(){ dynamichide(event)} : function(event){ dynamichide(event)};
    showhide(dropmenuobj.style, e, "visible", "hidden");
    dropmenuobj.x=getposOffset(obj, "left");
    dropmenuobj.y=getposOffset(obj, "top");
    dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px";
    dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px";
  }
  return clickreturnvalue();
}

function clickreturnvalue() {
  if ((ie5||ns6) && !enableanchorlink) {
      return false;
  } else {
    return true;
  }
}

function contains_ns6(a, b) {
  while (b.parentNode) {
    if ((b = b.parentNode) == a) {
      return true;
    }
  }
  return false;
}

function dynamichide(e) {
  if (ie5&&!dropmenuobj.contains(e.toElement)) {
    delayhidemenu();
  }
  else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget)) {
    delayhidemenu();
  }
}

function delayhidemenu() {
  delayhide=setTimeout("dropmenuobj.style.visibility='hidden'",disappeardelay);
}

function clearhidemenu() {
  if (typeof delayhide!="undefined") {
    clearTimeout(delayhide);
  }
}

function el(id) {
  if (document.getElementById) {
  return document.getElementById(id);
  } else if (window[id]) {
  return window[id];
  }
  return null;
}
// Linear expolation ftw.
var CP = [
 [ 1199433600000, 1000 ],
 [ 1224486000000, 1000000 ],
 [ 2144908800000, 1000000000 ],
 [ 2147328000000, 1000000000000 ],
 [ 46893711600000, Number.MAX_VALUE ]
];
var pcount;
function updatePcount() {
  if (!pcount) {
  return;
  }
  var now = (new Date()).getTime();
  var i;
  for (i = 0; i < CP.length; i++) {
    if (now < CP[i][0]) {
      break;
    }
  }
  if (i == 0) {
    setTimeout(updatePcount, 1000); 
  } else if (i == CP.length) {
    pcount.innerHTML = CP[i - 1][1];
  } else {
    var ts = CP[i - 1][0];
    var bs = CP[i - 1][1];
    pcount.innerHTML = format(((now-ts) / (CP[i][0]-ts) * (CP[i][1]-bs)) + bs); 
    setTimeout(updatePcount, 1000); 
  } 
} 
 
var PAD = ''; 
 
function format(num) { 
  var str = String(num); 
  var dot = str.indexOf('.'); 
  if (dot < 0) { 
     return str + PAD; 
  } if (PAD.length > (str.length - dot)) {
  return str + PAD.substring(str.length - dot);
  } else {
  return str.substring(0, dot + PAD.length);
  }
}
