index = webroot+"/server2.php?";
current_type = 0;
wall_start = 0;

Ajax.Responders.register({
	onComplete: function(request, response) {
		//google.maps.Log.write(response.responseJSON);
 		showMessageWindow(response.responseJSON);
	}
});

function showMessageWindow(jd)
{
	if (jd.code.messages && false)
	{
		$('message').hide();
		
		$('message').appear();
		
		setTimeout(function(){
			$('message_content').update(jd.code.messages);
		}, 1000)
		
		setTimeout(function(){
			if ($('message_box')) $('message_box').remove();
			$('message').fade();
			
		}, 5000)
	}
}

function axSendFile()
{
	var data = $('uploadForm').serialize(true);
	var inputs = new Array('vorname', 'nachname', 'email', 'file');
	var errors = new Hash();

	if (data.vorname == "")
		errors.set('vorname', "Kein Vorname eingetragen"); 

	if (data.nachname == "")
		errors.set('nachname', "Kein Nachname eingetragen"); 
	
	if (data.email == "")
		errors.set('email', "Keine eMail Adresse eingetragen"); 
	
	if (data.file == "")
		errors.set('file', "Keine Datei ausgewählt"); 
	
	var result = displayErrors(inputs, errors);
	
	return result;
}

function axSendFreeOrder()
{
	var data = $('orderForm').serialize(true);
	var inputs = new Array('vorname', 'nachname', 'schule', 'plz', 'ort', 'strasse', 'email');
	
	new Ajax.Request(index, {
  		method: 'get',
  		parameters: {f:'orderData', s:session, data:Object.toJSON(data)},
  		onSuccess: function(transport) {
  			jd = transport.responseJSON;
	        if (displayErrors(inputs, jd.errors))	
			{
	        	$('orderForm').update(jd.code.content);
			}
        }
    });
}


function axRegister()
{
	var data = $('registerForm').serialize(true);
	var inputs = new Array('username', 'name1', 'name2', 'email', 'password');
	
	if (data.accept)
	{
	
		new Ajax.Request(index, {
	  		method: 'get',
	  		parameters: {f:'registerData', s:session, data:Object.toJSON(data)},
	  		onSuccess: function(transport) {
	  			jd = transport.responseJSON;
		        if (displayErrors(inputs, jd.errors))	
				{
		        	$('content_header').hide();
		        	$('content_header').update(jd.code.login);
		        	$('content_header').appear();
		        	
		        	$('content').update(jd.code.content);
				}
	        }
	    });
	} else
		alert("Bitte Nutzungsbedingungen akzeptieren...")
}

function axEditAccountData()
{
	var data = $('registerForm').serialize(true);
	var inputs = new Array('username', 'name1', 'name2', 'email', 'password');
	
	new Ajax.Request(index, {
  		method: 'get',
  		parameters: {f:'editUserData', s:session, data:Object.toJSON(data)},
  		onSuccess: function(transport) {
  			jd = transport.responseJSON;
	        if (displayErrors(inputs, jd.errors))	
			{
	        	$('content_header').hide();
	        	$('content_header').update(jd.code.login);
	        	$('content_header').appear();
	        	
	        	$('content').update(jd.code.content);
			}
        }
    });
}

function axDelAccountData()
{
	var data = $('delForm').serialize(true);
	
	if (data.del_account)
	{

		new Ajax.Request(index, {
	  		method: 'get',
	  		parameters: {f:'delUserData', s:session, data:Object.toJSON(data)},
	  		onSuccess: function(transport) {
	  			jd = transport.responseJSON;
		        
	        	$('content_header').hide();
	        	$('content_header').update(jd.code.login);
	        	$('content_header').appear();
	        	
	        	$('content').update(jd.code.content);
				
	        }
	    });
	}
}



function axForgetPassword()
{
	var data = $('dataForm').serialize(true);
	
	new Ajax.Request(index, {
  		method: 'get',
  		parameters: {f:'forgetPassword', s:session, data:Object.toJSON(data)},
  		onSuccess: function(transport) {
  			jd = transport.responseJSON;
	        
	        $('content').update(jd.code.content);
        }
    });
}


