/**
* Calendar DHTML dialog for the date picker
*
* Shows a calendar under a date field
* Inspired by Richard Heyes' Popup Calendar
*
* @application  ePark Labs Central Intelligence Server
* @language     Javascript v1.4
* @author       Dan Fitzpatrick <dan@eparklabs.com>
* @copyright    ePark Labs, Inc. 2004
* @created      2004-03-04
* @version      
* @todo         
*/

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


/**
* Global variables
*/
	appCalendar_layers          = new Array();
	appCalendar_mouseoverStatus = false;
	appCalendar_mouseX          = 0;
	appCalendar_mouseY          = 0;
  user_date_format            = USER_DATE_FORMAT; // See appCalendar_formatResult for instructions

/**
* The calendar constructor
*
* @access public
* @param string field_id     The ID of the field that should get the date
*/
	function appCalendar()
	{
		/**
        * Properties
        */
		// Todays date
		this.today          = new Date();
		this.date           = this.today.getDate();
		this.month          = this.today.getMonth();
		this.year           = this.today.getFullYear();

		this.objName        = 'appCalendarDialog';
		this.imagesPath     = '/local/media/';
		this.layerID        = 'appCalendar_layer_' + appCalendar_layers.length;

		//this.offsetX        = -85;
		//this.offsetY        = 12;

		this.useMonthCombo  = true;
		this.useYearCombo   = true;
		this.yearComboRange = 5;

		this.currentMonth   = this.month;
		this.currentYear    = this.year;
        this.currentDate    = this.date;

		/**
        * Public Methods
        */
		this.show              = appCalendar_show;
		this.writeHTML         = appCalendar_writeHTML;
        this.display           = appCalendar_display;
        this.formatResult      = appCalendar_formatResult;
        this.getDateFromField  = appCalendar_getDateFromField;

		// Accessor methods
		this.setOffset         = appCalendar_setOffset;
		this.setOffsetX        = appCalendar_setOffsetX;
		this.setOffsetY        = appCalendar_setOffsetY;
		this.setImagesPath     = appCalendar_setImagesPath;
		this.setMonthCombo     = appCalendar_setMonthCombo;
		this.setYearCombo      = appCalendar_setYearCombo;
		this.setCurrentMonth   = appCalendar_setCurrentMonth;
		this.setCurrentYear    = appCalendar_setCurrentYear;
		this.setYearComboRange = appCalendar_setYearComboRange;

		/**
        * Private methods
        */
		// Layer manipulation
		this._getLayer         = appCalendar_getLayer;
		this._hideLayer        = appCalendar_hideLayer;
		this._showLayer        = appCalendar_showLayer;
		this._setLayerPosition = appCalendar_setLayerPosition;
		this._setHTML          = appCalendar_setHTML;

		// Miscellaneous
		this._getDaysInMonth   = appCalendar_getDaysInMonth;
		this._mouseover        = appCalendar_mouseover;
    this._getMonthNumber   = appCalendar_getMonthNumber;

		/**
        * Constructor type code
        */
		appCalendar_layers[appCalendar_layers.length] = this;
		//this.writeHTML();
	}
    
    function appCalendar_display(id)
    {
        this.field_id = id;
        this.field = document.getElementById(id);
        this.show();
    }

