// AS 20.07.09
// Ajax : Post an datahandler

var newMsgWin;

function save_formdata(form,type){
	if (form == "new_galerie_form"){
		var box = Ext.MessageBox.wait('Bilder werden hochgeladen', 'Neues Album anlegen');
	}
	if (form == "userpic_form"){
		var box = Ext.MessageBox.wait('Profilbild wird hochgeladen', 'Dein Profil');
	}
	Ext.Ajax.request({
		form: form
		,params: {action:'save',type:type}
		,callback:function(options, success, response){
				if(true !== success) {
					showError(response.responseText,'Fehler!');
					return;
				}
				try {
					var o = Ext.decode(response.responseText);
				}
				catch(e) {
					showError('<br /> <b> Es ist ein Fehler aufgetreten </b> <br /><br />' + response.responseText,"Fehler!");
					return;
				}
				if(true !== o.success) {
					if (o.msg){
						showError(o.msg,'Fehler');
					}
					if (o.session_timeout){
							window.location.href="index.php?show=error&type=session_timeout";
					}
					return;
				}
				
				switch(options.params.type) {
						case 'userpic':
							document.getElementById('profilbild').src = "img/profiles/" + o.file;
							box.hide();
						break;
						
						case 'newmail':
							openmail_win.close();
							openmail_win = "";
						break;
						
						case 'newalbum':
							box.hide();
							var eleid = Ext.get('album_id');
							Ext.getCmp("pic1").reset();
							Ext.getCmp("pic2").reset();
							Ext.getCmp("pic3").reset();
							
							eleid.dom.value = o.album_id;
							Ext.get('bild_titel1').dom.value = "";
							Ext.get('bild_titel2').dom.value = "";
							Ext.get('bild_titel3').dom.value = "";
							
							var albumlist = Ext.get('albumlist');
							albumlist.load({
											url: 'sites/galerie/view_album_overview.php',
											method:'POST',
											 scripts:true,
											text: "Lade Galerie...",
											params: {clubid:o.club_id,catid:o.cat_id,ajax:true}
										 });
						break;
						
						case 'addpics':
							box.hide();
							var picslide = Ext.get('picslide');
								picslide.load({
												url: 'sites/galerie/view_ajax_picslide.php',
												method:'POST',
												 scripts:true,
												text: "Lade Album...",
												params: {clubid:o.club_id,albumid:o.album_id}
											  });
							
						break;
						
						case 'invite':
							openinvite_win.close();
							openinvite_win = "";
						break;
						
						case 'new_forum':
							 var forenlist = Ext.get('forenlist');
							    forenlist.load({
                                    url: 'sites/forum/view_forum.php?clubid='+o.clubid+'&catid='+o.catid,
                                    method:'POST',
                                     scripts:true,
                                    text: "Lade Forum...",
                                    params: {clubid:o.clubid,ajax:true}
                                 });
						break;
				}
			
				if (o.msg){
					Ext.Msg.alert('Portal',o.msg);
				}
				
				if(o.redirect === true){
					o.redirect_url = o.redirect_url.replace(/\&amp;/g,"&");
					window.location.href=o.redirect_url;
				}
			
		}
	});
}

function showError(msg, title) {
		Ext.Msg.show({
			 title:title || 'Error'
			,msg:Ext.util.Format.ellipsis(msg, 2000)
			,icon:Ext.Msg.ERROR
			,buttons:Ext.Msg.OK
			,minWidth:1200 > String(msg).length ? 360 : 600
		});
}

function newMessage(to){
			openmail_win = new win_mail({
				title:'Neue Nachricht',
				action:'new',
				to:to
			});
			openmail_win.show();
}

function newMessageAll(club){
			openmail_win = new win_mail({
				title:'Neue Nachricht',
				action:'new',
				to:'all',
				clubid:club
			});
			openmail_win.show();
}


function invite(club){
			openinvite_win = new win_invite({
				title:'Freunde einladen',
				club:club
			});
			openinvite_win.show();
}

function newAlbum(club,catid,albumid){
			opengalerie_win = new win_galerie({
				club:club,
				catid:catid,
				albumid:albumid
			});
			opengalerie_win.show();
}

function createCalEntry(clubid){
	var cwin;
	if(!cwin){
            cwin = new Ext.Window({
                layout:'fit',
				title:'Neuen Termin eintragen',
                width:630,
                height:500,
                closeAction:'hide',
				modal:true,
                plain: false,
				autoLoad: {url:'sites/calendar/form_new_entry.php',scripts:true}
            });
        }
        cwin.show(this);
}

function addContact(user){
	Ext.Ajax.request({
					 	params:{action:'addcontact',userid:user}
						,method:'POST'
						,url:'sites/mailing/datahandler.php'
						,callback:function(options, success, response){
								if(true !== success) {
									showError(response.responseText,'Fehler!');
									return;
								}
								try {
									var o = Ext.decode(response.responseText);
									Ext.Msg.alert("Kontaktliste",o.msg);
								}
								catch(e) {
									showError('<br /> <b> Es ist ein Fehler aufgetreten </b> <br /><br />' + response.responseText,"Fehler!");
									return;
								}
								
								if(true !== o.success) {
									if (o.msg){
										showError(o.msg,'Fehler');
									}
									if (o.session_timeout){
											window.location.href="index.php?show=error&type=session_timeout";
									}
									return;
							}
						}
					 });
}

