calDateFormat = "DD/MM/YYYY";

var weekdayList = new Array("Domenica", "Luned&igrave;", "Marted&igrave;", "Mercoled&igrave;", "Gioved&igrave;", "Venerd&igrave;", "Sabato");
var weekdayArray = new Array("Lun","Mar","Mer","Gio","Ven","Sab", "Dom");
var monthArray = new Array("Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre");


// CALENDAR COLORS
bottomBackground = "#DBDBDB";     		// BG COLOR OF THE BOTTOM FRAME
tableBGColor     = "black";  			// BG COLOR OF THE BOTTOM FRAME'S TABLE
bgColor          = "grey";				// DEFAULT BODY BG COLOR
cellColor        = "#EDEDED";      		// TABLE CELL BG COLOR OF THE DATE CELLS IN THE BOTTOM FRAME
headingCellColor = "#DBDBDB";       	// TABLE CELL BG COLOR OF THE WEEKDAY ABBREVIATIONS
headingTextColor = "#666666";       	// TEXT COLOR OF THE WEEKDAY ABBREVIATIONS
dateColor        = "red";          		// TEXT COLOR OF THE LISTED DATES (1-28+)
hoverColor       = "darkred";       	// TEXT COLOR OF A LINK WHEN YOU HOVER OVER IT
fontStyle        = "10pt Verdana, Arial, Helvetica, sans-serif";       // TEXT STYLE FOR DATES
headingFontStyle = " bold 8pt Verdana, Arial, Helvetica, sans-serif";  // TEXT STYLE FOR WEEKDAY ABBREVIATIONS new

// FORMATTING PREFERENCES
bottomBorder  = false;        // TRUE/FALSE (WHETHER TO DISPLAY BOTTOM CALENDAR BORDER)
tableBorder   = 0;            // SIZE OF CALENDAR TABLE BORDER (BOTTOM FRAME) 0=none

// END USER-EDITABLE SECTION -------------------------------------------------------

// GET CURRENTLY SELECTED LANGUAGE
selectedLanguage = navigator.language;

// PRE-BUILD PORTIONS OF THE CALENDAR WHEN THIS JS LIBRARY LOADS INTO THE BROWSER
buildCalParts();

// CALENDAR FUNCTIONS BEGIN HERE ---------------------------------------------------

var _info = navigator.userAgent
var os = _info.indexOf("Mac")
function loadToday()
{
  if( os != "-1")
  {
     setToday();
  }
  return;
}

// SET THE INITIAL VALUE OF THE GLOBAL DATE FIELD
function setDateField(dateField1,dateField2,dateField3) {

    // ASSIGN THE INCOMING FIELD OBJECT TO A GLOBAL VARIABLE
    calDateField1 = dateField1;
    calDateField2 = dateField2;
    calDateField3 = dateField3;

    // GET THE VALUE OF THE INCOMING FIELD
    inDate = dateField1.value;

    // SET calDate TO THE DATE IN THE INCOMING FIELD OR DEFAULT TO TODAY'S DATE
    setInitialDate();

    // THE CALENDAR FRAMESET DOCUMENTS ARE CREATED BY JAVASCRIPT FUNCTIONS
    calendarTop    = buildTopCal();
    calendarBottom = buildBottomCal();
    calendarTop +=  calendarBottom;
}

