/*
 * calendar
 */
var fixedX = -1;
var fixedY = -1;
var startAt = 1;
/*** inTableStyle - Если true - отображает календарь таблицей, иначе отображает в линию ***/
var inTableStyle = true
var crossobj, monthSelected, yearSelected, dateSelected, omonthSelected, oyearSelected, odateSelected, monthConstructed, yearConstructed, ctlToPlaceValue, ctlNow, dateFormat, nStartingYear
var bPageLoaded=false
var today = new Date()
var dateNow  = today.getDate()
var monthNow = today.getMonth()
var yearNow  = today.getYear()
var format = "yyyy-mm-dd"
var bShow = false

/*** For language packs, month/day names should be changed here  ***/
var monthName = new Array("Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь")
var dayName = new Array("Пн","Вт","Ср","Чт","Пт","Сб","Вс")

if (dom)
{
        document.write ("<div onclick='bShow=true' id='calendar' class='dateselector-div-style'>\n");
        document.write ("<table class='dateselector-table-style'>\n");
        document.write ("  <tr class='dateselector-title-background-style'>\n");
        document.write ("    <td width='100%'>\n");
        document.write ("      <table width='100%'>\n");
        document.write ("        <tr>\n");
        document.write ("          <td class='dateselector-title-style'>\n");
        document.write ("            <span id='caption'></span>\n");
        document.write ("          </td>\n");
        document.write ("        </tr>\n");
        document.write ("      </table>\n");
        document.write ("    </td>\n");
        document.write ("  </tr>\n");
        document.write ("  <tr>\n");
        document.write ("    <td width='100%' class='dateselector-body-style'>\n");
        document.write ("      <span id='content'></span>\n");
        document.write ("    </td>\n");
        document.write ("  </tr>");
        document.write ("</table>")
        document.write ("</div>");
}

function hideCalendar() {
        crossobj.visibility="hidden"
        showElement( 'SELECT' );
        showElement( 'APPLET' );
}

function padZero(num) {
        return (num < 10)? '0' + num : num ;
}

function constructDate(d,m,y)
{
        sTmp = dateFormat
        sTmp = sTmp.replace ("dd","<e>")
        sTmp = sTmp.replace ("d","<d>")
        sTmp = sTmp.replace ("<e>",padZero(d))
        sTmp = sTmp.replace ("<d>",d)
        sTmp = sTmp.replace ("mmm","<o>")
        sTmp = sTmp.replace ("mm","<n>")
        sTmp = sTmp.replace ("m","<m>")
        sTmp = sTmp.replace ("<m>",m+1)
        sTmp = sTmp.replace ("<n>",padZero(m+1))
        sTmp = sTmp.replace ("<o>",monthName[m])
        return sTmp.replace ("yyyy",y)
}

function closeCalendar() {
        hideCalendar();
        ctlToPlaceValue.value = constructDate(dateSelected,monthSelected,yearSelected)
}

function incMonth () {
        monthSelected++
        if (monthSelected>11) {
                monthSelected=0
                yearSelected++
        }
        constructCalendar()
}

function decMonth () {
        monthSelected--
        if (monthSelected<0) {
                monthSelected=11
                yearSelected--
        }
        constructCalendar()
}



/*** calendar ***/
function constructCalendar () {
        var dateMessage
        var startDate = new     Date (yearSelected,monthSelected,1)
        var endDate = new Date (yearSelected,monthSelected+1,1);
        endDate = new Date (endDate     - (24*60*60*1000));
        numDaysInMonth = endDate.getDate()

        datePointer     = 0
        dayPointer = startDate.getDay() - startAt

        if (dayPointer < 0)
        {
                dayPointer = 6
        }

        sHTML = "<table width='100%' border='0' cellpadding='2' cellspacing='0' class='dateselector-body-style'><tr>"

        if (inTableStyle)
        {
                for     (i=0; i<7; i++) {
                        sHTML += "<td align='center'><b>"+ dayName[i]+"</b></td>"
                }
                sHTML +="</tr><tr>"
                for     ( var i=1; i<=dayPointer;i++ )
                {
                        sHTML += "<td>&nbsp;</td>"
                }
        }

        for     ( datePointer=1; datePointer<=numDaysInMonth; datePointer++ )
        {
                dayPointer++;
                sHTML += "<td nowrap align='center'>"

                var sStyle="normal-day-style"; //regular day

                if ((datePointer==dateNow)&&(monthSelected==monthNow)&&(yearSelected==yearNow)) //today
                { sStyle = "current-day-style"; }

                if ((dayPointer+startAt) % 7 == startAt || ((dayPointer+startAt) % 7 == startAt-1))
                { sStyle = "end-of-weekday-style"; } //weekend

                //selected day
                if ((datePointer==odateSelected) && (monthSelected==omonthSelected) && (yearSelected==oyearSelected))
                { sStyle += " selected-day-style"; }

                sHint = constructDate(datePointer, monthSelected, yearSelected)

                var regexp= /\"/g
                sHint=sHint.replace(regexp,"&quot;")

                sHTML += "<a class='"+sStyle+"' title=\"" + sHint + "\" href='#' onClick='javascript:dateSelected="+datePointer+";closeCalendar();return false'>" + datePointer + "</a>"
                if (((dayPointer+startAt) % 7 == startAt) && inTableStyle) {
                        sHTML += "</tr><tr>"
                }
        }

        document.getElementById("content").innerHTML   = sHTML
        document.getElementById("spanMonth").innerHTML = monthName[monthSelected]
        document.getElementById("spanYear").innerHTML = yearSelected
}

