﻿var oDatePicker;
var xmlHttp = null;
var strHolidays = "";

if(typeof(calInfo) == "undefined")
	calInfo = {
		ValidMessages : ["날짜 형식이 잘못되었습니다.","일 이후 입력 가능합니다.","일 까지 입력 가능합니다.","휴일은 선택 할 수 없습니다."],
		Months : ["1","2","3","4","5","6","7","8","9","10","11","12"],
		Days : ["일","월","화","수","목","금","토"],		
		Close : "Close",			
		MonthTitle : "월",
		PrevMonth : "이전 달",
		NextMonth : "다음 달",
		YearTitle : "년",
		Images : { prev : "/common/images/ico/ico_prev.gif", next : "/common/images/ico/ico_next.gif", close : "/common/images/ico/ico_close.gif" },
		Type : { from : "FROM", to : "TO", only : "ONLY" }
	};
	
if(typeof(ajax) == "undefined")
	ajax = {
		xmlHttps : ["Msxml2.XMLHTTP.7.0","Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0",
					"Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.3.0","Msxml2.XMLHTTP",
					"Microsoft.XMLHTTP"],
	    getXMLHttp : function(){
	    	for (var i =0; i<this.xmlHttps.length ; i++)
		    {
		           try
		           {
		                var req = new ActiveXObject(this.xmlHttps[i]);                
		                return req;
		           }
		           catch(e){return;}
		     }
		     return null;
	    }
	};
	
function DatePicker (name,responseUrl) {
	this.name = name;
	
	this.dtCompare = new Date();
	this.dt = new Date ();
	this.responseUrl = responseUrl;
}

DatePicker.prototype.show = function (dt, x, y, callback) {
	if (dt) {
		this.dt = dt;
	}	
	
	this.callback = callback;
	
	if (!this.oSpan) {
		this.render ();
	}
		
	this.oSpan.style.position = "absolute";
	this.oSpan.style.left = parseInt(this.position.left,10) + parseInt(x,10);
	this.oSpan.style.top = 18 + parseInt(this.position.top,10) + parseInt(y,10);
	this.oSpan.className = 'DatePicker';
	
	
	// 달력의 width
	this.oIFrame.style.width = '175px';
	this.oIFrame.style.height = this.oSpan.style.height;
	this.oIFrame.style.position = "absolute";
	this.oIFrame.style.left = 0;
	this.oIFrame.style.top = 0;
	this.oIFrame.style.visibility = "visible";	
	
	this.oSpan.style.visibility = "visible";
	this.oSpan.style.zIndex = 100;
	
	this.fill ();	
	this.oMonth.focus ();
}

DatePicker.prototype.hideAll = function () {
    
    if(document.all)
    {
        var spans = document.all.tags('span');
        
        for( var s = 0; s < spans.length; s++)
        {
            if(spans[s] != this.oSpan && spans[s].id && spans[s].id.indexOf('calendar_') > -1)
            {
                spans[s].style.visibility = "hidden";
            }
        }    
        
        var frames = document.all.tags('iframe');
        
        for( var f = 0; f < frames.length; f++)
        {
            if(frames[f] != this.oSpan && frames[f].id && frames[f].id.indexOf('calendar_') > -1)
            {
                frames[f].style.visibility = "hidden";
            }
        }   
    }    
}

DatePicker.prototype.hide = function () {
	
	if (this.oIFrame) {
		this.oIFrame.style.visibility = "hidden";
		this.oSpan.removeChild(this.oIFrame);
	}
	
	if (this.oSpan) {
		this.oSpan.style.visibility = "hidden";
		
		document.body.removeChild(this.oSpan);
	}
}