// SET THE INITIAL CALENDAR DATE TO TODAY OR TO THE EXISTING VALUE IN dateField
function setInitialDate() {
   
    // CREATE A NEW DATE OBJECT (WILL GENERALLY PARSE CORRECT DATE EXCEPT WHEN "." IS USED AS A DELIMITER)
    // (THIS ROUTINE DOES *NOT* CATCH ALL DATE FORMATS, IF YOU NEED TO PARSE A CUSTOM DATE FORMAT, DO IT HERE)
	
	var inDateTmp = "";
	if(inDate.length > 0)
	{
	   //getValForDB return DD.MM.YYYY oppure ""
	   inDateTmp = inDate;//getValForDB(inDate);

	   if(inDateTmp != "")
	   {
	       elementArray = inDateTmp.split("/");
		   calDate = new Date(elementArray[2], eval(elementArray[1]-1), elementArray[0]);
	   }
	   else
	   {
	       calDate = new Date(inDate);
	   }
	}
    else
	{
       //calDate = new Date(inDate);//se inDate.length non è >0, new Date(inDate) su Mac restituisce 1/1/1970
	   calDate = new Date();
	}

    // IF THE INCOMING DATE IS INVALID, USE THE CURRENT DATE
    if (isNaN(calDate)) {

        // ADD CUSTOM DATE PARSING HERE
        // IF IT FAILS, SIMPLY CREATE A NEW DATE OBJECT WHICH DEFAULTS TO THE CURRENT DATE
        calDate = new Date();
    }


    // KEEP TRACK OF THE CURRENT DAY VALUE
    calDay  = calDate.getDate();

    // SET DAY VALUE TO 1... TO AVOID JAVASCRIPT DATE CALCULATION ANOMALIES
    // (IF THE MONTH CHANGES TO FEB AND THE DAY IS 30, THE MONTH WOULD CHANGE TO MARCH
    //  AND THE DAY WOULD CHANGE TO 2.  SETTING THE DAY TO 1 WILL PREVENT THAT)
    calDate.setDate(1);
}


// POPUP A WINDOW WITH THE CALENDAR IN IT
function showCalendar(dateField1,dataField2,dataField3) {
	if(dateField1.disabled==true)
	{
		return;
	}
	
    // SET INITIAL VALUE OF THE DATE FIELD AND CREATE TOP AND BOTTOM FRAMES
    setDateField(dateField1,dataField2,dataField3);

    // USE THE JAVASCRIPT-GENERATED DOCUMENTS (calDocTop, calDocBottom) IN THE FRAMESET
    /*    
        calDocFrameset = 
        "<FRAMESET ROWS='60,*' BORDER='0'>\n" +
        "  <FRAME NAME='topCalFrame' SRC='javascript:parent.opener.calDocTop' SCROLLING='no'>\n" +
        "  <FRAME NAME='bottomCalFrame' SRC='javascript:parent.opener.calDocBottom' SCROLLING='no'>\n" +
        "</FRAMESET>\n";
    */

	//check if the window is already opened
	if(top.newWin && !top.newWin.closed)
	{
		top.newWin.close();
	}

    // DISPLAY THE CALENDAR IN A NEW POPUP WINDOW//235
    var winPrefs = "dependent=yes,width=235,height=300,screenX=200,screenY=300,titlebar=yes,status=no";
    top.newWin = window.open("javascript:parent.opener.calendarTop", "calWin", winPrefs);

    //top.newWin = window.open("javascript:parent.opener.calDocFrameset", "calendar", winPrefs);
    top.newWin.focus();
}