function setAdmin(user,clubid){
		Ext.Msg.show({
					   title:'Wirklich zum Admin machen?',
					   msg: 'Mitglied wirklich zum Admin machen?',
					   buttons: Ext.Msg.YESNO,
					   fn: function(btn,text){
						   if (btn == "yes"){
									Ext.Ajax.request({
														params:{action:'setadmin',userid:user,clubid:clubid}
														,method:'POST'
														,url:'sites/clubs/datahandler.php'
														,callback:function(options, success, response){
																if(true !== success) {
																	showError(response.responseText,'Fehler!');
																	return;
																}
																try {
																	var o = Ext.decode(response.responseText);
																	Ext.Msg.alert("Klub",o.msg);
																}
																catch(e) {
																	showError('<br /> <b> Es ist ein Fehler aufgetreten </b> <br /><br />' + response.responseText,"Fehler!");
																	return;
																}
																
																if(true !== o.success) {
																	if (o.msg){
																		showError(o.msg,'Fehler');
																	}
																	if (o.session_timeout){
																			window.location.href="index.php?show=error&type=session_timeout";
																	}
																	return;
															}
														}
													 });
						   }
					   },
					   animEl: 'elId',
					   icon: Ext.MessageBox.QUESTION
					});
}


function delAdmin(user,clubid){
		Ext.Msg.show({
					   title:'Adminrechte',
					   msg: 'Mitglied wirklich Adminrechte entfernen?',
					   buttons: Ext.Msg.YESNO,
					   fn: function(btn,text){
						   if (btn == "yes"){
									Ext.Ajax.request({
														params:{action:'deladmin',userid:user,clubid:clubid}
														,method:'POST'
														,url:'sites/clubs/datahandler.php'
														,callback:function(options, success, response){
																if(true !== success) {
																	showError(response.responseText,'Fehler!');
																	return;
																}
																try {
																	var o = Ext.decode(response.responseText);
																	Ext.Msg.alert("Klub",o.msg);
																}
																catch(e) {
																	showError('<br /> <b> Es ist ein Fehler aufgetreten </b> <br /><br />' + response.responseText,"Fehler!");
																	return;
																}
																
																if(true !== o.success) {
																	if (o.msg){
																		showError(o.msg,'Fehler');
																	}
																	if (o.session_timeout){
																			window.location.href="index.php?show=error&type=session_timeout";
																	}
																	return;
															}
														}
													 });
						   }
					   },
					   animEl: 'elId',
					   icon: Ext.MessageBox.QUESTION
					});
}

function delUser(user,clubid){
		Ext.Msg.show({
					   title:'Wirklich l&ouml;schen?',
					   msg: 'Mitglied wirklich l&ouml;schen?',
					   buttons: Ext.Msg.YESNO,
					   fn: function(btn,text){
						   if (btn == "yes"){
								Ext.Ajax.request({
													params:{action:'deluser',userid:user,clubid:clubid}
													,method:'POST'
													,url:'sites/clubs/datahandler.php'
													,callback:function(options, success, response){
															if(true !== success) {
																showError(response.responseText,'Fehler!');
																return;
															}
															try {
																var o = Ext.decode(response.responseText);
																Ext.Msg.alert("Klub",o.msg);
															}
															catch(e) {
																showError('<br /> <b> Es ist ein Fehler aufgetreten </b> <br /><br />' + response.responseText,"Fehler!");
																return;
															}
															
															if(true !== o.success) {
																if (o.msg){
																	showError(o.msg,'Fehler');
																}
																if (o.session_timeout){
																		window.location.href="index.php?show=error&type=session_timeout";
																}
																return;
														}
													}
												 });
														   }
					   },
					   animEl: 'elId',
					   icon: Ext.MessageBox.QUESTION
					});
}

function delClub(clubid){
		Ext.Msg.show({
					   title:'Wirklich l&ouml;schen?',
					   msg: 'M&ouml;chtest du den Klub wirklich l&ouml;schen? Es werden alle Forensbeitr&auml;ge, Fotoalben sowie Termine gel&ouml;scht!',
					   buttons: Ext.Msg.YESNO,
					   fn: function(btn,text){
						   if (btn == "yes"){
								Ext.Ajax.request({
													params:{action:'delclub',clubid:clubid}
													,method:'POST'
													,url:'sites/clubs/datahandler.php'
													,callback:function(options, success, response){
															if(true !== success) {
																showError(response.responseText,'Fehler!');
																return;
															}
															try {
																var o = Ext.decode(response.responseText);
																Ext.Msg.alert("Klub",o.msg);
															}
															catch(e) {
																showError('<br /> <b> Es ist ein Fehler aufgetreten </b> <br /><br />' + response.responseText,"Fehler!");
																return;
															}
															
															if(true !== o.success) {
																if (o.msg){
																	showError(o.msg,'Fehler');
																}
																if (o.session_timeout){
																		window.location.href="index.php?show=error&type=session_timeout";
																}
																return;
														}
													}
												 });
														   }
					   },
					   animEl: 'elId',
					   icon: Ext.MessageBox.QUESTION
					});
}



