function update_auswahl()
 {
  var kategorieAuswahl = document.forms.add_char.char_class;
  var unterkategorieAuswahl = document.forms.add_char.char_home;
  
  //unterkategorieAuswahl.options.length = 0; // DropDown Menü entleeren
  
  if (kategorieAuswahl.options[kategorieAuswahl.selectedIndex].value == "7")
   {
    unterkategorieAuswahl.selectedIndex = 2;
    disable();
   }
  else if (kategorieAuswahl.options[kategorieAuswahl.selectedIndex].value == "8")
   {
    unterkategorieAuswahl.selectedIndex = 2;
    disable();
   }
  else if (kategorieAuswahl.options[kategorieAuswahl.selectedIndex].value == "9")
   {
    unterkategorieAuswahl.selectedIndex = 3;
    disable();
   }
  else if (kategorieAuswahl.options[kategorieAuswahl.selectedIndex].value == "10")
   {
    unterkategorieAuswahl.selectedIndex = 3;
    disable();
   }
  else
   {
    unterkategorieAuswahl.selectedIndex = 0;
    enable();
   }
 }

function enable()
 {
  if (document.all || document.getElementById)
   {
    if (document.add_char.char_home.disabled == true)
     {
      document.add_char.char_home.disabled = false;
     }
   }
 }

function disable()
 {
  if (document.all || document.getElementById)
   {
    if (document.add_char.char_home.disabled == false)
     {
      document.add_char.char_home.disabled=true;
     }
   }
 }

function cleanit(thefield)
{
  if(thefield.defaultValue == thefield.value) {
   thefield.style.color = '#000';
   thefield.value = '';
  }
}

function restoreit(thefield)
{
  if(thefield.value == '') {
    thefield.value = thefield.defaultValue;
    thefield.style.color = '#aaa';
  }
}

function resetit(thefield) 
{
  thefield.value = thefield.defaultValue;
  thefield.style.color = '#aaa';
}
 

cross_0 = new Image();
cross_0.src = "images/Cross_white.png";
cross_1 = new Image();
cross_1.src = "images/Cross_red.png";

tick_0 = new Image();
tick_0.src = "images/Cross_tick.png";
tick_1 = new Image();
tick_1.src = "images/Tick_green.png";

check_0 = new Image();
check_0.src = "images/Check_white.png";
check_1 = new Image();
check_1.src = "images/Check_green.png";

function hiLite(imgID,imgObjName) 
{
  // imgID - Name des auszutauschenden Bildes z.B. foto2
  // imgObjName - Name des Bildes mit dem ausgetauscht wird z.B. i02
  document.images[imgID].src = eval(imgObjName + ".src")
}
  
function checkdigits() {
	var anz = document.edit_form.char_armor_extra.value.length;
	if (anz> 120 ) {
		document.edit_form.char_armor_extra.value = document.edit_form.char_armor_extra.value.substring(0,120);
		rest = 0;
	} else {
		rest = 120-anz;
	}
	document.edit_form.cmt_zeichen.value = rest;
	if (navigator.appName =="Netscape") {
		document.captureEvents(Event.KEYPRESS);document.edit_form.char_armor_extra.onkeypress = checkdigits;
	}
}
	
function checkdigits_2() {
	var anz = document.minibanner.own_title.value.length;
	if (anz > 38 ) {
		document.minibanner.own_title.value = document.minibanner.own_title.value.substring(0,38);
		rest = 0;
	} else {
		rest = 38-anz;
	}
	document.minibanner.cmt_zeichen.value = rest;
	if (navigator.appName =="Netscape") {
		document.captureEvents(Event.KEYPRESS);document.minibanner.own_title.onkeypress = checkdigits;
	}
}

function chkSearch () {
  if( document.search_form.tag.value == document.search_form.tag.defaultValue ) {
    return false;
  }
  else {
    return true;
  }
}

function resSearch () {
  if( !document.search_form.tag.value ) {
    document.search_form.tag.value = document.search_form.tag.defaultValue;
  }
}

function addClass(target, classValue)
{
  if (!hasClass(target, classValue))
  {
    if (target.className == "")
    {
      target.className = classValue;
    }
    else
    {
      target.className += " " + classValue;
    }
  }

  return true;
}

function removeClass(target, classValue)
{
  var removedClass = target.className;
  var pattern = new RegExp("(^| )" + classValue + "( |$)");

  removedClass = removedClass.replace(pattern, "$1");
  removedClass = removedClass.replace(/ $/, "");

  target.className = removedClass;

  return true;
}

