// The page to send the AJAX requests to
var messageRequestScript = 'messages.html';

// List of all valid STATUS values for a message
var messageStatus = new Array();
	messageStatus['U'] = 'Unread';
	messageStatus['R'] = 'Read';
	messageStatus['S'] = 'Saved';
	messageStatus['O'] = 'Sent';
	messageStatus['D'] = 'Deleted';

// The CSS class name to apply to the unread message count text 
// next to a nav item
var unreadCountClassName = 'unread_message_count';

// Stores messages that were marked as read so that they cannot
// be marked again
var readMessages = new Array();

/* Stores the number of unread messages for each folder. Index
 * should be the identifier as registered in messageNavItems.
 * Currently supported identifiers are:
 *	INBOX
 *	SENT
 *	SAVED
 *	SYSTEM
 * 	TRASH
 */
var unreadMessageCount = new Array();

/*
 * Stores objects that point to message navigation items
 * such as INBOX, SENT, DRAFTS, etc, so that we can manipulate
 * them on the fly.  The indicies should be the name of the nav
 * item (such as 'INBOX' ). The value is the object's ID
 */
var messageNavItems = new Array();



/*
 * Use this to register a div/span object in the messageNavItems
 * so that we know it exists and is valid, and can be modified by
 * the JS functions. Objects are stored in the array with an index
 * equal to an identifier (such as INBOX), and a value containing
 * the objects unique ID. For each item registered, a new SPAN
 * element will be created next to the nav item that displays updated
 * unread message counts
 * 
 * @param	String;	An identifier to reference the item by. Should
 *					  be one of: INBOX, SENT, SYSTEM, TRASH (currently)
 * @param	String;	The unique ID assigned to the div/span element that
 *					  the nav item belongs to
 * @param	Number;	The number of unread messages for this nav item, if
 *					  any
 * @return	Bool;	Returns FALSE on error, else TRUE
 *
 */
function registerMessageNavItem( sIdentifier, sElementID, nUnread )
{
	
	if( !isValidObject(getObj(sElementID)) || !isString(sIdentifier) ||
		sIdentifier.length < 1 )
			return false;

	// Register the element ID 
	messageNavItems[sIdentifier] = sElementID;

	// Create a SPAN element next to the nav item so that we can use it
	// to display unread messages.
	//oSpanElement = document.createElement( 'span' );
	//oSpanElement.setAttribute( "id", "unread_" + sIdentifier );
	//oSpanElement.innerHTML = '&nbsp;';
	//getObj(sElementID).appendChild( oSpanElement );
	
	// If an unread message count was passed, add it to the the array to
	// keep track of, and update the nav item
	if( isNumber(nUnread) )
	{
		unreadMessageCount[sIdentifier] = nUnread;
		updateUnreadMessages( sIdentifier );
	}

	return true;
}


/*
 * Updates the text in the navigation for the messages to
 * display the new number of unread messages after one has
 * been marked. The INBOX identifier must exist in the
 * messageNavItems array as registered with the
 * registerMessageNavItem() function, and the value (the
 * element's ID) must belong to a valid object. The SPAN
 * element containing the unread message count will have
 * been created as unread_<IDENTIFIER> when registered
 *
 * @param;	String;	The identifier for which nav item to 
 *						update (i.e. INBOX or SYSTEM )
 * @param;	Object;	If provided, the object passed in this
 *					  parameter will be used instead of the
 *					  object stored in messageNavItems by the
 *					  name passed in the first parameter
 * @return;	Bool;	TRUE on success, else FALSE on error
 */
function updateUnreadMessages( sNavIdentifier, oNavItem )
{
	sNavIdentifier = sNavIdentifier.toUpperCase();
	
	// We have no way to track unread messages after they are deleted
	if( sNavIdentifier == 'trash' )
		return false;

	// Make sure either an object was passed, or an identifier
	// that leads to a valid object exists
	if( isValidObject(oNavItem) )
		objPoint = oNavItem;
	else if( isValidString(sNavIdentifier) && 
				isValidString(messageNavItems[sNavIdentifier]) )
		objPoint = getObj(messageNavItems[sNavIdentifier]);
	else
	{
		return false;
	}
	
	if( !isValidObject(objPoint) )
		return false;
	
	if(sNavIdentifier == "SYSTEM" && false )
	{
		append = "message";
		if(unreadMessageCount[sNavIdentifier] > 1)
			append += "s";
	}
	else
		append = "unread";

	if( isNumber(unreadMessageCount[sNavIdentifier]) && 
				unreadMessageCount[sNavIdentifier] > 0 )
		objPoint.innerHTML = '&nbsp;(' + unreadMessageCount[sNavIdentifier] + ' '+append+')';
	else
		objPoint.innerHTML = '';

	objPoint.className = unreadCountClassName;
}