DatePicker.prototype.render = function () {
	var oTR1, oTD1, oTH1;
	var oTR2, oTD2;
	
	
	
	//this.oSpan = document.getElementById (this.name);
	this.oSpan = document.createElement("span");
	this.oSpan.id = this.name;
	
	document.body.appendChild(this.oSpan);
		
	this.oIFrame = document.createElement ("iframe");
	this.oIFrame.id = "IF_"+this.oSpan.id
	this.oSpan.appendChild (this.oIFrame);

	this.oTable1 = document.createElement ("table");
	this.oSpan.appendChild (this.oTable1);
	this.oTable1.style.position = "absolute";
	this.oTable1.style.left = 0;
	this.oTable1.style.top = 0;
	this.oTable1.className = 'calender';
	this.oTable1.border = 0;
	this.oTable1.cellSpacing = 0;
	this.oTable1.cellPadding = 0;	
	
	oTR1 = this.oTable1.insertRow (this.oTable1.rows.length);	
	oTD1 = oTR1.insertCell (oTR1.cells.length);	
	oTD1.height = 25;
	oTD1.bgColor = "#ffffff";
	oTD1.className = "border_head";	
	
	this.oTable2 = document.createElement ("table");
	this.oTable2.cellSpacing = 0;
	this.oTable2.cellPadding = 0;
	this.oTable2.border = 0;
	this.oTable2.height = 25;
	this.oTable2.width = "100%";
	
	oTD1.appendChild (this.oTable2);	
	
	// New row.
	oTR2 = this.oTable2.insertRow (this.oTable2.rows.length);	
	
	// Previous month.
	oTD2 = oTR2.insertCell (oTR2.cells.length);
	oTD2.title = this.texts.prevMonth;
	oTD2.onclick = function () { this.oDatePicker.onPrev (); }
	oTD2.oDatePicker = this;
	
	oTD2.innerHTML = "<img src=\"" + calInfo.Images.prev + "\">";	
	oTD2.width = "30px";
	oTD2.align = "left";
	oTD2.style.paddingTop = "3px";	
	
	// Year combo.
	oTD2 = oTR2.insertCell (oTR2.cells.length);
	oTD2.title = this.texts.yearTitle;
	oTD2.width = 80;
	oTD2.align = "right";
	
	this.oYear = document.createElement ("select");
	oTD2.appendChild (this.oYear);
	oTD2.appendChild(document.createTextNode(this.texts.yearTitle));
	oTD2.style.paddingTop = "3px";
	
	this.oYear.oDatePicker = this;
	this.oYear.onchange = this.oYear.onkeyup = function () { this.oDatePicker.onYear (); }
	for (i = this.dt.getFullYear () - 25; i < this.dt.getFullYear () + 25; i++) {
		this.oYear.add (new Option (i, i),undefined);		
	}
	
	oTD2.appendChild(document.createTextNode(" "));
	
	// Month combo.
	oTD2 = oTR2.insertCell (oTR2.cells.length);
	oTD2.title = this.texts.monthTitle;
	this.oMonth = document.createElement ("select");
	oTD2.appendChild (this.oMonth);
	oTD2.appendChild(document.createTextNode(this.texts.monthTitle));
	oTD2.style.paddingTop = "3px";
	
	this.oMonth.oDatePicker = this;
	this.oMonth.onchange = this.oMonth.onkeyup = function () { this.oDatePicker.onMonth (); }
	for (var i = 0; i < 12; i++) {
		this.oMonth.add (new Option (this.texts.months[i], i),undefined);
	}	
	
	// Next month.
	oTD2 = oTR2.insertCell (oTR2.cells.length);
	oTD2.title = this.texts.nextMonth;
	oTD2.width = 26;
	oTD2.align = "left";
	oTD2.style.paddingTop = "3px";
	oTD2.onclick = function () { this.oDatePicker.onNext (); }
	oTD2.oDatePicker = this;
	oTD2.innerHTML = "<img src=\"" + calInfo.Images.next + "\">";
	oTD2.className = 'DatePickerHdrBtn';
	
	// Next rows (dates).
	
	this.oTable3 = document.createElement ("table");
	this.oTable3.cellSpacing = 1;
	this.oTable3.cellPadding = 0;
	this.oTable3.border = 0;
	this.oTable3.height = 25;
	this.oTable3.width = "100%";
	this.oTable3.bgColor = "#ffffff";
	
	oTD1.appendChild (this.oTable3);	
	
	oTR3 = this.oTable3.insertRow (this.oTable3.rows.length);
	oTR3.bgColor = "#000000";
	oTR3.align = "center";
	
	for (i = 0; i < 7; i++) {
		oTH3 = document.createElement ("th");
		oTR3.appendChild (oTH3);		
		oTH3.innerHTML = this.texts.days[i];
		oTH3.className = 'week';
	}
	this.aCells = new Array;
	
	
	oTR1 = this.oTable1.insertRow (this.oTable1.rows.length);	
	oTD1 = oTR1.insertCell (oTR1.cells.length);
		
	this.oTable4 = document.createElement("table");
	
	this.oTable4.className = 'border_body';
	
	this.oTable4.bgColor = "#ffffff";
	this.oTable4.cellSpacing = 1;
	this.oTable4.border = 0;	
	
	this.oTable4.width = "100%";
	this.oTable4.style.borderCollapse = "collapse";
	
	oTD1.appendChild (this.oTable4);
	
		
	for (var j = 0; j < 6; j++) {
		this.aCells.push (new Array);
		oTR4 = this.oTable4.insertRow (this.oTable4.rows.length);
		oTR4.bgColor = "#ffffff";
		for (i = 0; i < 7; i++) {
			this.aCells[j][i] = oTR4.insertCell (oTR4.cells.length);
			this.aCells[j][i].align = "center";
			this.aCells[j][i].oDatePicker = this;
						
			if(i == 6)
			    this.aCells[j][i].className = 'day_blue'
			else if(i == 0)
			    this.aCells[j][i].className = 'day_red'
			else
			    this.aCells[j][i].className = 'day'
			    
			this.aCells[j][i].onclick = function () { this.oDatePicker.onDay (this); }
		}
	}
	
	// New buttons.
	oTR1 = this.oTable1.insertRow (this.oTable1.rows.length);
	oTR1.bgColor = "#ecf5ff";
	oTD1 = oTR1.insertCell (oTR1.cells.length);
	
	oTD1.className = 'DatePickerHdr';
	oTD1.height = 20;
	oTD1.align = "center";
	oTD1.bgColor = "#ffffff";
	
	oTD1.oDatePicker = this;
	oTD1.onclick = function() { this.oDatePicker.hide (); }
	oTD1.innerHTML = "<img src=\"" + calInfo.Images.close + "\">";
	oTD1.title = this.texts.close;
}

