//
// Calendar Object
//
// Copyright (c) Oklahoma Climate Survey
//
// Author: Himanshu Shrivastava
//
// Description:
//
//	Ver 2.0
// - modified it such that you can now control the appearance
//   by using styles
//			#<OBJNAME>_table ---------------------------- table id 
//			#<OBJNAME>_header_row ----------------------- header row
//			.<OBJNAME>_header_column -------------------- header columns 
//			#<OBJNAME>_close_box_div -------------------- close box div id on the header row
//			#<OBJNAME>_options_row ---------------------- options row
//			.<OBJNAME>_options_column ------------------- options row columns
//			#<OBJNAME>_options_prev_month_div ----------- previous month option div id on options row
//			#<OBJNAME>_options_current_month_div -------- current month option div id on options row
//			#<OBJNAME>_options_next_month_div ----------- next month option div id on options row
//			#<OBJNAME>_day_header_row ------------------- days header row ( SUN MON ... )
//			.<OBJNAME>_day_header_column_1 -------------- days header row column class for first, third, fifth and seventh columns
//			.<OBJNAME>_day_header_column_2 -------------- days header row column class for second, forth, and sixth columns
//			.<OBJNAME>_day_row -------------------------- days row
//			.<OBJNAME>_day_column_1 --------------------- days row first, third, fifth and seventh columns
//			.<OBJNAME>_day_column_2 --------------------- days row second, forth, and sixth columns
//			.<OBJNAME>_date_cell_div -------------------- days div class
//			.<OBJNAME>_date_selected_cell_div ----------- selected day div class
//			#<OBJNAME>_time_options_row ----------------- time row id
// 
// Ver 1.0
//	- Builds a month page for the calendar
//	- options to increase/decrease month
//	- can increase/decrease year/date but options not given on the calendar 
//	- Date Formats identifiers
//			d - date 
//			m - month digit
//			b - month "JAN" Format
//			M - month "JANUARY" Format
//			Y - 4 digit year
//			y - 2 digit year
//			h - hour
//			i - min
//			s - sec

