
function insertText(field, value){
	
	insertField = document.getElementById(field);

	if (typeof document.selection != 'undefined') {
		insertField.focus();
		selection = document.selection.createRange();
		selection.text = ' ' + value + ' ';
	} else if (insertField.selectionStart || insertField.selectionStart == '0') {
		var startPos = insertField.selectionStart;
		var endPos = insertField.selectionEnd;
		insertField.value = insertField.value.substring(0, startPos) + ((startPos == 0) ? '' : ' ') + value + ' ' + insertField.value.substring(endPos, insertField.value.length);
	} else {
		insertField.value += value;
	}	
	
	return;
}

function setImg(smile) {

	insertText('message', smile);
	checkEntryLength(insertField);
	return;
}

function checkEntryLength(textarea){

	if(textarea.value.length > 800){
		alertBox('Attention!','The maximum message length is set to 800 chars','ok');
		textarea.value = textarea.value.substr(0, 800);
	}
	document.getElementById('chars').innerHTML = '[ '+textarea.value.length+' / 800 Chars ]';
}


function insertItem(item, value){

	switch (item){

		case 'img':
				var text = prompt('Put in the path to the image below.\n\nFormat: http:// ...', '');
				if(!text) return;
				if(text.substr(0,7) != 'http://' || text.length < 15){
					alert('This is no valid imagepath.');
					return;
				}
			break;

		case 'email':
				var text = prompt('Put in an e-mail address below.\n\nFormat: name@domain.tld', '');
				if(!text) return;
				if(text.search(/[a-zA-Z0-9]{2,}@[a-zA-Z0-9]{3,}\.[a-zA-Z]{2,4}/) == -1){
					alert('This is not a valid e-mail address.');
					return;
				}
			break;
		case 'url':
				var text = prompt('Put in yout pagelink below.\n\nFormat: http:// ...','');
				if(!text) return;
				if(text.search(/[a-zA-Z0-9]{3,}\.[a-zA-Z]{2,4}/) == -1){
					alert('This is not a valid path.');
					return;
				} else if(text){
					if(text.substr(0,7) != 'http://'){
						text = 'http://' + text;
					}
					var linkValue = prompt('Put in your linktitle below.','');
				}
			break;
		case 'youtube':
				var text = prompt('Insert your 11-digit YouTube video-ID.','');
				if(!text) return;
				if(text.length != 11){
					alert('Invalid video-ID\nThe ID must contain exactly 11 chars.');
					return;
				}

			break;
		case 'myvideo':
				var text = prompt('Insert the myvideo video-ID (only numbers).','');
				if(!text) return;
				if(parseInt(text) != text){
					alert('Invalid video-ID.');
					return;
				}

			break;
	}
	
	var value = '';

	if(item == 'url'){
		if(linkValue && linkValue.length > 0 && text && text.length > 0){
			value = '['+ item.toUpperCase() +'='+ text.strip() +']' + linkValue.strip() + '[/'+ item.toUpperCase() +']';
		} else if (text && text.length > 0) {
			value = '['+ item.toUpperCase()+ ']' + text.strip() + '[/'+ item.toUpperCase() +']';
		}

	} else {

		if(text && text.length > 0){
			value = '['+ item.toUpperCase() +']' + text.strip() + '[/'+ item.toUpperCase() +']';
		}
	
	}
	
	insertText('message', value);
	
	checkEntryLength(document.getElementById('message'));
	return;
}

function getSelection(field){
	
	var field = document.getElementById(field);
	
	if (typeof document.selection != 'undefined') {
		
		field.focus();
		selectionRange = document.selection.createRange();
		selectionText = selectionRange.text;
			
		return selectionText;
			
	} else if (field.selectionStart || field.selectionStart == '0') {
		
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		selectionText = field.value.substr(parseInt(startPos), parseInt(endPos-startPos));
		
		return selectionText;
		
	} 
		
	return false;
	
}

