/*****************************************************************************
Copyright (C) 2006  Nick Baicoianu

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*****************************************************************************/
function Epoch(d,c,a,b){this.state=0;this.name=d;this.curDate=new Date;this.mode=c;this.selectMultiple=b==true;this.selectedDates=[];this.calendar;this.calHeading;this.calCells;this.rows;this.cols;this.cells=[];this.monthSelect;this.yearSelect;this.mousein=false;this.calConfig();this.setDays();this.displayYear=this.displayYearInitial;this.displayMonth=this.displayMonthInitial;this.createCalendar();if(this.mode=="popup"&&a&&a.type=="text"){this.tgt=a;this.calendar.style.position="absolute";this.topOffset=this.tgt.offsetHeight;this.leftOffset=0;this.calendar.style.top=this.getTop(a)+this.topOffset+"px";this.calendar.style.left=this.getLeft(a)+this.leftOffset+"px";document.body.appendChild(this.calendar);this.tgt.calendar=this;this.tgt.onfocus=function(){this.calendar.show()};this.tgt.onblur=function(){!this.calendar.mousein&&this.calendar.hide()}}else{this.container=a;this.container.appendChild(this.calendar)}this.state=2;this.visible?this.show():this.hide()}Epoch.prototype.calConfig=function(){this.displayYearInitial=this.curDate.getFullYear();this.displayMonthInitial=this.curDate.getMonth();this.rangeYearLower=2008;this.rangeYearUpper=2014;this.minDate=new Date(2008,0,1);this.maxDate=new Date(2014,0,1);this.startDay=1;this.showWeeks=false;this.selCurMonthOnly=false;this.clearSelectedOnChange=false;switch(this.mode){case"popup":this.visible=false;break;case"flat":this.visible=true}this.setLang()};Epoch.prototype.setLang=function(){this.daylist=["Su","Mo","Tu","We","Th","Fr","Sa","Su","Mo","Tu","We","Th","Fr","Sa"];this.months_sh=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];this.monthup_title="Go to the next month";this.monthdn_title="Go to the previous month";this.clearbtn_caption="Clear";this.clearbtn_title="Clears any dates selected on the calendar";this.maxrange_caption="This is the maximum range"};Epoch.prototype.getTop=function(c){var a=c,b=0;while(a.tagName!="BODY"){b+=a.offsetTop;a=a.offsetParent}return b};Epoch.prototype.getLeft=function(c){var a=c,b=0;while(a.tagName!="BODY"){b+=a.offsetLeft;a=a.offsetParent}return b};Epoch.prototype.show=function(){this.calendar.style.display="block";this.visible=true};Epoch.prototype.hide=function(){this.calendar.style.display="none";this.visible=false};Epoch.prototype.toggle=function(){if(this.visible)this.hide();else this.show()};Epoch.prototype.setDays=function(){this.daynames=[];for(var b=0,a=this.startDay;a<this.startDay+7;a++)this.daynames[b++]=this.daylist[a];this.monthDayCount=[31,(this.curDate.getFullYear()-2e3)%4?28:29,31,30,31,30,31,31,30,31,30,31]};Epoch.prototype.setClass=function(b,a){b.setAttribute("class",a);b.setAttribute("className",a)};Epoch.prototype.createCalendar=function(){var c,b,a;this.calendar=document.createElement("table");this.calendar.setAttribute("id",this.name+"_calendar");this.setClass(this.calendar,"calendar");this.calendar.onselectstart=function(){return false};this.calendar.ondrag=function(){return false};c=document.createElement("tbody");b=document.createElement("tr");a=document.createElement("td");a.appendChild(this.createMainHeading());b.appendChild(a);c.appendChild(b);b=document.createElement("tr");a=document.createElement("td");a.appendChild(this.createDayHeading());b.appendChild(a);c.appendChild(b);b=document.createElement("tr");a=document.createElement("td");a.setAttribute("id",this.name+"_cell_td");this.calCellContainer=a;a.appendChild(this.createCalCells());b.appendChild(a);c.appendChild(b);b=document.createElement("tr");a=document.createElement("td");a.appendChild(this.createFooter());b.appendChild(a);c.appendChild(b);this.calendar.appendChild(c);this.calendar.owner=this;this.calendar.onmouseover=function(){this.owner.mousein=true};this.calendar.onmouseout=function(){this.owner.mousein=false}};Epoch.prototype.createMainHeading=function(){var c=document.createElement("div");c.setAttribute("id",this.name+"_mainheading");this.setClass(c,"mainheading");this.monthSelect=document.createElement("select");this.yearSelect=document.createElement("select");for(var d=document.createElement("input"),e=document.createElement("input"),b,a=0;a<12;a++){b=document.createElement("option");b.setAttribute("value",a);this.state==0&&this.displayMonth==a&&b.setAttribute("selected","selected");b.appendChild(document.createTextNode(this.months_sh[a]));this.monthSelect.appendChild(b)}for(a=this.rangeYearLower;a<=this.rangeYearUpper;a++){b=document.createElement("option");b.setAttribute("value",a);this.state==0&&this.displayYear==a&&b.setAttribute("selected","selected");b.appendChild(document.createTextNode(a));this.yearSelect.appendChild(b)}e.setAttribute("type","button");e.setAttribute("value",">");e.setAttribute("title",this.monthup_title);d.setAttribute("type","button");d.setAttribute("value","<");d.setAttribute("title",this.monthdn_title);this.monthSelect.owner=this.yearSelect.owner=e.owner=d.owner=this;e.onmouseup=function(){this.owner.nextMonth()};d.onmouseup=function(){this.owner.prevMonth()};this.monthSelect.onchange=function(){this.owner.displayMonth=this.value;this.owner.displayYear=this.owner.yearSelect.value;this.owner.goToMonth(this.owner.displayYear,this.owner.displayMonth)};this.yearSelect.onchange=function(){this.owner.displayMonth=this.owner.monthSelect.value;this.owner.displayYear=this.value;this.owner.goToMonth(this.owner.displayYear,this.owner.displayMonth)};c.appendChild(d);c.appendChild(this.monthSelect);c.appendChild(this.yearSelect);c.appendChild(e);return c};Epoch.prototype.createFooter=function(){return document.createElement("div")};Epoch.prototype.resetSelections=function(a){this.selectedDates=[];this.rows=[false,false,false,false,false,false,false];this.cols=[false,false,false,false,false,false,false];if(this.tgt){this.tgt.value="";this.mode=="popup"&&this.hide()}if(a==true)this.goToMonth(this.displayYearInitial,this.displayMonthInitial);else this.reDraw()};Epoch.prototype.createDayHeading=function(){this.calHeading=document.createElement("table");this.calHeading.setAttribute("id",this.name+"_caldayheading");this.setClass(this.calHeading,"caldayheading");var d,c,a;d=document.createElement("tbody");c=document.createElement("tr");this.cols=[false,false,false,false,false,false,false];if(this.showWeeks){a=document.createElement("td");a.setAttribute("class","wkhead");a.setAttribute("className","wkhead");c.appendChild(a)}for(var b=0;b<7;b++){a=document.createElement("td");a.appendChild(document.createTextNode(this.daynames[b]));if(this.selectMultiple)a.headObj=new CalHeading(this,a,b+this.startDay<7?b+this.startDay:b+this.startDay-7);c.appendChild(a)}d.appendChild(c);this.calHeading.appendChild(d);return this.calHeading};Epoch.prototype.createCalCells=function(){this.rows=[false,false,false,false,false,false];this.cells=[];var e=-1,i=this.showWeeks?48:42,f=new Date(this.displayYear,this.displayMonth,1),j=new Date(this.displayYear,this.displayMonth,this.monthDayCount[this.displayMonth]),b=new Date(f);b.setDate(b.getDate()+(this.startDay-f.getDay())-(this.startDay-f.getDay()>0?7:0));this.calCells=document.createElement("table");this.calCells.setAttribute("id",this.name+"_calcells");this.setClass(this.calCells,"calcells");var g,c,a;g=document.createElement("tbody");for(var d=0;d<i;d++){if(this.showWeeks){if(d%8==0){e++;c=document.createElement("tr");a=document.createElement("td");if(this.selectMultiple)a.weekObj=new WeekHeading(this,a,b.getWeek(),e);else{a.setAttribute("class","wkhead");a.setAttribute("className","wkhead")}a.appendChild(document.createTextNode(b.getWeek()));c.appendChild(a);d++}}else if(d%7==0){e++;c=document.createElement("tr")}a=document.createElement("td");a.appendChild(document.createTextNode(b.getDate()));var h=new CalCell(this,a,b,e);this.cells.push(h);a.cellObj=h;b.setDate(b.getDate()+1);c.appendChild(a);g.appendChild(c)}this.calCells.appendChild(g);this.reDraw();return this.calCells};Epoch.prototype.reDraw=function(){this.state=1;for(var b,a=0;a<this.cells.length;a++)this.cells[a].selected=false;for(a=0;a<this.cells.length;a++){for(b=0;b<this.selectedDates.length;b++)if(this.cells[a].date.getUeDay()==this.selectedDates[b].getUeDay())this.cells[a].selected=true;this.cells[a].setClass()}this.state=2};Epoch.prototype.deleteCells=function(){this.calCellContainer.removeChild(this.calCellContainer.firstChild);this.cells=[]};Epoch.prototype.goToMonth=function(b,a){this.monthSelect.value=this.displayMonth=a;this.yearSelect.value=this.displayYear=b;this.deleteCells();this.calCellContainer.appendChild(this.createCalCells())};Epoch.prototype.nextMonth=function(){if(this.monthSelect.value<11)this.monthSelect.value++;else if(this.yearSelect.value<this.rangeYearUpper){this.monthSelect.value=0;this.yearSelect.value++}else alert(this.maxrange_caption);this.displayMonth=this.monthSelect.value;this.displayYear=this.yearSelect.value;this.deleteCells();this.calCellContainer.appendChild(this.createCalCells())};Epoch.prototype.prevMonth=function(){if(this.monthSelect.value>0)this.monthSelect.value--;else if(this.yearSelect.value>this.rangeYearLower){this.monthSelect.value=11;this.yearSelect.value--}else alert(this.maxrange_caption);this.displayMonth=this.monthSelect.value;this.displayYear=this.yearSelect.value;this.deleteCells();this.calCellContainer.appendChild(this.createCalCells())};Epoch.prototype.addZero=function(a){return(a<10?"0":"")+a};Epoch.prototype.addDates=function(c,e){for(var b,d,a=0;a<c.length;a++){d=false;for(b=0;b<this.selectedDates.length;b++)if(c[a].getUeDay()==this.selectedDates[b].getUeDay()){d=true;break}!d&&this.selectedDates.push(c[a])}e!=false&&this.reDraw()};Epoch.prototype.removeDates=function(c,d){for(var a,b=0;b<c.length;b++)for(a=0;a<this.selectedDates.length;a++)c[b].getUeDay()==this.selectedDates[a].getUeDay()&&this.selectedDates.splice(a,1);d!=false&&this.reDraw()};Epoch.prototype.outputDate=function(a,b){var j=this.addZero(a.getDate()),g=this.addZero(a.getMonth()+1),d=this.addZero(a.getFullYear()),c=this.addZero(a.getFullYear().toString().substring(3,4)),i=b.indexOf("yyyy")>-1?d:c,h=this.addZero(a.getHours()),e=this.addZero(a.getMinutes()),f=this.addZero(a.getSeconds());return b.replace(/dd/g,j).replace(/mm/g,g).replace(/y{1,4}/g,i).replace(/hh/g,h).replace(/nn/g,e).replace(/ss/g,f)};Epoch.prototype.updatePos=function(a){this.calendar.style.top=this.getTop(a)+this.topOffset+"px";this.calendar.style.left=this.getLeft(a)+this.leftOffset+"px"};function CalHeading(b,a,c){this.owner=b;this.tableCell=a;this.dayOfWeek=c;this.tableCell.onclick=this.onclick}CalHeading.prototype.onclick=function(){var a=this.headObj.owner,d=a.selectedDates,c=a.cells;a.cols[this.headObj.dayOfWeek]=!a.cols[this.headObj.dayOfWeek];for(var b=0;b<c.length;b++)if(c[b].dayOfWeek==this.headObj.dayOfWeek&&(!a.selCurMonthOnly||c[b].date.getMonth()==a.displayMonth&&c[b].date.getFullYear()==a.displayYear)){if(a.cols[this.headObj.dayOfWeek])a.selectedDates.arrayIndex(c[b].date)==-1&&d.push(c[b].date);else for(var e=0;e<d.length;e++)if(c[b].dayOfWeek==d[e].getDay()){d.splice(e,1);break}c[b].selected=a.cols[this.headObj.dayOfWeek]}a.reDraw()};function WeekHeading(b,a,c,d){this.owner=b;this.tableCell=a;this.week=c;this.tableRow=d;this.tableCell.setAttribute("class","wkhead");this.tableCell.setAttribute("className","wkhead");this.tableCell.onclick=this.onclick}WeekHeading.prototype.onclick=function(){var a=this.weekObj.owner,c=a.cells,e=a.selectedDates,b,d;a.rows[this.weekObj.tableRow]=!a.rows[this.weekObj.tableRow];for(b=0;b<c.length;b++)if(c[b].tableRow==this.weekObj.tableRow)if(a.rows[this.weekObj.tableRow]&&(!a.selCurMonthOnly||c[b].date.getMonth()==a.displayMonth&&c[b].date.getFullYear()==a.displayYear))a.selectedDates.arrayIndex(c[b].date)==-1&&e.push(c[b].date);else for(d=0;d<e.length;d++)if(e[d].getTime()==c[b].date.getTime()){e.splice(d,1);break}a.reDraw()};function CalCell(c,a,b,d){this.owner=c;this.tableCell=a;this.cellClass;this.selected=false;this.date=new Date(b);this.dayOfWeek=this.date.getDay();this.week=this.date.getWeek();this.tableRow=d;this.tableCell.onclick=this.onclick;this.tableCell.onmouseover=this.onmouseover;this.tableCell.onmouseout=this.onmouseout;this.setClass()}CalCell.prototype.onmouseover=function(){this.setAttribute("class",this.cellClass+" hover");this.setAttribute("className",this.cellClass+" hover")};CalCell.prototype.onmouseout=function(){this.cellObj.setClass()};CalCell.prototype.onclick=function(){var b=this.cellObj,a=b.owner;if(!a.selCurMonthOnly||b.date.getMonth()==a.displayMonth&&b.date.getFullYear()==a.displayYear){if(a.selectMultiple==true)if(!b.selected)a.selectedDates.arrayIndex(b.date)==-1&&a.selectedDates.push(b.date);else for(var d=a.selectedDates,c=0;c<d.length;c++)d[c].getUeDay()==b.date.getUeDay()&&d.splice(c,1);else{a.selectedDates=new Array(b.date);if(a.tgt){a.tgt.value=a.selectedDates[0].dateFormat();a.mode=="popup"&&a.hide()}}a.reDraw()}};CalCell.prototype.setClass=function(){if(this.selected)this.cellClass="cell_selected";else if(this.owner.displayMonth!=this.date.getMonth())this.cellClass="notmnth";else if(this.date.getDay()>0&&this.date.getDay()<6)this.cellClass="wkday";else this.cellClass="wkend";if(this.date.getFullYear()==this.owner.curDate.getFullYear()&&this.date.getMonth()==this.owner.curDate.getMonth()&&this.date.getDate()==this.owner.curDate.getDate())this.cellClass=this.cellClass+" curdate";this.tableCell.setAttribute("class",this.cellClass);this.tableCell.setAttribute("className",this.cellClass)};Date.prototype.getDayOfYear=function(){return parseInt((this.getTime()-(new Date(this.getFullYear(),0,1)).getTime())/8.64e7+1)};Date.prototype.getWeek=function(){return parseInt((this.getTime()-(new Date(this.getFullYear(),0,1)).getTime())/6.048e8+1)};Date.prototype.getUeDay=function(){return parseInt(Math.floor((this.getTime()-this.getTimezoneOffset()*6e4)/8.64e7))};Date.prototype.dateFormat=function(b){if(!b)b="m/d/Y";LZ=function(a){return(a<0||a>9?"":"0")+a};var h=["January","February","March","April","May","June","July","August","September","October","November","December","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],i=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sun","Mon","Tue","Wed","Thu","Fri","Sat"];b=b+"";var e="",d=0,j="",f="",m=this.getFullYear().toString(),g=this.getMonth()+1,k=this.getDate(),l=this.getDay(),c=this.getHours(),n=this.getMinutes(),o=this.getSeconds(),q,A,r,y,s,u,B,x,z,p,t,w,C,v,D,a={};a.Y=m.toString();a.y=m.substring(2);a.n=g;a.m=LZ(g);a.F=h[g-1];a.M=h[g+11];a.j=k;a.d=LZ(k);a.D=i[l+7];a.l=i[l];a.G=c;a.H=LZ(c);if(c==0)a.g=12;else if(c>12)a.g=c-12;else a.g=c;a.h=LZ(a.g);if(c>11){a.a="pm";a.A="PM"}else{a.a="am";a.A="AM"}a.i=LZ(n);a.s=LZ(o);while(d<b.length){j=b.charAt(d);f="";while(b.charAt(d)==j&&d<b.length)f+=b.charAt(d++);if(a[f]!=null)e=e+a[f];else e=e+f}return e};Array.prototype.arrayIndex=function(c,a){a=a!=null?a:0;for(var b=a;b<this.length;b++)if(c==this[b])return b;return-1};var ms_cal;window.onload=function(){ms_cal=new Epoch("epoch_multi","flat",document.getElementById("multi_container"),true)};var ms_date="";function setValues(a){for(j=0;j<ms_cal.selectedDates.length;j++)ms_date=ms_date+ms_cal.selectedDates[j].dateFormat("Y-m-d")+",";anyString=document.getElementById(a).value;document.getElementById(a).value=ms_date};
