function freundschaftsanfrage_senden( uid, redir )
{
	var nachricht = window.prompt( "Bitte gib eine kurze Nachricht für den Empfänger ein." );
	var url = '/freundschaftsanfrage-senden.php?uid=' + uid + '&nachricht=' + encodeURIComponent( '' + nachricht ) + '&redir=' + redir;
	window.location.href = url;
}

function call_page( url, confirm )
{
	var cont = true;
	if ( typeof( confirm ) != 'undefined' && confirm )
	{
		cont = window.confirm( confirm );
	}
	if ( cont )
	{
		window.location.href = url;
	}
}

function umfrage_submit( form )
{
	$.get( '/ajax/umfrage.php', $(form).serialize(), function( data ) {
		if ( data )
		{
			$('#' + form.id ).replaceWith( data );
		}
		else
		{
			alert( 'Es ist leider ein Fehler aufgetreten.' );
		}
	} );
	return false;
}

function set_ol_order( sel )
{
	var order = $(sel).val();
	var url = $('#onlinelist').attr( 'src');
	url = url.replace( /order=\w*/, 'order=' + order );
	$.get( '/setui.php', {userlist_order: order} );
	$('#onlinelist').attr( 'src', url );
}

function story_text_size( obj )
{
	$('.story-text').css('font-size', $(obj).css('font-size') );
}

function story_text_color( obj )
{
	$('.story-container').css('background-color', $(obj).css('background-color') );
	$('.story-container').css('color', $(obj).css('color') );
	$('.story-container').css( 'font-family', $(obj).css( 'font-family' ) );
}

function galerie_ausblenden( id )
{
	$.post( '/galerie-ausblenden.php', {id: id}, function( data ) {
		if ( data == 'OK' )
		{
		    $( '#kat_' + id ).fadeOut( 'slow' );
		}
	} );
}

function post_status ( form )
{
	form.elements['b'].disabled = true;
	$.post( '/wixlog-post.php', $(form).serialize(), function (data) {
	        if ( data == 'OK' )
	        {
	            form.elements['kommentar'].value = '';
			}
			else
			{
			    $('#wixlog-error').text( data ).show().delay(2000).fadeOut('slow').text('');
			}
			form.elements['b'].disabled = false;
			update_lateststatus();
		} );
}

function update_lateststatus( first )
{
	if ( typeof( update_lateststatus.running ) == 'undefined' )
	{
	    update_lateststatus.running = false;
	}
	if ( !update_lateststatus.running )
	{
		update_lateststatus.running = true;
		$.get( '/wixlog-getlatest.php?' + ( first?'first=true':'' ), function ( data ) {
		    if ( data.length > 0 )
		    {
		        $('#wixlog-newestmsg').html( data + ' <a style="float:right;" href="/wixlog.html">mehr&hellip;</a>' );
			}
	        update_lateststatus.running = false;
		} );
	}
}

function open_chat2( room_id, as_admin )
{
	var adm_string = '';
	
	if ( typeof( room_id ) == 'undefined' )
	{
	    room_id = 'r5';
	}
	if ( typeof( as_admin ) != 'undefined' && as_admin )
	{
	    adm_string = '&admin=true';
	}
	openWin( '/chat/chat.php?room=' + room_id + adm_string, -50, -10, false );
}

function menu_open(id)
{
    menu_close(menu_open._current);
	var elem = document.getElementById(id+'_subitems');
	if ( elem )
	{
	    elem.style.display = 'block';
		menu_open._current = id;
	}
}

function menu_close(id)
{
	if ( id )
	{
		var elem = document.getElementById(id+'_subitems');
		if ( elem )
		{
		    elem.style.display = 'none';
		    menu_open._current = null;
		}
	}
}

function longrun_action(the_button, new_text)
{
	the_button.disabled = true;
	the_button.value = new_text;
	the_button.form.submit();
	return true;
}


function menu_toggle_pin()
{
	$('#main_menu').toggleClass('pinned');
	$.get('/pinmenu.php?pinned='+($('#main_menu').hasClass('pinned')?'1':'0'));
}

function jq_toggle(id,src)
{
	var elem = $('#'+id);
	$(src).toggleClass('collapsed',elem.css('display')!='none').toggleClass('expanded',elem.css('display')=='none');
	elem.slideToggle('slow',
		function() {
			$.get('/togglebox.php?id='+id+'&display='+this.style.display);
		}
	);
}

function setFields(self,name)
{
	var form = self.form;
	for ( i=0; i<form.elements.length; i++ )
	{
		if ( (form.elements[i].name == name) && (form.elements[i].type == 'checkbox') )
		{
			form.elements[i].checked = self.checked;
		}
	}
}