// CREATE THE TOP CALENDAR FRAME
function buildTopCal() {

    // CREATE THE TOP FRAME OF THE CALENDAR
    var calDoc =
        "<HTML>" +
        "<HEAD>" +
        "<HTML><HEAD><TITLE>Calendario</TITLE>\n" +
	    //"<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"/cp/css/style.css\">"+		//http://localhost:9080
	    "<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"css/stagione.css\"/>"+
	    "<STYLE type='text/css'>" +
        "<!--" +
        "TD.heading { text-decoration: none; color:" + headingTextColor + "; font: " + headingFontStyle + "; }" +
        "-->" +
        "</STYLE>" +	    
        "</HEAD>" +
        "<BODY bgcolor='"+bgColor+"' topmargin='0' marginheight='0' marginwidth='0' leftmargin='0' rightmargin='0' bottommargin='0' link=black>" +
        "<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0 width=100%><tr><td class=\"BgMain\">" +
        "<FORM NAME='calControl' style='margin:0' action='Javascript:doNothing();' onSubmit='return parent.opener.setYear();'>" +
        "<CENTER>" +
        //"<TABLE CELLPADDING=2 CELLSPACING=2 BORDER=0 bgcolor='"+topBackground+"' width=100%>" +
        "<TABLE CLASS=\"boxscuro\" CELLPADDING=2 CELLSPACING=2 BORDER=0 width=100%>" +
        "<TR><TD COLSPAN=7 align=center>" +
		"<TABLE border=0 >"+
		"<TR><TD COLSPAN=6 align=center>" +
        //"<CENTER>" +
        getMonthSelect() +
		"</TD><TD align=center>" +//Emilia
        "<INPUT NAME='year' class='cal_text' VALUE='" + calDate.getFullYear() + "' TYPE=TEXT SIZE=4 MAXLENGTH=4 >" +//onChange='parent.opener.setYear()'
		//"<image src=\"/nica/images/pixel.gif\" >"+
        //"</CENTER>" +
		"</TD></TR></TABLE>"+
        "</TD>" +
        "</TR>" +
        "<TR>" +
        "<TD COLSPAN=7 align=center>" +
        "<INPUT " +
        "TYPE=BUTTON NAME='previousYear' VALUE='<<'  class='cal_button'  onClick='parent.opener.setPreviousYear()'>"+
		"&nbsp;<INPUT " +
        "TYPE=BUTTON NAME='previousMonth' VALUE=' < ' class='cal_button'  onClick='parent.opener.setPreviousMonth()'>" +
		"&nbsp;<INPUT " +
        "TYPE=BUTTON NAME='today' VALUE='Mese corrente' class='cal_button' onClick='parent.opener.setToday()'>" +
		"&nbsp;<INPUT " +
        "TYPE=BUTTON NAME='nextMonth' VALUE=' > ' class='cal_button'  onClick='parent.opener.setNextMonth()'>" +
		"&nbsp;<INPUT " +
        "TYPE=BUTTON NAME='nextYear' VALUE='>>'   class='cal_button' onClick='parent.opener.setNextYear()'>" +
        "<BR>" +
        "</TD>" +
        "</TR>" +
        "</TABLE>" +
        "</CENTER>" +
        "</FORM>";// +
        //"</BODY>" +
        //"</HTML>";

    return calDoc;
}


// CREATE THE BOTTOM CALENDAR FRAME 
// (THE MONTHLY CALENDAR)
function buildBottomCal() {       

    // START CALENDAR DOCUMENT
    var calDoc = calendarBegin;

    // GET MONTH, AND YEAR FROM GLOBAL CALENDAR DATE
    month   = calDate.getMonth();
    year    = calDate.getFullYear();


    // GET GLOBALLY-TRACKED DAY VALUE (PREVENTS JAVASCRIPT DATE ANOMALIES)
    currentDate = new Date();//aggiunto Emilia: data corrente
    day = currentDate.getDate();
    //day     = calDay;//with calDay the selected RED day is that related to inDate, instead of the current day

    var i   = 0;

    // DETERMINE THE NUMBER OF DAYS IN THE CURRENT MONTH
    var days = getDaysInMonth();

    // IF GLOBAL DAY VALUE IS > THAN DAYS IN MONTH, HIGHLIGHT LAST DAY IN MONTH
    if (day > days) {
        day = days;
    }

    // DETERMINE WHAT DAY OF THE WEEK THE CALENDAR STARTS ON
    var firstOfMonth = new Date (year, month, 1);

    // GET THE DAY OF THE WEEK THE FIRST DAY OF THE MONTH FALLS ON
    var startingPos  = firstOfMonth.getDay() - 1;
    if (startingPos < 0 ) startingPos = 6;
    days += startingPos;

    // KEEP TRACK OF THE COLUMNS, START A NEW ROW AFTER EVERY 7 COLUMNS
    var columnCount = 0;

    // MAKE BEGINNING NON-DATE CELLS BLANK
    for (i = 0; i < startingPos; i++) {

        calDoc += blankCell;
	columnCount++;
    }

    // SET VALUES FOR DAYS OF THE MONTH
    var currentDay = 0;
    var dayType    = "weekday";

	var dateTmp =  new Date();//Emilia
	var currentMonth= dateTmp.getMonth();//Emilia
	
    // DATE CELLS CONTAIN A NUMBER
    for (i = startingPos; i < days; i++) {

	var paddingChar = "&nbsp;";

        // ADJUST SPACING SO THAT ALL LINKS HAVE RELATIVELY EQUAL WIDTHS
        if (i-startingPos+1 < 10) {
            padding = "&nbsp;&nbsp;";
        }
        else {
            padding = "&nbsp;";
        }

        // GET THE DAY CURRENTLY BEING WRITTEN
        currentDay = i-startingPos+1;

        // SET THE TYPE OF DAY, THE focusDay GENERALLY APPEARS AS A DIFFERENT COLOR
        if (currentDay == day && month == currentMonth) {//Emilia
		//if (currentDay == day) {//OLD
            dayType = "focusDay";
        }
        else {
            dayType = "weekDay";
        }

        // ADD THE DAY TO THE CALENDAR STRING
    
		calDoc += "<TD width=32 align=center onMouseOver='this.style.backgroundColor=\"white\"' onMouseOut='this.style.backgroundColor=\"lightgrey\"' bgcolor=lightgrey align=center >" +
                  "<a class='" + dayType + "'  href='javascript:parent.opener.returnDate(" + 
                  currentDay + ", \"\", \"\")'>" + currentDay  + "</a></TD>";

        columnCount++;

        // START A NEW ROW WHEN NECESSARY
        if (columnCount % 7 == 0) {
            calDoc += "</TR><TR height=18>";
        }
    }

    // MAKE REMAINING NON-DATE CELLS BLANK
    for (i=days; i<42; i++)  {

        calDoc += blankCell;
	columnCount++;

        // START A NEW ROW WHEN NECESSARY
        if (columnCount % 7 == 0) {
            calDoc += "</TR>";
            if (i<41) {
                calDoc += "<TR height=18>";
            }
        }
    }

    // FINISH THE NEW CALENDAR PAGE
    calDoc += calendarEnd;

    // RETURN THE COMPLETED CALENDAR PAGE
    return calDoc;
}


