// 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 ('ActiveXObject' in window) {
	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 ('ActiveXObject' in window)
	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;
}

// Back end to realed_mat() and realed_amat().  Record the access to
// COMID in the database, and open the associated material.
function realed_matopen (comid, post) {
    var chan = realed_xmlhttp_new();
    chan.open("POST", "/reg/mat.php?" + post, false);
    chan.setRequestHeader("Content-Type",
			  "application/x-www-form-urlencoded");
    chan.send(post);
    var reply = null;
    eval(chan.responseText);
    realed_xmlhttp_abort(chan);
    var url = reply.url;
    if (reply.popup == '1')
	window.open(url, 'RealEdMat' + comid,
		    'toolbar=no,location=no,directories=no,status=yes,' +
		    'menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
    else
	window.location.href = url;
    return reply;
}

// A non-anonymous materials download link has just been clicked.
// Record the download in the database, update the status column
// accordingly, and open the associated material.
function realed_mat (enrollid, comid, tdid) {
    if (!comid)
	return;
    var reply = realed_matopen(comid, "enroll=" + enrollid +
			       "&com=" + comid);
    if (tdid.length > 0) {
	var cols = ['stat', 'date'];
	for (var i = 0; i < cols.length; i++) {
	    var col = cols[i];
	    if (col in reply) {
		var td = document.getElementById(tdid + '-' + col);
		if (td)
		    td.firstChild.data = reply[col];
	    }
	}
    }
}

// An anonymous materials download link has just been clicked.  Record
// the download in the database, and open the associated material.
function realed_amat (clscode, comid, jurcode) {
    if (!comid)
	return;
    realed_matopen(comid, "class=" + clscode + "&com=" + comid +
		   "&jur=" + jurcode);
}


