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
    $.each(
		document.getElementsByClassName( 'error', form_obj ),
		function( ) { $(this).html(''); }
	);

    // Get form data
    var params = $( form_obj ).serialize();
    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
    $.ajax( {
    	url: '/ajax/dms/add_comment.xml',
    	dataType: "xml",
   	    data : params,
		success : successFunc
	  }
	);	

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

function processAddComment( req, docnum )
{
    var respXML = req;
    //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
    $( '#__add_comment_form' + docnum )[0].reset();

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

    // Make sure add comment link is hidden
    $( '#' +  '__nal' + docnum ).hide();
    
    // Get new comment data
    var commentData = new Object();
    $.each( xml.getElementsByTagName( 'data' ),
		   function() { commentData[this.getAttribute('key')] = this.getAttribute('value' ) } 
    );
    //alert( commentData );
    
    // Check if comment list header is hidden
    var hdrDisp = $( '#' +  '__cheader' + docnum ).css("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
		$( '#' + '__naf' + docnum).slideUp(
			       // When the transition is complete:
			       function() { 

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

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

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

			         // Transition-show the entire outer comments box
			         $( '#' + '__c' + docnum).slideDown(						    
							// When the transition is complete, execute:
							function() {

							  	// Highlight the new comment
								$( '#' +  commentId ).effect("highlight", {}, 1750);
						    }
					     ); // SlideDown
		              } // after add comment form finishes hiding
			  ); // SlideUp
    } // 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
	$( '#' + '__naf' + docnum).slideUp( function() { 
			       
			         // Add the new comment to the list
			         $( '#' +  '__comments' + docnum ).append( newCommentHTML );
				 
					 // Transition-show the new comment
				 	$( '#' +  commentId ).slideDown( function() {

							     		// Highlight the new comment
							     		$( '#' +  commentId ).effect("highlight", {}, 1750);
						           } 
						       ); 
			     } // after finish func
			  ); // SlideUp
	
	// At the same time (pretty much) transition-show the add comment link
	$( '#' +  '__al' + docnum ).slideDown();
    } // else, comments already present
} // showComment

function showErrors( xml, prefix, suffix )
{
    $.each( xml.getElementsByTagName( 'error' ), 
    
	    function(  ) {
			var errKey = this.getAttribute( 'key' );
			var error  = this.getAttribute( 'msg' );

			
			var errTag = $( '#' +  prefix + errKey + suffix );
			if( errTag ) {
			    errTag.html(error);
			}
	    }
    
     );
} // show_errors

function removeDocument( docId, req )
{
    if( req == null ) {
		if( confirm( "Are you sure you want to remove this document?" ) ) {
		    
		    $.ajax( {url: '/ajax/dms/remove_document.xml',
					  data : 'did=' + docId,
					  success  : function ( req ) { removeDocument( docId, req ) },
					  error  : function () { alert( 'Removal of document failed.' ) }
				      }
				    );
		}
    } else {
		var respXML = req;
		var status  = respXML.getElementsByTagName( 'ruby' )[0].getAttribute( 'status' );
	
		if( status == 'errors' ) {
		    alert( respXML.getElementsByTagName( 'error' )[0].getAttribute( 'msg' ) );
		}
		else {
		    $( '#' +  'doc_' + docId ).fadeOut(750);
		}
    }
} // removeDocument

function showUploadForm()
{
    $( '#' +  'add-button' ).slideUp();
    $( '#' +  'upload_box' ).slideDown();
} // showUploadForm

function cancelUploadForm()
{
    $( '#upload_form' )[0].reset();
    $( '#' +  'upload_box' ).slideUp();
    $( '#' +  'add-button' ).slideDown();
    return false;
} // cancelUploadForm
