window.onload = initPage;  
// Make sure that no other javscripts assign a fuction to window.onload
// There can be only one window.onload at a time

function initPage() {
  initPopupLinks();
	fancyRules();
  // place here any other code you wish to run when the page loads.
	initReadCookie();
}

// give the option of switching styles for browsers like IE
function initReadCookie() {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

function setActiveStyleSheet(title) {
  var i, a, main;
	if (title==null || title=="") title="Full Glory";
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);


function initPopupLinks()
{
  if (!document.getElementsByTagName) return true;
  var pageLinks = document.getElementsByTagName("a");
  for (var i = 0; i < pageLinks.length; i++) 
  {
    if (((pageLinks[i].className != null) && 
         (pageLinks[i].className != "")) ||
        ((pageLinks[i].parentNode.className != null) && 
         (pageLinks[i].parentNode.className != "")))
    {
      var linkClass = " " + pageLinks[i].className + " ";
      if ((linkClass == "  ") && (pageLinks[i].parentNode.className != ""))
      {
        linkClass = " " + pageLinks[i].parentNode.className + " ";
      }
      for (var theKey in popupLinkConfig) 
      {
        if (linkClass.indexOf(" " + theKey + " ") > -1)
        {
          if ((pageLinks[i].target == "") || (pageLinks[i].target == null))
          {
            pageLinks[i].target = (popupLinkConfig[theKey][0] != "") ? popupLinkConfig[theKey][0] : theKey;
          }
          pageLinks[i].settings = popupLinkConfig[theKey][1];
          pageLinks[i].onclick = popUp;
        }
      }
    }
  }
  return true;
}

function popUp()
{
  newWin = window.open(this.href, this.target, this.settings);
  newWin.focus();
  return false;
} 

var popupLinkConfig = new Array;
// Delete/copy/modify the following lines to configure your popup windows.
popupLinkConfig["popup"] = new Array ( "", "width=350,height=450,scrollbar=yes,menubar=yes");
popupLinkConfig["external"] = new Array ( "external", "width=770,height=350,resizable=yes,scrollbars=yes");
popupLinkConfig["taf"] = new Array ( "external", "width=450,height=600,resizable=yes,scrollbars=no,menubar=no");


// Create nested hr within div to allow for styling in IE
function fancyRules() { 
  if (!document.getElementsByTagName) return; 
	  var hr = document.getElementsByTagName("hr");
  for (var i=0; i<hr.length; i++) { 
    var newhr = hr[i]; 
    var wrapdiv = document.createElement('div');
    wrapdiv.className = 'line';  
    newhr.parentNode.replaceChild(wrapdiv, newhr);  
    wrapdiv.appendChild(newhr);  
  } 
 } 

/* TAF */
function pageTAFcup() {
	var url = escape(document.location.href);
	var reg = /servlets\/Meta2(?:.+)?/
	if (reg.test(url)) {  url = url.replace(reg, "cup/srd_forms/english/signup/lmp_form.jsp","g");} 
	var taf = window.open("/gm/servlets/cupTAF?page="+url, "cuptaf", "height=500, width=455, resizable=yes, scrollbars=yes");
	taf.focus()
}

function pageTAFcupfr() {
	var url = escape(document.location.href);
	var reg = /servlets\/Meta2(?:.+)?/
	if (reg.test(url)) { url = url.replace(reg, "cup/srd_forms/french/signup/lmp_form.jsp","g");}
	var taf = window.open("/gm/servlets/cupTAF?page="+url+"&lang=fr", "cuptaf", "height=500, width=455, resizable=yes, scrollbars=yes");
	taf.focus()
}

/* Switch Language */
function switchLangCup() {
	docURL = document.URL;
	if (docURL.indexOf('/french/')!=-1) {
		newURL = docURL.replace('/french/','/english/')
	} else {
		newURL = docURL.replace('/english/','/french/')
	}
	if (newURL != 'null') {
		self.location = newURL;
		return;
	}
}



