var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var close = 'Close';
var clear = 'Clear';
if (language == 'fr') {
    months = new Array('Janvier', 'Février', 'Mars','avril','Mai','Juin','Juillet','août','Septembre','Octobre','Novembre','Décembre');
    close = 'Effacer';
    clear = 'Fermer';
}
if (language == 'es') {
    months = new Array('Enero', 'Febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'November', 'diciembre');
    close = 'Cerrar';
    clear = 'Borrar';
}
if (language == 'ar') {
    months = new Array('يناير', 'فبراير', 'مارس', 'إبريل', 'ماي', 'يونيو', 'يوليوز', 'غشت', 'شتنبر', 'أوكتوبر', 'نونبر', 'دجنبر');
    close = 'أغلق';
    clear = 'امسح';
}

var current_calendar = '';
var current_sel = '';
var callbacks = {foo: 1};

var ie = document.all;
var iframe_cal_obj = false;

if (ie) {
    iframe_cal_obj = $('iframe_cal');
}

// calendar object constructor
function calendar(id_cal, id_text, set_date)
{
    // is a calendar already open?
    if (current_calendar != '' && current_calendar != id_cal) {
        Effect.toggle(current_calendar, 'slide', {duration: 0.3});
    }

    current_calendar = id_cal;
    var mydate;

    div_cal = $(id_cal);
    div_text = $(id_text);

    if (set_date && set_date != 'yyyy/mm/dd') {
        mydate = new Date(set_date);
        current_sel = mydate.getTime();
    } else {
        mydate = new Date();
    }

    mydate.setHours(0); mydate.setMinutes(0); mydate.setSeconds(0); mydate.setMilliseconds(0);

    var out = '<div class="calendarwrap">'+
    '<div class="divider"><table border="0" cellpadding="0" cellspacing="0" style="background-color: #000000; text-align: center; color: white; padding-bottom: 0.25em; font-size: 12px; width: 100%;">'+
    '<tr><td style="width: 19%;"><a href="#" style="color: white" onclick="calendar(\''+id_cal+'\', \''+id_text+'\', \''+(mydate.getFullYear()-1)+'/'+(mydate.getMonth()+1)+'/01\', false, callbacks);return false;">&laquo;&laquo;</a></td><td style="width: 62%;"><span id="'+div_cal+'_year">'+mydate.getFullYear()+'</span></td><td style="width: 19%;"><a href="#" style="color: white" onclick="calendar(\''+id_cal+'\', \''+id_text+'\', \''+(mydate.getFullYear()+1)+'/'+(mydate.getMonth()+1)+'/01\', false, callbacks);return false;">&raquo;&raquo;</a></td></tr>'+
    '<tr><td style="width: 19%;"><a href="#" style="color: white" onclick="calendar(\''+id_cal+'\', \''+id_text+'\', \''+(mydate.getMonth()==0?(mydate.getFullYear()-1):mydate.getFullYear())+'/'+(mydate.getMonth()==0?12:mydate.getMonth())+'/01\', false, callbacks);return false;">&laquo;</a></td><td style="width: 62%;"><span id="'+div_cal+'_monthnum" style="display: none;">'+mydate.getMonth()+'</span><span id="'+div_cal+'_month">'+months[mydate.getMonth()]+'</span></td><td style="width: 19%;"><a href="#" style="color: white" onclick="calendar(\''+id_cal+'\', \''+id_text+'\', \''+(mydate.getMonth()==11?(mydate.getFullYear()+1):mydate.getFullYear())+'/'+(mydate.getMonth()==11?1:mydate.getMonth()+2)+'/01\', false, callbacks);return false;">&raquo;</a></td></tr>'+
    '</table></div>'+
    '<table style="margin-left: auto; margin-right: auto; font-size: 11px;">'+
    '<tr style="text-align: center;"><td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td></tr>';

    // start on a sunday
    var showdate = new Date(mydate.getFullYear()+'/'+(mydate.getMonth()+1)+'/01');
    showdate.setHours(12); showdate.setMinutes(0); showdate.setSeconds(0); showdate.setMilliseconds(0);
    while (showdate.getDay() != 0) {
        showdate.setTime(showdate.getTime() - 60*60*24*1000);
    }

    // weeks
    do {
        out += '<tr>';
        for (i=0; i<7; i++) {
            out += '<td style="text-align: center;"><a href="#" onclick="$(\''+id_text+'\').value=\''+(showdate.getFullYear())+'/'+(showdate.getMonth()+1)+'/'+showdate.getDate()+'\';calendar_off(\''+id_cal+'\'); '+(callbacks.onchange ? callbacks.onchange : '')+'return false;" style="'+(showdate.getMonth()!=mydate.getMonth()?'color: #999;':'')+(showdate.getTime()==current_sel?'background-color: yellow;':'')+'">'+showdate.getDate()+'</a></td>';
            showdate.setTime(showdate.getTime() + 60*60*24*1000);
        }
        out += '</tr>';
    } while (showdate.getMonth()==mydate.getMonth());

    out += '</table>'+
    '<div style="border-top: 1px solid #333; text-align: center; margin: 0.2em 0 0.2em 0; font-size: 12px;">'+
    '<a href="#" onclick="$(\''+id_text+'\').value=\'\'; calendar_off(\''+id_cal+'\'); '+(callbacks.onclear ? callbacks.onclear : '')+'return false;">'+clear+'</a> | '+
    '<a href="#" onclick="calendar_off(\''+id_cal+'\'); '+(callbacks.onclose ? callbacks.onclose : '')+'return false;">'+close+'</a>'+
    '</div></div>';
    
    div_cal.innerHTML = out;

    if (div_cal.style.display == 'none') {
        if (ie && $('iframe_cal')) {
            div_cal.style.display = '';
            var offsets = Position.cumulativeOffset(div_cal);
            $('iframe_cal').style.top = offsets[1] + 'px';
            $('iframe_cal').style.left = offsets[0] + 'px';
            $('iframe_cal').style.width = (div_cal.offsetHeight+12) + 'px';
            $('iframe_cal').style.height = (div_cal.offsetWidth+10) + 'px';
            $('iframe_cal').style.display = '';
            div_cal.style.display = 'none';
        }
        Effect.toggle(id_cal, 'slide', {duration: 0.3});
    }
    //return cal_c;
}

function calendar_off(id_cal)
{
    Effect.toggle(id_cal, 'slide', {duration:0.2});
    current_calendar = '';
    current_sel='';

    if (ie && $('iframe_cal')) {
        $('iframe_cal').style.display = 'none';
        $('iframe_cal').style.left = '-1000px';
    }
}



