var engMonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

function showCalendar(obj) {
    inputCalendarObj.showCalendar(obj);
}

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 test(){
	alert('Not Available on weekends and Public Holiday');	
}
function inputCalendar() {
    this.ctrlFrame, this.objYear, this.objMonth, this.oTable, this.currCell, this.inputObj;
    this.canPrevDate = true;
    this.sepa = "/";
    this.currDate = new Date();
    this.objDateCells = new Array();
    this.prevYearImage = "/kr/inc/js/img/ico_sml_cal_py.gif";
	this.nextYearImage = "/kr/inc/js/img/ico_sml_cal_ny.gif";
	this.prevMonthImage = "/kr/inc/js/img/ico_sml_cal_pm.gif";
    this.nextMonthImage = "/kr/inc/js/img/ico_sml_cal_nm.gif";
    
    // ³¯Â¥ ±¸ºÐÀÚ ¼³Á¤
    this.setSeparator = function(s) { this.sepa = (s) ? s : "/"; }

    // ¿À´ÃÀÌÀü³¯Â¥ ¼±ÅÃ°¡´É¿©ºÎ ¼³Á¤ : ÃÊ±â°ªÀº true
    this.setCanPrevDate = function(s) { this.canPrevDate = s; }

    // ´Þ·Â º¸ÀÌ±â
    this.showCalendar = function(obj) {
        this.inputObj = obj;
	    this.currDate = (obj.value != "") ? new Date(obj.value.substr(0,4),obj.value.substr(4+this.sepa.length,2)-1,obj.value.substr(6+this.sepa.length*2,2)) : new Date();
	    if (isNaN(this.currDate)) { obj.value=""; this.currDate = new Date(); }

        // ÀÔ·ÂÃ¢¿¡ Áß°£À§Ä¡¿¡ ´Þ·Â º¸ÀÓ
        diffY = findPosY(obj) + parseInt(obj.clientHeight/2);
        diffX = findPosX(obj) + parseInt(obj.clientWidth/2);
        if (typeof window.pageYOffset != 'undefined') {
            this.ctrlFrame.style.top = diffY + 'px';
            this.ctrlFrame.style.left = diffX + 'px';
        } else {
            this.ctrlFrame.style.pixelTop = diffY;
            this.ctrlFrame.style.pixelLeft = diffX;
        }
        this.ctrlFrame.style.display = '';
        if(document.all) this.ctrlFrame.focus();
        this.calAdjust();
    }

    // ´Þ·Â °è»êÃ³¸®
    this.calAdjust = function() {
    	var year = this.currDate.getFullYear();
    	var month = this.currDate.getMonth();

    	this.objYear.innerHTML = year;
    	this.objMonth.innerHTML = engMonths[month] + ".";

    	var dateFirst = new Date(year, month, 1);
    	var startCellNum = dateFirst.getDay();
    	var dateLast = new Date(year, month+1, 0);
    	var endCellNum = startCellNum + dateLast.getDate() - 1;

    	for (var i=0; i<42; i++) {
    		this.objDateCells[i].innerHTML = (i>=startCellNum && i<=endCellNum) ? i-startCellNum+1 : "";
    		switch (i%7) {
    			case 0: this.objDateCells[i].style.color="#FF6600"; this.objDateCells[i].onclick=test; break;
    			case 6: this.objDateCells[i].style.color="#1E98E9"; this.objDateCells[i].onclick=test;break;
    			default: this.objDateCells[i].style.color="#666666";
    		}
    	}

    	if (this.currCell) { this.currCell.style.backgroundColor="#ffffff"; }
    	this.currCell = this.objDateCells[this.currDate.getDate() + startCellNum - 1];
    	this.currCell.style.backgroundColor="#FFFFFF"; this.currCell.style.color="#000000";

    	this.ctrlFrame.style.width = this.ctrlFrame.contentWindow.document.body.scrollWidth + 'px';
    	this.ctrlFrame.style.height = this.ctrlFrame.contentWindow.document.body.scrollHeight + 'px';
    }

    // ´Þ·Â ÃÊ±âÈ­
    // IE ¿¡¼­´Â select ¶§¹®¿¡ °¡·ÁÁö´Â Çö»óÀÌ ÀÖ¾î¼­ iframe À¸·Î ´Þ·ÂÃ¢ »ý¼º
    this.init = function() {
        if(document.getElementsByTagName('BODY').length==0) document.write('<body>'); // ´õ¹Ì body ÅÂ±×

    	this.currDate = new Date();
    	var ifrmId = "htccalifrm" + Math.floor(Math.random() * 1000000);
    	this.ctrlFrame = document.createElement("IFRAME");
    	this.ctrlFrame.id = ifrmId;
    	this.ctrlFrame.frameBorder = 0;
    	this.ctrlFrame.scrolling = "no";
    	with (this.ctrlFrame.style) { width="135px"; position="absolute"; zIndex="9"; border = "0px solid #ffffff";	}
        document.body.appendChild(this.ctrlFrame);

        // IE¿¡¼­ ´Ù¸¥ À§Ä¡·Î Æ÷Ä¿½º ÀÌµ¿½Ã ´Þ·ÂÃ¢(IFRAME) ´ÝÀ½
        this.ctrlFrame.onblur = function() { this.style.display = 'none'; }

    	var ifrmDoc = this.ctrlFrame.contentWindow.document;
    	ifrmDoc.open("text/html","replace");
    	ifrmDoc.writeln("<html><head><style type='text/css'>TH,TD{font-size:11px;font-family:Dotum}</style><body style=margin:0px;background-color:#ffffff></body></html>");
		ifrmDoc.close();

        // IFRAME ´Þ·Â Å×ÀÌºí
    	this.oTable = ifrmDoc.createElement("TABLE");
    	this.oTable.cellPadding = 2; this.oTable.cellSpacing = 0;
    	with(this.oTable.style) { width = "100%"; fontSize = "12px"; borderCollapse = "collapse"; border = "1px solid #e3e3e3"; }

    	var row, cell, span, nbsp;
    	var oTbody = ifrmDoc.createElement("TBODY"); this.oTable.appendChild(oTbody);
    	oTbody.inputCalendar = this;

    	row = ifrmDoc.createElement("TR"); oTbody.appendChild(row);
    	cell = ifrmDoc.createElement("TH"); row.appendChild(cell);
    	cell.style.backgroundColor="#18170D"; cell.style.color="#ffffff";

    	// ÀÌÀüÇØ ÀÌµ¿¹öÆ°
    	span = ifrmDoc.createElement("IMG");
    	span.src = this.prevYearImage;
    	span.alt = "Previous Year";
    	span.style.cursor = "pointer";
    	cell.appendChild(span);
    	span.onmouseup = function() {
    	    p = this.parentNode.parentNode.parentNode.inputCalendar;
    	    p.currDate.setFullYear(p.currDate.getFullYear() - 1);
    	    p.calAdjust();
    	};
    	span.onselectstart = function(){return false;}

    	// ÀÌÀü´Þ ÀÌµ¿¹öÆ°
    	span = ifrmDoc.createElement("IMG");
    	span.src = this.prevMonthImage;
    	span.alt = "Previous Month";
    	span.style.cursor = "pointer";
    	cell.appendChild(span);
    	span.onmouseup = function() {
    	    p = this.parentNode.parentNode.parentNode.inputCalendar;
    	    p.currDate.setMonth(p.currDate.getMonth() - 1);
    	    p.calAdjust();
    	};
    	span.onselectstart = function(){return false;}				

        // ³â, ¿ù Ç¥½Ã
    	cell = ifrmDoc.createElement("TH"); cell.colSpan = "5"; row.appendChild(cell);
    	cell.style.backgroundColor="#18170D"; cell.style.color="#ffffff";
    	this.objMonth = ifrmDoc.createElement("SPAN"); cell.appendChild(this.objMonth);
    	nbsp = ifrmDoc.createTextNode(" "); cell.appendChild(nbsp);
        this.objYear = ifrmDoc.createElement("SPAN"); cell.appendChild(this.objYear);
    	cell = ifrmDoc.createElement("TH"); row.appendChild(cell);
    	cell.style.backgroundColor="#18170D"; cell.style.color="#ffffff";

        // ´ÙÀ½´Þ ÀÌµ¿¹öÆ°
    	span = ifrmDoc.createElement("IMG");
    	span.src = this.nextMonthImage;
    	span.alt = "Next Month";
    	span.style.cursor = "pointer";
    	cell.appendChild(span);
    	span.onmouseup = function() {
    	    p = this.parentNode.parentNode.parentNode.inputCalendar;
    	    p.currDate.setMonth(p.currDate.getMonth() + 1);
    	    p.calAdjust();
    	};
    	span.onselectstart = function(){return false;}

		// ´ÙÀ½ÇØ ÀÌµ¿¹öÆ°
    	span = ifrmDoc.createElement("IMG");
    	span.src = this.nextYearImage;
    	span.alt = "Next Year";
    	span.style.cursor = "pointer";
    	cell.appendChild(span);
    	span.onmouseup = function() {
    	    p = this.parentNode.parentNode.parentNode.inputCalendar;
    	    p.currDate.setFullYear(p.currDate.getFullYear() + 1);
    	    p.calAdjust();
    	}
    	span.onselectstart = function(){return false;}		
		

        // ¿äÀÏ Ãâ·Â
    	row = ifrmDoc.createElement("TR"); oTbody.appendChild(row);
    	var weekArray = ["S","M","T","W","T","F","S"];
    	for (var i=0; i<7; i++) {
    		cell=ifrmDoc.createElement("TH");
    		cell.style.backgroundColor="#E1E1E1"; //#0F8EEA
    		cell.style.color="#666666";
    		cell.innerHTML=weekArray[i];
    		row.appendChild(cell);
    	}
			aa = 0 ; 
        // ³¯Â¥ ÀÔ·ÂºÎºÐ Ãâ·Â
    	for (var i=0; i<6; i++) {
    		row = ifrmDoc.createElement("TR"); oTbody.appendChild(row);
    		for (var j=0; j<7; j++) {
    			this.objDateCells[i*7+j] = cell = ifrmDoc.createElement("TD");
    			with(cell.style) { 
    				textAlign = "center";  
    				cursor = "pointer"; 
    			}
    			
    			    			
    			row.appendChild(cell);

    			// ¼±ÅÃ½Ã ÀÔ·ÂÃ¢¿¡ ³¯Â¥¸¦ Ãâ·ÂÇÏ°í ´Þ·ÂÃ¢ ´ÝÀ½
    			cell.onclick = function() {
    			    p = this.parentNode.parentNode.inputCalendar;
                    currMonth = p.currDate.getMonth()+1;
                    currDay = parseInt(this.innerHTML);
                    var isNext = true;
                    if(!p.canPrevDate) {
                        var sDay = new Date(p.currDate.getFullYear(), currMonth-1, currDay);
                        var nowDay = new Date();		// 070830 ¼ÕÁÖ¿µ ¿À´ÃÀÌÈÄ ³¯Â¥¸¸ ½ÅÃ»°¡´ÉÇÏµµ·Ï ¼öÁ¤ 
                        isNext = (nowDay-sDay)<0;		// 070830 ¼ÕÁÖ¿µ ¿À´ÃÀÌÈÄ ³¯Â¥¸¸ ½ÅÃ»°¡´ÉÇÏµµ·Ï ¼öÁ¤ 
                    }
                    if(isNext) {
                        currMonth = p.currDate.getMonth()+1;
                        if (currMonth < 10) 
                        	currMonth = "0" + currMonth;
                        if (currDay < 10) 
                        	currDay = "0" + currDay;
                        p.inputObj.value =  engMonths[p.currDate.getMonth()] + ". " + currDay + ", " + p.currDate.getFullYear();
                        if(p.inputObj.form && p.inputObj.form.play_date) 
                        	p.inputObj.form.play_date.value = String(p.currDate.getFullYear()) + String(currMonth) + String(currDay);
                        try { changeCourse(); } catch (e){}
                        p.ctrlFrame.style.display = 'none';
                        p.inputObj.focus();
                        p.inputObj.blur();
                    } else {
                        alert("Select valid play date. ");
                    }
                }
    			cell.onselectstart = function(){return false;}
    		}
    	}

    	ifrmDoc.body.appendChild(this.oTable);
    	this.calAdjust();
    	this.ctrlFrame.style.display = 'none';
    }
}
var inputCalendarObj = new inputCalendar();

function inpuCalendarInit() {
    inputCalendarObj.init();
    inputCalendarObj.setSeparator(".");
    inputCalendarObj.setCanPrevDate(false);
}

if(document.all) document.body.onload = inpuCalendarInit;
else inpuCalendarInit();


