/* --- 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('<div id="fc" style="position:absolute; border:1px solid #cbd688; display:none"><table class="callendar" border=0 cellpadding=0 cellspacing=8>');
document.write('<tr><th colspan=7 align="left" valign="top"><div style="width:232px;height:26px;background-color:#f1dae8;position:absolute;margin:-8px 0 0 -8px;"><img style="cursor:pointer;" onclick="csubm()" src="client/images/arrowleftmonth.gif" align="left"><div style="font:bold 11px Tahoma; color:#c363a0; text-align:center; line-height:26px; width:153px; float:left;" id="mns"></div><img style="cursor:pointer;" onclick="caddm()" src="client/images/arrowrightmonth.gif" align="right"></div></th></tr>');
document.write('<tr><td align=center style="font:bold 11px Tahoma; color:#495d6e;">P</td><td align=center style="font:bold 11px Tahoma; color:#495d6e;">A</td><td align=center style="font:bold 11px Tahoma; color:#495d6e;">T</td><td align=center style="font:bold 11px Tahoma; color:#495d6e;">K</td><td align=center style="font:bold 11px Tahoma; color:#495d6e;">P</td><td align=center style="font:bold 11px Tahoma; color:#9fb51c;">Š</td><td align=center style="font:bold 11px Tahoma; color:#9fb51c;">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:22px;height:21px;">&nbsp;</td>');
	}
	document.write('</tr>');
}
document.write('</table></div>');

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;
var callobj;
function lcs(ielem, c_id) {  
	updobj=document.getElementById(ielem);
	callobj=document.getElementById(c_id);

	getObj('fc').style.left=Left(callobj)+29;
	getObj('fc').style.top=Top(callobj)+callobj.offsetHeight-37;
	getObj('fc').style.display='';
	getObj('fc').style.zIndex='2';
	
	// First check date is valid
	curdt=document.getElementById(ielem).value;
	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[0];		
		prepcalendar(curdtarr[2],curdtarr[1]-1,curdtarr[0]);
	}
	
}

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) {
   if(evtTgt(EvtObj(e)).style.backgroundColor=='rgb(203, 214, 136)' || evtTgt(EvtObj(e)).style.backgroundColor=='#cbd688') evtTgt(EvtObj(e)).style.backgroundColor='#a77aa8';
   else if(evtTgt(EvtObj(e)).style.backgroundColor=='rgb(247, 250, 225)' || evtTgt(EvtObj(e)).style.backgroundColor=='#f7fae1') evtTgt(EvtObj(e)).style.backgroundColor='#d9a1c3';
   else evtTgt(EvtObj(e)).style.backgroundColor='#f1dae8';
}
function cs_out(e) {
   if(evtTgt(EvtObj(e)).style.backgroundColor=='rgb(167, 122, 168)' || evtTgt(EvtObj(e)).style.backgroundColor=='#a77aa8') evtTgt(EvtObj(e)).style.backgroundColor='#cbd688';
   else if(evtTgt(EvtObj(e)).style.backgroundColor=='rgb(217, 161, 195)' || evtTgt(EvtObj(e)).style.backgroundColor=='#d9a1c3') evtTgt(EvtObj(e)).style.backgroundColor='#f7fae1';
   else evtTgt(EvtObj(e)).style.background='#fff';
}
function cs_click(e) {
	//updobj.value=calvalarr[evtTgt(EvtObj(e)).id.substring(1,evtTgt(EvtObj(e)).id.length)];

	/*********** START redaguojam isvedimo data ***********/
	var mano_masyvas = calvalarr[evtTgt(EvtObj(e)).id.substring(1,evtTgt(EvtObj(e)).id.length)].split('-');
	var mano_metai = mano_masyvas[0];
	
	var mano_menuo = mano_masyvas[1];
	if(mano_menuo.length == 1){// sudarytas is 1 skaiciaus
		mano_menuo = '0'+mano_menuo;
	}

	var mano_diena = mano_masyvas[2];
	if(mano_diena.length == 1){// sudaryta is 1 skaiciaus
		mano_diena = '0'+mano_diena;
	}

	updobj.value=mano_metai+'-'+mano_menuo+'-'+mano_diena;// isvedam pataisyta data
	/*********** END redaguojam isvedimo data ***********/

	getObj('fc').style.display='none';
	
}

