/*
 * $Id: CalendarTextFieldElement.js,v 1.4 2006/10/20 14:26:00 hack Exp $
 * Copyright 2003,2004 WorldTicket A/S
 *
 * @version $Revision: 1.4 $ $Date: 2006/10/20 14:26:00 $
 * @author Claus Br¿ndby Reimer (CBR) / 2M business applications a|s
 */


/**
 * Creates a Calendar element with a Textfield inside a DIV HTML element.
 *
 * <p>It provides a view for selecting dates within a period of time, and a
 * textfield for manualy inputting data.</p>
 *
 * <p>if no minimum or maximum date are giving it defaults to the current
 * system date and a year ahead, or if an minimum date are giving but no maximum
 * date it will default to that date and a year ahead.</p>
 *
 * <p>When initialized the current selected date is set to the minimum date.</p>
 *
 * @param divElement_htmlObject  The HTML div element to draw the Calendar
 *                               element inside.
 * @param minimunDate_Date       The first date which should be possible to
 *                               select.
 * @param maximumDate_Date       The last date which should be possible to
 *                               select.
 */
function CalendarTextFieldElement(divElement, minimunDate, maximumDate) {
    this.div = divElement;
    this.calendarElement;
    this.inputField;
    this.date;
    this.minimumDate = minimunDate;
    this.maximumDate = maximumDate;
    this.enabled = true;
    this.div.owner = this;

    this.doLayout();
    this.initBehavior();
    this.date = new Date(this.calendarElement.getDate());
    this.inputField.value = CalendarLocaleFactory.getLocale().asShortFormat(this.date);
}


/**
 * Draws this element in a HTML DIV
 */
CalendarTextFieldElement.prototype.doLayout = function() {
    var calendar;

    this.inputField = document.createElement("INPUT");
    this.inputField.type = "text";
    this.inputField.id = "date";
    this.inputField.name = "calendarTextField";

    calendar = document.createElement("DIV");

    this.div.appendChild(this.inputField);
    this.div.appendChild(calendar);

    this.calendarElement = new CalendarElement(calendar);
}


/**
 * Initialize this elements behavior by adding listeners its sub elements.
 *
 * <p>When a date is changed either by entering it manually in the textfield or
 * by selecting it in the <code>CalendarElement</code> the setDate method in
 * this object will be called.</p>
 */
CalendarTextFieldElement.prototype.initBehavior = function() {
    this.inputField.onblur = function() {
        CalendarElement.findObj(this).onTextFieldChange();
    }

    this.calendarElement.ref = this;
    this.calendarElement.onChange = function() {
        this.ref.onCalendarChange();
    }
}

CalendarTextFieldElement.prototype.onTextFieldChange = function() {
    var newDate;
    var locale;

    locale = CalendarLocaleFactory.getLocale();

    newDate = locale.fromShortFormat(this.inputField.value);

    if (!newDate) {
        newDate = this.date;
    }

    if (newDate.getTime() < this.getMinDate().getTime()) {
        alert("The date must be after " + locale.asShortFormat(this.getMinDate()));
        newDate = this.date;
    }

    if (newDate.getTime() > this.getMaxDate().getTime()) {
        alert("The date must be before " + locale.asShortFormat(this.getMaxDate()));
        newDate = this.date;
    }

    this.inputField.value = locale.asShortFormat(newDate);
    this.calendarElement.setDate(new Date(newDate));

    this.date = newDate;
    this.onChange();
}


CalendarTextFieldElement.prototype.onCalendarChange = function() {
    var locale;

    locale = CalendarLocaleFactory.getLocale();
    this.inputField.value = locale.asShortFormat(this.calendarElement.getDate());
    this.date = new Date(this.calendarElement.getDate());

    this.onChange();
}


/**
 * Sets the date.
 *
 */
CalendarTextFieldElement.prototype.setDate = function(date) {
    var locale;
    var newDate;
    var value;

    locale = CalendarLocaleFactory.getLocale();

    this.inputField.value = locale.asShortFormat(date);
    this.calendarElement.setDate(new Date(date));

    this.date = new Date(date);

    this.onChange();
}


/**
 * Gets the date.
 */
CalendarTextFieldElement.prototype.getDate = function() {
    return this.date;
}


/**
 * On change event for listening when a date either have been entered manually
 * through the text field or selected in the <code>CalendarElement</code>
 */
CalendarTextFieldElement.prototype.onChange = function() {
}


/**
 * Sets whether this element responds to user interactions.
 *
 * @param flag_boolean true if it should respond, false if not.
 */
CalendarTextFieldElement.prototype.setEnabled = function(flag) {
    if(this.enabled != flag) {
        this.enabled = flag;
        this.inputField.disabled = !this.enabled;
        this.calendarElement.setEnabled(this.enabled);
    }
}


/**
 * Tells whether this element responds to user interactions or not.
 */
CalendarTextFieldElement.prototype.isEnabled = function() {
    return this.enabled;
}


/**
 * Set a new <code>Date</code> for the first selectable date.
 *
 * <p>Note that the view will not automatical reflect this change, you need
 * to call showCal to apply it.</p>
 *
 * @param p_min_Date The new minimum date.
 */
CalendarTextFieldElement.prototype.setMinDate = function(p_min_Date) {
    this.calendarElement.setMinDate(p_min_Date);
}


/**
 * Gets the first selectable date.
 *
 * @return Date The first selectable date.
 */
CalendarTextFieldElement.prototype.getMinDate = function() {
    return this.calendarElement.getMinDate();
}


/**
 * Set a new <code>Date</code> for the last selectable date.
 *
 * <p>Note that the view will not automatical reflect this change, you need
 * to call showCal to apply it.</p>
 *
 * @param p_max_Date The new maximum date.
 */
CalendarTextFieldElement.prototype.setMaxDate = function(p_max_Date) {
    this.calendarElement.setMaxDate(p_max_Date);
}


/**
 * Gets the last selectable date.
 *
 * @return Date The last selectable date.
 */
CalendarTextFieldElement.prototype.getMaxDate = function() {
    return this.calendarElement.getMaxDate();
}

CalendarTextFieldElement.prototype.refresh = function() {
    var locale;

    locale = CalendarLocaleFactory.getLocale();
    this.inputField.value = locale.asShortFormat(this.date);
    this.calendarElement.refresh();
}

