// Append a cleared duplicate of the row whose id is trid to its table.
function realed_addrow (trid) {
    var trfirst = document.getElementById(trid);

    // Count the non-header rows, and clone the last row.
    var trs = trfirst.parentNode.childNodes;
    var nrows = 0;
    var trlast;
    for (var i = 0; i < trs.length; i++) {
	var tr = trs[i];
	if (tr.tagName == 'TR') {
	    trlast = tr;
	    nrows++;
	}
    }
    var tr2 = trlast.cloneNode(true);
    tr2.id = '';

    // Clear the corner classes of the last row.
    var tds = trlast.childNodes;
    var ncols = 0;
    var tdlast, corners = [];
    for (var i = 0; i < tds.length; i++) {
	var td = tds[i];
	if (td.tagName != 'TD')
	    continue;
	if (!ncols++)
	    corners.push(td);
	else
	    tdlast = td;
    }
    corners.push(tdlast);
    for (var i = 0; i < corners.length; i++) {
	var td = corners[i];
	var matched = td.className.match(/(.+) realed-c-b[lr](.*)/);
	if (matched)
	    td.className = matched[1] + matched[2];
    }

    // Toggle the odd-even class on the cloned row's cells, and clear its
    // values.
    tds = tr2.childNodes;
    for (var i = 0; i < tds.length; i++) {
	var td = tds[i];
	if (td.tagName != 'TD')
	    continue;

	// Set the class for proper odd-even row display.
	var matched = td.className.match(/(.*realed-result)([12])(.*)/);
	if (matched) {
	    var parity = matched[2] == '1' ? '2' : '1';
	    td.className = matched[1] + parity + matched[3];
	}

	// Clear the values.
	var inputs = td.childNodes;
	if (!inputs)
	    continue;
	for (var j = 0; j < inputs.length; j++) {
	    var input = inputs[j];
	    switch (input.type) {
	    case 'select-one':
	    case 'select-multi':
		for (var k = 0; k < input.options.length; k++)
		    if (input.options[k].selected)
			input.options[k].selected = false;
		break;
	    case 'text':
	    case 'textarea':
	    case 'password':
		input.value = '';
		break;
	    case 'radio':
	    case 'checkbox':
		input.checked = false;
		break;
	    default:
		continue;
	    }
	    input.name = input.name.replace(/\[\d+\]/, '[' + nrows + ']');
	}
    }
    trfirst.parentNode.appendChild(tr2);
    return false;
}

// The use of Shift-F10 and the 'Context Menu' key stopped in Internet Explorer 4+
function realed_roIE3 () {
    event.cancelBubble = true;
    event.returnValue = false;
    return false;
}

// The famous mouse double-click getaround stopped in Internet Explorer 4+
function realed_roIE4 () {
    event.returnValue = false;
}

// Page content selection stopped in Internet Explorer 4+
function realed_roNN1 (e) {
    if (e.which == 3)
	return false;
    return true;
}

// Mouse right-clicks, Shift-F10, and the 'Context Menu' key stopped in Netscape 3+
function realed_roNN2 () {
    return false;
}

// Default RealEd body onload function.
function realed_startup () {

    // Jump to the login box if it exists.
    var focus = document.getElementById('realed-focus');
    if (focus)
	focus.focus();

    // Activate read-only measures if requested.
    if (document.getElementById('realed-readonly')) {

	// Netscape.
	if (document.captureEvents) {
	    window.captureEvents(Event.MOUSEDOWN);
	    window.onmousedown = realed_roNN1;
	    window.onselectstart = realed_roNN2;
	    document.oncontextmenu = realed_roNN2;
	}

	// IE.
	else {
	    document.oncontextmenu = realed_roIE3;
	    document.onselectstart = realed_roIE4;
	}
    }
}

// Return a new XMLHTTP object.
function realed_xmlhttp_new () {
    if (window.ActiveXObject) {
	try {
	    return new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	    return new ActiveXObject("Microsoft.XMLHTTP");
	}
    }
    return new XMLHttpRequest();
}

// Deactivate any connection currently associated with CHAN.
function realed_xmlhttp_abort (chan) {
    if (window.ActiveXObject)
	delete chan.onreadystatechange;
    else
	chan.onreadystatechange = null;
    chan.abort();
}

// Onsubmit callback to protect against a double click of form NAME.
function realed_submit (name) {
    var form = document[name];

    // Allow at most one submit per two seconds.  Don't use the
    // "button.disabled = true" technique, because it can be useful to use the
    // Back button to return to a previously submitted form and resubmit.
    var now = new Date();
    var submittime = now.getTime();
    if (!form.submittime || form.submittime + 2000 <= submittime) {
	form.submittime = submittime;
	return true;
    }
    return false;
}


// Onchange callback to refresh mcls.php with the location or date chosen in
// select element SEL.
function realed_mcls_sel (sel) {
    if (sel.selectedIndex <= 0)
	return false;
    var val = sel.name + '=' + sel.options[sel.selectedIndex].value;
    var matched = location.search.match(/(.*)(loc|date)=[^&]*(.*)/);
    var search;
    if (!matched)
	search = location.search + '&' + val;
    else
	search = matched[1] + val + matched[3];
    if (search != location.search)
	window.location.href = location.pathname + search;
    return false;
}
