// ------------------------------------------------------------------------
//
// Copyright (c) 2002-2005 Stone Steps Inc. All rights reserved.
// http://www.stonesteps.ca/
//
// ------------------------------------------------------------------------

function findPostDiv(child)
{
	var node = child.parentNode;
	
	while(node && node.className != "forum_post_div") 
		node = node.parentNode;
		
	return node;
}

function findPostBodyDiv(child)
{
	var node = findPostDiv(child).firstChild;
	
	while(node && node.className != "post_body_div") 
		node = node.nextSibling;
	
	return node;
}

function findPosterName(child)
{
	var node = findPostDiv(child).firstChild;
	var nodelist;
	
	while(node && node.className != "post_header_div") 
		node = node.nextSibling;

	nodelist = node.getElementsByTagName("span");
	for(var index = 0; index < nodelist.length; index++) {
		if((node = nodelist[index]) == null)
			continue;
			
		if(node.className == "post_poster_span")
			return node.firstChild.nodeValue;
	}
	
	return "";
}

function makeQuote(str, pstrname)
{
	var line, quote;
	var bol = 0, eol, eow, maxline = 50;
	var delim = " .,!?\t\r\n";
	
	if(str == null || str.length == 0)
		return str;
	
	quote = new String("\n> --- " + pstrname + " ---\n");
	
	do {
		if((eol = str.indexOf("\n", bol)) == -1) 
			eol = str.length;
		line = str.substring(bol, ++eol);
		
		while(line.length > maxline) {
			eow = maxline;
			
			// find the end of the word
			while(eow < line.length && delim.indexOf(line.charAt(eow)) == -1)
				eow++;
			// leave all delimiters at the end of the line
			while(eow < line.length && delim.indexOf(line.charAt(eow)) != -1)
				eow++;
			
			// check if the end of the word is the end of the line (e.g. maxline is in the middle of a word)
			if(eow == line.length) 
				break;
					
			quote += "> ";
  			quote += line.substr(0, eow);
  			quote += "\n";
  			line = line.substr(eow);
		}
		
		quote += "> ";
		quote += line;
		
		bol = eol;
	} while(eol < str.length);
	
	quote += "\n";
	
	return quote;
}

function onClickReply()
{
	window.location.href = "#reply-form";
	document.getElementById("input_reply_id").focus();
}

function onClickQuote(button)
{
	var elem = findPostBodyDiv(button).firstChild;
	var textarea = document.getElementById("input_reply_id");
	var pstrname = findPosterName(button);
	var post = new String();
	var stack = new Array(0);

	do {
    	while(elem) {
    		switch(elem.nodeType) {
    			case 3:
    				if(elem.length)
    					post += elem.data;
    				break;
    			case 1:
    				if(elem.nodeName.toLowerCase() == "br")
    					post += "\n";
    				else {
    					stack.push(elem.nextSibling);
    					elem = elem.firstChild;
    					continue;
    				}
    				break;
    		}
    		elem = elem.nextSibling;
    	}
	} while((elem = stack.pop()) != null);
	
	textarea.value += makeQuote(post, pstrname); // FF cannot properly handle appendChild
	
	window.location.href = "#reply-form";
	document.getElementById("input_reply_id").focus();
}

function showFormatCodes()
{
	var helpbox = document.getElementById("format_codes_help_id");

	if(helpbox != null) 
		helpbox.style.display = (helpbox.style.display == "block") ? "none" : "block";
}

