


//############################ kalendoriaus klase ####################


//----------- klases konstruktorius ---------------------------
function createCalendar(objectName)
{
    this.objectName = objectName;

    date = '2005-12-12';
    
    this.time  = '';
    
    this.date = date;
    this.divID = this.objectName + 'formCalendar';
    
    
    document.write('<div id="' + this.divID + '" class="calendar"></div>');
    this.calendarDiv = document.getElementById(this.divID);
    
    this.show = show; 
    this.hide = hide; 
    this.fill = fill; 
    this.daysInMonth = daysInMonth;
    this.dayCell = dayCell;
    this.doReturn = doReturn;
    this.backMonth = backMonth;   
    this.backYear = backYear;   
    this.forwardMonth = forwardMonth;   
    this.forwardYear = forwardYear;   
    this.forwardMonth = forwardMonth;   
    this.forwardYear = forwardYear;   
    this.imageGetRealTop = imageGetRealTop;
    this.imageGetRealLeft = imageGetRealLeft;
    this.checkKeycode2 = checkKeycode2;
    this.onclickas = onclickCheck;
    this.bodyonclickas = bodyonclickCheck;
    
    
    function imageGetRealLeft(imgElem) 
    {
	    //alert(imgElem.src);
		xPos = eval(imgElem).offsetLeft;
	    tempEl = eval(imgElem).offsetParent;
  	    while (tempEl != null) 
        {
  		    //alert(tempEl + ' ' + tempEl.offsetLeft);
			
			xPos += tempEl.offsetLeft;
  		    tempEl = tempEl.offsetParent;
  	    }
		
	    return xPos;
    }    

    function imageGetRealTop(imgElem) 
    {
	    yPos = eval(imgElem).offsetTop;
	    tempEl = eval(imgElem).offsetParent;
	    while (tempEl != null) 
        {
  		    yPos += tempEl.offsetTop;
  		    tempEl = tempEl.offsetParent;
  	    }
	    return yPos;
    }    
    
    function daysInMonth (year, month) 
    {
        return 32 - new Date(year, month, 32).getDate();
    }
    

     function checkKeycode2(e) 
     {
            var keycode;
            if (window.event) keycode = window.event.keyCode;
            else if (e) keycode = e.which;
            
            if(keycode==27 )
            {
                amCal.hide();
            }
     }    
    
    
    function show(field, value)
    {
         this.closas = 1;
         this.first_click = 1;

        tmp = value.split(" ");
        
        if(tmp[1])
            this.time = tmp[1];

        tmp = tmp[0];
        tmp = value.split("-");
        
        this.year = tmp[0] * 1;
        this.month = tmp[1] * 1;
        this.day = tmp[2] * 1;
    
        this.sel_year = this.year;
        this.sel_month = this.month;
        this.sel_day = this.day;        
        
        this.onkeydown = document.onkeydown;
        document.onkeydown = this.checkKeycode2;
        
        //this.onclick = document.body.onclick;
        this.calendarDiv.onclick = this.onclickas;
        
        this.field=field;
        this.img = document.getElementById(this.field + '_cal'); 
        
        y = imageGetRealTop(this.img);
        x = imageGetRealLeft(this.img);
		
		//alert(x+ '' + y);

        this.fill();

        
        this.calendarDiv.style.top = y + 10 + 'px';
        this.calendarDiv.style.left = x + 10 + 'px';
        //this.calendarDiv.style.top = y - 4 + 'px';
        //this.calendarDiv.style.left = '960px';
        
        this.calendarDiv.style.visibility= "visible";
        
        this.document_body_onclick = document.body.onclick;
        document.body.onclick = this.bodyonclickas;
        
        
        
    }    
    
    
    function hide()
    {
        document.onkeydown = this.onkeydown;
        document.body.onclick = this.document_body_onclick;
        
        //alert('onclick' + document.body.onclick);
        
        this.calendarDiv.style.visibility= "hidden";
    }      
    
    function fill()
    {
        t = '';
		
		t += '<div class="cal" >';
		
		t += '<table border="0" cellspacing="0" cellpadding="0" style="width:100%; position:relative;">';
        t += '<tr>';
        t += '<td style="width:20px;"><a href="javascript:' + this.objectName + '.backMonth()"><img src="images/calendar/kalendo-left.gif" alt="" border="0"></a></td>';
        t += '<td style="text-align:center; font:11px Tahoma; color:#111;">' + this.year  + ' ' + str_month[this.month] + '</td>';
        t += '<td style="width:20px; text-align:right;"><a href="javascript:' + this.objectName + '.forwardMonth()"><img src="images/calendar/kalendo-right.gif" alt="" border="0"></a></td>';
        t += '</tr>';
        t += '</table>';
		
		t += '<table border="0" cellspacing="0" cellpadding="0" class="days">';
        t += '<tr>';
        t += '<td class="day">p</td>';
        t += '<td class="day">a</td>';
        t += '<td class="day">t</td>';
        t += '<td class="day">k</td>';
        t += '<td class="day">p</td>';
        t += '<td class="day">š</td>';
        t += '<td class="day">s</td>';
        t += '</tr>';		
		
		i=1;
	    j=1;
        
        kiek = this.daysInMonth(this.year, this.month-1);
		
        
        while( i<= kiek ) 
        {
            if( (j-1) % 7 == 0) 
		    {
			    t += '<tr align="center" >';
		    }
            
            // jeigu pirma diena listinsim -- patikrinsim ar nereikia tusciu celiu pavaryt;
		    if(i==1) 
		    {
			     d=new Date(); d.setDate(1);
                 d.setYear(this.year);
                 d.setMonth(this.month - 1 );
                 d.setDate(1);
                 
                 kelintadienis = d.getDay() ;     
                 
                 k = 1;
                 while(k<kelintadienis)
                 {
                    t += this.dayCell('');
                    k++;
                    j++;
                 }
            }
            
            t += this.dayCell(i);
            j++;
		    i++;
            if( (j+6) % 7 == 0) 
		    {
			    t += "</tr>";
		    }
        
        }
        
      	
        //alert(j);        
        j=j-1;
        if(j % 7 != 0)
        {
            i = j % 7;
            
            while(i<7)
            {
                t += this.dayCell('');  
                i++;
            }
        }
        
        t += '';
        t += '</table>';		
		
		
		t += '</div>';
        

        
        //alert(t);
        this.calendarDiv.innerHTML = t;
        
    
    }
    
    function dayCell(d)
    {
        
        
        if(d=='')
        {
            return '<td style="border:1px solid #fff; background: #fff" >&nbsp;</td>';  
        }
        else
        {
            if(this.sel_year==this.year && this.sel_month==this.month && this.sel_day==d)
                return '<td  style="border:1px solid #fff; background: #fff"><a href="javascript:' + this.objectName + '.doReturn(' + i + ')" style="color:#666; " class="day_sel">' + i + '</a></td>';
            else
                return '<td style=" background: #fff"><a href="javascript:' + this.objectName + '.doReturn(' + i + ')"  >' + i + '</a></td>';
        }
            
    }
    
    function doReturn(i)
    {        
        if(this.month < 10)
            this.month = '0' + this.month;
            
        if(i < 10)
            i = '0' + i;
            
        t = '' + this.year + '-' + this.month + '-' + i;
        
        if( this.time!='')
         t += ' ' + this.time; 
    
        this.hide();
        //field = document.getElementById(this.field);
        //field.value = t;
		renginiaiDate(t);
        
    }
    
    function backMonth()
    {
        if(this.month==1)
        {
            this.month=12;
            this.year--;
        }
        else
        {
            this.month--;
        }
        this.fill();
    }
    
    function backYear()
    {
        this.year--;
        this.fill();
    }
    
    
    function forwardMonth()
    {
        if(this.month==12)
        {
            this.month=1;
            this.year++;
        }
        else
        {
            this.month++;
        }
        this.fill();
    }
    
    function forwardYear()
    {
        this.year++;
        this.fill();
    }
    
    
    function onclickCheck()
    {
        amCal.closas = 0;
        //alert('div closas =' +  amCal.closas);
    }
    
    
    function bodyonclickCheck()
    {
       // alert('body closas = ' + amCal.closas + ' first_click = ' + amCal.first_click );
        if(amCal.closas && !amCal.first_click)
        {
            amCal.hide();
        }
        
        amCal.closas = 1;
        amCal.first_click = 0;
        
        
        
    }    
    
    
    
}   







