/* --- Swazz Javascript Calendar ---
/* --- v 1.0 3rd November 2006
By Oliver Bryant
http://calendar.swazz.org */

function getObj(objID)
{
    if (document.getElementById) {return document.getElementById(objID);}
    else if (document.all) {return document.all[objID];}
    else if (document.layers) {return document.layers[objID];}
}

function checkClick(e) {
                e?evt=e:evt=event;
                CSE=evt.target?evt.target:evt.srcElement;
               // if (getObj('fc'))
                 //               if (!isChild(CSE,getObj('fc')))
                   //                             getObj('fc').style.display='none';
}

function isChild(s,d) {
                while(s) {
                                if (s==d) 
                                                return true;
                                s=s.parentNode;
                }
                return false;
}

function Left(obj)
{
                var curleft = 0;
                if (obj.offsetParent)
                {
                                while (obj.offsetParent)
                                {
                                                curleft += obj.offsetLeft
                                                obj = obj.offsetParent;
                                }
                }
                else if (obj.x)
                                curleft += obj.x;
                return curleft;
}

function Top(obj)
{
                var curtop = 0;
                if (obj.offsetParent)
                {
                                while (obj.offsetParent)
                                {
                                                curtop += obj.offsetTop
                                                obj = obj.offsetParent;
                                }
                }
                else if (obj.y)
                                curtop += obj.y;
                return curtop;
}


//document.write('<table id="fc" style="position:absolute;border-collapse:collapse;background:#FFFFFF;border:1px solid #ABABAB;display:none" cellpadding=2>');
/*
var calObj = document.getElementById("divCal");
calObj.innerHTML='<table id="fc" style="position:absolute;border-collapse:collapse;background:#FFFFFF;border:1px solid #ABABAB;" cellpadding=2>';
calObj.innerHTML='<tr><td style="cursor:pointer" onclick="csubm()"><img src="arrowleftmonth.gif"></td><td colspan=5 id="mns" align="center" style="font:bold 13px Arial"></td><td align="right" style="cursor:pointer" onclick="caddm()"><img src="arrowrightmonth.gif"></td></tr>';
calObj.innerHTML='<tr><td align=center style="background:#ABABAB;font:12px Arial">S</td><td align=center style="background:#ABABAB;font:12px Arial">M</td><td align=center style="background:#ABABAB;font:12px Arial">T</td><td align=center style="background:#ABABAB;font:12px Arial">W</td><td align=center style="background:#ABABAB;font:12px Arial">T</td><td align=center style="background:#ABABAB;font:12px Arial">F</td><td align=center style="background:#ABABAB;font:12px Arial">S</td></tr>';
for(var kk=1;kk<=6;kk++) {
                calObj.innerHTML = '<tr>';
                for(var tt=1;tt<=7;tt++) {
                                num=7 * (kk-1) - (-tt);
                                calObj.innerHTML='<td id="v' + num + '" style="width:18px;height:18px">&nbsp;</td>';
                }
                calObj.innerHTML='</tr>';
}
calObj.innerHTML='</table>';
*/

document.all?document.attachEvent('onclick',checkClick):document.addEventListener('click',checkClick,false);


// Calendar script
var now = new Date;
var sccm=now.getMonth();
var sccy=now.getFullYear();
var ccm=now.getMonth();
var ccy=now.getFullYear();

var updobj;
function lcs(ielem) {
    updobj=ielem;
        
    // First check date is valid
    curdt=ielem.value; 
    
    if ( (curdt == null) || (curdt == '') ){
       return;
    }
                   
    curdtarr=curdt.split('/');
    
    isdt=true;
    
    for(var k=0;k<curdtarr.length;k++) {
        if (isNaN(curdtarr[k])) { isdt=false; }
    }
    
    if (isdt&(curdtarr.length==3)) {
        ccm=curdtarr[0]-1;
		ccd=curdtarr[1];
        ccy=curdtarr[2];
        prepcalendar(ccd,ccm,curdtarr[2]);
    }                
}