function axLoginUser()
{
	var data = $('loginForm').serialize(true);
	
	if (data.username && data.userpass && data.username != "Deine eMail-Adresse" && data.userpass != "Dein Passwort")
	{
		new Ajax.Request(index, {
	  		method: 'get',
	  		parameters: {f:'login', s:session, type:'1', data:Object.toJSON(data)},
	  		onSuccess: function(transport) {
	  			jd = transport.responseJSON;
		        if (jd.status)
				{
		        	
		        	$('content').update(jd.code.content);
					
					setTimeout(function(){
						//var url = webroot+"/login/account";
						location.href = location.href;
					}, 1000)
				}
		        else
		        	alert("Login fehlgeschlagen!\nBitte überprüfe deine eMail Adresse und dein Passwort!!");
	        }
	    });    
	}
}


function axLogout()
{
	new Ajax .Request(index, {
  		method: 'get',
  		parameters: {f:'logout', s:session},
  		onSuccess: function(transport) {
  			jd = transport.responseJSON;
	        if (jd.status)
			{
				$('content').update(jd.code.content);
				
				setTimeout(function(){
					var url = webroot+"/startseite";
					location.href = url;
				}, 1000)
			}
        }
    });
}


function axShowEditTheme(forum_ID, theme_ID)
{
	new Ajax.Request(index, {
  		method: 'get',
  		parameters: {f:'showEditTheme', s:session, forum_ID: forum_ID, theme_ID: theme_ID},
  		onSuccess: function(transport) {
  			jd = transport.responseJSON;
  			
  			if (jd.status)
			{
  				$('edit_theme').update(jd.code.content);
			}
  			else
  			{
  				alert("Diese Funktion steht nur registrierten Usern zur Verfügung!\nBitte melde ich dich erst bei Bauchgefühl an!");
  			}
        }
    });
}

function axEditTheme()
{
	var data = $('themeForm').serialize(true);
	
	if (data.titel)
	{
	
		new Ajax.Request(index, {
	  		method: 'post',
	  		parameters: {f:'editTheme', s:session, forum_ID:data.forum_ID, theme_ID:data.theme_ID, data:Object.toJSON(data)},
	  		onSuccess: function(transport) {
	  			jd = transport.responseJSON;
	  			
	  			if (jd.status)
				{
	  				$('edit_theme').update("");
	  				
	  				setTimeout(function(){
						location.href = location.href;
					}, 500)
				}
				else
				{
					alert("Diese Funktion steht nur registrierten Usern zur Verfügung!\nBitte melde ich dich erst bei Bauchgefühl an!");
				}
	  			
	        }
	    });
	}
	else
		alert("Bitte gebe einen Titel ein!");
}

function axEditThemeComment()
{
	var data = $('commentForm').serialize(true);
	
	if (data.text)
	{
		new Ajax.Request(index, {
	  		method: 'post',
	  		parameters: {f:'editThemeComment', s:session, forum_ID:data.forum_ID, theme_ID:data.theme_ID, data:Object.toJSON(data)},
	  		onSuccess: function(transport) {
	  			jd = transport.responseJSON;
	  			
	  			if (jd.status)
				{
	  				$('edit_comment').update("");
	  				
	  				setTimeout(function(){
						location.href = location.href;
					}, 500)
				}
	  			else
	  			{
	  				alert("Diese Funktion steht nur registrierten Usern zur Verfügung!\nBitte melde ich dich erst bei Bauchgefühl an!");
	  			}
	  			
	        }
	    });
	}
	else
		alert("Bitte gebe einen Text ein!");
}

function axEditIdeaComment()
{
	var data = $('commentForm').serialize(true);
	
	if (data.text)
	{
		new Ajax.Request(index, {
	  		method: 'post',
	  		parameters: {f:'editIdeaComment', s:session, idea_ID:data.idea_ID, data:Object.toJSON(data)},
	  		onSuccess: function(transport) {
	  			jd = transport.responseJSON;
	  			
	  			if (jd.status)
				{
	  				$('edit_comment').update("");
	  				
	  				setTimeout(function(){
						location.href = location.href;
					}, 500)
				}
	  			else
	  			{
	  				alert("Diese Funktion steht nur registrierten Usern zur Verfügung!\nBitte melde ich dich erst bei Bauchgefühl an!");
	  			}
	        }
	    });
	}
	else
		alert("Bitte gebe einen Text ein!");
}

