/* ================================================================ */
/* FUNZIONI RELATIVE ALLA VISUALIZZAZIONE E GESTIONE DEL CALENDARIO */
/* ================================================================ */

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
}

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]
}

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(frmitdate,frmserialdate,y,m,d) {
	
	if (frmitdate != null) text_italian_date = frmitdate
	if (frmserialdate != null) text_serial_date = frmserialdate
	
	// standard time attributes
	var today = 0
	//Today
	var now = new Date()
	var year = now.getYear()
	if (year < 1000)
	year+=1900
	var month = now.getMonth()
	var monthName = getMonthName(month)
	var date = now.getDate()
	today = date
	now = null
	
	//Date passed
	if (d != null && m != null && y != null) {
	
		var date_passed = new Date(y, m-1, d)
		var date_passed_year = date_passed.getYear()
		if (date_passed_year < 1000)
		date_passed_year+=1900
		var date_passed_month = date_passed.getMonth()
		var date_passed_monthName = getMonthName(date_passed_month)
		var date_passed_date = date_passed.getDate()
		date_passed = null

		if (month == date_passed_month && year == date_passed_year) today = date
		else today = 0
		
		date = date_passed_date
		month = date_passed_month
		monthName = date_passed_monthName
		year = date_passed_year
	}

	// 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()
	if (firstDay == 0) firstDay = 7
	firstDayInstance = null

	// number of days in current month
	var days = getDays(month, year)

	// call function to draw calendar
	drawCal(firstDay, days, today, date, month, monthName, year)
}

function drawCal(firstDay, lastDate, today, date, month, monthName, year) {
// constant table settings
var headerHeight = 10 // height of the table's header cell
var border = 1 // 3D height of table's border
var bordercolor = "silver"
var cellspacing = 0 // width of table's border
var headerColor = "navy" // color of table's header
var headerSize = "2" // size of tables header font
var weekSize = "2" // size of tables header font
var daySize = "2" // size of days
var dayColor = "black" // color of days
var colWidth = 10 // width of columns in table
var weekCellHeight = 10 // height of cells containing days of the week
var weekColor = "darkblue" // color of font representing week days
var cellHeight = 10 // height of cells representing dates in the calendar
var todayColor = "navy" // color specifying today's date in the calendar
var dateColor = "black" // color specifying today's date in the calendar
var timeColor = "purple" // color of font representing current time
var holidaybgcolor = "#e0e0e0"
var weekbgcolor = "#e0e0e0"

var currItalianLink = ''
var currCompactLink = ''
var digitTwoDigit = ''

month++
var prevmonth = (month == 0) ? 12 : month - 1
var nextmonth = (month == 13) ? 1 : month + 1

// create basic table structure
var text = "" // initialize accumulative variable to empty string
text += '<html><head><title>Scelta della data</title></head><body link="black" vlink="black" alink="black" text="black">'
text += '<CENTER>'
text += '<TABLE bordercolor="' + bordercolor + '" BORDER=' + border + ' CELLSPACING=' + cellspacing + '>' // table settings
text += '<TR><TD COLSPAN=7 HEIGHT=' + headerHeight + '><TABLE border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td width="9"><a href="javascript:window.opener.setCal(null,null,' + year + ',' + prevmonth + ',' + date + ')"><img src="images/frecciasx.gif" border="0" width="10" height="11" alt="mese precedente"></a></td><td align="center" width="60"><FONT COLOR="' + headerColor + '" SIZE=' + headerSize + '>' + monthName + '</font></td><td width="9"><a href="javascript:window.opener.setCal(null,null,' + year + ',' + nextmonth + ',' + date + ')"><img src="images/frecciadx.gif" border="0" width="10" height="11" alt="mese successivo"></a></td><td width="20">&nbsp;</td><td width="10"><a href="javascript:window.opener.setCal(null,null,' + parseInt(year - 10) + ',' + month + ',' + date + ')"><img src="images/frecciassx.gif" border="0" width="10" height="11" alt="10 anni prima"></a></td><td width="5">&nbsp;</td><td width="9"><a href="javascript:window.opener.setCal(null,null,' + parseInt(year - 1) + ',' + month + ',' + date + ')"><img src="images/frecciasx.gif" border="0" width="10" height="11" alt="anno precedente"></a></td><td align="center"><FONT COLOR="' + headerColor + '" SIZE=' + headerSize + '>' + year +  '</font></td><td width="9"><a href="javascript:window.opener.setCal(null,null,' + parseInt(year + 1) + ',' + month + ',' + date + ')"><img src="images/frecciadx.gif" border="0" width="10" height="11" alt="anno successivo"></a></td><td width="5">&nbsp;</td><td width="10"><a href="javascript:window.opener.setCal(null,null,' + parseInt(year + 10) + ',' + month + ',' + date + ')"><img src="images/frecciaddx.gif" border="0" width="10" height="11" alt="10 anni dopo"></a></td></tr></table>'

// variables to hold constant settings
var openCol = '<TD bgcolor="' + weekbgcolor + '" WIDTH=' + colWidth + ' HEIGHT=' + weekCellHeight + '>'
openCol += '<FONT COLOR="' + weekColor + '" SIZE="' + headerSize + '">'
var closeCol = '</FONT></TD>'

// create array of abbreviated day names
var weekDay = new Array(7)
weekDay[0] = "Lun"
weekDay[1] = "Mar"
weekDay[2] = "Mer"
weekDay[3] = "Gio"
weekDay[4] = "Ven"
weekDay[5] = "Sab"
weekDay[6] = "Dom"

// 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 += openCol + 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="right" VALIGN="top">'
	for (var col = 1; col <= 7; ++col) {
		if (digit > lastDate) {
			for (var i = col; i <= 7; ++i) {
				if (i > 5) text += '<TD bgcolor="' + holidaybgcolor + '">&nbsp;</TD>';
				else text += '<TD>&nbsp;</TD>';
			}
			break;
		}
		//break
		if (curCell < firstDay) {
			if (col > 5) text += '<TD bgcolor="' + holidaybgcolor + '">&nbsp;</TD>';
			else text += '<TD>&nbsp;</TD>';
			curCell++
		} else {
			if (col > 5) text += '<TD bgcolor="' + holidaybgcolor + '" HEIGHT=' + cellHeight + '>'
			else  text += '<TD HEIGHT=' + cellHeight + '>'
			currItalianLink = digit + ' ' + monthName + ' ' + year
			currCompactLink = String(year) + String((String(month).length == 2) ? String(month) : '0' + String(month)) + String((String(digit).length == 2) ? String(digit) : '0' + String(digit))
			
			text += '<a href="javascript:window.opener.document.' + text_italian_date + '.value=\'' + currItalianLink + '\';window.opener.document.' + text_serial_date + '.value=\'' + currCompactLink + '\';window.close()">'
			switch (digit) {
				case today:
					text += '<FONT COLOR="' + todayColor + '" SIZE="' + daySize + '"><b>' + digit + '</b>'
					break;
				//case date:
				//	text += '<FONT COLOR="' + dateColor + '" SIZE="' + daySize + '">' + digit
				//	break;
				default:
					text += '<FONT COLOR="' + dayColor + '" SIZE="' + daySize + '">' + digit
			}
			text += '</font></a></TD>'
			digit++
		}
	}
	text += '</TR>'
}

