function assegnaXMLHttpRequest() {

 XHR = null,
 
 // informazioni sul nome del browser
 browserUtente = navigator.userAgent.toUpperCase();

 if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
  XHR = new XMLHttpRequest();

 else if(
  window.ActiveXObject &&
  browserUtente.indexOf("MSIE 4") < 0
 ) {
 
  if(browserUtente.indexOf("MSIE 5") < 0)
   XHR = new ActiveXObject("Msxml2.XMLHTTP");

  else
   XHR = new ActiveXObject("Microsoft.XMLHTTP");
 }
	
 return XHR;
}
function loginUser(theform){
	var ajax = assegnaXMLHttpRequest();
	if(ajax)
		
	{
		uuss=theform.username.value.replace('&','ampersaund')
		ppss=theform.password.value.replace('&','ampersaund')
		ajax.open("get", "/checklogin.asp?par1="+uuss+"&par2="+ppss, true);
		ajax.onreadystatechange = function()
		{
			if (ajax.readyState == 4) 
			{
				//changeBodyClass('standby');
				var xmlDoc = ajax.responseXML;
				if (xmlDoc.documentElement)
				{
					var RESULT = xmlDoc.documentElement.getElementsByTagName("RESULT")[0].childNodes[0].nodeValue;
					if (RESULT=="OK")
					{
						var ID_UTE = xmlDoc.documentElement.getElementsByTagName("ID_UTE")[0].childNodes[0].nodeValue;
						var USR_UTE = xmlDoc.documentElement.getElementsByTagName("USR_UTE")[0].childNodes[0].nodeValue;
						var NOME_UTE = xmlDoc.documentElement.getElementsByTagName("NOME_UTE")[0].childNodes[0].nodeValue;
						
						
						
						setCookie("STUDIOPAA_ID_UTE",ID_UTE,1);
						setCookie("STUDIOPAA_USR_UTE",USR_UTE,1);
						setCookie("STUDIOPAA_NOME_UTE",NOME_UTE,1);
						
						
						setDatiLogin(ID_UTE,USR_UTE,NOME_UTE);
						div = document.getElementById('registrati-box');
					
						
						div.cont=0;
						div.limit=222;
						div.inc=8;
						div.freq=30;
						div.h=0;
						div2 = document.getElementById('loggato-box');
						div2.cont=0;
						div2.limit=130;
						div2.inc=8;
						div2.freq=30;
						div2.h=0;
						//slidebox(div,div2,0);
						$('#p1').fadeIn();
						$('#private').fadeIn();
						
						$('#loggato-box').slideDown();
						$('#registrati-box').slideUp();
						/*alertDiv('Bentornato '+NOME_UTE+'! Ora hai accesso ai documenti tecnici dei prodotti scaricabili in PDF, e all\'area Downloads in cui troverai altri documenti utili.\nGrazie e buona navigazione!',350);*/
						
						setTimeout('location.href="/utente/private.asp"',1000)
					
					}
					else
					{
						alertDiv('Nome utente e password errate!');
						focusError(theform);
					}
				}
			}
		}
	ajax.send(null);
	}
}

function slidebox(div,div2,direzione){
	
	if (direzione==0)
	{
		div.timer = setInterval(function()
		{
		riduci(div,div2);
		},div.freq);
	}
	else
	{
		div2.timer = setInterval(function()
		{
		esplodi_d(div2);
		},div2.freq);
	}
}

function esplodi_d(div)
{
	if(div.limit-div.h==0)
	{
		clearInterval(div.timer);
		div.style.height=(div.limit)+"px";
	}
	else
	{
		div.style.display="block";
		div.res=Math.ceil((div.limit-div.h)/10)
		div.style.height=(div.h+div.res)+"px";
		div.h=div.h+div.res;
	}
}

function riduci(div,div2)
{
	if(div.limit-div.h==0)
	{
		div.style.height="1px";
		//div.style.display="none";
		clearInterval(div.timer);
		slidebox("",div2,1);
		div.style.marginTop="0px";
		div.style.marginBottom="0px";
		div.style.display="none";
	}
	else
	{
		div.res=Math.ceil((div.limit-div.h)/10)

		div.style.height=(div.limit-(div.h+div.res))+"px";
		div.h=div.h+div.res;
	}
}

function riduci_old(div,div2){
	
	if (div.cont>div.limit-div.inc)
	{
		div.style.height="0px";
		//div.style.display="none";
		clearInterval(div.timer);
		slidebox("",div2,1);
	}
	else
	{
		div.cont=div.cont+div.inc;
		div.style.height=(div.limit-div.cont)+"px";
	}
}

