/*
 * $Id: CalendarElement.js,v 77fe63b8dced 2006/10/20 16:26:00 hack $
 * Copyright 2003,2004 WorldTicket A/S
 *
 * @version $Revision$ $Date: 2006/10/20 16:26:00 $
 * @author S¿ren Ejsing (SEJ) / 2M business applications a|s
 * @author Claus Br¿ndby Reimer (CBR) / 2M business applications a|s
 */


/**
 * Creates a Calendar element inside a DIV HTML element.
 *
 * <p>It provides a view for selecting dates within a period of time, 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>
 *
 * <p>The Calendar object is drawn inside a DIV element using the innerHTML
 * method.</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.
 * @param view_CalendarLayout    The layout to use for view, if unspecified
 *                               it defaults to <code>CalendarLayout</code>
 */
function CalendarElement(divElement, minimunDate, maximumDate, view) {
    this.div = divElement;
    this.enabled = true;
    this.minDate;
    this.maxDate;
    this.view;
    this.calendar;

    this.div.owner = this;
    if (minimunDate instanceof Date) {
        this.setMinDate(minimunDate);
    } else {
        var now = new Date();
        var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
        this.setMinDate(today);
    }
    this.setMaxDate(maximumDate instanceof Date ? maximumDate : new Date(this.minDate.getFullYear() + 1, this.minDate.getMonth(), this.minDate.getDate()));

    this.calendar = new Calendar(new Date(this.getMinDate()));
    this.setView(view instanceof CalendarLayout ? view : new CalendarLayout());
}


/**
 * Gets the <code>Calendar</code> instance which have created the HTML element
 *
 * @param The element to find the <code>Calendar</code> instance for.
 */
CalendarElement.findObj = function(element) {
    var parent;

    parent = element.parentNode;
    while(!parent.owner) {
        parent = parent.parentNode;
    }

    return parent.owner;
}


/**
 * Sets a field of date.
 *
 * @param field_Number A date field.
 * @param amount_Number An amount to add.
 * @see #Calendar.DATE
 * @see #Calendar.MONTH;
 * @see #Calendar.YEAR;
 */
CalendarElement.prototype.set = function(field, value) {
    this.calendar.set(field, value);
    this.refresh();
    this.onChange();
}


/**
 * Adds an amount to the selected date.
 *
 * @param field_Number A date field.
 * @param amount_Number An amount to add.
 * @see #Calendar.DATE
 * @see #Calendar.MONTH;
 * @see #Calendar.YEAR;
 */
CalendarElement.prototype.add = function(field, value) {
    this.calendar.add(field, value);
    this.refresh();
    this.onChange();
}


/**
 * 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.
 */
CalendarElement.prototype.setMinDate = function(p_min_Date) {
    if (p_min_Date instanceof Date) {
        this.minDate = p_min_Date;
    } else {
        throw "Minimun date must be an instance of Date";
    }
}


/**
 * Gets the first selectable date.
 *
 * @return Date The first selectable date.
 */
CalendarElement.prototype.getMinDate = function() {
    return this.minDate;
}


/**
 * 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.
 */
CalendarElement.prototype.setMaxDate = function(p_max_Date) {
    if (p_max_Date instanceof Date) {
        this.maxDate = p_max_Date;
    } else {
        throw "Maximun date must be an instance of Date";
    }
}


/**
 * Gets the last selectable date.
 *
 * @return Date The last selectable date.
 */
CalendarElement.prototype.getMaxDate = function() {
    return this.maxDate;
}

/**
 * Sets this calendar element enabled or not.
 *
 * <p>When it is set to enabled it will respond to user input otherwise it
 * wont.</p>
 *
 * @param bFlag_boolean true if it is enabled otherwise false.
 */
CalendarElement.prototype.setEnabled = function(bFlag) {
    var textfield;

    this.enabled = bFlag;
    this.refresh();
}


/**
 * Gets whether this calendar element is enabled or not.
 *
 * @return boolean True if it is enabled otherwise false.
 */
CalendarElement.prototype.isEnabled = function() {
    return this.enabled;
}


/**
 * Refreshes the view.
 */
CalendarElement.prototype.refresh = function() {
    this.div.innerHTML = this.view.doLayout(this.calendar.getDate(), this.getMinDate(), this.getMaxDate(), this.isEnabled());
}


/**
 * Sets the layout for the calendar view.
 *
 * @param view_CalendarView A layout.
 */
CalendarElement.prototype.setView = function(view) {
    this.view = view;
    this.refresh();
}


/**
 * Gets the date.
 *
 * @return The date.
 */
CalendarElement.prototype.getDate = function() {
    return this.calendar.getDate();
}


/**
 * Sets the date
 *
 * @param date_Date The new date.
 */
CalendarElement.prototype.setDate = function(date) {
    this.calendar.setDate(date);
    this.refresh();
    this.onChange()
}

/**
 * onChage event
 */
CalendarElement.prototype.onChange = function() {
}

