/* Календарь */

/* Пример использования:

    <form>
    <script>
        var cFrom = new clite('2004-03-09');
        cFrom.addField('from', '%Y-%M-%D 00:00:00');
        cFrom.write();
    </script>
    <br/>
    <script>
        var cTo = new clite(); // даты не выбрана
        cTo.addField('to-year', '%Y');
        cTo.addField('to-month', '%M');
        cTo.addField('to-day', '%D');
        cTo.write();
    </script>

    <input type="submit"/>
    </form>

    Метод addField добавляет поле с заданным именем и нужным форматом даты.
    Пока что есть %Y, %M, %D.
    При необходимости можно добавить новые "теги" в метод format.

    Дата принимается в формате YYYY-MM-DD, можно изменить в функции string2date.

*/

// ------------------------------------------------------------------------------------------------

var mShort = ['янв', 'фев', 'мар', 'апр', 'мая', 'июн', 'июл', 'авг', 'сен', 'окт', 'ноя', 'дек'];
var mLong = ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'];
var wShort = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс'];

var currentId = -1;

// ------------------------------------------------------------------------------------------------

/*
    id-шники:

    calDiv - div, в котором отображается календарь

    calObj4462 - object
    calInput4462 - input
    calButton4462 - button
*/

// clite.calendar()
function clite(string)
{
    this.date = string2date(string);
    if (!this.date)
    {
        this.date = new Date();
        this.empty = true;
        // если empty = true, то поле с датой пустое и ничего не сабмитится
    }

    this.fields = new Object;
    this.rnd = Math.floor(Math.random() * 1000000);
    window['calObj' + this.rnd] = this;

    // methods
    this.toString = date2string;
    this.addField = addField;
    this.format = format;
    this.write = write;
    this.update = update;
    this.html = html;
    this.update_date = update_date;

    var div = document.getElementById('calDiv');
    if (!div)
    {
        var div = document.createElement('div');
        div.id = 'calDiv';
        div.className = 'calendar-div';
        var body = document.body
        body.insertBefore(div, body.firstChild);

        addEvent(div, 'click', calClick);
    }

    this.div = div;
}

// ------------------------------------------------------------------------------------------------

// clite.write()
function write()
{
    var fields = this.fields;
    document.write('<input class="calendar-input" id="calInput' + this.rnd + '" value="' + this.toString() + '" readonly="yes">&nbsp;');
    document.write('<input class="calendar-button" id="calButton' + this.rnd + '" type="button" value=" " onclick="showCalendar(this, ' + this.rnd + ')">');
    for (field in fields)
    {
        document.write('<input name="' + field + '" id="' + field + '" type="hidden" value="' + this.format(fields[field]) + '">');
    }
    this.update();

    var button = document.getElementById('calButton' + this.rnd);
    addEvent(button, 'click', calClick);
}

function update_date(string)
{
    this.date = string2date(string);
    this.update();
}

// clite.update()
function update()
{
    this.div.innerHTML = this.html();
    var fields = this.fields;
    var myvar;
    for (field in fields)
    {
        myvar = this.format(fields[field]);
        document.getElementById(field).value = myvar;
    }
    document.getElementById('calInput' + this.rnd).value = this.toString();
}

// clite.toString()
function date2string()
{
    if (this.empty) {
        return '';
    }

    var date = this.date;
    return  zeroFill(date.getDate()) + ' ' + mShort[date.getMonth()] + ' ' + date.getFullYear();
}

// clite.addField()
function addField(name, format)
{
    this.fields[name] = format;
}

//clite.format()
function format(f)
{
    if (this.empty) {
        return '';
    }

    var fY = this.date.getFullYear()
    f = f.replace(/%Y/g, fY);
    f = f.replace(/%M/g, zeroFill(this.date.getMonth() + 1));
    f = f.replace(/%D/g, zeroFill(this.date.getDate()));
    f = f.replace(/%y/g, zeroFill(fY % 100))
    return f;
}

// ------------------------------------------------------------------------------------------------

function repos(o)
{
    var style = document.getElementById('calDiv').style;
    var pos = getPosition(o);

    style.left = pos.x;
    style.top = pos.y;
}

