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

    // Count the non-header rows.
    var trs = tr.parentNode.childNodes;
    var count = 0;
    for (var i = 0; i < trs.length; i++)
	if (trs[i].tagName == 'TR')
	    count++;

    // Clear the cloned row's values.
    var tds = tr2.childNodes;
    for (var i = 0; i < tds.length; i++) {
	var td = tds[i];
	var inputs = td.childNodes;

	// Set the class for proper odd-even row display.
	if (count % 2 && td.className == 'realed-result1')
	    td.className = 'realed-result2';

	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+\]/, '[' + count + ']');
	}
    }
    tr.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;
}
