/* --------------------------------------------------------------------------------------------

Ajax Dialogs

*/

var ajaxDialogs = new Array();

function link_foo() {}

function getScrollY() {
	var x,y;
	if (self.pageYOffset) // all except Explorer
	{	x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{	x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{	x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	return y;
}

function getHeight() {
	var x,y;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if (test1 > test2) // all but Explorer Mac
	{
		x = document.body.scrollWidth;
		y = document.body.scrollHeight;
	}
	else // Explorer Mac;
		 //would also work in Explorer 6 Strict, Mozilla and Safari
	{
		x = document.body.offsetWidth;
		y = document.body.offsetHeight;
	}
	return y;
}
function clickon(element, preserve, tb_func_display, tb_func_hide) {
	if (element.id) {
		id = element.id;
	} else {
	 	id = element;
	}
	if (id.substr(-4,0) == '_div') id = id.substr(0,id.length - 4);
	ps = '';
	
	if ($(preserve)) ps = $(preserve).id;

	var i = 0;
	var inarray = 0;
	for (i=0; i<ajaxDialogs.length; i++) {
		if (ajaxDialogs[i] != id) {
			tmp = document.getElementById(ajaxDialogs[i]+'_div');
			if (tmp && ajaxDialogs[i]+'_div' != ps) tmp.style.display = 'none';
		}
		if (ajaxDialogs[i] == id) inarray = true;
	}
	if (inarray == 0) ajaxDialogs.push(id);
	
	// if there is an element that is called <something>_refId
	// and there is a second parameter given,
	// the value of this element is set to the value of the
	// second parameter...
	// this has been introduced to be able to respond to posts
	// in the forum
	a = document.getElementById(id+"_refId");
	if (a) {
		if (clickon.arguments && clickon.arguments[1]) {
			a.value=clickon.arguments[1];
		} else if (arguments[1]) {
			a.value=arguments[1];
		} else {
			a.value = 'NULL';
		}
	}
	
	a = document.getElementById(id+"_div");
	if (a) {		
		org = a;
		if (a.style.display == 'none') { 
			a.style.display = 'block'; 
			if (tb_func_display) tb_func_display(a);
		} else if (a.style.display=='block') { 
			a.style.display='none';  
			if (tb_func_hide) tb_func_hide(a);
		}

		b = document.getElementById(id+"_nodata_div");
		if (b) {
			if (b.style.display == 'none') { b.style.display = 'block'; }
			else if (b.style.display=='block') { b.style.display='none';  }
		}		

		a = document.getElementById(id+'_dialog');
		if (a) a.style.display='block';
		a = document.getElementById(id+'_process');
		if (a) a.style.display='none';
		
		a = document.getElementById(id+'_finished');
		if (a) a.style.display='none';
		
		b = document.getElementById(id+"_focus");
		if (b && org.style.display != 'none') {
			b.focus();
		}	
		
	}
}

function toggleShow(element, show) {
	a = document.getElementById(element.id+"_div");
	if (a.style.display == 'none') { a.style.display = 'block'; }
	else if (a.style.display=='block') { a.style.display='none';  }
}

function toggleCollapse(id) {
	a = document.getElementById(id);
	b = document.getElementById(id.replace(/filters/, 'legend'));
	if (a) {
		org = a;
		if (a.style.display == 'none') { 
			a.style.display = 'block'; 
			if (b) b.className = 'on'; 
		} else if (a.style.display=='block') { 
			a.style.display='none';  
			if (b) b.className = 'off'; 
		}
	}
}
function doLogin() {
	$('response').value = MD5($('pass').value);
	$('pass').value='';
	delay(500);
	document.forms['login'].submit();
}


function delay(gap){
 var then,now;
 then=new Date().getTime();
 now=then;
 while((now-then)<gap) {
  now=new Date().getTime();
 }
}

function tct(obj) {
	if (obj) {
		org = obj;
		if (obj.className == 'inactivetab') { 
			obj.className = 'activetab';
		} else if (obj.className == 'activetab') { 
			obj.className = 'inactivetab';
		}
		if (obj.className == 'inactivetab2') { 
			obj.className = 'activetab2';
		} else if (obj.className == 'activetab2') { 
			obj.className = 'inactivetab2';
		}
	}
}

function ajax_check(element) {
	if (element == 'melden') {
		if (document.getElementById('accuse_reason').value.length>2) return true;
		else return false; 
	}
	if (element == 'forum_post') {
		if ($('forum_textbody').value.length>0) return true;
		else { alert('Du solltest schon etwas zum Thema beitragen.'); return false; }
	}
	if (element == 'forum_topic') {
		if (trim($('subject').value).length>0) return true;
		else { alert('Das Thema muss benannt werden.'); return false; }
	}
	return true;
	
	return true;
}

function ajax_error(element) {
	if (element == 'melden') {
		alert("Deine Beschreibung/Nachricht an uns fehlt!");
	}
}

function ajax_endaction(element) {
	if (element == 'melden') {
		document.getElementById('accuse_reason').value="";
	}
}


function updateTokens(newtoken, tokens) {
	// update Tokens für alle AjaxDialogs
	
	Alltokens = Array('accuse_form', 'poke_form', 'topic_del_form', 'comment_form', 'forum_topic_add_form', 'friendInvite_form', 'mailboxAction_form', 'msg-form_token', 'navsearchform_form', 'mail_form', 'comment_form', 'albumMarksSave_form', 'blog_form', 'blogEntryEdit_form', 'blogEntryEdit_form_token', 'ctoken_form', 'accuse_form', 'poke_form', 'friendInvite_form', 'comment_form', 'mailboxAction_form', 'pokeDiscard_form', 'comment_form', 'ctoken_form', 'accuse_form', 'poke_form', 'friendInvite_form', 'comment_form', 'mailboxAction_form', 'pokeDiscard_form', 'comment_form', 'ctoken_form', 'groupInvitation_form', 'groupInvitation', 'groups', 'groupOptions_form', 'pokeDiscard_form', 'comment_form', 'ctoken_form', 'innerGroupMembers_form', 'groupAnchorman_form', 'groupUninvite_form', 'groupAnchorman', 'groupUninvite', 'mailboxAction_form', 'marks_form', 'accuse_form', 'poke_form', 'forum_topic_form', 'navsearchform', 'friendInvite_form', 'groupMembershipEnd_form', 'groupInivitation', 'albumMarksSave_form', 'mailboxAction_form', 'comment_form', 'ctoken_form', 'objPropertiesSave_form', 'navsearchform', 'pokeDiscard_form', 'comment_form', 'mailboxAction_form', 'pokeDiscard_form', 'comment_form', 'ctoken_form', 'groupInvitation_form', 'groupInvitation', 'groups', 'marks_form', 'accuse_form', 'poke_form', 'forum_topic_form', 'navsearchform', 'friendInvite_form', 'groupMembershipEnd_form', 'groupInivitation', 'albumMarksSave_form', 'mailboxAction_form', 'innerGroupMembers_form', 'groupAnchorman_form', 'groupUninvite_form', 'groupAnchorman', 'groupUninvite', 'marks_form', 'accuse_form', 'poke_form', 'forum_topic_form', 'navsearchform', 'friendInvite_form', 'groupMembershipEnd_form', 'groupInivitation', 'albumMarksSave_form', 'mailboxSend_form', 'mailboxAction_form', 'cform_form', 'cform', 'marks_form', 'accuse_form', 'poke_form', 'poke_form_token', 'forum_topic_form', 'navsearchform', 'friendInvite_form', 'groupMembershipEnd_form', 'groupInivitation', 'albumMarksSave_form', 'mailboxAction_form', 'cform_form', 'cform', 'forum_topic_add_form_token', 'comment_form', 'forum_topic_add_form', 'msg-form', 'pokeDiscard_form', 'tag', 'profil_edit', 'album_edit', 'mode_edit', 'blog_edit', 'modeprops_form_token');
	tokens.concat(Alltokens);
	
	tokens.push('navsearchform');
	var tmp_diags = '';
	for (i=0; i<ajaxDialogs.length; i++) {
		tmp = document.getElementById(ajaxDialogs[i]+'_token');
		if (tmp) { tmp.value = newtoken; tmp_diags = tmp_diags + ajaxDialogs[i]+'_token, '; }
		tmp = document.getElementById(ajaxDialogs[i]+'_form_token');
		if (tmp) { tmp.value = newtoken; tmp_diags = tmp_diags + ajaxDialogs[i]+'_form_token, '; }
	}
	if ((typeof tokens) == 'object') {
		for (i=0; i<tokens.length; i++) {
			tmp = document.getElementById(tokens[i]+'_token');
			if (tmp) { tmp.value = newtoken; tmp_diags = tmp_diags + tokens[i]+'_token, '; }
		}
	}
	if (debug_mode) alert('update '+newtoken+" auf "+tmp_diags);
}




// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Ajax-Funktionen (neu)

currentAjaxAction = null;
function ajaxAction(element, func, func2, name){
	if (ajaxCheck(element)) {
		if (!name) name = element;
		var url = root_url+'community/ajax/'+name+'.php';
		var myAjax = new Ajax.Request(
			url, {
				method: 'post',
				postBody: Form.serialize(document.forms[element+'_form']),
				onCreate: (func2 ? func2 : ajaxProcess(element)),
				onComplete: (func ? func : ajaxFinished(element))
			}
		); 
	}
}
function ajaxProcess(element) {
	a = $(element+'_dialog');
	if (a) a.style.display='none';
	a = $(element+'_finished');
	if (a) { a.style.display='none'; }
	a = $(element+'_process');
	if (a) { a.style.display='block';  }
	
	a = document.getElementById(element+'_dialog_buttons');
	if (a) a.style.display='none';
	a = document.getElementById(element+'_dialog_buttons_finished');
	if (a) a.style.display='none';
}

function ajaxFinished(element) {
	a = $(element+'_dialog');
	if (a) a.style.display='none';
	a = document.getElementById(element+'_process');
	if (a) a.style.display='none';
	a = document.getElementById(element+'_finished');
	if (a) a.style.display='block';
	a = document.getElementById(element+'_dialog_buttons');
	if (a) a.style.display='none';
	a = document.getElementById(element+'_dialog_buttons_finished');
	if (a) a.style.display='block';
}

function ajaxCheck(element) {
	if (element.substr(0,6) == 'accuse') {
		if (document.getElementById(element+'_reason').value.length<10) {
			alert('Um Missbrauch zu vermeiden, muss diese Meldung im Feld "Details" näher erörtert werden.');
			return false;
		} 
	}
	if (element == 'forum_post') {
		if ($('forum_textbody').value.length>0) return true;
		else { alert('Du solltest schon etwas zum Thema beitragen.'); return false; }
	}
	if (element == 'forum_topic') {
		if (trim($('subject').value).length>0) return true;
		else { alert('Das Thema muss benannt werden.'); return false; }
	}
	return true;
}




/* --------------------------------------------------------------------------------------------

Hilfsfunktionen

*/

function editor_two_level_change(selector,subtypes_array,sublabels_array) {
	selector=$(selector);
	if(selector.getAttribute("typefor")) subselector=$(selector.getAttribute("typefor"));
	if(selector&&subselector) {
		subselector.options.length=1;
		type_value=selector.options[selector.selectedIndex].value;
		if(type_value==""){type_value=-1;}
		index=1;
		suboptions=subtypes_array[type_value];
		
		for(var key in suboptions) {
			if (!isNaN(key)) {
				subselector.options[index]=new Option(suboptions[key],key);
				index++;
			}
		}
		subselector.disabled=subselector.options.length<=1;
	}
}

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function show_please_wait() {
	if ($('please_wait')) $('please_wait').style.display = 'block';
}

function hide_please_wait() {
	if ($('please_wait')) $('please_wait').style.display = 'none';
}


/* --------------------------------------------------------------------------------------------

Profil + GÃƒÆ’Ã‚Â¤stebuch

*/


var profile_changed = false;

function pns() {
	profile_changed = true;
}

function p_redir(url) {
	if (profile_changed) {
		a = confirm("Achtung! Du hast Daten verändert und noch nicht gespeichert.\n"+
			"Änderungen verwerfen?");
		if (a) {
			document.location.href=url;
		}
	} else {
		document.location.href=url;
	}
}


/* --------------------------------------------------------------------------------------------

Generisch

*/

function objectDelete(type, id, check) {
	var txt = '';
	if (type == 7) txt = 'diese Produktliste';
	if (type == 3) txt = 'dieses Album';
	if (type == 4) txt = 'Deinen Blog';
	if (confirm('Willst Du '+txt+' wirklich löschen?')) {
		if (type == 7) document.location.href='mode.php?action=deleteStil&sid='+id+'&token='+check;
		if (type == 3) document.location.href='album.php?action=deleteAlbum&aid='+id+'&token='+check;
		if (type == 4) document.location.href='blog.php?action=deleteBlog&token='+check;
	}		
}

function objPropertiesSave() {
	$('please_wait').style.display = 'block';
	ajaxAction('objPropertiesSave', objPropertiesSaveFinished, show_please_wait());	
}

function objPropertiesSaveFinished() {
	//document.location.reload();
}



function locationSelect(what) {
	var url = root_url+'community/ajax.php';
	var myAjax = new Ajax.Request(
		url, {
			method: 'post',
			postBody: 'action=locationSelect&'+Form.serialize(document.forms['signup'])+'&what='+what,
			onCreate: waitLocationSelect(),
			onSuccess: returnLocationSelect} 
	);
}
function waitLocationSelect() {
	$('pleasewaitlocation').style.display = 'block';
	$('country').disabled = true;
	if ($('city')) $('city').disabled = true;
	if ($('state')) $('state').disabled = true;
	if ($('uni')) $('uni').disabled = true;
}
function returnLocationSelect(s) {
	$('pleasewaitlocation').style.display = 'none';
	var x = s.responseText;
	token = x.substr(0, 32);
	x = x.substr(32);
	$('location').innerHTML = x;
	updateTokens(token, Array('signup'));
}