weekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
monthes = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

/**
 * 
 */
function getWeekday(year, month, day) {
    var date = new Date(year, month - 1, day);
    return date.getDay();
}

/**
 * 
 */
function getDaysInMonth(month, year) {
    var r = month_days[month - 1];
    if (month == 2 && !(year % 4) && (!(year % 400) || (year % 100))) {
        r++;
    }
    return r;
}

/**
 * 
 */
function isFuture(year, month, day) {
    var date = new Date(year, month - 1, day);
    var cur_date = new Date();
    r = (date.getTime() > cur_date.getTime());
    return r;
}

function isDisabled(year, month, day){
    var date = new Date(year, month - 1, day);
    var max_date = new Date(2015, 11, 31);
    r = (date.getTime() > max_date.getTime());
    return r;
}



/**
 * findPosX & findPosY courtesy PPK
 * http://www.quirksmode.org/js/findpos.html
 */
function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    } else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}

/**
 * 
 */
function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    } else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}

/**
 * 
 */
function trim(s) {
    while (s.substring(0, 1) == ' ') {
        s = s.substring(1, s.length);
    }
    while (s.substring(s.length - 1, s.length) == ' ') {
        s = s.substring(0,s.length - 1);
    }
    return s;
}

/**
 * 
 */
function setcookie(name, value, expires, path, domain) {
    document.cookie = name + "=" + escape(value) +
    ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
    ((path == null)    ? "; path=/" : "; path=" + path) +
    ((domain == null)  ? "" : "; domain=" + domain);
}

/**
 * 
 */
function getcookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) {
            return null;
        }
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * 
 */
function closeCalendarOutsideClick(event) {
    if (!event) {
        event = window.event;
    }
    if (document.all) {
        var t = window.event.srcElement;
        while (t.parentElement != null) {
            if (t.id == 'calendar' || t.id == 'start' || t.id == 'finish' || t.id == 'licenseExpirationPeriodFrom' || t.id == 'licenseExpirationPeriodTo') {
                return false;
            }
            t = t.parentElement;
        }
        return true;
    } else if (event) {
        var t = event.originalTarget;
        while (t.parentNode != null) {
            if (t.id == 'calendar' || t.id == 'start' || t.id == 'finish' || t.id == 'licenseExpirationPeriodFrom' || t.id == 'licenseExpirationPeriodTo') {
                return false;
            }
            t = t.parentNode;
        }
        return true;
    }
    return true;
}

/**
 * 
 */
function createMouseupListener() {
    if (document.layers) {
        document.captureEvents(Event.MOUSEUP);
    }
    document.onmouseup = function closeCalendar(event) {
        if (closeCalendarOutsideClick(event)) {
            Calendar.killCalendar();
        }
    }
}

/**
 * 
 */
function killMouseupListener() {
    if (document.layers) {
        document.releaseEvents(Event.MOUSEUP);
    }
}

/*
 *
 */
