var P = {
	inizializza: function() {
		JS.$('container').setVisible(false);
		
		this.schermo(Cookies.read('schermo', 1000), true);
		this.testo(Cookies.read('testo', 'medio'), true);
		this.colore(Cookies.read('colore', 'grigio'), true);
		
		JS.$('container').setVisible(true);
	},
	
	schermo: function(valore, visibile) {
		JS.$('container').setVisible(false);
		
		var w = Math.min(parseInt(valore), JS.Document.getW());
		var h = JS.Document.getH();
		
		var oHead = JS.$('head');
		if (oHead) {
			oHead.setW(w);
			h -= oHead.getH();
		}
		
		var oFoot = JS.$('foot');
		if (oFoot) {
			oFoot.setW(w);
			h -= oFoot.getH();
		}
		
		var oPage = JS.$('page');
		if (oPage) {
			oPage.setW(w);
			oPage.setH(h);
		}
		
		Cookies.write('schermo', w);
		
		if (!visibile) JS.$('container').setVisible(true);
	},
	
	testo: function(valore, visibile) {
		JS.$('container').setVisible(false);
		
		var oLink = JS.$('testo');
		if (oLink) {
			var href = oLink.getValue().substring(0, oLink.getValue().lastIndexOf('/') + 1);
			oLink.setValue(href + valore + '.css');
		}
		
		Cookies.write('testo', valore);
		
		if (!visibile) JS.$('container').setVisible(true);
	},
	
	colore: function(valore, visibile) {
		JS.$('container').setVisible(false);
		
		var oLink = JS.$('colore');
		if (oLink) {
			var href = oLink.getValue().substring(0, oLink.getValue().lastIndexOf('/') + 1);
			oLink.setValue(href + valore + '.css');
		}
		
		Cookies.write('colore', valore);
		
		if (!visibile) JS.$('container').setVisible(true);
	},
	
	chiama: function(classe, metodo, parametri) {
		var risposta = '';
		
		Ajax.Http.create();
		if (Ajax.Http.isCreated()) {
			Ajax.Http.request('/action' +
												'?context=' + 'pneus' +
												'&class=' + classe +
												'&method=' + metodo +
												(parametri != null ? parametri : '')
											 );
			risposta = Ajax.Http.response();
		}
		Ajax.Http.destroy();
		
		return risposta;
	},
	
	Home: {
		carica: function() {
			var risposta = P.chiama('Home', 'load');
			if (risposta.length > 0) {
				JS.$('page').setValue(risposta);
			}
		}
	},
	
	Autenticazione: {
		inizializza: function() {
			JS.$('username').focus();
		},
		
		controlla: function(livello) {
			livello = (livello != null ? (livello == 1 ? '../' : '../../') : '');
			var accesso = Cookies.read('accesso', '-1');
			
			var risposta = P.chiama('Autenticazione', 'check',
															'&accesso=' + accesso
														 );
			if (risposta.length > 0) {
				if (risposta.indexOf('Exception') > -1) {
					alert(risposta);
				} else {
					if (accesso != '-1' && risposta == '-1') {
						JS.Host.setHref(livello + 'autenticazione');
					}
				}
			}
		},
		
		cambia: function(codconto) {
			var risposta = P.chiama('Autenticazione', 'load',
															'&codconto=' + codconto
														 );
			if (risposta.length > 0) {
				if (risposta.indexOf('Exception') > -1) {
					alert(risposta);
				} else {
					// do nothing
				}
			}
		},
		
		entra: function() {
			var risposta = P.chiama('Autenticazione', 'enter',
															'&username=' + JS.$('username').getValue() +
															'&password=' + JS.$('password').getValue()
														 );
			if (risposta.length > 0) {
				if (risposta.indexOf('Exception') > -1) {
					JS.$('messaggio').setValue(risposta.substring(10));
					this.annulla();
				} else {
					Cookies.write('accesso', risposta);
					JS.Host.setHref('../');
				}
			}
		},
		
		annulla: function() {
			JS.$('username').setValue('');
			JS.$('password').setValue('');
			this.inizializza();
		},
		
		nuovo: function() {
			JS.Host.setHref('../registrazione');
		},
		
		cancella: function() {
			Cookies.write('accesso', '-1');
			JS.Host.reload();
		}
	},
	
	Recupera: {
		inizializza: function() {
			if (JS.$('username').getValue() == '') {
				JS.$('username').focus();
			} else if (JS.$('email').getValue() == '') {
				JS.$('email').focus();
			} else {
				JS.$('conferma').setValue('');
				JS.$('conferma').focus();
			}
		},
		
		entra: function() {
			var risposta = P.chiama('Autenticazione', 'call',
															'&username=' + JS.$('username').getValue() +
															'&email=' + JS.$('email').getValue() +
															'&conferma=' + JS.$('conferma').getValue()
														 );
			if (risposta.length > 0) {
				if (risposta.indexOf('Exception') > -1) {
					JS.$('messaggio').setValue(risposta.substring(10));
					this.annulla();
				} else {
					JS.Host.setHref('../');
				}
			}
		},
		
		annulla: function() {
			JS.$('username').setValue('');
			JS.$('email').setValue('');
			JS.$('conferma').setValue('');
			this.inizializza();
		}
	},
	
	Modifica: {
		inizializza: function() {
			if (JS.$('username').getValue() == '') {
				JS.$('username').focus();
			} else if (JS.$('password').getValue() == '') {
				JS.$('password').focus();
			} else if (js.$('nuova').getValue() == '') {
				JS.$('nuova').focus();
			} else {
				JS.$('conferma').setValue('');
				JS.$('conferma').focus();
			}
		},
		
		entra: function() {
			var risposta = P.chiama('Autenticazione', 'update',
															'&username=' + JS.$('username').getValue() +
															'&password=' + JS.$('password').getValue() +
															'&nuova=' + JS.$('nuova').getValue() +
															'&conferma=' + JS.$('conferma').getValue()
														 );
			if (risposta.length > 0) {
				if (risposta.indexOf('Exception') > -1) {
					JS.$('messaggio').setValue(risposta.substring(10));
					this.annulla();
				} else {
					JS.Host.setHref('../../');
				}
			}
		},
		
		annulla: function() {
			JS.$('password').setValue('');
			JS.$('nuova').setValue('');
			JS.$('conferma').setValue('');
			this.inizializza();
		}
	}
};