function insertSpecialText(style){

	if(getSelection('message') == false){	
		if(style == 'b'){
			var text = prompt('Insert bold text below.','');
		} else if(style == 'i'){
			var text = prompt('Insert italic text below.','');
		} else if(style == 'u'){
			var text = prompt('Insert underlined text below.','');
		} else if(style == 'marquee'){
			var text = prompt('Insert ticker text below.','');
		}
	} else {
		var text = getSelection('message');
	}

	if(text && text.length > 0){
		
		value = '['+ style.toUpperCase() +']' + text + '[/'+ style.toUpperCase() +']';
		
		insertText('message', value);
		checkEntryLength(document.getElementById('message'));
	}
}


function checkForm(form){

	var alertContent = '';
	if(form.name == 'enter'){

		if(form.username.value == ''){
			alertContent = '- You dont have entered a name<br/>';
		}
		if(form.message.value == ''){
			alertContent += '- You dont have entered a message<br/>';
		}
		if(form.securitycode && form.securitycode.value== ''){
			alertContent += '- You dont have entered the securitycode<br/>';
		}
	}

	if(alertContent != ''){
		alertContent = '<b>The following errors occurred:</b><br/><br/>' + alertContent;
		alertBox('Attention!', alertContent, 'ok');
		return false;
	}else{
		return true;
	}
}


/**
 * get left position of passed object
 *
 * @param		object	HTML Object
 * @return		int 	position left
 */
function absLeft(element) {
	return (element.offsetParent) ? element.offsetLeft+absLeft(element.offsetParent) : element.offsetLeft;
}

/**
 * get top position of passed object
 *
 * @param		object	HTML Object
 * @return		int		position top
 */
function absTop(element) {
	return (element.offsetParent)? element.offsetTop+absTop(element.offsetParent) : element.offsetTop;
}

/**
 * get right position of passed object
 *
 * @param		object	HTML Object
 * @return		int		position top
 */
function absRight(element) {
	return (absLeft(element) + element.offsetWidth);
}

/**
 * get bottom  position of passed object
 *
 * @param		object	HTML Object
 * @return		int		position top
 */
function absBottom(element) {
	return (absTop(element) + element.offsetHeight);
}


/**
 * get the availible screenwidth (innerwidth)
 *
 * @return		int		screenwidth
 */
function getAvailScreenWidth() {

	var screenWidth;
	if (self.innerWidth){ // all except Explorer
		screenWidth = self.innerWidth;
		if(getDocumentHeight() > getAvailScreenHeight()){
			screenWidth -= 17;
		}
	}
	else if (document.documentElement && document.documentElement.clientWidth){ // Explorer 7 and 6 strict mode
		screenWidth = document.documentElement.clientWidth;
	}
	else if (document.body){ // ie6
		screenWidth = document.body.clientWidth;
		if(getDocumentHeight() > getAvailScreenHeight() && browser == 'ie6'){
			screenWidth -= 17;
		}
	}
	return screenWidth;
}



/**
 * get the availible screenheight (innerheigth)
 *
 * @return		int		screenheight
 */
function getAvailScreenHeight() {

	var screenHeight;
	if (self.innerHeight){ // all except Explorer
		screenHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight){ // Explorer 6 Strict Mode
		screenHeight = document.documentElement.clientHeight;
	}
	else if (document.body){ // other Explorers
		screenHeight = document.body.clientHeight;
	}
	return screenHeight;
}


/**
 * How much the page has scrolled
 *
 * @return		int		scrollYOffset
 */
function getScrollYOffset() {

	var scrollOffset;
	if (self.pageYOffset){ // all except Explorer
		scrollOffset = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict Mode
		scrollOffset = document.documentElement.scrollTop;
	}
	else if (document.body){ // other Browsers
		scrollOffset = document.body.scrollTop;
	}
	return scrollOffset;
}


/**
 * get the document height
 *
 * @return		int		documentheight
 */
