/**
 * 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, a count of how many
 * characters can still be entered will be automatically updated with that number
 *
 * @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
 */
function checkMaxChars(field_id, charsleft_text_id, maxChars)
{
	var formField = document.getElementById(field_id);

	if( charsleft_text_id.length > 0 )
	{
		var charsLeftField = document.getElementById(charsleft_text_id);
	}

	if( formField.value.length > maxChars )
	{
		formField.value = formField.value.substring(0,maxChars);

		if (charsLeftField != undefined)
		{
		 	charsLeftField.innerHTML = '0';
		}
	}
	else
	{
		charsLeftField.innerHTML = (maxChars - formField.value.length);
	}
}


/**
 * Determines whether the current file being viewed is a php file according to it's file extension
 *
 * @param string url	-- a url or file
 * @return boolean
 */
function is_php_file(url)
{
	// Separate the URI into the URL and query string components
	var url_components = url.split("?");

	// Get the file extension of the current file in the URL
	var file_extension = url_components[0].substr(url_components[0].lastIndexOf("."));

	// Is the file extension something other than '.php'?
	if( file_extension != '.php' )
	{
		// Yes. File extension something other than '.php'
		// Return failure.
		return false;
	}

	// File extension is '.php'
	return true;
}


/**
 * If the current page being viewed is offline and the URL in 'url' is an online url,
 * this function converts the url to an offline url.
 *
 * @param string url	-- a url or file
 * @return boolean
 */
function update_url_if_offline(url)
{
	if( (is_php_file(location.href) == false) && (is_php_file(url) == true) )
	{
		url = url.replace(/\?/, '@');
		url += '.html';

		return url;
	}
	else
	{
		return url;
	}
}