var url = "http://www.glico.co.jp/choco/blog/";
var archive_path = '';

function getCalendar(path) {
    var cookie = readCookie("AjaxCal");
    if(cookie != null) {
        path = url + archive_path + "calendar/" + cookie + "/";
    }
    changeMonth(path);
}


function createHttpRequest(){

	//Win ie用
	if(window.ActiveXObject){
    	try {
        	//MSXML2以降用
        	return new ActiveXObject("Msxml2.XMLHTTP");
    	} catch (e) {
        	try {
	            //旧MSXML用
    	        return new ActiveXObject("Microsoft.XMLHTTP");
	        } catch (e2) {
            	return null;
        	}
     	}
	} else if(window.XMLHttpRequest){
	    //Win ie以外のXMLHttpRequestオブジェクト実装ブラウザ用
	    return new XMLHttpRequest();
	} else {
	    return null;
	}
}

function requestFile( data , method , fileName , async ,callbackFunc){
	var r = createHttpRequest();
	if(r == null) return;

	r.open( method , fileName , async );

	r.onreadystatechange = function(){
  		if ((r.readyState==4)&&(r.status==200)){
			callbackFunc(r.responseText);
  		}
	}

	r.send( data );
}



function changeMonth(url) {
	if(navigator.platform.match(/Mac/)){
		if(navigator.userAgent.match(/MSIE/)){
			return false;
		}
	}

	requestFile( '' , 'GET', url , true ,endProcess);

    return false;
}



function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function endProcess(res) {
	document.getElementById('calendar').innerHTML = res;

    var value = document.getElementById('calendar').getElementsByTagName('table')[0].getAttribute('summary');
    setWeekendAndHoliday(value.split("/")[0], value.split("/")[1]);
    document.cookie = "AjaxCal=" + value + "; path=/";
}

function errorProcess() {
    document.getElementById('calendar').innerHTML = 'File Not Found';
}

function setWeekendAndHoliday(y,m) {
    setCurrentDate();
    var elements = document.getElementById('calendar').getElementsByTagName("table");
    for (j = 0; j < elements.length; j++) {
        var element = elements[j].getAttribute("summary");
        if(element == null){
            return;
        }
        var year = element.split("/")[0];
        var month = element.split("/")[1];
        if(!(year == y && month == m)){
            return;
        }
        var spans = elements[j].getElementsByTagName("span");
        var day;
        for (i = 0; i < spans.length; i++) {
            if (spans[i].parentNode.nodeName == "TD") {
                if(spans[i].innerHTML.indexOf("href") != -1){
                    day = spans[i].getElementsByTagName("a")[0].innerHTML;
                } else {
                    day = spans[i].innerHTML;
                }
                if (isHoliday(year, month, day)) {
                    spans[i].setAttribute('class', 'holiday');
                    spans[i].setAttribute('className', 'holiday');
                } else if(isSaturday(year, month, day)) {
                    spans[i].setAttribute('class', 'saturday');
                   spans[i].setAttribute('className', 'saturday');
                }
                if (isToday(year, month, day)) {
                    spans[i].parentNode.setAttribute('class', 'today');
                    spans[i].parentNode.setAttribute('className', 'today');
                }
            }
        }
    }
}

