// JavaScript Document
ce = 0;
cn = 0;

function eventiAvanti() {
	if (switchs) { return false; }
	nEventi = document.getElementById('nEventi').innerHTML;
	if (nEventi <= 1) { return; }
	dirEvnt = true;
	ce += 1;
	if (ce >= nEventi) { ce = 0; }
	startRequest("http://www.targabistrot.net/php/scorrimentoEventi.php?limite="+ce);
}

function eventiIndietro() {
	if (switchd) { return false; }
	nEventi = document.getElementById('nEventi').innerHTML;
	if (nEventi <= 1) { return; }
	dirEvnt = false;
	ce -= 1;
	if (ce < 0) { ce = nEventi - 1; }
	startRequest("http://www.targabistrot.net/php/scorrimentoEventi.php?limite="+ce);
}

function newsAvanti() {
	if (switchsu) { return false; }
	nNews = document.getElementById('nNews').innerHTML;
	if (nNews <= 2) { return; }
	dirNews = true;
	cn += 2;
	if (cn >= nNews) { cn = 0; }
	startRequest("http://www.targabistrot.net/php/scorrimentoNews.php?limite="+cn);
}

function newsIndietro() {
	if (switchgiu) { return false; }
	nNews = document.getElementById('nNews').innerHTML;
	if (nNews <= 2) { return; }
	dirNews = false;
	cn -= 2;
	if (cn < 0) { 
		cn = parseInt(nNews);
		pari = (cn%2 == 1 ?  false : true);
		if (pari) { cn = cn - 2; } else { cn = cn - 1; }
	}
	startRequest("http://www.targabistrot.net/php/scorrimentoNews.php?limite="+cn);
}

// --------------------------------------------- RICHIESTA

var xmlhttp;

function startRequest(url) {
// codice per Mozilla, etc.
if (window.XMLHttpRequest) {
  xmlhttp= new XMLHttpRequest();
  xmlhttp.onreadystatechange = xmlhttpChange;
  xmlhttp.open("GET", url, true);
  xmlhttp.send(null);
  }
// codice per  IExplore
else if (window.ActiveXObject)
  {
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    if (xmlhttp)
    {
    xmlhttp.onreadystatechange = xmlhttpChange;
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
    }
  }
}

function xmlhttpChange() {
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4) {
  // if "OK"
  if (xmlhttp.status==200) {
    elaboraRisposta();
  	}
  else {
    alert("Impossibile ricevere i dati");
    }
  }
}

function elaboraRisposta() {
	var risposta = xmlhttp.responseText;
	risposta = risposta.split(":::");
	switch (risposta[0]) {
		case "1":
			scorriEventi(risposta[1]);
		break;
		case "2":
			scorriNews(risposta[1]);
		break;
		default:
			return;
		break;
	}
	
	
}

// --------------------------------------------- RISPOSTA E SCORRIMENTO EVENTI

var s;
var ms = 0;
var switchs = 0;
var d;
var md = -480;
var switchd = 0;
var dirEvnt = false;

function scorriEventi(risp) {
	divoEvnt = document.getElementById("eventi");
	risp = risp.split(";;;");
	if(risp[0] !== "N") {
		if (dirEvnt) {
			nuovodivo = document.createElement("DIV");
			nuovodivo.className = 'd480 h300';
			nuovodivo.innerHTML = risp[0];
			divoEvnt.style.width = "960px";
			divoEvnt.appendChild(nuovodivo);
			scorriSx();
		}
		if (!dirEvnt) {
			nuovodivo = document.createElement("DIV");
			nuovodivo.className = 'd480 h300';
			nuovodivo.innerHTML = risp[0];
			divoEvnt.style.width = "960px";
			primo = divoEvnt.firstChild;
			divoEvnt.insertBefore(nuovodivo, primo);
			divoEvnt.style.left = "-480px";
			scorriDx();
		}
	}
}

// scrolling sinistra
function scorriSx() {
	switchs = 1;
    s = window.setInterval ( "sx()", 1 );
}

// scrolling destra
function scorriDx() {
	switchd = 1;
    d = window.setInterval ( "dx()", 1 );
}

function sx() {
	divoEvnt = document.getElementById("eventi");
	if (ms > -480) {
		ms = ms - 10;
		divoEvnt.style.left = ms + "px";
	} else {
		primo = divoEvnt.firstChild;
		divoEvnt.removeChild(primo);
		primo = divoEvnt.firstChild;
		divoEvnt.style.left = "0px";
		divoEvnt.style.width = "480px";
		window.clearInterval(s);
		ms = 0;
		switchs = 0;
	}
}