/*
 * Marks an exchange messages as having been read by
 * sending a request in the background to update it.
 * Also changes the font-weight for the message header
 * to 500 so that it is marked as read visually as well
 *
 * @param	Object;	The element that points to the message,
 *					to visually mark as read
 * @param	ID;		Message ID of the message to mark as read
 * @param;	String;	The identifier for which nav item to 
 *					update (i.e. INBOX or SYSTEM )
 * @param;	String;	An optional callback function to call when a
 *					response is returned
 * @return	VOID
 */ 
function markMessageRead( oMessage, nMessageID, sFolderIdentifier, sCallback )
{
	// If valid parameters were not passed, or the message has already been marked
	
	if( !isValidObject( oMessage ) || !nMessageID || 
			( isValidString(oMessage.getAttribute('messageStatus')) && 
				oMessage.getAttribute('messageStatus') != 'U' ) )
		return null;	
	
	//if( !isValidObject( oMessage ) || !nMessageID )
	//	return null;

	// Check for a value in sFolderIdentifier, and set to INBOX if not availabe
	if( !isValidString(sFolderIdentifier) )
		sFolderIdentifier = 'INBOX';
	else if ( isValidString(sFolderIdentifier) && !isValidString(messageNavItems[sFolderIdentifier]) )
	{
		// If this folder/identifier does not have a registered object, ignore it
		sFolderIdentifier = null;
	}
	else if ( isValidString(sFolderIdentifier) )
		sFolderIdentifier = sFolderIdentifier.toUpperCase();
	else
		return false;

	// Create the AJAX request
	AjaxRequest.post(
		{
			'url':messageRequestScript,
			'onSuccess':
					function(oResponse) 
					{ 
							// Register the message as having been read/saved
							oMessage.setAttribute('messageStatus', 'R');
							
							// If there is a messageStatus<ID> element, update it
							if( isValidObject(getObj("messageStatus"+nMessageID) ) )
								getObj("messageStatus"+nMessageID).value = 'R';
							
							// Reduce the number of unread messages for this folder by 1
							unreadMessageCount[sFolderIdentifier]--;
							
							// If a nav item is registered with the identifier in
							// 'sFolderIdentifier', update the number of unread
							// messages in the nav
							if( isValidString(sFolderIdentifier) )
								updateUnreadMessages( sFolderIdentifier );
							
							// Either unbolden the message header, or set its CSS class
							// name if it is using the single_message[_ur] method
							if( oMessage.className == 'single_message_ur' )
								oMessage.className = 'single_message';
							else
								oMessage.style.fontWeight = '500';
							
							if( isFunction(sCallback) )
								sCallback( oResponse );			
					},
			'onError':
					function(oResponse)
					{
						if( isFunction(sCallback) )
							sCallback( oResponse );
					},
				
			'parameters':{ 'updateMessageStatus':'R', 'messageID':nMessageID }
		}
	  );
	
	
	return true;
}


/*
 * Marks an exchange message as unread, and updates the style of the header
 * so that it is no longer bolded.
 *
 * Arguments are exactly the same as markMessageRead(). Check there for more
 * detailed information
 */ 
