// JavaScript Document

var STATUS_UNREAD = 0;
var STATUS_READ   = 1;

var PRIORITY_LOWEST  = 1;
var PRIORITY_LOW     = 2;
var PRIORITY_NORMAL  = 3;
var PRIORITY_HIGH    = 4;
var PRIORITY_HIGHEST = 5;

var MESSAGE_SELECTED   = 0;
var MESSAGE_ID         = 1;
var MESSAGE_STATUS     = 2;
var MESSAGE_ATTACHMENT = 3;
var MESSAGE_PRIORITY   = 4;
var MESSAGE_FROM       = 5;
var MESSAGE_SUBJECT    = 6;
var MESSAGE_DATE       = 7;
var MESSAGE_SIZE       = 8;
var MESSAGE_HIGHLIGHT  = 9;

// This arrray used for calaculating ids and labels to give id somewhoat huan readable ids

var type_label = new Array();
type_label[MESSAGE_SELECTED]   = "sel";
type_label[MESSAGE_ID]         = "id";
type_label[MESSAGE_STATUS]     = "read";
type_label[MESSAGE_ATTACHMENT] = "attch";
type_label[MESSAGE_PRIORITY]   = "prior";
type_label[MESSAGE_FROM]       = "from";
type_label[MESSAGE_SUBJECT]    = "subj";
type_label[MESSAGE_DATE]       = "date";
type_label[MESSAGE_SIZE]       = "size";

// This array is used to detemine which columns to render and in what order

var message_order = new Array(MESSAGE_SELECTED,
                              MESSAGE_ID,
                              MESSAGE_STATUS,
                              MESSAGE_ATTACHMENT,
                              MESSAGE_PRIORITY,
                              MESSAGE_FROM,
                              MESSAGE_SUBJECT,
                              MESSAGE_DATE,
                              MESSAGE_SIZE);

/**
 *
 * checkLabelledbyConfiguration 
 * test to see if any of the configuration checkboxes are checked
 *
 * @return ( boolean ) flag indicates whether one or more of the labelledby checkboxes is checked
 *
 */
 
 function checkLabelledbyConfiguration() {
	
	var flag = document.getElementById("selected_l").checked ||
               document.getElementById("message_l").checked ||
               document.getElementById("status_l").checked ||
               document.getElementById("attachment_l").checked ||
               document.getElementById("priority_l").checked ||
               document.getElementById("from_l").checked ||
               document.getElementById("subject_l").checked ||
               document.getElementById("date_l").checked ||
               document.getElementById("size_l").checked;
			   
	return flag;  
 }

/**
 *
 * getlablledbyForMessage 
 * Generate a list of ids for the labelledby of a messafe
 *
 * @param ( simpleEmailGrid object ) grid is s grid 
 * @param ( integer ) message the index of the image in the list
 *
 * @return ( string ) a list of ids separated by spaces
 */
 
 function getLablledbyForMessage( grid, message ) {

    var idrefs = "";
	
	if( document.getElementById("selected_l").checked) 
	   idrefs += calcGridcellId( grid, message, MESSAGE_SELECTED ) + " "; 

	if( document.getElementById("message_l").checked) 
	   idrefs += calcGridcellId( grid, message, MESSAGE_ID ) + " "; 

	if( document.getElementById("status_l").checked) 
	   idrefs += calcGridcellId( grid, message, MESSAGE_STATUS ) + " "; 

	if( document.getElementById("attachment_l").checked) 
	   idrefs += calcGridcellId( grid, message, MESSAGE_ATTACHMENT ) + " "; 

	if( document.getElementById("priority_l").checked) 
	   idrefs += calcGridcellId( grid, message, MESSAGE_PRIORITY ) + " "; 

	if( document.getElementById("from_l").checked) {
	   idrefs += calcHeaderCellId( grid, MESSAGE_FROM ) + " "; 
	   idrefs += calcGridcellId( grid, message, MESSAGE_FROM ) + " "; 
        } // endif

	if( document.getElementById("subject_l").checked) { 
	   idrefs += calcHeaderCellId( grid, MESSAGE_SUBJECT ) + " "; 
	   idrefs += calcGridcellId( grid, message, MESSAGE_SUBJECT ) + " "; 
        }

	if( document.getElementById("date_l").checked) {
	   idrefs += calcHeaderCellId( grid, MESSAGE_DATE ) + " "; 
	   idrefs += calcGridcellId( grid, message, MESSAGE_DATE ) + " "; 
        }  // endif

	if( document.getElementById("size_l").checked) {
	   idrefs += calcHeaderCellId( grid, MESSAGE_SIZE ) + " "; 
	   idrefs += calcGridcellId( grid, message, MESSAGE_SIZE ); 
        } // endif

    return idrefs;

 }

