
var visibilitykal = 0;
var objectjawny = "";

function GetObjectkal(object) {
    if (document.getElementById && document.getElementById(object) != null)
         return document.getElementById(object);
    else if (document.layers && document.layers[object] != null)
         return document.layers[object];
    else if (document.all)
         return document.all[object];
}


function getElementPositionK(elemID) {
	var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}


function showKAL(evt, elem) {
    evt = (evt) ? evt : event;
    if (evt) {
     	 myobject=GetObjectkal('kal');     	 
//        nic z ponizszego nie dziala w mozilli !!!
//       alert("visibilitykal: "+myobject.style.visibilitykal);
//       alert("display: "+myobject.style.disply);
//       alert("style object: "+myobject.style);
//       alert(document.all[object].style.visibilitykal);
//       alert(document.layers[object].style.visibilitykal);
//        nic z powyzszego nie dziala w mozilli !!!
       if ((myobject.style.visibilitykal == "hidden") || (myobject.style.visibilitykal != "visible")) {
           var IE = document.all?true:false;
			   var position = getElementPositionK(elem);
               var tempX = 500; // niech bedzie 100 na poczatek, gdyby nie dalo sie pozniej ustawic wartosci pozycji myszki
               var tempY = 300; // niech bedzie 100 na poczatek,                 --             ||             --
			   // odczytanie pozycji dla okna T/N wzgledem fielda (dla mozilli i netscape
			   // dla IE i Opery okno bedzie wzgledem pozycji myszki
   			   var offsetTrail = document.getElementById(elem);
   			   if ((offsetTrail != null) && (position != null))
   			   {
               tempX=500;tempY=300;
               tempX=position.left + offsetTrail.offsetWidth+15;
               tempY=position.top;  //+13
           }
           if (IE) { // grab the x-y pos.s - IE
                tempX=500;tempY=300;
                tempX = event.clientX + document.body.scrollLeft;
                tempY = event.clientY + document.body.scrollTop;
              } else {  // grab the x-y pos.s - no IE
                tempX=500;tempY=300;
                document.captureEvents(Event.MOUSEMOVE);
                // Set-up to use getMouseXY function onMouseMove
                document.onmousemove = getMouseXY;
                }  
                // NS4 - mozliwe ujemne wartosci, odwroc
           if (tempX < 0){tempX = 0}
           if (tempY < 0){tempY = 0}  
           //
           // PRZESUN kal i pokaz
           //alert("tworzymy");
           //alert(((self.window.width) / 2)-100);
           //
                      var windowwidth;
           windowwidth = window.innerWidth;
           if (windowwidth == null) {
             windowwidth = document.body.offsetWidth;
           }
           var windowheight;
           windowheight = window.innerHeight;
           if (windowheight == null) {
             windowheight = document.body.offsetHeight;
           }
           calwidth = getObjectWidth('calendarTable');
           calheight = getObjectHeight('calendarTable');
           movew = tempX-80;
         //  moveh = tempY-15-100;
           moveh = tempY;
           if ((movew+calwidth) > windowwidth)
             movew = windowwidth - calwidth - 1;
           if ((moveh+calheight) > windowheight)
             moveh = windowheight - calheight - 1;
           if (moveh <=0) {
             moveh=1;
           }
           if (movew <=0) {
             movew=1;
           }
           //
           shiftTo("kal", movew, moveh);
           //shiftTo("kal", tempX-80, tempY-15-100);
           //
           // zmienna visibilitykal z powodu mozilli, ktora NIE zwraca wlasciwosci myobject.style.visibilitykal (mozna ja tylko ustawiac)
           if (visibilitykal == 0) {
            visibilitykal = 1;     
            setTimeout("show(\"kal\")",100);//show("kal");   
           } else {
            visibilitykalSetZeroKAL()   
           }
      } else { // jak bylo show(y) i znow wywolano metode showKAL(evt) - to ukryj
            //alert("usuwamy");
          visibilitykalSetZeroKAL()    
      }
    }
}


function visibilitykalSetZeroKAL() {
  hide("kal"); 
  visibilitykal = 0;
}


