/**
 * 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 of the page using a file naming scheme
 * utilised by wget.
 *
 * NOTE: This function is specific to PHP-based sites
 *
 * @param string url			-- a url or file
 * @param remove_query_string	-- whether the query string should be included in the converted link
 * @return string
 */
function update_url_if_offline(url, remove_query_string)
{
	// Default 'remove_query_string' to TRUE if non-existent or undefined
	if( remove_query_string == undefined || remove_query_string == null )
	{
		remove_query_string = true;
	}

	// Determine whether the page calling this function is offline or online.
	// NOTE: If the location.pathname is equivalent to a '/' then that usually means the current site being
	//		 browsed when this function is called is usually an online site.
	if( (location.pathname != '/') && (is_php_file(location.href) == false) && (is_php_file(url) == true) )
	{
		// Site/page is offline.
		// So modify URL to be an offline one.

		// Remove any leading slash on the URL
		if( url.substr(0, 1) == "/" )
		{
			url = url.substr(1);
		}

		// Is the query string being removed from the converted URL?
		if( remove_query_string == true )
		{
			// Yes. Remove the query string from the URL and then replace all '.php' with '.php.html'
			// e.g. index.php?id=223 BECOMES index.php.html
			url = url.replace(/\?.*/, '');
			url = url.replace(/\.php/, '.php.html');
		}
		else
		{
			// No. So keep the query string in the converted link by replacing the '?' with an '@' and then add '.html' to the end of the URL.
			// e.g. index.php?id=223 BECOMES index.php@id=223.html
			url = url.replace(/\?/, '@');
			url += '.html';
		}

		return url;
	}
	else
	{
		// Site/page is online.
		// So leave URL unchanged.
		return url;
	}
}