/**
 *
 * checkDescribedbyConfiguration 
 * test to see if any of the configuration checkboxes are checked
 *
 * @return ( boolean ) flag indicates whether one or more of the labelledby checkboxes is checked
 *
 */
 
 function checkDescribedbyConfiguration() {
	
	var flag = document.getElementById("selected_d").checked ||
               document.getElementById("message_d").checked ||
               document.getElementById("status_d").checked ||
               document.getElementById("attachment_d").checked ||
               document.getElementById("priority_d").checked ||
               document.getElementById("from_d").checked ||
               document.getElementById("subject_d").checked ||
               document.getElementById("date_d").checked ||
               document.getElementById("size_d").checked;
			   
	return flag;  
 }

/**
 *
 * getDescribedbyForMessage 
 * Generate a list of ids for the describedby
 *
 * @param ( simpleEmailGrid object ) grid is s grid 
 * @param ( integer ) message the index of the image in the list
 *
 * @return ( string ) a list of ids separated by spaces
 */
 
 function getDescribedbyForMessage( grid, message ) {

    var idrefs = "";
	
	if( document.getElementById("selected_d").checked) 
	   idrefs += calcGridcellId( grid, message, MESSAGE_SELECTED ) + " "; 

	if( document.getElementById("message_d").checked) 
	   idrefs += calcGridcellId( grid, message, MESSAGE_ID ) + " "; 

	if( document.getElementById("status_d").checked) 
	   idrefs += calcGridcellId( grid, message, MESSAGE_STATUS ) + " "; 

	if( document.getElementById("attachment_d").checked) 
	   idrefs += calcGridcellId( grid, message, MESSAGE_ATTACHMENT ) + " "; 

	if( document.getElementById("priority_d").checked) 
	   idrefs += calcGridcellId( grid, message, MESSAGE_PRIORITY ) + " "; 

	if( document.getElementById("from_d").checked) 
	   idrefs += calcGridcellId( grid, message, MESSAGE_FROM ) + " "; 

	if( document.getElementById("subject_d").checked) 
	   idrefs += calcGridcellId( grid, message, MESSAGE_SUBJECT ) + " "; 

	if( document.getElementById("date_d").checked) 
	   idrefs += calcGridcellId( grid, message, MESSAGE_DATE ) + " "; 

	if( document.getElementById("size_d").checked) 
	   idrefs += calcGridcellId( grid, message, MESSAGE_SIZE ); 
	   
    return idrefs;
   
 }

/**
 *
 * The calcGridcellId 
 *
 * @param ( SimpleEmailGrid Object ) grid object contraining the messages 
 * @param ( integer ) message index of the message list
 *
 * @return ( string ) the unique id for a message container (TR element)
 */
 
 function calcMessageId( grid, message ) {
	 
	 return grid.id + "_message" + message;
	 
 }
 
 /**
 *
 * The calcHeaderRowId 
 *
 * @param ( SimpleEmailGrid Object ) grid object contraining the messages
 *
 * @return ( string ) the unique id for a message container (TR element)
 */
 
 function calcHeaderRowId( grid ) {
	 
	 return grid.id + "_header";
	 
 }
 
/**
 *
 * The calcGridcellId 
 * Create a table cell node for the grid cell
 *
 * @param ( SimpleEmailGrid Object ) grid object contraining the messages 
 * @param ( integer ) message_num is the current message number
 * @param ( integer ) data_type itentifies the type of message data in the cell
 * 
 * @retuen ( string ) the return value is the id for the cell
 */
 
 function calcGridcellId( grid, message_num, data_type  ) {
	 
	 return grid.id + "_message" + message_num + "_" + type_label[data_type];
	 
 }
 
 /**
 *
 * The calcHeaderCellId 
 *
 * @param ( SimpleEmailGrid Object ) grid object contraining the messages 
 * @param ( integer ) data_type itentifies the type of message data
 * 
 * @retuen ( string ) the return value is the id for the cell
 */
 
 function calcHeaderCellId( grid, data_type  ) {
	 
	 return grid.id + "_" + type_label[data_type];
	 
 }
 
/**
 *
 * The simpleEmailHeader 
 * Create a table header cell node for the column heading
 *
 * @param ( SimpleEmailGrid Object ) grid object contraining the messages 
 * @param ( string ) text for the heade cell
 * @param ( string ) css_style will be used to style content using CSS
 * 
 * @return ( node ) return DOM node with the header cell
 */
 
 function simpleEmailHeader( email_grid, type, text, abbr, css_style ) {
	 
   var node_th = document.createElement("th");
   browser.addEvent( node_th, "focus", function(event) {handleSimpleEmailGridFocusEvent(event, email_grid);}, true); 
   browser.addEvent( node_th, "blur", function(event)  {handleSimpleEmailGridBlurEvent(event, email_grid);},  true);
   node_th.tabIndex = -1;

   node_th.id  = calcHeaderCellId( email_grid, type );
   if( css_style != "" ) 
     node_th.className = css_style;
		 
   if( abbr ) {

     var node_text = document.createTextNode(text);
     var node_abbr = document.createElement("abbr");
     node_abbr.setAttribute("title", abbr ); 
     node_abbr.appendChild( node_text );
     node_th.appendChild( node_abbr );


   } else {
	 
     var node_text = document.createTextNode(text);
	 
     node_th.appendChild( node_text );
		 
   }
	 
   return node_th;

 }