// WRITE THE MONTHLY CALENDAR TO THE BOTTOM CALENDAR FRAME
function writeCalendar() {
	
	calendarTop    = buildTopCal();
	
    // CREATE THE NEW CALENDAR FOR THE SELECTED MONTH & YEAR
    calendarBottom = buildBottomCal();

	calendarTop += calendarBottom;
	
    // WRITE THE NEW CALENDAR TO THE BOTTOM FRAME
	top.newWin.document.open();
	top.newWin.document.write(calendarTop);
	top.newWin.document.close();
    //top.newWin.frames['bottomCalFrame'].document.open();
    //top.newWin.frames['bottomCalFrame'].document.write(calendarBottom);
    //top.newWin.frames['bottomCalFrame'].document.close();
}


// SET THE CALENDAR TO TODAY'S DATE AND DISPLAY THE NEW CALENDAR
function setToday() {

    // SET GLOBAL DATE TO TODAY'S DATE
    calDate = new Date();

    // SET DAY MONTH AND YEAR TO TODAY'S DATE
    var month = calDate.getMonth();
    var year  = calDate.getFullYear();
	calDay = calDate.getDate();//Emilia set current day

    // SET MONTH IN DROP-DOWN LIST
    //top.newWin.frames['topCalFrame'].document.calControl.month.selectedIndex = month;
    top.newWin.document.calControl.month.selectedIndex = month;

    // SET YEAR VALUE
    //top.newWin.frames['topCalFrame'].document.calControl.year.value = year;
    top.newWin.document.calControl.year.value = year;

    // DISPLAY THE NEW CALENDAR
    writeCalendar();
}


// SET THE GLOBAL DATE TO THE NEWLY ENTERED YEAR AND REDRAW THE CALENDAR
function setYear() {
    // GET THE NEW YEAR VALUE
    //var year  = top.newWin.frames['topCalFrame'].document.calControl.year.value;
    var year  = top.newWin.document.calControl.year.value;

    // IF IT'S A FOUR-DIGIT YEAR THEN CHANGE THE CALENDAR
    if (isFourDigitYear(year)) {
        calDate.setFullYear(year);
		writeCalendar();
		return true;//new
    }
    else {
        // HIGHLIGHT THE YEAR IF THE YEAR IS NOT FOUR DIGITS IN LENGTH
        //top.newWin.frames['topCalFrame'].document.calControl.year.focus();
        top.newWin.document.calControl.year.focus();
        //top.newWin.frames['topCalFrame'].document.calControl.year.select();
        top.newWin.document.calControl.year.select();
		return false;//new
    }
}


