

var nowYear ;
var nowMonth ;
//var dateFormat = "MM/DD/YYYY" ;
var dateFormat = "YYYY-MM-DD" ;
var targetInput ;

function getDates(year,month){
    month = month*1+1;
    if(month == 1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){
        return 31 ;
    }else if( month==4 || month==6 || month==9 || month==11){
        return 30 ;
    }else{
        if(year%400 ==0 || (year%100 !=0 && year%4 == 0)){
            return 29 ;
        }
        return 28 ;
    }
}
function loadMonth(year,month){

    var date = new Date();

	var today= new Date();
	var isThisMonth=(today.getYear()==year)&&(today.getMonth()==month);
	//alert(isThisMonth);

    date.setYear(year);
    date.setMonth(month);
    date.setDate(1);

    nowYear = year ;
    nowMonth = month ;

    selectTo(calendar_year,nowYear);
    selectTo(calendar_month,nowMonth);

    var day = date.getDay();	//get week day
	if (day>0){
		date.setDate(-day);
	}
	var table_calender=document.getElementById("table_calender");
    //alert(table_calender);
    for(var t=1;t<=6; t++){
        for(var i=0;i<=6;i++){
			var dateOfMonth=date.getDate();
            table_calender.rows[t].cells[i].innerText = dateOfMonth;
            table_calender.rows[t].cells[i].title     = formatDate(date);
			if(date.getMonth()!=nowMonth){
				table_calender.rows[t].cells[i].className = "td_other_month_day";
			}else{	            
				if(date.getTime()==today.getTime()){
					table_calender.rows[t].cells[i].className = "td_today" ;
				}else{
					table_calender.rows[t].cells[i].className = "td_day" ;
				}
			}
			date.setDate(dateOfMonth+1);
        }
    }
}

function refresh_calendar(me){
	nowMonth = parseInt(calendar_month.value);
    loadMonth(calendar_year.value,calendar_month.value);
	me.blur();
	lockCalen = false;
}
function changeLoad(value){
    addMonth(value);
    loadMonth(nowYear,nowMonth);
}

function selectTo(box,val){
    for(var i=0;i<box.options.length;i++){
        if(box.options[i].value*1 == val*1){
            box.options[i].selected = true ;
        }
    }
}
function addMonth(offset){
    nowMonth = nowMonth*1 + offset*1 ;
    while(nowMonth < 0){
        nowMonth = nowMonth*1 + 12*1 ;
        nowYear-- ;
    }
    while(nowMonth >= 12){
        nowMonth = nowMonth*1 - 12*1 ;
        nowYear++ ;
    }
}
function setDateFormat(format){
    dateFormat = format ;
}

function doReturnDate(me){
    div_calender.style.display = "none";
    targetInput.value = me.title;
    targetInput.focus();
}

function formatDate(date){
	var dist=dateFormat;
	dist=dist.replace(/YYYY/i,""+date.getYear());
	dist=dist.replace(/MM/i,lpad(""+(date.getMonth()+1),"0",2));
	dist=dist.replace(/DD/i,lpad(""+date.getDate(),"0",2));
	return dist;
}


function resetClass(){
    for(var i=1;i<7;i++){
        for(var j=0;j<7;j++){
            table_calender.rows[i].cells[j].className = "td_day";
        }
    }
}

function setDate(event,me){
	if(event==null)event=window.event;
    targetInput = document.getElementById(me);
    var x = event.clientX+document.body.scrollLeft;
    var y = event.clientY+document.body.scrollTop;
    div_calender.style.left = x-8;
    div_calender.style.top  = y-8;
    if(x>856)
    div_calender.style.left = x-100;
    div_calender.style.display = "block";
}

var mouseOnCD = false;
var mouseOnCT = false;
var mouseOnCC = false;
var lockCalen = false;

function doOverCD(me){
	mouseOnCD = true ;
}
function doOutCD(me){
	mouseOnCD = false ;
	setTimeout("hiddenCalender()",100);
}
function doOverCT(me){
	mouseOnCT = true ;
}
function doOutCT(me){
	mouseOnCT = false ;
}
function doOverCC(me){
	mouseOnCC = true ;
}
function doOutCC(me){
	mouseOnCC = false ;
}
function do_lockC(flag){
	lockCalen = flag;
}

