// copyright (c) 2006 Majigs, LLC

var months=['January','February','March','April','May','June','July','August','September','October','November','December'];var days=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];var suffix=['th','st','nd','rd','th','th','th','th','th','th','th','th','th','th','th','th','th','th','th','th'];var today=new Date();var eventMap=null;var eventList=null;var eventIdMap=null;var dateBoxes=null;var built=false;var afterLoadDate=null;var offsetLoaded=null;var dateShowing=null;var dateShowingDiv=null;var prefs=null;var curAd=null;var adcolor=1;var innerType='month';function logoutHook(){resetEvents();prefs=null;var i;for(i=0;i<dateBoxes.length;i++){makeColorTable(i);}}
function loginHook(){g('month').offset=0;load();}
function changeMonth(off){var m=g('month');m.offset=m.offset+off;load();}
function dateClick(targ,ev){if(!built){return false;}
if(window.transferring){return false;}
window.transferSteps=6;window.transferring=1;if(!ev){var ev=window.event;}
var pos=getPos(targ);var div=c('DIV');cls(div,'datedetails');m(div,pos[0],pos[1]);sz(div,targ.offsetWidth-1,targ.offsetHeight-1);ac(document.body,div);window.xferDiv=div;window.xferTarg=targ;var root=g('root');pos=getPos(root);window.xferLeft=pos[0];window.xferTop=pos[1];window.xferRight=pos[0]+root.offsetWidth;window.xferBottom=pos[1]+root.offsetHeight;setTimeout('transferUp()',30);return false;}
function transferUp(){if(window.transferSteps==1){m(window.xferDiv,window.xferLeft,window.xferTop);sz(window.xferDiv,window.xferRight-window.xferLeft,window.xferBottom-window.xferTop);window.transferring=0;window.transferSteps=0;populateDate(window.xferDiv,window.xferTarg.date);window.xferDiv=null;window.xferTarg=null;return;}
var cL=i(window.xferDiv.style.left);var cT=i(window.xferDiv.style.top);var cR=cL+i(window.xferDiv.offsetWidth);var cB=cT+i(window.xferDiv.offsetHeight);m(window.xferDiv,((window.transferSteps-1)*cL+window.xferLeft)/window.transferSteps,((window.transferSteps-1)*cT+window.xferTop)/window.transferSteps);var r=((window.transferSteps-1)*cR+window.xferRight)/window.transferSteps;var b=((window.transferSteps-1)*cB+window.xferBottom)/window.transferSteps;sz(window.xferDiv,r-i(window.xferDiv.style.left),b-i(window.xferDiv.style.top));window.transferSteps-=1;setTimeout('transferUp()',30);}
function populateDate(el,date){dateShowing=date;dateShowingDiv=el;var first=g('month').first;var gdiv=c('DIV');gdiv.align='center';var b=c('B');ac(gdiv,b);ac(b,t(months[first.getMonth()]+' '+date+suffix[date<20?date:date%10]+', '+first.getFullYear()));ac(gdiv,c('BR'));var a=c('A');a.href='#';a.onclick=dateClose;cls(a,'closedate');at(a,'Close');ac(gdiv,a);ac(el,gdiv);var evts=eventList[date-1];if(evts.length===0){ac(gdiv,c('BR'));ac(gdiv,c('BR'));at(gdiv,'No events scheduled');ac(gdiv,c('BR'));}else{var sdiv=c('DIV');cls(sdiv,'details');ac(gdiv,sdiv);sdiv.style.height=(_i(el.offsetHeight)-sdiv.offsetTop-31)+'px';var i;for(i=0;i<evts.length;i++){var span=c('SPAN');span.id='eventDiv'+evts[i][0];span.evDate=date;span.evIdx=i;var div=c('DIV');cls(div,'inliner');div.style.width='9px';var tmp=c('DIV');cls(tmp,'colorbox'+evts[i][1]);anbsp(tmp);ac(div,tmp);ac(span,div);div=c('DIV');cls(div,'inliner');div.style.width='45px';if(evts[i][3]!==null){var hr=evts[i][3]%12;at(div,((hr===0?12:hr)+':'+(evts[i][4]>9?'':'0')+evts[i][4]+(evts[i][3]>=12?'pm':'am')));ac(span,div);div=c('DIV');cls(div,'inliner');div.style.width='118px';}else{div.style.width='168px';}
tmp=c('A');tmp.href='#';tmp.mjgInner=evts[i][5]+' '+evts[i][6];tmp.onclick=function(e){f(this.detailRow);updateInner(this.mjgInner);return false;};if(evts[i][7]){cls(tmp,'eventtitle-important');}else{cls(tmp,'eventtitle');}
at(tmp,evts[i][5]);ac(div,tmp);ac(span,div);ac(span,c('BR'));div=c('DIV');div.id='evDetails'+evts[i][0];cls(div,'evDetails');tmp.detailRow=div;aft(div,evts[i][6]);var pb=c('P');cls(pb,'evButton');var dButton=c('A');dButton.href='#';cls(dButton,'evButton');at(dButton,'Delete');ac(pb,dButton);dButton.id='delBtn'+evts[i][0];dButton.onclick=deleteEvent;at(pb,' ');var eButton=c('A');eButton.href='#';cls(eButton,'evButton');at(eButton,'Edit');ac(pb,eButton);eButton.id='edtBtn'+evts[i][0];eButton.onclick=showEditEvent;at(pb,' ');var eButton=c('A');eButton.href='#';cls(eButton,'evButton');at(eButton,'Copy');ac(pb,eButton);eButton.id='cpyBtn'+evts[i][0];eButton.onclick=showCopyEvent;ac(div,pb);ac(span,div);ac(sdiv,span);}}
var bdiv=c('DIV');cls(bdiv,'dateButtons');var addEvent=c('A');cls(addEvent,'dateButton');addEvent.href='#';addEvent.onclick=showAdd;at(addEvent,'\u00a0Add Event/Task\u00a0');ac(bdiv,addEvent);anbsp(bdiv);var help=c('A');cls(help,'dateButton');help.href='#';help.onclick=function(){showHelp('help-date');return false};at(help,'\u00a0Help\u00a0');ac(bdiv,help);ac(gdiv,bdiv);}
function dateClose(){if(dateShowingDiv){dateShowingDiv.parentNode.removeChild(dateShowingDiv);dateShowingDiv=null;dateShowing=null;}
return false;}
function focusGoto(){sl(g('gotoMonth'));}
function showGoto(){var first=g('month').first;g('gotoMonth').value=first.getMonth()+1;g('gotoYear').value=first.getFullYear();g('gotoDay').value='';showADiv('goto-date',focusGoto);}
function gotoDate(){var month=i(g('gotoMonth').value);var year=i(g('gotoYear').value);var day=i(g('gotoDay').value);if(isNaN(month)||month<=0||month>12){alert('Please enter a valid month.');sl(g('gotoMonth'))
return;}
if(isNaN(year)||year<1000){alert('Please enter a valid four digit year');sl(g('gotoYear'));return;}
if(isNaN(day)&&g('gotoDay').value.length>0){alert('Please enter a valid day or leave this field blank.');sl(g('gotoDay'));return;}
if(!isNaN(day)&&day<=0||day>31){alert('Please enter a valid day or leave this field blank.');sl(g('gotoDay'));return;}
var date=new Date();date.setMonth(month-1);date.setFullYear(year-1);if(!isNaN(day)){date.setDate(day);if(date.getDate()!=day){alert('Please enter a valid existing day or leave this field blank.');sl(g('gotoDay'));return;}}
var yearDiff=year-today.getFullYear();var monthDiff=month-1-today.getMonth();var mo=g('month');mo.offset=monthDiff+12*yearDiff;h(g('goto-date'));if(!isNaN(day)){afterLoadDate=day;}
if(!isNull(offsetLoaded)&&offsetLoaded==mo.offset){finishLoad();}else{load();}}
function gotoToday(){g('month').offset=0;afterLoadDate=today.getDate();h(g('goto-date'));if(!isNull(offsetLoaded)&&offsetLoaded===0){finishLoad();}else{load();}}
loading=0;redo=0;function load(){if(loading){redo=1;}
loading=1;var mo=g('month');if(!mo.offset){mo.offset=0;}
var month=new Date(today.getTime());month.setDate(1);month.setMonth(today.getMonth()+mo.offset);built=false;var uri=noCacheURI('getevents.php?m='+(month.getMonth()+1)+'&y='+month.getFullYear());if(prefs==null){uri+='&p=1';}
if(curAd==null){uri+='&a=1';}
(new mjXR(uri,processData,"UGET")).send();buildDateBoxes();var div=g('loading');var tb=g('ctable');var pos1=getPos(tb);var pos2=getPos(dateBoxes[0]);m(div,pos1[0],pos2[1]);sz(div,tb.offsetWidth-1,tb.offsetHeight-1-pos2[1]+pos1[1]);s(div);}
function sunmon(num){if(prefs&&prefs[0]){if(num)
return num-1;else return 6;}else return num;}
function unsunmon(num){if(prefs&&prefs[0]){if(num<6)
return num+1;else return 0;}else return num;}
var dayheaders=['S','M','T','W','T','F','S'];function setDayHeaders(){var i;var arr=g('dayheaders').getElementsByTagName('TD');for(i=0;i<7;i++){cc(arr[i]);at(arr[i],dayheaders[unsunmon(i)]);}}
function buildDateBoxes(){var mo=g('month');dateBoxes=[];var first=new Date(today.getTime());first.setDate(1);first.setMonth(first.getMonth()+mo.offset);mo.first=first;mo.innerHTML=months[first.getMonth()]+" "+first.getFullYear();var tb=g('ctable');var tbody=tb.tBodies[0];var i;for(i=tbody.rows.length-1;i>=1;i--){tbody.removeChild(tb.rows[i]);}
var tr=c('TR');for(i=0;i<sunmon(first.getDay());i++){var td=c('TD');cls(td,'blank');td.width="30";td.height="25";td.innerHTML='&nbsp;';ac(tr,td);}
var d=new Date(first.getTime());do{if(sunmon(d.getDay())===0&&d.getDate()!=first.getDate()){ac(tbody,tr);tr=c('TR');}
var td=c('TD');if(d.getDate()==today.getDate()&&d.getMonth()==today.getMonth()&&d.getFullYear()==today.getFullYear()){cls(td,"today");}else if(d<today){cls(td,"past");}else{cls(td,"day");}
td.width="30";td.height="25";td.vAlign="top";var p=c('P');at(p,d.getDate());p.className='daylabel';ac(td,p);td.date=d.getDate();td.onclick=function(e){return dateClick(this,e);};ac(tr,td);push(dateBoxes,td);d.setDate(d.getDate()+1);}while(d.getMonth()==first.getMonth());if(sunmon(d.getDay())!=0){for(i=sunmon(d.getDay());i<7;i++){var td=c('TD');cls(td,'blank');td.width="30";td.height="25";anbsp(td);ac(tr,td);}}
ac(tbody,tr);built=true;}
function resetEvents(){eventMap=[];eventList=[];eventIdMap=new Object();var i;for(i=0;i<31;i++){eventMap[i]=[[],[],[]];eventList[i]=[];}}
function processData(xmlHttp,mjXR){loading=0;if(redo){redo=0;load();return;}
var events=eval(xmlHttp.responseText);if(events[0]=='LOGIN'){showLogin();preloginRequest=mjXR;loading=1;return;}
while(true){var x=pop(events);if(x&&x[0]==-1){prefs=x[1];setDayHeaders();buildDateBoxes();}else if(x&&x[0]==-2){showAd(x[1]);}else{push(events,x);break;}}
resetEvents();var evx;var i;var innerIdx=1+Math.floor(Math.random()*(events.length-1));for(i=1;i<events.length;i++){evx=events[i];if(i==innerIdx){updateInner(evx[5]+' '+evx[6]);}
push(eventMap[evx[2]-1][evx[1]],evx);push(eventList[evx[2]-1],evx);eventIdMap[evx[0]]=evx;}
processData2();finishLoad();}
function finishLoad(){if(afterLoadDate!=null){dateClick(dateBoxes[afterLoadDate-1],null);afterLoadDate=null;}
offsetLoaded=g('month').offset;}
function processData2(){if(!built){setTimeout('processData2()',20);return;}
h(g('loading'));populateMonthTable();}
function populateMonthTable(){var i;for(i=0;i<dateBoxes.length;i++){makeColorTable(i);}}
function resetDateBoxes(){setDayHeaders();buildDateBoxes();populateMonthTable();}
var altTypes=['Tasks','Meetings','Annuals']
function makeColorTable(i){var td=dateBoxes[i];var tableId='colorTable-'+(i+1);if(g(tableId)){d(g(tableId));}
var oColorTable=table(0,0);oColorTable.align="right";cls(oColorTable,'oColors');oColorTable.id=tableId;var oColorTBody=tbod(oColorTable);var oColorTR=c('TR');ac(oColorTBody,oColorTR);var oColorTD=c('TD');var colorTable=table(1,0);cls(colorTable,'colors');var colorTBody=tbod(colorTable);var colorTR=c('TR');ac(colorTBody,colorTR);var events=eventMap[i];var type;var j=0;var important=0;for(j=0;j<events.length;j++){type=events[j];var sz=Math.round((1.0-(2.0/(type.length+2.0)))*8.0);var cTD=c('TD');cTD.width='5px';cTD.vAlign='bottom';if(sz!=0){var alt=altTypes[j]+':';var k;for(k=0;k<eventList[i].length;k++){if(eventList[i][k][1]==j){alt+='\n'+(eventList[i][k][7]?'** ':'* ')+eventList[i][k][5];}}
var cDiv=c('DIV');cDiv.style.height=sz+'px';cls(cDiv,'color'+j);var img=c('IMG');img.src='sp.gif';img.width='5';img.height=sz;img.alt=uu(alt);ac(cDiv,img);ac(cTD,cDiv);}else{anbsp(cTD);}
ac(colorTR,cTD);var k;for(k=0;!important&&k<type.length;k++){if(type[k][7]){important=1;}}}
td.mmImportant=important;var importantAlt='Important Events:';if(important){var k;for(k=0;k<eventList[i].length;k++){if(eventList[i][k][7]){importantAlt+='\n* '+eventList[i][k][5];}}}else{importantAlt+='\nNone.';}
colorTable.align="right";oColorTD.align="right";ac(oColorTD,colorTable);ac(oColorTR,oColorTD);var eTD=c('TD');eTD.vAlign='bottom';var img=c('IMG');img.src='ex.gif';img.width='4';img.height='9';img.alt=uu(importantAlt);ac(eTD,img);cls(eTD,important?'important':'notimportant');ac(oColorTR,eTD);ac(td,oColorTable);}
function addOrEditEvent(){var editing=g('add-event').mmEditing;var iE=g('add-event-i');var aE=g('addButtons');var lo=g('addLoading');sz(lo,iE.offsetWidth,iE.offsetHeight+aE.offsetHeight);lo.style.paddingTop=i(i(iE.offsetHeight)/2);h(iE);h(aE);h(g('addClose'));s(lo);var formStr='';var type=g('addType');formStr='type='+type.options[type.selectedIndex].value;formStr+='&month='+g('addMonth').value;formStr+='&day='+g('addDay').value;formStr+='&year='+g('addYear').value;formStr+='&hour='+g('addHour').value;formStr+='&min='+g('addMin').value;var ampm=g('addAMPM');formStr+='&ampm='+ampm.options[ampm.selectedIndex].innerHTML;formStr+='&title='+urlencode(g('addTitle').value);formStr+='&desc='+urlencode(g('addDesc').value);formStr+='&important='+(g('addImportant').checked?1:0);var req;if(editing){formStr+='&id='+g('add-event').mmEditId;req=new mjXR('editevent.php',editResponse,'POST',formStr);}else{req=new mjXR('addevent.php',addResponse,'POST',formStr);}
req.send();}
function editResponse(x,mjXR){var ret=eval(x.responseText);if(ret[0]=='OK'){internalDeleteEvent(ret[1][0]);}
addResponse(x,mjXR);}
function addResponse(x,mjXR){h(g('addLoading'));var ret=eval(x.responseText);if(ret[0]=='LOGIN'){showLogin();preloginRequest=mjXR;return;}
if(ret[0]=='OK'){showAd(pop(ret));var dn=g('addDone');var lo=g('addLoading');dn.style.height=lo.style.height;dn.style.width=lo.style.width;dn.style.paddingTop=lo.style.paddingTop;s(dn);var event=ret[1];updateInner(event[5]+' '+event[6]);var m_y=ret[2];var mo=g('month');if(m_y[0]==(mo.first.getMonth()+1)&&(isNull(m_y[1])||m_y[1]==mo.first.getFullYear())){eventIdMap[event[0]]=event;push(eventMap[event[2]-1][event[1]],event);var ol=eventList[event[2]-1];var nl=[];var i=0;for(i=0;i<ol.length;i++){if(ol[i][3]<event[3]||(ol[i][3]==event[3]&&(ol[i][4]<event[4]||(ol[i][4]==event[4]&(ol[i][5]<=event[5])))))
{push(nl,ol[i]);}else{break;}}
push(nl,event);for(;i<ol.length;i++){push(nl,ol[i]);}
eventList[event[2]-1]=nl;makeColorTable(event[2]-1);if(dateShowing==event[2]){cc(dateShowingDiv);populateDate(dateShowingDiv,dateShowing);}}
setTimeout('closeAddOnSuccess()',isNull(dateShowing)?200:500);}else{s(g('add-event-i'));s(g('addButtons'));s(g('addClose'));alert(ret[2]);sl(g('add'+ret[1]));}}
function closeAddOnSuccess(){s(g('add-event-i'));s(g('addClose'));h(g('add-event'));h(g('addDone'));h(g('editDone'));s(g('addButtons'));}
function showAdd(){var addDiv=g('add-event');addDiv.mmEditing=0;var iE=g('add-event-i');iE.style.overflow='visible';iE.style.height='20px';g('addType').selectedIndex=0;g('addMonth').value=g('month').first.getMonth()+1;g('addYear').value=g('month').first.getFullYear();g('addDay').value=isNull(dateShowing)?'':dateShowing;g('addHour').value='09';g('addMin').value='00';g('addAMPM').selectedIndex=0;g('addTitle').value='';g('addDesc').value='';var m=g('addMode');cc(m);at(m,'Add');g('addSubmit').value='Add Event';vh(g('addButtons'));showADiv('add-event',fixAdd);return false;}
function fixAdd(elem){h(g('addLoading'));s(g('add-event-i'));var iE=g(elem.id+'-i');iE.style.overflow='hidden';var pos0=getPos(g('addP'));var pos1=getPos(iE);iE.style.height=(elem.offsetHeight+pos0[1]-pos1[1]-16-i(g('addButtons').offsetHeight))+'px';iE.style.overflow='auto';addTypeChange();vs(g('addButtons'));g('addType').focus();}
function showAddHelp(){vh(g('addType'));vh(g('addAMPM'));showHelp('help-'+g('addMode').innerHTML);}
function hideAddHelp(){h(g('help-'+g('addMode').innerHTML));vs(g('addType'));if(g('addType').selectedIndex==1){vs(g('addAMPM'));}}
function addTypeChange(){var idx=g('addType').selectedIndex;if(idx==0){trh(g('trAddTime'));trs(g('trAddYear'));}else if(idx==1){trs(g('trAddTime'));trs(g('trAddYear'));vs(g('addAMPM'));}else if(idx==2){trh(g('trAddTime'));trh(g('trAddYear'));}}
function showEditEvent(){var id=this.id.substring(6);var evx=eventIdMap[id];var addDiv=g('add-event');addDiv.mmEditing=1;addDiv.mmEditId=id;var iE=g('add-event-i');iE.style.overflow='visible';iE.style.height='20px';g('addType').selectedIndex=evx[1];g('addMonth').value=g('month').first.getMonth()+1;g('addYear').value=g('month').first.getFullYear();g('addDay').value=evx[2];var hour=(evx[3]!==null?i(evx[3]):9)%12;if(hour==0)hour=12;hour=hour+'';g('addHour').value=(hour.length==1?'0':'')+hour;var min=(evx[4]?i(evx[4]):0)+'';g('addMin').value=(min.length==1?'0':'')+min;g('addAMPM').selectedIndex=(evx[3]>=12?1:0);g('addTitle').value=uu(evx[5]);g('addDesc').value=uu(evx[6]);g('addImportant').checked=evx[7]?true:false;var m=g('addMode');cc(m);at(m,'Edit');g('addSubmit').value='Save Changes';vh(g('addButtons'));showADiv('add-event',fixAdd);return false;}
function showCopyEvent(){var id=this.id.substring(6);var evx=eventIdMap[id];var addDiv=g('add-event');addDiv.mmEditing=0;addDiv.mmEditId=id;var iE=g('add-event-i');iE.style.overflow='visible';iE.style.height='20px';g('addType').selectedIndex=evx[1];g('addMonth').value=g('month').first.getMonth()+1;g('addYear').value=g('month').first.getFullYear();g('addDay').value=evx[2];var hour=(evx[3]!==null?i(evx[3]):9)%12;if(hour==0)hour=12;hour=hour+'';g('addHour').value=(hour.length==1?'0':'')+hour;var min=(evx[4]?i(evx[4]):0)+'';g('addMin').value=(min.length==1?'0':'')+min;g('addAMPM').selectedIndex=(evx[3]>=12?1:0);g('addTitle').value=uu(evx[5]);g('addDesc').value=uu(evx[6]);g('addImportant').checked=evx[7]?true:false;var m=g('addMode');cc(m);at(m,'Copy');g('addSubmit').value='Save Changes';vh(g('addButtons'));showADiv('add-event',fixAdd);return false;}
function deleteEvent(e){var id=this.id.substring(6);if(confirm('Do you really want to delete this event?')){var div=g('evDetails'+id);cc(div);var b=c('B');at(b,'Deleting...');ac(div,b);new mjXR('delete.php?id='+id,deleteDone,'UGET').sendSync();}
return false;}
function deleteDone(x){var ret=eval(x.responseText);if(ret[0]=='LOGIN'){showLogin();preloginRequest=mjXR;return;}
internalDeleteEvent(ret[1]);}
function internalDeleteEvent(id){var evDiv=g('eventDiv'+id);var date=evDiv.evDate;var idx=evDiv.evIdx;var par=evDiv.parentNode;d(evDiv);eventIdMap[id]=null;try{delete eventIdMap[id];}catch(e){}
var evObj=eventList[date-1][idx];arrRemoveAt(eventList[date-1],idx);arrRemoveEqual(eventMap[date-1][evObj[1]],evObj);if(eventList[date-1].length==0){cc(dateShowingDiv);populateDate(dateShowingDiv,dateShowing);}
makeColorTable(evObj[2]-1);}
function editEvent(e){return false;}
function showOptions(){if(prefs&&prefs[0]){g('startday-m').checked=true;}else{g('startday-s').checked=true;}
if(prefs&&prefs[1]){g('optin-y').checked=true;}else{g('optin-n').checked=true;}
h(g('optionsstatus'));s(g('optionsform'));vs(g('closeoptions'));showADiv('options');return false;}
function setOptions(){h(g('optionsform'));s(g('optionsstatus'));vh(g('closeoptions'));new mjXR('setoptions.php?startday='+(g('startday-m').checked?1:0)+'&optin='+(g('optin-y').checked?1:0),optionsDone,'UGET').send();}
function optionsDone(x){h(g('options'));var ret=eval(x.responseText);if(ret[0]=='LOGIN'){showLogin();preloginRequest=mjXR;return;}
prefs=ret[1];resetDateBoxes();}