function handleStateChange()
{   
    if(xmlHttp.readyState == 4) 
    {
      if(xmlHttp.status == 200) 
      { 
        strHolidays = xmlHttp.responseText;
      }
    }
}


function GetHoliday(year, month, responseUrl)
{
   
    try
    {
    xmlHttp = ajax.getXMLHttp();
    
    if(xmlHttp != null)
    {   
        xmlHttp.onreadystatechange = handleStateChange;
        xmlHttp.open("get", responseUrl + "?year=" + year + "&month=" + month, false);
        xmlHttp.send(null);
        
        if(xmlHttp.status == 200)
        {   
            return strHolidays;
        }
    }
    }
    catch(e)
    {
        alert(e.message);
        return;
    }
}

DatePicker.prototype.getHoliday = function (year, month){
   
    try
    {
    xmlHttp = ajax.getXMLHttp();
    
    if(xmlHttp != null)
    {   
        var url = this.responseUrl + "?year=" + year + "&month=" + month;
                
        xmlHttp.onreadystatechange = handleStateChange;
        xmlHttp.open("get",url, false);
        xmlHttp.send(null);
        
        if(xmlHttp.status == 200)
        {   
            this.setHolidays(strHolidays);
        }
    }
    }
    catch(e)
    {
        alert(e.message);
        return;
    }
}

DatePicker.prototype.fill = function () {
	// first clear all
	this.clear ();
	
	// place the dates in the calendar
	var nRow = 0;
	var d = new Date (this.dt.getTime ());
	var m = d.getMonth ();
	
	
	var fullYear = d.getFullYear();
	var month = d.getMonth() + 1;
	
	// Call WebService
	this.getHoliday(fullYear, month);
	
	for (d.setDate (1); d.getMonth () == m; d.setTime (d.getTime () + 86400000)) {
		var nCol = d.getDay ();
				
		this.aCells[nRow][nCol].innerHTML = d.getDate ();
		if (d.getDate () == this.dt.getDate ()) {
			this.aCells[nRow][nCol].className = 'day_on';
		}
		
			
		for(var i=0; i < this.holidays.days.length; i++)
		{
		    var year = this.holidays.days[i].split('-')[0];
		    var month = this.holidays.days[i].split('-')[1];
		    var day = this.holidays.days[i].split('-')[2];
		    
		    month = month - 1;
		    		
		    var dCompare = new Date();
		    dCompare.setFullYear(year,month,day)
    		
		    if(d.getFullYear() == dCompare.getFullYear() && d.getMonth() == dCompare.getMonth() && d.getDate() == dCompare.getDate())
		    {
		        this.aCells[nRow][nCol].className = "day_red";
		        this.aCells[nRow][nCol].title = this.holidays.dayDescs[i];
		    }
		}
		    
		if (nCol == 6) {
			nRow++;
		}
	}
	
	// set the month combo
	this.oMonth.value = m;
	
	// set the year text
	this.oYear.value = this.dt.getFullYear ();
}

