﻿/*
#Region "History"
'20100205 - RH - Script file added
'20100212 - RH -date in function draw now passed as a string
#End Region
*/

var calendar;

var Calendar = Class.create({
    init: function() {
        if (!$('tbCalendar')) {
            Element.insert(document.body, { bottom: '<div id="tbCalendar" style="display:none;"></div>' });
        };

        $$('.tbCalendar').each(function(item) {
            calendar.draw(item.id, item.value)
        });
    },
    draw: function(elementID, date) {
        var s = '<img src="/gfx/calendar.gif" style="float: right;" onclick="calendar.open(this, \'' + elementID + '\');"/>';

        Element.insert(elementID, { 'before': s });
    },
    open: function(t, elementID) {
        if (!$('tbCalendar').visible()) {
            
            var a = $F(elementID).split('/');
            this.update(elementID, a[2], a[1], a[0]);
            t.hasLayout = true;

            try {
                $('tbCalendar').setStyle({ top: t.cumulativeOffset().top + 16 + 'px', left: t.cumulativeOffset().left + 16 + 'px' });
            } catch (e) {
                //dodgy fix for <ie8
                $('tbCalendar').setStyle({ top: main.getXY().y + 'px', left: main.getXY().x + 'px' });
            };

            $('tbCalendar').show();
        };
    },
    update: function(elementID, y, m, d) {
        var s = '/ajax/Calendar.aspx?elementID=';
        s += elementID;
        s += '&y=';
        s += y;
        s += '&m=';
        s += m;
        s += '&d=';
        s += d;

        new Ajax.Request(s, {
            method: 'get',
            onSuccess: function(transport) {
                $('tbCalendar').update(main.getContent(transport.responseText));
                $('tbCalendar').show();
            }
        });
    },
    close: function() {
        $('tbCalendar').hide();
    },
    select: function(elementID, y, m, d) {
        $(elementID).value = this.GetLeadingZero(d) + '/' + this.GetLeadingZero(m) + '/' + y;
        setTimeout('calendar.close(\'' + elementID + '\')', 100);
    },
    FormatDate: function(d) {
        var s = '';
        s += this.GetLeadingZero(d.getDate());
        s += '/';
        s += this.GetLeadingZero(d.getMonth());
        s += '/';
        s += d.getFullYear();

        return s;
    },
    GetLeadingZero: function(s) {
        var s = '00000000000' + s;
        return s.substr(s.length - 2);
    }
});

calendar = new Calendar();