// SET THE GLOBAL DATE TO THE SELECTED MONTH AND REDRAW THE CALENDAR
function setCurrentMonth() {

    // GET THE NEWLY SELECTED MONTH AND CHANGE THE CALENDAR ACCORDINGLY
    //var month = top.newWin.frames['topCalFrame'].document.calControl.month.selectedIndex;
    var month = top.newWin.document.calControl.month.selectedIndex;

    calDate.setMonth(month);
    writeCalendar();
}


// SET THE GLOBAL DATE TO THE PREVIOUS YEAR AND REDRAW THE CALENDAR
function setPreviousYear() {

    //var year  = top.newWin.frames['topCalFrame'].document.calControl.year.value;
    var year  = top.newWin.document.calControl.year.value;

    if (isFourDigitYear(year) && year > 1000) 
	{
        year--;
        calDate.setFullYear(year);
        //top.newWin.frames['topCalFrame'].document.calControl.year.value = year;
        top.newWin.document.calControl.year.value = year;
        writeCalendar();
    }
}


// SET THE GLOBAL DATE TO THE PREVIOUS MONTH AND REDRAW THE CALENDAR
function setPreviousMonth() {

    //var year  = top.newWin.frames['topCalFrame'].document.calControl.year.value;
    var year  = top.newWin.document.calControl.year.value;
    
    if (isFourDigitYear(year)) {
        //var month = top.newWin.frames['topCalFrame'].document.calControl.month.selectedIndex;
        var month = top.newWin.document.calControl.month.selectedIndex;

        // IF MONTH IS JANUARY, SET MONTH TO DECEMBER AND DECREMENT THE YEAR
        if (month == 0) {
            month = 11;
            if (year > 1000) {
                year--;
                calDate.setFullYear(year);
                //top.newWin.frames['topCalFrame'].document.calControl.year.value = year;
                top.newWin.document.calControl.year.value = year;
            }
        }
        else {
            month--;
        }
        calDate.setMonth(month);
        //top.newWin.frames['topCalFrame'].document.calControl.month.selectedIndex = month;
        top.newWin.document.calControl.month.selectedIndex = month;
        writeCalendar();
    }
}


// SET THE GLOBAL DATE TO THE NEXT MONTH AND REDRAW THE CALENDAR
function setNextMonth() {

    //var year = top.newWin.frames['topCalFrame'].document.calControl.year.value;
    var year = top.newWin.document.calControl.year.value;

    if (isFourDigitYear(year)) {
        //var month = top.newWin.frames['topCalFrame'].document.calControl.month.selectedIndex;
        var month = top.newWin.document.calControl.month.selectedIndex;

        // IF MONTH IS DECEMBER, SET MONTH TO JANUARY AND INCREMENT THE YEAR
        if (month == 11) {
            month = 0;
            year++;
            calDate.setFullYear(year);
            //top.newWin.frames['topCalFrame'].document.calControl.year.value = year;
            top.newWin.document.calControl.year.value = year;
        }
        else {
            month++;
        }
        calDate.setMonth(month);
        //top.newWin.frames['topCalFrame'].document.calControl.month.selectedIndex = month;
        top.newWin.document.calControl.month.selectedIndex = month;
        writeCalendar();
    }
}


// SET THE GLOBAL DATE TO THE NEXT YEAR AND REDRAW THE CALENDAR
function setNextYear() {

    //var year  = top.newWin.frames['topCalFrame'].document.calControl.year.value;
    var year  = top.newWin.document.calControl.year.value;
    if (isFourDigitYear(year)) {
        year++;
        calDate.setFullYear(year);
        //top.newWin.frames['topCalFrame'].document.calControl.year.value = year;
        top.newWin.document.calControl.year.value = year;
        writeCalendar();
    }
}


// GET NUMBER OF DAYS IN MONTH
function getDaysInMonth()  {

    var days;
    var month = calDate.getMonth()+1;
    var year  = calDate.getFullYear();

    // RETURN 31 DAYS
    if (month==1 || month==3 || month==5 || month==7 || month==8 ||
        month==10 || month==12)  {
        days=31;
    }
    // RETURN 30 DAYS
    else if (month==4 || month==6 || month==9 || month==11) {
        days=30;
    }
    // RETURN 29 DAYS
    else if (month==2)  {
        if (isLeapYear(year)) {
            days=29;
        }
        // RETURN 28 DAYS
        else {
            days=28;
        }
    }
    return (days);
}


