function go(){
	var pass = document.getElementById('pass').value;
	var passDue = document.getElementById('passDue').value;
	if((pass != passDue) || pass == ''){
		alert('Le password non coincidono!');
		return false;
	}else{
		theCapt();
	}
}

function theCapt(){
	var capt = document.getElementById('capt').value;
	var nick = document.getElementById('nick').value;
	var mod = document.getElementById('mod').value;
	if(mod != 0){
		var ignore = 1;
	}else{
		var ignore = 0;
	}
	var r = Math.random();
	myRequest = CreateXmlHttpReq(function() {RegisterHandler(ignore)});
	myRequest.open("GET","myLogin.php?x="+encodeURIComponent(r)+"&capt="+encodeURIComponent(capt)+"&nick="+encodeURIComponent(nick));
	myRequest.send(null);
}

function RegisterHandler(ignore) {
	if (myRequest.readyState == 4 && myRequest.status == 200) {
		var e = myRequest.responseText;
		if(e == "040" && ignore==0){
			alert("Spiacenti, il nickname non e' disponibile");
			return false;
		}else if(e == '020' || (e=='040' && ignore == 1)){
			document.myRegForm.submit();
			return false;
		}else if(e == '030'){
			alert('Controllare il codice Alfanumerico');
			return false;
		}
	}
}

function accedi(){
	MostraCentra('LoaDing');
	setTimeout("accedi2()", 0);
}

function accedi2(){
	var user = document.getElementById('theUser').value;
	var pass = document.getElementById('thePass').value;
	if(user == '' || user == 'Nome Utente'){
		alert("Inserire il Nome Utente!");
		document.getElementById('theUser').focus();
		Nascondi('LoaDing');
		return false;
	}
	if(pass == '' || pass == 'Password'){
		alert("Inserire la Password!");
		document.getElementById('thePass').focus();
		Nascondi('LoaDing');
		return false;
	}
	var r = Math.random();
	myRequest = CreateXmlHttpReq(function() {LoginHandler()});
	myRequest.open("GET","myLogin.php?x="+encodeURIComponent(r)+"&user="+encodeURIComponent(user)+"&pass="+encodeURIComponent(pass));
	myRequest.send(null);
}

function logOut(){
	MostraCentra('LoaDing');
	setTimeout("logOut2()", 0);
}

function logOut2(){
	var r = Math.random();
	myRequest = CreateXmlHttpReq(function() {LoginHandler()});
	myRequest.open("GET","myLogin.php?x="+encodeURIComponent(r)+"&logOut=1");
	myRequest.send(null);
}


function LoginHandler() {
	if (myRequest.readyState == 4 && myRequest.status == 200) {
		var e = myRequest.responseText;
		if(e == '020'){
			window.location = 'prodotto.php';
			return false;
		}else if(e == '0'){
			alert('Combinazione Nome Utente / Password errata.');
			Nascondi('LoaDing');
			return false;
		}else if(e == '0201'){
			window.location = 'index.php';
			return false;
		}else{
			alert('Errore');
			Nascondi('LoaDing');
			return false;
		}
	}
}


function findTop(obj) {
	var curtop = 0;
	var target = document.getElementById(obj);
	while (target.offsetParent) {
	curtop += target.offsetTop - target.scrollTop;
	target = target.offsetParent;
	}
	return curtop;
}

function findLeft(obj) {
	var curleft = 0;
	var target = document.getElementById(obj);
	while (target.offsetParent) {
		curleft += target.offsetLeft - target.scrollLeft;
		target = target.offsetParent;
	}
	return curleft;
}

function Decimali(valore, x){
	myNum = String(valore);
	var dot = myNum.indexOf(".");
	if(dot == "-1"){
//		alert(valore+" "+dot);
		return valore;
	}
	var lenTutto = myNum.length;
	var lenDecimali = (lenTutto-dot)-1;
	var Intero = myNum.substr(0, dot);
	var Decimali = myNum.substr((dot+1), lenDecimali);
	if(Decimali.length > x){
		primiDue = Decimali.substr(0,x);
		terzo = Decimali.substr(x,1);
		if(terzo > 4){
			primiDue++;
		}
		Decimali = primiDue;
	}
	if(Decimali=="100"){
		Decimali = 0;
		Intero++;
	}
	var Finito = Intero + '.' + Decimali;
//	alert(Finito+" "+dot);
	return(Finito);
}


