// This section is a browser detector. For IE5+, NN5+, Opera6+, Konqueror5+ use absolute positioning.

var name = window.navigator.appName, ver = window.navigator.appVersion, agent = window.navigator.userAgent, abs = false;
var opera = agent.indexOf("Opera");
if (opera != -1) {
	if (agent.charAt(opera + 5) == "/") opera++;
	if (parseInt(agent.substr(opera + 5)) >= 6) abs = true;
}
else if (name == "Netscape" && parseInt(ver) > 4) {
	abs = true;
}
else if (name == "Microsoft Internet Explorer") {
	var temp = ver.split("MSIE");
	if (parseInt(temp[1]) >= 5) abs = true;
}
else if (name == "Konqueror" && parseInt(ver) >= 5) {
    abs = true;
}
	
if (abs) document.writeln('<link rel="stylesheet" type="text/css" href="images/cbc_style_abs.css">');

//--------------------------------------------------------------------------------------------------------------------------------------

// 'btn' is a user defined object that stores an element's id and the stepsize of it's shading
// ie: positve for brightening, negative for darkening. 'btns' is a generic object used to
// form an associative array of btn type objects. The 'on' variable determines whether the
// shading routine is cycling or not.

var brighten = 0x333333;
var darken = -0x080808;
var on = false;
var pow = new Array(1048576, 65536, 4096, 256, 16, 1);
var hex = new Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
var btns = new Object;

function btn(id, dir)
{
	this.id = id;
	this.dir = dir;
}

function rollon_rolloff(e)
{
	var type, dir = 0, o;
	if (document.all) {
		type = window.event.type;
		o = window.event.srcElement;
	}
	else if (e != null) {
		type = e.type;
		o = e.target;
		if (o.className != "glow") o = e.target.parentNode;
	}
	
	// Only handle "mouseover" and "mouseout" events for 'glow' class elements.
	if (e.type == "mouseover") dir = brighten;
	if (e.type == "mouseout") dir = darken;

	if (dir != 0) {
		if (o.className == "glow") {
			if (!btns[o.id]) {
			    btns[o.id] = new btn(o.id, 0);
			    o.style.color = "#000000";
			}
			btns[o.id].dir = dir;
			if (!on) shade();
		}
	
		if (!document.all) return routeEvent(e);
	}
}

// rgbToHex returns a string; replaces rgb values (e.g rgb(xxx,x,xx)) 
// with hex values. All the rest of the string is left intact
function rgbToHex(rgbInput){
    var r, g, b;
    var commaFirst, commaSec;
    var output;
    if(rgbInput.indexOf("rgb(") == -1) return rgbInput;

    var tempStr = rgbInput.substring(rgbInput.indexOf("rgb("));
    var tempStrIndex = rgbInput.indexOf(tempStr);
	
    var rgb = rgbInput.substring(tempStrIndex + 4, 
        tempStrIndex + tempStr.indexOf(")"));

    commaFirst = rgb.indexOf(",");
    commaSec = rgb.lastIndexOf(",");

    r = rgb.substring(0, commaFirst);
    g = rgb.substring(commaFirst + 1, commaSec);
    b = rgb.substring(commaSec + 1);

    output = rgbInput.substring(0, rgbInput.indexOf("rgb("));
    output += "#" + toHex(r, 2) + toHex(g, 2) + toHex(b, 2);
    output += rgbInput.substring(tempStrIndex + tempStr.indexOf(")") + 1);
    if(output.indexOf("rgb(") > 0) output= rgbToHex(output);
    return output;
}

// Convert to hex and insure n digits. toString(16) does not work on all browsers.
function toHex(dec, ln) {
    var num = parseInt(dec);
    var i, n, result = '';
    
    for (i = 0; i < 6; i++) {
        n = Math.floor(num / pow[i]);
        if (result.length > 0 || n > 0) result += hex[n];
        num -= n * pow[i];
    }
    
    for (i = 0; i < ln - result.length; i++) { result = '0' + result; }
    
    return result;
}
        
// A continuous loop that increases/decreases the brightness of an array of elements.
// The loop stops when all elements have reached their minimum/maximum brightness.
function shade()
{
	var o, clr, id, dir, temp;
	on = false;
	for (id in btns) {
		dir = btns[id].dir;
		if (dir != 0) {
			on = true;
			o = document.getElementById(id);
			if (!o.style.color || o.style.color == "") o.style.color = "#000000";  //NN does not initialize.
			clr = ("0x" + rgbToHex(o.style.color).substr(1)) - 0 + dir; //NN uses rgb(nnn,n,nn) format, coerce string to number.

			btns[id].level += dir;
			if (dir > 0) {
				if (clr >= 0xffffff) {
					o.style.color = "#ffffff";
					btns[id].dir = 0;
				}
				else o.style.color = "#" + toHex(clr, 6);
			}
			else {
				if (clr <= 0x000000) {
					o.style.color = "#000000";
					btns[id].dir = 0;
				}
				else o.style.color = "#" + toHex(clr, 6);
				
			}
		}
	}
	if (on) setTimeout("shade()", 50);
}

//-----------------------------------------------------------------

// numDays - sets the proper number of option tags for select tag id_day according to select tags id_mon & id_year
function numDays(id_mon, id_day, id_year) {
        var dt = new Date();
	var sel_day = document.getElementById(id_day)
	var m = document.getElementById(id_mon).selectedIndex + (document.getElementById(id_mon).length < 12 ? dt.getMonth() : 0);
	var y = (id_year == 0 ? dt.getFullYear() : document.getElementById(id_year)[document.getElementById(id_year).selectedIndex].value);
	var days = 32 - new Date(y, m, 32).getDate();

	if (sel_day.length > days) {
	    sel_day.length = days;
	} else if (sel_day.length < days) {
		var opt;
		for (var i = sel_day.length; i < days; i++) {
			opt = document.createElement("OPTION");
			opt.value = i + 1;
			opt.text = i + 1;
			sel_day.options[i] = opt;
		}
	}
}

//-----------------------------------------------------------------------

// Installs global handlers for "onmouseover" and "onmouseout" events and disables the a:hover pseudo-class stylesheet.
// If the browser doesn't have this capability, a:hover is left intact.

if (document.all && document.attachEvent && opera == -1) {
	document.attachEvent("onmouseover", rollon_rolloff);
	document.attachEvent("onmouseout", rollon_rolloff);
	document.styleSheets[0].disabled = true;
}
else if (document.captureEvents && opera == -1)
{
	document.captureEvents(Event.ONMOUSEOVER | Event.ONMOUSEOUT);
	document.onmouseover = rollon_rolloff;
	document.onmouseout = rollon_rolloff;
	document.styleSheets[0].disabled = true;
}

function loadfunc() {
	if (window.goFadeSequence) goFadeSequence();
}
