var q_array = ['who', 'what', 'when', 'where', 'why', 'how'];


function occur(which, one) {
  if ((which == 'sub') || (which == 'sur') || (which == 'suc')) {
	var hotbox = document.getElementById('peephole');
	if ((hotbox.style.opacity * 100) == 0) {
	  if (which == 'sub') {flipdivs(which, 'sur');}
	  else {flipdivs(which, 'sub');}
	  fademe('peephole', 90);
	}
	else {
	  if (which == 'sub') {flipdivs(which, 'sur');}
	  if (which == 'sur') {flipdivs(which, 'sub');}
	  if (which == 'suc') {fademe('peephole', 0);}
	}
	document.getElementById('knockback').innerHTML = '';
  }
  else if (which == 'news') {
	var thisDiv = document.getElementById(which).style;
	if (thisDiv.display == "block") {thisDiv.display = "none";}
	else {thisDiv.display = "block";}
  }
  else if (which == 'culling') {
	var hotbox = document.getElementById(which + ':' + one);
	if (hotbox.childNodes.length > 0) {hotbox.innerHTML = '';}
	else {
	  var shuf = false;
	  if (window.XMLHttpRequest) {shuf = new XMLHttpRequest();}
	  else if (window.ActiveXObject) {
		shuf = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	  else {alert('Your browser does not support Ajax. Please upgrade!');}
	  if (shuf) {
		shuf.open("GET", 'functs.php?dowhat=culling&dale=' + one, true);
		shuf.onreadystatechange = function() {
		  if ((shuf.readyState == 4) && (shuf.status == 200)) {
			hotbox.innerHTML = shuf.responseText;
		  }
		}
		shuf.send(null);
	  }
	  else {alert('Your browser does not support Ajax. Please upgrade!');}
	}
  }
  else if (which == 'comments') {
	thisMenu = document.getElementById('comments_'+one).style;
	if (thisMenu.display == "block") {thisMenu.display = "none";}
	else {thisMenu.display = "block";}
  }
  else {
	var hotbox = document.getElementById(which + '_head');
	if (hotbox.style.width == '') {hotbox.style.width = '16%';}
	if (hotbox.style.width == '16%') {graw(which + '_head', 0, 500);}
	else {graw(which + '_head', 16, 500);}
  }
}


function graw(which, to, howlong) {
  var hur = document.getElementById(which);
  var orig = hur.style.width;
  //  alert(orig);
  from = orig.match(/[0-9]+/);
  var speed = Math.round(howlong / 100);
  var timer = 0;
  if (to < from) {
	//	alert('to: ' + to + ' and from: ' + from + ' in: ' + which);
    for (a = from; a >= to; a--) {
      n = a + '%';
	  if (a == '1') {setTimeout("move('"+which+"', '"+n+"', 'y')",(timer * speed));}
	  else {setTimeout("move('"+which+"', '"+n+"')",(timer * speed));}
      timer++;
	  //	  if (n == '0%') {hur.style.display = "none";}
    }
  }
  if (to > from) {
	//	alert('to: ' + to + ' and from: ' + from + ' in: ' + which);
    for (a = from; a <= to; a++) {
      n = a + '%';
	  if (a == '1') {setTimeout("move('"+which+"', '"+n+"', 'y')",(timer * speed));}
	  else {setTimeout("move('"+which+"', '"+n+"')",(timer * speed));}
      timer++;
    }
  }
}


function move(that, there, tog) {
  var box = document.getElementById(that).style;
  //  var show = box.display;
  box.width = there;
  if (tog == 'y') {
	//	alert(show);
	if (show == 'inline') {box.display = 'none'; alert('hiding!');}
	if (show == 'none') {box.display = 'inline'; alert('showing!');}
  }
}


function common(on, which) {
  if (comment == '') {alert("Will not record an empty comment!"); exit;}

  var hotbox = document.getElementById("noteslot_"+on+':'+which);
  var comment = document.getElementById("comment_"+on+':'+which).value;
  var poststr = 'newcom=yes&on='+on+'&which='+which+'&comment='+escape(encodeURI(comment));
  //   	alert(poststr);
  var htob = false;
  if (window.XMLHttpRequest) {htob = new XMLHttpRequest();}
  else if (window.ActiveXObject) {htob = new ActiveXObject("Microsoft.XMLHTTP");}
  else {alert('Your browser does not support Ajax. Please upgrade!');}
  if (htob) {
	htob.open('POST', '/functs.php', true);
	htob.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	htob.onreadystatechange = function() {
	  if ((htob.readyState == 4) && (htob.status == 200)) {
		hotbox.innerHTML = htob.responseText;
	  }
	}
	htob.send(poststr);
  }
  else {message.innerHTML = 'Your browser does not support Ajax. Please upgrade!';}
}


function flipdivs(tit, tat) {
  document.getElementById(tit).style.display = "block";
  document.getElementById(tat).style.display = "none";
}


function knockknock(pong) {
  var hotbox = document.getElementById('knockback');
  hotbox.innerHTML = '';
  var poststr = 'dowhat=knockknock&pong='+pong;

  var nom = document.getElementById(pong+'_nom').value;
  if ((nom == 'alias') || (nom == '') || (!nom.match(/^[-A-Z0-9a-z_]+$/))) {
	hotbox.innerHTML = '"'+nom+'" won\'t work. Please use only letters,<br />numbers, dashes and underscores.';
	document.getElementById(pong+'_nom').select();
	return;
  }
  var mot = document.getElementById(pong+'_mot').value;
  if ((mot == 'password') || (mot == '') || (mot.match(/[,"''"\(\)`\{\}\[\] ]/))) {
	hotbox.innerHTML = 'That password won\'t work.<br />Please try another.';
	document.getElementById(pong+'_mot').select();
	return;
  }
  if (pong == 'sur') {
	var zap = document.getElementById(pong+'_che').value;
	if (zap.match(/^[A-Z0-9a-z._%+-]+@[A-Z0-9a-z.-]+\.[A-Za-z]{2,4}$/)) {
	  poststr = poststr + '&zap='+zap;
	}
	else {
	  hotbox.innerHTML = '"'+zap+'" is not a valid email address!';
	  document.getElementById(pong+'_che').select();
	  return;
	}
  }
  poststr = poststr + '&nom='+nom+'&mot='+mot;
  //    alert(poststr);

  var htob = false;
  if (window.XMLHttpRequest) {htob = new XMLHttpRequest();}
  else if (window.ActiveXObject) {
	htob = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else {alert('Your browser does not support Ajax. Please upgrade!');}
  if (htob) {
	hotbox.innerHTML = '';
	htob.open('POST', 'functs.php', true);
	htob.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	htob.onreadystatechange = function() {
	  if ((htob.readyState == 4) && (htob.status == 200)) {
		var ret = htob.responseText.match(/\[seedysays:(.*?)\](.*)/);
		var say = ret[1]; var msg = ret[2];
		if (msg.match(/alias/))    {document.getElementById(pong+'_nom').select();}
		if (msg.match(/password/)) {document.getElementById(pong+'_mot').select();}
		hotbox.innerHTML = msg;
		if (say == 'yes') {buttons('on');}
	  }
	}
	htob.send(poststr);
  }
  else {alert('uh-oh');}
}



function buttons(flip) {
  var dock = document.getElementById('buttrow');
  var port = document.getElementById('perm');
  var cave = document.getElementById('abode');
  if (flip == 'on') {
	var shuf = false;
	if (window.XMLHttpRequest) {shuf = new XMLHttpRequest();}
	else if (window.ActiveXObject) {
	  shuf = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {alert('Your browser does not support Ajax. Please upgrade!');}
	if (shuf) {
	  shuf.open("GET", 'functs.php?dowhat=id', true);
	  shuf.onreadystatechange = function() {
		if ((shuf.readyState == 4) && (shuf.status == 200)) {
		  var id = shuf.responseText.split(" :|: ");
		  var pid = id[0]; var nom = id[1];
		  var come_home = '<span class="navtxt">[<a href="/people/'+nom+'~">you</a>]</span>';
		  cave.innerHTML = come_home;
		}
	  }
	  shuf.send(null);
	}
	else {alert('Your browser does not support Ajax. Please upgrade!');}

	var save_button = document.createElement('input');
	save_button.setAttribute('type', 'button');
	save_button.setAttribute('id', 'save_butt');
	save_button.setAttribute('value', 'save');
	save_button.setAttribute('onclick', "save()");

	var new_button = document.createElement('input');
	new_button.setAttribute('type', 'button');
	new_button.setAttribute('id', 'new_butt');
	new_button.setAttribute('value', 'new');
	new_button.setAttribute('onclick', "occur('news')");

	var perm_flip = '<a href="javascript:done()">sign out</a>';
	port.innerHTML = perm_flip;

	if (dock.childNodes.length < 4) {
	  if ((dock.appendChild(save_button)) && (dock.appendChild(new_button))) {return true;}
	  else {return false;}
	}
	else {return false;}
  }
  if (flip == 'off') {
	var perm_flip = "<a href=\"javascript:occur('sub')\">sign in</a>";
	port.innerHTML = perm_flip;
	while (dock.childNodes.length > 2) {dock.removeChild(dock.lastChild);}
	cave.innerHTML = '';
  }
}


function pull() {
  var qs = '';
  for (n = 0; n < 6; n++) {
	var mar = q_array[n] + '_many';
	qs = qs + '&q' + n + '=' + document.getElementById(mar).value;
  }
  //  alert(qs);

  var drawn = false;
  if (window.XMLHttpRequest) {drawn = new XMLHttpRequest();}
  else if (window.ActiveXObject) {
	drawn = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else {alert('Your browser does not support Ajax. Please upgrade!');}
  if (drawn) {
	var lsturl = "functs.php?dowhat=draw" + qs;
	drawn.open("GET", lsturl, true);
	drawn.onreadystatechange = function() {
	  if ((drawn.readyState == 4) && (drawn.status == 200)) {
		var draws = drawn.responseText;
		//		alert(draws);
		var cards = draws.split("\n");
		for (m = 0; m <= 5; m++) {
		  var qtou = q_array[m];
		  var cheq = '<' + qtou + ':.:';
		  var hotbox = document.getElementById(qtou + '_list');
		  hotbox.innerHTML = '';
		  for (n = 0; n < (cards.length - 1); n++) {
			var card = cards[n];
			if (card.match(cheq)) {
			  var bits = card.match(/<(.*?):.:(.*?):.:(.*?)>/);
			  var qcard = bits[1];
			  var scard = bits[2];
			  var kcard = bits[3];
			  shiv = '<p class="pulled">' + kcard + '</p>';
			  hotbox.innerHTML = hotbox.innerHTML + shiv;
			}
		  }
		}
	  }
	}
	drawn.send(null);
  }
  else {alert('Your browser does not support Ajax. Please upgrade!');}
}


function newsie(how) {
  var duck = document.getElementById('flak');
  var dock = document.getElementById('news');
  var newsies = [];
  if (how == 'stack') {
	duck.innerHTML = '';
	var strurl = 'functs.php?dowhat=stack';
	for (n = 0; n <= 5; n++) {
	  var qtou = q_array[n];
	  var val = document.getElementById(qtou + '_new').value;
	  if (val != qtou) {strurl = strurl + '&' + qtou + '=' + escape(encodeURI(val));}
	  newsies.push(val);
	}
	//	alert(strurl);

	var shuf = false;
	if (window.XMLHttpRequest) {shuf = new XMLHttpRequest();}
	else if (window.ActiveXObject) {
	  shuf = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {alert('Your browser does not support Ajax. Please upgrade!');}
	if (shuf) {
	  shuf.open("GET", strurl, true);
	  shuf.onreadystatechange = function() {
		if ((shuf.readyState == 4) && (shuf.status == 200)) {
		  //		  alert(shuf.responseText);
		  var cluck = shuf.responseText.match(/^([a-z]+)\(([0-9a-z ]+)\)$/);
		  var msg = cluck[1]; var num = cluck[2];
		  var nums = num.split(' ');
		  for (u = 0; u <= 5; u++) {
			if (nums[u] == 'no') {
			  alert('Uh oh: "' + newsies[u] + '" under ' + q_array[u] + ' is already in the stack.');
			}
			document.getElementById(q_array[u] + '_new').value = q_array[u];
		  }
		  duck.innerHTML = msg;
		}
	  }
	  shuf.send(null);
	}
	else {alert('Your browser does not support Ajax. Please upgrade!');}
  }
}


function save() {
  var iname = prompt("Save draw as...?", "Title goes here");
  if ((iname != null) && (iname != "") && (iname != 'Title goes here')) {
	var qurl = "functs.php?dowhat=recit&iname="+escape(encodeURI(iname));
	for (n = 0; n <= 5; n++) {
	  var qtou = q_array[n];
	  var div = document.getElementById(qtou + '_mark');
	  if (div.checked) {qurl = qurl + '&' + qtou + '=1';}
	  else {qurl = qurl + '&' + qtou + '=0';}
	}
	//  alert(qurl);


	var shuf = false;
	if (window.XMLHttpRequest) {shuf = new XMLHttpRequest();}
	else if (window.ActiveXObject) {
	  shuf = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {alert('Your browser does not support Ajax. Please upgrade!');}
	if (shuf) {
	  shuf.open("GET", qurl, true);
	  shuf.onreadystatechange = function() {
		if ((shuf.readyState == 4) && (shuf.status == 200)) {
		  alert(shuf.responseText);
		}
	  }
	  shuf.send(null);
	}
	else {alert('Your browser does not support Ajax. Please upgrade!');}
  }
}


function ed(what, que) {
  if (what == 'me') {
	var poststr = 'dowhat=edme';
	var hotbox = document.getElementById('ok_ko');
	
	var new_pw = document.getElementById('ed_pw').value;
	if (((new_pw == 'password')) || (new_pw.match(/[,"''"\(\)`\{\}\[\] ]/))) {
	  alert("That password's no good, baby."); return;
	}
	else {poststr = poststr + '&mot='+new_pw;}

	var new_em = document.getElementById('ed_em').value;
	if (new_em.match(/^[A-Z0-9a-z._%+-]+@[A-Z0-9a-z.-]+\.[A-Za-z]{2,4}$/)) {
	  poststr = poststr + '&zap='+new_em;
	}
	else {alert('"'+new_em+'" is not a valid email address!'); return;}

	var new_nk = document.getElementById('ed_nk').value;
	poststr = poststr + '&zip='+new_nk;
  }
  else if (what == 'seed') {
	var hotbox = document.getElementById('hull_'+que);
	var old = hotbox.childNodes[0].childNodes[0].nodeValue;
	if (hotbox.childNodes.length > 0) {hotbox.innerHTML = '';}
	var sprout = document.createElement('input');
	sprout.setAttribute('type', 'text');
	sprout.setAttribute('id', 'sprout_'+que);
	sprout.setAttribute('value', old);
	sprout.setAttribute('onblur', "kern('kern', '"+que+"')");
	hotbox.appendChild(sprout);
	sprout.focus();
	return;
  }
  else if (what == 'kern') {
	var hotbox = document.getElementById('sprout_'+que);
	var kern = hotbox.value;
	var val = escape(encodeURI(kern));
	var poststr = 'dowhat=kerned&kern='+val+'&sid='+que;
	//	alert(kern);
  }
  else {
	var hotbox = document.getElementById(what + ':demsg');
	var poststr = 'dowhat=deme&que='+que+'&which='+what;

	if (que == 'save') {
	  var nom = escape(encodeURI(document.getElementById(what + ':nom').value));

	  var zip = document.getElementById(what + ':lnk').value;
	  if (zip == 'link to piece?') {zip = '';}
	  else {zip = escape(encodeURI(zip));}

	  var luv = escape(encodeURI(document.getElementById(what + ':note').value));
	  if (document.getElementById(what + ':publik').checked) {var pub = 'y';}
	  else {var pub = 'n';}

	  poststr = poststr + '&nom=' + nom + '&zip=' + zip + '&luv=' + luv + '&pub=' + pub;
	}
	if (que == 'scrap') {poststr = poststr + '&scrap=y';}
  }
  //alert(poststr);

  var htob = false;
  if (window.XMLHttpRequest) {htob = new XMLHttpRequest();}
  else if (window.ActiveXObject) {
	htob = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else {alert('Your browser does not support Ajax. Please upgrade!');}
  if (htob) {
	hotbox.innerHTML = '';
	htob.open('POST', '/functs.php', true);
	htob.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	htob.onreadystatechange = function() {
	  if ((htob.readyState == 4) && (htob.status == 200)) {
		if (que == 'scrap') {document.getElementById(what + ':main').innerHTML = '';}
		else if (what == 'kern') {document.getElementById('hull_'+que).innerHTML = '<span id="cull_'+que+'" onclick="ed(\'seed\', \''+que+'\')">'+htob.responseText+'</span>';}
		else {hotbox.innerHTML = htob.responseText;}
	  }
	}
	htob.send(poststr);
  }
  else {alert('uh-oh');}
}


function kern(what, que) {
  var hotbox = document.getElementById('sprout_'+que);
  var kern = hotbox.value;
  var val = escape(encodeURI(kern));
  var poststr = 'dowhat=kerned&kern='+val+'&sid='+que;
  var htob = false;
  if (window.XMLHttpRequest) {htob = new XMLHttpRequest();}
  else if (window.ActiveXObject) {
	htob = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else {alert('Your browser does not support Ajax. Please upgrade!');}
  if (htob) {
	htob.open('POST', '/functs.php', true);
	htob.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	htob.onreadystatechange = function() {
	  if ((htob.readyState == 4) && (htob.status == 200)) {
		document.getElementById('hull_'+que).innerHTML = '<span id="cull_'+que+'" onclick="ed(\'seed\', \''+que+'\')">'+htob.responseText+'</span>';
	  }
	}
	htob.send(poststr);
  }
  else {alert('uh-oh');}
}



function fademe(which, to) {
  var hotbox = document.getElementById(which).style;
  var from = (hotbox.opacity * 100);
  if (from < to) {
	hotbox.zIndex = 2; hotbox.display = 'block'; opacity(which, from, to);
  }
  else {
	opacity(which, from, to);
	if ((hotbox.opacity * 100) == 0) {
	  hotbox.zIndex = -1;
	}
	setTimeout("document.getElementById('"+which+"').style.display = 'none'", 400);
  }
}

function opacity(id, opacStart, opacEnd) {
  //speed for each frame
  var speed = Math.round(200 / 100);
  var timer = 0;

  //determine the direction for the blending, if start and end are the same nothing happens
  if(opacStart > opacEnd) {
	for(i = opacStart; i >= opacEnd; i--) {
	  setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
	  timer++;
	}
  } else if(opacStart < opacEnd) {
	for(i = opacStart; i <= opacEnd; i++) {
	  setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
	  timer++;
	}
  }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
  var fadingdiv = document.getElementById(id).style;
  fadingdiv.opacity = (opacity / 100);
  fadingdiv.MozOpacity = (opacity / 100);
  fadingdiv.KhtmlOpacity = (opacity / 100);
  fadingdiv.filter = "alpha(opacity=" + opacity + ")";
}


function clearurs() {
  document.getElementById('sub_nom').value = 'alias';
  document.getElementById('sub_mot').value = 'password';
}



function done() {
  var hotbox = document.getElementById('peephole');
  if ((hotbox.style.opacity * 100) != 0) {occur('suc');}
  clearurs();

  var XMLHttpRequestObject = false;
  if (window.XMLHttpRequest) {XMLHttpRequestObject = new XMLHttpRequest();}
  else if (window.ActiveXObject) {
	XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else {alert('Your browser does not support Ajax. Please upgrade!');}
  if (XMLHttpRequestObject) {
	var lsturl = "/functs.php?dowhat=adios";
	XMLHttpRequestObject.open("GET", lsturl, true);
	XMLHttpRequestObject.onreadystatechange = function() {
	  if ((XMLHttpRequestObject.readyState == 4) && (XMLHttpRequestObject.status == 200)) {
		var ok = ok;
	  }
	}
	XMLHttpRequestObject.send(null);
  }
  else {alert('Your browser does not support Ajax. Please upgrade!');}
  buttons('off');
}