function popUpCalendar(ctl) {
        var     leftpos=0
        var     toppos=0

  DocumentRegisterEvents();
        if (bPageLoaded)
        {
                if ( crossobj.visibility ==     "hidden" ) {
                        ctlToPlaceValue = ctl
                        dateFormat=format;

                        formatChar = " "
                        aFormat = dateFormat.split(formatChar)
                        if (aFormat.length<3)
                        {
                                formatChar = "/"
                                aFormat = dateFormat.split(formatChar)
                                if (aFormat.length<3)
                                {
                                        formatChar = "."
                                        aFormat = dateFormat.split(formatChar)
                                        if (aFormat.length<3)
                                        {
                                                formatChar = "-"
                                                aFormat = dateFormat.split(formatChar)
                                                if (aFormat.length<3)
                                                {
                                                        // invalid date format
                                                        formatChar=""
                                                }
                                        }
                                }
                        }

                        tokensChanged = 0
                        if ( formatChar != "" )
                        {
                                // use user's date
                                aData = ctl.value.split(formatChar)

                                for     (i=0;i<3;i++)
                                {
                                        if ((aFormat[i]=="d") || (aFormat[i]=="dd"))
                                        {
                                                dateSelected = parseInt(aData[i], 10)
                                                tokensChanged ++
                                        }
                                        else if ((aFormat[i]=="m") || (aFormat[i]=="mm"))
                                        {
                                                monthSelected = parseInt(aData[i], 10) - 1
                                                tokensChanged ++
                                        }
                                        else if (aFormat[i]=="yyyy")
                                        {
                                                yearSelected = parseInt(aData[i], 10)
                                                tokensChanged ++
                                        }
                                        else if (aFormat[i]=="mmm")
                                        {
                                                for     (j=0; j<12;     j++)
                                                {
                                                        if (aData[i]==monthName[j])
                                                        {
                                                                monthSelected=j
                                                                tokensChanged ++
                                                        }
                                                }
                                        }
                                }
                        }

                        if ((tokensChanged!=3)||isNaN(dateSelected)||isNaN(monthSelected)||isNaN(yearSelected))
                        {
                                dateSelected = dateNow
                                monthSelected = monthNow
                                yearSelected = yearNow
                        }

                        odateSelected=dateSelected
                        omonthSelected=monthSelected
                        oyearSelected=yearSelected

                        aTag = ctl
                        do {
                                aTag = aTag.offsetParent;
                                leftpos += aTag.offsetLeft;
                                toppos += aTag.offsetTop;
                        } while(aTag.tagName!="BODY");
                        if (ie) { leftpos++; toppos-- }

                        crossobj.left = (fixedX==-1 ? ctl.offsetLeft + leftpos  : fixedX) + 'px'
                        crossobj.top = (fixedY==-1 ? ctl.offsetTop + toppos + ctl.offsetHeight : fixedY) + 'px'
                        constructCalendar (1, monthSelected, yearSelected);
                        crossobj.visibility=(dom||ie)? "visible" : "show"

                        hideElement( 'SELECT', document.getElementById("calendar") );
                        hideElement( 'APPLET', document.getElementById("calendar") );

                        bShow = true;
                }
        }
        else
        {
                DateSelectorInit()
                popUpCalendar(ctl)
        }
}

function DateSelectorInit()     {
        if (!ns4)
        {
                if (!ie) { yearNow += 1900      }

                crossobj=(dom)?document.getElementById("calendar").style : ie? document.all.calendar : document.calendar
                hideCalendar()

                monthConstructed=false;
                yearConstructed=false;

                sHTML1 = "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
                sHTML1 += "<tr>\n";
                sHTML1 += "     <td width='5'><span id='spanLeft' class='dateselector-title-control-normal-style' onclick='javascript:decMonth()' title='« Предыдущий месяц'><<</span></td>\n";
                sHTML1 += "     <td width='100%' align='center'><span id='spanMonth' class='dateselector-title-control-normal-style'></span>&nbsp;<span id='spanYear' class='dateselector-title-control-normal-style'></span></td>\n";
                sHTML1 += "     <td width='5'><span id='spanRight' class='dateselector-title-control-normal-style' onclick='incMonth()' title='Следующий месяц »'>>></span></td>\n";
                sHTML1 += "</tr>\n";
                sHTML1 += "</table>\n";

                document.getElementById("caption").innerHTML  = sHTML1

                bPageLoaded=true
        }
}

function DocumentRegisterEvents()
{
  document.onkeypress = function hideCalender_Trap1 ()
  {
          if (ie && (window.event.keyCode == 27))
          {
      hideCalendar();
          }
  }

  document.onclick = function hideCalender_Trap2()
  {
          if (!bShow)
          {
      hideCalendar();
          }
          bShow = false
  }
}