function getMouseXY(e) {
    tempX = e.pageX;
    tempY = e.pageY;
}


/************************
  RYSUJ KAL 
*************************/
function repaint(yourobjectname)
{
 if ((document.getElementById("kal")) != null) { // teraz jesli w poprzednim if, else wszystko poszlo OK.... to teraz
 //
// alert(document.getElementById("kal"));
 //
              if ((document.getElementById("tableBodyKal")) != null) {
                  var TBody = document.getElementById("tableBodyKal");
                  //
                  // wykasuj istniejace Row's
                   if (TBody != null)
                   {
                      while (TBody.rows.length > 0) {
                          TBody.deleteRow(0);          
                         }
                   }
                  }
                 else {
                  var TBody = document.createElement("tableBodyKal");
                  }
    chooseMonth = document.getElementById("chooseMonth");
    if (chooseMonth == null)
    {
       chooseMonth = document.dateChooser.chooseMonth;
//       alert(chooseMonth);
    }
    var theMonth = (chooseMonth.selectedIndex);
    chooseYear2=   document.getElementById("chooseYear");     
    if (chooseYear2 == null)
    {
       chooseYear2 = document.dateChooser.chooseYear;
       //alert(chooseYear2);
    }
    var theYear = parseInt(chooseYear2.options[chooseYear2.selectedIndex].text);
    var firstDay = getFirstDay(theYear, theMonth);
    var howMany = getMonthLen(theYear, theMonth);
    var today = new Date();
    // fill in month/year in table header
    document.getElementById("tableHeaderKal").innerHTML =  chooseMonth.options[theMonth].text + " " + theYear + " r.";
    // initialize
    var dayCounter = 1;
    var newR, newC, dateNum;
    var done=false;
    while (!done) {
        // create new row at end
//
        newR = TBody.insertRow(TBody.rows.length);
        if (newR) {
            for (var i = 0; i < 7; i++) {
                // create new cell at end of row
                newC = newR.insertCell(newR.cells.length);
                if (TBody.rows.length == 1 && i < firstDay) {
                    // empty boxes before first day
                    newC.innerHTML = "&nbsp;";
                    continue;
                }
                if (dayCounter == howMany) {
                    // no more rows after this one
                    done = true;
                }
                // plug in link/date (or empty for boxes after last day)
                if (dayCounter <= howMany) {
                          //alert(today);
                          //alert(today.getFullYear());
                          //alert(today.getMonth());
                          //alert(today.getDate());
                          if (
                              (today.getFullYear() == theYear) &&
                              (today.getMonth() == theMonth) &&
                              (today.getDate() == dayCounter)
                              ) {
                              newC.id = "today";
  
                          } 
                          newC.innerHTML = "<a href='#'onclick='chooseKAL(\"" +
                          theYear + 
                          "-" +
                          (((theMonth +1)<10)?("0" + ((theMonth +1))):((theMonth +1))) +
                          "-" + 
                          ((dayCounter<10)?("0" + (dayCounter)):(dayCounter))     +         
                          "\", \"" + yourobjectname + "\"); visibilitykalSetZeroKAL(); return false;'>" + dayCounter + "</a>";
                          dayCounter++;
               } else {
                         newC.innerHTML = "&nbsp;";
                }
            }
        } else {
            done = true;
        }
    } 
       
 }         
}  


