var Main = document.getElementById('Main');

// Line Spacing
var L120 = "1.2", L150 = "1.5";
var currentSpacing = L120;
// Font Sizes
var S100 = "100%", S120 = "120%", S140 = "140%";
var currentSize = S100;
// Font family
var Verdana = "Verdana, Arial, Helvetica, sans-serif";
var Georgia = "Georgia";
var Trebuchet = "Trebuchet MS";
var Comic = "Comic Sans MS";
// Background Colour
var White = "#FFFFFF", Parchment = "#FFFFF0", paleLilac = "#FFCCFF", paleCyan = "#CCFFFF";
// Paragraph Spacing
var halfEm = "0.5em", oneEm = "1em";
// Paragraph Width
var px400 = "400px", px600 = "600px", px700 = "700px", px800 = "800px";
// Access Box
var boxStub = "Stub"; boxLabels = "Lab";

var screenPrefs = new Array(L120, S100, Verdana, Parchment, halfEm, px700, boxLabels);
var printPrefs = new Array(L120, S100, Georgia, White, halfEm, px700, boxLabels);

var screenString = screenPrefs.join(":");
var printString = printPrefs.join(":");
var defaultScreenString = screenString;
var defaultPrintString = printString;


var expireDate = new Date;
expireDate.setMonth(expireDate.getMonth()+6);

// Get the page name
var pathName = location.pathname;
var pathSplit = pathName.split('/');
var pageName = pathSplit[pathSplit.length - 1];

// Are we going to print this page?
var medium;
var query = location.search;
  if ( query.search("Print") != -1 ) {
    medium = "Print";
	}
  else {
    medium = "Screen";
	}
// alert("Medium is: " + medium);


// Load preferences from cookies =============================================
function loadScreenOptions() {
  if (document.cookie != "") {
    cookieList = document.cookie.split("; ");
// alert("Cookie List: " + cookieList);
    for (i = 0; i < cookieList.length; i++) {
      cookieString = cookieList[i];
// alert("Cookie String: " + cookieString);
      cookieName  = cookieString.split("=")[0];
// alert("Cookie Name: " + cookieName);
      cookieValue = cookieString.split("=")[1];
// alert("Cookie Value: " + cookieValue);
      if (cookieName == "screenString") {
        screenString = cookieValue;
        screenPrefs = screenString.split(":");
        assignScreenPrefs();
        }
//  screenString = document.cookie.split("=")[1];
	  }
    }
  }

function loadPrintOptions() {
  if (document.cookie != "") {
    cookieList = document.cookie.split("; ");
// alert("Cookie List: " + cookieList);
    for (i = 0; i < cookieList.length; i++) {
      cookieString = cookieList[i];
// alert("Cookie String: " + cookieString);
      cookieName  = cookieString.split("=")[0];
// alert("Cookie Name: " + cookieName);
      cookieValue = cookieString.split("=")[1];
// alert("Cookie Value: " + cookieValue);
      if (cookieName == "printString") {
        printString = cookieValue;
        screenPrefs = printString.split(":");
        assignScreenPrefs();
        }
//  screenString = document.cookie.split("=")[1];
	  }
    }
}

function loadOptions() {
clientSideInclude('BottomLines', '../../Includes/BottomLines.htm');
clientSideInclude('TopLines', '../../Includes/TopLines.htm');
returnLines();
if (medium == "Screen") loadScreenOptions();
if (medium == "Print")  loadPrintOptions();
}

// Save Preferences as cookies =================================================
function saveScreenOptions() {
  screenString = screenPrefs.join(":");
  document.cookie = "screenString=" + screenString + "; expires=" + expireDate.toGMTString()
	+ "; path=/";
	}

function savePrintOptions() {
  printString = printPrefs.join(":");
	document.cookie = "printString=" + printString + "; expires=" + expireDate.toGMTString()
	+ "; path=/";
	}

function saveOptions() {

if (medium == "Screen") saveScreenOptions();
if (medium == "Print")  savePrintOptions();

}


// Functions to show and hide divs
function showItem(divID) {
  refID = document.getElementById(divID);
	refID.style.display = "block";
	}

function hideItem(divID) {
  refID = document.getElementById(divID);
	refID.style.display = "none";
	}

function swapItems(hideID, showID) {
  hideItem(hideID); showItem(showID);
  }

function Null() {}

function accessClose() {
  accID = document.getElementById('accessLab');
  accID.style.display = "none";
  }

// Functions to set styles ===========================================
function setPref(i, val) {
  if (medium == "Screen") screenPrefs[i] = val;
  if (medium == "Print")  printPrefs[i] = val;
  }