// funzione destra
function dx() {
	divoEvnt = document.getElementById("eventi");
	if (md < 0) {
		md = md + 10;
		divoEvnt.style.left = md + "px";
	} else {
		ultimo = divoEvnt.lastChild;
		divoEvnt.removeChild(ultimo);
		divoEvnt.style.left = "0px";
		divoEvnt.style.width = "480px";
		window.clearInterval(d);
		md = -480;
		switchd = 0;
	}
    
}

// --------------------------------------------- RISPOSTA E SCORRIMENTO NEWS

var su;
var msu = 0;
var switchsu = 0;
var giu;
var mgiu = -420;
var switchgiu = 0;
var dirNews = false;

function scorriNews(risp) {
	divoNews = document.getElementById('news');
	risp = risp.split(";;;");
	if(risp[0] !== "N") {
		if (dirNews) {
			nuovodivo = document.createElement("DIV");
			nuovodivo.className = 'd420 h300';
			nuovodivo.innerHTML = risp[0];
			divoNews.style.width = "840px";
			divoNews.appendChild(nuovodivo);
			scorriSu();
		}
		if (!dirNews) {
			nuovodivo = document.createElement("DIV");
			nuovodivo.className = 'd420 h300';
			nuovodivo.innerHTML = risp[0];
			divoNews.style.width = "840px";
			primo = divoNews.firstChild;
			divoNews.insertBefore(nuovodivo, primo);
			divoNews.style.left = "-420px";
			scorriGiu();
		}
	}
}

function scorriSu() {
	switchsu = 1;
    su = window.setInterval ( "vaiSu()", 1 );
}

function scorriGiu() {
	switchgiu = 1;
    giu = window.setInterval ( "vaiGiu()", 1 );
}

function vaiSu() {
	divoNews = document.getElementById('news');
	if (msu > -420) {
		msu = msu - 10;
		divoNews.style.left = msu + "px";
	} else {
		primo = divoNews.firstChild;
		divoNews.removeChild(primo);
		primo = divoNews.firstChild;
		divoNews.style.left = "0px";
		divoNews.style.width = "420px";
		window.clearInterval(su);
		msu = 0;
		switchsu = 0;
	}
}

function vaiGiu() {
	divoNews = document.getElementById('news');
	if (mgiu < 0) {
		mgiu = mgiu + 10;
		divoNews.style.left = mgiu + "px";
	} else {
		ultimo = divoNews.lastChild;
		divoNews.removeChild(ultimo);
		divoNews.style.left = "0px";
		divoNews.style.width = "420px";
		window.clearInterval(giu);
		mgiu = -420;
		switchgiu = 0;
	}
    
}

// --------------------------------------------- LINK EVENTO

function vaiEvento(idevento) {
	document.location.href = 'eventi.php?idevento='+idevento+'&ce='+ce;
}

// --------------------------------------------- ANIMAZIONE EVENTI

function scorrimentoEventi() {
	scorrimentoE = window.setInterval("eventiAvanti()", 10000);
}

// --------------------------------------------- PROMPT LOGIN

var sLogin = false;
h = 0;

function switchPrompt() {
	if(sLogin) {
		animazionePl = window.setInterval('nascondiPromptLogin()', 10);
		sLogin = false;
	} else {
		animazionePl = window.setInterval('mostraPromptLogin()', 10);
		sLogin = true;
	}
}

function mostraPromptLogin() {
	if (h < 60) {
		h = h + 2;
		document.getElementById('prompt').style.height = h+"px";
	} else {
		window.clearInterval(animazionePl);	
	}
}

function nascondiPromptLogin() {
	if (h > 0) {
		h = h - 2;
		document.getElementById('prompt').style.height = h+"px";
	} else {
		window.clearInterval(animazionePl);	
	}
}

function svuotaInput(obj) {
	obj.value = "";
	obj.style.color = 'black';
}


function checkSubmit() {
	modulo = document.getElementById('login');
	
	if (modulo.nick.value == "" || modulo.nick.value == "Amministratore") {     
	alert("Occorre inserire un nome utente");     
	return false;   
	}

	if (modulo.pass.value == "" || modulo.pass.value == "password") {     
	alert("Occorre inserire una password");     
	return false;   
	}

	document.forms['login'].submit();
}

function newsletterSubmit() {
	modulo = document.getElementById('newsletter');
	
	if (modulo.mailnewsletter.value == "" || modulo.mailnewsletter.value == "Email") {     
	alert("Occorre inserire un indirizzo email");     
	return false;   
	}

	document.forms['newsletter'].submit();
}