function markMessageUnread( oMessage, nMessageID, sFolderIdentifier, sCallback )
{
	// If valid parameters were not passed, or the message has already been marked
	if( !isValidObject( oMessage ) || !nMessageID  )
		return null;	

	// Check for a value in sFolderIdentifier, and set to INBOX if not availabe
	if( !isValidString(sFolderIdentifier) )
		sFolderIdentifier = 'INBOX';
	else if ( isValidString(sFolderIdentifier) && !isValidString(messageNavItems[sFolderIdentifier]) )
	{
		// If this folder/identifier does not have a registered object, ignore it
		sFolderIdentifier = null;
	}
	else if ( isValidString(sFolderIdentifier) )
		sFolderIdentifier = sFolderIdentifier.toUpperCase();
	else
		return false;
	
	// Create the AJAX request
	AjaxRequest.post(
		{
			'url':messageRequestScript,
			'onSuccess':
					function(oResponse) 
					{ 
							// Increase the number of unread messages for this folder by 1
							if( isNumber(unreadMessageCount[sFolderIdentifier]) )
								unreadMessageCount[sFolderIdentifier]++;
							else
								unreadMessageCount[sFolderIdentifier] = 1;
							
							// Mark the message as unread
							oMessage.setAttribute('messageStatus', 'U');
							
							// If there is a messageStatus<ID> element, update it
							if( isValidObject(getObj("messageStatus"+nMessageID) ) )
								getObj("messageStatus"+nMessageID).value = 'U';
							
							if( isValidString(sFolderIdentifier) )
								updateUnreadMessages( sFolderIdentifier );
							
							// Either bolden the message header, or set its CSS class
							// name if it is using the single_message[_ur] method
							if( oMessage.className == 'single_message' )
								oMessage.className = 'single_message_ur';
							else
								oMessage.style.fontWeight = '800';
							
							if( isFunction(sCallback) )
								sCallback( oResponse );			
					},
			'onError':
					function(oResponse)
					{
						if( isFunction(sCallback) )
							sCallback( oResponse );
					},
				
			'parameters':{ 'updateMessageStatus':'U', 'messageID':nMessageID }
		}
	  );
	
	
	return true;
}


/*
 * Marks an exchange message as deleted, and updates the style of the header
 * so that it is no longer bolded.
 *
 * @param	Object;	The element that points to the message,
 *					to visually mark as read
 * @param	ID;		Message ID of the message to mark as read
 * @param;	String;	The identifier for which nav item to 
 *					update (i.e. INBOX or SYSTEM )
 * @param;	String;	An optional callback function to call when a
 *					response is returned
 * @return	VOID
 */ 
function markMessageDeleted( oMessage, nMessageID, sFolderIdentifier, sCallback )
{
	// If valid parameters were not passed, or the message has already been marked
	if( !isValidObject( oMessage ) || !nMessageID  )
		return null;	

	// Check for a value in sFolderIdentifier, and set to INBOX if not availabe
	if( !isValidString(sFolderIdentifier) )
		sFolderIdentifier = 'INBOX';
	else if ( isValidString(sFolderIdentifier) && !isValidString(messageNavItems[sFolderIdentifier]) )
	{
		// If this folder/identifier does not have a registered object, ignore it
		sFolderIdentifier = null;
	}
	else if ( isValidString(sFolderIdentifier) )
		sFolderIdentifier = sFolderIdentifier.toUpperCase();
	else
		return false;
						
	// Create the AJAX request
	AjaxRequest.post(
		{
			'url':messageRequestScript,
			'onSuccess':
					function(oResponse) 
					{ 					
						// Mark the message as deleted
						oMessage.setAttribute('messageStatus', 'D');
						
						// If there is a messageStatus<ID> element, update it
						if( isValidObject(getObj("messageStatus"+nMessageID) ) )
							getObj("messageStatus"+nMessageID).value = 'D';

						// There is no counter for unread messages in the TRASH folder since we have
						// no way to track it, so just decrease the number in this folder
						if( isValidString(sFolderIdentifier) )
						{
							unreadMessageCount[sFolderIdentifier]--;
							updateUnreadMessages( sFolderIdentifier );
						}

    					oMessage.parentNode.parentNode.parentNode.removeChild(oMessage.parentNode.parentNode);
    					//getObj("top"+nMessageID).parentNode.removeChild(getObj("top"+nMessageID));
						
						if( isFunction(sCallback) )
							sCallback( oResponse );			
					},
			'onError':
					function(oResponse)
					{
						if( isFunction(sCallback) )
							sCallback( oResponse );
					},
				
			'parameters':{ 'updateMessageStatus':'D', 'messageID':nMessageID }
		}
	  );
	
	return true;
}

/*
 * Marks an exchange message as saved, and moves it to the right folder
 *
 * @param	Object;	The element that points to the message,
 *					to visually mark as read
 * @param	ID;		Message ID of the message to mark as read
 * @param;	String;	The identifier for which nav item to 
 *					update (i.e. INBOX or SYSTEM )
 * @param;	String;	An optional callback function to call when a
 *					response is returned
 * @return	VOID
 */ 
