var form_name   = "calendar-form";
var page_name   = "allNews.php";
var format      = 'eu'; // eu or us
var days        = new Array('Ïîí', 'Âò', 'Ñð', '×ò', 'Ïò', 'Ñóá', 'Âñ');
var months      = new Array('ßíâàðü','Ôåâðàëü','Ìàðò','Àïðåëü','Ìàé','Èþíü','Èþëü','Àâãóñò','Ñåíòÿáðü','Îêòÿáðü','Íîÿáðü','Äåêàáðü');
var last_click  = new Array();
var current_month  = '';
var current_year   = '';
var last_date  = '';
    
function calendar(id, d, highlight, adjusted)
{       
    if (adjusted == undefined)
    {   
        var d = new Date(d * 1000);
    }

    this.id         = id;
    this.highlight  = highlight;
    this.date_obj   = d;
    this.write      = build_calendar;
    this.total_days = total_days;
    this.month      = d.getMonth();
    this.date       = d.getDate();
    this.day        = d.getDay();
    this.year       = d.getFullYear();
    this.hours      = d.getHours();
    this.minutes    = d.getMinutes();
    this.seconds    = d.getSeconds();
    this.date_str   = date_str;
                
    if (highlight == false)
    {
        this.selected_date = '';
    }
    else
    {
        this.selected_date = this.year + '' + this.month + '' + this.date; 
    }
            
    //  Set the "selected date"
    d.setDate(1);
    this.firstDay = (d.getDay()-1);
    
    //then reset the date object to the correct date
    d.setDate(this.date);
}
        
//  Build the body of the calendar
function build_calendar()
{
    var str = '';
    
    //  Calendar Heading
    
    str += '<div id="cal' + this.id + '">';
    str += '<div id="calendar-title">';
    str += '<div id="clgrad"></div><div id="crgrad"></div>';
    str += '<div id="calendar-month">' + months[this.month] + ' ' + this.year + '<\/div>';
    str += '<div id="larr" onclick="change_month(-1, \'entry_date\')"></div>';
    str += '<div id="rarr" onclick="change_month(1, \'entry_date\')"></div>';
    str += '<\/div>';
    str += '<table>';
    
    //  Day Names
    
    str += '<tr class="head">';
    
    for (i = 0; i < 7; i++)
    {
        str += '<td>' + days[i] + '<\/td>';
    }
    
    str += '<\/tr>';
    
    //  Day Cells
        
    str += '<tr class="date">';
    
    selDate = (last_date != '') ? last_date : this.date;
    
    if (this.firstDay < 0) {
      this.firstDay = 6;
    }
    for (j = 0; j < 42; j++)
    {
        var displayNum = (j - this.firstDay + 1);
        var wd = j % 7;
        var wclass;
        if (wd == 5 || wd == 6) {
          wclass = "weekend";
        } else {
          wclass = "work";
        }
        
        if (j < this.firstDay) // leading empty cells
        {
            str += '<td>&nbsp;<\/td>';
        }
        else if (displayNum == selDate && this.highlight == true) // Selected date
        {
            str += '<td id="' + this.id +'selected" class="' + wclass + '" onClick="set_date(this,\'' + this.id + '\')">' + displayNum + '<\/td>';
        }
        else if (displayNum > this.total_days())
        {
            //str += '<td class="calblankbot">&nbsp;<\/td>'; // trailing empty cells
            break;
        }
        else  // Unselected days
        {
            str += '<td id="" class="' + wclass + '" onClick="set_date(this,\'' + this.id + '\'); return false;">' + displayNum + '<\/td>';
        }
        
        if (wd == 6 && displayNum < this.total_days())
        {
            str += '<\/tr><tr class="date">';
        }
    }

    str += '<\/tr>';    
    str += '<\/table>';
    str += '<\/div>';
    
    return str;
}

//  Total number of days in a month
function total_days()
{   
    switch(this.month)
    {
        case 1: // Check for leap year
            if ((  this.date_obj.getFullYear() % 4 == 0
                && this.date_obj.getFullYear() % 100 != 0)
                || this.date_obj.getFullYear() % 400 == 0)
                return 29; 
            else
                return 28;
        case 3:
            return 30;
        case 5:
            return 30;
        case 8:
            return 30;
        case 10:
            return 30
        default:
            return 31;
    }
}

