//*********Auto scroll Menu
var offsettop=0

function makeStatic() {
	if (document.all) {
		document.getElementById("object1").style.pixelTop=document.body.scrollTop;
	}
	else{
		eval(document.getElementById("object1").style.top = eval(window.pageYOffset + offsettop));
	}

setTimeout("makeStatic()",0);
}
//*********FINE Auto scroll Menu

//******** X creare link POPUP
function popUp(url,target,height,width,scrollbars){
	if (width==='' || width===null){
		width=500;
	}
	if ((height==="") || (height===null)){
		height=350;
	}
	window.open(url, target, 'scrollbars='+scrollbars+',status=yes,resizable=no,toolbar=no,height=' + height + ',width=' + width + ',left='+((screen.width/2)-(height/2))+',top='+((screen.height/2)-(height/2))+"");
}


//******** X converire i caratteri in maiuscole(M) o minuscole(m)
function Maiusc_Minusc(obj,valore){
	if (valore=='m'){
		var StrVal = obj.value
		return(obj.value = StrVal.toLowerCase())
	} else if (valore=='M'){		
		var StrVal = obj.value
		return(obj.value = StrVal.toUpperCase())
	}
}

//******** X far inserire solo caratteri numerici
function formatInt(obj){
	if (!(window.event.keyCode >= 48 
			&& window.event.keyCode<= 57))
		window.event.returnValue=false;
}

//******** X formattare un valore numerico con i decimali o non
function formatNumeric(obj,numDecimal){
	if (window.event.keyCode==44){ // h una virgola
	//se il campo contiene gi` una virgola non pur inserirne altre
	// e la virgola inizialmente non ci pur stare
		if (obj.value.length==0 || obj.value.indexOf(",",0)!=-1)
			window.event.returnValue=false;
		return
	}
	if (!(window.event.keyCode >= 48 && window.event.keyCode<= 57))
			window.event.returnValue=false;
	if (typeof(numDecimal)!= "udefined" && obj.value.indexOf(",",0) != -1)
		if ((obj.value.length-obj.value.indexOf(",",0)) > numDecimal)
			window.event.returnValue=false;			
}

//******** blocca l'inserimento nel campo a 10 caratteri e con la formattazione gg/mm/aaa - mm/aaaa - aaaa
function formatDate(obj,tipodata){
	if ((tipodata=='gg/mm/aaaa') || (tipodata==undefined)){
		//non fa inserire + di 10 char
		if (obj.value.length==10){
			window.event.returnValue=false;			
			return
		}
		// setta il 3 e il 5 char a /
		if (obj.value.length==2 || obj.value.length==5){
				window.event.keyCode=47;
				return	
		}
		// accetta solo interi		
		formatInt(obj);
	} else if (tipodata=='mm/aaaa'){
		//non fa inserire + di 7 char
		if (obj.value.length==7){
			window.event.returnValue=false;			
			return
		}
		// setta il 3 a /
		if (obj.value.length==2){
				window.event.keyCode=47;
				return	
		}
		// accetta solo interi		
		formatInt(obj);	
	} else if (tipodata=='aaaa'){
		//non fa inserire + di 4 char
		if (obj.value.length==4){
			window.event.returnValue=false;			
			return
		}
		// accetta solo interi		
		formatInt(obj);	
	}
}

//******** controla se il valore del campo è nullo
function isNull(string) {
	//Ritorna un booleano.
	//Ritorna 'true' se la stringa 'string' h nulla o contiene solo spazi bianchi, 
	//ritorna 'false' in caso contrario.
	var string1 = string.toString();
	string1 = string1.replace(/\s/g,"");
	if (string1=="") return true; else return false;
} 
		
//******** verifica se il valore inserito è una data		
function isDate(string,tipodata) {
	//Ritorna un booleano.
	//Ritorna 'true' se la stringa 'string' h una data (gg/mm/aaaa), 
	//ritorna 'false' in caso contrario. 
	string = string.toString();
	if (isNull(string)) return false;
		var formatoData = /(\d){2}(\/){1}(\d){2}(\/){1}(\d){4}/;
		if (tipodata == 'mm/aaaa'){
			string = '01/' + string
		} else if (tipodata == 'aaaa'){
			string = '01/01/' + string
		}
		string = string.replace(/\s/g,"");
	  	var sDate = string;
		string = string.replace(formatoData,"");
		if (string=="") {
			var day = parseInt(sDate.substring(0, 2), 10);
			var month = parseInt(sDate.substring(3, 5), 10)-1;
			var year = parseInt(sDate.substring(6, 10), 10);
	    	var oDate = new Date(year, month, day)
	    	
		    if ((oDate.getDate()==day) && (oDate.getMonth()==month) && ((oDate.getYear()==year) || (oDate.getFullYear()==year))){
				return true;
		    } else {
				return false;
		    }
		}
	else return false;
}