/**
 *
 * The SimpleEmailHeaders object contains information about the current e-mail message
 * and methods to render the headers
 *
 * @contructor
 */
 
function SimpleEmailHeaders() {
	
  this.cellMarkup = new Array();

  this.cellMarkup[MESSAGE_SELECTED]   = function( grid ) {
	                                 return simpleEmailHeader( grid, MESSAGE_SELECTED, "Sel", "Selected", "" );
                                  } // end function

  this.cellMarkup[MESSAGE_ID]         = function( grid ) {
	                                 return simpleEmailHeader( grid, MESSAGE_ID, "Mes", "Message", "" );
                                  } // end function

  this.cellMarkup[MESSAGE_STATUS]       = function( grid ) {
	                                 return simpleEmailHeader( grid, MESSAGE_STATUS, "Status", "", "" );
                                  } // end function

  this.cellMarkup[MESSAGE_ATTACHMENT] = function( grid ) {
	                                 return simpleEmailHeader( grid, MESSAGE_ATTACHMENT, "Att", "Attchment", "" );
                                  } // end function

  this.cellMarkup[MESSAGE_PRIORITY]  = function( grid ) {
	                                 return simpleEmailHeader( grid, MESSAGE_PRIORITY, "Pri", "Priority", "" );
                                  } // end function

  this.cellMarkup[MESSAGE_FROM]      = function( grid ) {
	                                 return simpleEmailHeader( grid, MESSAGE_FROM, "From", "", "" );
                                  } // end function

  this.cellMarkup[MESSAGE_SUBJECT]   = function( grid ) {
	                                 return simpleEmailHeader( grid, MESSAGE_SUBJECT, "Subject", "", "" );
                                  } // end function

  this.cellMarkup[MESSAGE_DATE]   = function( grid ) {
	                                 return simpleEmailHeader( grid, MESSAGE_DATE, "Date", "", "" );
                                  } // end function

  this.cellMarkup[MESSAGE_SIZE]   = function( grid ) {
	                                 return simpleEmailHeader( grid, MESSAGE_SIZE, "Size", "", "" );
                                  } // end function
}


/**
 *
 * The SimpleEmailMessageText 
 * Create a table cell node for the grid cell
 *
 * @param ( SimpleEmailGrid Object ) emil_grid object contraining the messages 
 * @param ( integer ) message is the number of the message
 * @param ( integer ) type is the type of message data in this cell
 * @param ( string ) text for the text node 
 * @param ( string ) css_style is the class name for the td element
 *
 * @return ( node ) DOM node for grid cell
 */
 
 function simpleEmailMessageText( email_grid, message, type, text, css_style ) {
	 
   var node_td = document.createElement("td");
   node_td.id  = calcGridcellId( email_grid, message, type );
   
   node_td.className = css_style;

   // Add event handlers to update visual styling when grid cell receives and looses focus
   browser.addEvent( node_td, "focus", function(event) {handleSimpleEmailGridFocusEvent(event, email_grid);}, true); 
   browser.addEvent( node_td, "blur", function(event)  {handleSimpleEmailGridBlurEvent(event, email_grid);},  true);
   
   // Set ARIA Properties
   node_td.setAttribute("role", "gridcell");
   node_td.tabIndex = -1;
   node_td.setAttribute("aria-labelledby", calcHeaderCellId(email_grid, type ) + " " + node_td.id );
	 
   var node_text = document.createTextNode(text);
	 
   node_td.appendChild( node_text );
	 
   return node_td;

 }


/**
 *
 * The SimpleEmailMessageImage 
 * Create a table cell node for the grid cell
 *
 * @param ( SimpleEmailGrid Object ) grid object contraining the messages 
 * @param ( integer ) message is the number of the message
 * @param ( integer ) type is the type of message data in this cell
 * @param ( src ) uri the uri to the image
 * @param ( text ) alt is a text equivalent for the image 
 * @param ( string ) css_style will be used to style content using CSS
 *
 * @return ( node ) DOM node for grid cell 
 */
 
 function simpleEmailMessageImage( email_grid, message, type, src, alt, css_style ) {
	 
   var node_td = document.createElement("td");
   node_td.id  = calcGridcellId( email_grid, message, type );
   node_td.className = css_style;

   // Add event handlers to update visual styling when grid cell receives and looses focus
   browser.addEvent( node_td, "focus", function(event) {handleSimpleEmailGridFocusEvent(event, email_grid);}, true); 
   browser.addEvent( node_td, "blur", function(event)  {handleSimpleEmailGridBlurEvent(event, email_grid);},  true);
   
   // Add ARIA information
   node_td.setAttribute("role", "gridcell");
   node_td.tabIndex = -1;
   node_td.setAttribute("aria-labelledby", calcHeaderCellId(email_grid, type ) + " " + node_td.id );
	 
   var node_img = document.createElement("img");
   node_img.setAttribute( "src", src );
   node_img.setAttribute( "alt", alt );
	 
   node_td.appendChild( node_img );
	 
   return node_td;
	 
 }
 
