var init   = new Date();
var day   = init.getDate();
var MyObj = null;
var oDiv = document.createElement("DIV");
var oShield = document.createElement("<DIV style='position:absolute; top:0px; left:0px; width:100%; height:100%; background:red; filter: Alpha(Opacity=1); '></DIV>");
var myRazdel = "";
oShield.onclick=close_cal;
html = "<DIV id=cal_body style='position:absolute; top:0px; left:0px; width:180; height:160; border:1px solid threeddarkshadow; border-top:1px solid buttonface; border-left:1px solid buttonface;'>";
html +="<DIV style='position:absolute; top:0px; left:0px; width:100%; height:100%; background:buttonface; border:1px solid buttonshadow; border-top:1px solid buttonhighlight; border-left:1px solid white;'>";
html +="<DIV style='position:absolute; left:0px; top:5px; width:176px;'><CENTER><SELECT ID=month class=control onchange=displayCalendar(this.selectedIndex,year.selectedIndex+2004)>";
html +="<OPTION>ßíâàðü<OPTION>Ôåâðàëü<OPTION>Ìàðò<OPTION>Àïðåëü<OPTION>Ìàé<OPTION>Èþíü<OPTION>Èþëü<OPTION>Àâãóñò<OPTION>Ñåíòÿáðü<OPTION>Îêòÿáðü<OPTION>Íîÿáðü<OPTION>Äåêàáðü</SELECT>&nbsp;";
html +="<SELECT  ID=year class=control onchange=displayCalendar(month.selectedIndex,this.selectedIndex+2004)>";
for (var i=2004; i<2010; i++) html += "<OPTION>"+i;
html +="</SELECT>";
html +="&nbsp;<INPUT id=more TYPE=BUTTON class=control VALUE='>>'     onClick='setWindowsWidth()'><BR>";
html +="<style type=text/css>";
html +="<!--";
html +=".control {  font-size: 9px; height: 18px}";
html +=".control1 {  font-size: 9px; height: 18px; width: 35px;}";
html +=".day_off_header {  background-color: #FF3333; height: 14px; width: 20px; font-size: 9px; font-weight: bold; color: #FFFFFF; text-align: center; cursor: default; border: 1px #FFFFFF outset; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px}";
html +=".day_header {  background-color: #666666; height: 14px; width: 20px; font-size: 9px; font-weight: bold; color: #FFFFFF; text-align: center; cursor: default; border: 1px #FFFFFF outset; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px}";
html +=".day_disabled {  font-size: 9px; color:#999999; font-weight: bold; background-color: buttonface; height: 14px; width: 20px; text-align: center; cursor: default; border: 1px #B4B4B4 solid; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px}";
html +=".day {  font-size: 9px; font-weight: bold; background-color: buttonface; height: 14px; width: 20px; text-align: center; cursor: default; border: 1px #FFFFFF outset; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px}";
html +=".day_selected {  font-size: 9px; font-weight: bold; background-color: #FFFFCC; height: 12px; width: 18px; text-align: center; cursor: default; border: 1px #CCCCCC inset; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px}";
html +=".day_off { font-size: 9px; font-weight: bold; background-color: buttonface; height: 14px; width: 20px; text-align: center ; color: #FF0000 ; cursor: default; border: 1px #FFFFFF outset; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px}";
html +=".day_mouseover { font-size: 9px; font-weight: bold; color: #990000; background-color: buttonface; height: 14px; width: 20px; text-align: center; cursor: default; border: 1px #FFFFFF outset; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px}";
html +="-->";
html +="</style>";
html +="<table ID=CalTable border=0  cellspacing=1>";
html +="<tr> ";
html +="<td class=day_off_header>Âñê</td><td class=day_header>Ïí</td><td class=day_header>Âò</td><td class=day_header>Ñð</td><td class=day_header>×ò</td><td class=day_header>Ïò</td><td class=day_header>Ñá</td>";
html +="</tr>";
for (var i=0; i<7; i++) html +="<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>";
html +="</table>";
html +="<INPUT TYPE=BUTTON class=control VALUE=' << '    onClick=setYear(-1)>";
html +="&nbsp;<INPUT TYPE=BUTTON class=control VALUE=' < '    onClick=setMonth(-1)>";
html +="&nbsp;<INPUT TYPE=BUTTON class=control VALUE=' Ñåãîäíÿ '    onClick=setToday()>";
html +="&nbsp;<INPUT TYPE=BUTTON class=control VALUE=' > '    onClick=setMonth(1)>";
html +="&nbsp;<INPUT TYPE=BUTTON class=control VALUE=' >> '   onblur='month.focus()' onClick=setYear(1)>";
html +="</CENTER></DIV><DIV id='ext_counter' style='position:absolute; visibility: hidden; left:180px; top:10px; width:70px; height:150px; z-index:1'><CENTER>";
html +="<FONT size='1'><b>Äåíü  :</b></FONT><BR><INPUT TYPE=BUTTON class=control VALUE='-'    onClick=count_date(-parseInt(text1.value)) >";
html +="<INPUT TYPE=text class=control1 VALUE='' id=text1>";
html +="<INPUT TYPE=BUTTON class=control VALUE='+'    onClick='count_date(parseInt(text1.value));' ><BR>";
html +="<FONT size='1'><b>Ìåñÿö:</b></FONT><BR><INPUT TYPE=BUTTON class=control VALUE='-'    onClick=setMonth(-parseInt(text2.value)) >";
html +="<INPUT TYPE=text class=control1 VALUE='' id=text2>";
html +="<INPUT TYPE=BUTTON class=control VALUE='+'    onClick=setMonth(parseInt(text2.value)) ><BR>";
html +="<FONT size='1'><b>Ãîä :</b></FONT><BR><INPUT TYPE=BUTTON class=control VALUE='-'    onClick=setYear(-parseInt(text3.value)) >";
html +="<INPUT TYPE=text class=control1 VALUE='' id=text3>";
html +="<INPUT TYPE=BUTTON class=control VALUE='+'    onClick=setYear(parseInt(text3.value)) >";
html +="<br><br><INPUT TYPE=BUTTON class=control VALUE=' OK '   onblur='month.focus()'  onClick=eventHandlerDblClick() >";
html +="</CENTER></DIV></DIV></DIV>";