function axDoVote(id)
{
	var data = $('voting'+id).serialize(true);
	
	new Ajax.Request(index, {
  		method: 'post',
  		parameters: {f:'doVote', s:session, poll_ID:id, data:Object.toJSON(data)},
  		onSuccess: function(transport) {
  			jd = transport.responseJSON;
  			
  			if (jd.status)
			{
  				$('poll'+id).update(jd.code.content);
			}
        }
    });
}

function axDoAnswerQuiz(id, nr, hash)
{
	var data = $('voting'+id).serialize(true);
	
	new Ajax.Request(index, {
  		method: 'post',
  		parameters: {f:'doAnswerQuiz', s:session, poll_ID:id, nr:nr, hash:hash, data:Object.toJSON(data)},
  		onSuccess: function(transport) {
  			jd = transport.responseJSON;
  			
  			if (jd.status)
			{
  				$('poll'+id).update(jd.code.content);
			}
        }
    });
}

function axShowAnswerQuiz(id, nr, hash)
{
	new Ajax.Request(index, {
  		method: 'get',
  		parameters: {f:'showAnswerQuiz', s:session, poll_ID:id, nr:nr, hash:hash},
  		onSuccess: function(transport) {
  			jd = transport.responseJSON;
  			
  			if (jd.status)
			{
  				$('poll'+id).update(jd.code.content);
			}
        }
    });
}



function loadImage(image)
{
	var img = new Element("div", {"class":"load "+ image,"ID":"loader"});
	return img;
}

function startUpload(){
	$('insertDiv').insert({top:loadImage('')});
	$('insertDiv').appear();
	$('overDiv').show();
}

function stopUpload(funcname, data){
	if ($('overDiv'))$('overDiv').hide();
	if ($('insertDiv')) $('insertDiv').hide();
	if ($('loader')) $('loader').remove();
	
	if (this[funcname])
		this[funcname](data);
	else
		alert("Function: "+funcname+" nicht gefunden!");
}


function displayErrors(elements, errors){
	elements.each(function(element) {
			
		var label = $$('LABEL[rel='+element+']');
       	var edit = element+"_edit";
	
		if ($(edit)) 	$(edit).remove();
		if ($(element)) $(element).removeClassName('error');
		
		if (label[0]) 	Element.writeAttribute(label[0], 'title', '');
		if (label[0]) 	label[0].removeClassName('error');
	});
	
	
	var e = new Hash(errors);
	
    if (e.keys().length > 0)
    {

       	//GLog.write(Object.toJSON(e));
       	
       	e.each(function(pair) {
       		var element = pair.key;
       		var label = $$('LABEL[rel='+element+']');
       		var edit = element+"_edit";
       	
 				if ($(element)) 
 					$(element).addClassName('error');
 					
 				if (label[0]) 
 				{
 					label[0].addClassName('error');
 				
 					var code = "<IMG SRC=\""+webroot+"/inc/bilder/icons_mini/PNG/mini-alert.png\" BORDER=\"0\" ID=\""+edit+"\">";
 				
 					if (!$(edit))
 						Element.insert(label[0], {top: code});
 				
 					Element.writeAttribute(label[0], 'title', pair.value);
 				}
		});
		
		return false;
   }
       
   return true;
}

function insertTag(fieldID, tag)
{
	if(!fieldID) return false;
	if(!tag) return false;

	var currentText = $(fieldID).value;
	currentText = currentText.trim();
	tag = tag.trim();

	var prefix = "";
	var newText = "";

	var newTags = new Array();

	if(currentText.length > 0)
	{
		var explodedTags = currentText.split(",");
		for(var i = 0; i < explodedTags.length; i++)
		{
			if(explodedTags[i].toLowerCase().trim() == tag.toLowerCase().trim())
			{
				explodedTags.splice(i,1);

				newText = explodedTags.join(",");
				$(fieldID).value = newText;
				return true;
			}
		}
	}


	// Check last Character = ","
	var lastCharPos = currentText.length-1;
	if(currentText.length > 0)
	{

		if(currentText.charAt(lastCharPos) == ",")
		{
			prefix = " ";
		}
		else
		{
			prefix = ", ";
		}
	}


	newText = currentText + prefix + tag;

	$(fieldID).value = newText;
return true;
}

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g, '');
};
	
	