/**
 * Opens a destination URL (destURL) in a popUp window of the given width and height
 *
 * @param string destURL
 * @param string windowName
 * @param integer width
 * @param integer height
 */
function popupWindow(destURL, windowName, width, height, left, top, options)
{
  var window_options = '';

  // Set the window distance from the top of screen
  if( top == 0 )
  {
  	var winHeight = parseInt(height);
  	var posY = (screen.height - winHeight) / 2;

  	window_options = window_options + 'top=' + posY +  ',';
  }
  else
  {
  	window_options = window_options + 'top=' + parseInt(top) + ',';
  }

  // Set the window distance from the left of screen
  if( left == 0 )
  {
	var winWidth = parseInt(width);
	var posX = (screen.width - winWidth) / 2;

	window_options = window_options + 'left=' + posX + ',';
  }
  else
  {
  	window_options = window_options + 'left=' + parseInt(left) + ',';
  }


  if( options.length > 0 )
  {
  	window_options = options;
  }
  else
  {
  	window_options = window_options + 'resizable=no,scrollbars=no,toolbar=no,menubar=no,location=no,status=no';
  }

  window.open(destURL,windowName,'width=' + width + ',height=' + height + ',' + window_options);
}


/**
 * Closes the window it is called from
 *
 */
function closeWindow()
{
	self.close();
}


/**
 * Modifies the CSS display attribute of a row identified by table_row_id to either be visible ('shown') or invisible ('hidden')
 *
 * @param string table_row_id
 * @param string visibility
 * @return boolean
 */
function setTableRowVisibility(table_row_id, visibility)
{
	// Validate the input data
	if( table_row_id.length == 0 )
	{
		alert('Ivalid table row specified to modify the visibility of');
		return false;
	}

	if( visibility != 'hidden' && visibility != 'shown' )
	{
		alert('Invalid visibility setting specified');
		return false;
	}

	// First ensure that one of the object we're modifying actually exists on the page
	if( document.getElementById(table_row_id) == undefined )
	{
		// Item doesn't exist on page so get out of here
		return false;
	}

	// Set the visibility 'shown' value for the table row
	var css_display_value_visible = "block";

	// Since Firefox (Netscape) doesn't render the table row correctly when the CSS display attribute is 'block',
	// we should use the value 'table-row' instead which is only currently supported by Firefox but renders correctly on screen.
	if( navigator.appName == "Netscape" && parseInt(navigator.appVersion) > 4 )
	{
		css_display_value_visible = "table-row";
	}

	// Are we requesting the table row to be shown?
	if( visibility == 'shown' )
	{
		// Yes. So show the row
		document.getElementById(table_row_id).style.display = css_display_value_visible;
	}
	else
	{
		document.getElementById(table_row_id).style.display = 'none';
	}

	// All successful
	return true;
}


/**
 * Restricts a form field with an id of field_id to be maxChars in length
 *
 * It should be called onkeyup and onchange from the form field declaration.
 *
 * If the form element with id of charsleft_text_id exists (usually a <span> element),
 * a count of how many characters can still be entered will be automatically displayed
 * inside that element.
 *
 * @param field_id				-- the id of the form field to limit the max characters on
 * @param charsleft_text_id		-- the id of the html element of where to display the number of characters remaining
 * @param maxChars				-- the maximum allowed characters
 * @param exclude_line_breaks 	-- if true, excludes line breaks from the count of total characters (defaults to false if ommitted)
 */
function checkMaxChars(field_id, charsleft_text_id, maxChars, exclude_line_breaks)
{
	var formField = document.getElementById(field_id);
	var length_of_line_breaks = 0;

	// Attempt to access the <span> element that contains the count of the number of characters left to enter.
	if( charsleft_text_id.length > 0 )
	{
		var charsLeftField = document.getElementById(charsleft_text_id);
	}

	// Should line breaks be excluded in the count of characters?
	if( true == exclude_line_breaks )
	{
		// Yes, so determine how many characters make up the line breaks in the current value of the form element.
		// As part of this, create a version of the field's value without line breaks.
		formField_value_no_linebreaks = formField.value;

		// IE on windows - uses both characters for a new line
		if( formField_value_no_linebreaks.indexOf('\r\n') != -1 )
		{
			formField_value_no_linebreaks = formField_value_no_linebreaks.replace(/\r\n/g, "");
		}
		// IE on a Mac - uses \r for a new line
		else if( formField_value_no_linebreaks.indexOf('\r') != -1 )
		{
			formField_value_no_linebreaks = formField_value_no_linebreaks.replace(/\r/g, "");
		}
		// Firefox/Netscape on any platform - uses \n for a new line
		else if( formField_value_no_linebreaks.indexOf('\n') != -1 )
		{
			formField_value_no_linebreaks = formField_value_no_linebreaks.replace(/\n/g, "");
		}

		// The length of the line breaks will be length of the field value minus the length of its value without line breaks.
		var length_of_line_breaks = formField.value.length - formField_value_no_linebreaks.length;
	}

	// Is the length of the value of the field (excluding line breaks if required) greater than the maximum allowable characters?
	if( (formField.value.length - length_of_line_breaks) > maxChars )
	{
		// Yes. The length of the value of the field exceeds the maximum allowable characters.
		// So update the field's value to be truncated to the maximum allowable characters.
		formField.value = formField.value.substring(0, (maxChars + length_of_line_breaks));

		if (charsLeftField != undefined)
		{
			// Set the characters left <span> element text to be 0 as no more characters can be entered.
			charsLeftField.innerHTML = '0';
		}
	}
	else
	{
		// No. The length of the value of the field does not exceed the maximum allowable characters.
		// So set the characters left <span> element text to be the number of characters that can still be entered (excluding line break characters if required).
		charsLeftField.innerHTML = (maxChars - (formField.value.length - length_of_line_breaks));
	}
}
