var now = new Date()
var real = new Date()
var caution = false

var realyear = now.getYear()
var realmonth = now.getMonth()
var realmonthName = getMonthName(month)
var realdate = now.getDate()

var year = getYear();
var month = now.getMonth()
var monthName = getMonthName(month)
var date = now.getDate()

function fixDate(date) {
        var base = new Date(0)
        var skew = base.getTime()
        if (skew > 0)
        date.setTime(date.getTime() - skew)
}

function winOpen(digit, m, year){
        var url;
         d = (digit > 9) ? digit : '0'+digit;
         m = (m > 9) ? m : '0'+m;
         window.open('/exsite2/heart/edit_mod_data_page.php?module=mod_xfstat&mod_data[series]='+series+'&mod_data[date]=' + year + '-'+ m + '-' + d + '&action=calendar_games&func=print','','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,height=500 ,width=570');
}

function getYear(){
        var now = new Date()
        var year = now.getFullYear()
        return year;
}

function getTime() {
        // initialize time-related variables with current time settings
        var now = new Date()
        var hour = now.getHours()
        var minute = now.getMinutes()
        now = null
        var ampm = ""

        // validate hour values        and set value of ampm
        if (hour >= 12) {
                hour -= 12
                ampm = "PM"
        } else
        ampm = "AM"
        hour = (hour == 0) ? 12 : hour

        // add zero digit to a one digit minute
        if (minute < 10)
        minute = "0" + minute // do not parse this number!

        // return time string
        return hour + ":" + minute + " " + ampm
}

function leapYear(year) {
        if (year % 4 == 0) // basic rule
        return true // is leap year
        return false // is not leap year
}

function getDays(month, year) {
        // create array to hold number of days in each month
        var ar = new Array(12)
        ar[0] = 31 // January
        ar[1] = (leapYear(year)) ? 29 : 28 // February
        ar[2] = 31 // March
        ar[3] = 30 // April
        ar[4] = 31 // May
        ar[5] = 30 // June
        ar[6] = 31 // July
        ar[7] = 31 // August
        ar[8] = 30 // September
        ar[9] = 31 // October
        ar[10] = 30 // November
        ar[11] = 31 // December

        // return number of days in the specified month (parameter)
        return ar[month]
}

function getMonthName(month) {
        // create array to hold name of each month
        var ar = new Array(12)
        ar[0] = "ßÍÂÀÐÜ"
        ar[1] = "ÔÅÂÐÀËÜ"
        ar[2] = "ÌÀÐÒ"
        ar[3] = "ÀÏÐÅËÜ"
        ar[4] = "ÌÀÉ"
        ar[5] = "ÈÞÍÜ"
        ar[6] = "ÈÞËÜ"
        ar[7] = "ÀÂÃÓÑÒ"
        ar[8] = "ÑÅÍÒßÁÐÜ"
        ar[9] = "ÎÊÒßÁÐÜ"
        ar[10] = "ÍÎßÁÐÜ"
        ar[11] = "ÄÅÊÀÁÐÜ"

        // return name of specified month (parameter)
        return ar[month]
}

