
function quoted_reply( source, target, author ) {

	m = 65;  // max length line
	r = 10;  // max levels of reply
	t = $(source).html();
	t = t.trim().replace(/<br>/g, "\n");
	t = t.trim().replace(/&gt;/g, ">");
	f = '';

	l = t.split("\n");
	for( var i = 0; i < l.length; i++ ) {
		if( /^\>+\s+/.exec( l[i] )) { // && l[i].length <= ( m + r )
			if( f.length > 0 ) {
				f = f + "\n";
			}
			f = f + '>' + l[i];

		} else {
			if( f.length > 0 ) {
				f = f + "\n";
			}
			f = f + ar_parse_quoteless_text( l[i], m );
		}
	}

	$(target).html( f.trim() );
 	position = $(target).offset();

	// Set the author field in the form
	$('#replied_comment_person_id').val( author );

	// Scroll to the reply window
	window.scrollTo( position.left, position.top );
}


function ar_parse_quoteless_text( t, m ) {
	w = t.split(' ');
	f = s = '';

	for ( var i = 0; i < w.length; i++ ) {
		newline = false;

		// Do we have a newline?
		if( /^(\n+)/.exec(w[i]) ) {
			w[i] = w[i].replace( /^\n+/, '' );

			if( f.length + s.length >= m ) {
				f = f + "\n" + "> " + s + "\n";
			} else {
				f = f + s + "\n";
				newline = true;
			}
			s = '';
		}

		if( (f.length == 0 && s.length == 0) || newline == true ) {
			s = w[i];
		} else if( w[i].length >= m ) {
			f = f + "\n" + s + "\n" + w[i];
		} else if( w[i].length + s.length + 3 >= m ) {
			f = f + "\n> " + s;
			s = w[i];
		} else {
			s = s + ' ' + w[i];
		}
	}

	// Finish up
	if( f.length + s.length >= m ) {
		f = f + "\n" + "> " + s;
	} else {
		f = f + '> ' + s;
	}

	return f;
}