/**
 *
 * The SimpleEmailMessageCheckbox 
 * Create a table cell node for the grid cell
 *
 * @param ( SimpleEmailGrid Object ) grid object contraining the messages 
 * @param ( integer ) message is the number of the message
 * @param ( integer ) type is the type of message data in this cell
 * @param ( boolean ) flag indicates if the message is selected
 * @param ( string ) label for the checkbox form control
 * @param ( string ) css_style will be used to style content using CSS
 */
 
 function simpleEmailMessageCheckbox( email_grid, message, type, flag, label, css_style ) {
	 
   var node_td = document.createElement("td");
   node_td.id  = calcGridcellId( email_grid, message, type );
   node_td.className = css_style;

   // Add event handlers to update visual styling when grid cell receives and looses focus
   browser.addEvent( node_td, "focus", function(event) {handleSimpleEmailGridFocusEvent(event, email_grid);}, true); 
   browser.addEvent( node_td, "blur", function(event)  {handleSimpleEmailGridBlurEvent(event, email_grid);},  true);
   
   // Add ARIA information
   node_td.setAttribute("role", "gridcell");
   node_td.tabIndex = -1;
   node_td.setAttribute("aria-labelledby", calcHeaderCellId(email_grid, type ) + " " + node_td.id );
	 
   var node_input = document.createElement("input");
   node_input.setAttribute("type", "checkbox");
   node_input.setAttribute("name", "selected_" + message );
   node_input.setAttribute("title", label);
   
   // Removed checkbox from TAB order
   node_input.tabIndex = -1;
   node_input.checked = flag;
	   
   node_td.appendChild( node_input );
	 
   return node_td;
   
 }


 
/**
 *
 * The SimpleEmailMessage object contains information about the current e-mail message
 * and methods to render the message
 *
 * @contructor
 */
 
function SimpleEmailMessage() {
  this.data = new Array();
  this.cellMarkup = new Array();
  this.id = null;
  this.highlight = false;
  this.selected = false;
  
  // function to update checkbox when the item is selected
  
  this.checkboxId = null;
  
  // id
  this.data[MESSAGE_ID]         = null;
  // add fuction for creating markup for rendering id
  this.cellMarkup[MESSAGE_ID]   = function( grid, message ) {
	                                return simpleEmailMessageText( grid, message, MESSAGE_ID, grid.messages[message].data[MESSAGE_ID], "center");
                                  } // end function
  
  // Selected state information
  this.data[MESSAGE_SELECTED]   = false;
  // add fuction for creating markup for rendering selected infotmation
  this.cellMarkup[MESSAGE_SELECTED]   = function( grid, message) {
	  
	                                 grid.messages[message].checkboxId = calcGridcellId( grid, message, MESSAGE_SELECTED ); 
	                                 return simpleEmailMessageCheckbox( grid, message, MESSAGE_SELECTED, grid.messages[message].data[MESSAGE_SELECTED], "Email " + message + " Selected","");
                                  } // end function
  
  // Status of being read
  this.data[MESSAGE_STATUS]       = STATUS_UNREAD;
  // add fuction for creating markup for rendering information about a message being read
  this.cellMarkup[MESSAGE_STATUS]   = function( grid, message) {
	                                if( grid.messages[message].data[MESSAGE_STATUS] ) 
	                                   return simpleEmailMessageImage( grid, message, MESSAGE_STATUS, "images/read.gif", "Read message" ,"center");
									else    
	                                   return simpleEmailMessageImage( grid, message, MESSAGE_STATUS, "images/unread.gif", "New message" ,"center");
                                  } // end function						  
  
  // Attachment
  this.data[MESSAGE_ATTACHMENT] = false;
  // add fuction for creating markup for rendering information about a message being read
  this.cellMarkup[MESSAGE_ATTACHMENT] = function( grid, message) {
	                                if( grid.messages[message].data[MESSAGE_ATTACHMENT] ) 									
	                                   return simpleEmailMessageImage( grid, message, MESSAGE_ATTACHMENT, "images/attach.gif", "Attachment" ,"center");
									else
	                                   return simpleEmailMessageImage( grid, message, MESSAGE_ATTACHMENT, "images/noattach.gif", "None" ,"center");
                                  } // end function
    
  // Email priority
  this.data[MESSAGE_PRIORITY]   = PRIORITY_NORMAL;
  // add fuction for creating markup for rendering information about a message being read
  this.cellMarkup[MESSAGE_PRIORITY]  = function( grid, message) {
	                                switch( grid.messages[message].data[MESSAGE_PRIORITY] ) {
										
                                       case PRIORITY_LOWEST:
	                                      return simpleEmailMessageImage( grid, message, MESSAGE_PRIORITY, "images/priority_lowest.gif", "Lowest priority" ,"center");
										  break;
									   
                                       case PRIORITY_LOW:
	                                      return simpleEmailMessageImage( grid, message, MESSAGE_PRIORITY, "images/priority_low.gif", "Low priority" ,"center");
										  break;
									   
                                       case PRIORITY_HIGH:
	                                      return simpleEmailMessageImage( grid, message, MESSAGE_PRIORITY, "images/priority_high.gif", "High priority" ,"center");
										  break;

                                       case PRIORITY_HIGHEST:
	                                      return simpleEmailMessageImage( grid, message, MESSAGE_PRIORITY, "images/priority_highest.gif", "Highest priority" ,"center");
										  break;
									   
                                       default:
	                                      return simpleEmailMessageImage( grid, message, MESSAGE_PRIORITY, "images/priority_none.gif", "" ,"center");
										  break;
										  
									} // end switch
									
                                  } // end function
  
  // Email From
  this.data[MESSAGE_FROM]       = "Unkown";
  // add fuction for creating markup for rendering the message is from
  this.cellMarkup[MESSAGE_FROM]        = function( grid, message) {
	                                return simpleEmailMessageText( grid, message, MESSAGE_FROM, grid.messages[message].data[MESSAGE_FROM], "");
                                  } // end function
    
  // Email Subject
  this.data[MESSAGE_SUBJECT]    = "No subject";
  // add fuction for creating markup for rendering the subject of the message
  this.cellMarkup[MESSAGE_SUBJECT]   = function( grid, message) {
	                                return simpleEmailMessageText( grid, message, MESSAGE_SUBJECT, grid.messages[message].data[MESSAGE_SUBJECT], "");
                                  } // end function
    
    // Email Date
  this.data[MESSAGE_DATE]       = "No date";
  // add fuction for creating markup for rendering the subject of the message
  this.cellMarkup[MESSAGE_DATE]      = function( grid, message ) {
	                                return simpleEmailMessageText( grid, message, MESSAGE_DATE, grid.messages[message].data[MESSAGE_DATE], "");
                                  } // end function
      
  // Email Size
  this.data[MESSAGE_SIZE]       = "No size";
  // add fuction for creating markup for rendering who the size of the message
  this.cellMarkup[MESSAGE_SIZE]   = function( grid, message) {
	                                return simpleEmailMessageText( grid, message, MESSAGE_SIZE, grid.messages[message].data[MESSAGE_SIZE], "");
                                  } // end function

}

