﻿fontStyle        = "12px arial";           // TEXT STYLE FOR DATES
headingFontStyle = "bold 12px arial";      // TEXT STYLE FOR WEEKDAY ABBREVIATIONS

// FORMATTING PREFERENCES
bottomBorder  = false;        // TRUE/FALSE (WHETHER TO DISPLAY BOTTOM CALENDAR BORDER)
tableBorder   = 0;            // SIZE OF CALENDAR TABLE BORDER (BOTTOM FRAME) 0=none
winPrefs = "dependent=yes,width=180,height=172,titlebar=yes";

buildCalParts();
 
function ShowCal(dcn,obj)
{
	var obj2Left = aspnm_pageX(obj) - window.document.body.scrollLeft-74; 
	var obj2Top = aspnm_pageY(obj) - window.document.body.scrollTop +128; 
    winPrefs = winPrefs+",top="+obj2Top+",screenX="+obj2Top+",left="+obj2Left+",screenY="+obj2Left
	calDateField = getObj(dcn);
	setDateField(getObj(dcn));
	showCalendar();
}

function setDateField(dateField)
{
    inDate = dateField.value;
    setInitialDate();

    calDocTop    = buildTopCalFrame();
    calDocCenter = buildCenterCalFrame();
    calDocBottom = buildButtomCalFrame();
}


function setInitialDate() {
   
    if(calDateField.dformat=="dd/MM/yyyy")
    {
		var dArr = inDate.split("/")
		calDate = new Date(dArr[2],dArr[1]-1,dArr[0])
	}
    else
		calDate = new Date(inDate);

    if (isNaN(calDate)) {
        calDate = new Date();
    }

    calDay  = calDate.getDate();

    calDate.setDate(1);
}
// POPUP A WINDOW WITH THE CALENDAR IN IT
function showCalendar() {

    // USE THE JAVASCRIPT-GENERATED DOCUMENTS (calDocTop, calDocBottom) IN THE FRAMESET
    calDocFrameset = "<HTML><HEAD><meta http-equiv='Content-Type' content='text/html; charset=utf-8'><TITLE>Календарь</TITLE></HEAD>\n" +
        "<FRAMESET ROWS='24,*,26' FRAMEBORDER='0' border='0'>\n" +
        "  <FRAME NAME='topCalFrame' SRC='../blank.htm' SCROLLING='no' noresize>\n" +
        "  <FRAME NAME='centerCalFrame' SRC='../blank.htm' SCROLLING='no' noresize>\n" +        
		"  <FRAME NAME='bottomCalFrame' SRC='../blank.htm' SCROLLING='no' noresize>\n" +
        "</FRAMESET>\n";

    // DISPLAY THE CALENDAR IN A NEW POPUP WINDOW
    top.newWin = window.open("../blank.htm", "calWin", winPrefs );
    
    with(top.newWin.document)
    {
		open();
		charset="utf-8";
		write(calDocFrameset);
		close();		
	}

    with(top.newWin.frames['bottomCalFrame'].document)
    {
		open();
		charset="utf-8";
		write(calDocBottom);
		close();		
	}
    with(top.newWin.frames['topCalFrame'].document)
    {
		open();
		charset="utf-8";
		write(calDocTop);
		close();		
	}
    with(top.newWin.frames['centerCalFrame'].document)
    {
		open();
		charset="utf-8";
		write(calDocCenter);
		close();		
	}
    top.newWin.focus();
}

function buildButtomCalFrame()
{
	var calDoc = 
        "<HTML>" +
        "<HEAD>"+
        "<LINK href='StyleNew.css' type='text/css' rel='stylesheet'>" +
        "</HEAD>" +
        "<BODY  CLASS=bgColorMain bottommargin=0 topmargin=2 leftmargin=0 rightmargin=0>" +
        "<FORM NAME='calControl' onSubmit='return false;'>" +		
		"<TABLE align=center cellspacing=0 cellpadding=0>" +
		"<TR>" +
        "<TD align=center>" +
        "<INPUT TYPE=BUTTON NAME='today' VALUE='Сегодня' CLASS='btnEx' onClick='parent.opener.setToday();'>" +
		"</TD>" +
	    "<TD align=center>" +
	    "&nbsp;&nbsp;&nbsp;<INPUT TYPE=BUTTON NAME='close' CLASS='btnEx' VALUE='Закрыть' onClick='parent.close()'>" +
        "</TD>" +
        "</TR>" +
        "</TABLE>" +
        "</FORM>" +
        "</BODY>" +
        "</HTML>"
             
    return calDoc;

}

