// Utility functions
var agt=navigator.userAgent.toLowerCase();
var appVer = navigator.appVersion.toLowerCase();
var is_minor = parseFloat(appVer);
var is_major = parseInt(is_minor);
var is_minor = parseFloat(appVer);
var is_major = parseInt(is_minor);
var is_opera = (agt.indexOf("opera") != -1);
var iePos  = appVer.indexOf('msie');
if (iePos !=-1) {
   is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)));  //added missing ; - 030617 - bdn
   is_major = parseInt(is_minor);
}
var is_konq = false;
var kqPos   = agt.indexOf('konqueror');
if (kqPos !=-1) {                 
   is_konq  = true;
   is_minor = parseFloat(agt.substring(kqPos+10,agt.indexOf(';',kqPos)));
   is_major = parseInt(is_minor);
}                                 
var is_safari = ((agt.indexOf('safari')!=-1)&&(agt.indexOf('mac')!=-1))?true:false;
var is_khtml  = (is_safari || is_konq);
var is_ie   = ((iePos!=-1) && (!is_opera) && (!is_khtml));
var is_ie6up = (is_ie && is_minor >= 6);
var is_ie8   = (is_ie && is_major >= 8);

// Retrieve a cookie expiration date in proper format.
// Pass three integer parameters for the number of days, hours,
// and minutes
function getExpDate(days, hours, minutes) {
    var expDate = new Date();
    if (typeof days == "number" && typeof hours == "number" && typeof hours == "number") {
        expDate.setDate(expDate.getDate() + parseInt(days));
        expDate.setHours(expDate.getHours() + parseInt(hours));
        expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
        return expDate.toGMTString();
    }
}

function getCookieVal(offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}

// primary function to retrieve cookie by name
function getCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            return getCookieVal(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
    }
    return null;
}

// store cookie value with optional details as needed
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape (value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

// remove the cookie by setting ancient expiration date
function deleteCookie(name,path,domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function translateEvent(evt) {
    // Return an 'event' on IE and a standards-based browser
    evt = (evt) ? evt : ((window.event) ? window.event : null);
    return evt;
}

function getElementFromEvent(evt) {
    // Returns the element associated with an event.
    evt = translateEvent(evt);
    var elem = null;
    if (evt) {
      elem = (evt.target) ? evt.target : 
              ((evt.srcElement) ? evt.srcElement : null);
    }
    return elem;
}

function loadPage(url) {
  window.location = url;
}

function loadhtml(url, target) {
  document.getElementById(target).innerHTML = 'Loading page...';
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {loadhtmlDone(url, target);};
    req.open("GET", url, true);
    req.send("");
  }
}  

function loadhtmlDone(url, target) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"
      document.getElementById(target).innerHTML = req.responseText;
    } else {
      document.getElementById(target).innerHTML="Load error:\n" + req.statusText + "\n" + req.status;
    }
  }
}

function zapChildNodes(elem, nodetype) {
    var nodes = elem.childNodes;
    if (!nodes) { return; }
    var tmp;
    
    for (var n = 0; n < nodes.length; ++n) {
        if (nodetype) {
            if (nodes[n].nodeType == nodetype) {
                tmp = elem.removeChild(nodes[n]);
            }
        }
        else {
            tmp = elem.removeChild(nodes[n]);
        }
    }
}