function evtTgt(e)
{
                var el;
                if(e.target)el=e.target;
                else if(e.srcElement)el=e.srcElement;
                if(el.nodeType==3)el=el.parentNode; // defeat Safari bug
                return el;
}
function EvtObj(e){if(!e)e=window.event;return e;}
function cs_over(e) {
                evtTgt(EvtObj(e)).style.background='#FFCC66';
}
function cs_out(e) {
                evtTgt(EvtObj(e)).style.background='#FFFFFF';
}
function cs_click(e) {
               updobj.value=calvalarr[evtTgt(EvtObj(e)).id.substring(1,evtTgt(EvtObj(e)).id.length)];
               getObj('div6').style.display='none'; 
               getObj('div7').style.display='none'; 
               getObj('div8').style.display='none'; 
               getObj('div9').style.display='none'; 
               getObj('div10').style.display='none';                               
               getObj('div11').style.display='none';                               
}

var mn=new Array('January','February','March','April','May','June','July','August','September','October','November','December','January');
var mnn=new Array('31','28','31','30','31','30','31','31','30','31','30','31');
var mnl=new Array('31','29','31','30','31','30','31','31','30','31','30','31');
var calvalarr=new Array(84);

function f_cps(obj) {
                obj.style.background='#FFFFFF';
                obj.style.font='12px Arial';
                obj.style.color='#333333';
                obj.style.textAlign='center';
                obj.style.textDecoration='none';
                obj.style.cursor='pointer';
}

function f_cpps(obj) {
                obj.style.background='#FFFFFF';
                obj.style.font='12px Arial';
                obj.style.color='#ABABAB';
                obj.style.textAlign='center';
                obj.style.textDecoration='line-through';
                obj.style.cursor='default';
}

function f_hds(obj) {
                obj.style.background='#FFFFFF';
                obj.style.font='bold 12px Arial';
                obj.style.color='#333333';
                obj.style.textAlign='center';
                obj.style.cursor='pointer';
}