// CREATE THE TOP CALENDAR FRAME
function buildTopCalFrame() {
    // CREATE THE TOP FRAME OF THE CALENDAR    
    var calDoc =
        "<HTML>" +
        "<HEAD>" +
        "<LINK href='StyleNew.css' type='text/css' rel='stylesheet'>" +
        "</HEAD>" +
        "<BODY CLASS=bgColorMain bottommargin=0 topmargin=2 leftmargin=0 rightmargin=0>" +
        "<FORM NAME='calControl' onSubmit='return false;'>" +
        "<CENTER>" +
        "<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0>" +
        "<TR><TD align=center>" +
        "<INPUT TYPE=BUTTON NAME='previousMonth' VALUE='<'   class=btn style='padding:0px 0px 0px 0px;' onClick='parent.opener.setPreviousMonth()'>" +
        "</TD><TD align=center>" +
        getMonthSelect() +
        "</TD><TD align=center>" +
        "<INPUT TYPE=BUTTON NAME='nextMonth' VALUE='>'  class=btn style='padding:0px 0px 0px 0px;' onClick='parent.opener.setNextMonth()'>" +
        "</TD><TD align=center>&nbsp;&nbsp;" +
        "<INPUT TYPE=BUTTON NAME='previousYear' VALUE='<'  class=btn style='padding:0px 0px 0px 0px;' onClick='parent.opener.setPreviousYear()'>" +
        "</TD><TD align=center>" +
        "<INPUT NAME='year'  VALUE='" + calDate.getFullYear() + "'TYPE=TEXT SIZE=4 MAXLENGTH=4 style='WIDTH:36px' onChange='parent.opener.setYear()'>" +
        "</TD><TD align=center>" +
        "<INPUT TYPE=BUTTON NAME='nextYear' VALUE='>' class=btn style='padding:0px 0px 0px 0px;' onClick='parent.opener.setNextYear()'>" +
        "</TD>" +
        "</TR>" +
        "<TR>" +
        "<TD colspan=6 align=center height=6>" +
        "</TD>" +
        "</TR>" +
        "</TABLE>" +
        "</CENTER>" +
        "</FORM>" +
        "</BODY>" +
        "</HTML>";

    return calDoc;
}


function buildCenterCalFrame() {       

    var calDoc = calendarBegin;

    month   = calDate.getMonth();
    year    = calDate.getFullYear();


    day     = calDay;

    var i   = 0;

    var days = getDaysInMonth();

    if (day > days) {
        day = days;
    }

    var firstOfMonth = new Date (year, month, 1);

    var startingPos  = firstOfMonth.getDay()-1;
    if(startingPos==-1) startingPos = 6;
    days += startingPos;

    var columnCount = 0;

    for (i = 0; i < startingPos; i++) {

        calDoc += blankCell;
	columnCount++;
    }

    var currentDay = 0;
    var dayType    = "weekday";

    for (i = startingPos; i < days; i++) {

	var paddingChar = "&nbsp;";

        if (i-startingPos+1 < 10) {
            padding = "&nbsp;&nbsp;";
        }
        else {
            padding = "&nbsp;";
        }

        currentDay = i-startingPos+1;

        if (currentDay == day) {
            dayType = "focusDay";
            
        }
        else {
            dayType = "weekDay";
        }

        calDoc += "<TD align=center>" +
                  "<a class='" + dayType + "' href='javascript:parent.opener.returnDate(" + 
                  currentDay + ")'>" + padding + currentDay + paddingChar + "</a></TD>";

        columnCount++;

        if (columnCount % 7 == 0) {
            calDoc += "</TR><TR>";
        }
    }

    for (i=days; i<42; i++)  {

        calDoc += blankCell;
	columnCount++;

        if (columnCount % 7 == 0) {
            calDoc += "</TR>";
            if (i<41) {
                calDoc += "<TR>";
            }
        }
    }

    calDoc += calendarEnd;

    return calDoc;
}


