var giorno=(new Date()).getDate();

function textClock(divId, mostraSecondi)
{
	var divObj = document.getElementById(divId);
	if (divObj)
	{
		var mesi = new Array("gennaio","febbraio","marzo","aprile","maggio","giugno","luglio","agosto","settembre","ottobre","novembre","dicembre");
		var data=new Date();

		var GG=data.getDate();
		var MM=mesi[data.getMonth()];
		var AAAA=data.getYear()+1900;
		var H=data.getHours();
		var M=data.getMinutes();
		var S=data.getSeconds();

		if (GG<10) { GG = '0'+GG; }
		if (H<10) { H = '0'+H; }
		if (M<10) { M = '0'+M; }
		if (S<10) { S = '0'+S; }

		if (mostraSecondi)
		{
			divObj.innerHTML = GG+' '+MM+' '+AAAA+' &nbsp; '+H+':'+M+':'+S;
			window.setTimeout("textClock('"+divId+"', true)", 100);
		}
		else
		{
			divObj.innerHTML = GG+' '+MM+' '+AAAA+' &nbsp; '+H+':'+M;
			window.setTimeout("textClock('"+divId+"', false)", 6000);
		}
	}
	return false;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var marked_row = new Array;

function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
	var theCells = null;
	if (((thePointerColor == '' || thePointerColor == null) && (theMarkColor == '' || theMarkColor == null))
		|| typeof(theRow.style) == 'undefined') {
		return false;
	}
	if (typeof(document.getElementsByTagName) != 'undefined') {
		theCells = theRow.getElementsByTagName('td');
	} else if (typeof(theRow.cells) != 'undefined') {
		theCells = theRow.cells;
	} else {
		return false;
	}
	var rowCellsCnt  = theCells.length;
	var domDetect    = null;
	var currentColor = null;
	var newColor     = null;
	if (typeof(window.opera) == 'undefined'
		&& typeof(theCells[0].getAttribute) != 'undefined') {
		currentColor = theCells[0].getAttribute('bgcolor');
		domDetect    = true;
	} else {
		currentColor = theCells[0].style.backgroundColor;
		domDetect    = false;
	}
	if (currentColor == '' || currentColor == null || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
		if (theAction == 'over' && thePointerColor != '') {
			newColor              = thePointerColor;
		} else if (theAction == 'click' && theMarkColor != '') {
			newColor              = theMarkColor;
			marked_row[theRowNum] = true;
		}
	} else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
			 && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
		if (theAction == 'out') {
			newColor              = theDefaultColor;
		} else if (theAction == 'click' && theMarkColor != '') {
			newColor              = theMarkColor;
			marked_row[theRowNum] = true;
		}
	} else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
		if (theAction == 'click') {
			newColor              = (thePointerColor != '')
								  ? thePointerColor
								  : theDefaultColor;
			marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
								  ? true
								  : null;
		}
	}
	if (newColor) {
		var c = null;
		if (domDetect) {
			for (c = 0; c < rowCellsCnt; c++) {
				theCells[c].setAttribute('bgcolor', newColor, 0);
			}
		} else {
			for (c = 0; c < rowCellsCnt; c++) {
				theCells[c].style.backgroundColor = newColor;
			}
		}
	}
	return true;
}

function CambiaCSS(IdName, Class1, Class2){
	TheBox=document.getElementById(IdName);
	if (TheBox.className == Class1) {
		TheBox.className=Class2;
	} else {
		TheBox.className=Class1;
	}
}

function ImpostaCSS(IdName, Class){
	TheBox=document.getElementById(IdName);
	if (!TheBox){
		alert('Attenzione!\n\nSi è verificato un errore imprevisto.\nContattare se possibile l\'amministratore del sisetma\nsegnalando il seguente codice di errore:\n\n"util.js - ImpostaCSS - '+IdName+'"');
	} else {
		TheBox.className=Class;
	}
}

function ImpostaCSS_parent(IdName, Class){
	TheBox=window.parent.document.getElementById(IdName);
	if (!TheBox){
		alert('Attenzione!\n\nSi è verificato un errore imprevisto.\nContattare se possibile l\'amministratore del sisetma\nsegnalando il seguente codice di errore:\n\n"util.js - ImpostaCSS - '+IdName+'"');
	} else {
		TheBox.className=Class;
	}
}


function ImpostaCSS_noCheck(IdName, Class){
	TheBox=document.getElementById(IdName);
	if (TheBox){
		TheBox.className=Class;
	}
}

