﻿var Qs_Form_Element_Date = {

    message: new Qs_Message({
        'eng': {
        },
        'uk': {
        }
    }),

    init: function (id, options)
    {
        var element = document.getElementById('_' + id);
        if (!element) {
            alert('Qs_Form_Element_Date. Can not find element with id = _' + id);
            return false;
        }
        if (!element.tagName || element.tagName != 'INPUT') {
            alert('Qs_Form_Element_Date. Element (id = _' + id + ') is not "input"');
            return false;
        }
        if (element.type != 'text') {
            alert('Qs_Form_Element_Date. Element (id = _' + id + ') type is not "text"');
            return false;
        }
        
        $('#_' + id).datepicker(options);
        
        $(element).focus(Qs_Form_Element_Date.onFocus);
        $(element).blur(Qs_Form_Element_Date.onBlur);
        $(element).keyup(Qs_Form_Element_Date.onKeyUp);
        $(element).change(Qs_Form_Element_Date.onChange);
        includeWzTooltip();
    },

    getElementOptions: function (id)
    {
        return $('#_' + id).datepicker('option');
    },

    getElementOption: function (id, name)
    {
        return $('#_' + id).datepicker('option', name);
    },

    setElementOption: function (id, name, value)
    {
        $('#_' + id).datepicker('option', name, value);
    },

    setElementOptions: function (id, options)
    {
        for (var option in options) {
            Qs_Form_Element_Date.setOption(id, option, options[option]);
        }
    },

    callMethod: function (id, method, value)
    {
        return $('#_' + id).datepicker(method, value);
    },

    onFocus: function ()
    {
        var id = $(this).attr('id').substring(1);
        var showOn = Qs_Form_Element_Date.getElementOption(id, 'showOn');
        //if (showOn != 'both' && showOn != 'focus') {
            var hintContainer = document.getElementById('date_hint')
            if (!hintContainer) {
                var hint = document.createElement("div");
                hint.id = 'date_hint';
                hint.className = 'date_hint';
                hint.innerHTML = 'Date in format: ' + $.datepicker.formatDate(Qs_Form_Element_Date.getElementOption(id, 'dateFormat'), new Date());
                hint.style.display = 'none';
                document.body.appendChild(hint);
            }
            TagToTip('date_hint', BALLOON, true, ABOVE, true, FIX, [this, 0, 0], DELAY, 0);
        //}
        return true;
    },

    onBlur: function ()
    {
        UnTip('date_hint');
        return true;
    },

    onKeyUp: function ()
    {
        Qs_Form_Element_Date.dateChanged(this);
    },

    onChange: function ()
    {
        Qs_Form_Element_Date.dateChanged(this);
    },

    dateChanged: function (el)
    {
        var id = $(el).attr('id').substring(1);
        var tDate = $('#_' + id).val();
        try {
            var formatted = $.datepicker.parseDate(Qs_Form_Element_Date.getElementOption(id, 'dateFormat'), tDate);
            Qs_Form_Element_Date.callMethod(id, 'setDate', formatted);
        } catch (e) {
            $('#' + id).val(tDate);
        }
    }
}

