var datePicker = {
    
    dateObj: {
        monthLen: [31,28,31,30,31,30,31,31,30,31,30,31],
        en: {
            todayStr: 'today',
            tomorrowStr: 'tomorrow',
            monthNames: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                            'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ],
            weekDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
        }
        // further languages go here...
    }
};

datePicker.checkIfLeapYear = function(y) {
    return (((y % 400) == "0") ? true
            : (((y % 100) == "0") ? false
            : (((y % 4) == "0") ? true
            : false)));
};

datePicker.appendOption = function(selectList, value, text, isSelected) {
    
    if(!this.f) {
        return;
    }
    
    var el = document.createElement("option");
    el.setAttribute("value", value);
    el.innerHTML = text;
    if (isSelected) {
        el.setAttribute("selected", "selected");
    }
    this.f[selectList].appendChild(el);
    return true;
};

datePicker.populateOptions = function(initDay) {
   
    var dIdx = this.curDay,
        wdIdx = this.curWeekDay,
        mIdx = this.curMonth,
        optionStr, j, dStr;
    
    for (j = 0; j <= this.daysToShow; j+=1) {
    
        dStr = (dIdx < 10) ? '0' + dIdx : dIdx;
        //create and append an option... 
        optionStr = this.dateObj[this.curLanguage].weekDays[wdIdx] + ' ' + 
                    dStr + ' ' + 
                    this.dateObj[this.curLanguage].monthNames[mIdx];
        
        if (j===0) {
            optionStr = this.dateObj[this.curLanguage].todayStr;
        }
        
        if (j===1) {
            optionStr = this.dateObj[this.curLanguage].tomorrowStr;
        }
        
        if (initDay && initDay === j) {
        	this.appendOption('dateSelect', j, optionStr, true);
        } else {
        	this.appendOption('dateSelect', j, optionStr, false);
        }
        // ... and update all local vars.
        if (dIdx < this.dateObj.monthLen[mIdx] || (this.isLeapYear && mIdx===1 && dIdx===28)) {
            dIdx += 1;
        } else {
            dIdx = 1;
            mIdx = (mIdx < 11) ? mIdx += 1 : 0;
        }
        wdIdx = (wdIdx === this.dateObj[this.curLanguage].weekDays.length-1) ? 0 : wdIdx+=1;
    }
};

datePicker.init = function(initDay, formId) {

    this.curLanguage = 'en';
    this.daysToShow = 30;
    this.date = new Date();
    this.curDay = this.date.getDate();
    this.curWeekDay = this.date.getDay();                   // 0-6, starting with sunday
    this.curYear = this.date.getFullYear();
    this.curMonth = this.date.getMonth();                   // 0-11
    this.isLeapYear = this.checkIfLeapYear(this.curYear);
    this.f = document.forms[formId] || document.forms[0];
    
    this.populateOptions(initDay);
    
    this.f['dateSelect'].onchange = function() { 
                            if (typeof dmap === 'object') {
                                dmap.setZoomlevel(0, this.value);
                            }
                        };
};