function esplodi_d_old(div){
	
	if (div.cont>div.limit-div.inc)
	{
		clearInterval(div.timer);
	}
	else
	{
		div.style.display="block";
		div.cont=div.cont+div.inc;
		div.style.height=div.cont+"px";
	}
}

function focusError(theform){
	/*div = document.getElementById("log-mex-error");
	div.style.height="20px";
	div.innerHTML="Nome utente e password errate!";*/
	theform.username.style.border="solid 1px #ff0000";
	theform.username.style.background="#FFD2D2";
	theform.password.style.border="solid 1px #ff0000";
	theform.password.style.background="#FFD2D2";	
}

function logoutEuro(){
	delCookie("STUDIOPAA_ID_UTE");
	delCookie("STUDIOPAA_USR_UTE");
	delCookie("STUDIOPAA_NOME_UTE");
	div = document.getElementById("log-mex-error");
	div.innerHTML="";
	div.style.height="0px";
	/*theform=document.getElementById("loginform");
	theform.username.style.border="solid 0px #ffffff";
	theform.username.style.background="#ffffff";
	theform.password.style.border="solid 0px #ffffff";
	theform.password.style.background="#ffffff";*/
	div = document.getElementById("registrati-box");
	div.cont=0;
	div.limit=134;
	div.inc=8;
	div.h=0;
	div.freq=30;
	div.style.marginTop="10px";
	div.style.marginBottom="10px";
	div2 = document.getElementById("loggato-box");
	div2.cont=0;
	div2.limit=120;
	div2.inc=8;
	div2.freq=30;
	div2.h=0;
	//slidebox(div2,div,0);
	$('#loggato-box').slideUp();
	$('#registrati-box').slideDown();
	$('#p1').fadeOut();
	$('#private').fadeOut();
	
	chkloginfrm();
	str=window.location.href;
	
	if (str.search('utente')!=-1)
	{setTimeout("location.href='/default.asp'",1000);}
	else
	{setTimeout("window.location.href=location.href",1000);}
}

function setDatiLogin(ID_UTE,USR_UTE,NOME_UTE){
	//document.getElementById("loggato-user").innerHTML=USR_UTE;
	document.getElementById("loggato-name").innerHTML+=NOME_UTE;
	//document.getElementById("loggato-visite").innerHTML=NLOG_UTE;
	document.getElementById("loggato-modificaprofilo").href="/modifica_profilo.asp?id="+ID_UTE;	
}

function chklogin(){
	if (!(getCookie("STUDIOPAA_ID_UTE")==""))
	{
		setDatiLogin(getCookie("STUDIOPAA_ID_UTE"),getCookie("STUDIOPAA_USR_UTE"),getCookie("STUDIOPAA_NOME_UTE"));
		document.getElementById("registrati-box").style.display="none";
		document.getElementById("loggato-box").style.height="130px";
		
	}
}

function controllaLogin()
{
//	setCookie('STUDIOPAA_LOGIN','123',1);

	if (document.loginform.username.value.length==0)
		{
			alertDiv("ATTENZIONE:\nAlcuni campi obbligatori non sono stati compilati!");
			focusError(document.loginform);
		    return false;
    	}
		
	if (document.loginform.password.value.length==0)
		{
			alertDiv("ATTENZIONE:\nAlcuni campi obbligatori non sono stati compilati!");
			focusError(document.loginform);
		    return false;
    	}
		
	loginUser(document.loginform);
}

function chkloginfrm(){
	if(document.getElementById("frm_dati_ute"))
	{
		if (!(getCookie("STUDIOPAA_ID_UTE")==""))
		{
			document.getElementById("frm_dati_ute").style.display="none";
			document.getElementById("idute").value=getCookie("STUDIOPAA_ID_UTE");
			//document.getElementById("frm_reg").style.height="1150px";
			document.getElementById("txt_dati_ute").innerHTML="Caro "+getCookie("STUDIOPAA_NOME_UTE")+" tuoi dati personali verrano automaticamente tracciati."
		}
		else
		{
			document.getElementById("frm_dati_ute").style.display="block";
			//document.getElementById("frm_reg").style.height="1580px";
			document.getElementById("idute").value="";
			document.getElementById("txt_dati_ute").innerHTML="Se sei un utente registrato, effettua la login."
		}
	}
}