function ViewHide(IdName){
	TheBox = document.getElementById(IdName);
	if (TheBox.style.display == 'none') {
		TheBox.style.display = 'block';
	} else {
		TheBox.style.display = 'none';
	}
}

function DataCheck(FORM_Name, Label_Fields, Label){
	errors='';
	if (!FORM_Name.elements[Label_Fields]) {
		alert(	'Attenzione!\n'+
				'\n'+
				'Impossibile eseguire il test sulla data.'+
				'\n');
		return false;
	}

	Data_Day = FORM_Name.elements[Label_Fields+'_Day'];
	Data_Month = FORM_Name.elements[Label_Fields+'_Month'];
	Data_Year = FORM_Name.elements[Label_Fields+'_Year'];

	Var_Day			= parseInt(Data_Day.options[Data_Day.selectedIndex].value);
	Var_Month		= parseInt(Data_Month.options[Data_Month.selectedIndex].value);
	Var_Year		= parseInt(Data_Year.options[Data_Year.selectedIndex].value);

	Var_Day			= Data_Day.options[Data_Day.selectedIndex].value;
	Var_MonthStr	= Data_Month.options[Data_Month.selectedIndex].text;
	Var_Month		= Data_Month.options[Data_Month.selectedIndex].value;
	if ((Var_Month==4 | Var_Month==6 | Var_Month==9 | Var_Month==11) & Var_Day==31) {
		errors += '- ['+ Label +'] Attenzione ' + Var_MonthStr + ' non ha 31 giorni.\n';
		FORM_Name.elements[Label_Fields+'_Day'].selectedIndex=Data_Day.selectedIndex-1;
	}
	//controllo febbraio se deve avere 28 o 29
	Var_Year=Data_Year.options[Data_Year.selectedIndex].value;
	if ((Var_Year % 4)==0) {
		if ((Var_Month==2) && ((Var_Day==30) || (Var_Day==31))) {
			errors += '- ['+ Label +'] Attenzione ' + Var_MonthStr + ' non ha ' + Var_Day + ' giorni.\n';
			FORM_Name.elements[Label_Fields+'_Day'].selectedIndex=27;
		}
	} else {
		if ((Var_Month==2) && ((Var_Day==30) || (Var_Day==31) || (Var_Day==29))) {
			errors += '- ['+ Label +'] Attenzione ' + Var_MonthStr + ' non ha ' + Var_Day + ' giorni.\n';
			FORM_Name.elements[Label_Fields+'_Day'].selectedIndex=27;
		}
	}
	FORM_Name.elements[Label_Fields].value = Data_Month.value+'/'+Data_Day.value+'/'+Data_Year.value;

	return errors
}


function Colora(Name){

	var obj = document.getElementsByName(Name+'_count');
	var form = obj[0].form;

	for (i = 0; i < form.elements[Name+'_count'].value; i++){
		var obj = document.getElementById(Name+'_'+i);
		if (form.elements[Name]){
			var obj2 = form.elements[Name][i];
		} else {
			var obj2 = form.elements[Name+'__'+i];
		}
		if (obj){
			if (obj2.checked){
				obj.className='TestoSel';
			} else {
				obj.className='TestoNOSel';
			}
		}
	}
}

function Upper(testo){
	if (testo==null){ return ''; }
	testo = (Trim(testo));
	return testo.toUpperCase();
}

function Lowercase(testo){
	if (testo==null){ return ''; }
	testo = (Trim(testo));
	return testo.toLowerCase();
}

function Trim(s){
	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r')){
		s = s.substring(1,s.length);
	}
	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r')){
		s = s.substring(0,s.length-1);
	}
	return s;
}

function ForceNumber(testo, default_value, FormatLenght){
	var old = testo.value;
	testo.value = (parseFloat(testo.value));
	var result = (testo.value == old);
	if (!result && (testo.value=='NaN')){
		testo.value = default_value;
	} 
	else if (FormatLenght > 0)
	{
		for (i=testo.value.length; i<FormatLenght; i++)
		{
			testo.value = '0'+testo.value;
		}
	}
	return result;
}

function ForceEuro(testo, default_value){
	var old = testo.value;
	testo.value = (parseInt(testo.value));
	var result = (testo.value == old);
	if (!result && (testo.value=='NaN')){
		testo.value = default_value;
	}
	testo.value = testo.value+'.00';
	return result;
}


function OpenWin(Object, URL, Width, Height){
	var Opzioni='width='+Width+',height='+Height+',resizable,scrollbars=1,status=1, left='+(800-Width)/1.5+', top='+(600-Height)/1.5;
	NewWin = window.open(URL, Object, Opzioni);
	return NewWin;
}