//******** X converire una stringa in data
function stringToDate(sDate)
{
	if (isDate(sDate)==true){
		var d = parseInt(sDate.substring(0, 2), 10);
		var m = parseInt(sDate.substring(3, 5), 10)-1;
		var y = parseInt(sDate.substring(6, 10), 10);
    	return new Date(y, m, d)
	}
	return null
}

//******** X converire una data in stringa
function dateToString(objData)
{
	if (objData==null)	return ""
	var d= "0" + objData.getDate()
	var m= "0" + (objData.getMonth()+1)
	return (d.substr(d.length-2,2) + "/"+  m.substr(m.length-2,2) + "/" + objData.getFullYear())
}	

//******** X verificarese se è una data gg/mm/aaaa o mm/aaaa o aaaa ed è racchiusa in un determinato valore Max e Min
function isDateOrNull(obj,tipodata,MaxMin){
	var datainput = obj.value;
	if (tipodata == 'mm/aaaa'){
		datainput = '01/' + datainput
	} else if (tipodata == 'aaaa'){
		datainput = '01/01/' + datainput
	} else if (tipodata == 'gg'){
		datainput = datainput + '/01/2000'
	}  else if (tipodata == 'mm'){
		datainput = '01/' + datainput + '/2000'
	}


	anni = parseInt(datainput.substr(6,4),10);
	mesi = parseInt(datainput.substr(3,2),10)-1;
	giorni = parseInt(datainput.substr(0,2),10);
	
	var dataMin= new Date(Date.UTC(1890,0,1)); //data minima
	var dataMax= new Date(); //data odierna
	
	if (MaxMin=='Max'){
	dataMax= new Date(Date.UTC(2150,0,1)); //data minima
	dataMin= new Date(); //data odierna
	}
	var dataok= new Date(Date.UTC(anni,mesi,giorni));		
	
	if (!isNull(obj.value) && !isDate(datainput)) {
		alert("Il campo contiene un valore DATA non valido");
		obj.focus();
		
	} else if ((dataok > dataMax) || (dataok < dataMin)){
		alert("Il campo contiene un valore DATA non valido");
		obj.focus();
	}
}

//******** X calcolare quanti giorni, mesi, ANNI sono passati da una certa data
function calcolagiorni(obj){ 
  datainput = obj; 
  anni = parseInt(datainput.substr(6,4),10);
  mesi = parseInt(datainput.substr(3,2),10)-1;
  giorni = parseInt(datainput.substr(0,2),10);
  var data1=new Date(); 
  var data2=new Date(Date.UTC(anni,mesi,giorni)); 
  data1time = data1.getTime(); 
  data2time = data2.getTime(); 
  difftime = Math.abs(data1time-data2time); 
  var giornilontano = parseInt(difftime/1000/60/60/24); 
  var datadiff=new Date(); 
  datadiff.setTime(difftime); 
  giornidiff = datadiff.getDate(); 
  mesidiff = datadiff.getMonth(); 
  annidiff = parseInt(datadiff.getFullYear()-1970); 
  //var risultato = "Il "+datainput+" è lontano "+giornilontano+" giorni dal 2 gennaio 1997. "+annidiff+" anni, "+mesidiff+" mesi e "+giornidiff+" giorni." 
  return (annidiff);
  	}


function leapYear(year) {
	if (year % 4 == 0) // basic rule
		return true // is leap year
	/* else */ // else not needed when statement is "return"
		return false // is not leap year
}

//******** X calcolare di quanti giorni è un mese
function getDays(month, year) {
	// create array to hold number of days in each month
	var ar = new Array(12)
	ar[0] = 31 // January
	ar[1] = (leapYear(year)) ? 29 : 28 // February
	ar[2] = 31 // March
	ar[3] = 30 // April
	ar[4] = 31 // May
	ar[5] = 30 // June
	ar[6] = 31 // July
	ar[7] = 31 // August
	ar[8] = 30 // September
	ar[9] = 31 // October
	ar[10] = 30 // November
	ar[11] = 31 // December

	// return number of days in the specified month (parameter)
	return ar[month]
}

//******** X sapere il nome del mese
function getMonthName(month) {
	// create array to hold name of each month
	var ar = new Array(12)
	ar[0] = "Gennaio "
	ar[1] = "Febbraio "
	ar[2] = "Marzo "
	ar[3] = "Aprile "
	ar[4] = "Maggio "
	ar[5] = "Giugno "
	ar[6] = "Luglio "
	ar[7] = "Agosto "
	ar[8] = "Settembre "
	ar[9] = "Ottobre "
	ar[10] = "Novembre "
	ar[11] = "Dicembre "

	// return name of specified month (parameter)
	return ar[month]
}

