/*
 * Al contrario degli include php, non è necessario che questo file stia per forza in questa directory
 * può essere spostato ovunque.
 */

var account = {
	login : function(_UserName, _Password, ricordami, busy_start, busy_stop, callback_ok, callback_ko){
		busy_start();
		$.post(
			"/SM_public/lib/auth/server/account_check.php", 
			{ action : "login", UserName: _UserName, Pwd: _Password }, 
			function (data, textStatus) {
				busy_stop();
				if(textStatus == "success"){
					if(data.result == true){
						var nome_cognome = data.nome + " " + data.cognome;
						$.cookie('nome_cognome', nome_cognome, { expires: 7, path: '/', secure: false });						
						if(ricordami){
							$.cookie('UserName', _UserName, { expires: 7, path: '/', secure: false });						
							$.cookie('Password', _Password, { expires: 7, path: '/', secure: false });						
						}
						else{
							$.cookie('UserName', null, {path: '/'});						
							$.cookie('Password', null, {path: '/'});						
						}
						// imposto il cookie della sessione (scadenza alla chiusura del browser)
						$.cookie('IDSessione', data.idsessione, {path: '/'});						
						
						// chiamo la funzione di feedback
						callback_ok(nome_cognome);
					}
					else
						callback_ko(data.messaggio);
				}
				else
					alert(textStatus);
			},
			"json");
	},
	recuperaPassword : function(_UserName, _Email, busy_start, busy_stop, callback_ok, callback_ko){
		busy_start();
		$.post(
			"/SM_public/lib/auth/server/account_check.php", 
			{ action : "recuperaPassword", UserName: _UserName, Email: _Email }, 
			function (data, textStatus) {
				busy_stop();
				if(textStatus == "success"){
					if(data.result == true)
						callback_ok(data.messaggio);
					else
						callback_ko(data.messaggio);
				}
				else
					alert(textStatus);
			},
			"json");
	},
	registrazione : function(account_manager_path, conferma_path, jsonStr, busy_start, busy_stop, callback_ok, callback_ko){
		var qs = new Pix.ws.queryString.obj();
		Pix.ws.queryString.addParameter(qs, "action", "user_crea");
		Pix.ws.queryString.addParameter(qs, "conferma_path", conferma_path);
		Pix.ws.queryString.addParameter(qs, "jsonStr", jsonStr);
	
		var onTimeOut = null;
		var onError = null;
		var async = true;
		
		busy_start();
	    Pix.ws.send(account_manager_path + "server/account_edit.php", qs, function(result){
			busy_stop();

			if (result == true) 
				callback_ok();
			else
				callback_ko(result);
		}, onTimeOut, onError, async);
	}
}
