// Original:  Nick Korosi (nfk2000@hotmail.com)
// Modified:  Rémy Dufour (rdufour@propage.com)
// - compatibility with mozilla
// - internationalization
// - personalized event
// - personalized style

//var myMonth;

function fToggleColor(myElement) {

    if (myElement.className == 'CALtd') {
        myElement.className = "CALtdOver";
    } else {
        myElement.className = 'CALtd';
    } // if
} // function

function fGetDaysInMonth(iMonth, iYear) {
    var dPrevDate = new Date(iYear, iMonth, 0);
    return dPrevDate.getDate();
} // function

function fBuildCal(iYear, iMonth, iDayStyle) {
    var aMonth = new Array();
    aMonth[0] = new Array(7);
    aMonth[1] = new Array(7);
    aMonth[2] = new Array(7);
    aMonth[3] = new Array(7);
    aMonth[4] = new Array(7);
    aMonth[5] = new Array(7);
    aMonth[6] = new Array(7);
    var dCalDate = new Date(iYear, iMonth-1, 1);
    var iDayOfFirst = dCalDate.getDay();
    var iDaysInMonth = fGetDaysInMonth(iMonth, iYear);
    var iVarDate = 1;
    var i, d, w;
    var iDate = '';


    if (iDayStyle == 2) {
        aMonth[0][0] = "Dimanche";
        aMonth[0][1] = "Lundi";
        aMonth[0][2] = "Mardi";
        aMonth[0][3] = "Mercredi";
        aMonth[0][4] = "Jeudi";
        aMonth[0][5] = "Vendredi";
        aMonth[0][6] = "Samedi";
    } else if (iDayStyle == 1) {
        aMonth[0][0] = "D";
        aMonth[0][1] = "L";
        aMonth[0][2] = "M";
        aMonth[0][3] = "M";
        aMonth[0][4] = "J";
        aMonth[0][5] = "V";
        aMonth[0][6] = "S";
    } else {
        aMonth[0][0] = "Dim";
        aMonth[0][1] = "Lun";
        aMonth[0][2] = "Mar";
        aMonth[0][3] = "Mer";
        aMonth[0][4] = "Jeu";
        aMonth[0][5] = "Ven";
        aMonth[0][6] = "Sam";
    }
    for (d = 0 ; d < iDayOfFirst; d++) {
        aMonth[1][d] = [
            '',
            null
        ];
    } // for
    for (d = iDayOfFirst; d < 7; d++) {
        iDate = '' + iYear + '-' + iMonth + '-' + iVarDate;
        if (CALuserEvents && CALuserEvents[iDate]) {
            aMonth[1][d] = [
                iVarDate,
                CALuserEvents[iDate]
            ];
        } else {
            aMonth[1][d] = [
                iVarDate,
                null
            ];
        }
        iVarDate++;
    } // for
    for (w = 2; w < 7; w++) {
        for (d = 0; d < 7; d++) {
            if (iVarDate <= iDaysInMonth) {
                iDate = '' + iYear + '-' + iMonth + '-' + iVarDate;
                if (CALuserEvents && CALuserEvents[iDate]) {
                    aMonth[w][d] = [
                        iVarDate,
                        CALuserEvents[iDate]
                    ];
                } else {
                    aMonth[w][d] = [
                        iVarDate,
                        null
                    ];
                }
                iVarDate++;
            } else {
                aMonth[w][d] = [
                    '',
                    null
                ];
            }// if
        } // for
    } // for

    return aMonth;
} // function

