// JavaScript utilities for importation into local web pages.
//
// These utilities require JavaScript1.2.

var devServerRegExp  = /^.+dev\.urnsandcaskets\.com/i;
var devCgiDir        = "cgi-local";
var serverNameRegExp = /^(http[s]?:\/\/)?([^\/]+)\//;
var liveCgiDir       = "cgi-bin";

function scriptUrl(scriptPath)
{
  // Launch the given CGI script, applying the correct URL
  // based on which server the script is being executed on.

  // Determine if we are to launch the script securely.

  var secure = false;
  if ((scriptUrl.arguments[1] != null) && (scriptUrl.arguments[1] == 1))
    secure = true;

  // Establish the correct protocol.

  var protocol = 'http';
  if (secure) protocol = 'https';

  // Eliminate leading and trailing slashes on the given URL.

  var slashRegExp = /^\/*([^\/].+[^\/])\/*$/;
  scriptPath      = scriptPath.replace(slashRegExp, "$1");

  // Prepare the absolute URL to the CGI script.

  var serverUrlList = serverNameRegExp.exec(document.URL);
  //fullUrl = protocol + "://" + serverUrlList[2] + "/" + getCgiDir() + "/" + scriptPath;
  fullUrl = protocol + "://urnsandcaskets.com/" + getCgiDir() + "/" + scriptPath;

  // Return the full URL.

  return(fullUrl);
//  location.href = fullUrl;
}

function getCgiDir()
{
  var cgiDir = liveCgiDir;
  if (devServerRegExp.test(document.URL))
  {
    cgiDir = devCgiDir;
  }

  return cgiDir;
}

function loadPage(page)
{
  // Load the given page securely, applying the correct URL
  // based on which server the script is being executed on.

  // Determine if we are to launch the script securely.

  var secure = false;
  if ((loadPage.arguments[1] != null) && (loadPage.arguments[1] == 1))
    secure = true;

  // Establish the correct protocol.

  var protocol = 'http';
  if (secure) protocol = 'https';

  // Prepare the absolute URL to the web page.

  var serverUrlList = serverNameRegExp.exec(document.URL);
  fullUrl = protocol + "://" + serverUrlList[2] + "/" + page;

  // Load the web page.

  location.href = fullUrl;
}
