/* --- 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(objectId) {
	s = objectId;
	sLeft = s.offsetLeft;
	while(s.offsetParent!=null) {
		sParent = s.offsetParent;
		sLeft += sParent.offsetLeft;
		s = sParent;
	}
	return sLeft;
}

function Top(objectId) {
	s = objectId;
	sTop = s.offsetTop;
	while(s.offsetParent!=null) {
		sParent = s.offsetParent;
		sTop += sParent.offsetTop;
		s = sParent;
	}
	return sTop;
}

document.write('<table id="fc" style="position:absolute;border-collapse:collapse;background:#FFFFFF;border:1px solid #E0E0E0;display:none;z-index:250;" cellpadding=2>');
document.write('<tr><td style="cursor:pointer" onclick="csuby()"><img style="display:block;" alt="Back 1 Year" src="images/arrowleftyear.gif"></td><td style="cursor:pointer" onclick="csubm()"><img style="display:block;" alt="Back 1 Month" src="images/arrowleftmonth.gif"></td><td colspan=3 id="mns" align="center" style="font:bold 10pt Arial;color:#000000"></td><td align="right" style="cursor:pointer" onclick="caddm()"><img style="display:block;" alt="Forward 1 Month" src="images/arrowrightmonth.gif"></td><td align="right" style="cursor:pointer" onclick="caddy()"><img alt="Forward 1 Year" style="display:block;" src="images/arrowrightyear.gif"></td></tr>');
document.write('<tr><td align=center style="background:#E0E0E0;font:12px Arial">S</td><td align=center style="background:#E0E0E0;font:9pt Arial">M</td><td align=center style="background:#E0E0E0;font:9pt Arial">T</td><td align=center style="background:#E0E0E0;font:9pt Arial">W</td><td align=center style="background:#E0E0E0;font:9pt Arial">T</td><td align=center style="background:#E0E0E0;font:9pt Arial">F</td><td align=center style="background:#E0E0E0;font:9pt Arial">S</td></tr>');
for(var kk=1;kk<=6;kk++) {
	document.write('<tr>');
	for(var tt=1;tt<=7;tt++) {
		num=7 * (kk-1) - (-tt);
		document.write('<td id="v' + num + '" style="width:18px;height:18px">&nbsp;</td>');
	}
	document.write('</tr>');
}
document.write('</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;
	if(document.getElementById('gotodate')||document.getElementById('daily_date')) {
		getObj('fc').style.left=(Left(ielem)-165)+'px';
		getObj('fc').style.top=(Top(ielem)+ielem.offsetHeight+7)+'px';
	} else {
		getObj('fc').style.left=Left(ielem)+'px';
		getObj('fc').style.top=(Top(ielem)+ielem.offsetHeight)+'px';
	}
	if(parseInt(ielem.offsetWidth)>180) getObj('fc').style.width=ielem.offsetWidth;
	else getObj('fc').style.width='180px';
	getObj('fc').style.display='';

	// First check date is valid
	curdt=ielem.value;
	if(curdt=='') {
	    var nowdate = new Date()
		var theyear = nowdate.getFullYear()
		var themonth = nowdate.getMonth()+1
		if(themonth<10) themonth = '0'+themonth;
		var thetoday = nowdate.getDate()
		if(thetoday<10) thetoday = '0'+thetoday;
		curdt = thetoday+"/"+themonth+"/"+theyear;
	}
	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[1]-1;
		ccy=curdtarr[2];
	    var nowdate = new Date()
		var thetoday = nowdate.getDate();
		var themonth = nowdate.getMonth();
		var theyear = nowdate.getFullYear();
		if(ccm!=themonth||ccy!=theyear) thetoday = '';
		prepcalendar(thetoday,curdtarr[1]-1,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) {
	var innerNumber = evtTgt(EvtObj(e)).innerHTML;
    var nowdate = new Date()
	var thetoday = nowdate.getDate()
	if(innerNumber==thetoday) evtTgt(EvtObj(e)).style.background='#E0E0E0';
	else evtTgt(EvtObj(e)).style.background='#FFFFFF';
}
function cs_click(e) {
	var passBack = calvalarr[evtTgt(EvtObj(e)).id.substring(1,evtTgt(EvtObj(e)).id.length)];
	var pieces = passBack.split("/");
	if(pieces[0].length==1) pieces[0] = '0'+pieces[0];
	if(pieces[1].length==1) pieces[1] = '0'+pieces[1];
	passBack = pieces[0]+'/'+pieces[1]+'/'+pieces[2];
	updobj.value=passBack;
	getObj('fc').style.display='none';
	if(document.getElementById('gotodate')) {
		document.getElementById('gotodate').value = passBack;
		document.main_form.action.value = 'Change-Date';
		setScrollComplete();document.main_form.submit();
	}
	if(document.getElementById('daily_date')) {
		var newvalue = passBack.substr(6,4)+'-'+passBack.substr(3,2)+'-'+passBack.substr(0,2);
		document.getElementById('daily_date').value = newvalue;
		document.main_form.action.value = 'Daily';
		setScrollComplete();document.main_form.submit();
	}
	if(document.getElementById('start_date_time')) {
		if(updobj.id=='start_date_time'||updobj.id=='start_date') {
	       	document.getElementById('start_date_time').value = passBack;
    	   	document.getElementById('end_date_time').value = passBack;
       		document.getElementById('start_date').value = passBack;
	       	document.getElementById('end_date').value = passBack;
		} if(updobj.id=='end_date_time'||updobj.id=='end_date') {
	       	checkTimeVal('','',updobj.id.substr(4));
		}
	}
}

var mn=new Array('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC');
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(42);

function f_cps(obj) {
	obj.style.background='#FFFFFF';
	obj.style.font='7pt Arial';
	obj.style.color='#787878';
	obj.style.textAlign='center';
	obj.style.textDecoration='none';
	obj.style.border='1px solid #E0E0E0';
	obj.style.cursor='pointer';
}

function f_cpps(obj) {
	obj.style.background='#FFFFFF';
	obj.style.font='7pt Arial';
	obj.style.color='#787878';
	obj.style.textAlign='center';
	obj.style.textDecoration='line-through';
	obj.style.border='1px solid #E0E0E0';
	obj.style.cursor='default';
}

function f_hds(obj) {
	obj.style.background='#E0E0E0';
	obj.style.font='bold 6pt Arial';
	obj.style.color='#787878';
	obj.style.textAlign='center';
	obj.style.border='1px solid #E0E0E0';
	obj.style.cursor='pointer';
}

// day selected
function prepcalendar(hd,cm,cy) {
	now=new Date();
	sd=now.getDate();
	td=new Date();
	td.setDate(1);
	td.setFullYear(cy);
	td.setMonth(cm);
	cd=td.getDay();
	getObj('mns').innerHTML=mn[cm]+ ' ' + cy;
	marr=((cy%4)==0)?mnl:mnn;
	var alertMe = false;
	for(var d=1;d<=42;d++) {
		f_cps(getObj('v'+parseInt(d)));
		if ((d >= (cd -(-1))) && (d<=cd-(-marr[cm]))) {
			dip=((d-cd < sd)&&(cm==sccm)&&(cy==sccy));
			htd=((hd!='')&&(d-cd==hd));
//			if (dip)
//				f_cpps(getObj('v'+parseInt(d)));
			if (htd)
				f_hds(getObj('v'+parseInt(d)));
			else
				f_cps(getObj('v'+parseInt(d)));

			getObj('v'+parseInt(d)).onmouseover=cs_over;
			getObj('v'+parseInt(d)).onmouseout=cs_out;
			getObj('v'+parseInt(d)).onclick=cs_click;

			getObj('v'+parseInt(d)).innerHTML=d-cd;	
			calvalarr[d]=''+(d-cd)+'/'+(cm-(-1))+'/'+cy;
		}
		else {
			getObj('v'+d).innerHTML='&nbsp;';
			getObj('v'+parseInt(d)).onmouseover=null;
			getObj('v'+parseInt(d)).onmouseout=null;
			getObj('v'+parseInt(d)).style.cursor='default';
			}
	}
}

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

function caddm() {
	marr=((ccy%4)==0)?mnl:mnn;

	ccm+=1;
	if (ccm>=12) {
		ccm=0;
		ccy++;
	}
	cdayf();
    var nowdate = new Date()
	var thetoday = nowdate.getDate();
	if(thetoday<10) thetoday = '0'+thetoday;
	var themonth = nowdate.getMonth();
	if(themonth) themonth = '0'+themonth;
	var theyear = nowdate.getFullYear();
	if(ccm!=themonth||ccy!=theyear) thetoday = '';
	prepcalendar(thetoday,ccm,ccy);
}

function csubm() {
	marr=((ccy%4)==0)?mnl:mnn;

	ccm-=1;
	if (ccm<0) {
		ccm=11;
		ccy--;
	}
    var nowdate = new Date()
	var thetoday = nowdate.getDate();
	if(thetoday<10) thetoday = '0'+thetoday;
	var themonth = nowdate.getMonth();
	if(themonth) themonth = '0'+themonth;
	var theyear = nowdate.getFullYear();
	if(ccm!=themonth||ccy!=theyear) thetoday = '';
	prepcalendar(thetoday,ccm,ccy);
}

function caddy() {
	ccy = parseInt(ccy) + 1;
    var nowdate = new Date()
	var thetoday = nowdate.getDate();
	if(thetoday<10) thetoday = '0'+thetoday;
	var themonth = nowdate.getMonth();
	if(themonth) themonth = '0'+themonth;
	var theyear = nowdate.getFullYear();
	if(ccm!=themonth||ccy!=theyear) thetoday = '';
	prepcalendar(thetoday,ccm,ccy);
}

function csuby() {
	ccy = parseInt(ccy) - 1;
    var nowdate = new Date()
	var thetoday = nowdate.getDate();
	if(thetoday<10) thetoday = '0'+thetoday;
	var themonth = nowdate.getMonth();
	if(themonth) themonth = '0'+themonth;
	var theyear = nowdate.getFullYear();
	if(ccm!=themonth||ccy!=theyear) thetoday = '';
	prepcalendar(thetoday,ccm,ccy);
}

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