/**
 *
 * The SimpleEmailGrid object is used to maintain information about a e-mail list widget
 *
 * @contructor
 */
 
function SimpleEmailGrid( id ) {

  this.id = id;
  
  this.in_headers = false;
  this.current_message = -1;
  this.current_column = -1;
  
  this.last_node = null;

  this.messages = new Array();

}

/**
 * init is a subclass of SimpleEmailGrid and is used to initialize data 
 * the event handlers and for SimpleEmailGrid
 *
 * @member SimpleEmailGrid
 *
 * @return nothing
 */
 
SimpleEmailGrid.prototype.init = function() {

  this.node = document.getElementById( this.id );
  
  // Initialize data model for the grid

  // TBODY contains the information on all the current e-mail messages
  var message_data = this.node.getElementsByTagName("tbody")[0];
  
  // if there is a TBODY element extract e-mail data
  if( message_data ) {
	
	var message_rows = message_data.getElementsByTagName("tr");
	
	for(var row = 0; row < message_rows.length; row++ ) {
	
	   var message_data = message_rows[row].getElementsByTagName("td");
	   
	   // see if message data is complete
	   if( message_data.length == 9) {
		   
		 var message = new SimpleEmailMessage( row );
		   
		 // Get selected
		 message.data[MESSAGE_SELECTED]   = message_data[0].getElementsByTagName("input")[0].checked;
			  
         // Get message id
		 message.data[MESSAGE_ID]         = getTextContentOfNode(message_data[1]);
		 
         // Has message been read?
         message.data[MESSAGE_STATUS]       = getTextContentOfNode(message_data[2]).match(/true/) >= 0;
		 
         // Attachment
         message.data[MESSAGE_ATTACHMENT] = getTextContentOfNode(message_data[3]).match(/true/) >= 0;

         // Email priority
         message.data[MESSAGE_PRIORITY]   = parseInt(getTextContentOfNode(message_data[4]));
		 
		 // From 
         message.data[MESSAGE_FROM]       = getTextContentOfNode(message_data[5]);
		 
		 // Subject
         message.data[MESSAGE_SUBJECT]    = getTextContentOfNode(message_data[6]);
		 
		 // Date
         message.data[MESSAGE_DATE]       = getTextContentOfNode(message_data[7]);
		 
		 // Size
         message.data[MESSAGE_SIZE]       = getTextContentOfNode(message_data[8]);;
		 
		 // Highlight
         message.data[MESSAGE_HIGHLIGHT]  = false;
		 
		 // Add the message to the list
		 this.messages[this.messages.length] = message;
		 
	   } // endif
		
	}  // endfor
	  
  } // endif
  
  // Render E-mail headers using javascript
  
  this.headers = new SimpleEmailHeaders();
  
  simpleEmailGridRenderHeaders( this );

  // Render E-mail data using javascript
  
  simpleEmailGridRenderMessages( this );

  var obj = this;
  
  // Add event handlers for selecting a email grid
  browser.addEvent(this.node, "keydown", function(event) {handleSimpleEmailGridKeyDownEvent(event, obj);}, true); 
  browser.addEvent(this.node, "focus", function(event) {handleSimpleEmailGridTableFocusEvent(event, obj);}, true); 
  browser.addEvent(this.node, "click", function(event) {handleSimpleEmailGridClickEvent(event, obj );}, true); 

  // event.addEvent(this.node, "click", function(event) {handleRadioGroupClickEvent(event, obj);}, false); 

}