function show()
{
    var cal = document.getElementById('calDiv');
    if (cal)
    {
        //		hide_selects('show');
        cal.style.visibility = 'visible';
    }
}

function hide()
{
    var cal = document.getElementById('calDiv');
    if (cal)
    {
        cal.style.visibility = 'hidden';
        //		hide_selects('hide');
        closeCalendarTimeOut = 0;
    }
}

function visible()
{
    var cal = document.getElementById('calDiv');
    if (cal) {
        return (cal.style.visibility == 'visible');
    }
    return false;
}

function string2date(string)
{
    var re = /(\d+)-(\d+)-(\d+)/;
    var date = re.exec(string);
    if (date) {
        return new Date(date[1], date[2] - 1, date[3]);
    }
}

function zeroFill(value)
{
    return (value < 10 ? '0' : '') + value;
}

function hide_selects(mode)
{
    var selects = document.getElementsByTagName('select');
    for (var i = 0; i < selects.length; i++)
    {
        selects[i].style.visibility = (mode == 'show') ? 'hidden' : 'visible';
    }
}

// ------------------------------------------------------------------------------------------------

function showCalendar(o, id)
{
    repos(o);

    var cal = window['calObj' + id];
    cal.update();

    if (id == currentId && visible())
    {
        currentId = -1;
        hide();
    }
    else {
        currentId = id;
        show();
    }
}

function calSetDate(id, i)
{
    var cal = window['calObj' + id];
    cal.date.setDate(i);
    hide();
    cal.empty = false;
    cal.update();
}

function calSetMonth(id, i)
{
    var cal = window['calObj' + id];
    cal.date.setMonth(i + cal.date.getMonth());
    cal.update();
}

function calSetYear(id, i)
{
    var cal = window['calObj' + id];
    cal.date.setYear(i + cal.date.getFullYear());
    cal.update();
}

function calSetToday(id)
{
    var cal = window['calObj' + id];
    cal.date = new Date();
    hide();
    cal.empty = false;
    cal.update();
}

function calClear(id)
{
    var cal = window['calObj' + id];
    cal.date = new Date();
    hide();
    cal.empty = true;
    cal.update();
}

function calDayOver(o)
{
    o.oldClassName = o.className;
    o.className = o.className + ' over';
}

function calDayOut(o)
{
    o.className = o.oldClassName;
}

// ------------------------------------------------------------------------------------------------

function getPosition(o)
{
    var x = o.offsetLeft;
    var y = o.offsetTop + o.offsetHeight;
    // + 1
    while (o.offsetParent != null)
    {
        o = o.offsetParent;
        x += o.offsetLeft;
        y += o.offsetTop;
    }
    var pos = new Object();
    pos.x = x-250;
    pos.y = y-34;
    return pos;
}

// ------------------------------------------------------------------------------------------------

// clite.html()
function html()
{
    var r = '<form action="login.php" method="POST"><table cellpadding="0" border="0" cellspacing="0" class="calendar">';



    r += '<tr><td valign="bottom" height="35">';


r += '<table border="0">';

r += '<tr><td align="right">';
 
r += 'E-mail:</td><td align="right"> <input size="30" type="text" name="username"  maxlength="30"></td></tr>';

r += '<tr><td>';

r += 'Пароль:</td><td align="right"><input size="30" name="password" type="password"  maxlength="14"></td></tr></table>';

r += '</td></tr><tr><td valign="top" align="right"><input type="submit" src="images/enter.gif" value="&nbsp;Войти&nbsp;"   name="userlogin" >&nbsp;</form>';

    return r;
}

// ------------------------------------------------------------------------------------------------

function addEvent(element, event, func)
{
    if (element.addEventListener)
    {
        element.addEventListener(event, func, false);
    }
    else if (element.attachEvent)
    {
        element.attachEvent('on' + event, func);
    } else {
        //
    }
}

function calClick(e)
{
    var event = (e) ? e : window.event;
    event.cancelBubble = true;
}

addEvent(window, 'resize', hide);
addEvent(document, 'click', hide);
