var selectCalendar_MonthNL = new Array('', 'januari', 'februari', 'maart', 'april', 'mei', 'juni', 'juli', 'augustus', 'september', 'oktober', 'november', 'december');
var selectCalendar_MonthFR = new Array('', 'janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre');
var selectCalendar_DayNL = new Array('zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag');
var selectCalendar_DayFR = new Array('dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi');
function selectCalendarChange(idDays, idMonths, idYears)
{
    var days = document.getElementById(idDays);
    var months = document.getElementById(idMonths);
    var years = document.getElementById(idYears);
    if(eval(months.getAttribute('isMonthYear')) == true){years = months;};
    if(days == null || months == null || years == null){return;}
   
    var day, month, year
     
    day = days.options[days.selectedIndex].value;
    if(day == ''){day = 0;};
    
    if(eval(months.getAttribute('isMonthYear')) == true)
    {
        month = months.options[months.selectedIndex].getAttribute('month');
        year = months.options[months.selectedIndex].getAttribute('year');
    }
    else
    {
        month = months.options[months.selectedIndex].value;
        year = years.options[years.selectedIndex].value;
    };
    if(month == null || month == ''){month = 0;};
    if(year == null || year == ''){year = 0;};
    
    var monthdays = 31;
    if(month > 0 && year > 0){monthdays = selectCalendarGetDays(month, year);};
    
    var curLen = days.options.length - 1;
    if(curLen > monthdays)
    {
        for(var i = curLen; i > monthdays; i--)
        {
            try{days.options.remove(i);
            }catch(e){days.removeChild(days.options[i]);};
        };
    }
    else if(curLen < monthdays)
    {
        for(var i = curLen; i < monthdays; i++)
        {
            var opt = document.createElement("OPTION");
            days.options.add(opt);
            opt.text = i + 1;
            opt.value = i + 1;
        };
    };
    days.options[((day >= days.options.length) ? days.options.length - 1 : day)].selected = true;
};
function selectCalendarGetDays(month, year)
{
    var MonthDays = new Array(0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
 if(2 == month){return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28;};
 return MonthDays[month]; 
};
function selectCalendarClean(select)
{
    var count = select.options.length;
    if(count == 0){return;};
    for(var i = count; i >= 0; i--)
    {
        try{select.options.remove(i);
        }catch(e){select.removeChild(select.options[i]);};
    };
    select.options.length = 0;
};
function selectCalendarFillDays(selectId)
{
    var select = document.getElementById(selectId);
    if(select == null) return;
    selectCalendarClean(select);
    var opt = document.createElement("OPTION");
    select.options.add(opt);
    opt.text = '';
    opt.value = '';
    opt.selected = true;
    for(var i = 1; i <= 31; i++)
    {
        opt = document.createElement("OPTION");
        select.options.add(opt);
        opt.text = i;
        opt.value = i;    
    };
};
function selectCalendarFillMonths(selectId, lg)
{
    var Months
    if(lg == 'FR'){Months = selectCalendar_MonthFR;}
    else {Months = selectCalendar_MonthNL;};
    var select = document.getElementById(selectId);
    if(select == null) return;
    selectCalendarClean(select);
    var opt = document.createElement("OPTION");
    select.options.add(opt);
    opt.text = '';
    opt.value = '';
    opt.selected = true;
    for(var i = 1; i <= 12; i++)
    {
        opt = document.createElement("OPTION");
        select.options.add(opt);
        opt.text = Months[i];
        opt.value = i;    
    };
};
function selectCalendarFillYears(selectId, fromYear, toYear)
{
    var select = document.getElementById(selectId);
    if(select == null) return;
    selectCalendarClean(select);
    var opt = document.createElement("OPTION");
    select.options.add(opt);
    opt.text = '';
    opt.value = '';
    opt.selected = true;
    for(var i = toYear; i >= fromYear; i--)
    {
        opt = document.createElement("OPTION");
        select.options.add(opt);
        opt.text = i;
        opt.value = i;    
    };
};
function selectCalendarFillMonthYears(selectId, lg, startDate, endDate)
{
    var select = document.getElementById(selectId);
    if(select == null) return;
    selectCalendarClean(select);
    
    var Months
    if(lg == 'FR'){Months = selectCalendar_MonthFR;}
    else {Months = selectCalendar_MonthNL;};
    select.setAttribute('isMonthYear', true); 
    var opt = document.createElement("OPTION");
    select.options.add(opt);
    opt.text = '';
    opt.value = '';
    opt.selected = true;
    while(startDate <= endDate)
    {
        opt = document.createElement("OPTION");
        select.options.add(opt);
        opt.text = Months[startDate.getMonth() + 1] + ' ' + startDate.getFullYear();
        opt.setAttribute('month', startDate.getMonth() + 1);
        opt.setAttribute('year', startDate.getFullYear() + 1);
        opt.value = startDate.getFullYear() + '' + selectCalendar_LZ(startDate.getMonth() + 1);
        startDate = new Date(startDate.setMonth(startDate.getMonth() + 1));
    };
};
function addMonth(d,month){
 t  = new Date (d);
  t.setMonth(d.getMonth()+ month) ;
  if (t.getDate() < d.getDate())
 {
      t.setDate(0);
  }
  return t;
} 
function selectCalendar_LZ(val){return ((val < 0)||(val > 9) ? '': '0') + val;};
function selectCalendar_FormatDate(date, format, months, days)
{
    var value = new Object();
    var year = date.getFullYear() + '';
    value['y'] = year;
    value['yy'] = year.substring(2, 4);
    value['yyyy'] = year;
    var month = date.getMonth() + 1;
    value['M'] = month;
    value['MM'] = selectCalendar_LZ(month);
    try{value['MMM'] = months[month];}catch(e){};
    try{value['MMMM'] = months[month];}catch(e){};
    var day = date.getDate();
    value['d'] = day;
    value['dd'] = selectCalendar_LZ(day);
    day = date.getDay();
    try{value['ddd'] = days[day];}catch(e){};
    try{value['dddd'] = days[day];}catch(e){};
 
    var hours = date.getHours();
    value['H'] = hours;
    value['HH'] = selectCalendar_LZ(hours);
    if(hours == 0){value['h'] = 12;}
    else if(hours > 12){value['h'] = hours - 12;}
    else{value['h'] = hours;};
    value['hh'] = selectCalendar_LZ(value['h']);
    value['a'] = (hours > 11) ? 'PM' : 'AM';
    var minutes = date.getMinutes(); 
    value['m'] = minutes;
    value['mm'] = selectCalendar_LZ(minutes);
    var seconds = date.getSeconds();
    value['s'] = seconds;
    value['ss'] = selectCalendar_LZ(seconds);
 
    var i = 0, ch = '', token = '', res = '';
    while(i < format.length)
    {
        ch = format.charAt(i);
        token = '';
        while((format.charAt(i) == ch) && (i < format.length))
        {
            token += format.charAt(i++);
        };
        if(value[token] != null)
        {
            res += value[token];
        }
        else
        {
            res += token;
        };
    };
    return res;
};