function markMessageSaved( oMessage, nMessageID, sFolderIdentifier, sCallback )
{
	// If valid parameters were not passed, or the message has already been marked
	if( !isValidObject( oMessage ) || !nMessageID  )
		return null;	

	// Check for a value in sFolderIdentifier, and set to INBOX if not availabe
	if( !isValidString(sFolderIdentifier) )
		sFolderIdentifier = 'INBOX';
	else if ( isValidString(sFolderIdentifier) && !isValidString(messageNavItems[sFolderIdentifier]) )
	{
		// If this folder/identifier does not have a registered object, ignore it
		sFolderIdentifier = null;
	}
	else if ( isValidString(sFolderIdentifier) )
		sFolderIdentifier = sFolderIdentifier.toUpperCase();
	else
		return false;
		
	// Create the AJAX request
	AjaxRequest.post(
		{
			'url':messageRequestScript,
			'onSuccess':
					function(oResponse) 
					{ 
						// If this message was previously unread, increase the number
						// of unread messages for the SAVED folder
						if( isValidObject(getObj("messageStatus"+nMessageID)) &&
							getObj("messageStatus"+nMessageID).value == 'U' )
						  updateSaved = true;
						else if ( oMessage.getAttribute('messageStatus') == 'U' )
						  updateSaved = true;
						else
						  updateSaved = false;
	
						if( updateSaved )
						{
							if( isNumber(unreadMessageCount['SAVED']) )
								unreadMessageCount['SAVED']++;
							else
								unreadMessageCount['SAVED'] = 1;
								
							updateUnreadMessages( 'SAVED' );
							
							// Also update the folder the message is being removed
							// from
							unreadMessageCount[sFolderIdentifier]--;
							updateUnreadMessages( sFolderIdentifier );
						}
						
						// Mark the message as saved
						oMessage.setAttribute('messageStatus', 'S');
							
						// If there is a messageStatus<ID> element, update it
						if( isValidObject(getObj("messageStatus"+nMessageID) ) )
							getObj("messageStatus"+nMessageID).value = 'S';

						if( isValidString(sFolderIdentifier) )
							updateUnreadMessages( sFolderIdentifier );
						
    					oMessage.parentNode.parentNode.parentNode.removeChild(oMessage.parentNode.parentNode);
    					//getObj("top"+nMessageID).parentNode.removeChild(getObj("top"+nMessageID));
						
						if( isFunction(sCallback) )
							sCallback( oResponse );			
					},
			'onError':
					function(oResponse)
					{
						if( isFunction(sCallback) )
							sCallback( oResponse );
					},
				
			'parameters':{ 'updateMessageStatus':'S', 'messageID':nMessageID }
		}
	  );
	
	
	return true;
}


/*
 * Fills the  message text field with text as part of a reply to
 * another private message
 * 
 * @param	Object;	The object for the textarea field to place the text in. Also
 *				can accept a string as an element ID, and locates the object
 * @param	String;	The text to pre-fill the message box with
 * @param	Object;	The object for the input field to pre-fill a subject. Also
 *				can accept a string as an element ID, and locates the object
 * @param	String; The text to fill into the subject box
 * @param	Date;	The date the message being replied to was sent
 * @return	VOID;
 */
function replyToMessage( messageInput, msgText, subjectInput, msgSubject, sentDate )
{
	// If we got the input parameter as a string, find the object
	//if( typeof messageInput == 'string' )
	if( isString(messageInput) )
		messageElement = eval('document.getElementById(\"' + messageInput + '\")');
		
	// If we got the subject parameter as a string, find the object
	//if( typeof subjectInput == 'string' )
	if( isString(subjectInput) )
		subjectElement = eval('document.getElementById(\"' + subjectInput + '\")');
		
	if( !isValidObject(messageElement) || msgText.length < 1 ) //typeof messageElement != 'object' || msgText.length < 1 )
		return false;
	
	messageText = '\r\r\r-------- original message --------\r';	
	messageText += '[SENT ' + sentDate + ']\r\r';
	
	if( typeof subjectElement == 'object' && msgSubject.length > 0 )
	{
		subjectElement.value = 'RE: ' + msgSubject;
		messageText += 'Subject: ' + msgSubject + '\r\r';
	}
	
	
	msgText = decodeURI(msgText);
	

	messageText += msgText;
	
	messageText += '\r\r-------- end message --------\r\r';
	
	messageElement.value = messageText;
}

