// Tigra Calendar v4.0.2 (01/12/2009) American (mm/dd/yyyy)
// http://www.softcomplex.com/products/tigra_calendar/
// Public Domain Software... You're welcome.

// default settings
var A_TCALDEF = {
	'months' : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
	'weekdays' : ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
	'weekstart': 0, // first day of week: 0-Su or 1-Mo
	'centyear'  : 70, // 2 digit years less than 'centyear' are in 20xx, othewise in 19xx.
	'imgpath' : 'img/' // directory with calendar images
}

// date parsing function
function f_tcalParseDate (s_date) {

	var re_date = /^\s*(\d{1,2})\/(\d{1,2})\/(\d{2,4})\s*$/;
	if (!re_date.exec(s_date))
		return alert ("Invalid date: '" + s_date + "'.\nAccepted format is mm/dd/yyyy.")
	var n_day = Number(RegExp.$2),
		n_month = Number(RegExp.$1),
		n_year = Number(RegExp.$3);
	
	if (n_year < 100)
		n_year += (n_year < this.a_tpl.centyear ? 2000 : 1900);
	if (n_month < 1 || n_month > 12)
		return alert ("Invalid month value: '" + n_month + "'.\nAllowed range is 01-12.");
	var d_numdays = new Date(n_year, n_month, 0);
	if (n_day > d_numdays.getDate())
		return alert("Invalid day of month value: '" + n_day + "'.\nAllowed range for selected month is 01 - " + d_numdays.getDate() + ".");

	return new Date (n_year, n_month - 1, n_day);
}

// date generating function
function f_tcalGenerDate (d_date) {
	return (
		 (d_date.getMonth() < 9 ? '0' : '') + (d_date.getMonth() + 1) + "/"
		+ (d_date.getDate() < 10 ? '0' : '') + d_date.getDate() + "/"
		+ d_date.getFullYear()
	);
}

function f_tcalRelDate (d_date, d_diff, s_units) {
	var s_units = (s_units == 'y' ? 'FullYear' : 'Month');
	var d_result = new Date(d_date);
	d_result['set' + s_units](d_date['get' + s_units]() + d_diff);
	if (d_result.getDate() != d_date.getDate())
		d_result.setDate(0);
	return ' onclick="A_TCALS[\'' + this.s_id + '\'].f_update(' + d_result.valueOf() + ')"';
	
}

f_getElement = document.all ?
	function (s_id) { return document.all[s_id] } :
	function (s_id) { return document.getElementById(s_id) };
	
function f_tcalSelDate(n_date)
{
    //return ' onclick="A_TCALS[\'' + this.s_id + '\'].f_onSelectDate(' + n_date + ')"';
    return ' href="javascript:A_TCALS[\'' + this.s_id + '\'].f_onSelectDate(' + n_date + ');"';

}
	
function f_tcalOnSelectDate(n_date) {
	if (n_date)
		this.e_input.value = this.f_generDate(new Date(n_date));
		
	//alert('selDate=' + this.e_input.value);

}

function f_tcalOnSelectMonthYear(selMonth, selYear) {

	//alert('selMonth=' + selMonth);
	//alert('selYear=' + selYear);
		
}

//Like a constructor
function tcalendar(a_cfg, a_tpl){

    this.a_cfg = a_cfg;
    // find input field
	if (!this.a_cfg.controlname)
		throw("TC: control name is not specified");
	if (this.a_cfg.formname) {
		var e_form = document.forms[this.a_cfg.formname];
		if (!e_form)
			throw("TC: form '" + this.a_cfg.formname + "' can not be found");
		this.e_input = e_form.elements[this.a_cfg.controlname];
	}
	else
		this.e_input = f_getElement(this.a_cfg.controlname);

	if (!this.e_input || !this.e_input.tagName || this.e_input.tagName != 'INPUT')
		throw("TC: element '" + this.a_cfg.controlname + "' does not exist in "
			+ (this.a_cfg.formname ? "form '" + this.a_cfg.controlname + "'" : 'this document'));
    
    
    // apply default template if not specified
	if (!a_tpl)
		this.a_tpl = A_TCALDEF;
	else
	    this.a_tpl = a_tpl;
	    
	// register in global collections
	if (!window.A_TCALS)
		window.A_TCALS = [];
		
	this.s_id = a_cfg.id ? a_cfg.id : A_TCALS.length;
	window.A_TCALS[this.s_id] = this;
	
	this.f_update = f_tcalUpdate;
	this.f_parseDate = f_tcalParseDate;
	this.f_generDate = f_tcalGenerDate;
	this.f_relDate = f_tcalRelDate;
	this.f_selDate = f_tcalSelDate;
	this.f_onSelectDate = f_tcalOnSelectDate;
	this.f_onSelectMonthYear = f_tcalOnSelectMonthYear;
	
	this.f_update();
}

