function replace(string,text,by) {
// Replaces text with by in string
	var strLength = string.length, txtLength = text.length;
	if ((strLength == 0) || (txtLength == 0)) return string;

	var i = string.indexOf(text);
	if ((!i) && (text != string.substring(0,txtLength))) return string;
	if (i == -1) return string;

	var newstr = string.substring(0,i) + by;

	if (i+txtLength < strLength)
		newstr += replace(string.substring(i+txtLength,strLength),text,by);

	return newstr;
}

function resize(image)
{
	s = document.getElementById(image).src;
	if(s.indexOf("images/thumbs")==-1)
	{
		document.getElementById(image).src = replace(s,"images","images/thumbs")+".jpg";
	}
	else
	{
		s = replace(s,"images/thumbs","images");
		document.getElementById(image).src = s.substring(0,s.length-4);
	}
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
	begin = dc.indexOf(prefix);
	if (begin != 0) return null;
  } else
	begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
	end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function Open (c) {
	window.open(c,
					'shoebox_chat',
					'scrollbars=yes,status=no,resizable=yes,toolbar=no,menubar=no,location=no');
}

function Popup (c,width,height) {
		 height = height+35;
		 width = width+37;
		leftVal = (screen.width - width) / 2;
		topVal = (screen.height - height) / 2;
		var l = leftVal;
	window.open(c,'pic',
				'scrollbars=yes,status=no,resizable=yes,toolbar=no,menubar=no,width='+width+',height='+height+',left='+l+',top='+topVal);
}



var req;

function loadXMLDoc(url)
{
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.send();
		}
	}
}

function processReqChange()
{
	// only if req shows "complete"
		if (req.readyState == 4)
		{
		// only if "OK"
		if (req.status == 200)
				{
			// ...processing statements go here...
						response  =  req.responseText.split("\r\n");

						document.getElementById("chatroom").title = response[0];


		p=top.frames['BLOGCHAT'].document;
		parentDiv = p.getElementById("parentDiv");
		lastid = p.getElementById('lastid').innerHTML;

	for(i=1; i<response.length-1; i+=3)
	{
				id = response[i];
				name = response[i+1];
				comment = response[i+2];

				c=p.getElementById(lastid);
				x = p.createElement("div");
				x.setAttribute("id", id);
				b=p.createElement("b");

						if(getCookie('shoebox_id'))
						{
								n=p.createElement("b");
								a=p.createElement("a");
								a.setAttribute("href", "blogchat.php?del="+id);
								ax=p.createTextNode(name+":");
								a.appendChild(ax);
								n.appendChild(a);
								ax=p.createTextNode(" ");
								n.appendChild(ax);
						}
						else n=p.createTextNode(name+": ");

				b.appendChild(n);
				hr=p.createElement("hr");
				hr.setAttribute("size", "1");
				x.appendChild(b);

						while(1)
						{
								start = comment.indexOf("<br>");
								if( start != -1 )
								{
										comm=p.createTextNode(comment.substr(0,start));
										x.appendChild(comm);
										a=p.createElement("br");
										x.appendChild(a);
										comment = comment.substr(start+4);
								}
								else break;
						}

						comm=p.createTextNode(comment);
						x.appendChild(comm);

				x.appendChild(hr);
				parentDiv.insertBefore(x,c);

				lastid=id;
	}
		p.getElementById('lastid').innerHTML = lastid;

				}
		}
}

		var clockID = 0;

		function update()
		{
				clockID = setTimeout("update()", 5000);
				var lastid = top.frames['BLOGCHAT'].document.getElementById('lastid').innerHTML;
				url  = 'http://www.themorts.org/mort/shoebox/updatechat.php?lastid=' + lastid;
				loadXMLDoc(url);

		}

		function startTimer()
		{
		  clockID = setTimeout("update()", 5000);
		}

		function endTimer()
		{
		  if(clockID)
		  {
			clearTimeout(clockID);
			clockID  = 0;
		  }
		}
	function sf(){document.submit_form.login_password.focus();}