DatePicker.prototype.clear = function () {
	for (var j = 0; j < 6; j++) {
		for (var i = 0; i < 7; i++) {
			this.aCells[j][i].innerHTML = "&nbsp;"
			
			if(i == 6)
			    this.aCells[j][i].className = 'day_blue'
			else if(i == 0)
			    this.aCells[j][i].className = 'day_red'
			else
			    this.aCells[j][i].className = 'day'
			    
			this.aCells[j][i].bgColor = "#ffffff"
		}
	}
}

DatePicker.prototype.onPrev = function () {
	if (this.dt.getMonth () == 0) {
		this.dt.setFullYear (this.dt.getFullYear () - 1);
		this.dt.setMonth (11);
	} else {
		this.dt.setMonth (this.dt.getMonth () - 1);
	}
	
	this.fill ();
}

DatePicker.prototype.onNext = function () {
	if (this.dt.getMonth () == 11) {
		this.dt.setFullYear (this.dt.getFullYear () + 1);
		this.dt.setMonth (0);
	} else {
		this.dt.setMonth (this.dt.getMonth () + 1);
	}
	
	this.fill ();
}

DatePicker.prototype.onMonth = function () {
	this.dt.setMonth (this.oMonth.value);
	this.fill ();
}

DatePicker.prototype.onYear = function () {
	this.dt.setYear (this.oYear.value);
	this.fill ();
}

DatePicker.prototype.onDay = function (oCell) {

	var d = parseInt (oCell.innerHTML);	
	var compareObject = this.compareInfo.compareObject;	
	
	if(oCell.className == 'day_red' && this.texts.useRedDay == 'false')
	{
	    alert(this.texts.message[3]);
	    return
	}
	
	
	if (d > 0) 
	{   
	    var rcode = true;
	    	
		this.dt.setDate (d);
		var dtCompare = this.dtCompare;
		
	    if(compareObject != null && compareObject.value.trim().length > 0)
        {
            var y = Number(compareObject.value.split('-')[0]);
            var m = Number(compareObject.value.split('-')[1]) - 1;
            var d = Number(compareObject.value.split('-')[2]);
                        
            dtCompare.setFullYear(y,m,d);            
            var strDate = compareObject.value;
            
            this.dt.setFullYear(this.dt.getFullYear(), this.dt.getMonth(), this.dt.getDate());
            
            switch(this.compareInfo.type.toUpperCase())
            {
                case "FROM": // from
                    if(this.dt > dtCompare)
                    {
                        alert(strDate + this.compareInfo.message[2]);
                        rcode = false;
                    }
                    break;
                case "TO": // to
                    if(this.dt < dtCompare)
                    {
                        alert(strDate + this.compareInfo.message[1]);	                    
                        rcode = false;
                    }
                    break;
                    
                case "ONLY": // OnlyOne
                    if(this.compareInfo.mark == 1 && this.dt > dtCompare) // 이전
	                {
	                    alert(strDate + this.compareInfo.message[2]);
                        rcode = false;
	                }
	                else if(this.compareInfo.mark == 2 && this.dt < dtCompare) // 이후
	                {
	                    alert(strDate + this.compareInfo.message[1]);	                    
                        rcode = false;
	                }
	                else if(this.compareInfo.mark == 3) // 사이
	                {
	                    // 
	                }
                break;
            }
        }
        else
        { 
	        var dtSelect = new Date();
	        
	        dtSelect.setFullYear(this.dt.getFullYear(), this.dt.getMonth(), this.dt.getDate());
	        
	        
	        //var strCompareDate = dtCompare.getFullYear() + "-" + eval(Number(dtCompare.getMonth())+1) + "-" + dtCompare.getDate();
	        //var strSelectedDate = this.dt.getFullYear() + "-" + eval(Number(this.dt.getMonth())+1) + "-" + this.dt.getDate();
	        
	        var strMsg = dtCompare.getFullYear() + "-";
	        
	        var m = eval(Number(dtCompare.getMonth())+1);
	        var day = dtCompare.getDate();
	        
	        if(m < 10)
	            m = '0' + m;
	            
	        if(day < 10)
	            day = '0' + day;
	        
	        strMsg += m + '-' + day;
	        
	        
            switch(this.compareInfo.type.toUpperCase())
            {
                case "FROM":
                    //if(dtSelect > dtCompare)
                    //{
                       //alert(strMsg + this.compareInfo.message[2]);
                       // rcode = false;
                    //}
                    //break;
                case "TO":
                    //if(dtSelect < dtCompare)
                    //{
                        //alert(strMsg + this.compareInfo.message[1]);	                    
                        //rcode = false;
                    //}
                    break;
                    
                case "ONLY":
                    if(this.compareInfo.mark == 1 && dtSelect > dtCompare) // 이전
	                {
	                    alert(strMsg + this.compareInfo.message[2]);
                        rcode = false;
	                }
	                else if(this.compareInfo.mark == 2 && dtSelect < dtCompare) // 이후
	                {
	                    alert(strMsg + this.compareInfo.message[1]);	                    
                        rcode = false;
	                }
	                else if(this.compareInfo.mark == 3) // 사이
	                {
	                    // 
	                }
                break;
            }
        }
        
        if(rcode)
        {
            this.hide ();
		    this.callback (this.dt);   
        }
    }
}

