// Gestione dei sondaggi

// gestione finte popup
// rilevo movemento del mouse
/*var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE)

document.onmousemove = getMouseXY;*/

// coordinate del mouse
var mouseX = 0
var mouseY = 0

// controllo il movimento del layer
var move = false;

var distX = 0;
var distY = 0;

// controllo del risize del layer
var resize = false;

var descrTopWidth = 0;

// coordinate del mouse
/*function getMouseXY(e) 
{
  if (IE) { // grab the x-y pos.s if browser is IE
	mouseX = event.clientX + document.body.scrollLeft
	mouseY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
	mouseX = e.pageX
	mouseY = e.pageY
  }  
  // catch possible negative values in NS4
  if (mouseX < 0){mouseX = 0}
  if (mouseY < 0){mouseY = 0}  
  
  
  // mouse resize layer
  // se il mouse si trova sul bordo del layer
  if(document.getElementById("descr"))
  {
	  // solo se il mouse è nella posizione giusta
	  var left = document.getElementById("descr").style.left.replace("px","")*1 + document.getElementById("descr").style.width.replace("px","")*1;
	  var top = document.getElementById("descr").style.top.replace("px","")*1 + document.getElementById("descr").style.height.replace("px","")*1;
  
  
	  if(  (  (mouseX <= left)  && (mouseX >= (left - 10)) ) && (  (mouseY <= top)  && (mouseY >= (top - 10)) )    )
	  {
		document.getElementById("descr").style.cursor = "nw-resize";
	  }
	  else
	  {
		document.getElementById("descr").style.cursor = "auto";
	  }
  }
   
	  
  // muovo il layer
  if(move == true)
  {
	// vecchie coord
	var X = document.getElementById("descrTop").style.left.replace("px","")*1;
	var Y = document.getElementById("descrTop").style.top.replace("px","")*1;
	
	// se nn si è ancora trovata la distanza tra mouse e layer
	if(distX == 0)
	{
		distX = mouseX - X;
		distY = mouseY - Y;
	}
	
	document.getElementById("descrTop").style.left = mouseX - distX + "px";
	document.getElementById("descrTop").style.top = mouseY - distY + "px";
	document.getElementById("descr").style.left = mouseX - distX + "px";
	document.getElementById("descr").style.top = mouseY + 30 - distY + "px";
	
  }
  
  // resize il layer
  if(resize == true)
  {
	// se nn si è ancora trovata la distanza tra mouse e layer
	if(distX == 0)
	{
		distX = document.getElementById("descr").style.left.replace("px","")*1;
		distY = document.getElementById("descr").style.top.replace("px","")*1;
	}
	
	if((mouseX - distX) > 0)
	{
		document.getElementById("descr").style.width = mouseX - distX + "px";
		document.getElementById("descrTop").style.width = mouseX - distX + "px";
	}
	if((mouseY - distY) > 0)
	{
		document.getElementById("descr").style.height = mouseY - distY + "px";
	}
  
  }
  
  
  
  return true
}
*/

	// creazione del layer
	function createLay(left,top,width,height)
	{
		var ogLay = document.createElement("DIV");

		// finta finestra toolbar	
		ogLay.id = "descrTop";
		ogLay.name = "descrTop";	
		if(width != "")
		{
			ogLay.style.width = width*1 + 5 + "px";
		}
		else
		{	
			ogLay.style.width = "380px";
		}
		if(height != "")
		{
			ogLay.style.height = "20px";
		}
		else
		{	
			ogLay.style.height = "20px";
		}
		
		
		ogLay.style.position = "absolute";
		ogLay.style.backgroundColor = "#091F65";
		ogLay.style.Color = "#AA00BB";
		ogLay.style.textAlign = "right";
		if(left != "")
		{
			ogLay.style.left = left*1 + "px";
		}
		else
		{
			ogLay.style.left = mouseX + "px";
		}
		if(top != "")
		{
			ogLay.style.top = top*1 + "px";
		}
		else
		{
			ogLay.style.top = mouseY + "px";			
		}
		
		ogLay.style.borderTop = "solid";
		ogLay.style.borderLeft = "solid";
		ogLay.style.borderRight = "solid";
		ogLay.style.zIndex = "3";
		ogLay.style.cursor = "move";
		ogLay.onmouseup = function()
		{
			stopDrag();
		}
		ogLay.onmousedown = function()
		{
			startDrag();
		}


		// per rimpicciolire
		var input = document.createElement("INPUT");
		input.setAttribute("type","BUTTON");
		input.setAttribute("id","CLOSE");
		input.setAttribute("name","CLOSE");
		input.setAttribute("value","_");
		input.style.height="17px";
		input.style.fontSize="9px";
		input.style.cursor = "pointer";
		input.onclick = function()
		{
			chiudiLay();
			stopDrag();
		}	
		//ogLay.appendChild(input);
		
		
		// chiusura layer
		var input = document.createElement("INPUT");
		input.setAttribute("type","BUTTON");
		input.setAttribute("id","X");
		input.setAttribute("name","X");
		input.setAttribute("value","X");	
		input.style.cursor = "pointer";
		input.style.font="10px verdana";
		input.style.padding="3px";
		input.style.margin="0px";
		input.style.border="none";
		input.style.height="17px";
		input.onclick = function()
		{
			delLay();
		}
		
		ogLay.appendChild(input);
		document.getElementById("body").appendChild(ogLay);
		
		
		// linea decorativa
		var ogLay = document.createElement("DIV");

		// finta finestra toolbar	
		ogLay.id = "linea";
		ogLay.name = "linea";	
		if(width != "")
		{
			ogLay.style.width = width*1 + 5 + "px";
		}
		else
		{	
			ogLay.style.width = "380px";
		}
		if(height != "")
		{
			ogLay.style.height = "2px";
		}
		else
		{	
			ogLay.style.height = "2px";
		}
		
		
		ogLay.style.position = "absolute";
		ogLay.style.backgroundColor = "#FFFF01";
		ogLay.style.Color = "#AA00BB";
		ogLay.style.textAlign = "right";
		if(left != "")
		{
			ogLay.style.left = left*1 + "px";
		}
		else
		{
			ogLay.style.left = mouseX + "px";
		}
		if(top != "")
		{
			ogLay.style.top = (top*1 +23) + "px";
		}
		else
		{
			ogLay.style.top = mouseY + "px";			
		}
		
		ogLay.style.borderTop = "none";
		ogLay.style.borderLeft = "solid";
		ogLay.style.borderRight = "solid";
		ogLay.style.zIndex = "3";
		document.getElementById("body").appendChild(ogLay);
		
		
		// layer principale
		var ogLay = document.createElement("DIV");
		ogLay.id = "descr";
		ogLay.name = "descr";
		ogLay.style.fontSize = "11px";
		ogLay.style.overflow = "auto";
		ogLay.style.paddingLeft = "5px";
		ogLay.style.borderBottom = "solid";
		ogLay.style.borderLeft = "solid";
		ogLay.style.borderRight = "solid";

		if(width != "")
		{
			ogLay.style.width = width + "px";
		}
		else
		{	
			ogLay.style.width = "380px";
		}
		if(height != "")
		{
			ogLay.style.height = height + "px";
		}
		else
		{	
			ogLay.style.height = "227px";
		}
		ogLay.style.position = "absolute";
		
		// uso le coordinate del mouse
		if(left != "")
		{
			ogLay.style.left = left*1 + "px";
		}
		else
		{
			ogLay.style.left = mouseX + "px";
		}
		if(top != "")
		{
			ogLay.style.top = top*1 + 25 + "px";
		}
		else
		{
			ogLay.style.top = mouseY + 25 + "px";			
		}
		
		
		
		
		//ogLay.style.backgroundColor = "#FFFF01";
		ogLay.style.zIndex = "3";
		ogLay.style.display = "block";	
		ogLay.onmouseup = function()
		{
			stopResizeLay();
		}
		ogLay.onmousedown = function()
		{
			startResizeLay();
		}
		
		
		// layerino nell'angolo in basso a sinistra per resize
		var ogLayRes = document.createElement("DIV");
		ogLayRes.id = "Resize";
		ogLayRes.name = "Resize";
		ogLayRes.style.width = "10px";
		ogLayRes.style.height = "5px";
		ogLayRes.style.backgroundColor = "#000000";
		ogLayRes.style.position = "absolute";
		ogLayRes.style.left = "392px";
		ogLayRes.style.top = "378px";
		//ogLayRes.style.cursor = "nw-resize";
		
		//per ora no!!
		//ogLay.appendChild(ogLayRes);
		
		
		document.getElementById("body").appendChild(ogLay);
		
	
	}

	// elimina layer
	function delLay()
	{	
		if(document.getElementById("descr"))
		{
			document.getElementById("body").removeChild(document.getElementById("descr"));
			document.getElementById("body").removeChild(document.getElementById("descrTop"));
			document.getElementById("body").removeChild(document.getElementById("linea"));
		}
	}	


	// memorizzo coordinate per il ri-ingrandimento
	var layX = 0;
	var layY = 0;

	// rimpicciolisce layer
	function chiudiLay()
	{	
		if(document.getElementById("descr"))
		{
			// mem coord lay
			layX = document.getElementById("descrTop").style.left;
			layY = document.getElementById("descrTop").style.top;
			// memorizzo width
			descrTopWidth = document.getElementById("descrTop").style.width; 
		
			// rendo inv il lay principale
			document.getElementById("descr").style.display = "none";
			
			// rimpicciolisco l'altro
			document.getElementById("descrTop").style.left = "2px";
			document.getElementById("descrTop").style.top = "2px";
			document.getElementById("descrTop").style.width = "90px";
			
			// cambio value al tasto
			document.getElementById("CLOSE").value = "-";
			document.getElementById("CLOSE").onclick = function()
			{
				apriLay();
				stopDrag();
			}
			
			
		}
	}	


	// ingrandisce il layer layer
	function apriLay()
	{	
		if(document.getElementById("descr"))
		{
			// mem coord lay

			document.getElementById("descrTop").style.left = layX;
			document.getElementById("descrTop").style.top = layY;
			// memorizzo width
			document.getElementById("descrTop").style.width = descrTopWidth; 
		
			// rendo vis il lay principale
			// e risistemo le coordinate
			document.getElementById("descr").style.left = layX;
			document.getElementById("descr").style.top = (layY.replace("px","")*1 + 30) + "px";
			document.getElementById("descr").style.display = "block";
			
			// cambio value al tasto
			document.getElementById("CLOSE").value = "-";
			document.getElementById("CLOSE").onclick = function()
			{
				chiudiLay();
				stopDrag();
			}
			
			
		}
	}	
	
	// parte il movimento
	function startDrag()
	{
		move = true;
	}
	
	
	// fermo il movimento
	function stopDrag()
	{
		move = false;
		// resetto coordinate
		distX = 0;
		distY = 0;
		
	}
	
	// inizio a resizare il alyer
	function startResizeLay()
	{
		// solo se il mouse è nella posizione giusta
		var left = document.getElementById("descr").style.left.replace("px","")*1 + document.getElementById("descr").style.width.replace("px","")*1;
		var top = document.getElementById("descr").style.top.replace("px","")*1 + document.getElementById("descr").style.height.replace("px","")*1;
		
		
		if(  (  (mouseX <= left)  && (mouseX >= (left - 10)) ) && (  (mouseY <= top)  && (mouseY >= (top - 10)) )    )
		{		
			resize = true;
		}
	}

	// fermo il resize del layer
	function stopResizeLay()
	{
		resize = false;
		// resetto coordinate
		distX = 0;
		distY = 0;		
	}