function populateTableKAL(yourobjectname) {
      objectjawny = yourobjectname;
      if (visibilitykal == 1) {
        return;
      }
      var doskadowania = document.getElementById('kal');
      if (doskadowania == null) {
      var kal;
      kal = document.createElement("kal");
      // UWAGA
      //innerHTML doesn't work in netscape, so you might
      //want to use something else. I don't know what
      //else would work except for document.write();
      //but if you put document.write inside a <td> tag,
      //the banners won't show in netscape.
      kal.innerHTML="" +
      "<div id=\"kal\">                                      " +
      "<table id=\"calendarTable\" border=1>                 " +
      "<tr>                                         " +
      "    <th id=\"HeaderKal\" align=\"right\" colspan=\"7\">  <right><img src=\"kalendarz/close.gif\" onclick=\"visibilitykalSetZeroKAL(); return false;\" alt=\"Wyj\u015Bcie\" >  </right>   </th>              " +
      "</tr>                                      " +                 
      "<tr>                                         " +
      "    <th id=\"tableHeaderKal\" colspan=\"7\"></th>              " +
      "</tr>                                      " +                 
      "<tr><th>Nie</th><th>Pon</th><th>Wto</th><th>\u015Aro</th>         " +
      "<th>Czw</th><th>Pi\u0105</th><th>Sob</th></tr>                  " +
      "<tbody id=\"tableBodyKal\"></tbody>                           " +
      "<tr>                                      " +
      "    <td colspan=\"7\">                               " +
      "    <p>                                                   " +
      "    <center><form name=\"dateChooser\" id=\"dateChooser\">        " +
      "        <select name=\"chooseMonth\"                      " +
      "        onchange='repaint(\""+yourobjectname+"\"); return false;'>           " +
      "            <option>Stycze\u0144<option>Luty         " +
      "            <option>Marzec<option>Kwiecie\u0144<option>Maj        " +
      "            <option>Czerwiec<option>Lipiec<option>Sierpie\u0144" +
      "            <option>Wrzesie\u0144<option>Pa\u017Adziernik               " +
      "            <option>Listopad<option>Grudzie\u0144                  " +
      "   </select>                                      " +        
      "    <select name=\"chooseYear\" onchange='repaint(\""+yourobjectname+"\"); return false;'>   " +
      "</select>                                     " +
      "    </form></center>                               " +
      "    </p>                               " +
      "    </td>                               " +
      "</tr>                                 " +
      "</table>                               " +
      "</div>                                 ";
      //
      document.body.appendChild(kal); 
      fillYears();
  }
  repaint(yourobjectname);      
}


// day of week of month's first day
function getFirstDay(theYear, theMonth){
    var firstDate = new Date(theYear,theMonth,1);
    return firstDate.getDay();
} 

// number of days in the month
function getMonthLen(theYear, theMonth) {
    var nextMonth = new Date(theYear, theMonth + 1, 1);
    nextMonth.setHours(nextMonth.getHours() - 3);
    return nextMonth.getDate();
}
       
// fill years              
function fillYears() {
    var today = new Date();
    var thisYear = today.getFullYear();
    var yearChooser2 =    document.getElementById("chooseYear");
    //
    //
    if (yearChooser2 == null)
    {
      for (i = (thisYear+3); i >= 1900 ; i--) {
      document.dateChooser.chooseYear.options[document.dateChooser.chooseYear.options.length] = new Option(i, i);
      }
      document.dateChooser.chooseYear.options[document.dateChooser.chooseYear.length-2].selected=true; 
      setCurrMonth(today);
      document.dateChooser.chooseYear.options[document.dateChooser.chooseYear.length-(thisYear-1900+1)].selected=true; 
    }
    else
    {
      for (i = (thisYear+3); i >= 1900 ; i--) {
      yearChooser2.options[yearChooser2.length] = new Option(i, i);
      }
      yearChooser2.options[yearChooser2.length-2].selected=true; 
      setCurrMonth(today);
      yearChooser2.options[yearChooser2.length-(thisYear-1900+1)].selected=true; 
    }
}

// set month choice to current month
function setCurrMonth(today) {
         var chooseMonth2 =    document.getElementById("chooseMonth");
         if (chooseMonth2 == null)
         {
           document.dateChooser.chooseMonth.selectedIndex = today.getMonth();
         }
         else
         {
         chooseMonth2.selectedIndex = today.getMonth();
         }
}
              
/*******************
   PROCES WYBIERANIA
********************/
function chooseKAL(kal, yourobject) {
    var Elem = null;
    if ((objectjawny != null) && (objectjawny != ""))
    {
      Elem = document.getElementById(objectjawny);
    }
    else
    {    
     Elem = document.getElementById(yourobject);
    }
    Elem.value= kal;
}

