//////////////////////////////////////////////////////////////////////////
///
/// Kalenteri kontrollin lhde koodi 
///
///////////////////////////////////////////////////////////////////////////
 function WebCalender(CalenderVariable , valueYear,valueMonth,valueDay, buttonElement)
 {
    //private fields
    var table;
    var header;
    var oBody;
    var cells = new Array();
    var currentDay;
    var imageNext;
    var imageBefore;
    var currentDate; 
    var currentMonth;
    var currentYear;
    var space = '&nbsp;';
    var headerCell; 
    var selectedDay; 
    var base = this; 
    var informationCell; 
    var busy = false;
    var cellIndex;
    var rowIndex;
    var cancelEvent = false;

    var mainDiv;
    var bgFrame;
    
    //document.oncontextmenu = new Function('return false;'); 
    // public fields 
    this.dateNames = ['','','','','','',''];
    this.monthNames = ['','','','','','','','' ,'','','',''];
    this.defaultInformation = '';
    this.previousYearInformation = '';
    this.nextYearInformation = '';
    this.previousMonthInformation = '';
    this.nextMonthInformation = '';
    this.todayInformation = '';
    this.previousYearLabel = '<<<';
    this.nextYearLabel = '>>';
    this.previousMonthLabel = '<<';
    this.nextMonthLabel = '>';
    this.todayLabel = '';
    this.initialize = initialize;
    this.url = document.location.href; 
    this.closeWithChange;
    this.canClose = true;
    this.open = open;
    this.hide = hide;
    this.noEvent = false;
    
    // puplic methods
    this.setDate = setDate;
    this.setDay = setDay;
    this.setMonth = setMonth;
    this.setYear = setYear;
    this.fireEvent = fireEvent;
    
    // public events 
    this.onChange = new Function();
    this.getValue = new Function();
    var base = this;
    var checkValue ; 
    
    // konstructor 
    if (typeof(CalenderVariable) == 'string' )
    {
        oBody = document.getElementById(CalenderVariable);
    }
    else 
    {
        oBody = CalenderVariable;
    }
    
    function initialize()
    {
        // rakentaa kalenterin 
        if (!valueYear)
        {
            setDate(new Date());
        }
        else 
        {
            var valuedate = new Date(); 
            if (valuedate == 'NaN')
            {
                valuedate = new Date(); 
            }
            valuedate.setFullYear(valueYear,valueMonth - 1 ,valueDay);
            selectedDay = valuedate.getDate();
            setDate(valuedate);
            valueChange(selectedDay);
        }
        if (base.closeWithChange)
        {
            table.style.display = 'none';
            mainDiv.style.display = 'none';
        }
        
        if (!window.cals)
        {
            window.cals = new Array();
        }
        window.cals[window.cals.length] = base;
        
        
        if (base.canClose)
        {
            document.attachEvent('onpropertychange', base.hide);
            if (window.frameElement)
            {
                window.parent.document.attachEvent('onpropertychange', base.hide);
                if (window.parent.frameElement)
                {
                    window.parent.frameElement.attachEvent('onpropertychange', base.hide);
                }
            }
        }
    }
    
    ////// Private functions and methods       
    function valueChange(value)
    {
        selectedDay = value;
        if (!base.noEvent)
        { 
            base.onChange(currentYear, currentMonth , value);
        }
    }
    function hide(e)
    {
        //if (table.style.display != 'none')
        if (mainDiv.style.display != 'none')
        {
            if (!checkValue)
            {
                cancelEvent = false;
                checkValue = window.setTimeout(closeWhenTime,250);
                function closeWhenTime(){
                    if (!cancelEvent)
                    {
                        table.style.display = 'none';
                        mainDiv.style.display = 'none';                        
                    }
                    checkValue = null;
                }
            }
        }
    }
    function open()
    {
        //if (table.style.display == 'none')
        if (mainDiv.style.display == 'none')
        {
            table.style.display = '';
            mainDiv.style.display = '';
            moveCalender();
            
            var arr = base.getValue();
            if (arr)
            {
                if (arr[0] == 'NaN'){arr[0]  = '';}
                if (arr[1] == 'NaN'){arr[1]  = '';}
                if (arr[2] == 'NaN'){arr[2]  = '';}
                var valuedate = new Date(); 
                valuedate.setFullYear(arr[0],arr[1] - 1 ,arr[2]);
                if (valuedate == 'NaN')
                {
                    valuedate = new Date(); 
                } 
                selectedDay = valuedate.getDate();
                base.noEvent = true;
                base.canClose = false;
                setDate(valuedate);
                base.noEvent = false;
                base.canClose = true;
            }
        }
    }
    
    function moveCalender()
    {
        if (buttonElement)
        {
            var x= 0;
            var y = 0; 
            y = getAbsoluteTop(buttonElement) + buttonElement.offsetHeight; 
		    x = getAbsoluteLeft(buttonElement) - table.offsetWidth + 35; 
		    if (top.window.document != document){ 	
		        y = y + getAbsoluteTop(window.frameElement);
		        x = x + getAbsoluteLeft(window.frameElement);
		        y = y + getAbsoluteTop(window.parent.frameElement);
		        x = x + getAbsoluteLeft(window.parent.frameElement);
		        y = y + getAbsoluteTop(window.parent.parent.frameElement);
		        x = x + getAbsoluteLeft(window.parent.parent.frameElement);
		        y = y + getAbsoluteTop(window.parent.parent.parent.frameElement);
		        x = x + getAbsoluteLeft(window.parent.parent.parent.frameElement);
   			    if(document.body.scrollTop){
                    y = y - document.body.scrollTop; 
                    x = x - document.body.scrollLeft; 
                    y = y - window.parent.window.document.body.scrollTop; 
                    x = x - window.parent.window.document.body.scrollLeft; 
                    y = y - window.parent.parent.window.document.body.scrollTop; 
                    x = x - window.parent.parent.window.document.body.scrollLeft; 
                    y = y - window.parent.parent.parent.window.document.body.scrollTop; 
                    x = x - window.parent.parent.parent.window.document.body.scrollLeft;             
   			    }
		    }else{
		        if(document.body.scrollTop){
		            y = y + document.body.scrollTop; 
			        x = x + document.body.scrollLeft; 
		        }
		    }
		    if (x < 0){x = 0}
		    if (y < 0){y = 0}
            if ((y + table.offsetHeight) > top.document.body.scrollHeight)
            {
                y = y - table.offsetHeight  - 22;
            }
            if ((x + table.offsetWidth) > top.document.body.scrollWidth)
            {
                x = top.document.body.scrollWidth - table.offsetWidth;
            }
            /*table.style.position = 'absolute';
            table.style.top = y + 3;
            table.style.left = x;*/
            mainDiv.style.position = 'absolute';
            mainDiv.style.top = y + 3;
            mainDiv.style.left = x;
        }
        cancelEvent = true;
    }
    
    
    function setDate(value)
    {
        currentDate = value;
        
        currentDay = new Date().getDate();
        currentMonth = currentDate.getMonth()+1;
        currentYear = currentDate.getFullYear();        
                
        var header = base.monthNames[currentDate.getMonth()] + ', ' + currentYear; 
        
        // rakentaa kalenterin 
        paintCalender();    
        
        updateHeader(header);
    }
    
    function setDay(value)
    {
        currentDate.setFullYear(currentYear,currentMonth - 1,value);
        setDate(currentDate);
    }
    function setMonth(value)
    {
        base.canClose = false;
        currentDate.setFullYear(currentYear,value - 1,currentDay);
        setDate(currentDate);
        base.canClose = true;
    }
    function setYear(value)
    {
        base.canClose = false;
        currentDate.setFullYear(value,currentMonth - 1,currentDay);
        setDate(currentDate);
        base.canClose = true;
    }
    
    
    function updateHeader(value)
    {
        headerCell.innerHTML = value;
    }

    function updateInformation(value) 
    {
        var iheight_tmp = informationCell.offsetHeight;
        var offset;
        informationCell.innerHTML = value;
        
        // Muutetaan samalla iframen korkeutta(tekstin pituudesta riippuen informationCell:n korkeus vaihtelee)
        if (mainDiv != null) {            
            if(informationCell.offsetHeight > iheight_tmp)
                offset = (informationCell.offsetHeight - iheight_tmp) + 4;
            else
                offset = (informationCell.offsetHeight - iheight_tmp) - 4;
            var iframe_height = parseInt(bgFrame.style.height) ;
            iframe_height += offset;
            if(offset != -4 && offset != 4)
                bgFrame.style.height = iframe_height;
        }
    }
    
    // private functions
    function paintCalender()
    {
        var numberOfWeeks = 6;
        var row;
        var cell;
        var hrow;
        var hcell;
        var fixWeekDay;
        var numberOfDays;
        var thisMonth; 
        var thisYear; 
        var selectedCell; 
        
        busy = true;
        if (table)        
        {
            //oBody.removeChild(table);
            //top.document.body.removeChild(table);
            top.document.body.removeChild(mainDiv);
            table = null;
            mainDiv = null;
            try{CollectGarbage();}catch(e){}
        }
        
        fixWeekDay = getFirstWeekDayOfMonth(currentDate) * -1;
        numberOfDays = getDaysOfMonth(currentDate);
        
        // asetetaan tarkastus arvotjotta ei tarte hakea naita loopissa 
        thisMonth = new Date().getMonth() +1 ; 
        thisYear = new Date().getFullYear();
          
        // luodaan table
        table = top.document.createElement('table');
        table.border = 0;
        table.cellPadding=0;
        table.cellSpacing=0;
        
        table.className = 'CalenderContent';

        if (window.addEventListener)
        { 
           document.addEventListener('keyup' , controlKeyUp , false);
        }
        else
        { 
           table.onkeydown = controlKeyUp;      
        }

            
        row = table.insertRow(table.rows.length);
        cell = row.insertCell(row.cells.length);
        cell.colSpan=7;
        cell.style.textAlign = 'center';
        cell.style.width = '100%';

        header = top.document.createElement('table');
        hrow = header.insertRow(header.rows.length);
        header.style.width = '100%';

        // kuva edellinen vuosi 
        hcell = hrow.insertCell(hrow.cells.length);
        hcell.className = 'CalenderPreviousYear';
        hcell.innerHTML = base.previousYearLabel;
        hcell.onclick = goPreviousYear;
        hcell.style.textAlign = 'center';
        hcell.informationText = base.previousYearInformation;
        hcell.onmouseover = menuMouseOver;
        hcell.onmouseout = menuMouseOut;
        if (!base.canClose) hcell.onmousedown = openMenu;
        if (!base.canClose) hcell.menu = new dropMenu(getMenuYears(true));
        if (!base.canClose) hcell.menu.onSelect = setYear;
        
        
        // kuva edellinen kuukausi 
        hcell = hrow.insertCell(hrow.cells.length);
        hcell.className = 'CalenderPreviousMonth';
        hcell.innerHTML = base.previousMonthLabel;
        hcell.onclick = goPreviousMonth;
        hcell.style.textAlign = 'center';
        hcell.informationText = base.previousMonthInformation;
        hcell.onmouseover = menuMouseOver;
        hcell.onmouseout = menuMouseOut;
        if (!base.canClose)hcell.onmousedown = openMenu;
        if (!base.canClose)hcell.menu = new dropMenu(getMenuMonths(true));
        if (!base.canClose)hcell.menu.onSelect = setMonth;
        
                        
        // tama paiva 
        hcell = hrow.insertCell(hrow.cells.length);
        hcell.className = 'CalenderToDay';
        hcell.onclick = goThisDate;
        hcell.innerHTML = base.todayLabel;
        hcell.style.textAlign = 'center';
        hcell.informationText = base.todayInformation;
        hcell.onmouseover = menuMouseOver;
        hcell.onmouseout = menuMouseOut;
        
        // kuva seuraava kuukausi 
        hcell = hrow.insertCell(hrow.cells.length);
        hcell.className = 'CalenderNextMonth';
        hcell.innerHTML = base.nextMonthLabel;
        hcell.onclick = goNextMonth;
        hcell.style.textAlign = 'center';
        hcell.informationText = base.nextMonthInformation;
        hcell.onmouseover = menuMouseOver;
        hcell.onmouseout = menuMouseOut;
        if (!base.canClose)hcell.onmousedown = openMenu;
        if (!base.canClose)hcell.menu = new dropMenu(getMenuMonths(false));
        if (!base.canClose)hcell.menu.onSelect = setMonth;
        
        // kuva seuraava vuosi
        hcell = hrow.insertCell(hrow.cells.length);
        hcell.className = 'CalenderNextYear';
        hcell.innerHTML = base.nextYearLabel;
        hcell.onclick = goNextYear;
        hcell.style.textAlign = 'center';
        hcell.informationText = base.nextYearInformation;
        hcell.onmouseover = menuMouseOver;
        hcell.onmouseout = menuMouseOut;
        if (!base.canClose)hcell.onmousedown = openMenu;
        if (!base.canClose)hcell.menu = new dropMenu(getMenuYears());
        if (!base.canClose)hcell.menu.onSelect = setYear;

        cell.appendChild(header);
        cells[cells.length] = cell;

        for (var line=0;line<numberOfWeeks + 1 ;line++)
        {
            row = table.insertRow(table.rows.length);
            row.index = line;
            if (line > 0)
            {
                row.onmouseover = weekMouseOver;
                row.onmouseout = weekMouseOut;
            }
            
            for (var col = 0; col < 7;col++)
            {
                cell = row.insertCell(row.cells.length);
                cell.index = col;
                if (line == 0)
                {
                    cell.innerHTML = base.dateNames[col];
                    if (col >= 5)
                    {
                        cell.className = 'CalenderWeekend CalenderDateNames';
                    }
                    else 
                    {
                        cell.className = 'CalenderDateNames';
                    }
                }
                else 
                {
                    // on typerasti mutta ok 
                    var label = (((line-1)*7) + parseInt(col + 1)) + fixWeekDay;
                    if (label > numberOfDays | label < 1)
                    {
                        cell.innerHTML = space;
                        cell.className = 'CalenderEmptyCell';
                    } 
                    else
                    {
                        cell.innerHTML = label.toString();
                        if (selectedDay)
                        {
                            if (label.toString() == selectedDay.toString())
                            {
                                selectedCell = cell;
                            }
                        }
                        
                        if (col >= 5)
                        {
                            cell.className = 'CalenderWeekend';
                        }
                        else 
                        {
                            cell.className = 'CalenderNormal';
                            cell.current = false; 
                        }
                        
                        if ( thisYear == currentYear && thisMonth == currentMonth && (((line-1)*7) + parseInt(col + 1)) == currentDay  - fixWeekDay )
                        {
                            cell.className += ' CalenderCurrentNormal';
                            cell.current = true;
                        }
                        cell.onclick = mouseClick;
                        cell.onmouseover = dayMouseOver;
                        cell.onmouseout = dayMouseOut;
                    }
                    cell.selected = false;
                    cell.row = row;
                    cell.origClassName = cell.className;
                    cells[cells.length] = cell;
                } 
                cell.style.padding = 1;    
                cell.style.textAlign = 'center';
            } 
        }
        
        // Lisataan informaatio kentta taulukon alkuun 
        row = table.insertRow(0);
        headerCell = row.insertCell(0);
        headerCell.innerHTML = "Information";
        headerCell.colSpan = 7;
        headerCell.style.textAlign = 'center';
        headerCell.className = 'CalenderInformationHeader';
        
        row = table.insertRow(table.rows.length);
        informationCell = row.insertCell(0);
        informationCell.innerHTML = "Information text";
        informationCell.colSpan = 7;
        informationCell.style.textAlign = 'center';
        informationCell.className = 'CalenderInformation';

        updateInformation(base.defaultInformation );

        table.style.position = 'absolute';
        
        // luodaan paa-div
        mainDiv = top.document.createElement('div');

        // luodaan kalenteri-div
        var calDiv = top.document.createElement('div');
        calDiv.style.position = 'absolute';
        
        // luodaan frame-div
        var frameDiv = top.document.createElement('div');      
        
        //mainDiv.style.display = 'none';
        
        // luodaan iframe kalenterin taakse
        bgFrame = top.document.createElement('iframe');
        bgFrame.src = 'dummy.htm';
        bgFrame.id = 'bgFrame';
        bgFrame.style.position = 'absolute';
        bgFrame.style.left = 0;
        bgFrame.style.top = 0;
                   
        // lisataan kalenteri divin sisaan
        //top.document.body.appendChild(table);
        frameDiv.style.zIndex = 0;

        frameDiv.appendChild(bgFrame);
        calDiv.appendChild(table);
                
        mainDiv.appendChild(frameDiv);
        mainDiv.appendChild(calDiv);
        
        top.document.body.appendChild(mainDiv);

        // kalenterin leveys on nyt tiedossa, asetetaan samat arvot myos iframelle
       try{
            bgFrame.style.width = table.currentStyle.width;
            bgFrame.style.height = (numberOfWeeks + 4) * 16; //table.currentStyle.height;
        }catch(e){}
       
        moveCalender();       
        
        if (selectedCell)
        {
            selectedCell.onclick();
        }
        try{CollectGarbage();}catch(e){}
        busy = false;
    }

    // tapahtuukunsoluaklikataan 
    function mouseClick()
    {
        this.style.padding = 0;
        if (this.selected)
        {
            this.selected = false;
            this.className = this.origClassName;
            rowIndex = null;
            cellIndex = null;    
            valueChange('');
        }
        else 
        {
            // poistetaan kaikki vanhat valinnat 
            for (var i = 0;i<cells.length ;i++)
            {
                if (cells[i] != this)
                { 
                    cells[i].className = cells[i].origClassName;
                    cells[i].selected = false;
                }
            }
            valueChange(this.innerHTML);
            this.selected = true;
            if(this.current)
            { 
                // asetetaan valituksi 
                this.className += ' CalenderCurrentSelected';
            }
            else
            {
                // asetetaan valituksi 
                this.className += ' CalenderSelected';
            }
            
            if (this.row)
            {
                rowIndex = this.row.index;
                cellIndex = this.index;
            }
        }
        // laitetaan viela samoin kuin oli ennen kutsua 
        if (this.row)
        {
            if (!busy)
            {
                this.row.onmouseover();
                this.onmouseover();
            }
        }
        if (base.closeWithChange)
        {
            if (base.canClose)
            {
                base.open();
            }
        }
    }
  
  
    function weekMouseOver()
    {
        this.className = 'CalenderMouseOverWeek';
        for (var i=0;i<this.cells.length;i++)
        {
            this.cells[i].className = 'CalenderMouseOverWeek ' + this.cells[i].className;
        }   
    }
    function weekMouseOut()
    {
        this.className = '';
        for (var i=0;i<this.cells.length;i++)
        {
            cleanClassNames(this.cells[i]);   
        }
    }
    
    
    function dayMouseOver()
    {
        this.className += ' CalenderMouseOverDay' ;   
        this.style.padding =0;
        updateInformation(this.innerHTML + '.' + currentMonth + '.' + currentYear);
    }
    
    function dayMouseOut()
    {
        cleanClassNames(this);   
        if (!this.selected)
        {
            this.style.padding = 1;
        }
        updateInformation(base.defaultInformation);
    }
    
    function menuMouseOver()
    {
        updateInformation(this.informationText);
    }        
    function menuMouseOut()
    {
        updateInformation(base.defaultInformation);
    }
    
    function goPreviousMonth()
    {
        cancelEvent = true;   
        setMonth(currentMonth - 1);
    }
  
    function goNextMonth()
    {
        cancelEvent = true;
        setMonth(currentMonth + 1);
    }
    
    function openMenu(e)
    {
        cancelEvent = true;
        if (!e)
        {
            e = window.event; 
        }
        if (!e)
        {
            e = top.window.event; 
        }
        
        if (e.button == 2)
        {
            
            // Haetaan paikka jossa hiiri on meua avatessa 
		    var x = 0;
		    var y = 0;
    		
		    // selvitetaaan absoluuttinen paikka jonne menu aukaistaan 
		    y = getAbsoluteTop(this);
		    x = getAbsoluteLeft(this);
		    y +=  this.offsetHeight; 
		    if (top.window.document != document){ 	
		        y = y + getAbsoluteTop(window.frameElement);
		        x = x + getAbsoluteLeft(window.frameElement);
		        y = y + getAbsoluteTop(window.parent.frameElement);
		        x = x + getAbsoluteLeft(window.parent.frameElement);
		        y = y + getAbsoluteTop(window.parent.parent.frameElement);
		        x = x + getAbsoluteLeft(window.parent.parent.frameElement);
		        y = y + getAbsoluteTop(window.parent.parent.parent.frameElement);
		        x = x + getAbsoluteLeft(window.parent.parent.parent.frameElement);
   			    if(document.body.scrollTop){
                    y = y - document.body.scrollTop; 
                    x = x - document.body.scrollLeft; 
                    y = y - window.parent.window.document.body.scrollTop; 
                    x = x - window.parent.window.document.body.scrollLeft; 
                    y = y - window.parent.parent.window.document.body.scrollTop; 
                    x = x - window.parent.parent.window.document.body.scrollLeft; 
                    y = y - window.parent.parent.parent.window.document.body.scrollTop; 
                    x = x - window.parent.parent.parent.window.document.body.scrollLeft;             
   			    }
		    }else{
		        if(document.body.scrollTop){
		            y = y + document.body.scrollTop; 
			        x = x + document.body.scrollLeft; 
		        }
		    }
		    if (x < 0){x = 0}
		    if (y < 0){y = 0}
    	    cancelEvent = true;
    	    // avataan menu 
            this.menu.open(x,y); 
            cancelEvent = true;
        }
    }
    
    function goPreviousYear(year)
    {    
        cancelEvent = true;
        setYear(currentYear - 1);
    }
  
    function goNextYear()
    {
        cancelEvent = true;
        setYear(currentYear + 1);
    }
    
    function goThisDate()
    {
        cancelEvent = true;
        var newDate = new Date();
        selectedDay = newDate.getDate();
        setDate(newDate);
    }
    
    function checkCell(cell)
    {
        if (!cell)
        {
            return false;
        }
        else if (!cell.onclick)
        {
            return false;
        }
        else 
        {
            return true; 
        }
    }
    
    // tapahtuma nappain ylas     
    function controlKeyUp(e)
    {
        var key = setKeyMask(e);
        var cell; 
        var memCellIndex = cellIndex; 
        var memRowindex = rowIndex;  
        
        if (rowIndex)
        {
            
            if (key == 37)
            {
                cell = table.rows[memRowindex+2].cells[memCellIndex - 1 ];
            }
            else if (key == 39)
            {
                cell = table.rows[memRowindex+2].cells[memCellIndex + 1];
            }
            else if (key == 38)
            {
                // tyhjennetaan mouse over toiminto 
                try
                {
                    table.rows[memRowindex+2].cells[memCellIndex].onmouseout();
                    table.rows[memRowindex+2].onmouseout();
                }
                catch(e){}
                cell = table.rows[memRowindex+2 - 1].cells[memCellIndex];
                if (!checkCell(cell))
                {
                    goPreviousMonth();
                    // haetaan ensimmainen validi rivi 
                    for(var i=8;i>1;i--)
                    {
                        cell = table.rows[i].cells[memCellIndex];    
                        if (cell.onclick)
                        {
                            break;
                        }
                    }
                }
            }
            else if (key == 40)
            {
                 // tyhjennetaan mouse over toiminto 
                try
                {
                    table.rows[memRowindex+2].cells[memCellIndex].onmouseout();
                    table.rows[memRowindex+2].onmouseout();
                }
                catch(e){}
                cell = table.rows[memRowindex+2 + 1].cells[memCellIndex];
                if (!checkCell(cell))
                {
                    goNextMonth();
                    for(var i=2;i<8;i++)
                    {
                        cell = table.rows[i].cells[memCellIndex];    
                        if (cell.onclick)
                        {
                            break;
                        }
                    }
                }
            }
            
            if (cell)
            {
                if (cell.onclick)
                {
                    cell.onclick();
                    cell.focus();
                }
            }
        }
    }
        
    function getMenuYears(previous)
    {
        var years = new Array(); 
        if (previous)
        {
            for( var i=5;i>0;i--)
            {
                years[years.length] = [currentYear - i,currentYear - i];                
            }
        }
        else 
        {
            for( var i=1;i<6;i++)
            {
                years[years.length] = [currentYear + i,currentYear + i];                
            }
        }
        return years;
    }
    function getMenuMonths(previous)
    {
        var months = new Array(); 
        if (previous)
        {
            for( var i= currentMonth-2; i>=0 ;i--)
            {
                months[months.length] = [base.monthNames[i],i+1];                
            }
        }
        else 
        {
            for( var i=currentMonth ;i<12;i++)
            {
                months[months.length] = [base.monthNames[i],i+1]
            }
        }
        return months;
    } 
    
    
    
    // Poistaa tyylimaaritykset solulta 
    function cleanClassNames(cell)
    {
        if (!cell.selected)
        {
            cell.className = cell.origClassName;
        }
        else if (cell.current) 
        {
            if (cell.className != cell.origClassName + ' CalenderCurrentSelected')
            {
                cell.className = cell.origClassName + ' CalenderCurrentSelected';
            }
        }
        else 
        {
            if (cell.className != cell.origClassName + ' CalenderSelected')
            {
                cell.className = cell.origClassName + ' CalenderSelected';
            }
        }
    }
    
    // palauttaa nappainkoodin selainversiosta riippumatta
    function setKeyMask(e)
    {
        var Key; 
        if (navigator.appName=='Microsoft Internet Explorer')
        {
            Key = window.event.keyCode; 
        } 
        else if (navigator.appName=='Netscape')
        {
            Key = e.which;
        }
        return Key;
    }
    
    
    // hakee kyseisen kuukauden ensimmaisen viikonpaivan jarjestysnumeron  
    function getFirstWeekDayOfMonth(date) 
    {
        date.setFullYear(date.getFullYear(),date.getMonth(),1);
        var result = parseInt(date.getDay());
        return  convertWeekDay(result);
    }
    
    // hakee kyseisen kuukauden viimeisen viikonpaivan jarjestysnumeron   
    function getLastWeekDayOfMonth(date) 
    {
        date.setFullYear(date.getFullYear(),date.getMonth() + 1 ,1);
        var result = parseInt(date.getDay()) - 1;
        return convertWeekDay(result);
    }
  
    // Palauttaa viikonpaivan oikeassa formaatissa eka paiva on maanantai ja viimeinen 
    // paiva on sunnuntai 
    function convertWeekDay(number)
    {
        if (parseInt(number) == 0)
        {
            return parseInt(6);
        } 
        else 
        {
            return parseInt(number)-1;
        }
    }

    function getDaysOfMonth(sdate) 
    {
        sdate.setFullYear(sdate.getFullYear(),sdate.getMonth(),1);
        sdate.setFullYear(sdate.getFullYear(),sdate.getMonth()+1,sdate.getDate()- 1);
        return parseInt(sdate.getDate());
    }	

    function fireEvent(y,m,d)
    {
        sendEventToServer(base.url,'Calender_' + CalenderVariable + '=' + y + '.' + m + '.' + d);
    }
} // class 



