var men=new Array(12);
men[0]='Janvāris';
men[1]='Februāris';
men[2]='Marts';
men[3]='Aprīlis';
men[4]='Maijs';
men[5]='Jūnijs';
men[6]='Jūlijs';
men[7]='Augusts';
men[8]='Septembris';
men[9]='Oktobris';
men[10]='Novembris';
men[11]='Decembris';

var wd=new Array(7);
wd[7]="Sv";
wd[1]="P";
wd[2]="O";
wd[3]="T";
wd[4]="C";
wd[5]="P";
wd[6]="S";

var ni=new Array(7);
ni[0]="svētdiena";
ni[1]="pirmdiena";
ni[2]="otrdiena";
ni[3]="trešdiena";
ni[4]="ceturtdiena";
ni[5]="piektdiena";
ni[6]="sestdiena";

var ct = new Date();

function ch_month(m,el_id)
	{
	ct.setFullYear(ct.getFullYear(),ct.getMonth()+m,1);
	showcal(ct,el_id);
	if (el_id!='cal')
		{
		showcal(ct,'cal1');
		showcal(ct,'cal2');
		}
	else showcal(ct,el_id);
	return false;
	}

function ins_date(el_id,txt)
	{
	d.getElementById('event_'+el_id).value=txt;
	if (el_id=='cal2') d.getElementById('event_cal1').value=txt;
	return false;
	}
function cal_click(el)
	{
	var anchors = el.parentNode.parentNode.parentNode.parentNode.getElementsByTagName("a");
 	for (var i=0; i<anchors.length; i++) anchors[i].className='';
	el.className='cal_click'
	}
function showcal(ft,el_id)
	{
	var gads=ft.getFullYear();
	var menesis=ft.getMonth();
	var diena=ft.getDate();	
	var start = new Date();
	start.setFullYear(gads,menesis,1);
	if (start.getDay()!=0) start.setDate(start.getDate()-(start.getDay()-1));
	else start.setDate(start.getDate()-6);
	var end = new Date();
	end.setFullYear(gads,menesis+1,0);
	if (end.getDay()!=0) end.setDate(end.getDate()+(7-end.getDay()));
	
	div=d.getElementById(el_id);
	if (div.childNodes[0]) clearChild(div);
	
	container=ce('div');
	container.className='c';
	py=ce('a');
	py.setAttribute('href','#3');
	py.onclick = new Function("ch_month(-12,'"+el_id+"');return false;");
	py.setAttribute('title',(gads-1));
	ac(py,d.createTextNode('<'));	
	
	ny=ce('a');
	ny.setAttribute('href','#3');
	ny.onclick = new Function("ch_month(12,'"+el_id+"');return false;");
	ny.setAttribute('title',(gads+1));
	ac(ny,d.createTextNode('>'));
	ac(container,py);
	ac(container,d.createTextNode(' '+gads+' '));
	ac(container,ny);
	ac(container,ce('br'));
	
	pm=ce('a');
	pm.setAttribute('href','#3');
	pm.onclick = new Function("ch_month(-1,'"+el_id+"');return false;");
	if (menesis>0) pm.setAttribute('title',(men[menesis-1]));
	else pm.setAttribute('title',(men[11]));
	ac(pm,d.createTextNode('<'));	
	
	nm=ce('a');
	nm.setAttribute('href','#3');
	nm.onclick = new Function("ch_month(1,'"+el_id+"');return false;");
	if (menesis==11) nm.setAttribute('title',(men[0]));
	else nm.setAttribute('title',(men[menesis+1]));
	ac(nm,d.createTextNode('>'));
	ac(container,pm);
	ac(container,d.createTextNode(' '+men[menesis]+' '));
	ac(container,nm);
	
	t=ce('table');
	stils (t,'border-collapse:collapse;');
	t.setAttribute('class','c');
	tb=ce('tbody');
  
  row=ce('tr');
	
	for (nd=1;nd<=7;nd++) 
		{
		cell=ce('td');
		stils(cell,'font-weight:bold;padding:2px;');
		ac(cell,d.createTextNode(wd[nd]));
		ac(row,cell);
		}
	stils(cell,'color:#f33;font-weight:bold;');
	ac(tb,row);
		
	var dat=0;
	while (start<=end)
		{
		if(start.getDate()==1) dat++;
		if (start.getDay()==1) row=ce('tr');	//1diena
		cell=ce('td');
		stils(cell,'width:1.5em;height:1.5em;text-align:right;');
		
		lnk=ce('a');
		stils(lnk,'color:#000;');
		if (dat==0 || dat==2) stils(lnk,'color:#999;display:block;padding:2px;text-decoration:none;');
		else stils(lnk,'display:block;padding:2px;text-decoration:none;')
		cell.onmouseover=new Function ('stils(this,"background-color:#cfc;width:1.5em;height:1.5em;text-align:right;");');
		cell.onmouseout=new Function ('stils(this,"background-color:transparent;width:1.5em;height:1.5em;text-align:right;");');
		if (el_id=='cal')
			lnk.setAttribute('href','#4');
		else 
			{	
			lnk.setAttribute('href','#3');
			lnk.onclick = new Function("cal_click(this);ins_date('"+el_id+"','"+start.getDate()+'.'+(start.getMonth()+1)+'.'+start.getFullYear()+"');return false;");
			}
		lnk.setAttribute('title',ni[start.getDay()]+', '+gads+'.gada '+start.getDate()+'.'+men[menesis].toLowerCase());
		ac(lnk,d.createTextNode(start.getDate()));
		ac(cell,lnk);
		ac(row,cell);
		if (start.getDay()==0)  //7diena
			{
			if (dat==1) stils(lnk,'color:#f33;display:block;padding:2px;text-decoration:none;');
			else stils(lnk,'color:#f99;display:block;padding:2px;text-decoration:none;');
			ac(tb,row);
			}
		var taga = new Date();
		if (start.getYear()==taga.getYear() && start.getMonth()==taga.getMonth() && start.getDate()==taga.getDate()) stils(lnk,'color:#f00;font-weight:bold;display:block;padding:2px;text-decoration:none;'); //shod
		start.setDate(start.getDate()+1);
		}
	ac(t,tb);
  ac(container,t);
  ac(div,container);
	}
