// JScript source code
    function getElementPosition(obj)
    {
      var t = 0;
      var l = 0;
      
      if (obj.offsetParent)
      {
        while (obj.offsetParent)
        {
            t += obj.offsetTop;
            l += obj.offsetLeft;
            obj = obj.offsetParent; 
        }
      }
      else if (obj.y && obj.x)
      {
        t += obj.y;
        l += obj.x;
      }
      return {left : l, top : t};
    } 

    function setupSchermoW(){
	    j = screen.width/2
	    return j
    }

    function setupSchermoH(){
	    j = screen.height/2
	    return j
    }
	
    function View_Calendario(posizione){
        var SiNo
        SiNo = document.all.livello1.style.visibility 
   
        today = new Date()
        document.all["T1"].value = posizione
        
        //da customizzare in base all'uso
        //document.all["DA"].value = ""
        //document.all["A"].value = (today.getMonth()+1) +"/"+ today.getDate() +"/"+ today.getYear()
      
        if (SiNo == "hidden"){
            if (document.all["X"].value == "") document.all["X"].value = "0"
            if (document.all["Y"].value == "") document.all["Y"].value = "0"
 
            document.all.livello1.style.left = (setupSchermoW()+parseInt(document.all["X"].value))
            document.all.livello1.style.top = (setupSchermoH()+parseInt(document.all["Y"].value))

            document.all.livello1.style.visibility = 'visible'
        }

        if (SiNo == "visible"){ 
            document.all.livello1.style.visibility = 'hidden'
	    }

    }

    function View_CalendarioNew(posizione, splitX, splitY){
        var SiNo
        SiNo = document.all.livello1.style.visibility 
   
        today = new Date()
        document.all["T1"].value = posizione
        
        //da customizzare in base all'uso
        //document.all["DA"].value = ""
        //document.all["A"].value = (today.getMonth()+1) +"/"+ today.getDate() +"/"+ today.getYear()
      
        if (SiNo == "hidden"){
            if (document.all["X"].value == "") document.all["X"].value = "0"
            if (document.all["Y"].value == "") document.all["Y"].value = "0"


            document.all.livello1.style.left = parseInt(getElementPosition(document.getElementById(posizione)).left) + parseInt(splitX)
            document.all.livello1.style.top =  parseInt(getElementPosition(document.getElementById(posizione)).top) + parseInt(splitY)

            document.all.livello1.style.visibility = 'visible'
        }

        if (SiNo == "visible"){ 
            document.all.livello1.style.visibility = 'hidden'
	    }

    }
		
    function leapYear(year) {
	    if (year % 4 == 0) 
	        return true
	    	return false 
    }

    function y2k(number) { 
        return (number < 1000) ? number + 1900 : number; 
    }
    
	var now = new Date();
	var month_array = new Array("Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre");
		
	document.write("<table cellspacing='0' cellpadding='2' border='0' style='border-width:1px;border-style:solid;font-family:Verdana;font-size:8pt;border-collapse:collapse;'>")
	document.write("<tr><td colspan='7' style='background-color:#D90323;'><table cellspacing='0' border='0' style='color:#F1F1F5;font-family:Verdana;font-size:8pt;width:100%;border-collapse:collapse;'>")
		 
	document.write("<tr><td style='width:15%;'><a title='muove mese' href=\"javascript: month_down();\" class=\"k\" style='color:Black'><img border=0 src=../ImmaginiProgramma/Back.gif></a><a title='muove anno' href=\"javascript: year_down();\" class=\"k\" style='color:Black'><img border=0 src=../ImmaginiProgramma/Back.gif></a></td><td id=dat1 align='Center' style='width:30%;'>&nbsp;&nbsp;" + month_array[now.getMonth()] + "</td><td id=dat2 align='Center' style='width:30%;'>" + y2k(now.getYear()) + "&nbsp;&nbsp;</td><td align='Right' style='width:15%;'><a title='muove anno' href=\"javascript: year_up();\" class=\"k\" style='color:Black'><img border=0 src=../ImmaginiProgramma/Next.gif></a><a title='muove mese' href=\"javascript: month_up();\" class=\"k\" style='color:Black'><img border=0 src=../ImmaginiProgramma/Next.gif></a></td></tr>")
		 
	document.write("</table></td></tr><tr><td align='Center' style='color:#F1F1F5;background-color:#999999;'>lun</td><td align='Center' style='color:#F1F1F5;background-color:#999999;'>mar</td><td align='Center' style='color:#F1F1F5;background-color:#999999;'>mer</td><td align='Center' style='color:#F1F1F5;background-color:#999999;'>gio</td><td align='Center' style='color:#F1F1F5;background-color:#999999;'>ven</td><td align='Center' style='color:#F1F1F5;background-color:#999999;'>sab</td><td align='Center' style='color:#F1F1F5;background-color:#999999;'>dom</td></tr><tr><td nowrap='nowrap' align='Center' style='color:#F1F1F5;width:14%;'></td></tr><tr>")
		
	for(j=0;j<6;j++){
	    for(i=0;i<7;i++){
	        document.write("<td align=center id=d"+i+"r"+j+"></td>")
	    }
	    document.write("</tr>");
	}
	
	document.write("</table>");
	show_date = new Date();
		
	function set_cal(show_date){
		begin_day = new Date (y2k(show_date.getYear()),show_date.getMonth(),1);
		begin_day_date = begin_day.getDay();
		end_day = new Date (y2k(show_date.getYear()),show_date.getMonth()+1,1);
		
		var pd = new Array(12)
		pd[0] = 31 // Gennaio
		pd[1] = (leapYear(show_date.getYear())) ? 29 : 28 // Febbraio utile per gli anni bisestili
		pd[2] = 31 // Marzo
		pd[3] = 30 // Aprile
		pd[4] = 31 // Maggio
		pd[5] = 30 // Giugno
		pd[6] = 31 // Luglio
		pd[7] = 31 // Agosto
		pd[8] = 30 // Settembre
		pd[9] = 31 // Ottobre
		pd[10] = 30 // Novembre
		pd[11] = 31 // Dicembre
		
		rok = show_date.getFullYear();
		if ((rok%4 == 0 && rok%100 != 0) || (rok%400 == 0)) pd[1]=29;
		count_day = pd[show_date.getMonth()];
		input_table(begin_day_date,count_day);
	}
		
	set_cal(show_date);
		
	function input_table(begin,count){
		init();
		j=0;
		
		if (begin!=0){
		    i=begin-1;
		}
		else{
		i=6
		}
		for (c=1;c<count+1;c++){
		    colum_name = "d"+i+"r"+j;
		    if ((now.getDate() == c)&&(show_date.getMonth() == now.getMonth())&&(y2k(show_date.getYear()) == y2k(now.getYear()))) {
		        document.getElementById(colum_name).style.backgroundColor = "#006C9C";document.getElementById(colum_name).style.color = "blue";
		    };
		    tmpkmont=show_date.getMonth()+1;
		    if (tmpkmont<10) kmont = "0"+tmpkmont; else kmont=tmpkmont; 
		    if (c<10) kc = "0"+c; else kc=c; 
		    kdat=kc+"."+kmont+"."+y2k(show_date.getYear());
		
		    document.getElementById(colum_name).innerHTML =  "<a href=javascript:calendario('"+y2k(show_date.getYear())+"',"+kmont+","+kc+"); class=\"k\" style='color:blue;'>"+c+"</a>";
		
		    i++;
		    if (i==7){
		        i=0;
		        j++;
		    }
		}
		
		document.getElementById('dat1').innerHTML = month_array[show_date.getMonth()];
		document.getElementById('dat2').innerHTML = y2k(show_date.getYear());
	}
		
	function init(){
		for(j=0;j<6;j++){
		    for(i=0;i<7;i++){
		        colum_name = "d"+i+"r"+j;
		        document.getElementById(colum_name).innerHTML =  "-";
		        document.getElementById(colum_name).style.backgroundColor ="";
		        document.getElementById(colum_name).style.color = "black";
		     }
		}
	}
		
	function change_month(sel_month){
	    show_date = new Date(y2k(show_date.getYear()), sel_month, 1);
	    set_cal(show_date);
	}
		
	function change_year(sel_year){
	    sel_year = sel_year.value;
	    show_date = new Date(sel_year,show_date.getMonth(),1);
	    set_cal(show_date);
	}
		
	function change_today(){
	    show_date = now;
	    set_cal(show_date);
	}
	
	function year_up(){
	    par=y2k(show_date.getYear()) + 1;
	    show_date = new Date(par,show_date.getMonth(),1);
	    set_cal(show_date);
	}
		
	function year_down(){
	    par=y2k(show_date.getYear()) - 1;
	    show_date = new Date(par,show_date.getMonth(),1);
	    set_cal(show_date);
	}
		
	function month_up(){
	    par=show_date.getMonth() + 1;
	    if (par>11){
	        par=0;
		    par2=y2k(show_date.getYear()) + 1;
		}
		else{
		    par2=y2k(show_date.getYear());
		}
		show_date = new Date(par2,par,1);
		set_cal(show_date);
	}
		
	function month_down(){
	    par=show_date.getMonth() - 1;
	    if (par<0){
	        par=11;
	        par2=y2k(show_date.getYear()) - 1;
	    }
	    else{
	        par2=y2k(show_date.getYear());
	    }
	    show_date = new Date(par2,par,1);
	    set_cal(show_date);
	}
		
	function calendario(anno, mese, giorno){
		var str = giorno +"/"+ mese +"/"+ anno
		
		SETT_date = new Date(mese +"/"+ giorno +"/"+ anno)
			
		document.all[document.all["T1"].value].value = SETT_date.getDate() +"/"+ (SETT_date.getMonth()+1) +"/"+ SETT_date.getYear()
		var event;
		if (window.ActiveXObject) {
		    event= document.createEventObject();
            document.all[document.all["T1"].value].fireEvent("onchange",event);
        } else {
            event = document.createEvent("HTMLEvents");
            event.initEvent(eventType, "true", "true");
            document.all[document.all["T1"].value].dispatchEvent(event);
        }
                 	
		if (document.all["DA"].value != ""){
			giorno = (document.all["DA"].value).substring(0,(document.all["DA"].value).indexOf("/",0))
   			mese = (document.all["DA"].value).substring(((document.all["DA"].value).indexOf("/",0)+1),(document.all["DA"].value).indexOf("/",((document.all["DA"].value).indexOf("/",0)+1)))
    		anno = (document.all["DA"].value).substring(((document.all["DA"].value).length - 4),(document.all["DA"].value).length) 
      			
			CTRL1_date = new Date(mese +"/"+ giorno +"/"+ anno)
				
			if (SETT_date < CTRL1_date)
				document.all[document.all["T1"].value].value = ""	
		}
			
		if (document.all["A"].value != ""){
			mese = (document.all["A"].value).substring(0,(document.all["A"].value).indexOf("/",0))
   			giorno = (document.all["A"].value).substring(((document.all["A"].value).indexOf("/",0)+1),(document.all["A"].value).indexOf("/",((document.all["A"].value).indexOf("/",0)+1)))
    		anno = (document.all["A"].value).substring(((document.all["A"].value).length - 4),(document.all["A"].value).length)  
      			
			CTRL2_date = new Date(mese +"/"+ giorno +"/"+ anno)
		
			if (SETT_date > CTRL2_date)
				document.all[document.all["T1"].value].value = ""
							
		}
			
		document.all.livello1.style.visibility = 'hidden'
	}