function setCal(d) {
	// standard time attributes
	var now 
	if(d==null)	now= new Date()
	else now=d
	var year = now.getFullYear()
	var month = now.getMonth()
	var monthName = getMonthName(month)
	var date = now.getDate()
	now = null

	// create instance of first day of month, and extract the day on which it occurs
	var firstDayInstance = new Date(year, month, 1)
	var firstDay = firstDayInstance.getDay()
	firstDayInstance = null

	// number of days in current month
	var days = getDays(month, year)

	// call function to draw calendar
	drawCal(firstDay + 1, days, date, monthName, year,month)
}

function drawCal(firstDay, lastDate, date, monthName, year,month) {
	//alert(firstDay + ' '+  lastDate+ ' '+ date)
	// constant table settings
	
	// create basic table structure
	var text = "" // initialize accumulative variable to empty string
	text += '<CENTER>'
	text += '<TABLE width="210 px" bgcolor=' + IntestColor + ' BORDER=' + BordRiquadroSize + ' bordercolorlight=' + BordRiquadroColorlight + ' bordercolordark=' + BordRiquadroColorDark + ' CELLSPACING=' + cellspacing + '>' // table settings
	text += '<tr><td>'
	text += 		'<TABLE width="100%" bgcolor=' + IntestColor + ' BORDER=' + border + ' bordercolor=' + IntestColor + ' CELLSPACING=' + cellspacing + '>' // table settings
	text += 			'<TR ALIGN="right">'
	text += 				'<TH width="100%" COLSPAN=1 HEIGHT=' + headerHeight + '>' 
	text += 				'</TH>'
	text += 				'<TH width="0%" COLSPAN=1 style="cursor : hand" HEIGHT=' + headerHeight + ' onclick="Mouse_fuori()">' 
	text += 					'<img style="cursor : hand" src="' + MapRootRelativa + 'pannello/img_pannello/chiudi.gif" >'
	text += 				'</TH>'
	text += 			'</TR>'
	text += 		'</TABLE>'			
	text += '<TABLE bgcolor=' + borderColor + ' width="100%" BORDER=' + border + ' bordercolor=' + borderColor + ' CELLSPACING=' + cellspacing + '>' // table settings
	text += 	'<TH COLSPAN=1 style="cursor : hand" bgcolor=' + HeadborderColor + ' bordercolor=' + HeadborderColor + ' HEIGHT=' + headerHeight + ' onclick="setCal(new Date(' + year + ',' + (month-1) + ',' + date + '))">' 
	text += 			'<img style="cursor : hand" src="' + MapRootRelativa + 'pannello/img_pannello/freccia_sx.gif" >'
	text += 	'</TH>' 
	text += 	'<TH nowrap COLSPAN=5 HEIGHT=' + headerHeight + ' bgcolor=' + HeadborderColor + ' bordercolor=' + HeadborderColor + ' align="center">' // create table header cell
	text += 		'<table cellpadding="0" cellspacing="0" border="0"><tr><td rowspan="2" nowrap><FONT face="Arial" COLOR="' + headerColor + '" SIZE=' + headerSize + '>' // set font for table header
	text += 			monthName + ' ' + year 
	text += 		'</FONT></td>' // close table header's font settings
	text += 			'<td><img style="cursor : hand" src="' + MapRootRelativa + 'pannello/img_pannello/freccia_su.gif"  onclick="setCal(new Date(' + (year+1) + ',' + month + ',' + date + '))"></td></tr><tr><td>'
	text += 			'<img style="cursor : hand" src="' + MapRootRelativa + 'pannello/img_pannello/freccia_giu.gif" onclick="setCal(new Date(' + (year-1) + ',' + month + ',' + date + '))"></td></tr></table>'
	text += 	'</TH>' // close header cell
	text += 	'<TH COLSPAN=1 style="cursor : hand" HEIGHT=' + headerHeight + ' bgcolor=' + HeadborderColor + ' bordercolor=' + HeadborderColor + ' onclick="setCal(new Date(' + year + ',' + (month+1) + ',' + date + '))">' 
	text += 			'<img style="cursor : hand" src="' + MapRootRelativa + 'pannello/img_pannello/freccia_dx.gif" >'
	text += 	'</TH>' 


	// variables to hold constant settings
	var openColA = '<TD bordercolor=' + borderColorDay + ' align="center" WIDTH=' + colWidth + ' HEIGHT=' + dayCellHeight + ' bgcolor='
	var openColB ='><FONT face="Arial" COLOR="' + dayColor + '"><p align="center"><b>'
	var closeCol = '</b></p></FONT></TD>'

	// create first row of table to set column width and specify week day
	text += '<TR ALIGN="center" VALIGN="center">'
	for (var dayNum = 0; dayNum < 7; ++dayNum) {
		text += openColA + weekDayColor[dayNum] + openColB +weekDay[dayNum] + closeCol 
	}
	text += '</TR>'
	
	// declaration and initialization of two variables to help with tables
	var digit = 1
	var curCell = 1
	
	for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
		text += '<TR ALIGN="center" VALIGN="top">'
		for (var col = 1; col <= 7; ++col) {
			if (curCell < firstDay || digit > lastDate) {
				text += '<TD bgcolor='+ weekDayColor[col-1] +'>&nbsp;</TD>';
				curCell++
			} else {
				text += '<TD  HEIGHT=' + cellHeight + ' bgcolor='+ weekDayColor[col-1] +'>'
				if (digit == date)  // current cell represent today's date
					text += '<a class="' + classToday + '" onclick="setData(new Date(' + year +','+ month + ',' + digit + '))" href="#">' + digit + '</a>'
				else
					text += '<a class="' + classDay + '" onclick="setData(new Date(' + year +','+ month + ',' + digit + '))" href="#">' + digit + '</a>'
					
				digit++
				text += '</TD>'
			}
		}
		text += '</TR>'
	}
	
	// close all basic table tags
	text += '</TABLE>'
	text += 		'<TABLE width="100%" bgcolor=' + IntestColor + ' BORDER=' + border + ' bordercolor=' + IntestColor + ' CELLSPACING=' + cellspacing + '>' // table settings
	text += 			'<TR ALIGN="right">'
	text += 				'<TH width="100%" COLSPAN=1 HEIGHT=' + headerHeight + '>' 
	text += 				'</TH>'
	text += 			'</TR>'
	text += 		'</TABLE>'
	text += '</td></tr>'
	text += '</TABLE>'
	text += '</CENTER>'

	// print accumulative HTML string
	//prompt("",text)
	document.getElementById("Calendar").innerHTML= text
	document.getElementById("Calendar").style.top= getOffsetTop(objDataCurrentButton)
	document.getElementById("Calendar").style.left= getOffsetLeft(objDataCurrentButton)
	document.getElementById("Calendar").style.visibility= "visible"
}

			// create array of abbreviated day names
	var weekDay = new Array(7)
	weekDay[0] = "dom"
	weekDay[1] = "lun"
	weekDay[2] = "mar"
	weekDay[3] = "mer"
	weekDay[4] = "gio"
	weekDay[5] = "ven"
	weekDay[6] = "sab"
	
	var weekDayColor = new Array(7)
	weekDayColor[0] = "ff9797"
	weekDayColor[1] = "A8C5E1"
	weekDayColor[2] = "A8C5E1"
	weekDayColor[3] = "A8C5E1"
	weekDayColor[4] = "A8C5E1"
	weekDayColor[5] = "A8C5E1"
	weekDayColor[6] = "ff9797"
	
	var BordRiquadroSize = "3"
	var BordRiquadroColorlight = "5B92C8"
	var BordRiquadroColorDark = "2B5580"
	var IntestColor = "336699"
	var headerHeight = 10 // height of the table's header cell
	var border = 1 // 3D height of table's border
	var borderColor = "ffffff" // 3D height of table's border
	var cellspacing = 0 // width of table's border
	var headerColor = "336699" // color of table's header - default: midnightblue
	var HeadborderColor = "CCCCCC"
	var headerSize = "+1" // size of tables header font
	var colWidth = "25" // width of columns in table
	var dayCellHeight = "15" // height of cells containing days of the week
	var dayColor = "000000" // color of font representing week days
	var CellColorDay = "E2E2E2"
	var borderColorDay = "ffffff"
	var cellHeight = "15" // height of cells representing dates in the calendar
	var timeColor = "ffff00" // color of font representing current time
	var classDay = "Day"
	var classToday = "Today" // color specifying today's date in the calendar
	
	var objDataCurrent=null
	var objDataCurrentButton=null
	var CalendarAlign=null

//******** X Box Calendario
function getCalendar(obj,objButton,allinea)
{
	if (document.getElementById("Calendar")==null)
		document.body.insertAdjacentHTML('BeforeEnd','<div id="Calendar" style="position : absolute; visibility : hidden; z-index : 5;"></div>')
	objDataCurrent=obj;
	objDataCurrentButton=objButton
	CalendarAlign=allinea
	setCal(stringToDate(objDataCurrent.value))

}

function setData(objData)
{
	objDataCurrent.value= dateToString(objData)
	document.getElementById("Calendar").style.visibility= "hidden"
}

function Mouse_fuori()
{
	document.getElementById("Calendar").style.visibility= "hidden"
}

function getOffsetTop(obj)
{
	if (obj==null) return 0
	return obj.offsetTop + getOffsetTop(obj.offsetParent)
}

function getOffsetLeft(obj)
{
	if (obj==null && CalendarAlign==null) return 0
	if (obj==null && CalendarAlign=='sx') return -215
	return obj.offsetLeft + getOffsetLeft(obj.offsetParent)
}