// reload current window
function reload() { window.location.reload(); }

// Opens new window with certain name, width, heights, left/top coordinates
function openW(wname,wWidth,wHeight,leftCoord,topCoord) {
	var wnew,str_param;
	if (!leftCoord) leftCoord = 100;
	if (!topCoord) topCoord = 100;
	str_param="toolbar=0,menubar=1,location=0,scrollbars=1,width="+wWidth+",height="+wHeight+",resizable=1,left="+leftCoord+",top="+topCoord;			 
	wnew=window.open("",wname,str_param);
	wnew.focus();
	return wnew;
}

// Opens new window with certain name, width, heights, left/top coordinates
function openUrl(e, url, name, width, height, leftOff, topOff) {
	// prevent from bubbling up
	e = (e) ? e : window.event;
	if (e) {
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
	}
	
	var wnew,str_param;
	if (!leftOff) leftCoord = 50;
	if (!topOff) topCoord = 50;
	str_param="toolbar=0,menubar=1,location=0,scrollbars=1,width="+width+",height="+height+",resizable=1,left="+leftOff+",top="+topOff;			 
	wnew=window.open(url,name,str_param);
	wnew.focus();
	return false;
}

function openWindow(e, url) {
	// prevent from bubbling up
	e = (e) ? e : window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	
	wnew=window.open(url);
	wnew.focus();
	return false;
}

function followUrl(e, url) {
	// prevent from bubbling up
	e = (e) ? e : window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	
	window.location=url;
	return false;
}

// Prevent Bubbling to higher elements
function preventBubbling(e) {
	e = (e) ? e : window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

// written by Dean Edwards, 2005
// with input from Tino Zijdel - crisp@xs4all.nl
// http://dean.edwards.name/weblog/2005/10/add-event/
function addEvent(element, type, handler) {
	if (element.addEventListener)
		element.addEventListener(type, handler, false);
	else {
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		if (!element.events) element.events = {};
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			if (element['on' + type]) handlers[0] = element['on' + type];
			element['on' + type] = handleEvent;
		}
	
		handlers[handler.$$guid] = handler;
	}
}
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	if (element.removeEventListener)
		element.removeEventListener(type, handler, false);
	else if (element.events && element.events[type] && handler.$$guid)
		delete element.events[type][handler.$$guid];
}

function handleEvent(event) {
	event = event || fixEvent(window.event);
	var returnValue = true;
	var handlers = this.events[event.type];

	for (var i in handlers) {
		if (!Object.prototype[i]) {
			this.$$handler = handlers[i];
			if (this.$$handler(event) === false) returnValue = false;
		}
	}
	if (this.$$handler) this.$$handler = null;
	return returnValue;
}

function fixEvent(event) {
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
}
fixEvent.preventDefault = function() {
	this.returnValue = false;
}
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
}

// This little snippet fixes the problem that the onload attribute on the body-element will overwrite
// previous attached events on the window object for the onload event
if (!window.addEventListener) {
	document.onreadystatechange = function() {
		if (window.onload && window.onload != handleEvent) {
			addEvent(window, 'load', window.onload);
			window.onload = handleEvent;
		}
	}
}

// Manage cookies create / read / rerase
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// needed to detect the screen size
addEvent(window, 'load', setScreenClass);
addEvent(window, 'resize', setScreenClass);
    //window.onload = setScreenClass; 
	//window.onresize = setScreenClass;

	//  Following transition classes will be declared:
	//
	//	classname		  screenwidth
	//	------------------------------------------
	//	pda_v			  240px			
	//	pda_h			  320px			
	//	ultralow		  320px -  640px	
	//	screen_lo		  640px -  800px	
	//	screen_med		  800px - 1024px	
	//	screen_hi		 1024px - 1280px	
	//	screen_wide				> 1280px			

function setScreenClass(){
	var fmt = document.documentElement.clientWidth;
	var cls = (fmt<=240)?'pda_ver':(fmt>240&&fmt<=320)?'pda_hor':(fmt>320&&fmt<=640)?'screen_ultralow':(fmt>640&&fmt<=800)?'screen_low':(fmt>800&&fmt<=1024)?'screen_med':(fmt>1024&&fmt<=1280)?'screen_high':'screen_wide';
	document.body.className=cls;
};