/*
 * This function is used to update a value in a message form
 * to track the id of the message being replied to, and to
 * notify the user they are entering a reply
 *
 * @param	Object;	The element to display the notification in
 * @param	Object;	The element to track the message id for the message
 * @param	String;	The encrypted message id for the message being replied to
 * @return	VOID
 */
function markReply( oNotify, oReply, sReplyID )
{
	if( !isValidObject(oNotify) || !isValidObject(oReply) || !isString(sReplyID) )
		return null;
	
	//oNotify.innerHTML = 'Reply!';
	oReply.value = sReplyID;
}



/*
 * Clears the input fields for a message
 *
 * @param	Object;	The object containing the message to clear
 * @param	Object;	The object containing the subject to clear
 * @return	VOID
 */
function clearMessage( oMessage, oSubject )
{
	if( !isValidObject(oMessage) || !isValidObject(oSubject) )
		return null;
	
	oMessage.value = '';
	oSubject.value = '';	
}


/*
 * Cancels the sending of a reply by clearing the reply message
 * ID and hiding the input fields
 *
 * @param	Object;	The object that points to the table containing
 *					the message reply fields
 * @param	Object;	The object that points to the element containing
 *					the message ID for the message being replied to.
 *					This is optional, and is ignored if not present
 *
 * @return	VOID
 *
 */
function cancelMessageReply( oMessageReplyForm, oReplyMessageID )
{
	if( !isValidObject(oMessageReplyForm) )
		return false;
	
	if( isValidObject(oReplyMessageID) )
		oReplyMessageID.value = null;
	
	oMessageReplyForm.style.display = 'none';
	
}


/*
 * Sends a reply to a message by submitting the form
 * specified in the parameters
 *
 * @param	Object;	The object for the form to submit the
 *					data in
 * @param	Object;	The object for the table the reply fields
 *					are in (for hiding on success)
 * @param	Object;	The span or div element to display the
 *					status in (sending/errors/etc); optional
 * @return	VOID
 */
function sendMessageReply( oReplyForm, oReplyTable, oStatus )
{
	if( !isValidObject(oReplyForm) || !isValidObject(oReplyTable) )
		return false;
	
	var status = AjaxRequest.submit( oReplyForm,
			{
				'onSuccess':function(ret) 
					{
						if( isValidObject(oStatus) )
							oStatus.innerHTML = ret.responseText;
							
						oReplyTable.style.display = 'none';
							
					 },
				'onError':function(ret) 
					{
						if( isValidObject(oStatus) )
							oStatus.innerHTML = ret.responseText;
						
					}
				
			}
				);
	
}

/*
 * Sends a reply to a message by submitting the form
 * specified in the parameters
 */
function sendMessageData( to_uaid, from_uaid, message, subject, job_id )
{
    if(!to_uaid)
	{
		alert("Unable to add send - No to uaid");
		return false;
	}

	if(!from_uaid)
	{
		alert("Unable to add send - No from uaid");
		return false;
	}
	
	if(!message || (message &&  message.length == 0))
	{
		alert("Please provide a message");
		return false;
	}
	//alert(subject);
	//alert(message);
	
	if(!subject)
		subject = "Message from user";

	AjaxRequest.post(	
		{
			'url':'/exchange/messages.html',
			'parameters':{ 
				'send_msg':true, 
				'crypt_to_id':to_uaid,
				'crypt_from_id':from_uaid,
				'type':'U',
				'subject':subject,
				'message':message,
				'assoc_job_id':job_id
				},
			'onSuccess': function(oResponse)
					 {
					    if( isValidObject(getObj("statusMessage")) )
					 	    getObj("statusMessage").innerHTML = "<B>Message Sent.</B><br><br>";
					 },
			'onError': function(oResponse)
					 {
					 	//alert("Unable send message to user - "+oResponse.error);
					 	if( isValidObject(getObj("statusMessage")) )
					 	    getObj("statusMessage").innerHTML = "Unable send message to user - "+oResponse.error;
					 }
		} 
	);
	
}

/*
 * Mark all messages selected on the page as read, unread,
 * or deleted, depending on the value passed
 *
 * @param	Char;	The 1-letter status that tells use what
 *					to mark the message as, and what other
 *					actions need to be performed. See
 *					'messageStatus' at the top of this script
 *					for valid options
 * @param	String;	The identifier for the folder to mark the message in
 * @return	VOID
 */