function hasClass(target, classValue)
{
  var pattern = new RegExp("(^| )" + classValue + "( |$)");
  
  if (target.className.match(pattern))
  {
  	return true;
  }
  
  return false;
}

function showExtensive(theText)
{
  var divBlock = 'extensive_text_'+ theText
  var legend = 'legend_'+ theText;
  var element = document.getElementById(divBlock);
  
  if (typeof(Effect) == 'undefined' || false) {
  	
	if(document.getElementById(divBlock).style.display == "none") {
		
		document.getElementById(divBlock).style.display = "";
		
		if (document.getElementById(legend).firstChild.nodeValue == 'Textblock zeigen') {
  			document.getElementById(legend).firstChild.nodeValue = 'Textblock schließen';
  		}
	}
	else {
		
		document.getElementById(divBlock).style.display = "none";
		
		if (document.getElementById(legend).firstChild.nodeValue == 'Textblock schließen') {
  			document.getElementById(legend).firstChild.nodeValue = 'Textblock zeigen';
  		}
	}
	
  }
  else {
  
  	if (document.getElementById(divBlock).style.display == "none") {
  		Effect.BlindDown(divBlock, {
  			queue: 'end',
  			duration: 0.5
  		});
  		
  		if (document.getElementById(legend).firstChild.nodeValue == 'Textblock zeigen') {
  			document.getElementById(legend).firstChild.nodeValue = 'Textblock schließen';
  		}
  	}
  	else {
  		Effect.BlindUp(divBlock, {
  			queue: 'end',
  			duration: 0.5
  		});
  		
  		if (document.getElementById(legend).firstChild.nodeValue == 'Textblock schließen') {
  			document.getElementById(legend).firstChild.nodeValue = 'Textblock zeigen';
  		}
  	}
  }
}

function highlight_img(imgID) {
  document.getElementById(imgID).style.backgroundColor = '#aaf';
  document.getElementById(imgID).style.padding = '2px';
  document.getElementById(imgID).style.border = '1px solid #000';
}

function downlight_img(imgID) {
  document.getElementById(imgID).style.backgroundColor = '';
  document.getElementById(imgID).style.padding = '3px';
  document.getElementById(imgID).style.border = '0px solid #000';
}

function show(theBox) 
{
  if($(theBox).style.display == 'none') Effect.BlindDown(theBox, {duration: 0.2});
}

function hide(theBox)
{
  if($(theBox).style.display != 'none') Effect.BlindUp(theBox, {duration: 0.2});
}

function insert_bbcodes(tagOpen, tagClose, sampleText) {
	var txtarea;
	if (document.editform) {
		txtarea = currentFocused;
	} else {
		// some alternate form? take the first one we can find
		var areas = document.getElementsByTagName('textarea');
		txtarea = areas[0];
	}
	var selText, isSample = false;

	if (document.selection  && document.selection.createRange) { // IE/Opera

		//save window scroll position
		if (document.documentElement && document.documentElement.scrollTop)
			var winScroll = document.documentElement.scrollTop
		else if (document.body)
			var winScroll = document.body.scrollTop;
		//get current selection
		txtarea.focus();
		var range = document.selection.createRange();
		selText = range.text;
		//insert tags
		checkSelectedText();
		range.text = tagOpen + selText + tagClose;
		//mark sample text as selected
		if (isSample && range.moveStart) {
			if (window.opera)
				tagClose = tagClose.replace(/\n/g,'');
			range.moveStart('character', - tagClose.length - selText.length);
			range.moveEnd('character', - tagClose.length);
		}
		range.select();
		//restore window scroll position
		if (document.documentElement && document.documentElement.scrollTop)
			document.documentElement.scrollTop = winScroll
		else if (document.body)
			document.body.scrollTop = winScroll;

	} else if (txtarea.selectionStart || txtarea.selectionStart == '0') { // Mozilla

		//save textarea scroll position
		var textScroll = txtarea.scrollTop;
		//get current selection
		txtarea.focus();
		var startPos = txtarea.selectionStart;
		var endPos = txtarea.selectionEnd;
		selText = txtarea.value.substring(startPos, endPos);
		//insert tags
		checkSelectedText();
		txtarea.value = txtarea.value.substring(0, startPos)
			+ tagOpen + selText + tagClose
			+ txtarea.value.substring(endPos, txtarea.value.length);
		//set new selection
		if (isSample) {
			txtarea.selectionStart = startPos + tagOpen.length;
			txtarea.selectionEnd = startPos + tagOpen.length + selText.length;
		} else {
			txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length;
			txtarea.selectionEnd = txtarea.selectionStart;
		}
		//restore textarea scroll position
		txtarea.scrollTop = textScroll;
	}

	function checkSelectedText(){
		if (!selText) {
			selText = sampleText;
			isSample = true;
		} else if (selText.charAt(selText.length - 1) == ' ') { //exclude ending space char
			selText = selText.substring(0, selText.length - 1);
			tagClose += ' '
		}
	}

}

