﻿   /*******************
      UTILITY FUNCTIONS
    ********************/
    // day of week of month's first day

    function getFirstDay(theYear, theMonth){
        var firstDate = new Date(theYear,theMonth,1)
        return firstDate.getDay()
    }
    // number of days in the month
    function getMonthLen(theYear, theMonth) {
        var oneDay = 1000 * 60 * 60 * 24
        var thisMonth = new Date(theYear, theMonth, 1)
        var nextMonth = new Date(theYear, theMonth + 1, 1)
        var len = Math.ceil((nextMonth.getTime() - 
            thisMonth.getTime())/oneDay)
        return len
    }
    // create array of English month names
    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        }
    }
    var theMonths = ["იანვარი", "თებერვალი", "მარტი", "აპრილი", "მაისი", "ივნისი", "ივლისი", "აგვისტო", "სექტემბერი", "ოქტომბერი", "ნოემბერი", "დეკემბერი"];
    if (getQueryVariable('lang')=="1") {
        var theMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    }
    // return IE4+ or W3C DOM reference for an ID
    function getObject(obj) {
        var theObj
        if (document.all) {
            if (typeof obj == "string") {
                return document.all(obj)
            } else {
                return obj.style
            }
        }
        if (document.getElementById) {
            if (typeof obj == "string") {
                return document.getElementById(obj)
            } else {
                return obj.style
            }
        }
        return null
    }
    var now = new Date();
    var _Month = now.getMonth()+1;
    var _Year = now.getFullYear();
   var kkkkkkk=true;
    function populateTable(k,Body) {
       if (!Body)
       {
        if(k)
        {
        _Month= _Month+1;    
        }
        else
        {
            _Month=_Month-1;
        }
        if(_Month < 0)
        {
            _Year=_Year-1;
            _Month=11;
        }
        if(_Month > 11)
        {
            _Year = _Year + 1;
            _Month = 0;
        }
        }
        else
        {
        _Month  =_Month-1;
        }
        var theMonth = _Month;
        var theYear = _Year;
        var firstDay = getFirstDay(theYear, theMonth)
        var howMany = getMonthLen(theYear, theMonth)
        
        // fill in month/year in table header
        getObject("tableHeader").innerHTML = theMonths[theMonth];
        getObject("Ricxvi").innerHTML = now.getDate();
        
        // initialize vars for table creation
        var dayCounter = 1
        var TBody = getObject("tableBody")
        // clear any existing rows
        while (TBody.rows.length > 0) {
            TBody.deleteRow(0)
        }
        var newR, newC
        var done=false
		firstDay=firstDay==0?7:firstDay;
        while (!done) {
            // create new row at end
            newR = TBody.insertRow(TBody.rows.length)
            for (var i = 1; i < 8; i++) {
                // create new cell at end of row
                newC = newR.insertCell(newR.cells.length)
                if (TBody.rows.length == 1 && i < firstDay) {
                    // no content for boxes before first day
                    newC.innerHTML = ""    
                    continue;
                }
                if (dayCounter == howMany) {
                    // no more rows after this one
                    done = true;
                }
                // plug in date (or empty for boxes after last day)
                newC.innerHTML = (dayCounter <= howMany) ? 
                    dayCounter++ : ""
                   var newcinn = newC.innerHTML;
                   var mm=theMonth+1;
                   newcinn = "<div style=\"margin-top:10px;\"><a class=\"cal\" href=\"?lang=" + getQueryVariable('lang') + "&date=" + (theMonth + 1) + "-" + newcinn + "-" + theYear + "\">" + newC.innerHTML + "</a></div>";
                newC.innerHTML = newcinn;
            }
        }
    }
    /*******************
      INITIALIZATIONS
    ********************/
    // create dynamic list of year choices
    function fillYears() {
        var today = new Date()
        var thisYear = today.getFullYear()
        var yearChooser = document.dateChooser.chooseYear
        for (i = thisYear; i < thisYear + 5; i++) {
            //yearChooser.options[yearChooser.options.length] = new Option(i, i)
        }
        setCurrMonth(today)
    }
    // set month choice to current month
    function setCurrMonth(today) {
        //document.dateChooser.chooseMonth.selectedIndex = today.getMonth()
    }
    function changelang() {
        if (getQueryVariable('lang') == 1) {
            document.getElementById("span1").innerHTML = "Mo";
            document.getElementById("span2").innerHTML = "Tu";
            document.getElementById("span3").innerHTML = "We";
            document.getElementById("span4").innerHTML = "Th";
            document.getElementById("span5").innerHTML = "Fr";
            document.getElementById("span6").innerHTML = "Sa";
            document.getElementById("span7").innerHTML = "Su";
        } else {
            document.getElementById("span1").innerHTML = "ორშ";
            document.getElementById("span2").innerHTML = "სამ";
            document.getElementById("span3").innerHTML = "ოთხ";
            document.getElementById("span4").innerHTML = "ხუთ";
            document.getElementById("span5").innerHTML = "პარ";
            document.getElementById("span6").innerHTML = "შაბ";
            document.getElementById("span7").innerHTML = "კვ";
        }
    }