// IDs for Javascript-manipulated DOM elements:
var addentryform = '#addentryform';
var entryspanprefix = '#entry';  // entry ids are of the form "entry76", etc.
var commentspanprefix = '#comment'; // 'add comment' ids


var editentry_origcontent;
var editentry_currentid = -1;  // -1 if nothing currently being edited.

// Internal adviser function: if a current entry is being edited
// (i.e. it has the "edit this entry" form activated in its content),
// then restore the entry to its original state (dumping any changes
// the user may have made). 
//
// If no entry is being edited, then does nothing.
function CancelActiveEntry()
{
	if (editentry_currentid != -1) {
    	$(entryspanprefix + editentry_currentid).html(editentry_origcontent);
        editentry_currentid = -1;
    }
}

// When the user clicks on 'edit' for an entry, this replaces the
// entry with an edit form.
function EditEntry(entryid, day, starttime, endtime)
{
	// Already editing this entry? Nothing to do.
	if (editentry_currentid == entryid)	
		return false;
	
    CancelActiveEntry();

    var entryspan = entryspanprefix + entryid;
    var oldcontent = $(entryspan).html()
    var newcontent = $(addentryform).clone();

    editentry_origcontent = oldcontent;
    editentry_currentid = entryid;
    
 	  // Change the "add" submit value, add a "cancel" button.
	  var newsubmithtml = '<input type="submit" value="Save" DISABLED> <input type="submit" value="Cancel" onclick="return CancelActiveEntry();">';
	  newcontent.find('.addbutton').html(newsubmithtml);

    // Update the content.
    oldcontent = oldcontent.replace(/<br>/g, '\n');
    newcontent.find('[@name=content]').text(oldcontent);

    // Convert our form copy to HTML.
    newcontent = newcontent.html();

    // Set new content settings appropriately:

    newcontent = newcontent.replace(/ SELECTED>/g, '>');
    newcontent = newcontent.replace(/ selected\s*=\s*""/gi, '');
    newcontent = newcontent.replace(/ selected\s+jQuery/g, ' jQuery');

    // Update the 'day' selected.
    var dayvalue = 'value="' + day + '"';
    newcontent = newcontent.replace(dayvalue, 'SELECTED ' + dayvalue);
    dayvalue = 'value=' + day;
    newcontent = newcontent.replace(dayvalue, 'SELECTED ' + dayvalue);

    // Update the 'start time' and 'end time' selected.
    var splitindex = newcontent.indexOf('endtime');
    if (splitindex != -1) {
        var nc1 = newcontent.substring(0, splitindex);
        var nc2 = newcontent.substring(splitindex);

        // Replace 'start time':
        var startvalue = 'value="' + starttime + '"';
        newcontent = nc1.replace(startvalue, 'SELECTED ' + startvalue);

        // Replace 'end time':
        var endvalue = 'value="' + endtime + '"'
        newcontent += nc2.replace(endvalue, 'SELECTED ' + endvalue);
    }
		
    // Replace our static content.
    $(entryspan).html(newcontent);
    return false;
}


var commententry_origcontent;
var commententry_currentid = -1;  // -1 if nothing currently being commented on.

function CancelCommentEntry()
{
	if (commententry_currentid != -1) {
    	$(commentspanprefix + commententry_currentid).html(commententry_origcontent);
        commententry_currentid = -1;
    }
    return false;
}

// Add an adviser comment to the specified entry.
function AddComment(entryid)
{ 
	// Already commenting on this entry? Nothing to do.
	if (commententry_currentid == entryid)	
		return false;
	
	CancelCommentEntry();

    var commentspan = commentspanprefix + entryid;
    var oldcontent = $(commentspan).html()

    commententry_origcontent = oldcontent;
    commententry_currentid = entryid;
   
 	// Create our 'add comment' form.
    newcontent = '<div style="font-size: 12px; margin-top: 10px"><b>New comment:</b></div>'
				 +   '<textarea name="content" rows="3" style="width: 98%"></textarea>'
				 +   '<br><input type="submit" value="Add" disabled>'
				 +   '<input type="submit" value="Cancel" onclick="return CancelCommentEntry();">'
		        ;
	// Add the form.
    $(commentspan).html(newcontent);
		
    return false;
}

