 //-----------------------------------------------------------------------------
//Function font control
//-----------------------------------------------------------------------------
function fontCtrl(action){
if (!document.body.style.fontSize) { 
        document.body.style.fontSize = '100%';
    }
    else if(document.body.style.fontSize=='10px'){
        document.body.style.fontSize = '65%';
    }
 var newFont = parseInt(document.body.style.fontSize);
if (action=='increase'){
 newFont = newFont+10;
}
else if (action =='decrease'){
 newFont = newFont-10;
}
else {
alert("unknown action !");
}
document.body.style.fontSize = newFont+'%';
createCookie( 'fontSize' , newFont+'%' , 1 );
return false
}

pad = function(int) {
    return int=(int<10)?"0"+int:int
}
function buildCal(m, y, cM, cH, cDW, cD, brdr, lang, root_section_url, over_day){
    var mn_en=['January','February','March','April','May','June','July','August','September','October','November','December'];
    var mn_fr=['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre'];
    var previous_month_fr = 'Voir le calendrier du mois précédent'
    var next_month_fr = 'Voir le calendrier du mois suivant'
    var see_events_fr = 'Voir les évènements du '
    var previous_month_en = 'See the previous month calendar'
    var next_month_en = 'See the next month calendar'
    var see_events_en = 'See events of the '
    var mn = mn_fr
    var previous_month = previous_month_fr
    var next_month =  next_month_fr
    var see_events =  see_events_fr
    var dim=[31,0,31,30,31,30,31,31,30,31,30,31];
    var day = "LMMJVSD"
    if(lang == 'en'){
        mn = mn_en
        day = "MTWTFSS"
        previous_month = previous_month_en
        next_month =  next_month_en
        see_events =  see_events_en
    }
    var oD = new Date(y, m-1, 1); //DD replaced line to fix date bug when current day is 31st
    oD.od=oD.getDay(); //DD replaced line to fix date bug when current day is 31st
    if (oD.od == 0) oD.od=7
    var todaydate=new Date() //DD added
    var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD added
    var scanforoverday=
    dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
    var t='<table summary="Calendrier mensuel"><caption>Calendrier</caption>';
    t+='<thead>';
    t+='<tr>';
    t+='<td><a id="moisPrecedent" href="#" title="' + previous_month +  '" onclick="javascript:rewriteCalendar(' + (m - 1) +', ' + y + ', \'' + cM + '\', \'' + cH + '\', \'' + cDW + '\', \'' + cD + '\', ' + brdr + ', \'' + lang + '\', \'' + root_section_url + '\', \'' + over_day + '\');return false;"><span class="noDisplay">Mois précédent</span></a></td>';
    t+='<th scope="col" colspan="5">' + mn[m-1].toUpperCase() + ' ' + oD.getFullYear() + '</th>';
    t+='<td><a id="moisSuivant" href="#" title="' + next_month +  '" onclick="javascript:rewriteCalendar(' + (m + 1) +', ' + y + ', \'' + cM + '\', \'' + cH + '\', \'' + cDW + '\', \'' + cD + '\', ' + brdr + ', \'' + lang + '\', \''+ root_section_url + '\', \'' + over_day + '\');return false;"><span class="noDisplay">Mois suivant</span></a></td>';
    t+='</tr>';
    t+='</thead>';
    t+='<tbody>';
    t+='<tr id="jours">';
    for(s=0;s<7;s++)t+='<td class="'+cDW+'">'+day.substr(s,1)+'</td>';
    t+='</tr><tr>';
    for(i=1;i<=42;i++){
        var x=((i-oD.od>=0)&&(i-oD.od<dim[m-1]))? i-oD.od + 1 : '&nbsp;';
        var class_a = ''
        var str_date = pad(i-oD.od+1) +'/'+pad(m)+'/'+oD.getFullYear();
        if (x==scanfortoday){
            //x='<span id="today">'+x+'</span>';} //DD added
            class_a = 'class="today"';
        }
        if (str_date==over_day){
            class_a = 'class="highlight"';
        }
        if (x != '&nbsp;'){
            t+='<td><a href="'+root_section_url+'/events_search?startDate=' + str_date + '&endDate=' + str_date + '&over_day=' + str_date + '&f=" ' + class_a + ' title="' + see_events + (i-oD.od+1) + ' ' + mn[m-1] + '">'+x+'</a></td>';
        }else{
            t+='<td>'+x+'</td>';
        }
        if(((i)%7==0)&&(i<36))t+='</tr><tr>';
    }
    return t+='</tr></tbody></table>';
}

function rewriteCalendar(m, y, cM, cH, cDW, cD, brdr, lang, root_section_url, over_day){
    if (m < 1){
        m =12;
        y = y - 1;
    }
    else if (m > 12){
        m =1;
        y = y + 1;
    }
    document.getElementById('calendarspace').innerHTML = buildCal(m ,y, cM, cH, cDW, cD, brdr, lang, root_section_url, over_day)
}

function writeCalendar(year, lang, root_section_url, over_day){
    var todaydate = new Date()
    var curmonth = todaydate.getMonth()+1 //get current month (1-12)
    var curyear = todaydate.getFullYear() //get current year
    if (year != ''){
        curyear = year;
    }
    if (over_day != ''){
        var reg = new RegExp("/", "g");
        var tmp = over_day.split(reg);
        curmonth = Number(tmp[1]);
        curyear = Number(tmp[2]);
    }
    return buildCal(curmonth, curyear, 'main', 'month', 'daysofweek', 'days', 1, lang, root_section_url, over_day)
}
function writeTodayCalendar(lang, root_section_url, over_day){
    var todaydate=new Date()
    var curmonth=todaydate.getMonth()+1 //get current month (1-12)
    var curyear=todaydate.getFullYear() //get current year
    if (over_day != ''){
        var reg = new RegExp("/", "g");
        var tmp = over_day.split(reg);
        curmonth = Number(tmp[1]);
        curyear = Number(tmp[2]);
    }
    return buildCal(curmonth, curyear, 'main', 'month', 'daysofweek', 'days', 1, lang, root_section_url, over_day)
}
function centerPopup(url,name,width,height,options) {
    var top = (screen.height-height)/2;
    var left = (screen.width-width)/2;
    popup = window.open(url,name,"top="+ top +",left="+ left
+",width="+ width +",height="+ height +","+options);
    popup.focus();
}
 
function createCookie( nom, contenu, jours ) 
{
 var expireDate = new Date();
 expireDate.setTime( expireDate.getTime() + jours*24*3600*1000 );
 document.cookie = nom + "=" + escape( contenu ) + ";expires=" + expireDate.toGMTString();
}
function readCookie( nom ) 
{
 var deb, fin;
 deb = document.cookie.indexOf( nom + "=" );
 if ( deb >= 0 ) 
 {
  deb += nom.length + 1;
  fin = document.cookie.indexOf( ";" , deb );
  if ( fin < 0 ) 
   fin = document.cookie.length
  return unescape( document.cookie.substring( deb , fin ) );
 }
 return "";
}
function killCookie( nom ) 
{
 createCookie( nom , "" , -1 );
}
window.onload = function()
{
 var font =  readCookie( 'fontSize' );
 if ( font != "" )
 {
  document.body.style.fontSize = font;
 }
} 