// day selected
function prepcalendar(hd,cm,cy) {

                var now = new Date();
                var currentDayOfMonth = now.getDate();
                                                
                // set the top table
                
                var topMonthReferenceDate = new Date();                
                topMonthReferenceDate.setDate(1);
                topMonthReferenceDate.setFullYear(cy);
                topMonthReferenceDate.setMonth(cm);
                
                getObj('mns').innerHTML=mn[topMonthReferenceDate.getMonth()] + ' ' + topMonthReferenceDate.getFullYear();
                
                var topYearLastDayOfMonthArray = (( topMonthReferenceDate.getFullYear() % 4)==0)?mnl:mnn;
                
                for(var d = 1; d <= 42; d ++) {
                    var dayContainer = getObj('v'+parseInt(d));
                    f_cps(dayContainer);
                    
                    if ((d >= (topMonthReferenceDate.getDay() -(-1))) && (d <= topMonthReferenceDate.getDay() - ( -topYearLastDayOfMonthArray[topMonthReferenceDate.getMonth()]))) {
                        var isDayPassed = ((d - topMonthReferenceDate.getDay() < currentDayOfMonth ) && (topMonthReferenceDate.getMonth() == sccm) && (topMonthReferenceDate.getFullYear() == sccy));
                        var isDaySelected =( (hd!='') && (d - topMonthReferenceDate.getDay() == hd) );
                        
                        // configure day appearance
                        if (isDayPassed) {                            
                            f_cpps(dayContainer);                                                                                                                  
                        } else if (isDaySelected) {                            
                            f_hds(dayContainer);
                        } else {                            
                            f_cps(dayContainer);
                        }

                        dayContainer.onmouseover=(isDayPassed)?null:cs_over;
                        dayContainer.onmouseout=(isDayPassed)?null:cs_out;
                        dayContainer.onclick=(isDayPassed)?null:cs_click;
                        
                        dayContainer.innerHTML= (d - topMonthReferenceDate.getDay());            
                        calvalarr[d]= '' + (topMonthReferenceDate.getMonth() - (-1) ) + '/' + (d - topMonthReferenceDate.getDay()) + '/' + topMonthReferenceDate.getFullYear(); 
                        
                    } else {
                        dayContainer.innerHTML='&nbsp;';
                        dayContainer.onmouseover=null;
                        dayContainer.onmouseout=null;
                        dayContainer.style.cursor='default';
                    }
                }
                
                // set the bottom table
                                
                var nextMonthIndex = '';
                var nextMonthYear = '';
				if(cm == 11) {
				    nextMonthIndex = 0;
				    nextMonthYear = (cy+1);				    
				} else {
				    nextMonthIndex = (cm+1);
				    nextMonthYear = cy;				    				    
				}
				
				var bottomMonthReferenceDate = new Date();                
                bottomMonthReferenceDate.setDate(1);
                bottomMonthReferenceDate.setFullYear(nextMonthYear);
                bottomMonthReferenceDate.setMonth(nextMonthIndex);
				
				getObj('mns2').innerHTML=mn[bottomMonthReferenceDate.getMonth()]+ ' ' + bottomMonthReferenceDate.getFullYear();
                
                var bottomYearLastDayOfMonthArray = ((bottomMonthReferenceDate.getFullYear() % 4)==0)?mnl:mnn;
                
                for(var d = 43; d <= 84; d++) {
                    var bottomDayContainer = getObj('v'+parseInt(d));
                    var bottomDayPosition = d - 42;                                                           
                    
                    f_cps(bottomDayContainer);     
                                               
                    if ((bottomDayPosition >= (bottomMonthReferenceDate.getDay() -(-1))) && (bottomDayPosition <= bottomMonthReferenceDate.getDay() - ( -bottomYearLastDayOfMonthArray[bottomMonthReferenceDate.getMonth()]))) {                                                
                                    
                        bottomDayContainer.onmouseover=cs_over;
                        bottomDayContainer.onmouseout=cs_out;
                        bottomDayContainer.onclick=cs_click;
                        
                        bottomDayContainer.innerHTML = bottomDayPosition - bottomMonthReferenceDate.getDay();            
                        calvalarr[d]= '' + (bottomMonthReferenceDate.getMonth() -(-1)) + '/' + ( (bottomDayPosition) - bottomMonthReferenceDate.getDay()) + '/' + bottomMonthReferenceDate.getFullYear(); 
                    }
                    else {
                        bottomDayContainer.innerHTML='&nbsp;';
                        bottomDayContainer.onmouseover=null;
                        bottomDayContainer.onmouseout=null;
                        bottomDayContainer.style.cursor='default';
                    }
                }
}

//prepcalendar('',ccm,ccy);
//getObj('fc'+cc).style.visibility='hidden';

function caddm() {
                marr=((ccy%4)==0)?mnl:mnn;
                
                ccm+=2;
                if (ccm>=12) {
                                ccm=0;
                                ccy++;
                }
                cdayf();
                prepcalendar('',ccm,ccy);
}

function ciddm() {
                marr=((ccy%4)==0)?mnl:mnn;
                
                ccm+=1;
                if (ccm>=12) {
                                ccm=0;
                                ccy++;
                }
                cdayf();
                prepcalendar('',ccm,ccy);
}

function cuddm() {
                marr=((ccy%4)==0)?mnl:mnn;
                
                ccm+=0;
                if (ccm>=12) {
                                ccm=0;
                                ccy++;
                }
                cdayf();
                prepcalendar('',ccm,ccy);
}

function csubm() {
                marr=((ccy%4)==0)?mnl:mnn;
                
                ccm-=2;
                if (ccm<0) {
                                ccm=11;
                                ccy--;
                }
                cdayf();
                prepcalendar('',ccm,ccy);
}

function cdayf() {
if ((ccy>sccy)|((ccy==sccy)&&(ccm>=sccm)))
                return;
else {
                ccy=sccy;
                ccm=sccm;
                //cfd=scfd;
                }
}



