
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('.line {border-top-width: 1px;border-top-style: solid;border-top-color: 727272;}');	
document.write('<table id="fc" border="0" style="width:170px;height:170px;position:absolute;border-collapse:collapse;background-image:url(/sitogioco/Immagini/Servizi/quadri_grey.jpg);background-repeat:no-repeat;display:none" cellpadding=2 class="claendarBorderGray">');
document.write('<tr><td style="cursor:pointer" onclick="csubm()"><img src="/sitogioco/Immagini/Servizi/arrowleftmonth.gif"></td><td style="cursor:pointer" onclick="csuby()"><img src="/sitogioco/Immagini/Servizi/arrowleftyear.gif"></td><td colspan="3" id="mns" align="center" style="font:bold 11px Verdana"></td><td align="right" style="cursor:pointer" onclick="caddy()"><img src="/sitogioco/Immagini/Servizi/arrowrightyear.gif"></td><td align="right" style="cursor:pointer" onclick="caddm()"><img src="/sitogioco/Immagini/Servizi/arrowrightmonth.gif"></td></tr>');
document.write('<tr><td class="lineTestCalendar" align=center style="font:12px Verdana"><strong>D</strong></td><td class="lineTestCalendar" align=center style="font:12px Verdana"><strong>L</strong></td><td class="lineTestCalendar" align=center style="font:12px Verdana"><strong>M</strong></td><td  class="lineTestCalendar" align=center style="font:12px Verdana"><strong>M</strong></td><td class="lineTestCalendar" align=center style="font:12px Verdana"><strong>G</strong></td><td class="lineTestCalendar" align=center style="font:12px Verdana"><strong>V</strong></td><td class="lineTestCalendar" align=center style="font:12px Verdana"><strong>S</strong></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('<tr><td colspan="7"class="calendarLinkChiudi"><a onclick="chiudicalendarn();" style="cursor:pointer">Chiudi</a></td></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 sccyx=now.getFullYear();
var ccm=now.getMonth();
var ccyx=now.getFullYear();

ccyx=parseInt(ccyx-18);//SETTO a MENO 18
sccyx=parseInt(sccyx-18);//SETTO a MENO 18


var updobj;
function lcs(ielem) {
	
	updobj=ielem;
	getObj('fc').style.left=Left(ielem) + 'px';
	getObj('fc').style.top=Top(ielem)+ielem.offsetHeight + 'px';
	getObj('fc').style.display='';
	
	//In primo luogo verifico se la data è valida
	curdt=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;
		ccyx=curdtarr[2];
		prepcalendar(curdtarr[0],curdtarr[1]-1,curdtarr[2]);
		
	}
	
	//chiudialtricalendari();
}

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; //Safari bug
	return el;
}
function EvtObj(e){if(!e)e=window.event;return e;}
function cs_over(e) {
	evtTgt(EvtObj(e)).style.background='#7bc143'; //FFCC66
}
function cs_out(e) {
	evtTgt(EvtObj(e)).style.background='transparent';//C4D3EA
}
function cs_click(e) {
	updobj.value=calvalarr[evtTgt(EvtObj(e)).id.substring(1,evtTgt(EvtObj(e)).id.length)];
	getObj('fc').style.display='none';
	
}

var mn=new Array('Gen','Feb','Mar','Apr','Mag','Giu','Lug','Ago','Set','Ott','Nov','Dic');
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='transparent'; //#ffffff sfondo tabella  C4D3EA
	obj.style.font='10px Verdana'; //Arial
	obj.style.color='#333333';
	obj.style.textAlign='center';
	obj.style.textDecoration='none';
	//obj.style.border='1px solid #ffffff'; //bordi delle celle 6487AE
	obj.style.cursor='pointer';
}

function f_cpps(obj) {
	//alert('barrato grigio');
	//obj.style.background='#ffffff'; //sfondo tabella  C4D3EA
	obj.style.font='10px Verdana'; //Arial
	obj.style.color='#ABABAB'; //giorni disabilitati
	obj.style.textAlign='center';
	obj.style.textDecoration='line-through';
	//obj.style.border='1px solid #ffffff'; // bordi delle celle 6487AE
	obj.style.cursor='default';
}

function f_hds(obj) {
	obj.style.backgroundColor ='#FFF799'; //background
	obj.style.font='bold 10px Verdana'; //Arial
	obj.style.color='#333333';
	obj.style.textAlign='center';
	//obj.style.border='1px solid #ffffff'; // bordi delle celle  6487AE
	obj.style.cursor='pointer';
}

// Giorno selezionato
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;
	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==sccyx); //&&(cy==sccyx) SCOMMENTATO IL 1 OTTOBRE
			htd=((hd!='')&&(d-cd==hd));
			if (dip)
				f_cpps(getObj('v'+parseInt(d)));
			else if (htd)
				f_hds(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;	

			var giorno = (d-cd);
            var mese   = (cm-(-1));

            if (giorno <= 9){ 
				giorno = '0' + giorno;}           
			if (mese <= 9){ 
				mese = '0' + mese;} 
				
			calvalarr[d]=''+(giorno)+'/'+(mese)+'/'+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,ccyx);


function caddm() {//Scorre di mese in mese DESTRA	
		marr=((ccyx%4)==0)?mnl:mnn;
		ccm+=1;

	if (ccm>=12) {
		ccm=0;
		ccyx++;
	}
	cdayf(); //cdayf(); blocca il ritorno indietro
	prepcalendar('',ccm,ccyx);
}

function caddy() { //SCORRO  ANNI VERSO DESTRA //##########FUNZIONA######
	
	if ((ccyx==sccyx)&&(ccm<=sccm)){ //||(ccm<sccm)
		return;
	}else{
		(ccyx++);
		prepcalendar('',ccm,ccyx);
	}
} 

function csubm() {  //Scorrimento di MESE in mese SINISTRA 
	marr=((ccyx%4)==0)?mnl:mnn;
	ccm-=1;
	
	if (ccm<0) {
		//ccm=11;
		if(ccyx==1900){ //((ccyx==1901)|(ccyx==1900))
			if(ccm<=0){
				ccm=0;
				return;
			}else{
				//alert("entro else" + ccm);
				ccm=11;
			}
		}else{
			ccm=11;
			ccyx=(ccyx-1);
		}
	}
		//alert('scorro verso sinistra mese' + ccm);
		prepcalendar('',ccm,ccyx);
} 


function csuby() { //SCORRIMENTO ANNI VERSO SINISTRA#########FUNZIONA

	if (ccyx < 1901){
		return;
	}else{
		prepcalendar('',ccm,ccyx=parseInt(ccyx-1));
	}
}

function cdayf() {

if ((ccyx<sccyx)|((ccyx==sccyx)&&(ccm<=sccm))){
	return;
}else {
	ccyx=sccyx;
	ccm=sccm;
	cfd=scfd;
	}
}

function chiudicalendarn() {
//getObjs('fcs').style.display='none';
//document.getElementById('fc').style.display='none';
getObj('fc').style.display='none';
//getObjs('fcc').style.display='none';
}

function chiudialtricalendari() {
	
//getObj('fcs').style.display='none';
getObjs('fc').style.display='none';
getObj('fcc').style.display='none';

}
