/**
 * @author Peter Britka
 * Spassbaron Javascripts
 */

/**
 * Counts how many characters are left
 * @param element el where you type
 * @param string messageId id of element for the message
 * @param int max number of allowed characters
 */
function countCharsLeft(el, messageId, max)
{
    var text = $j(el).val();
    var charsLeft = max - text.length;
    if ( charsLeft < 0 )
    {
        charsLeft = 0;
        text = $j(el).val().substring(0, max)
    }
    $j(el).val(text);
    $j('#' + messageId).html(charsLeft);
}

/**
 * Class for date validation
 */
Varien.VoucherDate = Class.create();
Varien.VoucherDate.prototype = {
    initialize: function(selector, required, format) {
        var el        = $$(selector)[0];
        this.day      = Element.select($(el), '.voucher-valid-until-day input')[0];
        this.month    = Element.select($(el), '.voucher-valid-until-month input')[0];
        this.year     = Element.select($(el), '.voucher-valid-until-year input')[0];
        this.voucherValidUntil      = Element.select($(el), '.voucher-valid-until-full input')[0];
        this.advice   = Element.select($(el), '.validation-advice')[0];
        this.required = required;
        this.format   = format;

        this.day.validate = this.validate.bind(this);
        this.month.validate = this.validate.bind(this);
        this.year.validate = this.validate.bind(this);

        this.advice.hide();
    },

    validate: function() {
        var error = false;

        if (this.day.value=='' && this.month.value=='' && this.year.value=='') {
            if (this.required) {
                error = 'This date is a required value.';
            } else {
                this.voucherValidUntil.value = '';
            }
        } else if (this.day.value=='' || this.month.value=='' || this.year.value=='') {
            error = 'Please enter a valid full date.';
        } else {
            var date = new Date();
            if (this.day.value<1 || this.day.value>31) {
                error = 'Please enter a valid day (1-31).';
            } else if (this.month.value<1 || this.month.value>12) {
                error = 'Please enter a valid month (1-12).';
            } else if (this.year.value<date.getFullYear() || this.year.value>date.getFullYear()+2) {
                error = 'Please enter a valid year (%s - %s).';
                try
                {
                    var translated = Translator.translate(error);
                    this.advice.innerHTML = sprintf(translated, date.getFullYear(), date.getFullYear() + 2);
                }
                catch (e)
                {
                    this.advice.innerHTML = error;
                }

                this.advice.show();
                return false;
            } else {
                this.voucherValidUntil.value = this.format.replace(/(%m|%b)/i, this.month.value).replace(/(%d|%e)/i, this.day.value).replace(/%y/i, this.year.value);
                var testDate = this.month.value + '/' + this.day.value + '/'+ this.year.value;
                var test = new Date(testDate);
                if (isNaN(test)) {
                    error = 'Please enter a valid date.';
                }
            }
        }

        if (error !== false) {
            try {
                this.advice.innerHTML = Translator.translate(error);
            }
            catch (e) {
                this.advice.innerHTML = error;
            }
            this.advice.show();
            return false;
        }

        this.advice.hide();
        return true;
    }
}
