function jsDoLogin(form) {
	username = form.elements["username"].value;
	password = form.elements["password"].value;
	form.elements["password"].value = '';
	ret = doLogin(username, password, {"content_type": "json", "type": "synchronous", "method": "GET"});
	if (ret.esito == "NO") {
		alert(ret.messaggio);
	}
	jsHideLogin();
	jsRenderWelcome();
}

function jsDoLogout() {
	ret = doLogout({"content_type": "json", "type": "synchronous"});
	if (ret.esito == "NO") {
		alert(ret.messaggio);
	}
  if (document.location.pathname == "/carrello.php" ||
      document.location.pathname == "/ordini.php" ||
      document.location.pathname == "/ordine.php" ||
      document.location.pathname == "/ordineRiepilogo.php") {
    document.location.href="index.php";
  }
	jsRenderWelcome();
}

function jsIsLogged() {
	return isLogged({"content_type": "json", "type": "synchronous"});
}

function jsRenderWelcome() {
	getWelcome({"target": "login", "type": "asynchronous"});
}

function jsHideLogin() {
	/* hide loginbox then fade and remove overlay */
	$('loginbox').hide();
	new Effect.Fade($('overlay'), { 
			duration: 1,
			from: 0.8, to: 0,
			queue: 'end',
			afterFinish: (function(){
				$('overlay').remove();
			})
	});
}

function jsShowLogin() {
	/* get the body and insert the overlay hidden */
	var objBody = document.body;
	var overlay = objBody.appendChild(Builder.node('div',{id:'overlay'}));
	overlay.setStyle({height:'100%'});
	overlay.hide();
	/* appear overlay then show loginbox */
	$('overlay').appear({
			duration: 1,
			from: 0, to: 0.8,
			queue: 'end',
			afterFinish: (function(){
				$('loginbox').show();
			})
	});
}

function jsSetLanguage(lang) {
	ret = setLanguage(lang, {"content_type": "json", "type": "synchronous", "method": "GET"});
}

