/* See the prototype API docs for more information:
   http://www.prototypejs.org/api/event
*/
var escapeOverlay = {
	fx: function(e) 
	{
		// To make script compatable with both MSIE and Firefox
		var kC  = (window.event) ? event.keyCode : e.keyCode;
		var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;
		
		// If keypressed is escape and the new entry field is empty
		if(kC==Esc && ($F('tellus') == '' || $F('tellus') == null) )
			closeDialogue();
		else if(kC==Esc && window.confirm('Are you sure you wish to close the dialogue box?') )
			closeDialogue();
	}
}

// Save in cache (to be able to stopObserving() it), see Prototype API docs for more info:
// http://www.prototypejs.org/api/event
escapeOverlay.bfx = escapeOverlay.fx.bindAsEventListener(escapeOverlay);

// loadPopup shows the overlay and dialogue box
function loadCommentPopup() {
   	// Show the overlay (disables rest of page)
	showOverlay();
	
	// Show dialogue and focus on tellus
	$('dialogue').show();
	$('tellus').focus();
}
 
// Shows the overlay and starts the ESCAPE event listener
function showOverlay() {
	$('overlay').show();
	Event.observe(document, 'keypress', escapeOverlay.bfx );
}

// Hides the overlay and stops the ESCAPE event listener
function hideOverlay() {
	$('overlay').hide();
	
	Event.stopObserving(document, 'keypress', escapeOverlay.bfx );
}

// Closes the dialogue box, resets it and hides the overlay
function closeDialogue()
{
	hideOverlay();
	
	// Hide dialogue
	$('dialogue').hide();
	
	// Clear dialogue
	$('tellus').value = '';
}

/* Event handler for onKeyPress for the tellus field. Enables the use of the ENTER (RETURN)
key when adding a new entry in the dialogue box */
function enterKey(event, field, source) {
	// If the event key pressed was a return (code 13)
	if (event.which == 13 || event.keyCode == 13)
		giveComment(field.value, source);
}

// count is used to number the entries LI IDs
var count = 1;

// Adds an entry
function giveComment(message, source, email) {
	// Close the dialogue
	closeDialogue();
	
	// If the value entered for the new entry is not empty
	if (message != '' && message != null) {		
		// alert('xx - '+$F+' - '+message);
		// document.getElementById('comment').innerHTML = message;
		document.getElementById('comment').innerHTML = 'Your comments have been submitted.  Thanks!';
		submitComment(source, message, email);
// alert(email);
	}
}
