function check_day_in_month(year, month) {
  return (new Date(year, month, 0)).getDate();
}

function hideDateSelect() {
  document.getElementById('datePicker').style.display = "none";
}

function setDate(element,year,month,day) {
  document.getElementById(element).innerHTML = year + "-" + month + "-"+day;
  hideDateSelect();
}

function draw_calendar($g_year, $g_month, $g_day, $g_floating, $g_element) {
  $current_year=(new Date()).getYear();
  $current_day=(new Date()).getDate();
  $current_month=(new Date()).getMonth()+1;

  if (!$g_year) {
    $g_year = $current_year;
    $g_month = $current_month;
    $g_day = $current_day;
  }

  $year = $g_year;
  $month = $g_month;
  $day = $g_day;

  //znajdz ilosc dni w tym miesiacu
  $day_in_month=check_day_in_month($year,$month);
  
  //znajdz ilosc dni w poprzednim miesišcu
  $temp_m=$month;
  $temp_y=$year;
  
  $temp_m--;
  if ($temp_m==0)
  {
    $temp_m=1;
    $temp_y--;
  }
  $day_in_prev_month=check_day_in_month($temp_y,$temp_m);
   
  //zlokalizuj pierwszy dzien tego miesiaca
  $fd=(new Date($year, $month-1, 1)).getDay();
  if ($fd==0) $fd=7;

  $temp = "&nbsp;";
  $temp2 = "";
  if ($g_floating == "1") {
    $temp = "Bez daty";
    $temp2 = "onClick=\"setDate('"+$g_element+"','','','')\" style=\"cursor: pointer;\"";
  }
  
  var $out = '';
  $out += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">";
  $out += "<tr>";
  $out += "<td class=\"ttc_middle_nav\" style=\"width: 20px;\">&nbsp;</td>";
  $out += "<td class=\"ttc_middle_nav\" "+$temp2+">"+$temp+"</td>";
  $out += "<td class=\"ttc_middle_nav\" style=\"text-align: right; width: 20px;\"><img onClick=\"hideDateSelect()\" src=\"gfx/close.gif\" class=\"img_link\" /></td>";
  $out += "</tr>";
  $out += "</table>";

  $rnumb=1;
  
  $event_left_far = "onClick=\"dateSelect("+($g_year-1)+","+($g_month)+",15,'"+$g_element+"',1,"+$g_floating+")\"";
  $event_right_far = "onClick=\"dateSelect("+(parseInt($g_year)+1)+","+($g_month)+",15,'"+$g_element+"',1,"+$g_floating+")\"";

  if ($month>1) {
    $event_left = "onClick=\"dateSelect("+$g_year+","+($g_month-1)+",15,'"+$g_element+"',1,"+$g_floating+")\"";
  }else {
    $event_left = "onClick=\"dateSelect("+($g_year-1)+",12,15,'"+$g_element+"',1,"+$g_floating+")\"";
  }
  
  if ($month<12) {
    $event_right = "onClick=\"dateSelect("+$g_year+","+(parseInt($g_month)+1)+",15,'"+$g_element+"',1,"+$g_floating+")\"";
  }else {
    $event_right = "onClick=\"dateSelect("+(parseInt($g_year)+1)+",1,15,'"+$g_element+"',1,"+$g_floating+")\"";
  }
  
  var months = ['Styczen', 'Luty', 'Marzec', 'Kwiecien', 'Maj', 'Czerwiec', 'Lipiec', 'Sierpien', 'Wrzesien', 'Pazdziernik', 'Listopad', 'Grudzien'];
  $out += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">";
  $out += "<tr>";
  $out += "<td class=\"ttc_left_nav\"><img "+$event_left_far+" src=\"gfx/left_far.gif\" class=\"img_link\" /></td>";
  $out += "<td class=\"ttc_left_nav\"><img "+$event_left+" src=\"gfx/left.gif\" class=\"img_link\" /></td>";
  $out += "<td class=\"ttc_middle_nav\">"+months[$month-1]+" "+$year+"</td>";
  $out += "<td class=\"ttc_right_nav\"><img "+$event_right+" src=\"gfx/right.gif\" class=\"img_link\" /></td>";
  $out += "<td class=\"ttc_right_nav\"><img "+$event_right_far+" src=\"gfx/right_far.gif\" class=\"img_link\" /></td>";
  $out += "</tr>";
  $out += "</table>";

  $out += "<table class = \"ttc\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">";
  $out += "<tr>";
  $out += "<th>Po</td>";
  $out += "<th>Wt</td>";
  $out += "<th>Sr</td>";
  $out += "<th>Cz</td>";
  $out += "<th>Pt</td>";
  $out += "<th>So</td>";
  $out += "<th>Ni</td>";
  
  $out += "</tr>";
  $out += "<tr>";
  //wygeneruj puste komórki
  $do_przesuniecia=$fd-1;
    
  for ($i=1;$i<=$do_przesuniecia;$i++)
  {
    $class_prefix="";
    if ($i==6) 
    {
      $class_prefix="_free";
    }
    $out += "<td class=\"ttc_im"+$class_prefix+"\">"+($day_in_prev_month-$do_przesuniecia+$i)+"</td>\n";
  }
  //petla po dniach
  for ($i=1;$i<=$day_in_month;$i++)
  {
    if ((($i+$do_przesuniecia)%7==1)&&($i!=1)) 
    {
      $out += "</tr><tr>";
      $rnumb++;
    }
    //sprawdz, czy to sobota lub niedziela
    
    $class_prefix="";
    $class_prefix2="";
    if ((($i+$do_przesuniecia)%7==6)||(($i+$do_przesuniecia)%7==0)) 
    {
      $class_prefix="free";
    }    
    
      
    if (($year==$current_year)&&($month==$current_month)&&($i==$current_day))
    {
      $class_prefix+="current";
      $class_prefix2+="_current";
    }
    
    $out += "<td onClick=\"setDate('"+$g_element+"',"+$year+","+$month+","+$i+")\" class=\"ttc_"+$class_prefix+"\">"+$i+"</td>\n";  
  }
  //zlokalizuj ostatni dzien tego miesiaca
  $ld=(new Date($year,$month-1,$day_in_month)).getDay();
  if ($ld==0) $ld=7;
  
  $do_przesuniecia=7-$ld;
  for ($i=1;$i<=$do_przesuniecia;$i++)
  {
    $class_prefix="";
    if (($i==$do_przesuniecia)||($i==$do_przesuniecia-1)) 
    {
      $class_prefix="_free";
    }
    
    $t_month=$month+1;
    $t_year=$year;
    if ($t_month==13)
    {
      $t_month=1;
      $t_year++;
    }
    
    $out += "<td class=\"ttc_im"+$class_prefix+"\">"+$i+"</td>\n";
  }
  
  $out += "</tr></table>";  
  return $out;
}

function dateSelect(year,month,day,element,req,floating) {

  if (req == 1) document.getElementById('datePicker').style.display="none";

  dateHTTP=GetXmlHttpObject();
  if (dateHTTP==null) {
    alert ("Browser does not support HTTP Request");
    return
  }

  if (floating == null) floating = 0;
  
  if (req == 1) {
    document.getElementById('datePicker').innerHTML = draw_calendar(year, month, day, floating, element);
    document.getElementById('datePicker').style.display = "block";
  }else {
    document.getElementById('datePicker').style.top = mouseY + 10 + 'px';
    document.getElementById('datePicker').style.left = mouseX + 10+'px';
    document.getElementById('datePicker').innerHTML = draw_calendar(year, month, day, floating, element);
    document.getElementById('datePicker').style.display="block";
  }
}