function markSelectedMessages( cStatus, sFolder )
{
	if( !isValidString(cStatus) || !isValidString(sFolder) )
		return null;
		
	cStatus = cStatus.toUpperCase();
	sFolder = sFolder.toUpperCase();
	
	//oSelMsgs = document.getElementsByName("selectMessage[]");
	oSelMsgs = document.getElementsByName("selectMessage");
	listSize = oSelMsgs.length;

	// Loop through each message, and if it is checked, mark it
	// accordingly (and move/update/etc as needed)
	//for( index in oSelMsgs )
	for( index = 0; index < listSize; index++ )
	{
		// Not checked, so skip this message
		if( !oSelMsgs[index].checked )
			continue;

		oMessage = getObj("message[" + oSelMsgs[index].value + "]");
		
		// We need to check the message to make sure that we are not
		// about to mark it with a status it already has
		sCurrStatus = getObj("messageStatus" + oSelMsgs[index].value).value;
		
		if( sCurrStatus == cStatus )
			continue
		
		// Mark as unread
		if( cStatus == 'U' )
		{
			markMessageUnread( oMessage, oSelMsgs[index].value, sFolder );			
		}
		else if( cStatus == 'R' )
		{
			markMessageRead( oMessage, oSelMsgs[index].value, sFolder );	
		}
		else if( cStatus == 'S' )
		{
			markMessageSaved( oMessage, oSelMsgs[index].value, sFolder );	
		}
		else if ( cStatus == 'D' )
		{
			markMessageDeleted( oMessage, oSelMsgs[index].value, sFolder );
		}
		

	}
	
}

/*
 * Permanantly delete the selected messages
 *
 * @return	VOID
 */
function deleteMessages()
{
	oSelMsgs = document.getElementsByName("selectMessage");
	listSize = oSelMsgs.length;

	nMessageIDs = new Array();
	numMsgs = 0;
	
	// Loop through each message, and if it is checked, mark it
	// accordingly (and move/update/etc as needed)
	//for( index in oSelMsgs )
	for( index = 0; index < listSize; index++ )
	{
		// Not checked, so skip this message
		if( !oSelMsgs[index].checked )
			continue;

		nMessageIDs[numMsgs++] = oSelMsgs[index].value;
	}

	if( numMsgs == 0 )
		return;

	var status = AjaxRequest.post( 
			{
				'url':messageRequestScript,
				'onSuccess':function(ret) 
					{
						alert(ret.responseText);
						
						for( index in nMessageIDs )
						{
							oMessage = getObj("message[" + nMessageIDs[index] + "]");
							oMessage.parentNode.parentNode.parentNode.removeChild(oMessage.parentNode.parentNode);
						}
					 },
				'onError':function(ret) 
					{
						alert(ret.responseText);	
					},
				'parameters': { 'deleteMessages':'true', 'messageIDs':nMessageIDs }
				
			}
				);
}


function createMessageReply( oCopyFrom, sNewElementID )
{
	if( !isValidObject(oCopyFrom) || !isValidString( sNewElementID ) )
		return null;
		
	oTempCopy = oCopyFrom;
	oTempCopy.setAttribute("id", sNewElementID);

	return oTempCopy;
	
	
}



/*
 * If the lightBox object has been created, this function will create a 'popup' with the
 * dialog for sending a message to another user designated by their user account id.
 *
 * @param   String  The long encrypted user account id of the user to send a message to
 * @param   String  The long encrypted user account id of the user sending the message
 * @param   String  The long encrypted job id if sending a message regarding a job
 * @return  void
 */