DatePicker.prototype.onToday = function () {
	this.dt = new Date ();
	this.fill ();
}

DatePicker.prototype.onYesterday = function () {
	today = new Date ();
	if (today.getDate () == 1) {
		if (today.getMonth() == 1) {
			this.dt.setYear (today.getFullYear () - 1);
			this.dt.setMonth (11);
			this.dt.setDate (31);
		} else {
			this.dt.setYear (today.getFullYear ());
			this.dt.setMonth (today.getMonth () - 1);
			this.dt.setDate (this.getDaysInMonth (today.getMonth () - 1));
		}
	} else {
		this.dt.setYear (today.getFullYear ());
		this.dt.setMonth (today.getMonth ());
		this.dt.setDate (today.getDate () - 1);
	}
	
	this.fill ();
}

DatePicker.prototype.onTomorrow = function () {
	today = new Date ();
	if (today.getDate () == this.getDaysInMonth (today)) {
		if (today.getMonth () == 11) {
			this.dt.setYear (today.getFullYear () + 1);
			this.dt.setMonth (0);
			this.dt.setDate (1);
		} else {
			this.dt.setYear (today.getFullYear ());
			this.dt.setMonth (today.getMonth() + 1);
			this.dt.setDate (1);
		}
	} else {
		this.dt.setYear (today.getFullYear ());
		this.dt.setMonth (today.getMonth ());
		this.dt.setDate (today.getDate () + 1);
	}
	
	this.fill ();
}

DatePicker.prototype.onWeekAgo = function () {
	today = new Date ();
	if (today.getDate () < 8) {
		if (today.getMonth () == 1) {
			this.dt.setYear (today.getFullYear () - 1);
			this.dt.setMonth (11);
			this.dt.setDate (31 + today.getDate () - 7);
		} else {
			this.dt.setYear (today.getFullYear ());
			this.dt.setMonth (today.getMonth () - 1);
			this.dt.setDate (this.getDaysInMonth (today) + today.getDate () - 7);
		}
	} else {
		this.dt.setYear (today.getFullYear ());
		this.dt.setMonth (today.getMonth ());
		this.dt.setDate (today.getDate () - 7);
	}
	
	this.fill ();
}

// new untested shorter version
DatePicker.prototype.daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
DatePicker.prototype.getDaysInMonthNewVersion = function (calDate) {
	var month = calDate.getMonth ();
	
	if ((month == 2) && isLeapYear (calDate.getFullYear ())) {
		return 29;
	} else {
		return this.daysInMonth[month];
	}
}