function joinEvent(eventid,clubid,type){
	Ext.Ajax.request({
					 	params:{action:'joinevent',eventid:eventid,clubid:clubid,type:type}
						,method:'POST'
						,url:'sites/calendar/datahandler.php'
						,callback:function(options, success, response){
								if(true !== success) {
									showError(response.responseText,'Fehler!');
									return;
								}
								try {
									var o = Ext.decode(response.responseText);
									Ext.Msg.alert("Kontaktliste",o.msg);
								}
								catch(e) {
									showError('<br /> <b> Es ist ein Fehler aufgetreten </b> <br /><br />' + response.responseText,"Fehler!");
									return;
								}
								
								if(true !== o.success) {
									if (o.msg){
										showError(o.msg,'Fehler');
									}
									if (o.session_timeout){
											window.location.href="index.php?show=error&type=session_timeout";
									}
									return;
								}else{
									 var userlist = Ext.get('userlist');
									  userlist.load({
										url: 'sites/global/list_ajax_users.php',
										method:'POST',
										 scripts:true,
										text: "Lade Teilnehmer...",
										params: {clubid:o.clubid,calid:o.calid}
									  });	
									  link_joinevent
									  
									  var link_joinevent = Ext.get('link_joinevent');
									link_joinevent.setVisibilityMode(Ext.Element.DISPLAY);
									link_joinevent.setVisible(false,true);
								}
						}
					 });
}


function joinClub(clubid){
	Ext.Msg.show({
				 	title:'Wirklich dem Klub beitreten?',
					msg:'M&ouml;chtest du wirklich dem Klub beitreten?',
					buttons: Ext.Msg.YESNO,
					fn:function(btn,text){
						  if (btn == "yes"){
							  	Ext.Ajax.request({
													params:{action:'joinclub',clubid:clubid}
													,method:'POST'
													,url:'sites/clubs/datahandler.php'
													,callback:function(options, success, response){
															if(true !== success) {
																showError(response.responseText,'Fehler!');
																return;
															}
															try {
																var o = Ext.decode(response.responseText);
																Ext.Msg.alert("Klub",o.msg);
															}
															catch(e) {
																showError('<br /> <b> Es ist ein Fehler aufgetreten </b> <br /><br />' + response.responseText,"Fehler!");
																return;
															}
															
															if(true !== o.success) {
																if (o.msg){
																	showError(o.msg,'Fehler');
																}
																if (o.session_timeout){
																		window.location.href="index.php?show=error&type=session_timeout";
																}
																return;
															}
															 
													}
												 });
						  }
					},
					  animEl: 'elId',
					   icon: Ext.MessageBox.QUESTION
					});
	
}

function leaveClub(clubid,catid){
	Ext.Msg.show({
					   title:'Wirklich austreten?',
					   msg: 'M&ouml;chtest du wirklich aus dem Klub austreten? Nach dem Austritt hast du keine M&ouml;glichkeit mehr auf die Klubfunktionen zuzugreifen.',
					   buttons: Ext.Msg.YESNO,
					   fn: function(btn,text){
						   if (btn == "yes"){
								Ext.Ajax.request({
													params:{action:'leaveclub',clubid:clubid,catid:catid}
													,method:'POST'
													,url:'sites/clubs/datahandler.php'
													,callback:function(options, success, response){
															if(true !== success) {
																showError(response.responseText,'Fehler!');
																return;
															}
															try {
																var o = Ext.decode(response.responseText);
															}
															catch(e) {
																showError('<br /> <b> Es ist ein Fehler aufgetreten </b> <br /><br />' + response.responseText,"Fehler!");
																return;
															}
															
															if(true !== o.success) {
																if (o.msg){
																	showError(o.msg,'Fehler');
																}
																if (o.session_timeout){
																		window.location.href="index.php?show=error&type=session_timeout";
																}
																return;
															}	
																if(o.redirect === true){
																	o.redirect_url = o.redirect_url.replace(/\&amp;/g,"&");
																	window.location.href=o.redirect_url;
																}
													}
												 });
											}
					   },
					   animEl: 'elId',
					   icon: Ext.MessageBox.QUESTION
					});
}

	 function loadForum(e,forumid,clubid){
		 var e = Ext.get(e);
		 e.load({
				  url: 'sites/forum/view_ajax_forum.php?clubid=' + clubid + '&forumid=' + forumid,
					method:'POST',
					 scripts:true,
					text: "Lade Forum...",
					params: {clubid:clubid,forumid:forumid}
				});
	 }


// Callback auflösung
function callback(){
	
}

function trim (zeichenkette) {
  return zeichenkette.replace (/^\s+/, '').replace (/\s+$/, '');
}