function setCal() {
        // standard time attributes


        //        var year = now.getYear()
        //        var month = now.getMonth()
        //        var monthName = getMonthName(month)
        //        var date = now.getDate()
        now = null

        // create instance of first day of month, and extract the day on which it occurs
        var firstDayInstance = new Date(year, month, 1)
        var firstDay = firstDayInstance.getDay()
        firstDayInstance = null

        // number of days in current month
        var days = getDays(month, year)

        // call function to draw calendar
        drawCal(firstDay, days, date, monthName, year)
}
function calnew(yy,mm)
{
        if (mm==0) {mm1="01";}
        if (mm==1) {mm1="02";}
        if (mm==2) {mm1="03";}
        if (mm==3) {mm1="04";}
        if (mm==4) {mm1="05";}
        if (mm==5) {mm1="06";}
        if (mm==6) {mm1="07";}
        if (mm==7) {mm1="08";}
        if (mm==8) {mm1="09";}
        if (mm==9) {mm1="10";}
        if (mm==10) {mm1="11";}
        if (mm==11) {mm1="12";}
        //calendar refresh

        var firstDayInstance = new Date(year, month, 1)
        var firstDay = firstDayInstance.getDay()
        firstDayInstance = null
        var year1 = document.calen.y.value;
        var month1 = document.calen.mes.value
        // number of days in current month
        var days = getDays(mm, yy)
        drawCal(firstDay, days, date, month1, year1)
};
function drawCal(firstDay, lastDate, date, monthName, year) {
        // constant table settings
        var headerHeight = 35 // height of the table's header cell
        var border = 0 // 3D height of table's border
        var cellspacing = 0 // width of table's border
        var headerColor = "00008b" // color of table's header
        var headerSize = "+2" // size of tables header font
        var colWidth = 45 // width of columns in table
        var dayCellHeight = 15 // height of cells containing days of the week
        var dayColor = "000000" // color of font representing week days
        var cellHeight = 25 // height of cells representing dates in the calendar
        var todayColor = "red" // color specifying today's date in the calendar
        var timeColor = "purple" // color of font representing current time
        var dayCellcolor="87ceda"
        var monthColor="6485ed"


        // create basic table structure
        var text = '' // initialize accumulative variable to empty string
        text += '<form method=get target=_parent name=calen><input type=hidden name=cal value=1>'
        text += '<div id=calendar>'
        text += '<span>'
        text += '<select style="margin: 5 10 0 5" name=mes onchange="month=document.all.calen.mes.value-1;calnew(year,month)">'
        text += '<option value=01 '+((month==0) ? 'selected' : '')+'>January'
        text += '<option value=02 '+((month==1) ? 'selected' : '')+'>February'
        text += '<option value=03 '+((month==2) ? 'selected' : '')+'>March'
        text += '<option value=04 '+((month==3) ? 'selected' : '')+'>April'
        text += '<option value=05 '+((month==4) ? 'selected' : '')+'>May'
        text += '<option value=06 '+((month==5) ? 'selected' : '')+'>June'
        text += '<option value=07 '+((month==6) ? 'selected' : '')+'>July'
        text += '<option value=08 '+((month==7) ? 'selected' : '')+'>August'
        text += '<option value=09 '+((month==8) ? 'selected' : '')+'>September'
        text += '<option value=10 '+((month==9) ? 'selected' : '')+'>October'
        text += '<option value=11 '+((month==10) ? 'selected' : '')+'>November'
        text += '<option value=12 '+((month==11) ? 'selected' : '')+'>December'
        text += '</select>'
        text += '</span>'
        text += '<span>'
        text += '<select name=y onchange="year=document.all.calen.y.value;calnew(year,month)">'

        //for (var i=realyear;i>2003;i--)
        //{
        //        text += '<option  '+((year==i) ? 'selected' : '')+' value='+i+'>'+i+'</option>'
        //};
        text += '<option  '+((year==2004) ? 'selected' : '')+' value='+2004+'>'+2004+'</option>'
        text += '<option  '+((year==2005) ? 'selected' : '')+' value='+2005+'>'+2005+'</option>'
        text += '<option  '+((year==2006) ? 'selected' : '')+' value='+2006+'>'+2006+'</option>'
        text += '<option  '+((year==2007) ? 'selected' : '')+' value='+2007+'>'+2007+'</option>'
        text += '<option  '+((year==2008) ? 'selected' : '')+' value='+2008+'>'+2008+'</option>'
        text += '<option  '+((year==2009) ? 'selected' : '')+' value='+2009+'>'+2009+'</option>'
        text += '<option  '+((year==2010) ? 'selected' : '')+' value='+2010+'>'+2010+'</option>'
        text += '<option  '+((year==2011) ? 'selected' : '')+' value='+2011+'>'+2011+'</option>'
        text += '<option  '+((year==2012) ? 'selected' : '')+' value='+2012+'>'+2012+'</option>'
        text += '<option  '+((year==2013) ? 'selected' : '')+' value='+2013+'>'+2013+'</option>'
        text += '<option  '+((year==2014) ? 'selected' : '')+' value='+2014+'>'+2014+'</option>'

        text += '</select>'
        text += '</span>'
        text += '<ul id=day><li>Mn</li><li>Tue</li><li>Wd</li><li>Thu</li><li>Fr</li><li id=red>St</li><li id=red>Sn</li></ul>'
        // variables to hold constant settings

        // create array of abbreviated day names
        var weekDay = new Array(7)
        weekDay[0] = "Ïí"
        weekDay[1] = "Âò"
        weekDay[2] = "Ñð"
        weekDay[3] = "×ò"
        weekDay[4] = "Ïò"
        weekDay[5] = "Ñá"
        weekDay[6] = "Âñ"


        // declaration and initialization of two variables to help with tables
        var digit = 1
        var curCell = 1
        var m=month+1;
        if (m==1) { m="01"; }
        if (m==2) { m="02"; }
        if (m==3) { m="03"; }
        if (m==4) { m="04"; }
        if (m==5) { m="05"; }
        if (m==6) { m="06"; }
        if (m==7) { m="07"; }
        if (m==8) { m="08"; }
        if (m==9) { m="09"; }
        if (firstDay==0) firstDay=7;
        if(window.opera) {
                text += '<div id=Number><table cellpadding=0 cellspacing=0 border=0 bgcolor=white width="202px">'
        }
        else {
                text += '<div id=Number><table cellpadding=0 cellspacing=0 border=0 bgcolor=white width="202px">'
        }
        for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
                text += '<tr>'
                for (var col = 1; col <= 7; ++col) {
                        if (digit > lastDate)
                        break
                        if (curCell < firstDay) {
                                text += '<td>&nbsp;</td>';
                                curCell++
                        } else {
                                var query;
                                        if(digit <= 9) {
                                                query = '0'+digit + '-'+m+'-'+year;
                                        }
                                        else {
                                                query = digit + '-'+m+'-'+year;
                                        }


                                if(dates[query] != undefined) {
                                        if (digit == date) { // current cell represent today's date
                                                if (digit <= 9) {
                                                        text += '<td id=active width=14% align=center><a href="#" onclick="winOpen(0'+digit+', '+m+', '+year+')"/><div class="rekans">0'+digit+'</div></a></td>'
                                                } else {
                                                        text += '<td id=active width=14% align=center><a href="#" onclick="winOpen('+digit+', '+m+', '+year+')"/><div class="rekans">'+digit+'</div></a></td>'
                                                }
                                        } else
                                        if (digit <= 9) {
                                                text += '<td class=rekans2 align=center><a href="#" onclick="winOpen('+digit+', '+m+', '+year+')"/><div class="rekans">0'+digit+'</div></a></td>'
                                        } else {
                                                text += '<td class=rekans2 align=center><a href="#" onclick="winOpen('+digit+', '+m+', '+year+')"/><div class="rekans">'+digit+'</div></a></td>'
                                        }
                                }
                                else {
                                        if (digit == date) { // current cell represent today's date
                                                if (digit <= 9) {
                                                        text += '<td id=active align=center>0'+digit+'</td>'
                                                } else {
                                                        text += '<td id=active align=center>'+digit+'</td>'
                                                }
                                        } else
                                        if (digit <= 9) {
                                                text += '<td class=rekans3>0'+digit+'</td>'
                                        } else {
                                                text += '<td class=rekans3>'+digit+'</td>'
                                        }

                                }


                                digit++
                        }
                }
                text += '</tr>'
        }
        text += '</table>';
        div = document.getElementById("calendar");
        if(window.opera) {
                div.outerHTML = text;
        }
        else {
                document.getElementById("calendar").innerHTML = text;
        }
}