var UtenteId = "Utente"
var UtenteName = "Utente"
var UtenteModo  
var UtenteCategoria  
var UtenteSezione  
var UtenteLoaded = false;
var UtenteStyle = false;
var UtenteForm = false;
var UtenteServerPage = "/webiris/Modules/Utente/AjaxServer/index.aspx";
//var UtenteServerPage = "webiris/AjaxServer/index.aspx";

function Utente_init(ajaxIndex)
{
	var response = ajaxObjects[ajaxIndex].responseXML;
	// inizializza il modulo
	try
	{
		UtenteId=response.getElementsByTagName("contenitore")[0].childNodes[0].nodeValue;
	  	var moduleStyle=response.getElementsByTagName("style")[0].childNodes[0].nodeValue;
		if (UtenteStyle == false) {
			UtenteStyle = true;
			addStyle(moduleStyle); 
		}
	} catch (err) {
	}	

	clearAjaxSession(ajaxIndex); 
	UtenteLoaded = true;
	if (UtenteModo=='form') 
			Utente_formAjax();
	else if (UtenteModo=='list') 
			Utente_listAjax();
	else if (UtenteModo=='login') 
			Utente_loginAjax();
	else if (UtenteModo=='logout') 
			Utente_logoutAjax();
}

function Utente_showForm(ajaxIndex)
{
	replace_html(UtenteId, ajaxIndex)	

}

function Utente_showList(ajaxIndex)
{
	replace_html(UtenteId, ajaxIndex)	
}

function Utente_initAjax()
{
	if (!UtenteModo) {
		UtenteModo = 'form'
	}

	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = newAjaxSession(ajaxIndex, UtenteId, 'init')
//	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].isBookmark = false;
	ajaxObjects[ajaxIndex].requestFile = UtenteServerPage;
//	ajaxObjects[ajaxIndex].onLoading = show_progressbar(UtenteId);	// Action when AJAX is loading the file
	ajaxObjects[ajaxIndex].setVar('module', UtenteName);
//	ajaxObjects[ajaxIndex].setVar('func','init');
	ajaxObjects[ajaxIndex].onCompletion = function(){ Utente_init(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}

function Utente_formAjax()
{
	if (!UtenteLoaded) {
		UtenteModo='form'
		Utente_initAjax();
		return;
	}

	Utente_setForm()
	
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = newAjaxSession(ajaxIndex, UtenteId, 'form')
//	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = UtenteServerPage;
	ajaxObjects[ajaxIndex].isBookmark = false;
//	ajaxObjects[ajaxIndex].onLoading = show_progressbar(UtenteId);	// Action when AJAX is loading the file
	ajaxObjects[ajaxIndex].setVar('module', UtenteName);
//	ajaxObjects[ajaxIndex].setVar('func','form');
	ajaxObjects[ajaxIndex].onCompletion = function(){ Utente_showForm(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}

function Utente_listAjax()
{
	if (!UtenteLoaded) {
		UtenteModo='list'
		Utente_initAjax();
		return;
	}

	Utente_setForm()

	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = newAjaxSession(ajaxIndex, UtenteId, 'list')
//	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = UtenteServerPage;
	ajaxObjects[ajaxIndex].isBookmark = false;
//	ajaxObjects[ajaxIndex].onLoading = show_progressbar(UtenteId);	// Action when AJAX is loading the file
	ajaxObjects[ajaxIndex].setVar('module', UtenteName);
//	ajaxObjects[ajaxIndex].setVar('func','list');
	ajaxObjects[ajaxIndex].onCompletion = function(){ Utente_showList(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}

// Chiama la funzione login passando i campi del form
function Utente_loginAjax()
{
	if (!UtenteLoaded) {
		UtenteModo='login'
		Utente_initAjax();
		return;
	}

	Utente_setForm()

	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = newAjaxSession(ajaxIndex, UtenteId, 'login')
//	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].isBookmark = false;
	ajaxObjects[ajaxIndex].requestFile = UtenteServerPage;

	ajaxObjects[ajaxIndex].setVar('module', UtenteName);
//	ajaxObjects[ajaxIndex].setVar('func','login');
	UtenteForm.submit( ajaxObjects[ajaxIndex] )
	
	ajaxObjects[ajaxIndex].onCompletion = function(){ Utente_getForm(ajaxIndex); };	// Specify function that will be executed after file has been found
	
	ajaxObjects[ajaxIndex].onLoading = show_progressbar(UtenteId);	// Action when AJAX is loading the file
	
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}

// Chiama la funzione login passando i campi del form
function Utente_logoutAjax()
{
	if (!UtenteLoaded) {
		UtenteModo='logout'
		Utente_initAjax();
		return;
	}
	Utente_setForm()

	var ajaxIndex = ajaxObjects.length;
//	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex] = newAjaxSession(ajaxIndex, UtenteId, 'logout')
	ajaxObjects[ajaxIndex].requestFile = UtenteServerPage;
	ajaxObjects[ajaxIndex].isBookmark = false;
//	ajaxObjects[ajaxIndex].onLoading = show_progressbar(UtenteId);	// Action when AJAX is loading the file
	ajaxObjects[ajaxIndex].setVar('module', UtenteName);
//	ajaxObjects[ajaxIndex].setVar('func','logout');
	ajaxObjects[ajaxIndex].onCompletion = function(){ Utente_showForm(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}

// Controlla esito 
function Utente_getForm(ajaxIndex)
{
	var response = ajaxObjects[ajaxIndex].responseXML;
	try
	{
		var stato=response.getElementsByTagName("stato")[0].childNodes[0].nodeValue;
		if (stato!='ok') {
	  		var msg=response.getElementsByTagName("msg")[0].childNodes[0].nodeValue;
	  		var field=response.getElementsByTagName("field")[0].childNodes[0].nodeValue;
			UtenteForm.error(field, msg)
		}
		clearAjaxSession(ajaxIndex); 
		hide_progressbar(UtenteId)	  	
	} catch (err) {
		show_progressbar(UtenteId);
		Utente_showForm(ajaxIndex)	
	}
}
// Crea  form
function Utente_setForm()
{
	if (!UtenteForm) {
		UtenteForm = new FormValidate("login");
		UtenteForm.add('u_utente', 'req|email','Y', 'Nome utente non valido')		
		UtenteForm.add('u_pwd', 'req|psw','Y', 'Password non valida')		
	}
}

