function removeStyleClass(element, styleClass){
    var ary = element.className.split(" ");
    var target = "";
    for(var i=0; i<ary.length; i++){
        if(ary[i] != styleClass){
            target += ary[i] + " ";
        }
    }
    element.className = target;
}
function addStyleClass(element, styleClass){
    var s = element.className;
    if (s == null){
        s = "";
    }
    if (s == ""){
        s += styleClass;
    }else{
        s += " " + styleClass;
    }
    element.className = s;
}

function getCalendarHtml(y, m, containerId, onclickFunction){
    var d = new Date(y, m, 1);
    var tmpD = new Date(y, m, (1 - d.getDay()));
    var html = "<table border='0' cellpadding='2' cellspacing='0' class='sitecalendar'>"
    html += "<thead>";
    html += "<tr>";
    html += "<th align='left' onmousemove=\"addStyleClass(this, 'roolover');\" onmouseout=\"removeStyleClass(this, 'roolover');\" class='button clickable' onclick=\"document.getElementById('" + containerId + "').innerHTML=getCalendarHtml(" + y + "," + (m - 1) + ",'" + containerId + "','" + onclickFunction + "');\">&lt;</th>";
    html += "<th colspan='5' align='center'>" + d.getFullYear() + '年' + (d.getMonth() + 1) + '月' + "</th>";
    html += "<th align='right' onmousemove=\"addStyleClass(this, 'roolover');\" onmouseout=\"removeStyleClass(this, 'roolover');\" class='button clickable' onclick=\"document.getElementById('" + containerId + "').innerHTML=getCalendarHtml(" + y + "," + (m + 1) + ",'" + containerId + "','" + onclickFunction + "');\">&gt;</th>";
    html += "</tr>";
    html += "<tr>";
    html += "<th class='weekday'>日</th><th class='weekday'>一</th><th class='weekday'>二</th><th class='weekday'>三</th><th class='weekday'>四</th><th class='weekday'>五</th><th class='weekday'>六</th>";
    html += "</tr>";

    html += "</thead>";
    html += "<tbody>";
    while (true){
        if((tmpD.getFullYear() * 100 + tmpD.getMonth()) > (d.getFullYear() * 100 + d.getMonth()) && tmpD.getDay() == 0){
            html += "</tr>";
            break;
        }
        if (tmpD.getDay() == 0){
            if (tmpD.getMonth() == d.getMonth()){
                html += "</tr>";
            }
            html += "<tr>";
        }
        var styleClass = "clickable";
        if (tmpD.getMonth() != d.getMonth()){
            styleClass += " othermonth";
        }else if(tmpD.getMonth() == (new Date()).getMonth() && tmpD.getDate() == (new Date()).getDate() && tmpD.getFullYear() == (new Date()).getFullYear()){
            styleClass += " today";
        }
        html += "<td class='" + styleClass + "' onmousemove=\"addStyleClass(this, 'roolover');\" onmouseout=\"removeStyleClass(this, 'roolover');\" onclick='eval(\"" + onclickFunction + "(" + tmpD.getFullYear() + "," + tmpD.getMonth() + "," + tmpD.getDate() + ");\");'>" + tmpD.getDate() + "</td>";


        tmpD.setDate(tmpD.getDate() + 1);
    }
    html += "</tbody>";
    html += "</table>";
    return html;
}

