
var NS = (document.all) ? false : true; // MSIE vs. Others
var LAYERS = (document.layers) ? true : false; // NN4 vs. Others
var calWinSize = [160,111];
var calMonths = ['ßíâàðü','Ôåâðàëü','Ìàðò','Àïðåëü','Ìàé','Èþíü','Èþëü','Àâãóñò','Ñåíòÿáðü','Îêòÿáðü','Íîÿáðü','Äåêàáðü'];
var calWeek = ['Ïí','Âò','Ñð','×ò','Ïò','Ñá','Âñ'];
var calMonthDays = [31,28,31,30,31,30,31,31,30,31,30,31];
var curInput = null;
var calUseTransit = true; // Whether to use transition alpha effects in MSIE
var calOpacity = 100; // Final opacity of the control in MSIE
var calOpacitySteps = 1; // Number of steps when changing opacity in MSIE

if(LAYERS)
          document.write("<layer id=calMain visibility=hide bgcolor=#dddddd><br></layer>");
else
          document.write("<div id=calMain class=cl_out style='position:absolute; display:none; background-color:#dddddd;'><br></div>");


//****************************************************************************
function calGetDate(dateBtn, dateField) {
          calPosX = (LAYERS) ? dateBtn.x : dateBtn.offsetLeft;
          calPosY = (LAYERS) ? dateBtn.y : dateBtn.offsetTop + dateBtn.offsetHeight;
          for(el=dateBtn.offsetParent; el!=null; el=el.offsetParent) {
                    calPosX += el.offsetLeft; calPosY += el.offsetTop;
          }
          if(!LAYERS) {
                    bodyWidth = (NS) ? document.body.offsetWidth : document.body.clientWidth-5;
                    while((calPosX > 0) && ((calPosX+calWinSize[0]) > bodyWidth)) calPosX--;
          }

          curInput = (NS) ? dateField : dateField.name;
          curDate = dateField.value.split("-"); nowDate = new Date();
          curDate[1] = parseInt(curDate[1]); curDate[2] = parseInt(curDate[2]);
          if(curDate[1]); // WTF? I get a bug without this line
          calFillInside((((curDate[1]>=1)&&(curDate[1]<=12)) ? (curDate[1]-1) : nowDate.getMonth()), ((curDate[2]>=1900) ? curDate[2] : nowDate.getFullYear()));

          main = (LAYERS) ? document.layers.calMain : (NS) ? document.getElementById('calMain') : document.all.calMain;
          if(LAYERS) {
                    main.left = calPosX; main.top = calPosY;
                    main.visibility = "show";
          } else {
                    main.style.left = calPosX; main.style.top = calPosY;
                    if(NS||((!NS)&&(!calUseTransit))) main.style.display = ''; else calTransitShow(true, 0);
          }
          return false;

}

//****************************************************************************
function calTransitShow(isShow, step) {
          obj = document.all.calMain;
          opStart = (isShow) ? 0 : calOpacity;
          opEnd = (isShow) ? calOpacity : 0;
          if(!step) {
                    obj.style.filter = "alpha(opacity="+opStart+")";
                    obj.style.display = "";
                    setTimeout("calTransitShow("+isShow+","+(step+1)+")", 1);
          } else if(step <= (calOpacitySteps+1)) {
                    opacity = parseInt( (opStart-opEnd) * (step-1) / calOpacitySteps );
                    if(opacity < 0) opacity = -opacity;
                    if(!isShow) opacity = 100 - opacity;
                    obj.style.filter = "alpha(opacity="+opacity+")";
                    setTimeout("calTransitShow("+isShow+","+(step+1)+")", 1);
          } else {
                    obj.style.display = (isShow) ? "" : "none";
          }
}

//****************************************************************************
function calExit(day, mon, yea) {
          if(LAYERS) {
                    with(document.layers.calMain.document) {
                              open("text/html"); write("<br>"); close();
                    }
                    document.layers.calMain.visibility = "hide";
          } else {
                    obj = (NS) ? document.getElementById('calMain') : document.all.calMain;
                    if(NS||((!NS)&&(!calUseTransit))) obj.style.display = "none"; else calTransitShow(false, 0);
                    obj.innerHTML = "<br>";
          }
          if(curInput && day && (++mon) && yea) {
                    curInputObj = (NS) ? curInput : document.all[curInput];
                    curInputObj.value = yea + "-" + ((mon < 10)?"0":"")+mon + "-" + ((day < 10)?"0":"")+day;
          }
          return false;
}

