
//===========
// PATCH LINK
//===========
window.onload = function OnLoad() { PatchLink(); Init(); }
function Init(){}
function PatchLink()
{
	for (var i=0; i<document.links.length; i++)
	{
		// Attributo target specificato in className per compatibilità XHTML
        if (document.links[i].className.search("target_blank") >= 0)
			document.links[i].target="_blank";
		
		// Link verso documenti e/o pubblicazioni editati da backoffice e link speciali (immagine, fotogallery, stampa)
        if (document.links[i].className.search("target_open") >= 0)
        	document.links[i].href = 'javascript:OpenLink("'+ document.links[i].href +'")';
	}	
}


//===========
// OPEN POPUP
//===========
function OpenLink(arg)
{
	var name = "_blank";
	try	{ queryString = arg.split("?")[1] }	catch(e) { queryString = "_blank" }
		
	// Default features
	var features = "width=700, height=500, location=no, menubar=no, resizable=yes, scrollbars=yes, status=yes, titlebar=yes";

	// Specific features
	if (arg.search("OpenGallery.asp") >= 0)
	{
		features = "width=700, height=500, location=no, menubar=no, resizable=yes, scrollbars=yes, status=yes, titlebar=yes";
	}
	else if (arg.search("OpenImage.asp") >= 0)
	{
		features = "width=700, height=500, location=no, menubar=no, resizable=yes, scrollbars=yes, status=yes, titlebar=yes";
	}
	else if (arg.search("OpenObject.asp") >= 0)
	{
		// link a oggetti aperti nella stessa pagina 
		document.location = arg;
		return;
	}
	else if (arg.search("OpenDoc.asp") >= 0)
	{
		features = "width=800, height=600, location=no, menubar=no, resizable=yes, scrollbars=yes, status=yes, titlebar=yes";		
	}
	else
	{
		// Redirezione con arg inalterato, tipicamente utilizzata per la versione stampabile
		features = "width=800, height=600, location=no, menubar=no, resizable=yes, scrollbars=yes, status=yes, titlebar=yes";		
	}

	// Le aperture in popup sono segnalate con parametro sulla query
	window.open(arg +"&popup=1", name, features).focus();
}


//==================
// CALENDAR FUNCTION
//==================

function DoCalendarRefresh(idCalendar, urlPattern)
{
	var month = document.getElementById(idCalendar +"_SELECT_Month").value;
	var year = document.getElementById(idCalendar +"_SELECT_Year").value;
	var url = urlPattern.replace("_event_", 2);
	var url = url.replace("_month_", month);
	var url = url.replace("_year_", year);
	window.document.location = url;	
}


//=======
// LOGIN
//=======
function DoLogin(formName)
{
	// LoginForm
	var form = document.getElementById(formName);

	// Check autologin
	if (form.Subject.value == form.InputUtente.value && form.InputPassword.value == ")auto(")
	{
		document.location = form.UrlSuccess.value;
		return;
	}

	// Utente
	var subject = Trim(form.InputUtente.value).toLowerCase();
	if ( subject == "" )
	{
		window.alert("Utente obbligatorio");
		FormLogin.InputUtente.focus();
		return false;
	}
	form.Subject.value = subject; 

	// Password
	var password = Trim(form.InputPassword.value).toLowerCase();
	
	// IpAddress
	var ip = Trim(form.IpAddress.value);
	
	// Ticket
	var ticket = Trim(form.Ticket.value);
	form.Ticket.value = ticket;
	
	// Digest
	var key = subject + password;
	key = SHA1EsaDigest(key, key.length);
	fullMsg = subject + ip + key + ticket;
	var digest = SHA1EsaDigest(fullMsg, fullMsg.length);
	form.Digest.value = digest;
	
	form.submit();
}