/**
* Shows the calendar, or updates the layer if
* already visible.
*
* @access public
* @param integer month Optional month number (0-11)
* @param integer year  Optional year (YYYY format)
*/
	function appCalendar_show()
	{
		// Variable declarations to prevent globalisation
		var month, year, monthnames, numdays, thisMonth, firstOfMonth;
		var ret, row, i, cssClass, linkHTML, previousMonth, previousYear;
		var nextMonth, nextYear, prevImgHTML, prevLinkHTML, nextImgHTML, nextLinkHTML;
		var monthComboOptions, monthCombo, yearComboOptions, yearCombo, html;
		
        if(this.field.value != '' && arguments.length == 0)
        {
          parts = this.getDateFromField();
          this.currentMonth = month = parts[1];
          this.currentYear  = year  = parts[2];
          this.currentDate = date = parts[0];
        }else{
          this.currentMonth = month = arguments[0] != null ? arguments[0] : this.currentMonth;
          this.currentYear  = year  = arguments[1] != null ? arguments[1] : this.currentYear;
        }

		monthnames = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
		numdays    = this._getDaysInMonth(month, year);

		thisMonth    = new Date(year, month, 1);
		firstOfMonth = thisMonth.getDay();

		previousYear  = thisMonth.getFullYear();
		previousMonth = thisMonth.getMonth() - 1;
		if(previousMonth < 0){
			previousMonth = 11;
			previousYear--;
		}
		
		nextYear  = thisMonth.getFullYear();
		nextMonth = thisMonth.getMonth() + 1;
		if(nextMonth > 11){
			nextMonth = 0;
			nextYear++;
		}

        // Start the return array
		ret = new Array(new Array());
        
		// Previous month
        previousNumDays    = this._getDaysInMonth(previousMonth, previousYear);
        i = previousNumDays - firstOfMonth +1;
        while(i <= previousNumDays)
        {
			ret[0][ret[0].length] = '<td class="appCalendar_other_month_day" align="center"><div class="cal-pop-day" onclick="appCalendar_hideLayer();setField(\'' + this.field_id + '\',appCalendar_formatResult(' + i + ', ' + (previousMonth + 1) + ', ' + previousYear + '));">' + i + '</div></td>';
            i++;
		}

		// Current month
		row = 0;
		i   = 1;
		while(i <= numdays){
			if(ret[row].length == 7){
				ret[++row] = new Array();
			}

			/**
            * Generate this cells' HTML
            */
			cssClass = (i == this.date && month == this.month && year == this.year) ? 'appCalendar_today' : 'appCalendar_day';
			cssClass = (i == this.currentDate && month == this.currentMonth && year == this.currentYear) ? 'appCalendar_selected_day' : cssClass;
			//linkHTML = (i++);
			ret[row][ret[row].length] = '<td align="center" class="' + cssClass + '"><div class="cal-pop-day" onclick="appCalendar_hideLayer();setField(\'' + this.field_id + '\',appCalendar_formatResult(' + i + ', ' + (Number(month) + 1) + ', ' + year + '));">' + (i++) + '</div></td>';
		}
        
		// Next month
        i = 1;
        x = ret[row].length;
		while(i <= (7 - x)){
			ret[row][ret[row].length] = '<td class="appCalendar_other_month_day" align="center"><div class="cal-pop-day" onclick="appCalendar_hideLayer();setField(\'' + this.field_id + '\',appCalendar_formatResult(' + i + ', ' + (nextMonth + 1) + ', ' + nextYear + '));">' + i + '</div></td>';
            i++;
		}

		// Format the HTML
		for(i=0; i<ret.length; i++){
			ret[i] = ret[i].join('\n') + '\n';
		}

		prevImgHTML  = '&lt;' // <img src="' + this.imagesPath + 'icon_prev.gif" alt="&lt;" border="0" />';
		prevLinkHTML = '<a href="javascript: ' + this.objName + '.show(' + previousMonth + ', ' + previousYear + ')">' + prevImgHTML + '</a>';
		nextImgHTML  = '&gt;' //<img src="' + this.imagesPath + 'icon_next.gif" alt="&gt;" border="0" />';
		nextLinkHTML = '<a href="javascript: ' + this.objName + '.show(' + nextMonth + ', ' + nextYear + ')">' + nextImgHTML + '</a>';

		/**
        * Build month combo
        */
		if (this.useMonthCombo) {
			monthComboOptions = '';
			for (i=0; i<12; i++) {
				selected = (i == thisMonth.getMonth() ? 'selected="selected"' : '');
				monthComboOptions += '<option value="' + i + '" ' + selected + '>' + monthnames[i] + '</option>';
			}
			monthCombo = '<select name="months" style="font-size:10pt" onchange="' + this.objName + '.show(this.options[this.selectedIndex].value, ' + this.objName + '.currentYear)">' + monthComboOptions + '</select>';
		} else {
			monthCombo = monthnames[thisMonth.getMonth()];
		}
		
		/**
        * Build year combo
        */
		if (this.useYearCombo) {
			yearComboOptions = '';
			for (i = thisMonth.getFullYear() - this.yearComboRange; i <= (thisMonth.getFullYear() + this.yearComboRange); i++) {
				selected = (i == thisMonth.getFullYear() ? 'selected="selected"' : '');
				yearComboOptions += '<option value="' + i + '" ' + selected + '>' + i + '</option>';
			}
			yearCombo = '<select style="border: 1px groove;font-size:10pt" name="years" onchange="' + this.objName + '.show(' + this.objName + '.currentMonth, this.options[this.selectedIndex].value)">' + yearComboOptions + '</select>';
		} else {
			yearCombo = thisMonth.getFullYear();
		}

		html = '<table>';
		html += '<tr><td class="appCalendar_header" colspan="7" align="center">&nbsp;' + prevLinkHTML + '&nbsp;' + monthCombo + yearCombo + '&nbsp;' + nextLinkHTML + '&nbsp;</td></tr>';
		html += '<tr>';
		html += '<td class="appCalendar_dayname">Sun</td>';
		html += '<td class="appCalendar_dayname">Mon</td>';
		html += '<td class="appCalendar_dayname">Tue</td>';
		html += '<td class="appCalendar_dayname">Wed</td>';
		html += '<td class="appCalendar_dayname">Thu</td>';
		html += '<td class="appCalendar_dayname">Fri</td>';
		html += '<td class="appCalendar_dayname">Sat</td></tr>';
		html += '<tr>' + ret.join('</tr>\n<tr>') + '</tr>';
		html += '</table>';

		this._setHTML(html);
		if (!arguments[0] && !arguments[1]) {
			this._setLayerPosition();
			this._showLayer();
		}
	}

