function setCookie(n, v) {
	document.cookie = n+'='+v+'; path=/';
}

function setCookieTime(n, v, t) {
	var a = new Date();
	a = new Date(a.getTime() + t);
	document.cookie = n + '=' + v + '; expires=' + a.toGMTString() + '; path=/';
}

function getCookie(n) {
	a = document.cookie;
	res = '';
	while(a != '') {
		while(a.substr(0,1) == ' '){
			a = a.substr(1,a.length);
		}
		cookiename = a.substring(0, a.indexOf('='));
		if(a.indexOf(';') != -1) {
			cookiewert = a.substring(a.indexOf('=') + 1, a.indexOf(';'));
		} else {
			cookiewert = a.substr(a.indexOf('=') + 1, a.length);
		}
		
		if(n == cookiename) {
			res = cookiewert;
		}
		i = a.indexOf(';') + 1;
		if(i == 0) {
			i = a.length;
		}
		a = a.substring(i, a.length);
	}
	return(res)
}

function delCookie(n) {
	document.cookie = n + '=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/';
}

function handleKeyPress(e) {
	var tcode;

    if(!e){
		e = window.event;
	}
	if (e.which) {
		tcode = e.which;
	} else if (e.keyCode) {
		tcode = e.keyCode;
	}

	if(nav_prev != '' && tcode == 37) {
		self.location = nav_prev;
	} else if(nav_next != '' && tcode == 39) {
		self.location = nav_next;
	} else if(nav_up != '' && tcode == 38) {
		self.location = nav_up;
	}
}

function showTab(what) {
	$("#tab_events_p_left").addClass('tab_left_inactive');
	$("#tab_events_p_main").addClass('tab_main_inactive');
	$("#tab_events_p_right").addClass('tab_right_inactive');
	
	$("#tab_events_c_left").addClass('tab_left_inactive');
	$("#tab_events_c_main").addClass('tab_main_inactive');
	$("#tab_events_c_right").addClass('tab_right_inactive');
	
	if(what == 1) {
		$("#tab_events_p_left").removeClass('tab_left_inactive');
		$("#tab_events_p_main").removeClass('tab_main_inactive');
		$("#tab_events_p_right").removeClass('tab_right_inactive');
		$("#tab_events_p_left").addClass('tab_left_active');
		$("#tab_events_p_main").addClass('tab_main_active');
		$("#tab_events_p_right").addClass('tab_right_active');
		$('#events_c').css('display', 'none');
		$('#events_p').css('display', 'table-cell');
	} else {
		$("#tab_events_c_left").removeClass('tab_left_inactive');
		$("#tab_events_c_main").removeClass('tab_main_inactive');
		$("#tab_events_c_right").removeClass('tab_right_inactive');
		$("#tab_events_c_left").addClass('tab_left_active');
		$("#tab_events_c_main").addClass('tab_main_active');
		$("#tab_events_c_right").addClass('tab_right_active');
		$('#events_p').css('display', 'none');
		$('#events_c').css('display', 'table-cell');
	}
}

function val(id) {
	return $("#" + id).attr('value');
}

function showDialog(title, msg) {
	var dialog_html = '<h1>' + title + '</h1>' + msg + '<div class="footer"><input type="button" value="schliessen" id="close" onclick="tb_remove();" /></div>';
	tb_show('', '#TB_html?height=200&width=300&modal=true&html=' + escape(dialog_html));
}

function textRemLength(textfield, counterfield, limit) {
	if (textfield.value.length > limit)
		textfield.value = textfield.value.substring(0, limit);
	else
		counterfield.value = limit - textfield.value.length;
}

function insertText(field, content) {
	var contentField = document.getElementById(field);

	if(content) {
		//IE support
		if (document.selection) {
			contentField.focus();
			sel = document.selection.createRange();
			sel.text = content;
			document.sqlform.insert.focus();
		}
		//MOZILLA/NETSCAPE support
		else if (contentField.selectionStart || contentField.selectionStart == "0") {
			var startPos = contentField.selectionStart;
			var endPos = contentField.selectionEnd;
			var contentField_old = contentField.value;

			contentField.value = contentField_old.substring(0, startPos) + content + contentField_old.substring(endPos, contentField_old.length);
		} else {
			contentField.value += content;
		}
	}
}

function insertSports(from) {
	var selectBox = document.getElementById(from);
	var retVal = '';
	for(i = 0; i < selectBox.length; i++) {
		if(selectBox[i].selected) {
			retVal += '\n- '+ selectBox[i].value;
		}
	}
	
	insertText('prf_sports', retVal);
}

function smiley(name, text) {
	var field = document.getElementById(name);

	text = text + ' ';
	if (document.selection) {		//IE support
		var temp;
		field.focus();
		sel = document.selection.createRange();
		temp = sel.text.length;
		sel.text = text;
		if (text.length == 0) {
			sel.moveStart('character', text.length);
			sel.moveEnd('character', text.length);
		}else{
			sel.moveStart('character', -text.length + temp);
		}
		sel.select();
	}else if (field.selectionStart || field.selectionStart == '0') {	//MOZILLA/NETSCAPE support
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		field.value = field.value.substring(0, startPos) + text + field.value.substring(endPos, field.value.length);
		field.selectionStart = startPos + text.length;
		field.selectionEnd = startPos + text.length;
		field.scrollTop=field.scrollHeight;
	} else {
		field.value += text;
	}

	field.focus();
}

function getPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function getPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y)
		curtop += obj.y;
	return curtop;
}

// find out if ie runs in quirks mode
var docEl = (
             typeof document.compatMode != "undefined" && 
             document.compatMode        != "BackCompat"
            )? "documentElement" : "body";

/* from: http://www.wendenburg.de/jstipps/mousepos.php */
function getMousePos(e) {
	var pos = new Object();
	
	// position where mousemove fired
    pos.x    =  e? e.pageX : window.event.x;
	pos.y    =  e? e.pageY : window.event.y;
	
	// for ie add scroll position
	if (document.all && !document.captureEvents) {
	    pos.x    += document[docEl].scrollLeft;
	    pos.y    += document[docEl].scrollTop;
    }
    
    // for the dino pass event
    if (document.layers) routeEvent(e);
	
	return pos;
}