/**
 * simpleEmailGridSetHeaderFocus
 * 
 * @param ( SimpleEmailGrid object )  email_grid is the SimpleEmailGrid object to render
 * @param ( integer ) column of the column to highlight
 *
 * @return nothing
 */
 
function simpleEmailGridSetHeaderFocus( email_grid, column ) {

  if( column >= message_order.length )
	  column = message_order.length - 1;
		
  if( ( column >= 0 ) && 
      ( column < message_order.length ) )
     {
     // do this if one of the columns is selected
	  
    email_grid.current_column = column;
    email_grid.last_node = document.getElementById(calcHeaderCellId(email_grid, message_order[column]));
	
  } else {
    //	
    // do this if no column is selected	
		
    email_grid.last_node = document.getElementById(calcHeaderRowId( email_grid ));

  } // endif
	
  email_grid.last_node.focus();

}


/**
 * simpleEmailGridSetFocus
 * 
 * @param ( SimpleEmailGrid object )  email_grid is the SimpleEmailGrid object to render
 * @param ( integer ) message of the message to highlight
 * @param ( integer ) column of the column to highlight
 *
 * @return nothing
 */
 
function simpleEmailGridSetFocus( email_grid, message, column ) {

  if( column >= message_order.length )
	  column = message_order.length - 1;
		
  if( (message >= 0 ) && 
	  (message < email_grid.messages.length)
	  ) {

    if( ( column >= 0 ) && 
        ( column < message_order.length ) )
       {
      // do this if one of the columns is selected
	  
      email_grid.current_message = message;
      email_grid.current_column = column;
	  email_grid.last_node = document.getElementById(calcGridcellId(email_grid, message, message_order[column]));

    } else {
      //	
      // do this if no column is selected	
		
      email_grid.current_message = message;
      email_grid.last_node = document.getElementById(email_grid.messages[message].id);
	
    } // endif
	
    email_grid.last_node.focus();

  }  // endif
 
}

/**
 * simpleEmailGridSelected 
 * 
 * @param ( SimpleEmailGrid object )  email_grid is the SimpleEmailGrid object to render
 * @param ( integer ) message of the message to highlight
 * @param ( boolean ) boolean value on whether to highlight or to remove 
 *
 * @return nothing
 */
 
function simpleEmailGridSetSelected( email_grid, message, flag ) {

  if( (message >= 0 ) && ( message < email_grid.messages.length ) ) {
    email_grid.messages[message].selected = flag;	
    document.getElementById(email_grid.messages[message].checkboxId).getElementsByTagName("input")[0].checked = flag;

    var node = document.getElementById(email_grid.messages[message].id);

    // set ARIA flags
    if( flag ) {
      node.setAttribute("aria-selected", "true");    
	  
      if( node.className.search(/selected/i) < 0) {
         node.className += " selected";
	  }

	} else {
    node.setAttribute("aria-selected", "false");    
	 
	 // remove selected from current node
   node.className = email_grid.last_node.className.replace(/selected/i,"");

	}  // endif

  } // endif
  
}

/**
 * simpleEmailGridToggledSelected 
 * 
 * @param ( SimpleEmailGrid object )  email_grid is the SimpleEmailGrid object to render
 *
 * @return nothing 
 */
 
function simpleEmailGridToggleSelected( email_grid ) {

    if( email_grid.messages[email_grid.current_message].selected )
      simpleEmailGridSetSelected( email_grid, email_grid.current_message, false );
    else 
      simpleEmailGridSetSelected( email_grid, email_grid.current_message, true );
  
}

/**
 * simpleEmailGridRenderHeaders 
 * creates a dom node with renderable HTML mark up of current header cells  
 * 
 * @param ( SimpleEmailGrid object )  email_grid is the SimpleEmailGrid object to render
 *
 * @return nothing
 */
 
function simpleEmailGridRenderHeaders( email_grid ) {

   var node_new_header = document.createElement("thead");
   
   var node_headings = document.createElement("tr");
   browser.addEvent( node_headings, "focus", function(event) {handleSimpleEmailGridFocusEvent(event, email_grid);}, true); 
   browser.addEvent( node_headings, "blur", function(event)  {handleSimpleEmailGridBlurEvent(event, email_grid);},  true);
	   
   // Make the row keyboard focusable using tabindex attribute
   node_headings.tabIndex = -1;
	 
	 // Add a id
   node_headings.id = calcHeaderRowId( email_grid );
   
   // Set the role of the role to grid cell
   node_headings.setAttribute("role","group");

   // add new table row markup
   node_new_header.appendChild( node_headings );
   
   for(var j = 0; j < message_order.length; j++ ) {

      var node = email_grid.headers.cellMarkup[message_order[j]]( email_grid );
      node_headings.appendChild( node);
		   
   } // endfor
	   
   var node_data = email_grid.node.getElementsByTagName("thead")[0];

   // remove current grid information on messages
   email_grid.node.removeChild( node_data );
   
   // add updated rendering information on about messages
   email_grid.node.appendChild( node_new_header );
  
}

