function showTime( idx )
{
    for( var i = 1; i<=8; i++ ) {
	var desc = document.getElementById ? document.getElementById( 'desc_' + i ) : document.all[ 'desc_' + i ];
	var link = document.getElementById ? document.getElementById( 'tod_' + i ) : document.all[ 'tod_' + i ];
	if( link ) {
	    link.className = (i == idx) ? 'current' : '';
	}
	if( desc ) {
	   desc.style.display = (i == idx) ? '' : 'none';
	}
    }
} // showTime()

function addComment( form_obj, docnum, docid )
{
    // Clear any previous error messages
    var errTags = $A( document.getElementsByClassName( 'error', form_obj ) );
    errTags.each( function( err ) { err.innerHTML = '' } );

    // Get form data
    var params = Form.serialize( form_obj );
    params += '&did=' + escape( docid );
    //alert( params );

    // Formulate a function that gets called when ajax returns
    var successFunc = function(req) { processAddComment( req, arguments.callee.docnum ); }
    successFunc.docnum = docnum;

    // Submit to ajax
    new Ajax.Request( '/ajax/dms/add_comment.xml',
                       {
			   parameters : params,
			   onSuccess  : successFunc
	  	       }
		    );	

    // Don't submit the form
    return false;
} // addComment

function processAddComment( req, docnum )
{
    var respXML = req.responseXML;
    //alert( req.responseText );
    //alert( docnum );

    var status = respXML.getElementsByTagName( 'ruby' )[0].getAttribute( 'status' );
    //alert( status );

    if( status == 'errors' ) {
	showErrors( respXML, '__', '_err' + docnum );
    }
    else if( status == 'success' ) {
	showComment( respXML, docnum );
    }
} // processAddComment

function showComment( xml, docnum )
{
    // Reset the form
    Form.reset( '__add_comment_form' + docnum );

    // Modify comment counts
    var countTag1 = $( '__cc1' + docnum );
    var countTag2 = $( '__cc2' + docnum );
    var comment_count = parseInt( countTag1.innerHTML );
    countTag1.innerHTML = comment_count + 1;
    countTag2.innerHTML = comment_count + 1;
    if( comment_count == 1 ) {
	$( '__ccp1' + docnum ).innerHTML = 's';
	$( '__ccp2' + docnum ).innerHTML = 's';
    }

    // Make sure add comment link is hidden
    Element.hide( '__nal' + docnum );
    
    // Get new comment data
    var commentData = new Object();
    $A( xml.getElementsByTagName( 'data' ) ).each( 
		   function(node) { commentData[node.getAttribute('key')] = node.getAttribute('value' ) } 
    );
    //alert( commentData );
    
    // Check if comment list header is hidden
    var hdrDisp = $( '__cheader' + docnum ).style.display;
    
    // Create new comment HTML
    var commentId      = '__comment_' + docnum + '_' + (comment_count+1);
    var commentStyle   = ' style="display: ' + (hdrDisp == 'none' ? 'block' : 'none') + '"';
    var newCommentHTML = '<div id="' + commentId + '" class="comment' + (comment_count == 0 ? ' first' : '') + '"' + commentStyle + '>'
	               +   '<div class="title">' + commentData.title   + '</div>'
                       +   '<div class="text">'  + commentData.comment + '</div>'
	               +   '<div class="poster">'
	               +     'Posted ' + commentData.posted_date + ' '
              	       +     'by <a href="mailto:' + commentData.posted_by_email + '" class="mailto">' + commentData.posted_by + '</a>'
	               +   '</div>'
                       + '</div>';

    // If comment list header is hidden (then this is the first EVER comment for this document)
    if( hdrDisp == 'none' ) {
	
	// Transition-hide the add comment form
	new Effect.BlindUp(  
			   '__naf' + docnum, 
	                   { afterFinish : 

			       // When the transition is complete:
			       function() { 

			         // Make sure the entire outer comments box is hidden
			         Element.hide( '__c' + docnum );

			         // Make sure comments header & list & link is visible
			         Element.show( '__cheader' + docnum );
			         Element.show( '__comments' + docnum );
			         Element.show( '__al' + docnum );

			         // Add the new comment to the list
			         $( '__comments' + docnum ).innerHTML += newCommentHTML;

			         // Transition-show the entire outer comments box
			         new Effect.BlindDown( 
						      '__c' + docnum, 
				                      { afterFinish :
						    
							// When the transition is complete, execute:
							function() {

							  // Highlight the new comment
							  new Effect.Highlight( commentId );
						        } 
						      } 
						     ); // BlindDown
		               } // after add comment form finishes hiding
			   }
			  ); // BlineUp
    } // if comment header hidden
    else { 
	
	// Comment header is not hidden (which means there are already comments for this document

	// Transition-hide the add comment form
	new Effect.BlindUp(  
			   '__naf' + docnum, 
	                   { afterFinish : 

			       // When the transition is complete:
			       function() { 
			       
			         // Add the new comment to the list
			         $( '__comments' + docnum ).innerHTML += newCommentHTML;
				 
				 // Transition-show the new comment
				 new Effect.BlindDown( commentId,
				                       { afterFinish : 
							   function() {

							     // Highlight the new comment
							     new Effect.Highlight( commentId );
						           } 
						       } 
						     );
			     } // after finish func
			   } 
			  ); // BlindUp
	
	// At the same time (pretty much) transition-show the add comment link
	new Effect.BlindDown( '__al' + docnum );
    } // else, comments already present
} // showComment

function showErrors( xml, prefix, suffix )
{
    var showErrFunc = function( err ) {
	var params = arguments.callee.params;
	var prefix = params.prefix;
	var suffix = params.suffix;

	var errKey = err.getAttribute( 'key' );
	var error  = err.getAttribute( 'msg' );
	
	var errTag = $( prefix + errKey + suffix );
	if( errTag ) {
	    errTag.innerHTML = error;
	}
    }; // showErrFunc
    showErrFunc.params = { 'prefix' : prefix, 'suffix' : suffix };

    $A( xml.getElementsByTagName( 'error' ) ).each( showErrFunc );
} // show_errors

function removeDocument( docId, req )
{
    if( req == null ) {
	if( confirm( "Are you sure you want to remove this document?" ) ) {
	    
	    new Ajax.Request( '/ajax/dms/remove_document.xml',
	                      {
				  parameters : 'did=' + docId,
				  onSuccess  : function ( req ) { removeDocument( docId, req ) },
				  onFailure  : function () { alert( 'Removal of document failed.' ) }
			      }
			    );
	}
    } else {
	var respXML = req.responseXML;
	var status  = respXML.getElementsByTagName( 'ruby' )[0].getAttribute( 'status' );

	if( status == 'errors' ) {
	    alert( respXML.getElementsByTagName( 'error' )[0].getAttribute( 'msg' ) );
	}
	else {
	    new Effect.Fade( 'doc_' + docId );
	}
    }
} // removeDocument

function showUploadForm()
{
    Element.hide( 'add-button' );
    new Effect.BlindDown( 'upload_box' );
} // showUploadForm

function cancelUploadForm()
{
    Form.reset( 'upload_form' );
    new Effect.BlindUp( 'upload_box',
           { afterFinish : function () {
	       Element.show( 'add-button' );
	     } // afterFinish
	   } 
    );

    return false;
} // cancelUploadForm