// close all basic table tags
text += '</TABLE><table border="0" cellpadding="0" cellspacing="0" height="30"><tr><td align="center"><a href="javascript:window.opener.document.' + text_italian_date + '.value=\'\';window.opener.document.' + text_serial_date + '.value=\'00000000\';window.close()"><font color="navy" size="2"><u>annulla data</u></font></a></td></tr></table>'
text += '</CENTER>'

//text += '</body></html>'

// print accumulative HTML string
var w
var h
var x = (screen.width - 200) / 2
var y = (screen.height - 200) / 2
var calWin = window.open("", "calWin","width=200,height=200,status=no,resizable=no,top="+y+",left="+x);
calWin.document.write(text)
calWin.document.close()
calWin.focus()
}

/* ================================ */
/* FUNZIONI DI CONTROLLO SULLE DATE */
/* ================================ */

function datediff(sDateStart,sDateEnd,bestremi) {
	var dateStart = Date.parse(new Date(sDateStart.substr(0,4), sDateStart.substr(4,2), sDateStart.substr(6,2)))
	var dateEnd = Date.parse(new Date(sDateEnd.substr(0,4), sDateEnd.substr(4,2), sDateEnd.substr(6,2)))
	var retdiff = (dateStart-dateEnd)/86400000
	if (bestremi && retdiff > 0) retdiff = retdiff + 1
	return retdiff
}

/* =========================================== */
/* FUNZIONI RELATIVE ALL'ESTRAZIONE DELLA DATA */
/* =========================================== */

function getItalianDate(date) {
	date = trimDate(date)
	if (IsNullStringDate(date) || date == '00000000') {
		return ''
	} else {
		return extractDay(date) + ' ' + getMonthName(extractMonth(date)-1) + ' ' + extractYear(date)
	}
}

function extractYear(date) {
	date = trimDate(date)
	if (IsNullStringDate(date) || date == '00000000') return null
	else return date.substr(0,4)
}

function extractMonth(date) {
	date = trimDate(date)
	if (IsNullStringDate(date) || date == '00000000') return null
	else return date.substr(4,2)
}

function extractDay(date) {
	date = trimDate(date)
	if (IsNullStringDate(date) || date == '00000000') return null
	else return date.substr(6,2)
}

/* ==================== */
/* FUNZIONI DI UTILITA' */
/* ==================== */

function trimDate(sString) {

	if (sString.length == 0 || IsNullStringDate(sString)) return sString

	var newString = ''

	while (sString.charAt(0) == ' ') {
		for (i=1;i<sString.length - 1;i++) { newString = newString + sString.charAt(i) }
		sString = newString
		newString = ''
	}
	while (sString.charAt(sString.length - 1) == ' ') {
		for (i=0;i<sString.length - 1;i++) { newString = newString + sString.charAt(i) }
		sString = newString
		newString = ''
	}

	return sString
}

function IsNullStringDate(sString) {

	if (sString.length == 0) return true

	var i = -1
	var bempty = true
	while (bempty && i < sString.length - 1) {
		i++;
		if (sString.charAt(i) != ' ') { bempty = false }
	}
	return bempty
}