DatePicker.prototype.getDaysInMonth = function (calDate) {
	var days;
	var month = calDate.getMonth () + 1;
	var year = calDate.getFullYear ();
	// RETURN 31 DAYS 
	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
		days = 31;
	} else if (month == 4 || month == 6 || month == 9 || month == 11) { 
		// RETURN 30 DAYS 
		days = 30;
	} else if (month == 2) {
		if (isLeapYear (year)) { 
			// RETURN 28 DAYS 
			days = 29;
		} else { 
			// RETURN 28 DAYS 
			days = 28;
		}
	}
	return (days);
}

DatePicker.prototype.onWeekAfter = function () {
	today = new Date ();
	if (today.getDate () + 7 > this.getDaysInMonth (today)) {
		if (today.getMonth () == 11) {
			this.dt.setYear (today.getFullYear () + 1);
			this.dt.setMonth (0);
			this.dt.setDate (today.getDate () + 7 - this.getDaysInMonth (today));
		} else {
			this.dt.setYear (today.getFullYear ());
			this.dt.setMonth (today.getMonth () + 1);
			this.dt.setDate (today.getDate () + 7 - this.getDaysInMonth (today));
		}
	} else {
		this.dt.setYear (today.getFullYear ());
		this.dt.setMonth (today.getMonth ());
		this.dt.setDate (today.getDate () + 7);
	}
	
	this.fill ();
}

DatePicker.prototype.onMonthAgo = function () {
	today = new Date ();
	if (today.getMonth () == 1) {
		this.dt.setYear (today.getFullYear () - 1);
		this.dt.setMonth (11);
		this.dt.setDate (today.getDate ());
	} else {
		this.dt.setYear (today.getFullYear ());
		if (today.getDate () > this.getDaysInMonth (new Date (today.getFullYear (), today.getMonth () - 1, 1))) {
			this.dt.setMonth (today.getMonth () - 1);
			this.dt.setDate (this.getDaysInMonth (new Date (today.getFullYear (), today.getMonth () - 1, 1)));
		} else {
			this.dt.setMonth (today.getMonth () - 1);
			this.dt.setDate (today.getDate ());
		}
	}
	
	this.fill ();
}

DatePicker.prototype.onMonthAfter = function () {
	today = new Date ();
	if (today.getMonth () == 11) {
		this.dt.setYear (today.getYear () + 1);
		this.dt.setMonth (0);
		this.dt.setDate (today.getDate ());
	} else {
		this.dt.setYear (today.getFullYear ());
		this.dt.setMonth (today.getMonth () + 1);
		this.dt.setDate (today.getDate ());
	}
	
	this.fill ();
}

DatePicker.prototype.setHolidays = function (holidays)
{
    var arrDate = holidays.split('@');
    
    for(i = 0; i < arrDate.length; i++)
    {
        if(arrDate[i].toString().trim().length == 8)
        {              
            arrDate[i] = arrDate[i].toString().substr(0,4) + "-"
             + arrDate[i].toString().substr(4,2) + "-" 
             + arrDate[i].toString().substr(6,2);
        }
    }
    
    this.holidays.days = arrDate;
}

DatePicker.prototype.holidays = {
    days:["2007-09-24","2007-09-25","2007-09-26"],
    dayDescs:["chusuk1","chusuk2","chusuk3"]
}