/**
* Writes HTML to document for layer
*
* @access public
*/
	function appCalendar_writeHTML()
	{
		//if (is_ie5up || is_nav6up || is_gecko) {
			document.write('<a href="javascript: ' + this.objName + '.show()"><img src="' + this.imagesPath + 'calendar.gif" border="0" width="16" height="16" /></a>');
			//document.write('<div class="appCalendar" id="' + this.layerID + '" onmouseover="' + this.objName + '._mouseover(true)" onmouseout="' + this.objName + '._mouseover(false)"></div>');
		//}
	}

/**
* Sets the offset to the mouse position
* that the calendar appears at.
*
* @access public
* @param integer Xoffset Number of pixels for vertical
*                        offset from mouse position
* @param integer Yoffset Number of pixels for horizontal
*                        offset from mouse position
*/
	function appCalendar_setOffset(Xoffset, Yoffset)
	{
		this.setOffsetX(Xoffset);
		this.setOffsetY(Yoffset);
	}

/**
* Sets the X offset to the mouse position
* that the calendar appears at.
*
* @access public
* @param integer Xoffset Number of pixels for horizontal
*                        offset from mouse position
*/
	function appCalendar_setOffsetX(Xoffset)
	{
		this.offsetX = Xoffset;
	}

/**
* Sets the Y offset to the mouse position
* that the calendar appears at.
*
* @access public
* @param integer Yoffset Number of pixels for vertical
*                        offset from mouse position
*/
	function appCalendar_setOffsetY(Yoffset)
	{
		this.offsetY = Yoffset;
	}
	
/**
* Sets the images path
*
* @access public
* @param string path Path to use for images
*/
	function appCalendar_setImagesPath(path)
	{
		this.imagesPath = path;
	}

/**
* Turns on/off the month dropdown
*
* @access public
* @param boolean useMonthCombo Whether to use month dropdown or not
*/
	function appCalendar_setMonthCombo(useMonthCombo)
	{
		this.useMonthCombo = useMonthCombo;
	}

/**
* Turns on/off the year dropdown
*
* @access public
* @param boolean useYearCombo Whether to use year dropdown or not
*/
	function appCalendar_setYearCombo(useYearCombo)
	{
		this.useYearCombo = useYearCombo;
	}

/**
* Sets the current month being displayed
*
* @access public
* @param boolean month The month to set the current month to
*/
	function appCalendar_setCurrentMonth(month)
	{
		this.currentMonth = month;
	}

/**
* Sets the current month being displayed
*
* @access public
* @param boolean year The year to set the current year to
*/
	function appCalendar_setCurrentYear(year)
	{
		this.currentYear = year;
	}

/**
* Sets the range of the year combo. Displays this number of
* years either side of the year being displayed.
*
* @access public
* @param integer range The range to set
*/
	function appCalendar_setYearComboRange(range)
	{
		this.yearComboRange = range;
	}

/**
* Returns the layer object
*
* @access private
*/
	function appCalendar_getLayer()
	{
        return document.getElementById('app_calendar_dialog');
	}

/**
* Hides the calendar layer
*
* @access private
*/
	function appCalendar_hideLayer()
	{
		document.getElementById('app_calendar_dialog').style.visibility = 'hidden';
	}

/**
* Shows the calendar layer
*
* @access private
*/
	function appCalendar_showLayer()
	{
		this._getLayer().style.visibility = 'visible';
	}