function writeCalendar() {

    calDocCenter = buildCenterCalFrame();

    top.newWin.frames['centerCalFrame'].document.open();
    top.newWin.frames['centerCalFrame'].document.write(calDocCenter);
    top.newWin.frames['centerCalFrame'].document.close();
    
}

function setToday() {

    calDate = new Date();

    var month = calDate.getMonth();
    var year  = calDate.getFullYear();

    top.newWin.frames['topCalFrame'].document.calControl.month.selectedIndex = month;

    top.newWin.frames['topCalFrame'].document.calControl.year.value = year;

    writeCalendar();    
       
    returnDate(calDate.getDate());      
}


function setYear() {

    var year  = top.newWin.frames['topCalFrame'].document.calControl.year.value;

    if (isFourDigitYear(year)) {
        calDate.setFullYear(year);
        writeCalendar();
    }
    else {
        top.newWin.frames['topCalFrame'].document.calControl.year.focus();
        top.newWin.frames['topCalFrame'].document.calControl.year.select();
    }
}


function setCurrentMonth() {

    var month = top.newWin.frames['topCalFrame'].document.calControl.month.selectedIndex;

    calDate.setMonth(month);
    writeCalendar();
}


function setPreviousYear() {

    var year  = top.newWin.frames['topCalFrame'].document.calControl.year.value;

    if (isFourDigitYear(year) && year > 1000) {
        year--;
        calDate.setFullYear(year);
        top.newWin.frames['topCalFrame'].document.calControl.year.value = year;
        writeCalendar();
    }
}


function setPreviousMonth() {

    var year  = top.newWin.frames['topCalFrame'].document.calControl.year.value;
    if (isFourDigitYear(year)) {
        var month = top.newWin.frames['topCalFrame'].document.calControl.month.selectedIndex;

        if (month == 0) {
            month = 11;
            if (year > 1000) {
                year--;
                calDate.setFullYear(year);
                top.newWin.frames['topCalFrame'].document.calControl.year.value = year;
            }
        }
        else {
            month--;
        }
        calDate.setMonth(month);
        top.newWin.frames['topCalFrame'].document.calControl.month.selectedIndex = month;
        writeCalendar();
    }
}


function setNextMonth() {

    var year = top.newWin.frames['topCalFrame'].document.calControl.year.value;

    if (isFourDigitYear(year)) {
        var month = top.newWin.frames['topCalFrame'].document.calControl.month.selectedIndex;

        if (month == 11) {
            month = 0;
            year++;
            calDate.setFullYear(year);
            top.newWin.frames['topCalFrame'].document.calControl.year.value = year;
        }
        else {
            month++;
        }
        calDate.setMonth(month);
        top.newWin.frames['topCalFrame'].document.calControl.month.selectedIndex = month;
        writeCalendar();
    }
}


function setNextYear() {

    var year  = top.newWin.frames['topCalFrame'].document.calControl.year.value;
    if (isFourDigitYear(year)) {
        year++;
        calDate.setFullYear(year);
        top.newWin.frames['topCalFrame'].document.calControl.year.value = year;
        writeCalendar();
    }
}


function getDaysInMonth()  {

    var days;
    var month = calDate.getMonth()+1;
    var year  = calDate.getFullYear();

    if (month==1 || month==3 || month==5 || month==7 || month==8 ||
        month==10 || month==12)  {
        days=31;
    }
    else if (month==4 || month==6 || month==9 || month==11) {
        days=30;
    }
    else if (month==2)  {
        if (isLeapYear(year)) {
            days=29;
        }
        else {
            days=28;
        }
    }
    return (days);
}

function isLeapYear (Year) {
    return (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0))
}