function getDocumentHeight() {

	var pageHeight;
	var var1 = document.body.scrollHeight;
	var var2 = document.body.offsetHeight

	if (var1 > var2){ // all exept Explorer Mac
		pageHeight = document.body.scrollHeight;
	}
	else{ // Explorer Mac; would also work in Explorer 6 Strict, Mozilla and Safari
		pageHeight = document.body.offsetHeight;
	}
	return pageHeight;
}



/**
* draw an alternativ alertbox
*
*@param 		string 	title of the box
*@param 		string	content of the box
*@param 		string	label for the button
*
* @return		void
*/
function alertBox(boxTitle, boxContent, buttonLabel){

	if(!$('alertbox')){

		var alertouterbox = Builder.build('<div class="alertouterbox" id="alertbox"></div>');
		var alertBox = alertouterbox;

		if($('top')){
			$('top').appendChild(alertouterbox);
		}else{
			document.body.appendChild(alertouterbox);
		}
		alertouterbox.innerHTML = '<div class="alertboxtitle" id="alerttitle"></div><div class="alertbox"><span id="alertcontent"></span><center><input type="button" id="alertbutton" class="alertbutton" onclick="$(\'alertbox\').hide();removeInactiveLayer(\'inactivelayer\')" /></center></div>';
	}else{
		var alertBox = $('alertbox');
	}

	alertBox.show();
	showInactiveLayer('inactivelayer');
	window.setTimeout("$('alertbutton').focus()",300);

	screenHeight = getAvailScreenHeight();
	screenWidth = getAvailScreenWidth();
	scrollOffset = getScrollYOffset();
	alertBoxWidth = alertBox.offsetWidth;
	alertBoxHeight = alertBox.offsetHeight;

	alertBox.style.left = (screenWidth / 2) - (alertBoxWidth / 2) + 'px' ;
	alertBox.style.top = (((screenHeight / 2) - (alertBoxHeight / 2) + scrollOffset) - 50) + 'px' ;
	alertBox.style.zIndex = 100;

	$('alerttitle').innerHTML = boxTitle;
	$('alertbutton').value = buttonLabel;
	$('alertcontent').innerHTML = boxContent;

	document.onkeypress = function(key){
		if(window.event){
			if(window.event.keyCode == 13){
				$('alertbox').hide();
				removeInactiveLayer('inactivelayer');
				document.onkeypress = '';
			}
		} else {
			if(key.which == 13){
				$('alertbox').hide();
				removeInactiveLayer('inactivelayer');
				document.onkeypress = '';
			}
		}
	}
}


/**
* shows the grey inactivelayer
*
* @param 		string	layerid
* @param 		function	that function is called when layer is clicked
*
* @return		void
*/
function showInactiveLayer(layerid, callbackFunktion){

	var scrollOffset = getScrollYOffset();

	if($(layerid) == null || typeof($(layerid)) == 'undefined'){
		//creates an inactivelayer
		var inactivLayer = Builder.build('<div id="'+layerid+'" style="position:absolute;top:0;left:0;width:2300px;height:1500px;z-index:99;display:block;background:#000000" class="transparent70"></div>');
		//append it to pagecontent
		if(document.body){
			document.body.appendChild(inactivLayer);
		}
	}else{
		$(layerid).show();
	}

	var inactiveLayer = $(layerid);

	document.documentElement.style.overflow = 'hidden';
	document.documentElement.scrollTop = scrollOffset;

	if(typeof(callbackFunktion) == 'function'){
		inactiveLayer.onclick = function(){
			callbackFunktion();
		};
	}
	return;
}

/**
* remove the grey inactivelayer
*
* @param 		string   layerid
*
* @return		void
*/
function removeInactiveLayer(layerid){

	if($(layerid)){
		$(layerid).hide();
	}

	document.documentElement.style.overflow = 'auto';

	return;
}


/**
 * shows tooltip box
 *
 * @param		object	element: onclick handle
 * @param		string	content: tooltip content
 * @param		string	align of tooltip (left,right,top,bottom)e
 * @param		int		left offset in px
 * @param		int		top offset in px
 * @param		int		if 1 diasabling HTML code
 * @return		void
 */