/**
* Sets the layers position
*
* @access private
*/
	function appCalendar_setLayerPosition()
	{
		this._getLayer().style.top  = findPosY(this.field) + 20; // (appCalendar_mouseY + this.offsetY) + 'px';
		this._getLayer().style.left = findPosX(this.field); //(appCalendar_mouseX + this.offsetX) + 'px';
	}
    
    

/**
* Sets the innerHTML attribute of the layer
*
* @access private
*/
	function appCalendar_setHTML(html)
	{
		this._getLayer().innerHTML = html;
	}

/**
* Returns number of days in the supplied month
*
* @access private
* @param integer month The month to get number of days in
* @param integer year  The year of the month in question
*/
	function appCalendar_getDaysInMonth(month, year)
	{
		monthdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
		if (month != 1) {
			return monthdays[month];
		} else {
			return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? 29 : 28);
		}
	}

/**
* onMouse(Over|Out) event handler
*
* @access private
* @param boolean status Whether the mouse is over the
*                       calendar or not
*/
	function appCalendar_mouseover(status)
	{
		appCalendar_mouseoverStatus = status;
		return true;
	}

/**
* onMouseMove event handler
*/
	appCalendar_oldOnmousemove = document.onmousemove ? document.onmousemove : new Function;

	document.onmousemove = function ()
	{
        if (arguments[0]) {
            appCalendar_mouseX = arguments[0].pageX;
            appCalendar_mouseY = arguments[0].pageY;
        } else {
            appCalendar_mouseX = event.clientX + document.body.scrollLeft;
            appCalendar_mouseY = event.clientY + document.body.scrollTop;
            arguments[0] = null;
        }
        appCalendar_oldOnmousemove();
	}

/**
* Callbacks for document.onclick
*/
	appCalendar_oldOnclick = document.onclick ? document.onclick : new Function;

	document.onclick = function ()
	{
        if(!appCalendar_mouseoverStatus)
        {
            for(i=0; i<appCalendar_layers.length; ++i){
            	appCalendar_layers[i]._hideLayer();
            }
        }
        appCalendar_oldOnclick(arguments[0] ? arguments[0] : null);
	}
    
/**
* Format the result back to the field
* The variable user_date_format should be set using and 
*  combination of the following (any case): D, DD, M, MM, YY, YYYY
*/
    var month_array = ["","Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    function appCalendar_getMonthNumber(mon)
    {
        var mon_str = mon.toString(); 
        for(i = 1; i <= 12; i++)
        {
            if(month_array[i].toUpperCase() == mon_str.toUpperCase() || parseInt(mon) == i) return i;
        }
        return null;
    }
    function appCalendar_formatResult(D,M,YYYY)
    {
        DD = '0'+D; DD = DD.length == 2 ? DD : D;
        MM = '0'+M; MM = MM.length == 2 ? MM : M;
        MMM = month_array[parseInt(M)]
        YY = '0'+YYYY; YY = YY.length == 2 ? YY : YYYY;
        re = /DD/i
        date = user_date_format.replace(re,DD);
        re = /D/i
        date = date.replace(re,D);
        re = /MMM/i
        date = date.replace(re,MMM);
        re = /MM/i
        date = date.replace(re,MM);
        re = /M/i
        date = date.replace(re,M);
        re = /YYYY/i
        date = date.replace(re,YYYY);
        re = /YY/i
        date = date.replace(re,YY);
        return date;
    }
    
    function appCalendar_getDateFromField()
    {
        val = this.field.value;
        if(val.length < 4) return new Array(this.currentDay,this.currentMonth,this.currentYear);
        re = /\W/;
        vals = val.split(re);
        parts = user_date_format.toLowerCase().split(re);
        switch(parts[0].substr(0,1))
        {
            case 'm': m = this._getMonthNumber(vals[0])-1; break;
            case 'd': d = vals[0]; break;
            case 'y': y = vals[0]; break;
        }
        switch(parts[1].substr(0,1))
        {
            case 'm': m = this._getMonthNumber(vals[1])-1; break;
            case 'd': d = vals[1]; break;
            case 'y': y = vals[1]; break;
        }
        switch(parts[2].substr(0,1))
        {
            case 'm': m = this._getMonthNumber(vals[2])-1; break;
            case 'd': d = vals[2]; break;
            case 'y': y = vals[2]; break;
        }
        return new Array(d,m,y);
    }
    