DatePicker.prototype.texts = {
	months: [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" ],
	close: "Close",
	days: ["sun", "mon", "tue", "wed", "thu", "fri", "sat"],
	monthTitle: "Month",
	prevMonth: "Prev Month",
	nextMonth: "Next Month",
	yearTitle: "Year",
	today: "Today",
	yesterday: "Yesterday",
	tomorrow: "Tomorrow",
	weekAgo: "Last Week",
	weekAfter: "Next Week",
	monthAgo: "Prev Month",
	monthAfter: "Next Month",
	useRedDay: "false"
}

DatePicker.prototype.compareInfo = 
{
    compareObject : null,
    message : ["","",""],
    type : "",
    mark : ""
}

DatePicker.prototype.position = 
{
    top : 0,
    left : 0
}

DatePicker.prototype.script = {
    onClick : ""
}

// setting Date format
function callback (dt) {
    var year = dt.getFullYear();
    var month = pasteZero(dt.getMonth() + 1);      
    var day = pasteZero(dt.getDate());    
    
    var arrDate = new Array(year,month,day);
    
    oDatePicker.client.style.backgroundImage = '';
    oDatePicker.client.style.backgroundRepeat = '';
    oDatePicker.client.style.backgroundColor = '';
    
	oDatePicker.client.value = arrDate.join('-');
	
	if(oDatePicker.script.onClick.trim() != '')
	    eval(oDatePicker.script.onClick);
}

function pasteZero(num)
{
    if(Number(num) < 10)
        num = String(num).padLeft(1,'0');
    return num;
}

function getElementPosition(el) {
  var sLeft = 0, sTop = 0;
  var isDiv = /^div$/i.test(el.tagName);
  if (isDiv && el.scrollLeft) {
    sLeft = el.scrollLeft;
  }
  if (isDiv && el.scrollTop) {
    sTop = el.scrollTop;
  }
  var r = { left: el.offsetLeft - sLeft, top: el.offsetTop - sTop };
  if (el.offsetParent) {
    var tmp = getElementPosition(el.offsetParent);
    r.left += tmp.left;
    r.top += tmp.top;
  }
  return r;
}

function DatePickerShow (
	oTxt, oBtn, oCompare,type,mark,useRedDay,
	top,left,onblurScript) 
{	
	if(typeof(oTxt) == "string")
		oTxt = $(oTxt);
		
	if(typeof(oBtn) == "string")
		oBtn = $(oBtn);
		
	if(typeof(oCompare) == "string")
		oCompare = $(oCompare);

	responseUrl = "/index.jsp";
	
	if (!document.getElementById) {
		return;
	}
	
	oDatePicker = new DatePicker ("calendar_" + oTxt.id.replace(/txt/gi,'') , responseUrl);
	
	oDatePicker.compareInfo.compareObject = oCompare;
	oDatePicker.compareInfo.message = calInfo.ValidMessages;
	oDatePicker.compareInfo.type = type;
	oDatePicker.compareInfo.mark = mark;

	oDatePicker.texts.months = calInfo.Months;
	oDatePicker.texts.close = calInfo.Close;
	oDatePicker.texts.days = calInfo.Days;
	oDatePicker.texts.monthTitle = calInfo.MonthTitle;
	oDatePicker.texts.prevMonth = calInfo.PrevMonth;
	oDatePicker.texts.nextMonth = calInfo.NextMonth;
	oDatePicker.texts.yearTitle = calInfo.YearTitle;
	oDatePicker.texts.message = calInfo.ValidMessages;
	oDatePicker.texts.useRedDay = useRedDay;
	
	oDatePicker.position.top = top;
	oDatePicker.position.left = left;
	
	oDatePicker.script.onClick = onblurScript.trim();
	
	var aDt = oTxt.value.trim().replace(/\./g,"-").split ("-");
	var dt = null;
	if (aDt && (aDt.length == 3)) {
		dt = new Date ();
		dt.setFullYear (parseInt (aDt[0],10));
		dt.setMonth (parseInt (aDt[1],10)-1);
		dt.setDate (parseInt (aDt[2],10));
	}
	
	oDatePicker.client = oTxt;
	pos = getElementPosition (oBtn);
	
	oDatePicker.hideAll();
	
	oDatePicker.show (dt, pos.left, pos.top, callback);
}

// Checking Date Type format
function CheckDatePickerDate (oInput,oCompare,type,mark) {
	
	var cal = oInput.value.replace(/\./g,"-");

	var value = oInput.value;
	var objCompare = null;
	
	if(typeof(oCompare) == "string")	
		objCompare = document.getElementById(oCompare);
	else
		objCompare = oCompare;
		
	var rcode = true;
	var bYearSet = false, bMonthSet = false, bDaySet = false;
	var Day = "", Month = "", Year = "";
	
	var arrMsg = calInfo.ValidMessages;
	
		
	value = value.trim().replace(/\./g,"-");

	for (i = 0; i < value.length; i++) 
	{
	    if(value.charAt(i) != "-")
	    {
	        if(!bYearSet)
	        {
	            Year += value.charAt(i);
	        }
	        else
	        {
	            if(!bMonthSet)
	            {
	                Month += value.charAt(i);
	            }
	            else
	            {
	                if(!bDaySet)
	                {
	                    Day += value.charAt(i);
	                }
	            }
	        }
	    }
	    else
	    {
	        if(!bYearSet)
	        {
	            bYearSet = true;
	        }
	        else
	        {
	            if(!bMonthSet)
	            {
	                bMonthSet = true;
	            }
	            else
	            {
	                if(!bDaySet)
	                {
	                    bDaySet = true;
	                }
	            }
	        }
	    }
	}

	if (Year.length == 0) 
	{
		rcode = false;
	}

	Month -= 1;
	
	var Datum = new Date();
    Datum.setFullYear(Year, Month, Day);

	if (Datum.getYear () == Year && Datum.getMonth () == Month && Datum.getDate () == Day) 
	{
	    if(objCompare != null && objCompare.value.trim().length > 0)
	    {
	        var y = Number(objCompare.value.split('-')[0]);
	        var m = Number(objCompare.value.split('-')[1]) - 1;
	        var d = Number(objCompare.value.split('-')[2]);        
	        
	        
	        var dtCompare = new Date();
	        dtCompare.setFullYear(y,m,d);
	        
	        var strDate = objCompare.value;
	        
	        
	        
	        switch(type.toUpperCase())
	        {
	            case "FROM":
	                if(Datum > dtCompare)
	                {
	                    alert(strDate + arrMsg[2]);
	                    rcode = false;
	                }
	                break;
	            case "TO":
	                if(Datum < dtCompare)
	                {
	                    alert(strDate + arrMsg[1]);	                    
	                    rcode = false;
	                }
	                break;
	            case "ONLY":
	                if(mark == 1 && Datum > dtCompare) // 이전
	                {
	                    alert(strDate + arrMsg[2]);
	                    rcode = false;
	                }
	                else if(mark == 2 && Datum < dtCompare) // 이후
	                {
	                    alert(strDate + arrMsg[1]);	                    
	                    rcode = false;
	                }
	                else if(mark == 3) // 사이
	                {
	                    // 
	                }
	            break;
	        }
	    }
	    else if(objCompare == null)
	    {   
	        var dtToday = new Date();
	        
	        var strDate = dtToday.getFullYear() + "-" + eval(Number(dtToday.getMonth())+1) + "-" + dtToday.getDate();
	        
	        switch(type.toUpperCase())
	        {
	            case "FROM":
	                if(Datum > dtToday)
	                {
	                    alert(strDate + arrMsg[2]);
	                    rcode = false;
	                }
	                break;
	            case "TO":
	                if(Datum < dtToday)
	                {
	                    alert(strDate + arrMsg[1]);	                    
	                    rcode = false;
	                }
	                break;
	            case "ONLY":
	                if(mark == 1 && Datum > dtToday) // 이전
	                {
	                    alert(strDate + arrMsg[2]);
	                    rcode = false;
	                }
	                else if(mark == 2 && Datum < dtToday) // 이후
	                {
	                    alert(strDate + arrMsg[1]);	                    
	                    rcode = false;
	                }
	                else if(mark == 3) // 사이
	                {
	                    // 
	                }
	            break;
	        }
	    }
	    
	    if(!rcode)
	    {
	        oInput.value = "";	
	    }
	    else
	    {		
		var arrCal = cal.split('-');

		if(parseInt(arrCal[1]) < 10){
			arrCal[1] = '0' + parseInt(arrCal[1]);
		}

		if(parseInt(arrCal[2]) < 10){
			arrCal[2] = '0' + parseInt(arrCal[2]);
		}

		oInput.value = arrCal.join('-');
	        oInput.style.color = document.fgColor;
	    }

	    return rcode;
	} 
	else 
	{
		rcode = false;
	}


	if (rcode) 
	{
		var arrCal = cal.split('-');

		if(parseInt(arrCal[1]) < 10){
			arrCal[1] = '0' + parseInt(arrCal[1]);
		}

		if(parseInt(arrCal[2]) < 10){
			arrCal[2] = '0' + parseInt(arrCal[2]);
		}

		oInput.value = arrCal.join('-');
		oInput.style.color = document.fgColor;
	} 
	else 
	{
		if(value != "")
		{   	    
	        alert(arrMsg[0]);
	    }
	    
	    oInput.value = "";	    
	}
	
	return rcode;
}
