//                                               filename: setmirror1.js
//
//
//     "Setting e-print archive mirrors, part 1"
//
//  JavaScript code Copyright 1999 by David R. Morrison.
//  (Incorporates public domain cookie functions written by
//  Bill Dortch, hIdaho Design <bdortch@hidaho.com>, and 
//  documented at <http://www.hidaho.com/cookies/cookie.txt>.)
//
//  Permission is granted to use, but not to modify, this code,
//  provided that the code -- including comments and copyright notice --
//  is reproduced completely, and that the author David R. Morrison
//  is notified by email <drm@math.duke.edu> of all such uses.
//
//  Version 1.0, 30 March 1999. 
//
//  Usage: The script should be called in the <head> of an <html> document,
//  and must be preceded by the line
//
//    <base href="http://xxx.lanl.gov/">
//
//  (so that older browsers will do something reasonable).
//
//  This script will
//     (1) Define the function "changeMirror" used in part 2 of the script
//     (2) Look for a cookie called "mirror", set "chosenMirror" to the 
//         cookie's value if that value is one of the valid mirrors of 
//         xxx.lanl.gov, otherwise set "chosenMirror" to xxx.lanl.gov
//     (3) Determine the index value within mirrorArray corresponding to
//         "chosenMirror" and set "goodIndex" to that value
//     (4) Reset the base href value to that of the desired mirror site
//

mirrorArray = new Array(
"xxx.adelaide.edu.au",
"xxx.if.usp.br",
"xxx.itp.ac.cn",
"xxx.lpthe.jussieu.fr",
"xxx.uni-augsburg.de",
"xxx.imsc.ernet.in",
"xxx.tau.ac.il",
"xxx.sissa.it",
"xxx.yukawa.kyoto-u.ac.jp",
"xxx.itep.ru",
"xxx.snu.ac.kr",
"xxx.unizar.es",
"xxx.sf.nchc.gov.tw",
"xxx.soton.ac.uk",
"xxx.lanl.gov")

// First we load Bill Dortch's cookie functions

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}

function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// Now we define "changeMirror", first setting up the expiration date

var expdate = new Date ();
FixCookieDate (expdate); // Correct for Mac date bug 
expdate.setTime (expdate.getTime() + (365 * 24 * 60 * 60 * 1000)); // + 1 year

function changeMirror(newvalue){
   SetCookie("mirror", newvalue, expdate, "/")
   return true}

// next we determine the desired mirror, and fixup the cookie if necessary

chosenMirror=GetCookie("mirror");
if (chosenMirror == null) chosenMirror="xxx.lanl.gov"

badMirror = true
for (var i = 0; i < mirrorArray.length; i++) {
  if (chosenMirror == mirrorArray[i]) badMirror = false}
if (badMirror) DeleteCookie("mirror")
if (badMirror) chosenMirror = "xxx.lanl.gov"

// Determine which element in the array corresponds to the chosen mirror

for (var i = 0; i < mirrorArray.length; i++) {
  if (chosenMirror == mirrorArray[i]) goodIndex = i}

// Finally, modify the base href to be that of the chosen mirror

document.writeln("<base href='http://"+ chosenMirror +"/'>")