function button_exec(tipo){
	if (tipo=='annulla')
	{
		elem = document.getElementById('btn_TornaIndietro');
		if (elem){
			document.location = elem.href;
		} else {
			alert("Pulsante 'Torna Indietro' mancante.");
		}
	}
	else
	{
		if (document.forms['MainFORM'].redirect_type){
			document.forms['MainFORM'].redirect_type.value = tipo;
			document.forms['MainFORM'].submit();
		} else {
			alert("ATTENZIONE! Tipo 'redirect_type' mancante!");
		}
	}
	return false;
}


function EmailCheck(emailStr, err_msg)
{
	var emailPat=/^(.+)@(.+)$/
	var specialChars1="\\(\\)<>@,;:\\\\\\\"\\[\\]"
	var specialChars2="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars1="\[^\\s" + specialChars1 + "\]"
	var validChars2="\[^\\s" + specialChars2 + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom1=validChars1 + '+'
	var atom2=validChars2 + '+'
	var word="(" + atom1 + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "$")
	var domainPat=new RegExp("^" + atom2 + "(\\." + atom2 +")*$")

	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		return(err_msg+" (controllare '@' e '.').\n");
	}

	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
		return(err_msg+" (username prima di '@' non valido).\n");
	}

	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return(err_msg+" (Indirizzo IP di destinazione non valido).\n");
			}
		}
		return("");
	}

	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		return(err_msg+" (dominio dopo di '@' non valido).\n");
	}

	var atomPat=new RegExp(atom2,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>5) {
		return(err_msg+" (l'indirizzo inserito deve terminare con un dominio di 2 o 5 lettere).\n");
	}

	if (len<2) {
		return(err_msg+" (hostname prima del dominio non inserito).\n");
	}

	return("");
}

function getMousePosition(e)
{
	if(!e) e = window.event;
	var vDoc = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement : document.body;
	var mouseX=(e.pageX) ? e.pageX : e.clientX + vDoc.scrollLeft;
	var mouseY=(e.pageY) ? e.pageY : e.clientY + vDoc.scrollTop;
	alert(mouseX+' - '+ mouseY);
	return Array(mouseX, mouseY);
}

function getScreen_WidthHeight()
{
	if (self.innerWidth)
	{
		scrWidth = self.innerWidth;
		scrHeight = self.innerHeight;
	}
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
		scrWidth = document.documentElement.clientWidth;
		scrHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		scrWidth = document.body.clientWidth;
		scrHeight = document.body.clientHeight;
	}
	return Array(scrWidth, scrHeight);
}

function display_01_mouse(idName, cssOff, cssOn, event)
{
	var TopLeft		= getPosition_topLeft(idName+'_main');
	var theBoxMain	= document.getElementById(idName+'_main');
	var theBox		= document.getElementById(idName+'_div');
	var theB		= document.getElementById(idName);

	maxTOP = window.clientWidth;
	maxLEFT = document.documentElement.clientHeight;

	Top = TopLeft[0] + 12;
	Left = TopLeft[1] - 50;

	theBox.style.top = Top+'px';
	theBox.style.left = Left+'px';

	CambiaCSS(idName, cssOff, cssOn);

	// alert(idName+' => '+theBox.style.left +' - '+theBox.style.top);
}


function browserWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && document.documentElement.clientWidth ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && document.body.clientWidth ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

function getPosition_topLeft(idName)
{
	oElement = document.getElementById(idName);

	var top = 0;
	var left = 0;
	while( oElement != null)
	{
		top += oElement.offsetTop;
		left += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}
	return Array(top, left);
}


function selectText(objId)
{
	selectedText_remove();
	if (document.selection)
	{
		var range = document.body.createTextRange();
		range.moveToElementText(document.getElementById(objId));
		range.select();
	}
	else if (window.getSelection)
	{
		var range = document.createRange();
		range.selectNode(document.getElementById(objId));
		window.getSelection().addRange(range);
	}
}

function selectedText_remove()
{
	if (document.selection)
		document.selection.empty(); 
	else if (window.getSelection)
		window.getSelection().removeAllRanges();
}

function yesNo_onclick(obj, cssSi, cssNo)
{
	if (obj.value=='Si')
	{
		obj.value = 'No';
		obj.className = cssNo;
	}
	else
	{
		obj.value = 'Si';
		obj.className = cssSi;
	}
}