function oCalendar(name) {
    this.month = 5;
    this.day = 3;
    this.year = 2005;
    
    this.cur_month = 5;
    this.cur_day = 3;
    this.cur_year = 2005;
    
    this.oInput = false;
    

    this.setYear = function() {
        var d = new Date();
        now_year = d.getFullYear();
        if (arguments.length) {
            year = parseInt(arguments[0], 10);
						if (year < now_year - 20)
						{
							    year = now_year - 20;
						} 
						else if (year > now_year + 10) 
						{
							    year = now_year + 10;
						}
        } else {
					  year = now_year;
        }
        this.cur_year = this.year = year;
    }

    this.setMonth = function() {
        if (arguments.length) 
        {

            month = parseInt(arguments[0], 10);
            
            if (month > 12) 
            {
                month = 12;
            } 
            else if (month < 1) 
            {
                month = 1;
            }
        } else {
            var d = new Date();
            month = d.getMonth();
        }
        this.cur_month = this.month = month;
    }
    
    this.setDay = function() {
        if (arguments.length) {
            day = parseInt(arguments[0], 10);
            if (day > getDaysInMonth(this.month, this.year)) {
                day = getDaysInMonth(this.month, this.year);
            } else if (day < 1) {
                day = 1;
            }
        } else {
            var d = new Date();
            day = d.getDate();
        }
        this.cur_day = this.day = day;
    }

    this.createCalendarHeader = function() {
        var oTable = document.createElement('TABLE');
        var oTbody = document.createElement('TBODY');
        var oTr = document.createElement('TR');
        var oTd = document.createElement('TD');
        oTable.className = 'calendar-header';
        oTd.className = 'previous';
        oTd.innerHTML = '<div style="padding: 2px;"><span style="cursor: pointer; cursor: hand;" onclick="Calendar.drawPrevMonth()">&lt;&nbsp;' + monthes[this.getPrevMonth() - 1] + '</span></div>';
        oTd.innerHTML += '<div style="padding: 2px;"><span style="cursor: pointer; cursor: hand; font-size: 85%;" onclick="Calendar.drawPrevYear()">&lt;&nbsp;' + (this.year - 1) + '</span></div>';
        oTr.appendChild(oTd.cloneNode(true));
    
        oTd.className = 'current';
        oTd.innerHTML = '<div>' + monthes[this.month - 1] + '</div>';
        oTd.innerHTML += '<div style="font-size: 75%;">' + this.year + '</div>';
        oTr.appendChild(oTd.cloneNode(true));
    
        oTd.className = 'next';
        if (isDisabled(this.year, this.month + 1, 1)) {
            oTd.innerHTML = '<div style="padding: 2px;"><span class="next-disabled">' + monthes[this.getNextMonth() - 1] + '&nbsp;&gt;</span></div>';
        } else {
            oTd.innerHTML = '<div style="padding: 2px;"><span style="cursor: pointer; cursor: hand;" onclick="Calendar.drawNextMonth()">' + monthes[this.getNextMonth() - 1] + '&nbsp;&gt;</span></div>';
        }
        if (isDisabled(this.year + 1, 1, 1)) {
            oTd.innerHTML += '<div style="padding: 2px;"><span class="next-disabled" style="font-size: 85%;">' + (this.year + 1) + '&nbsp;&gt;</span></div>';
        } else {
            oTd.innerHTML += '<div style="padding: 2px;"><span style="cursor: pointer; cursor: hand; font-size: 85%;" onclick="Calendar.drawNextYear()">' + (this.year + 1) + '&nbsp;&gt;</span></div>';
        }
        oTr.appendChild(oTd.cloneNode(true));
    
        oTbody.appendChild(oTr);
        oTable.appendChild(oTbody);
        return oTable;
    }
    
    this.createCalendarWeekdays = function() {
        var oTr = document.createElement('TR');
        oTr.className = 'weekday';
        for (k = 0; k < 7; k++) {
            var oTd = document.createElement('TD');
            oTd.className = 'weekday';
            var oText = document.createTextNode(' ');
            oText.nodeValue = weekdays[k];
            oTd.appendChild(oText.cloneNode(true));
            oTr.appendChild(oTd.cloneNode(true));
        }
        return oTr;
    }
    
    this.createCalendar = function() {
        var oTable = document.createElement('TABLE');
        var oTbody = document.createElement('TBODY');
        oTable.className = 'calendar';
        oTbody.appendChild(this.createCalendarWeekdays());
        var day_counter = 0;
        for (i = 0; i < 6; i++) {
            var oTr = document.createElement('TR');
            for (k = 0; k < 7; k++) {
                var oTd = document.createElement('TD');
                var oText = document.createTextNode(' ');
                var oDiv = document.createElement('DIV');
                if ((getWeekday(this.year, this.month, day_counter + 1) <= (i * 7 + k)) && day_counter < getDaysInMonth(this.month, this.year)) {
                    oText.nodeValue = ++day_counter;
                    if (isDisabled(this.year, this.month, day_counter)) {
                        oDiv.className = 'future-date';
                    } else {
                        oDiv.onmouseover = function() {
                            Calendar.over(this);
                        }
                        oDiv.onmouseout = function() {
                            Calendar.out(this);
                        }
                        oDiv.id = day_counter;
                        oDiv.onclick = function() {
                            Calendar.select(this, this.id);
                        }
                        //alert(this.year + "," + this.cur_year + "," + this.month + "," + this.cur_month);
                        if (day_counter == this.cur_day && this.year == this.cur_year && this.month == this.cur_month) 
                        {
							//alert(oDiv.id);
                            oDiv.className = 'current';
                        }
                    }
                } else {
                    oText.nodeValue = '';
                    oTd.className = 'empty-cell';
                }
                oDiv.appendChild(oText);
                oTd.appendChild(oDiv);
                oTr.appendChild(oTd);
            }
            oTbody.appendChild(oTr);
        }
        oTable.appendChild(oTbody);
        return oTable;
    }
    
    this.over = function() {
        obj = arguments[0];
        if (obj.className != 'current') {
            obj.className = 'over';
        }
    }
    this.out = function() {
        obj = arguments[0];
        if (obj.className != 'current') {
            obj.className = '';
        }
    }
    this.select = function() {
        obj = arguments[0];
        day = arguments[1];
        if (obj.className != 'current') {
            this.oInput.setAttribute('value', this.month + '/' + day + '/' + this.year);
            this.oInput.value = this.month + '/' + day + '/' + this.year;
            this.cur_month = this.month;
            this.cur_day = day;
            this.cur_year = this.year;
            oTds = obj.parentNode.parentNode.parentNode.getElementsByTagName('DIV');
            obj.style.backgroundColor = 'white';
            obj.className = 'current';



			if(typeof(this.oInput)!="undefined" && typeof(IsPossibleDate)!="undefined")
				IsPossibleDate(this.oInput.id);
				
            this.killCalendar();
            
            //IsPossiblePeriod("licenseExpirationPeriodFrom","licenseExpirationPeriodTo");
        }
    }
    
    this.drawCalendar = function() {
        this.killCalendar();
        oSelect = document.getElementsByTagName('SELECT');
        for (i = 0; i < oSelect.length; i++) {
            oSelect[i].style.visibility = 'hidden';
        }
        if (!this.oInput) {
            this.oInput = arguments[0];
        }
        this.parseDateString(this.oInput.value);
        var oDiv = document.createElement('DIV');
        oDiv.setAttribute('id', 'calendar');
        oDiv.setAttribute('class', 'calendar-container');
        oDiv.id = 'calendar';
        oDiv.className = 'calendar-container';
        oDiv.appendChild(this.createCalendarHeader());
        oDiv.appendChild(this.createCalendar());
        oDiv.style.top = findPosY(this.oInput) + this.oInput.offsetHeight;
        oDiv.style.left = findPosX(this.oInput);
        document.body.appendChild(oDiv);
        createMouseupListener();
    }
    
    this.redrawCalendar = function() {
        var oCalendar = document.getElementById('calendar');
        if (oCalendar) {
            oCalendar.parentNode.removeChild(oCalendar);
        }
        var oDiv = document.createElement('DIV');
        oDiv.setAttribute('id', 'calendar');
        oDiv.setAttribute('class', 'calendar-container');
        oDiv.id = 'calendar';
        oDiv.className = 'calendar-container';
        oDiv.appendChild(this.createCalendarHeader());
        oDiv.appendChild(this.createCalendar());
        oDiv.style.top = findPosY(this.oInput) + this.oInput.offsetHeight;
        oDiv.style.left = findPosX(this.oInput);
        document.body.appendChild(oDiv);
    }

    this.killCalendar = function() {
        var oCalendar = document.getElementById('calendar');
        if (oCalendar) {
            this.oInput = false;
            oCalendar.parentNode.removeChild(oCalendar);
        }
        oSelect = document.getElementsByTagName('SELECT');
        for (i = 0; i < oSelect.length; i++) {
            oSelect[i].style.visibility = 'visible';
        }
        killMouseupListener();
    }

    this.getPrevMonth = function() {
        if (this.month == 1) {
            r = 12;
        } else {
            r = this.month - 1;
        }
        return r;
    }
    this.getNextMonth = function() {
        if (this.month == 12) {
            r = 1;
        } else {
            r = this.month + 1;
        }
        return r;
    }
    this.drawPrevMonth = function() {
        this.month = this.getPrevMonth();
        if (this.month == 12) {
            this.year--;
        }
        this.redrawCalendar();
    }
    this.drawNextMonth = function() {
        this.month = this.getNextMonth();
        if (this.month == 1) {
            this.year++;
        }
        this.redrawCalendar();
    }
    this.drawPrevYear = function() {
        this.year--;
        this.redrawCalendar();
    }
    this.drawNextYear = function() {
        this.year++;
        for (i = this.month; i > 0; i--) {
            if (isDisabled(this.year, i, 1)) {
                this.month--;
            }
        }
        this.redrawCalendar();
    }

    this.parseDateString = function() {
        date_strging = arguments[0];
        str = date_strging.match(/^(\d+)\/(\d+)\/(\d+)$/);
        //debugger;
        if (str && str.length) {
            this.setYear(str[3]);
            this.setMonth(str[1]);
            this.setDay(str[2]);
        } else {
            this.setYear();
            this.setMonth();
            this.setDay();
        }
    }

} 

function switchTab(obj) {
if (obj.className == 'inactive-tab') {
setcookie('time-tab', obj.id);
obj.className = 'active-tab';
document.getElementById(obj.id + '-content').style.display = 'block';
if (obj.id == 'predefined') {
document.getElementById('range').className = 'inactive-tab';
document.getElementById('range-content').style.display = 'none';
} else {
document.getElementById('predefined').className = 'inactive-tab';
document.getElementById('predefined-content').style.display = 'none';
}
}
}

Calendar = new oCalendar();
