var type;
var thisYear;
var thisMon;

function lpad(str,fill,leng)
{
    var n = leng - str.length;
    var out ="";
    for (i =0; i < n; i++)  out = out + fill;
    out=out+str;
    return out;
}

function toInt(str)
{
    i=0;
    while(1)
    {
        if(str.substr(0,1)=='0') {str=str.substr(1,str.length)};
        else break;
        i++;
    }
    return parseInt(str);
}

// ±â³äÀÏ(¼ýÀÚÀÔ·Â)
var anniversary = new Array();

function show_cal(selectDate,calDivObj) 
{
    //Àü¿ªº¯¼öµé ¼¼ÆÃ
    var selectDate = ''+selectDate; //Àü¿ªº¯¼ö1 - ÀÌ½´°¡ µÇ´Â ³¯Â¥ ÁöÁ¤
    today = new Date();
    toDate = today.getYear() + lpad(''+(today.getMonth()+1),'0',2) + lpad(''+today.getDate(),'0',2); // ¿À´Ã³¯Â¥ ÁöÁ¤
    if (selectDate == '')
    {
        selectDate=toDate;
    }

	thisYear=selectDate.substr(0,4);
	thisMon=selectDate.substr(4,2);

    var preMonDate;
    var nextMonDate;
    preMonDate= selectDate.substr(0,4)+lpad(''+(toInt(selectDate.substr(4,2))-1),'0',2)+selectDate.substr(6,2);
    nextMonDate= selectDate.substr(0,4)+lpad(''+(toInt(selectDate.substr(4,2))+1),'0',2)+selectDate.substr(6,2);
    if(selectDate.substr(4,2)=='01') preMonDate= (toInt(selectDate.substr(0,4))-1) + '12' + selectDate.substr(6,2);
    if(selectDate.substr(4,2)=='12') nextMonDate= (toInt(selectDate.substr(0,4))+1) + '01' + selectDate.substr(6,2);

    var firstDay = getFirstDay(selectDate.substr(0,4), selectDate.substr(4,2));            // Ã¹¹øÂ° ¿äÀÏÀÇ ¼ýÀÚ°ª        
    var lastDay = getLastDay(selectDate.substr(0,4), selectDate.substr(4,2));            // ¸¶Áö¸· ¿äÀÏÀÇ ¼ýÀÚ°ª
    var daysOfMonth = getDaysOfMonth(selectDate.substr(0,4), selectDate.substr(4,2));    // 28, 29, 30, 31 Áß ÇÏ³ª
    var calString;//´Þ·Â HTMLÀ» ÀúÀåÇÏ±â À§ÇÑ º¯¼ö´Ù.

	calString="<table border='0' cellspacing='1' cellpadding='0' bgcolor='black' width='170'><tr><td bgcolor='white' align='center'>";
    calString+="<table border='0' cellspacing='0' cellpadding='0' width='100%' style='font-size:11px'>";

    calString+="<tr style='color=#0000C6'><td colspan='7' align=center height='20'>";
    calString+="<a href=\"javascript:show_cal('"+ (parseInt(selectDate.substr(0,4))-1)+ selectDate.substr(4,4) +"',"+ calDivObj.id +");\" style='color:#C0C0C0' title='ÀÌÀü ³âµµ'>¢¸</a>";
    calString+="<a href=\"javascript:show_cal('"+ preMonDate +"',"+ calDivObj.id +");\" title='ÀÌÀü ´Þ'>¢¸</a>&nbsp;";
    calString+=""+selectDate.substr(0,4)+"³â "+selectDate.substr(4,2)+"¿ù&nbsp;";
    calString+="<a href=\"javascript:show_cal('"+ nextMonDate +"',"+ calDivObj.id +");\" title='´ÙÀ½ ´Þ'>¢º</a>";
    calString+="<a href=\"javascript:show_cal('"+ (parseInt(selectDate.substr(0,4))+1)+ selectDate.substr(4,4) +"',"+ calDivObj.id +");\" style='color:#C0C0C0' title='´ÙÀ½ ³âµµ'>¢º</a>";
    calString+="&nbsp;&nbsp;&nbsp;<a href='javascript:OffCal()'><img src='./images/x_close.gif' width='12' height='11' align='middle' alt='´Ý±â' border='0'></a>";
	calString+="</td></tr>";

    calString+="<tr height='5'><td colspan='7'></td></tr>";
    calString+="<tr height=19>";
    calString+="<td width='19' align=center style='color=#C60000'>ÀÏ</td>";
    calString+="<td width='19' align=center>¿ù</td>";
    calString+="<td width='19' align=center>È­</td>";
    calString+="<td width='19' align=center>¼ö</td>";
    calString+="<td width='19' align=center>¸ñ</td>";
    calString+="<td width='19' align=center>±Ý</td>";
    calString+="<td width='19' align=center style='color=#0000C6'>Åä</td>";
    calString+="</tr>";
    
    // ´Þ·Â textfield Ãâ·Â
    for (var i=0; i < Math.ceil( (firstDay+daysOfMonth)/7 ); i++) {
        calString+="<tr valign='middle' height='19'>";
        for (var j=1; j <= 7; j++) {         
            colNum=i*7+j; //´Þ·ÂÀÇ °¢ Ä­ÀÇ Ä®·³À» ¹øÈ£·Î ÁöÁ¤
            
            if (colNum>firstDay && colNum<firstDay+daysOfMonth+1) //´Þ·Â¿¡ ³¯Â¥°¡ ³ª¿Í¾ß µÇ´Â Á¶°Ç
            {
                thisDay=colNum-firstDay; //ÀÌ³¯ÀÇ ³¯Â¥(¼ýÀÚ)
                
                if(colNum%7==1) {tdColor="C60000";}
                else if(colNum%7==0) {tdColor="0000C6";}
                else {tdColor="333333";}
                
                for(k=0;k<anniversary.length;k++)
                {
                    if(thisDay==anniversary[k])
                    {
                        thisDay="<a href='#'><b>"+thisDay+"</b></a>";
                        break;
                    }
                }

				thisDay="<a href='#none' onclick=\"GetDate('"+thisDay+"')\" style='color:#"+tdColor+"'>"+thisDay+"</a>";
                calString+="<td align='center' onClick='' onMouseOver=this.style.background='#e4e4e4'; onMouseOut=this.style.background='#ffffff';>"+thisDay+"</td>";
            }
            else
            {
                calString+="<td></td>";
            }
            //calString+="<td>"+colNum+"</td>";
        }
        calString+="</tr>";
    }
    calString+="</table></td></tr></table>";

    //ÀúÀåµÈ ½ºÆ®¸µº¯¼ö¸¦ DIV·¹ÀÌ¾î¿¡ ¿Ã¸®ÀÚ.. 
    calDivObj.innerHTML=calString;
}