/**
 * simpleEmailGridRenderMessages 
 * creates a dom node with renderable HTML mark up for current messages 
 * 
 * @param ( SimpleEmailGrid object )  email_grid is the SimpleEmailGrid object to render
 *
 * @return nothing
 */
 
function simpleEmailGridRenderMessages( email_grid ) {

   var node_new_data = document.createElement("tbody");
   
   for(var i = 0; i < email_grid.messages.length; i++ ) {
	   
	   var node_message = document.createElement("tr");
	   
	   // Focus and blur events update visual styling of the node with focus
       browser.addEvent( node_message, "focus", function(event) {handleSimpleEmailGridFocusEvent(event, email_grid);}, true); 
       browser.addEvent( node_message, "blur", function(event)  {handleSimpleEmailGridBlurEvent(event, email_grid);},  true);

       // Make the row keyboard focusable using tabindex attribute
	   node_message.tabIndex = -1;

	   // Set the role of the role to grid cell
	   node_message.setAttribute("role","group");

	   // Set the labelledby of message if any of the confurgation controls is set
	   if( checkLabelledbyConfiguration() )
	      node_message.setAttribute("aria-labelledby", getLablledbyForMessage( email_grid, i ));

	   // Set the labelledby of message if any of the confurgation controls is set
	   if( checkDescribedbyConfiguration() )
	      node_message.setAttribute("aria-describedby", getDescribedbyForMessage( email_grid, i ));

	   // set the id of the grid cell
	   node_message.id = calcMessageId( email_grid, i );

	   // save the id as part of the message data
	   email_grid.messages[i].id = node_message.id;

	   for( var j = 0; j < message_order.length; j++ ) {

       var node = email_grid.messages[i].cellMarkup[message_order[j]]( email_grid, i );
	     node_message.appendChild( node);
		   
	   } // endfor
	   
	   node_new_data.appendChild( node_message );
		   
   }  // endfor
   
   var node_data = email_grid.node.getElementsByTagName("tbody")[0];

   // remove current grid information on messages
   email_grid.node.removeChild( node_data );
   
   // add updated rendering information on about messages
   email_grid.node.appendChild( node_new_data );
  
}

/**
 * simpleEmailGridUpdateMessageLabels 
 * 
 * @param ( SimpleEmailGrid object )  email_grid is the SimpleEmailGrid object to update labeling
 */
 
function simpleEmailGridUpdateMessageLabels( email_grid ) {

   for(var i = 0; i < email_grid.messages.length; i++ ) {
	   
     // Set the labelledby of message if any of the confurgation controls is set
     if( checkLabelledbyConfiguration() )
	     document.getElementById(email_grid.messages[i].id).setAttribute("aria-labelledby", getLablledbyForMessage( email_grid, i ));
     else
       document.getElementById(email_grid.messages[i].id).setAttribute("aria-labelledby", "");
   

     // Set the labelledby of message if any of the confurgation controls is set
     if( checkDescribedbyConfiguration() )
       document.getElementById(email_grid.messages[i].id).setAttribute("aria-describedby", getDescribedbyForMessage( email_grid, i ));
     else
       document.getElementById(email_grid.messages[i].id).setAttribute("aria-describedby", "");


   }  // endfor
   
}


/**
 * handleSimpleEmailGridKeyDownEvent processes keys associated with a radio button group
 * 
 * @param ( event ) event is the event handler for the event
 * @param ( SimpleEmailGrid object )  email_grid is the SimpleEmailGrid object that is the target of the keyboard event
 *
 * @return false if keyboard event was used by the Email grid, else true
 */
 
function handleSimpleEmailGridKeyDownEvent( event, email_grid ) {
	
  var e = event || windows.event;

  switch( e.keyCode ) {
  
    case KEY_DOWN:
 
         simpleEmailGridSetFocus( email_grid, email_grid.current_message + 1, email_grid.current_column);

         return browser.stopPropagation(event);
         break;
	
    case KEY_UP:
		
         simpleEmailGridSetFocus( email_grid, email_grid.current_message - 1,  email_grid.current_column);
				 
         return browser.stopPropagation(event);
         break;
		 
    case KEY_LEFT:
		
	 
         if( email_grid.current_column > 0 ) 
           simpleEmailGridSetFocus( email_grid, email_grid.current_message, email_grid.current_column - 1 );
         else 
           simpleEmailGridSetFocus( email_grid, email_grid.current_message, 0 );
						 
				 
         return browser.stopPropagation(event);
         break;
	
    case KEY_RIGHT:

	       if( email_grid.current_column >= 0 ) 
             simpleEmailGridSetFocus( email_grid, email_grid.current_message, email_grid.current_column + 1 );
			   else
             simpleEmailGridSetFocus( email_grid, email_grid.current_message, 0 );
					 
         return browser.stopPropagation(event);
         break;
		 
    case KEY_SPACE:
		
         if( e.ctrlKey ) {
           email_grid.current_column = -1;
           simpleEmailGridSetFocus( email_grid, email_grid.current_message, -1 );			 
         } else {
           simpleEmailGridToggleSelected( email_grid );
         } // endif
				 
         return browser.stopPropagation(event);
         break;	
  }
  
  return true;

}