//  Highlight Cell on Mouseover
function cell_highlight(td, num, cal)
{
    cal = eval(cal);

    if (last_click[cal.id]  != num)
    {
        td.className = "caldaycellhover";
    }
}       

//  Reset Cell on MouseOut
function cell_reset(td, num, cal)
{   
    cal = eval(cal);

    if (last_click[cal.id] == num)
    {
        td.className = "caldayselected";
    }
    else
    {
        td.className = "caldaycells";
    }
}       

//  Clear Field
function clear_field(id)
{               
    eval("document." + form_name + "." + id + ".value = ''");
    
    document.getElementById(id + "selected").className = "caldaycells";
    document.getElementById(id + "selected").id = "";   
    
    cal = eval(id);
    cal.selected_date = '';     
}       


//  Set date to specified time
function set_to_time(id, raw)
{           
    if (document.getElementById(id + "selected"))
    {           
        document.getElementById(id + "selected").className = "caldaycells";
        document.getElementById(id + "selected").id = "";   
    }
    
    document.getElementById('cal' + id).innerHTML = '<div id="tempcal'+id+'">&nbsp;<'+'/div>';              
        
    var nowDate = new Date();
    nowDate.setTime = raw * 1000;
    
    current_month   = nowDate.getMonth();
    current_year    = nowDate.getFullYear();
    current_date    = nowDate.getDate();
    
    oldcal = eval(id);
    oldcal.selected_date = current_year + '' + current_month + '' + current_date;               

    cal = new calendar(id, nowDate, true, true);        
    cal.selected_date = current_year + '' + current_month + '' + current_date;  
    
    last_date = cal.date;
    
    document.getElementById('tempcal'+id).innerHTML = cal.write();  
    
    insert_date(cal);
}

//  Set date to what is in the field
var lastDates = new Array();

function update_calendar(id, dateValue)
{
    if (lastDates[id] == dateValue) return;
    
    lastDates[id] = dateValue;
    
    var fieldString = dateValue.replace(/\s+/g, ' ');
    
    while (fieldString.substring(0,1) == ' ')
    {
        fieldString = fieldString.substring(1, fieldString.length);
    }
    
    var dateString = fieldString.split(' ');
    var dateParts = dateString[0].split('-')

    if (dateParts.length < 3) return;
    var newYear  = dateParts[0];
    var newMonth = dateParts[1];
    var newDay   = dateParts[2]; 
    
    if (isNaN(newDay)  || newDay < 1 || (newDay.length != 1 && newDay.length != 2)) return;
    if (isNaN(newYear) || newYear < 1 || newYear.length != 4) return;
    if (isNaN(newMonth) || newMonth < 1 || (newMonth.length != 1 && newMonth.length != 2)) return;
    
    if (newMonth > 12) newMonth = 12;
    
    if (newDay > 28)
    {
        switch(newMonth - 1)
        {
            case 1: // Check for leap year
                if ((newYear % 4 == 0 && newYear % 100 != 0) || newYear % 400 == 0)
                {
                    if (newDay > 29) newDay = 29; 
                }
                else
                {
                    if (newDay > 28) newDay = 28;
                }
            case 3:
                if (newDay > 30) newDay = 30;
            case 5:
                if (newDay > 30) newDay = 30;
            case 8:
                if (newDay > 30) newDay = 30;
            case 10:
                if (newDay > 30) newDay = 30;
            default:
                if (newDay > 31) newDay = 31;
        }
    }
    
    if (document.getElementById(id + "selected"))
    {           
        document.getElementById(id + "selected").className = "caldaycells";
        document.getElementById(id + "selected").id = "";   
    }
    
    document.getElementById('cal' + id).innerHTML = '<div id="tempcal'+id+'">&nbsp;<'+'/div>';              
        
    var nowDate = new Date();
    nowDate.setDate(newDay);
    nowDate.setMonth(newMonth - 1);
    nowDate.setYear(newYear);
    nowDate.setHours(12);
    
    current_month   = nowDate.getMonth();
    current_year    = nowDate.getFullYear();

    cal = new calendar(id, nowDate, true, true);                        
    document.getElementById('tempcal'+id).innerHTML = cal.write();  
}