function openWin(url,width,height,scrollbars,target)
{
	var top,left;
   	if ( typeof(target) == 'undefined' )
	{
	   target = '_blank';
	}
	if ( typeof(width) == 'undefined' )
	{
      	width = Math.round(screen.availWidth * 2 / 3);
   	}
	else if ( width <= 0 )
   	{
    	left = -width;
      	width = screen.availWidth + 2*width;
   	}
   	if ( typeof(height) == 'undefined' )
	{
    	height = Math.round(screen.availHeight * 2 / 3 );
   	}
	else if ( height <= 0 )
	{
    	top = -height;
		height = screen.availHeight + 2*height;
   	}
   	var flags = '';
   	if (window.innerWidth)
	{
    	flags += 'innerWidth='+width+',innerHeight='+height+',';
   	}
	else
	{
    	flags += 'width='+width+',height='+height+',';
   	}
	flags += 'location=no,status=no,menubar=no,toolbar=no,resizable=yes';
	if ( scrollbars )
	{
		flags += ',scrollbars=yes';
	}
	else
	{
		flags += ',scrollbars=no';
	}

	if ( top != 0 )
	{
		flags += ',top='+top;
	}
	if ( left != 0 )
	{
		flags += ',left='+left;
	}

	try
	{
   		var win = window.open(url,target,flags);
    	win.focus();
      	try
		{
      		win.resizeTo(width,height);
		}
		catch(Exception)
		{
			// do nothing, fail silently
		}
	}
	catch ( Exception )
	{
	    alert('Bitte Popup-Blocker deaktivieren!');
	}
	return false;
}

var activeElement = null;

function storeCaret ( elem )
{ 
	if ( elem.type == 'textarea' )
	{
		activeElement = elem;
		if ( elem.createTextRange )
		{
   	   		elem.caretPos = document.selection.createRange().duplicate();
   		}
	}
} 

function insertAtCaret(tag1,tag2) { 
   if ( (activeElement) && (activeElement.type == 'textarea') ) {
      if(activeElement.createTextRange && activeElement.caretPos) { 
			selectedText = activeElement.caretPos.text; 
			if (selectedText.length > 0) {
			   activeElement.caretPos.text = tag1 + selectedText + tag2; 
			} else {
			   activeElement.caretPos.text = tag1 + ' ' + tag2; 
         }
		} else if (activeElement.setSelectionRange) {
		   var startPos = activeElement.selectionStart;
		   var endPos = activeElement.selectionEnd;
		   var fullText = activeElement.value;
		   var firstPart = fullText.substr(0,startPos);
         var selectedText = fullText.substr(startPos,endPos-startPos);
         var lastPart = fullText.substr(endPos,fullText.length-endPos);
         activeElement.value = firstPart + tag1 + selectedText + tag2 + lastPart;
		} else {
			activeElement.value = activeElement.value + ' '+tag1 + ' ' + tag2+' '; 
		}
		activeElement.focus(); 
	}
}

function insertLinkAtCaret() {   
   var url = window.prompt('Bitte gib die URL des Links ein (inkl. http://)','http://');
   if ( (url.substr(0,7) == 'http://') || (url.substr(0,8) == 'https://') ) {
      var desc = window.prompt("Seitentitel (kann auch leer gelassen werden)",'');
      if (desc.length == 0) desc = url;
      var code = '[url='+url+']'+desc+'[/url]';
      insertAtCaret(code,'');
   }
}

function printEditorButtons() {
   document.write('<div style="border:1px solid #cccccc; padding:2px; background-color:#ffffff;" >');
   document.write(' <input type="button" onClick="insertAtCaret(\'[b]\',\'[/b]\'); return false;" style="font-weight:bold" value="Fett" /> ');
   document.write(' <input type="button" onClick="insertAtCaret(\'[i]\',\'[/i]\'); return false;" style="font-style:italic" value="Kursiv" /> ');
   document.write(' <input type="button" onClick="insertAtCaret(\'[u]\',\'[/u]\'); return false;" style="text-decoration:underline" value="Unterstrichen" /> ');
   document.write(' <input type="button" onClick="insertAtCaret(\'[quote]\',\'[/quote]\'); return false;" value="Zitat" /> ');
   document.write(' <input type="button" onClick="insertLinkAtCaret(); return false;" value="Link" /> ');
   document.write('</div>');
}

function checkForThumb(sender) {
   var result = true;
   if (sender.elements['bild'].value) {
      if (sender.elements['bild'].value.length == 0) {
         alert('Du musst ein Bild für den Upload angeben. Wenn du einen Videoclip hochlädst, muß das Bild aus dem Videoclip stammen (Vorschaubild)!');
         result = false;
      }
   }
   return result;
}

function isOneChecked(form,fieldname)
{
	var result = false;
	for ( var i=0; i<form.elements.length; i++ )
	{
	    if (form.elements[i].name == fieldname)
	    {
			try
			{
	        	result |= form.elements[i].checked;
			}
			catch ( e )
			{
			    // NO-OP
			}
		}
		if ( result )
		{
		    break;
		}
	}
	return result;
}