function showCalenderTable(){
    document.writeln("<div id='div_calender' class='div_calendar' style='display:none;' style='position:absolute;z-index:3;' onmouseover=\"doOverCD(this)\" onmouseout=\"doOutCD(this)\">");
    document.writeln("<TABLE class='tb_calendar' style='border-bottom: 0px solid;' width=148 onmouseover=\"doOverCT(this)\" onmouseout=\"doOutCT(this)\">");
    document.writeln("<TR>");
    document.writeln("	<TD class='td_set' style='width:20px;text-align:center;'>");
    document.writeln("		<img src='../images/aheadD.gif' style='cursor:hand;' onclick='changeLoad(-1)'></img>");
    document.writeln("	</TD>");
    document.writeln("	<TD class='td_set'>");
    document.writeln("		<select class='cal_select' name='calendar_year' id='calendar_year'  onchange='refresh_calendar(this)' onfocus=\"do_lockC(true)\" onblur=\"do_lockC(false)\">");
    for(var i=1950;i<2050;i++){
        document.writeln("			<option value='"+i+"'>"+i+"</option>");
    }
    document.writeln("		</select>");
    document.writeln("	</TD>");
    document.writeln("	<TD class='td_set'>");
    document.writeln("		<select class='cal_select' name='calendar_month' id='calendar_month' onchange='refresh_calendar(this)' onfocus=\"do_lockC(true)\" onblur=\"do_lockC(false)\">");
    for(var i=0;i<12;i++){
        document.writeln("			<option value='"+i+"'>"+(i+1)+"</option>");
    }
    document.writeln("		</select>");
    document.writeln("	</TD>");
    document.writeln("	<TD class='td_set' onmouseover='this.className=\"td_set_mouseover\"'  onmouseout='this.className=\"td_set\"'>");
    document.writeln("<div title=\""+formatDate(new Date())+"\" onclick=\"doReturnDate(this)\">Today</div>");
    document.writeln("	</TD>");
    document.writeln("	<TD class='td_set' style='width:20px;text-align:center;'>");
    document.writeln("		<img src='../images/nextD.gif' style='cursor:hand;' onclick='changeLoad(1)'></img>");
    document.writeln("	</TD>");
    document.writeln("</TR>");
    document.writeln("</TABLE>");
    document.writeln("<TABLE border=1 class='tb_calendar' id='table_calender' width=148 onmouseover=\"doOverCC(this)\" onmouseout=\"doOutCC(this)\">");
    document.writeln("<TR>");
    document.writeln("	<TD class=td_cap>Su</TD>");
    document.writeln("	<TD class=td_cap>Mo</TD>");
    document.writeln("	<TD class=td_cap>Tu</TD>");
    document.writeln("	<TD class=td_cap>We</TD>");
    document.writeln("	<TD class=td_cap>Th</TD>");
    document.writeln("	<TD class=td_cap>Fr</TD>");
    document.writeln("	<TD class=td_cap>Sa</TD>");
    document.writeln("</TR>");
	for(var w=0;w<6;w++){
		document.writeln("<TR>");
		for(var d=0;d<7;d++){
			document.writeln("	<TD class='td_day' onclick='doReturnDate(this)' onmouseover='this.style.background=\"#508F21\"' onmouseout='this.style.background=\"#F5F5F5\"'></TD>");
		}
		document.writeln("</TR>");
	}
    document.writeln("</TABLE>");
    document.writeln("</div>");
}

function hiddenCalender(){
	if(!mouseOnCD && !mouseOnCT && !mouseOnCC && !lockCalen){
		div_calender.style.display = "none";
	}
}

function calenderInit(date){
    nowYear = date.getYear();
    nowMonth = date.getMonth();
    showCalenderTable();
    loadMonth(nowYear,nowMonth);
}
function lpad(str,chr,len){
	while(str.length < len){
		str = chr+str;
	}
	return str ;
}
calenderInit(new Date());