function fDrawCal(iYear, iMonth, iCellWidth, iCellHeight, sDateTextSize, sDateTextWeight, iDayStyle) {
    var myMonth = fBuildCal(iYear, iMonth, iDayStyle);
    var monthName =	new	Array("Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre");
    var dCurDate = new Date();

    document.write("<form name='frmCalendarSample' method='post' action=''>");
    document.write("<input type='hidden' name='calSelectedDate' id='calSelectedDate' value=''>");
    document.write("<table width=100%>");
    document.write("<tr>");
    document.write("<td align='center'>");
    document.write("<select name='tbSelMonth' class='CALselectMois' onchange='fUpdateCal(frmCalendarSample.tbSelYear.value, frmCalendarSample.tbSelMonth.value)'>");
    document.write("<option value='1'" + (dCurDate.getMonth()==0?" selected":"") + ">" + monthName[0] + "</option>");
    document.write("<option value='2'" + (dCurDate.getMonth()==1?" selected":"") + ">" + monthName[1] + "</option>");
    document.write("<option value='3'" + (dCurDate.getMonth()==2?" selected":"") + ">" + monthName[2] + "</option>");
    document.write("<option value='4'" + (dCurDate.getMonth()==3?" selected":"") + ">" + monthName[3] + "</option>");
    document.write("<option value='5'" + (dCurDate.getMonth()==4?" selected":"") + ">" + monthName[4] + "</option>");
    document.write("<option value='6'" + (dCurDate.getMonth()==5?" selected":"") + ">" + monthName[5] + "</option>");
    document.write("<option value='7'" + (dCurDate.getMonth()==6?" selected":"") + ">" + monthName[6] + "</option>");
    document.write("<option value='8'" + (dCurDate.getMonth()==7?" selected":"") + ">" + monthName[7] + "</option>");
    document.write("<option value='9'" + (dCurDate.getMonth()==8?" selected":"") + ">" + monthName[8] + "</option>");
    document.write("<option value='10'" + (dCurDate.getMonth()==9?" selected":"") + ">" + monthName[9] + "</option>");
    document.write("<option value='11'" + (dCurDate.getMonth()==10?" selected":"") + ">" + monthName[10] + "</option>");
    document.write("<option value='12'" + (dCurDate.getMonth()==11?" selected":"") + ">" + monthName[11] + "</option>");
    document.write("</select>");
    document.write("<select name='tbSelYear' class='CALselectAnnee' onchange='fUpdateCal(frmCalendarSample.tbSelYear.value, frmCalendarSample.tbSelMonth.value)'>");
    document.write("<option value='2006'" + (dCurDate.getFullYear()==2006?" selected":"") + ">2006</option>");
    document.write("<option value='2007'" + (dCurDate.getFullYear()==2007?" selected":"") + ">2007</option>");
    document.write("<option value='2008'" + (dCurDate.getFullYear()==2008?" selected":"") + ">2008</option>");
	document.write("<option value='2009'" + (dCurDate.getFullYear()==2009?" selected":"") + ">2009</option>");
	document.write("<option value='2010'" + (dCurDate.getFullYear()==2010?" selected":"") + ">2010</option>");
	document.write("<option value='2011'" + (dCurDate.getFullYear()==2011?" selected":"") + ">2011</option>");
	document.write("<option value='2012'" + (dCurDate.getFullYear()==2012?" selected":"") + ">2012</option>");
	document.write("<option value='2013'" + (dCurDate.getFullYear()==2013?" selected":"") + ">2013</option>");
	document.write("<option value='2014'" + (dCurDate.getFullYear()==2014?" selected":"") + ">2014</option>");
	document.write("<option value='2015'" + (dCurDate.getFullYear()==2015?" selected":"") + ">2015</option>");
	document.write("<option value='2016'" + (dCurDate.getFullYear()==2016?" selected":"") + ">2016</option>");
    document.write("</select>");


    document.write("</td>");
    document.write("</tr>");
    document.write("<tr>");
    document.write("<td>");



    document.write("<table border='0' cellspacing='2' cellpadding='2' width='100%'>");
    document.write("<tr>");
    document.write("<td align='center' class='CALJour'>" + myMonth[0][0] + "</td>");
    document.write("<td align='center' class='CALJour'>" + myMonth[0][1] + "</td>");
    document.write("<td align='center' class='CALJour'>" + myMonth[0][2] + "</td>");
    document.write("<td align='center' class='CALJour'>" + myMonth[0][3] + "</td>");
    document.write("<td align='center' class='CALJour'>" + myMonth[0][4] + "</td>");
    document.write("<td align='center' class='CALJour'>" + myMonth[0][5] + "</td>");
    document.write("<td align='center' class='CALJour'>" + myMonth[0][6] + "</td>");
    document.write("</tr>");
    for (w = 1; w < 7; w++) {
        document.write("<tr id=weekrow" + w + ">")
        for (d = 0; d < 7; d++) {
            i = ((7*w)+d)-7;
            document.write("<td class='CALtd' id=calDateText" + i + " name=calDateText" + i + ">");
            document.write(myMonth[w][d][0]);
            document.write("</td>");
        } // for
        document.write("</tr>");
    } // for
    document.write("</table>");


    document.write("</td>");
    document.write("</tr>");
    document.write("</table>");
    document.write("</form>");


    fUpdateCal(iYear, iMonth);

} // function

function fUpdateCal(iYear, iMonth) {
    var monthName =	new	Array("Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre");

    var myMonth = fBuildCal(iYear, iMonth);
    var myPrevDate = new Date(iYear, iMonth - 1, 1);
    var myNextDate = new Date(iYear, iMonth + 1, 1);

    //document.getElementById('calSelectedDate').value = "";

    /*document.getElementById("calCurMonth").innerHTML = monthName[iMonth-1] + " " + iYear;
    document.getElementById("calLinkPrev").setAttribute("onClick", "fUpdateCal(" + myPrevDate.getFullYear() + "," + myPrevDate.getMonth() + ")");
    document.getElementById("calLinkNext").setAttribute("onClick", "fUpdateCal(" + myNextDate.getFullYear() + "," + myNextDate.getMonth() + ")");
    */

    for (w = 1; w < 7; w++) {
        for (d = 0; d < 7; d++) {
            i = ((7*w)+d)-7;

            document.getElementById("calDateText" + i).innerHTML = myMonth[w][d][0];

            if (myMonth[w][d][1] != null) {
                document.getElementById("calDateText" + i).className = myMonth[w][d][1][0];                
                if (myMonth[w][d][1][1] != null) {                    
                    /*document.getElementById("calDateText" + i).innerHTML =
                        '<a href="' + myMonth[w][d][1][1] + '" class="CalLink"' +
                        (myMonth[w][d][1][2]!=null?' title="' + myMonth[w][d][1][2] + '"':'')
                         + '>' + myMonth[w][d][0] + '</a>';
                     */
                     document.getElementById("calDateText" + i).innerHTML =
                        '<a href="javascript:null;" onClick="return popActivites(\'' + myMonth[w][d][1][2] + '\', \'' + myMonth[w][d][1][0] + '\');" class="CalLink"' +
                        (myMonth[w][d][1][2]!=null?' title="' + myMonth[w][d][1][2] + '"':'')
                         + '>' + myMonth[w][d][0] + '</a>';
                } // if
            } else {                
                if (myMonth[w][d][0]) {
                    document.getElementById("calDateText" + i).className = "CALtd";
                } else  {
                    document.getElementById("calDateText" + i).className = "CALtdVide";
                }
            } // if
        } // for
    } // for
} // function

// End js