// CHECK TO SEE IF YEAR IS A LEAP YEAR
function isLeapYear (Year) {

    if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
        return (true);
    }
    else {
        return (false);
    }
}


// ENSURE THAT THE YEAR IS FOUR DIGITS IN LENGTH
function isFourDigitYear(year) {

    if (year.length != 4) {
        //top.newWin.frames['topCalFrame'].document.calControl.year.value = calDate.getFullYear();
        //top.newWin.frames['topCalFrame'].document.calControl.year.select();
        //top.newWin.frames['topCalFrame'].document.calControl.year.focus();
        top.newWin.document.calControl.year.value = calDate.getFullYear();
        top.newWin.document.calControl.year.select();
        top.newWin.document.calControl.year.focus();
    }
    else {
        return true;
    }
}

// BUILD THE MONTH SELECT LIST
function getMonthSelect() {

	//monthArray - - > retrieved by jsp file (with internationalized strings)

    // DETERMINE MONTH TO SET AS DEFAULT
    var activeMonth = calDate.getMonth();

    // START HTML SELECT LIST ELEMENT
    monthSelect = "<SELECT NAME='month' class='cal_select' onChange='parent.opener.setCurrentMonth()' >";

    // LOOP THROUGH MONTH ARRAY
    for (i in monthArray) {
	
        // SHOW THE CORRECT MONTH IN THE SELECT LIST
        if (i == activeMonth) {
            monthSelect += "<OPTION SELECTED>" + monthArray[i] + "\n";
        }
        else {
            monthSelect += "<OPTION>" + monthArray[i] + "\n";
        }
    }
    monthSelect += "</SELECT>";

    // RETURN A STRING VALUE WHICH CONTAINS A SELECT LIST OF ALL 12 MONTHS
    return monthSelect;
}
//------------
// SET DAYS OF THE WEEK DEPENDING ON LANGUAGE
function createWeekdayList() {

    //weekdayList and weekdayArray - - > retrieved by jsp file (with internationalized strings)

    // START HTML TO HOLD WEEKDAY NAMES IN TABLE FORMAT
    var weekdays = "<TR BGCOLOR='" + headingCellColor + "'>";

    // LOOP THROUGH WEEKDAY ARRAY
    for (i in weekdayArray) {
        weekdays += "<TD width=32 class='heading' align=center>" + weekdayArray[i] + "</TD>";
    }
    weekdays += "</TR>";

    // RETURN TABLE ROW OF WEEKDAY ABBREVIATIONS TO DISPLAY ABOVE THE CALENDAR
    return weekdays;
}
//-----------

// PRE-BUILD PORTIONS OF THE CALENDAR (FOR PERFORMANCE REASONS)
function buildCalParts() {

    // GENERATE WEEKDAY HEADERS FOR THE CALENDAR
    weekdays = createWeekdayList();

    // BUILD THE BLANK CELL ROWS
    blankCell = "<TD width=32 align=center bgcolor='" + cellColor + "'>&nbsp;&nbsp;&nbsp;</TD>";

    // BUILD THE TOP PORTION OF THE CALENDAR PAGE USING CSS TO CONTROL SOME DISPLAY ELEMENTS
    calendarBegin =
        //"<HTML>" +
        //"<HEAD>" +
	    //"<LINK REL='stylesheet' TYPE='text/css' HREF='/nica/style/nicastyle.css'>"+		
        // STYLESHEET DEFINES APPEARANCE OF CALENDAR
        //"<STYLE type='text/css'>" +
        //"<!--" +
        //"TD.heading { text-decoration: none; color:" + headingTextColor + "; font: " + headingFontStyle + "; }" +
        //"-->" +
        //"</STYLE>" +
        //"</HEAD>" +
        //"<BODY topmargin='10' >" +
        "<BR><CENTER>";
        
        // BUILD WEEKDAY HEADINGS
        calendarBegin +=
            "<TABLE CELLPADDING=0  CELLSPACING=1 BORDER=" + tableBorder + " ALIGN=CENTER BGCOLOR='" + tableBGColor + "' width=90%>" +
            weekdays +
            "<TR height=18>";


    // BUILD THE BOTTOM PORTION OF THE CALENDAR PAGE
    calendarEnd = "";

        // WHETHER OR NOT TO DISPLAY A THICK LINE BELOW THE CALENDAR
        if (bottomBorder) {
            calendarEnd += "<TR height=18></TR>";
        }

        // END THE TABLE AND HTML DOCUMENT
        calendarEnd +=
            "</TABLE>" 
			
		// sets todays date
		today_date = new Date();
		m = today_date.getMonth() + 1;
		//var datetoconvert = today_date.getDate()+"."+m+"."+today_date.getFullYear();
		//alert(datetoconvert)

		converteddate = today_date.getDate()+"/"+m+"/"+today_date.getFullYear();;//getValToShow(datetoconvert);


		calendarEnd += "<table cellpadding=3><tr><td><a class=cal href=\"javascript:parent.opener.returnDate(" +today_date.getDate()+","+m+","+today_date.getFullYear()+ ")\" ><font color=white> Inserisci data odierna: "+ converteddate +" </font></a></td></tr></table></CENTER>" +
		"</TD></TR></TABLE></BODY></HTML>";
}


