// Copyright (C) 2004 David Gymer
// All Rights Reserved

// The 500 is to try and prevent runaway reframing.
var reframe = !(!decodeURIComponent || !encodeURIComponent || !location.replace || location.search.length > 500);

var search = location.search;

function isRootIndex(location) {
  return location.pathname == '/ukgpl/index.html' || location.pathname == '/ukgpl/' || location.pathname == '/ukgpl';
}

function insertEmails(mails) {
  for (var i = 0; i < mails.length; ++i) {
    if (mails[i]) {
      var id = mails[i].id;
      var email = mails[i].user + '@' + mails[i].host;
      var e = document.getElementById(id);
      var text = e.innerHTML;
      e.innerHTML = '<A HREF="mailto:' + email + '?subject=UKGPL%20league">'+ text + '</A>';
    }
  }
}

function commonOnLoad(parentURL, frameName) {
  if (!reframe)
    return false; // Old browser?

  var params = {};

  for (var i = 1; i < search.length; ) {
    var j = search.indexOf('&', i + 1);
    if (j == -1)
      j = search.length;
    var k = search.indexOf('=', i + 1);

    if (k > j)
      k = -1;

    var name = search.substring(i, k == -1 ? j : k);
    var value = k == -1 ? null : decodeURIComponent(search.substring(k + 1, j));

    params[name] = value;

    i = j + 1;
  }

  var frames = window.frames;
  for (var i = 0; i < frames.length; ++i) {
    var frame = frames[i];
    var url = params[frame.name];

    if (url != null) {
      frame.location.href = url;
    }
  }

  if (parentURL != null && frameName != null) {
    var myName = location.pathname.replace(/\\/g, '/');

    if (!parent[frameName]) { // I'm where where I think I should be.
      var url = parentURL + '?' + frameName + '=' + encodeURIComponent(myName + search)
      top.location.href = url;
    }
  }
}
