/*
 *	$Rev: 201 $:
 *	$Author: sergio $:
 *	$Date: 2010-06-30 21:08:33 +0200 (Wed, 30 Jun 2010) $:
 */

/*
 * 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(
				WebRootPath + "/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(
			WebRootPath + "/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(
			WebRootPath + "/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");
	}
}

var utils = {
	openLoginDialog : function (obj) {
		var boxLogin = document.getElementById("sm_box_login");
		var boxRecupera = document.getElementById("sm_box_recuperapwd");
	
		if (boxLogin.style.display == "none") {
			boxLogin.style.display = "block";
			obj.className = "selected";
		}
		
		else {
			boxLogin.style.display = "none";
			boxRecupera.style.display = "none"
			obj.className = "";
		}
	},
	openPassReminder : function () {
		var boxLogin = document.getElementById("sm_box_login");
		var boxRecupera = document.getElementById("sm_box_recuperapwd");
		
		boxLogin.style.display = "none";
		boxRecupera.style.display = "block";			
		
	},
	backToLogin : function () {
		var boxLogin = document.getElementById("sm_box_login");
		var boxRecupera = document.getElementById("sm_box_recuperapwd");
		
		boxLogin.style.display = "block";
		boxRecupera.style.display = "none";			
		
	},
	toggleEtichetta : function (obj, id) {
		var daEsplodere = document.getElementById(id);
				
		if (obj.className == "chiudi-etichetta") {
			daEsplodere.style.display = "none";
			obj.className = "apri-etichetta";
			obj.innerHTML = obj.innerHTML.replace("Chiudi", "Apri");
		}
		else {
			daEsplodere.style.display = "table";
			obj.className = "chiudi-etichetta";
			obj.innerHTML = obj.innerHTML.replace("Apri", "Chiudi");
		}
		
	},
	buongiornoBuonasera : function () {
		var d = new Date();
		var curr_hour = d.getHours();
		var curr_month = d.getMonth()+1;			
		if (curr_month < 5 || curr_month > 9) var orario = 16
		else var orario = 18
		if (curr_hour < orario) document.write("Buongiorno,")
		else document.write("Buonasera,")			
	}
}
