/*
 * $Id: Calendar.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
 * @author Bjorn Rosell (BRO) / 2M business applications a|s
 */


/**
 * Creates a Calendar object for manipulating dates.
 *
 * @param date_Date A date if not specified it defaults to the curent system
 *                  date
 */
function Calendar(date) {
    this.date

    this.date = date ? date : new Date();
}

Calendar.DATE = 1;
Calendar.MONTH = 2;
Calendar.YEAR = 3;

Calendar.isLeapYear = function(iYear) {
    if (iYear % 4 != 0) return false;
    if (iYear % 100 != 0) return true;
    return (iYear % 400 == 0);
};
    

// month is zero-based
Calendar.daysInMonth = function(iYear, iMonth) {
    if (iMonth != 1) {
        if (iMonth<=6) {
            return 31 - (iMonth % 2)
        }
        else {
            return 30 + (iMonth % 2)
        }
    }
    else {
        return Calendar.isLeapYear(iYear) ? 29 : 28;
    }
};

/**
 * Adds an amount to the selected date.
 * FIX-ME: This method is fundementally poor.
 *
 * @param field_Number A date field.
 * @param amount_Number An amount to add.
 * @see #Calendar.DATE
 * @see #Calendar.MONTH;
 * @see #Calendar.YEAR;
 */
Calendar.prototype.add = function(field, amount) {
    var d = this.getDate();

    switch (field) {
        case Calendar.DATE:
            this.getDate().setDate(this.getDate().getDate() + amount);
            break;

        case Calendar.MONTH:
            var newMonth = (d.getMonth() + amount + 12) % 12;
            var newYear = d.getFullYear();
            if (amount > 0) {
                newYear = newYear + Math.floor(amount/12);
                if (d.getMonth() + amount >= 12) {
                    newYear++;
                }
            }
            else {
                newYear = newYear - Math.floor(-amount/12);
                if (d.getMonth() + amount < 0) {
                    newYear--;
                }
            }
            var iDaysInMonth = Calendar.daysInMonth(d.getYear(), newMonth);
            var newDate = d.getDate();
            if (newDate > iDaysInMonth) {
                newDate = iDaysInMonth
            }
            this.getDate().setDate(newDate);
            this.getDate().setMonth(newMonth);
            this.getDate().setFullYear(newYear);
            
            break;

        case Calendar.YEAR:
            this.getDate().setYear(this.getDate().getYear() + amount);
            break;
    }
}


/**
 * 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;
 */
Calendar.prototype.set = function(field, value) {

    switch (field) {
        case Calendar.DATE:
            this.getDate().setDate(value);
            break;

        case Calendar.MONTH:
            this.getSelectedMonth().setMonth(value);
            break;

        case Calendar.YEAR:
            this.getDate().setYear(value);
            break;
    }
}


/**
 * Sets the date.
 *
 * <p>The view will be refreshed to reflect the new date.</p>
 *
 * @param p_selected_Date The new date.
 */
Calendar.prototype.setDate = function(p_selected_Date) {
    if (p_selected_Date instanceof Date) {
        this.date = p_selected_Date;
    } else {
        throw "Minimun date must be an instance of Date";
    }
}


/**
 * Gets the selected date.
 *
 * @return Date The selected date.
 */
Calendar.prototype.getDate = function() {
    return this.date;
}


/**
 * Converts a <code>Date</code> object to a <code>String</code> object.
 *
 * @param p_Date A <code>Date</code> which should be converted.
 * @return The converted date as a <code>String</code>
 */
Calendar.prototype.date2String = function(p_Date) {
	return (new String(p_Date.getDate()+"-"+(p_Date.getMonth()+1)+"-"+p_Date.getFullYear()+" "));
}