function Calendar(inCallingObjectName, inLyrnm, inDtHolder)
{
	this.mCallingObjectName = inCallingObjectName;
	this.mLyrnm = inLyrnm;
	this.mDtHolder = inDtHolder;

	this.mPopUpMonth=1;
	this.setPopUpMonth = function (inVal)	{	this.mPopUpMonth = inVal;	}

	this.mShowTimeOptions = 1;
	this.setShowTimeOptions = function(inVal)	{ this.mShowTimeOptions = inVal;	}

	this.mFullMonthNames = new Array('JANUARY','FEBRUARY','MARCH','APRIL','MAY','JUNE','JULY','AUGUST','SEPTEMBER','OCTOBER','NOVEMBER','DECEMBER');
	this.mMonthAcronym = new Array('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC');
	this.mWeekDaysAcronym = new Array('SUN','MON','TUE','WED','THU','FRI','SAT');
	this.mWeekDaysFullName = new Array('SUNDAY','MONDAY','TUESDAY','WEDNESDAY','THURSDAY','FRIDAY','SATURDAY');
	
	this.getMonthIndex		= function (inMnth)
	{
		var i=0;
		if(inMnth.length > 3)
			while( i< this.mFullMonthNames.length)	{	if(inMnth.toUpperCase()==this.mMonthAcronym[i])	{	return i;	}	i++;	}
		else
			while( i< this.mMonthAcronym.length)	{	if(inMnth.toUpperCase()==this.mMonthAcronym[i])	{	return i;	}	i++;	}
		return -1;
	}

	this.mDtObj = new Date();

	this.returnDateFormat = 'd-b-Y h:i:s';	

	this.mYear = this.mDtObj.getFullYear();
	this.mMonth = this.mDtObj.getMonth();
	this.mDate = this.mDtObj.getDate();
	this.mHour = this.mDtObj.getHours();
	this.mMinute = this.mDtObj.getMinutes();
	this.mSeconds = this.mDtObj.getSeconds();
	this.mSeconds = 0;

	this.mSelectedYear = this.mYear;
	this.mSelectedMonth = this.mMonth;
	this.mSelectedDate = this.mDate;
	this.mSelectedHour = this.mHour;
	this.mSelectedMinute = this.mMinute;
	this.mSelectedSeconds = this.mSeconds;
	this.mSelectedSeconds = 0;

	this.setReturnDateFormat = function(format)
	{
		this.returnDateFormat = format;
	}

	this.setWholeDate = function(inYear, inMonth, inDate)
	{
		this.mYear=inYear*1;	this.mMonth=inMonth*1;	this.mDate=inDate*1;
		this.mDtObj.setFullYear(this.mYear, this.mMonth, this.mDate);
	}

	this.setWholeSelectedDate = function(inYear, inMonth, inDate)
	{
		this.mSelectedYear=inYear*1;	this.mSelectedMonth=inMonth*1;	this.mSelectedDate=inDate*1;
	}

	this.setWholeSelectedTime = function(inHr, inMin, inSec)
	{
		this.mSelectedHour=inHr*1;	this.mSelectedMinute=inMin*1;	this.mSelectedSeconds=inSec*1;
	}

	this.setTime = function(inHour, inMin, inSec)
	{
		inSec=0;
		this.mHour = inHour;	this.mMinute = inMin;	this.mSeconds = inSec;
		this.mDtObj.setHours(this.mHour, this.mMinute, this.mSeconds);
	}
	this.appendZero			= function (inVal) { return (inVal<10 ? "0" + inVal : inVal);	}

	this.getSelectedDate 				= function ()	{	return this.appendZero(this.mSelectedDate);	}
	this.getSelectedMonth 				= function ()	{	return this.appendZero(this.mSelectedMonth);	}
	this.getSelectedFullMonth 			= function ()	{	return this.mFullMonthNames[this.mSelectedMonth];	}
	this.getSelectedMonthAcronym 		= function ()	{	return this.mMonthAcronym[this.mSelectedMonth];	}
	this.getSelectedYear 				= function ()	{	return this.mSelectedYear;	}
	this.getSelectedHour 				= function ()	{	return this.appendZero(this.mSelectedHour);	}
	this.getSelectedMin 					= function ()	{	return this.appendZero(this.mSelectedMinute);	}
	this.getSelectedSec 					= function ()	{	return this.appendZero(this.mSelectedSeconds);	}

	this.getDate 				= function ()	{	return this.appendZero(this.mDate);	}
	this.getMonth 				= function ()	{	return this.appendZero(this.mMonth);	}
	this.getFullMonth 		= function ()	{	return this.mFullMonthNames[this.mMonth];	}
	this.getMonthAcronym 	= function ()	{	return this.mMonthAcronym[this.mMonth];	}
	this.getYear 				= function ()	{	return this.mYear;	}
	this.getNoDaysInMonth	= function ()
	{
		no_days=30;	
		if(this.mMonth==0 || this.mMonth==2 || this.mMonth==4 || this.mMonth==6 || this.mMonth==7 || this.mMonth==9 || this.mMonth==11) {	no_days=31;	}
		if(this.mMonth==1) { if(this.mYear%4==0)  no_days=29;  else  no_days=28; }
		return no_days;
	}
	this.getHour 				= function ()	{	return this.appendZero(this.mHour);	}
	this.getMin 				= function ()	{	return this.appendZero(this.mMinute);	}
	this.getSec 				= function ()	{	return this.appendZero(this.mSeconds);	}
	this.getDay					= function ()	{	return this.mDtObj.getDay();	} 
	this.getDayAcronym		= function ()	{	return this.mWeekDaysAcronym[this.mDtObj.getDay()];	} 
	this.getDayFullName		= function ()	{	return this.mWeekDaysFullName[this.mDtObj.getDay()];	} 

	this.getDayOnFirst		= function ()
	{
		var tmpObj = new Date();
		tmpObj.setFullYear(this.mYear, this.mMonth, 1);
		return tmpObj.getDay();
	}
	
	this.increaseDate		= function() { if(this.mDate == this.getNoDaysInMonth()) { this.mDate=1; this.increaseMonth(); } else { this.mDate++; }	this.setWholeDate(this.mYear, this.mMonth, this.mDate);	}
	this.decreaseDate		= function() { if(this.mDate ==1) { this.decreaseMonth(); this.mDate=this.getNoDaysInMonth(); } else { this.mDate--; }	this.setWholeDate(this.mYear, this.mMonth, this.mDate);	}
	
	this.increaseMonth	= function() { if(this.mMonth==11) { this.mMonth=0; this.increaseYear();	} else { this.mMonth++; }	this.setWholeDate(this.mYear, this.mMonth, this.mDate);	}
	this.decreaseMonth	= function() { if(this.mMonth==0) { this.mMonth=11; this.decreaseYear();	} else { this.mMonth--; }	this.setWholeDate(this.mYear, this.mMonth, this.mDate);	}
	
	this.increaseYear		= function() { this.mYear++;	this.setWholeDate(this.mYear, this.mMonth, this.mDate);	}
	this.decreaseYear		= function() { this.mYear--;	this.setWholeDate(this.mYear, this.mMonth, this.mDate);	}


	this.cleanLayer 		= function() 
	{
		var lyr = document.getElementById(this.mLyrnm);
		while(lyr.firstChild) 
		{ 
			lyr.removeChild(lyr.firstChild); 
		}
		lyr.style.display='none';
	}

	this.closeMonth = function()
	{
		var lyr = document.getElementById(this.mLyrnm);
		lyr.style.width = '0px';	lyr.style.height='0px';

		this.cleanLayer();
	}

		alertDisplayIn = function(val)	{	document.getElementById('calhtml').value += val + "\n";	}

	this.mSelectionFunction = '';
	this.setSelectionFunction = function (inFunc )	{	this.mSelectionFunction = inFunc;	}

	this.buildMonth	= function()
	{
		var inDtHolder = this.mDtHolder;
		var inLyrnm = this.mLyrnm;
		var objname = this.mCallingObjectName;

		/*alertDisplayIn("CallingObjectName: " + this.mCallingObjectName + "-----" + objname);*/
		var lyr = document.getElementById(inLyrnm);

		this.cleanLayer(inLyrnm);
		lyr.style.display='block';
		
		tabl = document.createElement('table');
			tabl.id = objname + "_table";
			tabl.cellPadding = '1px';
			tabl.cellSpacing = '0px';
		
		tbl = document.createElement('tbody');	
		
			trheader = document.createElement('tr');
			trheader.id = objname + '_header_row';
				td = document.createElement('td');
					td.className = objname + "_header_column";
					td.appendChild(document.createTextNode(' '));
						trheader.appendChild(td);

				td = document.createElement('td');
					td.colSpan=5;
					td.align='center';
					td.className = objname + "_header_column";
					td.appendChild(document.createTextNode(this.getFullMonth() +"-" +this.getYear()));
						trheader.appendChild(td);

				td = document.createElement('td');
				td.align='center';
				td.className = objname + "_header_column";	
					if(this.mPopUpMonth==1)
					{
						dv = document.createElement('div');
						dv.style.cursor='pointer';	dv.style.textAlign='center';
						dv.onclick = function ()	{	eval(objname+".closeMonth()");	}
						dv.appendChild(document.createTextNode('X'));
						dv.id = objname + "_close_box_div"
							td.appendChild(dv);
					}
					else
					{
						td.appendChild(document.createTextNode(' '));
					}
							trheader.appendChild(td);

			tbl.appendChild(trheader);
		
			troptions = document.createElement('tr');
			troptions.id = objname + '_options_row'
				td = document.createElement('td');	
				td.className = objname + '_options_column';
					dv = document.createElement('div');
					dv.id = objname + "_options_prev_month_div";
					dv.onclick = function () { eval(objname+".decreaseMonth()"); eval(objname+".buildMonth()");	};
					dv.appendChild(document.createTextNode('<'));
						td.appendChild(dv);
							troptions.appendChild(td);
					
				td = document.createElement('td');
				td.className = objname + '_options_column';
				td.colSpan=2;
					td.appendChild(document.createTextNode(' '));
						troptions.appendChild(td);

				td = document.createElement('td');
				td.className = objname + '_options_column';
					dv = document.createElement('div');
					dv.id = objname + "_options_current_month_div";
					dv.onclick = function() 
					{ 
						eval(objname+".mMonth = parseInt("+objname+".getSelectedMonth())");
						eval(objname+".mYear = parseInt("+objname+".getSelectedYear())");
						eval(objname+".buildMonth()");  
					}
					dv.appendChild(document.createTextNode('^'));
						td.appendChild(dv);
							troptions.appendChild(td);
				
				
				td = document.createElement('td');
				td.className = objname + '_options_column';
			  	//td.style.borderBottom = 'solid 1px #000000';
				td.colSpan=2;
					td.appendChild(document.createTextNode(' '));
						troptions.appendChild(td);
				
				td = document.createElement('td');
				td.className = objname + '_options_column';
					dv = document.createElement('div');
					dv.id = objname + "_options_next_month_div";
					dv.onclick = function() { eval(objname+".increaseMonth()"); eval(objname+".buildMonth()"); /*alertDisplayIn("IncreaseMonth: "+objname + "--" + inLyrnm);*/ 	}
					dv.appendChild(document.createTextNode('>'));
						td.appendChild(dv);
							troptions.appendChild(td);
							
			tbl.appendChild(troptions);
		
			trdayheader = document.createElement('tr');
			trdayheader.id = objname + "_day_header_row";
				for(i=0; i<7; i++)
				{
					td = document.createElement('td');
					td.className = objname + "_day_header_column_1";
					if(i%2==0)	{	td.className = objname + "_day_header_column_2";	}
					
					td.appendChild(document.createTextNode(this.mWeekDaysAcronym[i]));
						trdayheader.appendChild(td);
				}
					tbl.appendChild(trdayheader);
					
		// no_rows=3;
		
		var dt = 1;
		while(dt <= this.getNoDaysInMonth())
		{
			tr = document.createElement('tr');
			tr.className = objname + "_day_row";
			day=0;
			while(day<7)
			{
				cond=1;
				td = document.createElement('td');

				td.className = objname + "_day_column_1";
				if(day%2==0)	{	td.className = objname + "_day_column_2";	}

				if(dt==1)
				{
					if(this.getDayOnFirst() == day)	{	cond=1;	}
					else	{	cond=0;	}
				}
				if(dt > this.getNoDaysInMonth())	{	cond=0;	}
				if(cond==1)
				{
					txt = document.createTextNode(dt);

						dv = document.createElement('div');
						dv.id = objname + '_cell_' + dt;
						
						dv.className = objname + "_date_cell_div";
						if(this.mSelectedDate == dt && this.mYear==this.mSelectedYear && this.mMonth==this.mSelectedMonth)
							dv.className = objname + "_date_selected_cell_div";
						
						dv.onclick = function() 
						{
							eval(objname+".setSelection('"+parseInt(this.firstChild.nodeValue)+"')"); 	
							if(objname+".mSelectionFunction"!="")	{	eval(eval(objname+".mSelectionFunction")+";");	}
						}

						dv.appendChild(txt);
					
					td.appendChild(dv);
					dt++;
				}
				else
				{
					td.appendChild(document.createTextNode(' '));
				}
				tr.appendChild(td);
				day++;
			}
			tbl.appendChild(tr);
			// no_rows++;
		}

		if(this.mShowTimeOptions == 1)
		{
			tr = document.createElement('tr');
			tr.id = objname + "_time_options_row"
				td = document.createElement('td');
				td.colSpan=7;
					dv = document.createElement('div');
					dv.setAttribute('id', 'selected_values');
						hhsel = document.createElement('select');
						hhsel.setAttribute('name', 'hhsel');
						hhsel.id = objname + '_hhsel';
						hhsel.onchange = function() { eval(objname+'.mHour = '+(this.value*1));	eval(objname+'.mSelectedHour = '+(this.value*1)); eval(objname+'.returnDateTime()'); }
							for(i=0; i <24; i++)
							{
								hhsel.options[i] = new Option(this.appendZero(i),this.appendZero(i));
								if(parseInt(this.mSelectedHour) == i) {	hhsel.selectedIndex=i;	}
							}
						dv.appendChild(hhsel);			
							mmsel = document.createElement('select');
							mmsel.setAttribute('name', 'mmsel');
							mmsel.id = objname + '_mmsel';
							mmsel.onchange = function() { eval(objname+'.mMinute = '+(this.value*1));	eval(objname+'.mSelectedMinute = '+(this.value*1)); eval(objname+'.returnDateTime()'); }
								for(i=0; i <60; i++)
								{
									mmsel.options[i] = new Option(this.appendZero(i),this.appendZero(i));
									if(parseInt(this.mSelectedMinute) == i) {	mmsel.selectedIndex=i;	}
								}
						dv.appendChild(mmsel);
					td.appendChild(dv);
				tr.appendChild(td);
			tbl.appendChild(tr);
		}
		else
		{
			/*
			tr = document.createElement('tr');
				td = document.createElement('td');
				td.colSpan=7;
				td.style.borderTop = 'solid 1px #000000';	
					td.appendChild(document.createTextNode(' '));
				tr.appendChild(td);
			tbl.appendChild(tr);
			*/
		}
		tabl.appendChild(tbl);
		lyr.appendChild(tabl);

		this.putBackGroundLayer();
	}

	this.putBackGroundLayer = function ()
	{
		var lyr = document.getElementById(this.mLyrnm);
		ifrm = document.createElement('iframe');
		ifrm.frameBorder=0;
		ifrm.setAttribute('id',this.mCallingObjectName + '_iframe');
		ifrm.style.width = lyr.offsetWidth;
		ifrm.style.height = lyr.offsetHeight;
		ifrm.style.top = 0;			
		ifrm.style.left = 0;
		ifrm.style.position='absolute';
		lyr.style.xIndex=2;
		ifrm.style.zIndex = lyr.style.zIndex - 1;
		ifrm.style.display='block';
			lyr.appendChild(ifrm);
	}

	this.setSelection = function (inDt)
	{
		var objname = this.mCallingObjectName;

		var nm;
		nm = objname+'_cell_' + this.mSelectedDate;
			if(document.getElementById(nm))	{	document.getElementById(nm).className = objname + "_date_cell_div";	} 
		nm = objname+'_cell_' + parseInt(inDt);	
			if(document.getElementById(nm))	{	document.getElementById(nm).className = objname + "_date_selected_cell_div";	}

		this.mSelectedDate = inDt;
		this.mSelectedMonth = this.mMonth;
		this.mSelectedYear = this.mYear;
		this.mSelectedHour = this.mHour;
		this.mSelectedMinute = this.mMinute;
		this.mSelectedSeconds = this.mSeconds;

		this.setDate(inDt);	
		
		this.returnDateTime();
	}
	
	this.setDate = function ( inDt )		{	this.mDate = inDt;	}
	this.setHour = function ( inHr )		{	this.mHour=inHr;	}
	this.setMinutes = function ( inMm )	{	this.mMinute=inMm;	}
	
	this.getSelectedDateTime = function()
	{
		// var retval = this.getSelectedDate() + "-" + this.getSelectedMonthAcronym() + "-" + this.getSelectedYear() + " " + this.getSelectedHour() + ":" + this.getSelectedMin() + ":" + this.getSelectedSec();
		var retval='';
		if(this.returnDateFormat!='')
		{
			retval='';
			for(i=0; i<this.returnDateFormat.length; i++)
			{
				switch(this.returnDateFormat.substr(i,1))
				{
					case "Y":	retval += this.getSelectedYear();	break;
					case "y":	retval += (this.getSelectedYear()+"").substr(2,2);	break;

					case "m":	retval += this.getSelectedMonth();	break;
					case "b":	retval += this.getSelectedMonthAcronym();	break;
					case "M":	retval += this.getSelectedFullMonth();	break;

					case "d":	retval += this.getSelectedDate();	break;

					case "h":	retval += this.getSelectedHour();	break;
					case "i":	retval += this.getSelectedMin();	break;
					case "s":	retval += this.getSelectedSec();	break;
					default: 	retval += "" + this.returnDateFormat.substr(i,1);	break;
				}
			}
		}
		return(retval);
	}

	this.returnDateTime = function()
	{
		if(this.mDtHolder && document.getElementById(inDtHolder))
		{
			document.getElementById(inDtHolder).value = this.getSelectedDateTime();
		}
	}
}