function isFourDigitYear(year) {

    if (year.length != 4) {
        top.newWin.frames['topCalFrame'].document.calControl.year.value = calDate.getFullYear();
        top.newWin.frames['topCalFrame'].document.calControl.year.select();
        top.newWin.frames['topCalFrame'].document.calControl.year.focus();
    }
    else {
        return true;
    }
}

function getMonthSelect()
{
    monthArray = new Array('Янв', 'Фев', 'Март', 'Апр', 'Май', 'Июнь', 'Июль', 'Авг', 'Сент', 'Окт', 'Нояб', 'Дек');

    var activeMonth = calDate.getMonth();

    monthSelect = "<SELECT NAME='month' style='font:"+fontStyle+"' onChange='parent.opener.setCurrentMonth()'>";

    for (i in monthArray) {
        
        if (i == activeMonth) {
            monthSelect += "<OPTION SELECTED>" + monthArray[i] + "</OPTION>\n";
        }
        else {
            monthSelect += "<OPTION>" + monthArray[i] + "</OPTION>\n";
        }
    }
    monthSelect += "</SELECT>";

    // RETURN A STRING VALUE WHICH CONTAINS A SELECT LIST OF ALL 12 MONTHS
    return monthSelect;
}


// SET DAYS OF THE WEEK DEPENDING ON LANGUAGE
function createWeekdayList() {

    weekdayList  = new Array('Понедельник', 'Вторник', 'Среда', 'Четверг', 'Пятница', 'Суббота', 'Воскресение');
    weekdayArray = new Array('Пн','Вт','Ср','Чт','Пт','Сб','Вс');

    var weekdays = "<TR>";

    for (i in weekdayArray) {

        weekdays += "<Th align=center width=22>" + weekdayArray[i] + "</Th>";
    }
    weekdays += "</TR>";

    // RETURN TABLE ROW OF WEEKDAY ABBREVIATIONS TO DISPLAY ABOVE THE CALENDAR
    return weekdays;
}


// PRE-BUILD PORTIONS OF THE CALENDAR (FOR PERFORMANCE REASONS)
function buildCalParts() {

    // GENERATE WEEKDAY HEADERS FOR THE CALENDAR
    weekdays = createWeekdayList();

    // BUILD THE BLANK CELL ROWS
    blankCell = "<TD align=center>&nbsp;&nbsp;&nbsp;</TD>";

    // BUILD THE TOP PORTION OF THE CALENDAR PAGE USING CSS TO CONTROL SOME DISPLAY ELEMENTS
    calendarBegin =
        "<HTML>" +
        "<HEAD>" + 
//        "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>"+
        "<LINK href='StyleNew.css' type='text/css' rel='stylesheet'>" +
        "</HEAD>" +
        "<BODY CLASS=bgColorMain bottommargin=0 topmargin=4 leftmargin=0 rightmargin=0 >" +
        "<CENTER>";


        calendarBegin +=
            "<TABLE CELLPADDING=0 CELLSPACING=1 class=tableStyle ALIGN=CENTER>" +
            weekdays +
            "<TR>";


    calendarEnd = "";

        if (bottomBorder) {
            calendarEnd += "<TR></TR>";
        }

        calendarEnd +=
            "</TABLE>" +
            "</CENTER>" +
            "</BODY>" +
            "</HTML>";
}

function doNothing() {
}

function makeTwoDigit(inValue) {

    var numVal = parseInt(inValue, 10);
    if (numVal < 10)
        return("0" + numVal);
    else
        return numVal;
}

function returnDate(inDay)
{
    calDate.setDate(inDay);

    var day           = calDate.getDate();
    var month         = calDate.getMonth()+1;
    var year          = calDate.getFullYear();
    var sFormat = calDateField.dformat
    sFormat = ((!sFormat || sFormat=="")?"dd/MM/yyyy":sFormat)
    calDateField.value = sFormat.replace("dd",makeTwoDigit(day)).replace("MM",makeTwoDigit(month)).replace("yyyy",year);
    
	calDateField.click();
    top.newWin.close();
}