/**
 * handleSimpleEmailGridTableFocusEvent
 *
 * @param ( event ) event is the event handler for the event
 * @param ( SimpleEmailGrid object ) email_grid is the SimpleEmailGrid object that is the target of the pointer event
 *
 * @return false if poiner event was used by radio group, else true
 */
 
function handleSimpleEmailGridTableFocusEvent( event, email_grid ) {
  
  var node = browser.target( event );
	 
  if( node.id == email_grid.node.id ) {
		 
     email_grid.node.className = "emailfocus";
     email_grid.node.tabIndex = -1;
	 
	 if( email_grid.last_node == null )
     simpleEmailGridSetFocus( email_grid, 0, -1 );
	 else
     simpleEmailGridSetFocus( email_grid, email_grid.current_message, email_grid.current_column );
	 
  } // endif

}

/**
 * handleSimpleEmailGridFocusEvent
 *
 * @param ( event ) event is the event handler for the event
 * @param ( SimpleEmailGrid object ) email_grid is the SimpleEmailGrid object that is the target of the pointer event
 * @return false if poiner event was used by radio group, else true
 */
 
function handleSimpleEmailGridFocusEvent( event, email_grid ) {
  
   var node = browser.target( event );
	 
   email_grid.node.className = "emailfocus";
	 
	 if( node.id != email_grid.node.id ) {
		 
     // if highlight is already set don't set again
     if( node.className.search(/highlight/i) < 0)
         node.className += " highlight";

     node.tabIndex = 0;

	 } // endif

}

/**
 * handleSimpleEmailGridBlurEvent
 *
 * @param ( event ) event is the event handler for the event
 * @param ( SimpleEmailGrid object ) email_grid is the SimpleEmailGrid object that is the target of the pointer event
 * 
 * @return nothing
 */
 
function handleSimpleEmailGridBlurEvent( event, email_grid ) {

   var node = browser.target( event );
	 
   email_grid.node.className = "email";
	 
   if( node != email_grid.node ) {
		 
     // remove highlight from current node
     node.className = node.className.replace(/highlight/i,"");
	 node.tabIndex = -1;
	 email_grid.node.tabIndex = 0;
	 
   } // endif

}


/**
 * handleSimpleEmailGridClickEvent processes pointer click events with in the SimpleEmailGrid widget
 *
 * @param ( event ) event is the event handler for the event
 * @param ( SimpleEmailGrid object ) email_grid is the SimpleEmailGrid object that is the target of the pointer event
 *
 * @return false if poiner event was used by radio group, else true
 */
 
function handleSimpleEmailGridClickEvent( event, email_grid ) {

  var node = browser.target( event );
  
  while( node &&
		 ( node.tagName.search(/tr/i) < 0 )
	   ) 
     node = node.parentNode;

  if( node ) {

    // find message number
    var result = node.id.match(/(message)([\d])/);
	
	if( result && result[2] ) {
	
	  var message = new Number(result[2]);
	
      email_grid.in_headers = false;
      simpleEmailGridSetFocus( email_grid, message, -1 );
      simpleEmailGridToggleSelected( email_grid );
	  
	} else {

      email_grid.current_message = -1;
      email_grid.in_headers = true;
					 
	  simpleEmailGridSetHeaderFocus( email_grid, -1);
		
	} // endif
	
  
  } // endif

}

/**
 * handleConfigurationToggleClickEvent 
 * Show and hide configuration information
 *
 * @param ( event ) event is the event handler for the event
 * @param ( id ) id of the content to show or hide
 *
 * @return false to prevent event bubbling
 */
 
function handleConfigurationToggleClickEvent( event, id ) {

  if( document.getElementById( id ).style.display == "block" ) {
    document.getElementById( id ).style.display = "none";  
    event.target.value = "Show Labelledby and Describedby settings for messages";  
  } else {
    document.getElementById( id ).style.display = "block";
    event.target.value = "Hide Labelledby and Describedby settings for messages";  
  } // endif

  return false;
}

/**
 * handleSimpleEmailGridApplyClickEvent 
 * Show and hide configuration information
 *
 * @param ( event ) event is the event handler for the event
 * @param ( grid object ) id of the content to show or hide
 *
 * @return false to prevent event bubbling
 */
 
function handleSimpleEmailGridApplyClickEvent( event, email_grid ) {

  // Render E-mail data using javascript
  
  simpleEmailGridUpdateMessageLabels( email_grid );

  return false;

}

