// console.log('js_codelib.js');

//##########################################################
//###################### DHTML LIBRARY #####################
//##########################################################
//v1.7

//DOM sniffer for v4+ browsers
ns6 = false;
ns4 = (document.layers)? true:false;
ie4 = (document.all)? true:false;
opera = (window.opera)? true:false; //for opera, "ie4" and "opera" are true
mac = 	((navigator.appVersion.indexOf("Mac") != -1) || (navigator.appVersion.indexOf("PowerPC") != -1))? true:false;
//if the DOM is none of the above, it must be NS6
if (ns4 == ie4 && opera == false) {
	ns6 = true;
	ie4 = ns4 = false;
}

//initialise a layer object depending on browser
function layer_init(name) {
	if (ie4) return eval(name+'.style');
	if (ns4) return eval('document.'+name);
	if (ns6) return eval('document.getElementById("'+name+'").style');
}

//###### the following functions use the object reference returned from the layer_init() function

//switch object visibility on
function show_object(obj) {
	if (ns4) obj.visibility = "show"
	else if (ie4 || ns6) obj.visibility = "visible";
}

//switch object visibility off
function hide_object(obj) {
	if (ns4) obj.visibility = "hide"
	else if (ie4 || ns6) obj.visibility = "hidden";
}

//move object to a certain coordinate
function move_to(obj,x,y) {
	obj.xpos = x;
	obj.left = obj.xpos;
	obj.ypos = y;
	obj.top = obj.ypos;
}

//move object a given number of pixels
function move_by(obj,x,y) {
	obj.xpos += x;
	obj.left = obj.xpos;
	obj.ypos +=y;
	obj.top = obj.ypos;
}

//clip object
function object_clip(obj,xmin,ymin,xmax,ymax) {
	if (ie4 || ns6) {
		cstring = 'rect(' + ymin + 'px ' + xmax + 'px ' + ymax + 'px ' + xmin + 'px)';
		obj.clip = cstring;
	}
	if (ns4) {
		obj.clip.top = ymin;
		obj.clip.right = xmax;
		obj.clip.bottom = ymax;
		obj.clip.left = xmin;
	}
}

//###### the following functions need the original layer name as string, not an object reference

//write layer content
function write_to_layer(lay,txt) {
	if (ie4) {
		document.all[lay].innerHTML = txt;
	}
	if (ns4) {
		document[lay].document.write(txt);
		document[lay].document.close();
	}
	if (ns6) {
		over = document.getElementById([lay]);
		range = document.createRange();
		range.setStartBefore(over);
		domfrag = range.createContextualFragment(txt);
		while (over.hasChildNodes()) {
			over.removeChild(over.lastChild);
		}
		over.appendChild(domfrag);
	}
}

//return the height of the layer in pixels
function layer_height(lay) {
	if(ie4 && !opera) return eval(lay+'.clientHeight');
	if(ns4) return eval('document.'+lay+'.document.height');
	if(ns6) return eval('document.getElementById("'+lay+'").offsetHeight');
	if(opera) return eval(lay+'.style.pixelHeight');
}

//return the width of the layer in pixels
function layer_width(lay) {
	if(ie4 && !opera) return eval(lay+'.clientWidth');
	if(ns4) return eval('document.'+lay+'.document.width');
	if(ns6) return eval('document.getElementById("'+lay+'").offsetWidth');
	if(opera) return eval(lay+'.style.pixelWidth');
}

//################## END OF DHTML LIBRARY ##################




//additional functions to restore page after a resize event on ns4
// to use, put onResize="restore()" into body tag
// this function needs the dom sniffer in the DHTML library above
function restore() {
	if (ns4) {
		if (navigator.appVersion.indexOf("4.08") != -1) {
			if (GetCookie('mmhilresize') == null) {
				SetCookie('mmhilresize','done');
				this.location.reload();
			} else {
				DeleteCookie('mmhilresize');
			}
		} else {
			this.location.reload();
		}
	} else {
		check_win_size();
		place_objects();
	}
}

//on opera we call this function to check the window size every second
function opera_onresize() {
	op_ow = win_width;
	op_oh = win_height;
	check_win_size();
	if (op_ow != win_width  ||  op_oh != win_height) {
		restore();
	}
	timer_opera_onresize = setTimeout('opera_onresize()',1000);
}


//check the window size
//this function can only be called if the pageload has progressed into the body of the page or later if the pageload is completed
var win_width = 0;
var win_height = 0;
function check_win_size() {
	//needs DOM sniffer output from dhtml library
	if (parseInt(navigator.appVersion)>3) {					//sniff window dimensions
		if (ie4 && !opera) {			//ie
			win_width = document.body.offsetWidth;
			win_height = document.body.offsetHeight;
		} else {					//NS or opera
			win_width = window.innerWidth;
			win_height = window.innerHeight;
		}
	}
	if (mac){
		//mac browsers give window width without scrollbar width, we have to adjust for that
		win_width = win_width + 20;
	}
}


//##########################################################
//##################### COOKIE LIBRARY #####################
//##########################################################

var expHours = 1;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expHours*60*60*1000));

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;
}

// due to several issues with cookie not sticking around, we hardcoded some of the values in this function
function SetCookie (name, value) {  
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + "=" + escape (value) + 
	"; expires=" + exp.toGMTString() + 
	"; path=/" +  
	((domain == null) ? "" : ("; domain=" + domain)) +    
	((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {  
	var exp = new Date();  
	exp.setTime (exp.getTime() - 1);  
	document.cookie = name + "=" + '' + "; expires=" + exp.toGMTString();
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

//##################### END OF COOKIE LIBRARY #####################