/////////////////////////³¯Â¥ °ü·ÃµÈ ¿¬»ê ÇÔ¼öµé////////////////////////////
function getDaysOfMonth(year, month) { 
    var DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];        // Non-Leap year Month days.. 
    var lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];    // Leap year Month days.. 
    /* 
    Check for leap year .. 
    1.Years evenly divisible by four are normally leap years, except for... 
    2.Years also evenly divisible by 100 are not leap years, except for... 
    3.Years also evenly divisible by 400 are leap years. 
    */ 
    if ((year % 4) == 0) {
        if ((year % 100) == 0 && (year % 400) != 0)
            return DOMonth[toInt(month)-1];
     
        return lDOMonth[toInt(month)-1];
    } else 
        return DOMonth[toInt(month)-1];
} 

// Ã¹¹øÂ° ¿äÀÏ ±¸ÇÏ±â
function getFirstDay(year, month) {
    var tmpDate = new Date(); 
    tmpDate.setDate(1); 
    tmpDate.setMonth(toInt(month)-1); 
    tmpDate.setFullYear(year); 
    return tmpDate.getDay(); 
}

// ¸¶Áö¸· ¿äÀÏ ±¸ÇÏ±â
function getLastDay(year, month) {
    var tmpDate = new Date(); 
    tmpDate.setDate( getDaysOfMonth(year,month) ); 
    tmpDate.setMonth(toInt(month)-1); 
    tmpDate.setFullYear(year); 
    return tmpDate.getDay(); 
}

function OnCal(what,div_name)
{
	type=what;
	document.all.cal.style.top = event.clientY+document.body.scrollTop;
	document.all.cal.style.left = event.clientX+document.body.scrollLeft;
	document.all.cal.style.display="";
	show_cal('',cal);
}

function OffCal()
{
	document.all.cal.style.display="none";
}

function GetDate(day)
{
	if(day.length==1) day='0'+day;

	document.play.play_date.value=thisYear+"-"+thisMon+"-"+day;
	OffCal();
}