function f_tcalUpdate(d_date){

    window.A_TCALS[this.s_id] = this;
    
    var d_client = new Date();
	d_client.setHours(0);
	d_client.setMinutes(0);
	d_client.setSeconds(0);
	d_client.setMilliseconds(0);
	
	var d_today = this.a_cfg.today ? this.f_parseDate(this.a_cfg.today) : d_client;
	
	//var d_selected = this.e_input.value == ''
	//	? (this.a_cfg.selected ? this.f_parseDate(this.a_cfg.selected) : d_today)
	var d_selected = d_today;
	//	: this.f_parseDate(this.e_input.value);
		
	// figure out date to display
	if (!d_date)
		// selected by default
		d_date = d_selected;
	else if (typeof(d_date) == 'number')
		// get from number
		d_date = new Date(d_date);
	else if (typeof(d_date) == 'string')
		// parse from string
		this.f_parseDate(d_date);
		
	if (!d_date) return false;
	
	// first date to display
	var d_firstday = new Date(d_date);
	d_firstday.setDate(1);
	d_firstday.setDate(1 - (7 + d_firstday.getDay() - this.a_tpl.weekstart) % 7);
	
	var a_class, s_html;
	
	//<<    May 2009   >>  table class="ctrl"
	/** if( d_date.getYear() > d_client.getYear())
		s_html = '<table class="calendarTable"><tbody><tr>'
		    + '<td class="monthYearTitleCell" ' + this.f_relDate(d_date, -1) + ' title="Previous Month"><img src="' + this.a_cfg.imgpath + 'prev_mon.gif" /></td>'
			+ '<td class="monthYearTitleCell" align="center" colspan="5">'
			+ this.a_tpl.months[d_date.getMonth()] + ', ' + d_date.getFullYear()
			+ '</td>' 
			+ '<td class="monthYearTitleCell" ' + this.f_relDate(d_date, 1) + ' title="Next Month"><img src="' + this.a_cfg.imgpath + 'next_mon.gif" /></td>'
			+ '</tr>';
	else if ( d_date.getYear() == d_client.getYear() &&
	          d_date.getMonth() > d_client.getMonth()
	         )
	    s_html = '<table class="calendarTable"><tbody><tr>'
		    + '<td class="monthYearTitleCell" ' + this.f_relDate(d_date, -1) + ' title="Previous Month"><img src="' + this.a_cfg.imgpath + 'prev_mon.gif" /></td>'
			+ '<td class="monthYearTitleCell" align="center" colspan="5">'
			+ this.a_tpl.months[d_date.getMonth()] + ', ' + d_date.getFullYear()
			+ '</td>' 
			+ '<td class="monthYearTitleCell" ' + this.f_relDate(d_date, 1) + ' title="Next Month"><img src="' + this.a_cfg.imgpath + 'next_mon.gif" /></td>'
			+ '</tr>'; 
    else
	    s_html = '<table class="calendarTable"><tbody><tr>'
		    + '<td class="monthYearTitleCell">&nbsp;</td>'
			+ '<td class="monthYearTitleCell" align="center" colspan="5">'
			+ this.a_tpl.months[d_date.getMonth()] + ', ' + d_date.getFullYear()
			+ '</td>' 
			+ '<td class="monthYearTitleCell" ' + this.f_relDate(d_date, 1) + ' title="Next Month"><img src="' + this.a_cfg.imgpath + 'next_mon.gif" /></td>'
			+ '</tr>'; **/

      s_html = '<table class="calendarTable"><tbody><tr>'
		    + '<td class="monthYearTitleCell" ' + this.f_relDate(d_date, -1) + ' title="Previous Month"><img src="' + this.a_cfg.imgpath + 'prev_mon.gif" /></td>'
			+ '<td class="monthYearTitleCell" align="center" colspan="5">'
			+ this.a_tpl.months[d_date.getMonth()] + ', ' + d_date.getFullYear()
			+ '</td>' 
			+ '<td class="monthYearTitleCell" ' + this.f_relDate(d_date, 1) + ' title="Next Month"><img src="' + this.a_cfg.imgpath + 'next_mon.gif" /></td>'
			+ '</tr>';

		
	//Rows including weekdays titles Su Mo... plus calendar days 1, 2, 3, ... 30
	//table  without class defined
	s_html += '<tr>';
	
	// Single row print weekdays titles Su Mo Tu We Th Fr Sa 
	for (var i = 0; i < 7; i++)
		s_html += '<td class="weekdayTitleCell"><div align="center">' + this.a_tpl.weekdays[(this.a_tpl.weekstart + i) % 7] + '</div></td>';

	s_html += '</tr>' ;
	
	
	// print calendar table
	var num_rows = 0;
	var d_current = new Date(d_firstday);
	while (d_current.getMonth() == d_date.getMonth() ||
		d_current.getMonth() == d_firstday.getMonth()) {
	
		// print row heder
		s_html +='<tr>';
		for (var n_wday = 0; n_wday < 7; n_wday++) {

			a_class = [];
			// other month
			if (d_current.getMonth() != d_date.getMonth())
				a_class[a_class.length] = 'othermonth';
			// weekend
			if (d_current.getDay() == 0 || d_current.getDay() == 6)
				a_class[a_class.length] = 'weekend';
			// today
			if (d_current.valueOf() == d_today.valueOf())
				a_class[a_class.length] = 'today';
			// selected
			if (d_current.valueOf() == d_selected.valueOf())
				a_class[a_class.length] = 'selected';
				
			if(d_current.valueOf() < d_today.valueOf() && 
			   d_current.getMonth() == d_date.getMonth() 
			   )
			    a_class[a_class.length] = 'curmonthpastday';
			    
		
			var tdclass = a_class.join(' ');
		
			
			if( tdclass.indexOf("othermonth") >= 0)
			    s_html += '<td class="otherMonthDayCell"><span class = "small">&nbsp;</span></td>';
			else if (tdclass.indexOf("curmonthpastday") >= 0)
			    //s_html += '<td class="curMonthPastDayCell">'+ d_current.getDate() + '</td>';
                      s_html += '<td class="curMonthPastDayCell"><a ' + this.f_selDate(d_current.valueOf()) 
                             + ' >' + d_current.getDate() + '</a></td>';

			else
			    s_html += '<td class="curMonthCurFutureDayCell"><a ' + this.f_selDate(d_current.valueOf()) 
                             + ' >' + d_current.getDate() + '</a></td>';

                 
	
			d_current.setDate(d_current.getDate() + 1);
		} // for loop for 0 ,1, 2, ...6
		
		// print row footer
		s_html +='</tr>';
		
		num_rows += 1;
	}
	
	/** if(num_rows < 6){
	    for (var n_wday = 0; n_wday < 7; n_wday++)
	       s_html += '<td class="otherMonthDayCell"><span class = "small">&nbsp;</span></td>'; 
	} **/
	    
	s_html +='</tbody></table>';
	
	this.e_div = f_getElement('tcalendar');
	
	if (!this.e_div) {
	    
		s_html = '<DIV id="tcalendar">' + s_html + '</DIV>';
		document.write(s_html);
	}
	else
	{ 
	    this.e_div.innerHTML = s_html;
    }
	
	return;
	
}