function spotSud(master, victim){
	Nascondi(victim);
	var myFix = document.getElementById(master);
	var myDyn = document.getElementById(victim);
	myDyn.style.display = 'block';
	var fixLen = myFix.offsetWidth;
	var fixHei = myFix.offsetHeight;
	var fixLeft = findLeft(master);
	var fixTop = findTop(master);
	var dynLen = myDyn.offsetWidth;
	var dynHei = myDyn.offsetHeight;
	var screenLen = document.body.clientWidth;
	var screenHei=document.body.clientHeight;
	/*
	alert("FIX - Len: "+fixLen+"| Hei: "+fixHei+"| Left: "+fixLeft+"| Top: "+fixTop);
	alert("DYN - Len: "+dynLen+"| Hei: "+dynHei);
	alert("SCREEN - Len: "+screenLen+"| Hei: "+screenHei);
	*/
	if((fixLeft + fixLen + dynLen) > screenLen){
		var myLeft = (fixLeft +fixLen) - dynLen;
		var myTop = fixTop + fixHei;
		
	}else{
		var myLeft = fixLeft + fixLen;
		var myTop = fixTop;
	}
	if((myTop + dynHei) > screenHei){
		myTop -= (dynHei + fixHei);
	}
	myDyn.style.left = myLeft;
	myDyn.style.top = myTop;
	myDyn.style.visibility = 'visible';
	myDyn.style.display = 'block';
}



function Mostra(quale){
	var r = document.getElementById(quale);
	r.style.visibility = 'visible';
	r.style.display = 'block';
}

function MostraCentra(div){
	var hC=document.body.clientHeight;
	var wC=document.body.clientWidth;
	var sL = document.body.scrollLeft;
	
	myDiv = document.getElementById(div);
	
	myDiv.style.visibility = 'visible';
	myDiv.style.display = 'block';
	
	var myDivW = myDiv.offsetWidth;
	var myDivH = myDiv.offsetHeight;
	
	var myLeft = (wC/2)-(myDivW/2)+document.body.scrollLeft;
	var myTop = (hC/2)-(myDivH/2)+document.body.scrollTop;
	
	
	myDiv.style.top = myTop;
	myDiv.style.left = myLeft;
}


function Nascondi(quale){
	var r = document.getElementById(quale);
	r.style.visibility = 'hidden';
	r.style.display = 'none';
}

function illu(quale){
	var r = document.getElementById(quale);
	if(r.style.visibility == 'hidden'){
		r.style.visibility = 'visible';
		r.style.display = 'block';
	}else{
		r.style.visibility = 'hidden';
		r.style.display = 'none';
	}
}

function CreateXmlHttpReq(handler) {
	var xmlhttp = null;
	try {
		xmlhttp = new XMLHttpRequest();
	}catch(e){
		try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	xmlhttp.onreadystatechange = handler;
	return xmlhttp;
}

function StandardMyHandler(div) {
	if (myRequest.readyState == 4 && myRequest.status == 200) {
		e = document.getElementById(div);
		e.innerHTML = myRequest.responseText;
		Nascondi('LoaDing');
	}
}

function StandardReq(x, div){
	MostraCentra('LoaDing');
	var Search = document.getElementById('TxtSrc').value;
	var Num = document.getElementById('NumSrc').value;
	var r = Math.random();
	myRequest = CreateXmlHttpReq(function() {StandardMyHandler(div)});
	myRequest.open("GET","emptyPre.php?cosa="+encodeURIComponent(x)+"&r=RestOfMyVar");
	myRequest.send(null);
}
function hereWeGo(){
	var destinazione = document.getElementById('mySezione').value;
	window.location = destinazione+'.php';
}