// REPLACE ALL INSTANCES OF find WITH replace
// inString: the string you want to convert
// find:     the value to search for
// replace:  the value to substitute
//
// usage:    jsReplace(inString, find, replace);
// example:  jsReplace("To be or not to be", "be", "ski");
//           result: "To ski or not to ski"
//
function jsReplace(inString, find, replace) {

    var outString = "";

    if (!inString) {
        return "";
    }

    // REPLACE ALL INSTANCES OF find WITH replace
    if (inString.indexOf(find) != -1) {
        // SEPARATE THE STRING INTO AN ARRAY OF STRINGS USING THE VALUE IN find
        t = inString.split(find);

        // JOIN ALL ELEMENTS OF THE ARRAY, SEPARATED BY THE VALUE IN replace
        return (t.join(replace));
    }
    else {
        return inString;
    }
}


// JAVASCRIPT FUNCTION -- DOES NOTHING (USED FOR THE HREF IN THE CALENDAR CALL)
function doNothing() {
}


// ENSURE THAT VALUE IS TWO DIGITS IN LENGTH
function makeTwoDigit(inValue) {

    var numVal = parseInt(inValue, 10);

    // VALUE IS LESS THAN TWO DIGITS IN LENGTH
    if (numVal < 10) {

        // ADD A LEADING ZERO TO THE VALUE AND RETURN IT
        return("0" + numVal);
    }
    else {
        return numVal;
    }
}


