/**********************************************************************/
/****************************AJAX**************************************/
/**********************************************************************/

function makeRequest(url, handler)
{
	http_request = false;
	
	if (window.XMLHttpRequest)
	{
		// IE7, Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType)
		{
			http_request.overrideMimeType('text/html');
		}
	}
	else if (window.ActiveXObject)
	{ // IE
		try
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{}
		}
	}
	
	if (!http_request)
	{
		alert('Your browser is obsolete! Please update it!'); //impossibile creare l'istanza XMLHTTP
		return false;
	}
	
	http_request.onreadystatechange = handler;
	http_request.open('GET', url, true);
	http_request.setRequestHeader("connection", "close");
	http_request.send(null);

}
/************************************************************************************************/
/************************************************************************************************/
/************************************************************************************************/


function onRefreshEvento()
{
var my_response_array;

	if (http_request.readyState == 4)
	{
		//alert(http_request.status+'  '+http_request.responseText);
		
		if (http_request.status == 200)
		{
			my_response_array = http_request.responseText.split(";");
			if( my_response_array.length == 3)
			{
				if( document.getElementById("xvotes-" + my_response_array[0]) )
					document.getElementById("xvotes-" + my_response_array[0]).innerHTML = my_response_array[1];
				
				if( document.getElementById("xvote-" + my_response_array[0]) )
				{
					if(  my_response_array[2] == 'v' )
					{
						document.getElementById("xvote-" + my_response_array[0]).innerHTML = "<a href=\"#\" onClick=\"nonVadoEvento('" + my_response_array[0] + "'); return false; \">Non Vado</a>";
						fineContabilizzaVoto( true );
					}
					else
					{
						document.getElementById("xvote-" + my_response_array[0]).innerHTML = "<a href=\"#\" onClick=\"vadoEvento('" + my_response_array[0] + "'); return false; \">Ci Vado!</a>";
						fineContabilizzaVoto( false );
					}
				}
			}
		}
		else
		{
			alert('Internal server error\n\n Request Status: ' + http_request.status );
		}
	}
}

function fineContabilizzaVoto( cosafa )
{
	var div_help = document.getElementById("div_partecipa_u");
	if( div_help )
	{
		if( cosafa )
			div_help.innerHTML = "Sei nella lista dei partecipanti!";
		else
			div_help.innerHTML = "Non sei nella lista dei partecipanti!<br>Clicka sul pulsante Ci Vado! se pensi che parteciperai all'evento.";
	}
}

function contabilizzaVoto( cosafa )
{
	var div_help = document.getElementById("div_partecipa_u");
	if( div_help )
	{
		if( cosafa )
			div_help.innerHTML = "<img src='/images/loading.gif'> Stiamo contabilizzando la tua partecipazione...";
		else
			div_help.innerHTML = "<img src='/images/loading.gif'> Stiamo cancellando la tua partecipazione...";
	}
}

function vadoEvento(news_id)
{
	contabilizzaVoto( true );
	makeRequest("ajax/ajax.php?act=vota&id="+news_id, onRefreshEvento);
	return false;
}

function nonVadoEvento(news_id)
{
		contabilizzaVoto( false );
	makeRequest("ajax/ajax.php?act=nonvado&id="+news_id, onRefreshEvento);
	return false;
}

function loadHideSubmitImGal()
{
	var div_subImGal = document.getElementById("invia_image_gallery");
	if( div_subImGal )
	{
		if( div_subImGal.style.display == "none" )
		{
			div_subImGal.style.display="";
			div_subImGal.innerHTML="<img src='/images/loading.gif'> Caricamento in corso...";
			makeRequest("ajax/ajax.php?act=galleria_invia", onGalleriaInvia);
		}
		else
		{
			div_subImGal.style.display="none";
		}
	}
	return false;
}

function onGalleriaInvia()
{
	if (http_request.readyState == 4)
	{
		//alert(http_request.status+'  '+http_request.responseText);
		
		if (http_request.status == 200)
		{
				var div_subImGal = document.getElementById("invia_image_gallery");
				if( div_subImGal )
				{
					div_subImGal.innerHTML = http_request.responseText;
				}

		}
		else
		{
			alert('Internal server error\n\n Request Status: ' + http_request.status );
		}
	}
}

function addBookmark(title,url)
{
if (window.sidebar)
{ 
window.sidebar.addPanel(title, url,""); 
}
else if( document.all )
{
window.external.AddFavorite( url, title);
}
else if( window.opera && window.print )
{
return true;
}
}

