/*
 * Login Script v1.0.0
 * http://www-rsg-grimma.de/
 *
 * Copyright (c) 2009 Mirko Brauns
 *
 * Date: 2009-02-19 17:34:21 -0500 (Thu, 19 Feb 2009)
 * Revision: 6246
 */ 
 

function login(path) {

	exitsLoginDialog();

	var login = true;
	var username = $('#username');
	var	password = $('#password');
	var passwordControls = $('#passwordControls');
	var loginControls = $('#loginControls');
	var hints = $('#loginHints');	
	var form = $('#login');
	var submit = false;
	var loginBtn = null;
	var toggleBtn = null;
	var cancelBtn = null;
		
	var dialog = $("#loginDialog").dialog({
		title: 'Login',
		show: 'scale',
		hide: 'scale',
		modal: true,
		width: 450,
		buttons: {
			Login: function() {
				if (login) {
					ajaxLogin($(this));
				} else {
					ajaxResetUser($(this));
				}
			},
			'Passwort vergessen?' : function() {
				toggleForms();
			},
			Abbrechen: function() {
				removeErrors();
				$(this).dialog('close');
			}
		},
		open: function() {
			getLoginBtn().html('<span class="ui-button-text"><img src="' + path + 'images/tick.png" alt=""/>Login</span>');
			getToggleBtn().html('<span class="ui-button-text"><img src="' + path + 'images/textfield_key.png" alt=""/>Passwort vergessen?</span>');
			getCancelBtn().html('<span class="ui-button-text"><img src="' + path + 'images/cross.png" alt=""/>Abbrechen</span>');
			setTimeout(function() {
				$('#username').focus();
			}, 500);
		},
		close: function() {
			$(this).dialog('destroy');
		}
	});
		
	form.submit(function() {
		if (login) {
			ajaxLogin();
		} else {
			ajaxResetUser();
		}
		return submit;
   	});

	function exitsLoginDialog() {
		if ($('#loginDialog').length == 0) {
			$('#col1_content').append(
				'<div id="loginDialog" style="display: none;">' +
					'<form id="login" class="yform" method="post" action="' + location.href + '">' +
						'<div id="loginHints" class="important">Bitte geben Sie Ihren Benutzernamen und Passwort ein.</div>' +
						'<fieldset id="loginControls" class="columnar">' +
							'<div class="type-text">' +
								'<label for="username">Benutzername<sup title="Pflichtfeld">*</sup></label>' +
								'<input id="username" type="text" name="form[username]" size="20" maxlength="60" tabindex="1001" />' +
							'</div>' +
							'<div class="type-text" id="passwordControls">' +
								'<label for="password">Passwort<sup title="Pflichtfeld">*</sup></label>' +
								'<input id="password" type="password" name="form[password]" size="20" maxlength="60" tabindex="1002" />' +
							'</div>' +
						'</fieldset>' +
					'</form>' +
				'</div>');
			$('#login').keyup(function(event){
				if (event.keyCode == 13) {
					$(this).submit();
				};
			});
		}
	}

	function showAjaxLoading(msg) {
		hints.removeClass('important');
		hints.removeClass('warning');
		hints.addClass('info');
		hints.html('<img src="' + path + 'images/loading.gif" alt="loading" width="16" height="16" alt="" style="vertical-align: middle; margin: 5px;" />' + msg);
	}
	
	function ajaxLogin() {
		removeErrors();
	
		var bValid = checkLength(username, "Der Benutzername", 3, 60);
		bValid = bValid && checkLength(password, "Das Passwort", 5, 20);
		
		if (bValid) {
			showAjaxLoading('Die Benutzerdaten werden überprüft! Bitte warten!');
			$.ajax({
				type: "post",
				url: location.href,
				data: form.serialize() + '&form[login]=1',
				cache: false,
				success: function(msg) {
					if (msg == 1) {
						dialog.dialog('close');
						submit = true;
						form.submit();
					} else {
						hints.removeClass('important');
						hints.addClass('warning');
						hints.text(msg);
						password.get(0).select();
					}
				}
			});
		}
	}
	
	function ajaxResetUser() {
		removeErrors();
		
		if (checkLength(username, "Der Benutzername", 3, 60)) {
			showAjaxLoading('Das Passwort des Benutzers "' + username.val() + '" wird zurückgesetzt! Bitte warten!');
			$.ajax({
				type: "post",
				url: location.href,
				data: form.serialize() + '&form[reset]=1',
				cache: false,
				success: function(msg) {
					if (msg == 1) {
						hints.removeClass('info');
						hints.addClass('important');
						hints.text('Das Passwort für den Benutzer "' + username.val() + '" wurde zurückgesetzt! Sie erhalten in Kürze eine Email mit dem neuen Passwort!');
						loginControls.hide();
						$('.ui-dialog-buttonpane button:first').hide();					
					} else {
						hints.removeClass('important');
						hints.addClass('warning');
						hints.text(msg);
					}
				}
			});
		}
	}

	function removeErrors() {
		hints.removeClass('warning');
		hints.addClass('important');
		hints.text('Bitte geben Sie Ihren Benutzernamen und Passwort ein.');
		username.parent().removeClass('error');
		password.parent().removeClass('error');
	}
			
	function checkLength(o, n, min, max) {
		if (o.val().length > max || o.val().length < min) {
			hints.removeClass('important');
			hints.addClass('warning');
			hints.text(n + ' muss aus mindestens mindestens ' + min + ' und höchstens ' + max + ' Zeichen bestehen!').effect('pulsate', {}, 500);
			o.parent().addClass('error');
			return false;
		} 
		return true;
	}
	
	function toggleForms() {
		if (login) {
			login = false;
			passwordControls.hide();
			hints.removeClass('warning');
			hints.addClass('important');
			hints.text('Bitte geben Sie den Benutzernamen des Benutzers an, dessen Passwort zurückgesetzt werden soll!');
			getLoginBtn().html('<span class="ui-button-text"><img src="' + path + 'images/tick.png" alt=""/>Zurücksetzen</span>');
			getToggleBtn().html('<span class="ui-button-text"><img src="' + path + 'images/textfield_key.png" alt=""/>Login</span>');
			dialog.dialog('option', 'title', 'Passwort zurücksetzten');
 		} else {
			login = true;
			passwordControls.show();
			hints.text('Bitte geben Sie Ihren Benutzernamen und Passwort ein.');
			loginControls.show();
			getToggleBtn().html('<span class="ui-button-text"><img src="' + path + 'images/textfield_key.png" alt=""/>Passwort vergessen?</span>').show();	
			getLoginBtn().html('<span class="ui-button-text"><img src="' + path + 'images/tick.png" alt=""/>Anmelden</span>').show();
			dialog.dialog('option', 'title', 'Login');
		}
	}
	
	function getLoginBtn() {
		if (loginBtn == null) {
			loginBtn = $('.ui-dialog-buttonpane button:first');
		}
		return loginBtn;
	}
	
	function getToggleBtn() {
		if (toggleBtn == null) {
			toggleBtn = $('.ui-dialog-buttonpane button:odd');
		}
		return toggleBtn;
	}
	
	function getCancelBtn() {
		if (cancelBtn == null) {
			cancelBtn = $('.ui-dialog-buttonpane button:last');
		}
		return cancelBtn;
	}	
}	