function submit_survey(survey_id)
{
	var fieldset = $('container_' + survey_id).childElements()[0];
	var conHeight = fieldset.getHeight()-14;
	
	$('form_' + survey_id).addClassName('hidden');
	
	fieldset.setStyle({'height':conHeight}).addClassName('loading');
	
	new Ajax.Request('ajax/submit_survey.php', {
		method: 'post',
		parameters: $('form_' + survey_id).serialize(true),
		onSuccess: function(transport) {
			fieldset.removeClassName('loading');
			
			var response = transport.responseText || "no response text";
			
			if(response == 'failure') {
				$('form_' + survey_id).removeClassName('hidden');
			}
			else {
				location.reload(true);
			}
		},
    	onFailure: function() { $('form_' + survey_id).removeClassName('hidden'); }
	});
	
	return false;
}


function getPageSize() {
	var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}
	
	function addFavorite(favorite)
	{
		new Ajax.Request('ajax/add_user_favorite.php', {
			method: 'post',
			parameters: { data: favorite },
			onComplete: function(e) {
				loadFavorites();
				$('isFavorite').src = 'images/favorite_heart_1.png';
				$('isFavorite').writeAttribute('onclick', 'return false;');
			}
		});
		
		Effect.Appear('drop_content', { duration: 0.2 });	
		
		return false;
	}
	
	Event.observe('dropdown', 'mouseenter', function(e) {
		if (!$('drop_content').visible()) {
			Effect.Appear('drop_content', { duration: 0.2 });
		}
	});
	
	Event.observe('dropdown', 'mouseleave', function(e) {
		if ($('drop_content').visible()) {
			Effect.Fade('drop_content', { duration: 0.2 });
		}
	});
	
	var dragged = false;
	function open_link(link)
	{
		if(!dragged) bleh = true;
		else bleh = false;
		
		dragged = false;
		
		return bleh;
	}
	
	function startDraggable()
	{
		$$('.draggable').each(function(e) {
			var id = e.identify();
			
			new Draggable(id, { 
				constraint: false,
				revert: true,
				onStart: function(a, b) {
					dragged = true;
				},
				onEnd: function(a, b) {
					var offset = b.element().viewportOffset();
					if(offset.top > $('drop_content').getHeight() + 20)
					{
						b.element().hide();
						
						new Ajax.Request('ajax/delete_user_favorite.php', {
							method: 'post',
							parameters: { data: id },
							onComplete: function(e) {
								loadFavorites();
							}
						});
					}
				}
			});
		});
	}
	
	function startSortable()
	{
		Sortable.create('favoriten', {
			tag: 'a',
		 	constraint: false,
			onChange: function() {
				dragged = true;
				new Ajax.Request('ajax/sort_user_favorites.php', {
					method: 'post',
					parameters: { 
						data: Sortable.serialize('favoriten', { tag: 'a' })
					}
				});
			}
		});
	}
	
	function loadFavorites()
	{
		new Ajax.Updater('drop_content', 'ajax/user_favorite.php', {
			onComplete: function(e) {
				var response = e.responseText;
				// alert(response);
				if(response != '') $('dropdown').removeClassName('hidden');
				
				startSortable();
				startDraggable();
			}
		});
	}
	
	loadFavorites();
	
	document.observe("dom:loaded", function() {
		if ($$('a.scroll_me')) {
			$$('a.scroll_me').invoke('observe','click', function(event) {
				Event.stop(event);
				
				var elementLink = Event.element(event).readAttribute('href');
				if(!elementLink) var elementLink = Event.element(event).ancestors()[0].readAttribute('href');
				
				var scrollHere = elementLink.substr(1, elementLink.length);
				Effect.ScrollTo(scrollHere, {duration: 0.5, offset: -20});
				return false;
			});
		}
	});
	