// ajax
var xmlHttpSond;
function createXMLHttpRequestSond()
{
	if(window.ActiveXObject)
	{
		xmlHttpSond = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		xmlHttpSond = new XMLHttpRequest();
	}
}

function RichiestaServerSond(bLay,type,param1,param2,param3,param4)
{
	// solo se nn e' gia' stato creato
	if( (!document.getElementById("descrSond")) && (bLay == "true"))
	{
		delLay();
		var left = (document.body.clientWidth*1 - 380) / 2;
		createLay(left,"200","380","227");	
	}
	else
	{
		delLay();
		var left = (document.body.clientWidth*1 - 380) / 2;
		createLay(left,"200","380","227");			
	}
	
	createXMLHttpRequestSond();
			
	var url = "Private/SondaggioAjax.php?timestamp=" + new Date().getTime() + "&ric=" + type + "&param1=" + param1 + "&param2=" + param2 + "&param3=" + param3 + "&param4=" + param4;
	xmlHttpSond.open("GET",url,true);
	xmlHttpSond.onreadystatechange = function()
								 {
									StateChangeSond(type,param1,param2,param3,param4);
								 }
	xmlHttpSond.send(null);
	
}

function StateChangeSond(type,param1,param2,param3,param4)
{
	if(xmlHttpSond.readyState == 4)
	{
		if(xmlHttpSond.status == 200)
		{
			switch (type)
			{
				case "vis":case "vispast":
					var response = xmlHttpSond.responseText;
					document.getElementById("descr").innerHTML = response;
					break;
					
				case "invia":
					var response = xmlHttpSond.responseText.split("X__X");
					document.getElementById("descr").innerHTML = response[0];
					if(response[1] == "SI")
					{
						document.getElementById("descr").style.height = "260px";
					}
					break;				
			}
		}
	}
}

