/*
 * 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(conferma_path, jsonStr, busy_start, busy_stop, callback_ok, callback_ko){

		busy_start();
		$.post(
			"/SM_public/lib/auth/server/account_edit.php", 
			{ action : "user_crea", conferma_path: conferma_path, jsonStr: jsonStr }, 
			function (data, textStatus) {
				busy_stop();
				if (data.toString().substr(0, 6) == "<br />") {
					OpenWindow = window.open("", "newwin", "height=250, width=250,toolbar=no,scrollbars=" + scroll + ",menubar=no");
					OpenWindow.document.write(data);
					OpenWindow.document.close();
				}
				else {
					if (textStatus == "success") {
						if (data.result == true)
							callback_ok();
						else 
							callback_ko(data.messaggio);
					}
					else 
						alert(textStatus);
				}
			},
			"json");
	}
}