/**

Author			: Fabio Bonacina
Version			: 1.0.05
Created			: 16/08/2008
Last Modified	: 06/10/2009
Description		: 
Modifications	: 
	- dynamic load of active languages
	- added logout
	- fixed markup for panel title
	- added xml class flush
ToDo			: 
	- dynamic load of scripts
**/

$(document).ready(function(){
	login_check();
	$('#login_form')
		.attachValidator({
			 returnedvalue : false
			,noerrors_callback : function(e){
				$('#login_form')
					.slideUp('fast');
				$('#login_messages')
					.slideUp('slow', function(){
						$(this)
							.removeClass("yellow")
							.addClass("blue");
						$('.content:first', $(this))
							.removeClass("yeld")
							.addClass("working")
							.html("<h3>Signing in..</h3>Wait while system is checking for your credentials..");
						$(this)
							.slideDown("slow");
						$('#login_container')
							.find('.summary:first')
							.hide();
						login_verify();
					});
			}
			,errors_callback : function(e){
				var message = "<h3>Errors occurred</h3>";
				$('#login_form')
					.find('.yellow')
					.each(function(){
						message += "il campo <strong>"
								+ $(this).children('label').text()
								+ "</strong> non pu&ograve; essere vuoto<br />";
					});
				$('#login_messages')
					.slideDown('slow')
					.find('.content:first')
					.html(message);
			}
			,noerrors_class : "grey"
			,errors_class : "yellow"
		});
	$('input:text:first', $('#login_form'))
		.focus();
});

function login_logout(){
	Configuration.activityMessage = "Uscita dal sistema in corso..";
	$.post("provider.aspx"
		,{
			 act : "logout"
			,res : "auth"
			,session : User.session
		}
		,function(data, status){login_logoutDone(data)}
		,'xml'
	);
}

function login_check(){
	Configuration.activityMessage = "Caricamento dati di login in corso..";
	$.post("provider.aspx"
		,{
			 act : "check"
			,res : "auth"
			,session : User.session
		}
		,function(data, status){login_verifyCallback(data, "checking")}
		,'xml'
	);
}

function login_verify(){
	Configuration.activityMessage = "Autenticazione in corso..";
	$.post("provider.aspx"
		,{
			 user_name : $('#user_username').val()
			,user_password : $('#user_password').val()
			,act : "login"
			,res : "auth"
		}
		,function(data, status){login_verifyCallback(data)}
		,'xml'
	);
}

function login_verifyCallback(data, checking){	
	var errors = response_errors(data);
	var extraXML = $(response_extraxml(data));
	if(errors == null){
		$('#login_container').hide();
		User.id = extraXML.find('id').text();
		User.nickname = extraXML.find('displayname').text();
		User.lastSeen = extraXML.find('lastseen').text();
		User.session = extraXML.find('session').text();
		User.type = extraXML.find('type').text();
		active_languages_load();
		Menu = new phMenu(User);
		Area = new phArea();
		addLogoutIcon();
		if(User.type>=99)addXMLFlushIcon();
	}else{
		if(checking != "checking")
			$('#login_messages')
				.removeClass("blue")
				.addClass("yellow")
				.children('.content:first')
				.removeClass("working")
				.addClass("yeld")
				.html(string_htmlify("<h3>Si è verificato il seguente errore</h3>" + errors.description + "<br /><br />" + errors.level));
		$('#login_form')
			.slideDown('fast');
	}
}

function login_logoutDone(data){	
	location.replace(location.href.replace(/\?.*$/, '') + '?' + Math.random());
	location.reload();
}

var User = {
	 nickname : ""
	,type : 0
	,lastSeen : ""
	,session : ""
	,id : 0
};

function addLogoutIcon(){
	$('<a>')
		.addClass("log-out")
		.html("log-out")
		.attr({
			 href : "#"
			,title : "effettua il logout"
		})
		.click(function(){
			if(confirm("Sei sicuro di voler uscire dal gestionale ?")){
				login_logout();
			}else{return false;}
			
		})
		.appendTo(Area.$actions.parent('div:first').find('h2'));
}
function addXMLFlushIcon(){
	$('<a>')
		.addClass("xml-flush")
		.html("xml-flush")
		.attr({
			 href : "#"
			,title : "flush xml classes"
		})
		.click(function(){
			var c = prompt("Please insert class/es to flush");
			if(c !=null && c != ''){
				Configuration.activityMessage = "Flushing class/es..";
				$.post("manager.aspx"
					,{
						 act : "flush"
						,res : "xml"
						,'class' : c
						,session: User.session
					}
					,function(data, status){return true;}
					,'xml'
				);
				
			}else{return false;}
			
		})
		.appendTo(Area.$actions.parent('div:first').find('h2'));
}