// invia sondaggio
function InviaSondaggio(lang)
{
	var param1 = "";
	for(i=0;i<document.getElementsByTagName("INPUT").length;i++)
	{
		if(document.getElementsByTagName("INPUT")[i].type == "radio")
		{
			if(document.getElementsByTagName("INPUT")[i].checked == true)
			{
				param1 = document.getElementsByTagName("INPUT")[i].value;
			}
		}
	}
	if(param1 != "")
	{
		RichiestaServerSond("true","invia",param1,lang,"","");
	}
}

// vis sondaggio
function VisSondaggio(lang)
{
	var param1 = "";
	for(i=0;i<document.getElementsByTagName("INPUT").length;i++)
	{
		if(document.getElementsByTagName("INPUT")[i].type == "radio")
		{
			param1 = document.getElementsByTagName("INPUT")[i].value;
			break;
		}
	}
	
	RichiestaServerSond("true","vis",param1,lang,"","");
}

var numero = 1;
function VisSondaggioPast(num,lang,Piu)
{
	if(num == "1")
	{
		RichiestaServerSond("true","vispast",num,lang,"","");
	}
	else
	{
		if(Piu == "true")
		{
			numero = numero*1 +1;
		}
		else
		{
			numero = numero*1 -1;
		}
		RichiestaServerSond("false","vispast",numero,lang,"","");
	}
}