// SET FIELD VALUE TO THE DATE SELECTED AND CLOSE THE CALENDAR WINDOW
function returnDate(inDay, inMonth, inYear)
{
    // inDay = THE DAY THE USER CLICKED ON
    calDate.setDate(inDay);

    // SET THE DATE RETURNED TO THE USER
    var day           = calDate.getDate();
	
	
    var month         = calDate.getMonth()+1;
    var year          = calDate.getFullYear();
	
	if(inMonth != "")
	   month=inMonth;
	if(inYear != "")
	   year=inYear;
	
	
    var monthString   = monthArray[calDate.getMonth()];
    var monthAbbrev   = monthString.substring(0,3);
    var weekday       = weekdayList[calDate.getDay()];
    var weekdayAbbrev = weekday.substring(0,3);

    outDate = calDateFormat;

    // RETURN TWO DIGIT DAY
    if (calDateFormat.indexOf("DD") != -1) {
        day = makeTwoDigit(day);
        outDate = jsReplace(outDate, "DD", day);
    }
    // RETURN ONE OR TWO DIGIT DAY
    else if (calDateFormat.indexOf("dd") != -1) {
        outDate = jsReplace(outDate, "dd", day);
    }

    // RETURN TWO DIGIT MONTH
    if (calDateFormat.indexOf("MM") != -1) {
        month = makeTwoDigit(month);
        outDate = jsReplace(outDate, "MM", month);
    }
    // RETURN ONE OR TWO DIGIT MONTH
    else if (calDateFormat.indexOf("mm") != -1) {
        outDate = jsReplace(outDate, "mm", month);
    }
    // RETURN FOUR-DIGIT YEAR
    if (calDateFormat.indexOf("yyyy") != -1) {
        outDate = jsReplace(outDate, "yyyy", year);
    }
    // RETURN FOUR-DIGIT YEAR
	//Emilia : aggiunto quest'if per usare stessa nomenclatura MM-DD-YYYY e non MM-DD-yyyy
    else if (calDateFormat.indexOf("YYYY") != -1) {
        outDate = jsReplace(outDate, "YYYY", year);
    }
    // RETURN TWO-DIGIT YEAR
    else if (calDateFormat.indexOf("yy") != -1) {
        var yearString = "" + year;
        var yearString = yearString.substring(2,4);
        outDate = jsReplace(outDate, "yy", yearString);
    }
    // RETURN FOUR-DIGIT YEAR
    else if (calDateFormat.indexOf("YY") != -1) {
        outDate = jsReplace(outDate, "YY", year);
    }
    // RETURN DAY OF MONTH (Initial Caps)
    if (calDateFormat.indexOf("Month") != -1) {
        outDate = jsReplace(outDate, "Month", monthString);
    }
    // RETURN DAY OF MONTH (lowercase letters)
    else if (calDateFormat.indexOf("month") != -1) {
        outDate = jsReplace(outDate, "month", monthString.toLowerCase());
    }
    // RETURN DAY OF MONTH (UPPERCASE LETTERS)
    else if (calDateFormat.indexOf("MONTH") != -1) {
        outDate = jsReplace(outDate, "MONTH", monthString.toUpperCase());
    }

    // RETURN DAY OF MONTH 3-DAY ABBREVIATION (Initial Caps)
    if (calDateFormat.indexOf("Mon") != -1) {
        outDate = jsReplace(outDate, "Mon", monthAbbrev);
    }
    // RETURN DAY OF MONTH 3-DAY ABBREVIATION (lowercase letters)
    else if (calDateFormat.indexOf("mon") != -1) {
        outDate = jsReplace(outDate, "mon", monthAbbrev.toLowerCase());
    }
    // RETURN DAY OF MONTH 3-DAY ABBREVIATION (UPPERCASE LETTERS)
    else if (calDateFormat.indexOf("MON") != -1) {
        outDate = jsReplace(outDate, "MON", monthAbbrev.toUpperCase());
    }

    // RETURN WEEKDAY (Initial Caps)
    if (calDateFormat.indexOf("Weekday") != -1) {
        outDate = jsReplace(outDate, "Weekday", weekday);
    }
    // RETURN WEEKDAY (lowercase letters)
    else if (calDateFormat.indexOf("weekday") != -1) {
        outDate = jsReplace(outDate, "weekday", weekday.toLowerCase());
    }
    // RETURN WEEKDAY (UPPERCASE LETTERS)
    else if (calDateFormat.indexOf("WEEKDAY") != -1) {
        outDate = jsReplace(outDate, "WEEKDAY", weekday.toUpperCase());
    }

    // RETURN WEEKDAY 3-DAY ABBREVIATION (Initial Caps)
    if (calDateFormat.indexOf("Wkdy") != -1) {
        outDate = jsReplace(outDate, "Wkdy", weekdayAbbrev);
    }
    // RETURN WEEKDAY 3-DAY ABBREVIATION (lowercase letters)
    else if (calDateFormat.indexOf("wkdy") != -1) {
        outDate = jsReplace(outDate, "wkdy", weekdayAbbrev.toLowerCase());
    }
    // RETURN WEEKDAY 3-DAY ABBREVIATION (UPPERCASE LETTERS)
    else if (calDateFormat.indexOf("WKDY") != -1) {
        outDate = jsReplace(outDate, "WKDY", weekdayAbbrev.toUpperCase());
    }

    // SET THE VALUE OF THE FIELD THAT WAS PASSED TO THE CALENDAR
   calDateField1.value = day;
   calDateField2.value = month;
   calDateField3.value = year;

    // GIVE FOCUS BACK TO THE DATE FIELD
   calDateField1.focus();
   calDateField2.focus();
   calDateField3.focus();
  
    // CLOSE THE CALENDAR WINDOW
    top.newWin.close()
}

function statusDateSetter() {   
	window.defaultStatus = "";   
	window.status = "The date format is : dd/mm/yyyy";
}