//  Set the date
function set_date(td, ccl)
{                
    ccl = entry_date;
    var year  = ccl.date_obj.getFullYear();
    var month = ccl.date_obj.getMonth() + 1;
    if (month < 10) {
      month = '0' + month;
    } 
    var date = td.firstChild.nodeValue;
    if (date < 10) {
      date = '0' + date; 
    } 
    location.href = "/allNews.php?date=" + year + '-' + month + '-' + date;
}

//  Insert the date into the form field
function insert_date(cal)
{
    cal = eval(cal);
    fval = eval("document." + form_name + "." + cal.id);    
    
    if (fval.value == '')
    {
        fval.value = cal.date_str('Y');
    }
    else
    {
        //time = fval.value.substring(10);
        new_date = cal.date_str('n') + time;
        fval.value = new_date;
    }
    window.location = page_name+"?date="+fval.value;
    return false;
}
        
//  Remove the date from the form field
function remove_date(cal)
{
    cal = eval(cal);
    fval = eval("document." + form_name + "." + cal.id);    
    fval.value = '';
}

//  Change to a new month
function change_month(mo, ccl)
{       
    ccl = eval(ccl);

    if (current_month != '')
    {
        ccl.date_obj.setMonth(current_month);
        ccl.date_obj.setYear(current_year);
    
        current_month   = '';
        current_year    = '';
    }
                
    var newMonth = ccl.date_obj.getMonth() + mo;
    var newDate  = ccl.date_obj.getDate();
    
    if (newMonth == 12) 
    {
        ccl.date_obj.setYear(ccl.date_obj.getFullYear() + 1)
        newMonth = 0;
    }
    else if (newMonth == -1)
    {
        ccl.date_obj.setYear(ccl.date_obj.getFullYear() - 1)
        newMonth = 11;
    }
    
    if (newDate > 28)
    {
        var newYear = ccl.date_obj.getFullYear();
        
        switch(newMonth)
        {
            case 1: // Check for leap year
                if ((newYear % 4 == 0 && newYear % 100 != 0) || newYear % 400 == 0)
                {
                    if (newDate > 29) newDate = 29; 
                }
                else
                {
                    if (newDate > 28) newDate = 28;
                }
            case 3:
                if (newDate > 30) newDate = 30;
            case 5:
                if (newDate > 30) newDate = 30;
            case 8:
                if (newDate > 30) newDate = 30;
            case 10:
                if (newDate > 30) newDate = 30;
            default:
                if (newDate > 31) newDate = 31;
        }
    }
    
    ccl.date_obj.setDate(newDate);
    ccl.date_obj.setMonth(newMonth);
    new_mdy = ccl.date_obj.getFullYear() + '' + ccl.date_obj.getMonth() + '' + ccl.date;
    
    highlight = (ccl.selected_date == new_mdy) ? true : false;          
    cal = new calendar(ccl.id, ccl.date_obj, highlight, true);          
    document.getElementById('cal' + ccl.id).innerHTML = cal.write();    
}

//  Finalize the date string
function date_str(time)
{
    var month = this.month + 1;
    if (month < 10)
        month = '0' + month;
        
    var day     = (this.date  < 10)     ?  '0' + this.date      : this.date;
    var minutes = (this.minutes  < 10)  ?  '0' + this.minutes   : this.minutes;
        
    if (format == 'us')
    {
        var hours   = (this.hours > 12) ? this.hours - 12 : this.hours;
        var ampm    = (this.hours > 11) ? 'PM' : 'AM'
    }
    else
    {
        var hours   = this.hours;
        var ampm    = '';
    }
    
    if (time == 'y')
    {
        return this.year + '-' + month + '-' + day + '  ' + hours + ':' + minutes + ' ' + ampm;     
    }
    else
    {
        return this.year + '-' + month + '-' + day;
    }
}


        var entry_date = new calendar("entry_date", 1401080646, true);
        $(function () {
        var cld = document.getElementById("calendar");
        cld.innerHTML = entry_date.write();});
    