//****************************************************************************
function calFillInside(mon, yea) {
          date1 = new Date(1, mon, yea);
          wek1 = date1.getDay()+1; monthdays = calMonthDays[mon] + (((mon == 1) && (!(yea % 4))) ? 1 : 0);
          main = (LAYERS) ? document.layers.calMain : (NS) ? document.getElementById('calMain') : document.all.calMain;
          if(LAYERS) {
                    main.width = calWinSize[0];
                    main.height = calWinSize[1] + (((monthdays + wek1) > 35) ? 16 : 0);
          } else {
                    main.style.width = calWinSize[0];
                    main.style.height = calWinSize[1] + (((monthdays + wek1) > 35) ? 16 : 0);
          }

          out="";
          if(LAYERS) {
                    out += "<table border=1 width="+(4+calWinSize[0])+" cellspacing=1 cellpadding=0><tr><th>";
                              out += "<div class=cl_dayns4>";
                              out += "<a class=cl_dayns4 href='javascript:' onclick='return calFillInside("+((mon>0)?mon-1:11)+","+yea+");'>&lt;&lt;&nbsp;</a>";
                              out += calMonths[mon];
                              out += "<a class=cl_dayns4 href='javascript:' onclick='return calFillInside("+((mon<11)?mon+1:0)+","+yea+");'>&nbsp;&gt;&gt;</a>";
                              out += "</div>";
                    out += "</th><th>";
                              out += "<div class=cl_dayns4>";
                              out += "<a class=cl_dayns4 href='javascript:' onclick='return calFillInside("+mon+","+(yea-1)+");'>&lt;&lt;&nbsp;</a>";
                              out += yea;
                              out += "<a class=cl_dayns4 href='javascript:' onclick='return calFillInside("+mon+","+(yea+1)+");'>&nbsp;&gt;&gt;</a>";
                              out += "</div>";
                    out += "</th><th>";
                              out += "<a class=cl_dayns4 href='javascript:' onclick='return calExit(0,0,0);'>&nbsp;X&nbsp;</a>";
                    out += "</th></tr></table>";
          } else {
                    out += "<table border=0 width=100% cellspacing=0 cellpadding=0><tr><th width=10% class=cl_out>";
                              out += "<select name=calMon class=cl_sel onchange='calFillInside(this.value,"+yea+")'>";
                              for(i=0; i<12; i++) out += "<option value="+i+((i==mon)?" selected":"")+">"+calMonths[i]+"</option>";
                              out += "</select>";
                    out += "</th><th width=10% class=cl_out>";
                              out += "<select name=calYea class=cl_sel onchange='calFillInside("+mon+",this.value)'>";
                              for(i=1945; i<=2010; i++) out += "<option value="+i+((i==yea)?" selected":"")+">"+i+"</option>";
                              out += "</select>";
                    out += "</th><th class=cl_out>";
                              out += "<a "+((NS)?"href='javascript:' ":"")+"style='cursor:hand; font:bold xx-small Verdana; color:black; width:100%; text-decoration:none;' onclick='return calExit(0,0,0);' title='Cerrar'>X</a><br>";
                    out += "</th></tr></table>";
          }

          out += "<table width="+((LAYERS)?4+calWinSize[0]:"100%")+" border="+((LAYERS)?1:0)+" cellspacing=1 cellpadding=0 cols=7><tr>";
                    for(i=0; i<7; i++) out += ((LAYERS) ? "<th class=cl_wek>"+calWeek[i]+"</th>" : "<th class=cl_out><div class=cl_wek>"+calWeek[i]+"</div></th>");
                    out += "</tr>";

                    if(wek1 > 0) {
                              out += "<tr>";
                              for(daycnt=0; daycnt<wek1; daycnt++) out += "<th></th>";
                    } else daycnt = 0;
                    for(i=0; i<31; i++) {
                              if(i >= monthdays) break;
                              if(!(daycnt % 7)) {
                                        if(daycnt > 0) out += "</tr>";
                                        out += "<tr>";
                              }
                              out += "<th class=cl_in onmouseover='this.className=\"cl_out\";' onmouseout='this.className=\"cl_in\";'>";
                              out += "<a class=cl_day"+((LAYERS)?"ns4 ":" ")+((NS|LAYERS)?"href='javascript:' ":"")+"onclick='return calExit("+(i+1)+","+mon+","+yea+");'>"+(i+1)+"</a></th>";
                              daycnt++;
                    }
          out += "</tr></table>";

          if(LAYERS) {
                    with(document.layers.calMain.document) {
                              open("text/html"); write(out); close();
                    }
          } else {
                    obj = (NS) ? document.getElementById('calMain') : document.all.calMain;
                    obj.innerHTML = out;
          }
          return false;
}

function setPointer(theRow, thePointerColor, theNormalBgColor)
{
    var theCells = null;

    if (thePointerColor == '' || typeof(theRow.style) == 'undefined') {
        return false;
    }
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    var rowCellsCnt  = theCells.length;
    var currentColor = null;
    var newColor     = null;
    // Opera does not return valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined' && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        newColor     = (currentColor.toLowerCase() == thePointerColor.toLowerCase())
                     ? theNormalBgColor
                     : thePointerColor;
        for (var c = 0; c < rowCellsCnt; c++) {
            theCells[c].setAttribute('bgcolor', newColor, 0);
        } // end for
    }
    else {
        currentColor = theCells[0].style.backgroundColor;
        newColor     = (currentColor.toLowerCase() == thePointerColor.toLowerCase())
                     ? theNormalBgColor
                     : thePointerColor;
        for (var c = 0; c < rowCellsCnt; c++) {
            theCells[c].style.backgroundColor = newColor;
        }
    }

    return true;
} // end of the 'setPointer()' function