Number.prototype.fillZero = function(n) {
    var r = this.toString().split('');
    while(r.length <= n) {
        r.unshift('0');
    }
    return r.join('');
}

var current_date = '';

function setCurrentDate(_year, _month, _date){
    if(current_date == ''){
        current_date = new Date(_year,_month-1,_date);
    }
}
function updateCurrentDate(_update, this_year,this_month,this_day){
    if(current_date == ''){
    	setCurrentDate(this_year,this_month ,this_day);
    }else{
    }

    current_date_year = current_date.getFullYear();
    current_date_month = current_date.getMonth() ;
    current_date_date = 1;
    
    current_date = new Date(current_date_year,current_date_month + _update , current_date_date);
}

function getCalendarData(url, term, type, span, cms, startDate, this_year,this_month,this_day){

    var todayDateObj = new Date();
    var startDateObj = new Date(startDate.substr(0,4), startDate.substr(4,2) - 1, startDate.substr(6,2));
    switch (term){
        case 'lastyear':
            move_month = -12;
            break;
        case 'lastmonth':
            move_month = -1;
            break;
        case 'nextmonth':
            move_month = 1;
            break;
        case 'nextyear':
            move_month = 12;
            break;
    }
    backup_current_date = current_date;
    updateCurrentDate(move_month, this_year,this_month,1);
    if(startDateObj.getTime() > current_date.getTime() || current_date.getTime() > todayDateObj.getTime()){
        current_date = backup_current_date;
        return;
    }
    var ranking_date_year = current_date.getFullYear();
    var ranking_date_month = current_date.getMonth() + 1;
    var ranking_date_date = current_date.getDate();

    url = url  +'/' +type+'/' + span+'/'+ cms+'/' + ranking_date_year + '' +ranking_date_month.fillZero(1) + '' + ranking_date_date.fillZero(1);
	new Ajax.Request(url, {
	onFailure:getFailureCode,
	onSuccess: function (req) {
		if (req.status == 200) {
			try {
			    if(req.responseText.substr(0,5) == '<!DOC')return;
				$("calendar").innerHTML = req.responseText;
				return;

			} catch (e) { }
		}
	}
});
   
}
function getFailureCode(){
}