function setWindowsWidth(){
if (cal_body.style.width=="180px"){
 cal_body.style.width = "265px";
 more.value="<<";
 ext_counter.style.visibility="visible";
 }
  else {
 cal_body.style.width = "180px";
 more.value=">>";
 ext_counter.style.visibility="hidden";
 }
}

function openCalendar(razdel,obj, x_l, x_t)
{
if (!x_l) var x_l=0;	
if (!x_t) var x_t=0;
myRazdel = razdel;
MyObj=obj;
oDiv.innerHTML=html;
oDiv.style.position="absolute";
oDiv.style.zIndex=301;
oShield.style.zIndex=300;
oDiv.style.pixelLeft=calcLeft(obj)-3 + x_l;
oDiv.style.pixelTop=calcTop(obj)+obj.offsetHeight-3 + x_t;
document.body.appendChild(oShield);
document.body.appendChild(oDiv);

if (obj){
var x_day=parseInt(obj.value.substr(0,2));
var x_month=parseInt(obj.value.substr(3,2));
var x_year=parseInt(obj.value.substr(6,4));
if (x_day > 0 && x_day <= getDaysInMonth(x_month,x_year) && x_month  > 0 && x_month < 13 && x_year > 1899 && x_year < 2100){
day=x_day;
month.selectedIndex=x_month-1;
year.selectedIndex=x_year-2004;
displayCalendar(x_month-1,x_year,x_day);
} else
setToday();
}
else
setToday();
}

function eventHandlerOver(anEvObj) {
if (this.className!="day_selected")
this.className="day_mouseover";
}
function eventHandlerOut(anEvObj) {
if (this.className!="day_selected")
(this.cellIndex>0) ? this.className="day":this.className="day_off";
}
function eventHandlerDblClick(anEvObj) {
var str_day ="";
var str_month =month.selectedIndex+1;
var str_year=year.selectedIndex+2004;
(day.toString().length==1)? str_day="0"+day : str_day=day;
if (str_month<10) str_month="0"+str_month;
MyObj.value=(str_day + "-" + str_month + "-" + str_year);
close_cal();

window.location = "board_"+myRazdel+"_archive_"+str_year+"_"+str_month+"_"+str_day+".htm";

}

function close_cal(){
document.body.removeChild(oDiv);
document.body.removeChild(oShield);	
}

function eventHandlerClick(anEvObj) {
day=this.innerHTML;
if (this.className=="day_disabled"){
(parseInt(day) > 20) ? setMonth(-1): setMonth(1);
}
else displayCalendar(month.selectedIndex, year.selectedIndex+2004, day);
eventHandlerDblClick(anEvObj);
}

function setYear(val) {
if (!isNaN(val)) {
x_year=year.selectedIndex+2004;
x_year= Number(x_year) + val;
if (x_year < 2004) x_year = 2004;
if (x_year > 2010) x_year = 2010;
year.selectedIndex = x_year-2004;
displayCalendar(month.selectedIndex, year.selectedIndex+2004);
}
}