var tooltiptimer;

function showTooltip(element, content, align, alignXOffset, alignYOffset, disableHtml, maxwidth) {

	var elementHeight = element.offsetHeight;
	var elementWidth = element.offsetWidth;
	var tooltipBox = document.getElementById('tooltip');
	var tooltipContent = document.getElementById('tooltipcontent');

	// set content
	if(disableHtml==1){
		content = htmlEntities(content);
		//encode breaks back(<br>)
		content = content.replace(/&lt;br\/&gt;/g,"<br/>");
	}

	tooltipContent.innerHTML = content

	if(typeof(maxwidth) != 'undefined'){
		tooltipBox.style.width = maxwidth + 'px';
	}else{
		tooltipBox.style.width = 'auto';
	}

	var boxWidth = tooltipBox.offsetWidth;
	var boxHeight = tooltipBox.offsetHeight;

	//clear timer, prevent start of a parallel timer
	window.clearTimeout(tooltiptimer);


	var elementTop = absTop(element);
	var elementLeft = absLeft(element);
	var alignXOffset = (alignXOffset) ? alignXOffset : 0;
	var alignYOffset = (alignYOffset) ? alignYOffset : 0;

	if(navigator.appName == 'Microsoft Internet Explorer' && navigator.appVersion.search(/MSIE 6.0/) != -1 && $('statebar')){
		// the height of the statebar
		elementTop -= 30;
	}

	// set position (align) of tooltip box

	switch(align) {

		case 'left':
			tooltipBox.style.top = (elementTop + ((elementHeight - boxHeight) / 2) + alignYOffset) +'px';
			tooltipBox.style.left = elementLeft - (boxWidth - alignXOffset) + 'px';
			break;
		case 'top':
			tooltipBox.style.top = elementTop - (boxHeight + alignYOffset) + 'px';
			tooltipBox.style.left = (elementLeft + ((elementWidth - boxWidth) / 2) + alignXOffset) + 'px';
			break;
		case 'right':
			tooltipBox.style.top = (elementTop + ((elementHeight - boxHeight) / 2) + alignYOffset) + 'px';
			tooltipBox.style.left = elementLeft + (elementWidth + alignXOffset) + 'px';
			break;
		case 'bottom':
			tooltipBox.style.top = elementTop  + (elementHeight + alignYOffset) + 'px';
			tooltipBox.style.left = (elementLeft + ((elementWidth - boxWidth) / 2) + alignXOffset) + 'px';
			break;
		default:
			break;
	}

	// set visibility if box has content
	if(content){
		tooltipBox.style.visibility = 'visible';
	}

}


/**
* closes tooltip box
*
*@param 		int 	timeout before closing tooltip
*@param 		bool
*
* @return		void
*/
function closeTooltip(timeout,closeing){

	if(closeing == 1) {
		document.getElementById('tooltip').style.visibility = 'hidden';
		document.getElementById('tooltipcontent').innerHTML = '';

	} else {
		var timeout = (timeout) ? timeout : 50;
		//clear timer, prevent start of a parallel timer
		window.clearTimeout(tooltiptimer);
		tooltiptimer = window.setTimeout('closeTooltip('+ timeout +',1)', timeout);
	}

}



var browser = '';
function getClientVersion(){

	if(navigator.userAgent.search(/Firefox/) != -1){
		browser = 'ff';
	}else if (navigator.appName == 'Microsoft Internet Explorer'){

			if(navigator.appVersion.search(/MSIE 7.?/) != -1){
					browser = 'ie7';
			}else if(navigator.appVersion.search(/MSIE 6.?/) != -1){
					browser = 'ie6';
			}else if(navigator.appVersion.search(/MSIE 5.?/) != -1){
					browser = 'ie5	';
			}

	}else{
		browser = 'other';
	}
}
getClientVersion();

//-->