var isNS4 = document.layers ? true : false;
var isIE  = document.all ? true : false;
var isIE6 = isIE && document.getElementById ? true : false;
var isIE4 = isIE && !isIE6 ? true : false;
var isNS6 = !isIE6 && document.getElementById ? true : false;
var isNS  = isNS4 || isNS6;

// ---------------------------------------
//  Include a file dynamically.
//  *** ONLY WORKS IN BODY OF HTML!! ***
// ---------------------------------------

function include(file) {
  document.write('<script language=javascript src="' + file + '"><\/script>');
}

// ----------------------------
//  Parse URL parameters.
// ----------------------------

function get_parameter(name) {
    var query = window.location.search;
    var idx, str;
    idx = query.indexOf(name + "=");
    if (idx == -1) return null;
    str = query.substr(idx, query.length);
    idx = str.indexOf("+");
    if (idx == -1) idx = str.length;
    return unescape( str.substr(name.length + 1, idx - name.length - 1) );
}

// ------------------------------
//  Change location of browser.
// ------------------------------

function redirect(doc, url) {
    if (top.CAN_REPLACE) {
        doc.location.replace(url);
    } else {
        doc.location.href = url;
    }
}

// ----------------------------
//  Get browser window size.
// ----------------------------

function get_window_size() {
    var obj = new Object;
    if (isNS) {
        obj.w = window.innerWidth;
        obj.h = window.innerHeight;
        if (document.height > window.innerHeight - 15) obj.w -= 15;
        if (document.width  > window.innerWidth  - 15) obj.h -= 15;
    } else if (isIE) {
        obj.w = document.body.clientWidth;
        obj.h = document.body.clientHeight;
    } else {
        obj.w = obj.h = 0;
    }
    return obj;
}

function get_window_x_offset() {
    if (isNS)
        return window.pageXOffset;
    if (isIE)
        return document.body.scrollLeft;
    return 0;
}

function get_window_y_offset() {
    if (isNS)
        return window.pageYOffset;
    if (isIE)
        return document.body.scrollTop;
    return 0;
}

// ----------------------------
//  Cookie stuff.
// ----------------------------

function find_cookie(name) {
    var cookie = document.cookie;
    while (cookie != "") {
        var x = cookie.replace(/=.*/, "");
        if (x == name) {
            cookie = cookie.replace(/^[^=]*=/, "");
            cookie = cookie.replace(/ .*/, "");
            cookie = cookie.replace(/;$/, "");
            cookie = unescape(cookie);
            return cookie;
        } else {
            cookie = cookie.replace(/^[^ ]*/, "");
            cookie = cookie.replace(/^ */, "");
        }
    }
    return null;
}

function parse_cookie(cookie, name) {
    var str = cookie;
    while (str != "") {
        var x = str.replace(/:.*/, "");
        if (x == name) {
            str = str.replace(/^[^:]*/, "");
            str = str.replace(/^:/, "");
            str = str.replace(/\|.*/, "");
            return str;
        } else {
            str = str.replace(/^[^\|]*/, "");
            str = str.replace(/^\|/, "");
        }
    }
    return null;
}

// ----------------------------
//  Convert to/from RGB.
// ----------------------------

function parseRGB(str) {
    var obj = new Object;
    str = str.replace(/^#/, "");
    obj.r = parseInt("0x" + str.substr(0,2));
    obj.g = parseInt("0x" + str.substr(2,2));
    obj.b = parseInt("0x" + str.substr(4,2));
    return obj;
}

function RGBify(r, g, b) {
    if (r > 255) r = 255;
    if (g > 255) g = 255;
    if (b > 255) b = 255;
    return( "#" + hexify(r,2) + hexify(g,2) + hexify(b,2) );
}

var HEX = [ "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F" ];

function hexify(dec, n) {
    var hex = "";
    for (var i=0; i<n; i++) {
        hex = HEX[dec & 15] + hex;
        dec = Math.floor(dec/16);
    }
    return(hex);
}

// ----------------------------------
//  Remove underscores from string.
// ----------------------------------

function debar(x) {
    var i;
    while ((i = x.indexOf("_")) >= 0)
        x = x.replace(/_/, " ");
    return x;
}