function setLineSpace(spacing) {
var Main = document.getElementById('Main');
  setPref(0, spacing);
	Main.style.lineHeight = spacing;
}

function setFontSize(size) {
var Main = document.getElementById('Main');
  setPref(1, size);
	Main.style.fontSize = size;
}

function setFontFamily(face) {
var Main = document.getElementById('Main');
  setPref(2, face);
	Main.style.fontFamily = face;
}

function setBackgroundColour(colour) {
var Main = document.getElementById('Main');
  setPref(3, colour);
	Main.style.backgroundColor = colour;
}

function setParSpace(spacing) {
var Main = document.getElementById('Main');
var pars = Main.getElementsByTagName("p");
  setPref(4, spacing);
	for (i = 0; i < pars.length; i++) {
		var p = pars[i];
		p.style.marginTop = spacing;
		p.style.marginBottom = spacing;
		}
}

function setParWidth(bodyWidth) {
var Main = document.getElementById('Main');
  setPref(5, bodyWidth);
	Main.style.width = bodyWidth;
}

function setBoxState(boxState) {
  if (boxState == boxLabels) swapItems("accessStub", "accessLab");
  if (boxState == boxStub) swapItems("accessLab", "accessStub");
  setPref(6, boxState);
}

// Functions to assign style sets ===================================
function assignScreenPrefs() {
  setLineSpace(screenPrefs[0]);
	setFontSize(screenPrefs[1]);
	setFontFamily(screenPrefs[2]);
	setBackgroundColour(screenPrefs[3]);
	setParSpace(screenPrefs[4]);
	setParWidth(screenPrefs[5]);
  setBoxState(screenPrefs[6])
  }

function assignPrintPrefs() {
  setLineSpace(printPrefs[0]);
	setFontSize(printPrefs[1]);
	setFontFamily(printPrefs[2]);
	setBackgroundColour(printPrefs[3]);
	setParSpace(printPrefs[4]);
	setParWidth(printPrefs[5]);
  setBoxState(printPrefs[6])
  }

// Ajax Client Side Include
function clientSideInclude(id, url) {
  var req = false;
  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch (e) {
      req = false;
    }
  } else if (window.ActiveXObject) {
    // For Internet Explorer on Windows
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        req = false;
      }
    }
  }
 var element = document.getElementById(id);
 if (!element) {
  alert("Bad id " + id +
   "passed to clientSideInclude." +
   "You need a div or span element " +
   "with this id in your page.");
  return;
 }
  if (req) {
    // Synchronous request, wait till we have it all
    req.open('GET', url, false);
    req.send(null);
    element.innerHTML = req.responseText;
  } else {
    element.innerHTML =
   "Sorry, your browser does not support " +
      "XMLHTTPRequest objects. This page requires " +
      "Internet Explorer 5 or better for Windows, " +
      "or Firefox for any system, or Safari. Other " +
      "compatible browsers may also exist.";
  }
}


// Return functions ===================================================
// returnLines displays lines at the end of the page as appropriate
function returnLines() {
var Query = location.search;
if ( Query.indexOf("?Test") == 0) {
showItem("test"); showItem("message");
}
else  if ( Query.indexOf("?Return")  == 0) {
showItem("return"); showItem("message");
	}
else if (window.opener) {
showItem("close"); showItem("message");
	}
}

function closePageOld() {
	if (history.length > 1 ) {
		history.back();
		}
	else {
		window.close();
		}
}

function closePage() {
	if (window.opener) {
	window.close();
	}
}

function printPage() {
  screenString = screenPrefs.join(":");
    assignPrintPrefs();
	window.print();
//	setTimeout("assignScreenPrefs();", 60000);
  screenPrefs = screenString.split(":");
	assignScreenPrefs();
	}

// diagnostic functions
function showPrefs(Prefs) {
	document.write('Preferences: ' + Prefs.length);
	document.write('<ol>\n');
	for (i = 0; i < Prefs.length; i++) {
		document.write('<li>' + Prefs[i] + '</li>\n');
		}
	document.write('</ol>\n');
	document.write('End');
	}

function showBothPrefs() {
  document.write('<table border="0" cellspacing="4" cellpadding="2">\n<tr>\n');
  	document.write('<td width="150px">');
		showPrefs(screenPrefs);
	document.write('</td>\n');
  	document.write('<td width="150px">');
		showPrefs(printPrefs);
	document.write('</td>\n');
  document.write('</tr>\n</table>\n');
}

function restoreDefaults() {
  screenPrefs = defaultScreenString.split(":");
  printPrefs = defaultPrintString.split(":");
	assignScreenPrefs();
	assignPrintPrefs();
	}