function setMonth(val) {
if (!isNaN(val)) {
var i = 0;
var x_month = month.selectedIndex;
i=x_month + val;
x_month=i%12;
if (x_month < 0) x_month = x_month + 12;
month.selectedIndex = x_month;
setYear(Math.floor(i/12));
displayCalendar(month.selectedIndex, year.selectedIndex+2004);	
}
}

function setToday() {
var now   = new Date();
var x_day   = now.getDate();
var x_month = now.getMonth();
var x_year  = now.getYear();
day=x_day;
month.selectedIndex = x_month;
year.selectedIndex = x_year-2004;
displayCalendar( month.selectedIndex, year.selectedIndex+2004, x_day);
}

function getDaysInMonth(x_month,x_year)  {
var days;
if (x_month==1 || x_month==3 || x_month==5 || x_month==7 || x_month==8 || x_month==10 || x_month==12)  days=31;
else if (x_month==4 || x_month==6 || x_month==9 || x_month==11) days=30;
else if (x_month==2)  {
if (isLeapYear(x_year)) { days=29; }
else { days=28; }
}
return (days);
}
function isLeapYear (x_year) {
if (((x_year % 4)==0) && ((x_year % 100)!=0) || ((x_year % 400)==0)) {
return (true);
} else { return (false); }
}

function displayCalendar(x_month, x_year, x_day) {
x_month = parseInt(x_month);
x_year = parseInt(x_year);
var days = getDaysInMonth(x_month+1,x_year);
if (x_month > 1) var days_before = getDaysInMonth(x_month,x_year);
else var days_before = getDaysInMonth(12,x_year-1);
if (day > days) day=days;
if (!x_day) x_day=day;
var curr_day = 0;
var firstOfMonth = new Date (x_year, x_month, 1);
var startingPos = firstOfMonth.getDay();
for (i=0; i<42; i++){
curr_day=i-startingPos+1;
(CalTable.rows[Math.floor(i/7)+1].cells[i%7].cellIndex>0) ? CalTable.rows[Math.floor(i/7)+1].cells[i%7].className="day":CalTable.rows[Math.floor(i/7)+1].cells[i%7].className="day_off";
if (curr_day <= 0 ) {CalTable.rows[Math.floor(i/7)+1].cells[i%7].innerHTML=curr_day + days_before;
CalTable.rows[Math.floor(i/7)+1].cells[i%7].className="day_disabled";
CalTable.rows[Math.floor(i/7)+1].cells[i%7].onmouseover="";
CalTable.rows[Math.floor(i/7)+1].cells[i%7].onmouseout="";}
else if (curr_day > 0 && curr_day <= days) {CalTable.rows[Math.floor(i/7)+1].cells[i%7].innerHTML=curr_day;
CalTable.rows[Math.floor(i/7)+1].cells[i%7].onmouseover=eventHandlerOver;
CalTable.rows[Math.floor(i/7)+1].cells[i%7].onmouseout=eventHandlerOut;
}
else if (curr_day > days) {CalTable.rows[Math.floor(i/7)+1].cells[i%7].innerHTML=curr_day-days;
CalTable.rows[Math.floor(i/7)+1].cells[i%7].className="day_disabled"
CalTable.rows[Math.floor(i/7)+1].cells[i%7].onmouseover="";
CalTable.rows[Math.floor(i/7)+1].cells[i%7].onmouseout="";}

CalTable.rows[Math.floor(i/7)+1].cells[i%7].onclick=eventHandlerClick;
CalTable.rows[Math.floor(i/7)+1].cells[i%7].onclick=eventHandlerClick;
 
if (curr_day==x_day) CalTable.rows[Math.floor(i/7)+1].cells[i%7].className="day_selected";
}
}

function count_date(x_diff){
if (!isNaN(x_diff)){
curr_d = new Date(year.selectedIndex+2004, month.selectedIndex, day); 
newDate = curr_d.getDate() + x_diff;
curr_d.setDate( newDate );
day=curr_d.getDate();
year.selectedIndex=curr_d.getYear()-2004;
month.selectedIndex=curr_d.getMonth()
displayCalendar( curr_d.getMonth(), curr_d.getYear(), day);
}
}

function calcTop(x_ele){
var x_ret=0;
var oParent = x_ele.offsetParent;
if (oParent == null) return 0
else x_ret=x_ele.offsetTop + oParent.clientTop + calcTop(oParent);
return x_ret
}

function calcLeft(x_ele){
var x_ret=0;
var oParent = x_ele.offsetParent;
if (oParent == null) return 0
else x_ret=x_ele.offsetLeft + oParent.clientLeft + calcLeft(oParent);
return x_ret
}