var mn=new Array('Sausis','Vasaris','Kovas','Balandis','Gegužė','Birželis','Liepa','Rugpjūtis','Rugsėjis','Spalis','Lapkritis','Gruodis');
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) {//future days
	obj.style.background='#fff';
	obj.style.font='11px Tahoma';
	obj.style.color='#2e3f4d';
	obj.style.textAlign='center';
	obj.style.textDecoration='none';
	obj.style.border='1px solid #cbd688';
	obj.style.cursor='pointer';
}

function f_cpss(obj) {//future days weekends
	obj.style.background='#f7fae1';
	obj.style.font='11px Tahoma';
	obj.style.color='#2e3f4d';
	obj.style.textAlign='center';
	obj.style.textDecoration='none';
	obj.style.border='1px solid #cbd688';
	obj.style.cursor='pointer';
}

function f_cpps(obj) {//past days
	obj.style.background='#fff';
	obj.style.font='11px Tahoma';
	obj.style.color='#ABABAB';
	obj.style.textAlign='center';
	obj.style.border='1px solid #cbd688';
	obj.style.cursor='default';
}

function f_cppss(obj) {//past days weekends
	obj.style.background='#f7fae1';
	obj.style.font='11px Tahoma';
	obj.style.color='#ABABAB';
	obj.style.textAlign='center';
	obj.style.border='1px solid #cbd688';
	obj.style.cursor='default';
}

function f_hds(obj) {//today
	obj.style.background='#cbd688';
	obj.style.font='bold 11px Tahoma';
	obj.style.color='#2e3f4d';
	obj.style.textAlign='center';
	obj.style.border='1px solid #cbd688';
	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();
	if(cd>0) cd=cd-1;
	//alert('cd: '+cd);
	getObj('mns').innerHTML=cy + ' ' + mn[cm]; //month and year
	marr=((cy%4)==0)?mnl:mnn;
	for(var d=1;d<=42;d++) {
  
    if(d%7==6 || d%7==0) f_cpss(getObj('v'+parseInt(d)));
    else 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){
			  if(d%7==6 || d%7==0) f_cppss(getObj('v'+parseInt(d)));
        else f_cpps(getObj('v'+parseInt(d)));				
        }
			else if (htd){
				f_hds(getObj('v'+parseInt(d)));				
        }
			else{			  
				if(d%7==6 || d%7==0) f_cpss(getObj('v'+parseInt(d)));
				else f_cps(getObj('v'+parseInt(d)));				
        }

			getObj('v'+parseInt(d)).onmouseover=(dip)?null:cs_over;
			getObj('v'+parseInt(d)).onmouseout=(dip)?null:cs_out;
			getObj('v'+parseInt(d)).onclick=(dip)?null:cs_click;
			
			getObj('v'+parseInt(d)).innerHTML=d-cd;	
			/*if((d-cd)<10) diena='0'+(cm-(-1));
			else diena=''+(d-cd);
			if((cm-(-1))<10) menuo='0'+(cm-(-1));
			else menuo=''+(cm-(-1));
			calvalarr[d]=cy+'-'+menuo+'-'+diena;*/
			calvalarr[d]=''+cy+'-'+(cm-(-1))+'-'+(d-cd);
		}
		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);

function caddm() {
	marr=((ccy%4)==0)?mnl:mnn;
	
	ccm+=1;
	if (ccm>=12) {
		ccm=0;
		ccy++;
	}
	cdayf();
	prepcalendar('',ccm,ccy);
}

function csubm() {
	marr=((ccy%4)==0)?mnl:mnn;
	
	ccm-=1;
	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;
	}
}