// *** This function comes from easyMoblog (http://easymoblog.com) ***
function open_popup (url, w, h) {
   var top = calc_top (h);
   var left = calc_left (w);

   popup = window.open (url,'','scrollbars=yes,width='+w+',height='+h+',left='+left+',top='+top+',screenX='+left+',screenY='+top);
   popup.focus();
}

function no_spam (user,domain) {
  locationstring = "mailto:" + user + "@" + domain;
  window.location = locationstring;
}

function isMicrosoft(v) {
	// *** From: http://www.rgagnon.com/jsdetails/js-0039.html ***

	/*
	** Check if the browser is Microsoft Internet Explorer compatible
	**    v  version number
	** returns  true if MSIE and version equals or greater
	*/
	return isBrowser("Microsoft", v);
}

function isBrowser(b,v) {
	// *** From: http://www.rgagnon.com/jsdetails/js-0039.html ***

	/*
	** Check if the current browser is compatible
	**  b  browser name
	**  v  version number (if 0 don't check version)
	** returns true if browser equals and version equals or greater
	*/
	browserOk = false;
	versionOk = false;

	browserOk = (navigator.appName.indexOf(b) != -1);
	if (v == 0) versionOk = true;
	else  versionOk = (v <= parseInt(navigator.appVersion));
	return browserOk && versionOk;
}

var whos_open;

function do_whos(ie)
{
	// *** This works in IE, but IE incorrectly positions the <div>.  Dumb thing. ***
	whoss = document.getElementById("whos");
	scratch = document.getElementById("scratchpad");

	// if (isMicrosoft("0.0") && (ie == 1))
	// return false;
	
	if (whos_open == true)
	{
		whoss.style.display = "none";
		whos_open = false;
		scratch.style.width = "992px";
	} else {
		whoss.style.display = "block";
		whos_open = true;
		scratch.style.width = "778px";
	}	
}

var scratch_open;

function do_scratch(ie)
{
	// *** This works in IE, but IE incorrectly positions the <div>.  Dumb thing. ***
	scratch = document.getElementById("ul_scratches");

	// if (isMicrosoft("0.0") && (ie == 1))
	// return false;
	
	if (scratch_open == true)
	{
		scratch.style.display = "none";
		scratch_open = false;
	} else {
		scratch.style.display = "block";
		scratch_open = true;
	}
}

var stats_open;
var got_stats;
function do_stats()
{
	// *** This works in IE, but IE incorrectly positions the <div>.  Dumb thing. ***
	stats1 = document.getElementById("ul_stats");
	scratch = document.getElementById("scratchpad");
	
	if (isMicrosoft("0.0"))
	{
		alert("Sorry, this doesn't work in IE.  So far only firefox.  Switch to a *real* browser!");
		return;
	}


	if (!got_stats)
	{
		var W3CDOM = (document.getElementsByTagName && document.createElement);
		if (!W3CDOM)
		{
			alert("Sorry, your browser does not support the W3C DOM Standard." + 
			"I suggest updating your browser.  I'm working on support for this!");
			return;
		}
			
		// *** The setup for this is from http://jibbering.com/2002/4/httprequest.html ***
		var xmlhttp = getHTTPObject();

		var posts = "?ajax,stats";

		xmlhttp.open("POST", "index.php" + posts, true);
		xmlhttp.send(null);
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState == 4) 
				writeit(xmlhttp.responseText, "stats_more");
		}
		
		got_stats = 1;
	}
	
	if (stats_open == true)
	{
		stats1.style.display = "none";
		stats_open = false;
		scratch.style.display = "block";
	} else {
		stats1.style.display = "block";
		stats_open = true;
		scratch.style.display = "none";
	}
}

function hide_child(id, hidden)
{
	// *** a nifty little function I made.  Just name a div foo and its child foo-child ***
	// *** foo-child will then be hidden/shown by clicking on foo ***
	if (document.getElementById)
	{
		var child = document.getElementById(id + "-child");
		var parent = document.getElementById(id);
	}
	else if (document.all)
	{
		var child = document.all[id + "-child"];
		var parent = document.all[id];
	}

	if (child.style.display == "")
		child.style.display = (hidden) ? "none" : "block";
		
	if (child.style.display == "block")
	{
		child.style.display = "none";
	} else {
		child.style.display = "block";
	}

}

// *** AJAX!! XMLHTTPREQUEST!! this stuff rules ***

function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}


function send_scratch()
{
	var W3CDOM = (document.getElementsByTagName && document.createElement);
	if (!W3CDOM)
	{
		alert("Sorry, your browser does not support the W3C DOM Standard.  I suggest updating your browser.  I'm working on support for this!");
		return;
	}
	// *** The setup for this is from http://jibbering.com/2002/4/httprequest.html ***
	var xmlhttp = getHTTPObject();
            var xmlhttp2 = getHTTPObject();

	
      
	var scratchname = encodeURIComponent(escape(document.getElementById("scratch_name").value));
	var scratchurl = encodeURIComponent(escape(document.getElementById("scratch_url").value));
	var scratchbody = encodeURIComponent(escape(document.getElementById("scratch_body").value));
	var scratchcaptcha = encodeURIComponent(escape(document.getElementById("scratch_captcha").value));
	var posts = "?ajax,scratch_post," + scratchname + "," + scratchbody + "," + scratchurl + "," + scratchcaptcha;
	
      document.getElementById("scratch_submit").disabled = true;
      document.getElementById("scratch_submit").value = "Posting... Please Wait!";
      document.getElementById("scratch_name").value = "";
      document.getElementById("scratch_url").value = "";
      document.getElementById("scratch_body").value = "";
      document.getElementById("scratch_captcha").value = "";
   
	xmlhttp.open("POST", "index.php" + posts, true);
	xmlhttp.send(null);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
               writeit(xmlhttp.responseText, "scratch_more");
               document.getElementById("scratch_submit").disabled = false;
               document.getElementById("scratch_submit").value = "Ajax it!";
		}
	}
	
	// *** Do it again, this time update the preview scratch ***
	xmlhttp2 = getHTTPObject();
	xmlhttp2.open("GET", "index.php?ajax,scratch_update", true);
	xmlhttp2.send(null);
	xmlhttp2.onreadystatechange = function() {
		if (xmlhttp2.readyState == 4) {
               writeit(xmlhttp2.responseText, "li_scratchpad");
               document.getElementById("scratch_submit").disabled = false;
               document.getElementById("scratch_submit").value = "Ajax it!";
		}
	}
}

function writeit(text,id)
{
	// *** From http://www.quirksmode.org/js/layerwrite.html ***
	if (document.getElementById)
	{
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = text;
	}
	else if (document.all)
	{
		x = document.all[id];
		x.innerHTML = text;
	}
	else if (document.layers)
	{
		alert("Sorry, Netscape 4 is not supported");
	}
}