/*  ** version 1.1 **
 Browser sniffing code borrowed from: http://www.webreference.com/tools/browser/javascript.html
 The following bit of javascript code is Free Software[tm]. Licence: GNU/GPL v2.0 or later.
*/
var agt=navigator.userAgent.toLowerCase();
var appVer = navigator.appVersion.toLowerCase();

// *** BROWSER VERSION ***
var is_minor = parseFloat(appVer);
var is_major = parseInt(is_minor);

var iePos  = appVer.indexOf('msie');
if (iePos !=-1) {
  is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)))
  is_major = parseInt(is_minor);
}

var is_opera = agt.indexOf('opera') != -1;

var is_safari = ((agt.indexOf('safari')!=-1)&&(agt.indexOf('mac')!=-1))?true:false;
var is_konq = agt.indexOf('konqueror') != -1;
var is_khtml  = (is_safari || is_konq);

var is_ie   = ((iePos!=-1) && (!is_opera) && (!is_khtml));
var is_ie5dn = (is_ie && is_minor < 5.0);

// *** PLATFORM ***
var is_mac = (agt.indexOf("mac")!=-1);


// Quickly disable all CSS on old versions of IE.
if (is_ie5dn) 
{
  var x = (document.getElementsByTagName) ? document.getElementsByTagName('link') : (document.all) ? document.all.tags('link') : null;
  if (x != null)
  {
    x[0].disabled = true;
    x[1].disabled = true;
  }
}





function setCookie (name, value, path, domain, expires, secure)
{
  if (expires == null) // defaults to 1 year
  {
    var expires = new Date();
    var tmpBase = new Date(0);
    var tmpSkew = tmpBase.getTime();
    if (tmpSkew > 0) { expires.setTime(expires.getTime() - tmpSkew); }
    expires.setTime(expires.getTime() + 365 * 24 * 60 * 60 * 1000);
  }
  var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}



function getCookieValue (theName)
{
  var c = document.cookie;
  if (c && theName && (theName != ""))
  {
    c = ";" + c;
    var regex = eval("/(;|; )" + theName + "=([^;]+)/");
    if (c.match(regex)) { return unescape( c.match(regex)[2] ); }
  }
  return false;
}



function stripHref(theUrl)
{
  var regexp = eval("/" + document.location.protocol + "\\/\\/" + document.location.host + "/");
  return theUrl.replace(regexp, "");
};





//  getElementsBySelector and getAllChildren is written by Simon Willison.
//  For original source see: http://simon.incutio.com/archive/2003/03/25/getElementsBySelector

function getAllChildren(e)
{
  return e.all ? e.all : e.getElementsByTagName('*');
}

document.getElementsBySelector = function(selector) {
  if (!document.getElementsByTagName) {
    return new Array();
  }
  var tokens = selector.split(' ');
  var currentContext = new Array(document);
  for (var i = 0; i < tokens.length; i++) {
    token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');;
    if (token.indexOf('#') > -1) {
      var bits = token.split('#');
      var tagName = bits[0];
      var id = bits[1];
      var element = document.getElementById(id);
      if (tagName && element.nodeName.toLowerCase() != tagName) {
        return new Array();
      }
      currentContext = new Array(element);
      continue;
    }
    if (token.indexOf('.') > -1) {
      var bits = token.split('.');
      var tagName = bits[0];
      var className = bits[1];
      if (!tagName) {
        tagName = '*';
      }
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        for (var j = 0; j < elements.length; j++) {
          found[foundCount++] = elements[j];
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
        if (found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b'))) {
          currentContext[currentContextIndex++] = found[k];
        }
      }
      continue;
    }
    if (token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/)) {
      var tagName = RegExp.$1;
      var attrName = RegExp.$2;
      var attrOperator = RegExp.$3;
      var attrValue = RegExp.$4;
      if (!tagName) {
        tagName = '*';
      }
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        for (var j = 0; j < elements.length; j++) {
          found[foundCount++] = elements[j];
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      var checkFunction;
      switch (attrOperator) {
        case '=': checkFunction = function(e) { return (e.getAttribute(attrName) == attrValue); }; break;
        case '~': checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); }; break;
        case '|': checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); }; break;
        case '^': checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) == 0); }; break;
        case '$': checkFunction = function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); }; break;
        case '*': checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); }; break;
        default : checkFunction = function(e) { return e.getAttribute(attrName); };
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
        if (checkFunction(found[k])) {
          currentContext[currentContextIndex++] = found[k];
        }
      }
      continue;
    }
    tagName = token;
    var found = new Array;
    var foundCount = 0;
    for (var h = 0; h < currentContext.length; h++) {
      var elements = currentContext[h].getElementsByTagName(tagName);
      for (var j = 0; j < elements.length; j++) {
        found[foundCount++] = elements[j];
      }
    }
    currentContext = found;
  }
  return currentContext;
}

// End of code by Simon Willison


function mergeArrays(array1, array2)
{
  var nA = new Array;
  for (var i = 0; i < array1.length; i++) { nA[nA.length] = array1[i]; }
  for (var i = 0; i < array2.length; i++) { nA[nA.length] = array2[i]; }
  return nA;
}
