/* global.js */


/* Global variables */

// The website's domain name (minus trailing slash)
var domain = 'http://v1.jdclark.org';

// W3C DOM compatability check
var W3CDOM = (document.getElementById && document.createElement && document.getElementsByTagName);



/* Multiple onload events */

function addLoadEvent(func)
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    };
  }
}



/* Multiple onunload events */

function addUnloadEvent(func)
{
  var oldonunload = window.onunload;
  if (typeof window.onunload != 'function') {
    window.onunload = function() {
      func();
      window.onload = null;
      window.onunload = null;
    }
  } else {
    window.onunload = function() {
      oldonunload();
      func();
    }
  }
}



/* Escape from framesets */

function bustFrames()
{
  if(top != self) {
    top.window.location = window.location;
  }
}
addLoadEvent(bustFrames);



/* Append a SCRIPT element to the HEAD of a document */

function appendScript(url)
{
  if(!document.getElementsByTagName || !document.createElement) {return;}
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = url;
  document.getElementsByTagName('head')[0].appendChild(script);
}



/* Append a LINK to a stylesheet to the HEAD of a document */

function appendStyle(url)
{
  if(!document.getElementsByTagName || !document.createElement) {return;}
  var link = document.createElement("link");
  link.rel = "stylesheet";
  link.type = "text/css";
  link.href = url;
  document.getElementsByTagName('head')[0].appendChild(link);
}



/* Focus the cursor on a form field on page load */

function focusField(field) {
  var element = document.getElementById(field);
  element = element.focus();
}



/* Create a "Go Back" link */

function goBack()
{
  var span = document.getElementsByTagName('span');
  for (i = 0; i < span.length; i++) {
    if (span[i].className == 'go-back') {
      var text = span[i].innerHTML;
      span[i].innerHTML = '<a class="back" href="javascript:history.back();">' + text + '</a>';
    }
  }
}

addLoadEvent(goBack);



/* Fix ABBR element for Internet Explorer (versions 6 and lower) */

if(document.all) {
  document.createElement('abbr');
}



/* Confirm mailto links */

function launchMail()
{
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("email")) {
      links[i].onclick = function() {
        var agree=confirm("This link will launch your default email client. Would you like to continue?");
        if (agree) return true;
        else return false;
      }
    }
  }
}
addLoadEvent(launchMail);



/* Confirm reset for forms */

function confirmReset()
{
  if (!document.getElementsByTagName) return false;
  var input = document.getElementsByTagName("input");
  for (var i=0; i < input.length; i++) {
    if (input[i].className.match("reset")) {
      input[i].onclick = function() {
        var agree = confirm("Are you sure you want to reset this form?");
        if (agree) return true;
        else return false;
      }
    }
  }
}
addLoadEvent(confirmReset);



/* Validate search form input */

function checkSearch()
{
  if (document.getElementById && document.getElementById('q').value=='') {
    alert("Please enter a keyword or phrase.");
    document.getElementById('q').focus();
    return false;
  } else {
    return true;
  }
}

/*
  Attach this function to the search form via the 'onSubmit' event handler.
  This is done with the following syntax:
  <form ... onsubmit="return checkSearch();">
*/



/* Alert for future links */

function futureLinks()
{
  if (!document.getElementsByTagName) return false;
  var future = document.getElementsByTagName("a");
  for (var i=0; i < future.length; i++) {
    if (future[i].className.match("future")) {
      future[i].onclick = function() {
        alert("This resource is still under construction, and is not yet ready for viewing.\n\nSorry for any inconvenience this may have caused.");
        return false;
      }
    }
  }
}
addLoadEvent(futureLinks);



/* Cookie functions */

function cookie() {}

cookie.isEnabled = function()
{
  return window.navigator.cookieEnabled;
}

cookie.getCookie = function(name)
{
  var cs = document.cookie;
  var start = cs.indexOf(name + "=");
  var len = start + name.length + 1;
  if ((!start ) && (name != cs.substring(0, name.length ))) {
    return null;
  }
  if (start == -1) {
    return null;
  }
  var end = cs.indexOf(";", len);
  if (end == -1) {
    end = cs.length;
  }
  return unescape(cs.substring(len, end));
}

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

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