//epb date selector control.
function epb_dateselector(str_name, str_id, object_name)
{
   this.selected_date = new Date();
   this.active_day_class = 'epb_active_day_class';
   this.day_class = 'epb_day_class';
   this.dayrow_class = 'epb_dayrow';
   this.table_class = 'epb_cal_table';
   this.table_border = '0';
   this.cal_heading_class = 'epb_cal_head';
   this.cellpadding = 0;
   this.cellspacing = 0;
   
   this.next_month_class = 'epb_next_month_class';
   this.last_month_class = 'epb_last_month_class';
   
   this.next_month_text = '&gt;&gt;';
   this.last_month_text = '&lt;&lt;';
   
   this.q_month_class = 'epb_q_month_class';
   this.q_year_class = 'epb_q_year_class';
   this.str_name = str_name;
   this.str_id = str_id;
   this.object_name = object_name;
   this.day_mouse_over_class = 'epb_mouse_over';
   this.table_container_class = '';
   this.min_year = this.selected_date.getFullYear() - 5;
   this.max_year = this.selected_date.getFullYear() + 5;
   this.overall_class = '';
   this.view_year = this.selected_date.getFullYear();
   this.view_month =this.selected_date.getMonth();
   
   //textual versions of things are retrieved here.
   this.__get_month_name = function __get_month_name(monthnum)
   {
			try
			{
				var monthnames = MonthNames;
			}
			catch(e)
			{
				var monthnames = new Array('January', 'February', 'March', 
						 'April', 'May', 'June', 'July', 'August', 'September',
						 'October', 'November', 'December');
			}
	   return monthnames[monthnum % 12];
	   
   }
   
   this.__get_day_shortname = function __get_day_shortname(daynum)
   {
	  var daynames = new Array('S', 'M', 'T', 'W', 'T', 'F', 'S');	  
	  return daynames[daynum % 7];
   }
   
   this.set_active_date = function saproto(date, month, year)
   {
	   var tempnewactive, tempoldactive;
	   
	/*   if(this.selected_date.getMonth() == month && this.selected_date.getFullYear() == year)
	   {
		   tempoldactive = document.getElementById(this.str_id + '__date'+this.selected_date.getDate());
		   tempoldactive.className = this.day_class;		   
		   
		   tempnewactive = document.getElementById(this.str_id + '__date'+date);
		   tempnewactive.className = this.active_day_class;
		   tempoldactive.onMouseOver = new Function('this.className=\''+this.day_mouse_over_class+'\';');
		   tempoldactive.onMouseOut = new Function('this.className=\''+this.day_class+'\';');
		   //onmouseover="this.className=\''+this.day_mouse_over_class+'\'" onmouseout="this.className=\''+this.day_class+'\'"
		   //tempnewactive
		   
		   this.selected_date.setMonth(month);
		   this.selected_date.setFullYear(year);	
		   this.selected_date.setDate(date);	   
	   }
	   else
	   {*/
		   this.selected_date.setMonth(month);
		   this.selected_date.setFullYear(year);
		   this.selected_date.setDate(date);
		   tempnewactive = document.getElementById(this.str_id + '__tablecontainer');
			 document.getElementById(this.str_id+'__yearpick').selectedIndex = this.selected_date.getFullYear() - document.getElementById(this.str_id+'__yearpick').options[0].value;
		   this.view_year = this.selected_date.getFullYear();
			 document.getElementById(this.str_id+'__monthpick').selectedIndex = this.selected_date.getMonth();
       this.view_month = this.selected_date.getMonth();
		   tempnewactive.innerHTML = this.__build_month(this.view_month, this.view_year);
	   //}
       //Here we set the value of our hidden element so as to be able to pass this on to
       //the next page in a form submission.
       document.getElementById(this.str_id+'__value').value = this.formatted_date();
   }
   
   //Respond to changes in the quick select menus or clicks on the next and last month
   //links.
   this.quick_select_respond = function quick_select_respond(command, number)
   {
	   var tableitem;
	   var newmonth, newyear;
	   if(command == 'change_month')
	   {
		   this.view_month = number;
		   tableitem = document.getElementById(this.str_id + '__tablecontainer');
		   tableitem.innerHTML = this.__build_month(this.view_month, this.view_year);
	   }
	   if(command == 'change_year')
	   {
		   this.view_year = number;
		   tableitem = document.getElementById(this.str_id + '__tablecontainer');
		   tableitem.innerHTML = this.__build_month(this.view_month, this.view_year);
		   
	   }
	   if(command == 'next_month')
	   {
		   newmonth = this.view_month + 1;
		   if(newmonth == 12)
		   {
			   newmonth = 0;
			   this.view_year = this.view_year + 1;
			   
		   }
		   this.view_month = newmonth;
		   tableitem = document.getElementById(this.str_id + '__tablecontainer');
		   tableitem.innerHTML = this.__build_month(this.view_month, this.view_year);		   
	   }
	   if(command == 'last_month')
	   {
		   newmonth = this.view_month - 1;
		   if(newmonth == -1)
		   {
			   newmonth = 11;
			   this.view_year = this.view_year - 1;
		   }
		   this.view_month = newmonth;
		   tableitem = document.getElementById(this.str_id + '__tablecontainer');
		   tableitem.innerHTML = this.__build_month(this.view_month, this.view_year);	
	   }
   }
   
   this.bind_to_existing_by_id = function replace_existing_item(id)
   {
	   var to_replace = document.getElementById(id);
	   var newthingy;
	   if(to_replace != null)
	   {		
		   if(document.createElement)
		   {   
			   newthingy = document.createElement('div');
			   newthingy.innerHTML = this.generate_control();
			   to_replace.parentNode.replaceChild(newthingy, to_replace);
			   newthingy.id = this.str_id;
           }
	   }
   }
   
   //Returns HTML/JS for this control.
   this.generate_control = function gencntl(date, month, fullyear)
   {
	   var i, isselected;
	   var output;
	   this.selected_date.setDate(date != undefined ? date: this.selected_date.getDate());
	   this.selected_date.setMonth(month != undefined ? month: this.selected_date.getMonth());
	   this.selected_date.setFullYear(fullyear != undefined ? fullyear: this.selected_date.getFullYear());
	   
	   output = '<div id="'+this.str_id+'_overallcontainer" class="'+this.overall_class+'">';
	   
	   
	   //Add hidden value hold here.
	   output += '<input id="'+this.str_id+'__value" type="hidden" value="'+this.formatted_date()+'" name="'+this.str_name+'" />';
	   //Month quick selection.
	   output += '<select class="'+this.q_month_class+'" onchange="'+this.object_name+'.quick_select_respond(\'change_month\', this.value);"  id="'+this.str_id+'__monthpick">';	   
	   /*output += '<option value="'+this.selected_date.getMonth()+'">'+
	   this.__get_month_name(this.selected_date.getMonth())+'</option>';*/
	   for(i = 0; i < 12; i++)
	   {  
		 		optionSelected = (i == this.selected_date.getMonth()) ? "SELECTED" : "";
	      output += '<option value="'+i+'" '+optionSelected+'>'+this.__get_month_name(i)+'</option>';
	   }
	   output += '</select>';	 
	   
	   //Year quick selection.
	   output += '<select class="'+this.q_year_class+'"  onchange="'+this.object_name+'.quick_select_respond(\'change_year\', this.value);"  id="'+this.str_id+'__yearpick">';	   
	   /*output += '<option value="'+this.selected_date.getFullYear()+'">'+	   
	   this.selected_date.getFullYear()+'</option>';*/
	   for(i = this.min_year; i <= this.max_year; i++)
	   {  
	      optionSelected = (i == this.selected_date.getFullYear()) ? "SELECTED" : "";
				output += '<option value="'+i+'" '+optionSelected+'>'+i+'</option>';
	   }
	   output += '</select>';
	   output += '<a class="'+this.next_month_class+'" id="'+this.str_id+'__nextmonth" href="javascript: '+this.object_name+'.quick_select_respond(\'last_month\')">'+this.last_month_text+'</a>';
	   output += '<a class="'+this.last_month_class+'"  id="'+this.str_id+'__lastmonth" href="javascript: '+this.object_name+'.quick_select_respond(\'next_month\')">'+this.next_month_text+'</a>';
	   
output += '<div id="'+this.str_id+'__tablecontainer" class="'+this.table_container_class+'">'
	   +this.__build_month(this.selected_date.getMonth(), this.selected_date.getFullYear()) + '</div>';	   
	   
	   output += '</div>';
	   return output;
   }
   
   this.formatted_date = function formatted_date()
   {
	   var out = this.selected_date.getFullYear() + '-';
	   
	   out += (this.selected_date.getMonth()+1)>9 ? (this.selected_date.getMonth()+1) : ('0'+(this.selected_date.getMonth()+1));
	   out += '-';
	   out += this.selected_date.getDate() > 9 ? this.selected_date.getDate() : '0' + this.selected_date.getDate();
	   return out;
   }
   
   // Constructs the HTML-visible part of the control.
   // month is the integer value for this month.
   // year is the integer value for this 
   this.__build_month = function __build_month(month, year)
   {
	   var daythingy = new Date();
	   var i;
	   daythingy.setMonth(month);
	   daythingy.setFullYear(year);
	   daythingy.setDate(1);
	   var tablehtml = '<table  id="'+this.str_id+'__table"  border="'+this.table_border+'" cellpadding="'+this.cellpadding+'" cellspacing="'+this.cellspacing+'" class="'+this.table_class+'"><tr ><td colspan="7" class="'+this.cal_heading_class+'">' + this.__get_month_name(month) + '&nbsp;&nbsp;' + year + '</td></tr>';
	   tablehtml += '<tr class="'+this.dayrow_class+'">';
	   for(i = 0 ; i < 7; i++)
	   {
		   tablehtml += '<td>'+ this.__get_day_shortname(i) + '</td>';		            
	   }
	   tablehtml += '</tr><tr>';
	   
	   //Now that the heading and such was created, now let us build the dates.
	   //If one of the dates matches this.selected_date, then off we go!
	   
	   
	   //First we need to make the empty spaces. 
	   for(i = 0; i < daythingy.getDay(); i++)
	   {
		   tablehtml += '<td>&nbsp;</td> '; 		  
	   }
	   
       var cx = daythingy.getDay();  //cx hold the current position for building the table.
	   var rowopen = true;
	   var continue_along = true;
	   while(continue_along)
	   {
		   if(rowopen == false)
		   {tablehtml += '<tr>'; rowopen = true;}
			   
		   
		   if(daythingy.getDate() == this.selected_date.getDate() &&
		   daythingy.getMonth() == this.selected_date.getMonth() &&
		   daythingy.getFullYear() == this.selected_date.getFullYear())
		   {
			   //This is the active date.
			   tablehtml += '<td id="'+this.str_id+'__date'+daythingy.getDate()+'" class="'+this.active_day_class+'">'+daythingy.getDate()+'</td>';
		   }
		   else
		   {
			   tablehtml += '<td onmouseover="this.className=\''+this.day_mouse_over_class+'\'" onmouseout="this.className=\''+this.day_class+'\'" id="'+this.str_id+'__date'+daythingy.getDate()+'"  onclick="'+this.object_name+'.set_active_date('+
			   daythingy.getDate()+','+month+','+year
			   +');" class="'+this.day_class+'"><a href="javascript:;">'+daythingy.getDate()+'</a></td>';
		   }
		   cx++;
		   if((cx % 7) == 0)
		   {
			  tablehtml += '</tr>';
			  rowopen = false;
		   }
		   daythingy.setDate(daythingy.getDate()+1);
		   if(daythingy.getDate() == 1)
		   {
			   //We've gotten to next month, let's stop.
			   continue_along = false;
		   }
	   }
	   
	   //now if we didn't get to the end of the calendar row, let's fill it in with empty spaces.
	   if((cx % 7) != 0)
	   {
		   while((cx % 7) != 0)
		   {
			  tablehtml += '<td>&nbsp</td>'; 
			  cx++;
		   }
		   tablehtml += '</tr>';
       }
	   
	   tablehtml += '</table>';
	   
	   return tablehtml;
   }
}