/* Free software; MIT licensed.

	Copyright (c) 2005 Joseph Walton
*/

function act()
{
	if (window.getSelection) {
		var sel = window.getSelection();
		if (sel.rangeCount < 1) {
			return false;
		}
		var r = sel.getRangeAt(0);
		if ((r.startContainer == r.endContainer) && (r.startOffset == r.endOffset))
		{
			return false;
		}

		var e1 = r.startContainer;
		var e2 = r.endContainer;

		initFields(document.location, genPath(e1), r.toString(), r.startOffset,
			(e1 != e2) ? genPath(e2) : '', r.endOffset);
	
		return true;
	} else if (document.all) {
		if (document.selection.type != 'Text')
			return false;

		var sel = document.selection.createRange();

		initFields(document.location, genPath(sel.parentElement()), sel.text,
			'', '', '');
		return true;
	} else {
		throw "Sorry, this feature isn't supported in your browser."
	}
}

function initFields(location, startxp, text, startoffset,
	endxp, endoffset)
{
	document.getElementById('url').value = location;

	document.getElementById('startxp').value = startxp;
	document.getElementById('text').value = text;
	document.getElementById('startoffset').value = startoffset;

	document.getElementById('endxp').value = endxp;
	document.getElementById('endoffset').value =  endoffset;

	var p = document.getElementById('preview');
	
    if (p.firstChild)
		p.removeChild(p.firstChild);
	
	var s = text;
	if (s.length > 25) {
		s = s.substring(0, 10) + '...' + s.substring(s.length - 10);
	}

	p.appendChild(document.createTextNode(s));
	p.title = startxp;

	return true;
}

function genPath(e)
{
	var x = '';

	while (e) {
		var n = idDef(e);
		if (n) {
			x = n + x;
			break;
		}
		n = nodeDef(e);
		if (n) {
			x = n + x;
		}
		e = e.parentNode;
	}

//	x = 'document("' + document.location + '")' + x;

	return x;
}

function idDef(e)
{
	if (e.nodeType != 1) //Node.ELEMENT_NODE)
		return null;

	var id = e.getAttribute("id");
	if (id) {
		return 'id("' + id + '")';
	} else {
		return null;
	}
}

function nodeDef(e)
{
	if (e.nodeType != 1) // Node.ELEMENT_NODE)
		return null;

	var name = e.nodeName;

	var i = 0;

	var x = e;

	while (x) {
		x = x.previousSibling;
		if (x && x.nodeName == name) {
			i++;
		}
	}

	var j = 0;

	var x = e;

	while (x) {
		x = x.nextSibling;
		if (x && x.nodeName == name) {
			j++;
		}
	}

	name = name.toLowerCase();

	if (i == 0 && j == 0)
		return "/" + name;
	else
		return "/" + name + "[" + i + "]";
}

function initForm()
{
	var x = document.getElementById('reportButtonPara');
	x.style.display = 'block';
}

function checkOtherReason()
{
	var x = document.getElementById('reason');
	var y = document.getElementById('other');
	if (x.value != 'other')
		d = 'none';
	else
		d = 'inline';
	y.style.display = d;
}

function showForm()
{
	try {
		if (!act()) {
			window.alert("Please select some text first.");
			return;
		}
	} catch (x) {
		window.alert(x);
		return;
	}

	var x = document.getElementById('reportForm');
	checkOtherReason();
	x.style.display = 'block';
}

function hide()
{
	var x = document.getElementById('reportForm');
	checkOtherReason();
	x.style.display = 'none';
}

/*
<?xml version='1.0' encoding='us-ascii'?>

<!--
	In main page:
	 <script src=''/>
	 onload='initForm();'

-->

<!-- A popup for reporting typos and mistakes -->
<div id='reportButtonPara' style='display: none;' xmlns='http://www.w3.org/1999/xhtml'>
<p><button onclick='showForm()'>Report typo...</button></p>
<div class='menu' id='reportForm' style='display: none; xborder: 1px solid black;'>

<form action='/cgi-bin/typo.cgi' method='POST' onsubmit='hide()' onreset='hide()'>

<p>
<!-- Hidden elements -->
<input name='url' id='url' type='hidden'/>
<input name='startxp' id='startxp' type='hidden'/>
<input name='text' id='text' type='hidden'/>
<input name='startoffset' id='startoffset' type='hidden'/>
<input name='endxp' id='endxp' type='hidden'/>
<input name='endoffset' id='endoffset' type='hidden'/>

<del id='preview'/><br/>

<select name='reason' id='reason' onchange='checkOtherReason()'>
<option>Typo</option>
<option>Sub-par grammar</option>
<option>Blatant inaccuracy</option>
<option>Needless offence</option>
<option value='other'>Other...</option>
</select>
<input name='other' id='other'/>

<br/><input type='submit' value='Report'/>
<input type='reset' value='Cancel'/>
</p>
</form>

</div>
</div>
*/
