
var AuthForm = new Object();


AuthForm.authFormPath = "/zfforms/auth/";


AuthForm.procError = function(data)
{
	if (data.status == 'ok')
		return true;
	
	var messages = new Array();
	if (data.errors.login != undefined)
		messages.push('Ошибка: неверно указано имя');
	if (data.errors.password != undefined)
		messages.push('Ошибка: неверно указан пароль');
	if (data.errors.remember != undefined)
		messages.push('Ошибка: неверный флаг');

	if (data.errors.fatal != undefined)
		messages.push(data.errors.fatal);
	
	alert(messages.join(', '));
	return false;
}

AuthForm.submitForm = function()
{
	var authdata = {
		login: $('#authLogin').val(),
		password: $('#authPassword').val(),
		remember: $('#authRemember').attr('checked') ? 1 : 0
	};

	$.getJSON(AuthForm.authFormPath +"auth.php",
		authdata,
        function(data){
			if (AuthForm.procError(data)) {
				window.location.reload(true);
				//$('#auth_form').load(AuthForm.authFormPath + "form.php", null, AuthForm.handleExitLink);
			}
        }
	);

	return false;
}

AuthForm.handleAuthForm = function ()
{
	$('#authForm').validate({
		rules: {
			authLogin: {
				required: true,
				email: true
			},
			authPassword: {
				required: true,
				minlength: 4
			}
		},
		messages : {
			authLogin: {
				required: "Необходимо ввести логин"
			},
			authPassword: {
				required: "Необходимо ввести пароль"
			}			
		},
		wrapper: "div",
		errorClass: "errorauth"
	});
}
AuthForm.handleExitLink = function()
{
	$('#exitLink').click(function(){
		$.get(AuthForm.authFormPath +"unauth.php",
				null,
		        function(data){
			        window.location.reload(true);
					$('#auth_form').load(AuthForm.authFormPath + "form.php", null, AuthForm.handleAuthForm);
		        }
			);
	});
}