function sendMessagePopup( sUserID, sSenderID, sJobID )
{
    // First, make sure that the lightBox object has been created
    if( !isObj(lightBox) )
        return;
    
    // Now make sure that a user ID was sent
    if( !isString(sUserID) || sUserID.length == 0 || !isString(sSenderID) || sSenderID.length == 0)
        return;
        
    // If a job id was provided, the title will be different
    if( !isNull(sJobID) )
        lightBox.enableTitleBar("Send a message about this job");
    else
        lightBox.enableTitleBar("Send a message to another member");
    
    lightBox.setPopupSize( 400, 450 );

    // The user most likely has scrolled down the page, so we do not want
    // to set the lightBox location to an absolute position, but one that
    // is relative to the distance from the top of the screen
    topPosition = document.body.scrollTop;
    popupTop = topPosition + 125;
    
    lightBox.setLocation( 300, popupTop );
    
    // Create the HTML content for the form
    html =  "<div id='sendMessageForm'>";
    html += "<table cellspacing='0' cellpadding='0' border='0' style='text-align: left; width: 350px; " +
                    " font-weight: 800; font-size: 10px; margin-left: 10px;'>";
    
    // Give them a brief set of instructions. If they are sending a message regarding a job, inform them that
    // the user will be informed that this message is about the job
    if( !isNull(sJobID) )
    {
        html += "<tr>" +
                "  <td style='font-weight: 500;'>" +
                "   Enter both a subject and a message below and click 'Send Message' to send a message to the member " +
                " that posted this job.  When the member receives the message, they will be informed as to what job you " +
                " are sending this message regarding. " +
                "  </td>" +
                "</tr>" +
                "";
    }
    else
    {
        html += "<tr>" +
                "  <td style='font-weight: 500;'>" +
                "   Enter both a subject and a message below and click 'Send Message' to send your message to this member. " +
                "  </td>" +
                "</tr>" +
                "";
    }
    
    html += 
            "<tr height='20'><td></td></tr>" +
            "<tr>" +
            "  <td>" +
            "   <b>Subject:</b><br>" +
            "	<input type='text' id='messageSubject' name='messageSubject' value='' style='width: 300px;'>" +
            "  </td>" +
            "</tr>" +
            "<tr height='10'><td></td></tr>" +
            "<tr>" +
			"  <td>" +
			"	Message:<br>" +
			"	<textarea rows='10' id='messageBody' name='messageBody' style='width: 300px;' value=''></textarea>" +
			"  </td>" +
			"</tr>" +
			"<tr height='5'><td></td></tr>" +
			"<tr>" +
			"  <td>" +
			"   <a href='#' onClick='getObj(\"messageSubject\").value = \"\"; getObj(\"messageBody\").value = \"\"; lightBox.hide(); return false;'>" +
			"<img hsrc='/exchange/images/buttons/cancel_message_sm1.gif' src='/exchange/images/buttons/cancel_message_sm.gif' border='0'>" +
			"</a>" +
			"&nbsp;&nbsp;&nbsp;" +
			"<a href='#' id='sendMessageClick' onClick='checkSendMessage( \"" + sUserID + "\", \"" + sSenderID + "\", \"" + sJobID + "\"); return false;'>" +
			"<img hsrc='/exchange/images/buttons/send_message_sm1.gif' src='/exchange/images/buttons/send_message_sm.gif' border='0'>" +
			"</a>" +
            "  </td>" +
            "</tr>" +
			"";
   
    html += "</table>";
    html += "</div>";

    // Now set the content of the popup to the HTML
    lightBox.setPopupContent(html);
    
    
    lightBox.reveal();
}


// Checks the message and if there are no problems, send the message
function checkSendMessage( sUserID, sSenderID, sJobID )
{
	if( checkMessageData(getObj("messageSubject").value, getObj("messageBody").value ) )
	{ 
		sendMessageData( sUserID, sSenderID, getObj("messageBody").value, getObj("messageSubject").value,  sJobID ); 
		confirmMessageSent("sendMessageForm");
		return true;
	}
	
	return false;
}
		                
function confirmMessageSent( sDivID )
{
    confirmObj = getObj(sDivID);
    if( !isValidObject(confirmObj) )
        return false;
    
    confirmObj.innerHTML =  "<div style='text-align: center; width: 300px; padding-top: 40px;'>" +
                            "<b>Your message has been sent</b>" +
                            "</div>" +
                            "";
                            
    // If using the lightBox and it is revealed, redraw it smaller and provide a link to close the box
    if( isValidObject(lightBox) && lightBox.isRevealed() )
    {
        lightBox.setPopupSize(400,150);
        confirmObj.innerHTML += "<br><br>" +
                                "<a href='#' onClick='lightBox.hide(); return false;'>Return to page</a>" +
                                "";
    }
    
}


function checkMessageData( sSubject, sMessage )
{
    if( isString(sSubject) && sSubject.length > 0 && isString(sMessage) && sMessage.length > 0 )
        return true;
    else
    {
        alert("You must enter both a subject and a message");
        return false;
    }
}