// JavaScript Document
document.write("<div id='__calendar' style='position:absolute;display:none;'></div>");
document.write("<table cellspacing=\"0\" cellpadding=\"0\" id=\"calendarTable\"><thead><tr class=\"function\"><th><a href=\"javascript:void(0);\" title=\"Previous month\" onclick=\"preMonth()\">&lt;</a></th><th colspan=\"5\" id=\"sohwdate\"></th><th><a href=\"javascript:void(0);\" title=\"Next month\" onclick=\"nextMonth()\">&gt;</a></th></tr><tr><th class=\"week\">日</th><th class=\"week\">一</th><th class=\"week\">二</th><th class=\"week\">三</th><th class=\"week\">四</th><th class=\"week\">五</th><th class=\"week\">六</th></thead><tbody id=\"calendarTbody\"></tbody></table>");

document.write("<div id='__calendar2' style='position:absolute;display:none;'></div>");
document.write("<table cellspacing=\"0\" cellpadding=\"0\" id=\"calendarTable2\"><thead><tr class=\"function\"><th><a href=\"javascript:void(0);\" title=\"Previous month\" onclick=\"preMonth2()\">&lt;</a></th><th colspan=\"5\" id=\"sohwdate2\"></th><th><a href=\"javascript:void(0);\" title=\"Next month\" onclick=\"nextMonth2()\">&gt;</a></th></tr><tr><th class=\"week\">日</th><th class=\"week\">一</th><th class=\"week\">二</th><th class=\"week\">三</th><th class=\"week\">四</th><th class=\"week\">五</th><th class=\"week\">六</th></thead><tbody id=\"calendarTbody2\"></tbody></table>");

var objouter; 
var objInput = "";
var objouter2;
var objInput2 = "";

var isShow = true;

objouter=document.getElementById("__calendar"); 
var calendarTable = document.getElementById("calendarTable");
objouter.appendChild(calendarTable);

objouter2=document.getElementById("__calendar2"); 
var calendarTable2 = document.getElementById("calendarTable2");
objouter2.appendChild(calendarTable2);

function setday(obj){objInput = obj;writeDate();sohwDate();objouter.style.top =getAbsoluteHeight(objInput)+getAbsoluteTop(objInput);objouter.style.left =getAbsoluteLeft(objInput);objouter.style.display = "block";}

function setday2(obj){objInput2 = obj;writeDate2();sohwDate2();objouter2.style.top =getAbsoluteHeight(objInput2)+getAbsoluteTop(objInput2);objouter2.style.left =getAbsoluteLeft(objInput2);objouter2.style.display = "block";}

function getAbsoluteHeight(ob){return ob.offsetHeight;}
function getAbsoluteWidth(ob){return ob.offsetWidth;}
function getAbsoluteLeft(ob){var s_el=0;el=ob;while(el){s_el=s_el+el.offsetLeft;el=el.offsetParent;}; return s_el}
function getAbsoluteTop(ob){var s_el=0;el=ob;while(el){s_el=s_el+el.offsetTop ;el=el.offsetParent;}; return s_el}

var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 
//var toDay = new Date();      //now writed in masterpage.master
var tempYear = toDay.getFullYear();
var tempMonth = toDay.getMonth()+1;
var tempDate = toDay.getDate();
var toDay2 = new Date(toDay.getFullYear(), toDay.getMonth(), toDay.getDate());
toDay2.setDate(toDay2.getDate()+1);
var tempYear2 = toDay2.getFullYear();
var tempMonth2 = toDay2.getMonth()+1;
var tempDate2 = toDay2.getDate();
var compareYear = toDay.getFullYear();
var compareMonth = toDay.getMonth()+1;
var compareDate = toDay.getDate();

var tbody = document.getElementById("calendarTbody"); 
var sohwId = document.getElementById("sohwdate");
var tbody2 = document.getElementById("calendarTbody2"); 
var sohwId2 = document.getElementById("sohwdate2");

function getDays(month, year)
{ 	
	if (1 == month) return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28; 
	else return daysInMonth[month]; 
} 

function writeDate() { 	
	var curCal = new Date(tempYear,tempMonth-1,1);
	var startDay = curCal.getDay();
	var daily = 0;
	var	today = toDay.getDate();
	if(tempYear != toDay.getFullYear() || tempMonth != toDay.getMonth()+1) today = -1;
	
	var large = 0;
	if((tempYear <  toDay.getFullYear()) || ((tempMonth < toDay.getMonth()+1) && (tempYear == toDay.getFullYear()))) large = -1;
	
	var todayStyle = "";
	var weekEndStyle = "";
	clear();
	var intDaysInMonth =getDays(curCal.getMonth(), curCal.getFullYear());
	var weeks = (intDaysInMonth + startDay) % 7 == 0 ? (intDaysInMonth + startDay) / 7 : parseInt((intDaysInMonth + startDay ) / 7) + 1;	

	for (var intWeek = 1; intWeek <= weeks; intWeek++){ 
		var tr = document.createElement("tr");
		tr.setAttribute("onmouseover","javascript:this.className='cur'");		
		tr.setAttribute("onmouseout","javascript:this.className='com'");
		tr.onmouseover = function (){this.className = "cur";}
		tr.onmouseout = function (){this.className = "com";}
		for (var intDay = 0;intDay < 7;intDay++){			
			var td = document.createElement("td");
			if ((intDay == startDay) && (0 == daily)) 
				daily = 1; 
				
			if(today == daily) 	todayStyle="today";
			
			if (intDay == 6 || intDay == 0) weekEndStyle = "week" ;
			
			if ((daily > 0) && (daily <= intDaysInMonth)) 
			{ 
			     if (large >= 0)
			     { if ((large != -1) && (daily >= today))
					{					 
					 td.innerHTML = "<a href=\"javascript:void(0);\" class=\""+ weekEndStyle + todayStyle +"\" onclick=\"getDate1('"+daily+"')\" title=\""+eval(tempMonth)+"月"+daily+"日\">" + daily + "</a>";}
					 else
					 { td.innerHTML="<a href=\"javascript:void(0);\" class=\"old\" title=\""+eval(tempMonth)+"月"+daily+"日\">" + daily + "</a>";}}
				
				 else
				  { td.innerHTML="<a href=\"javascript:void(0);\" class=\"old\" title=\""+eval(tempMonth)+"月"+daily+"日\">" + daily + "</a>";}
				todayStyle = "";
				weekEndStyle = "";
				daily++; 
			}else{ 
				td.innerHTML = "&nbsp;"; 
				todayStyle = "";
				weekEndStyle = "";
			}			
			tr.appendChild(td);			
		}
		tbody.appendChild(tr);
	} 
}

function writeDate2() { 
	var curCal = new Date(tempYear2,tempMonth2-1,1);

	var startDay = curCal.getDay();
	var daily = 0;
	var	today = (Number(tempDate)+1);

	if(tempYear2 != compareYear || tempMonth2 != compareMonth) today = -1;

	var large = 0;
	if((tempYear2 < compareYear) || ((tempMonth2 < compareMonth) && (tempYear2 == compareYear))) large = -1;

	
	var todayStyle = "";
	var weekEndStyle = "";
	clear2();
	var intDaysInMonth =getDays(curCal.getMonth(), curCal.getFullYear());
	var weeks = (intDaysInMonth + startDay) % 7 == 0 ? (intDaysInMonth + startDay) / 7 : parseInt((intDaysInMonth + startDay ) / 7) + 1;	

	for (var intWeek = 1; intWeek <= weeks; intWeek++){ 
		var tr = document.createElement("tr");
		tr.setAttribute("onmouseover","javascript:this.className='cur2'");		
		tr.setAttribute("onmouseout","javascript:this.className='com2'");
		tr.onmouseover = function (){this.className = "cur2";}
		tr.onmouseout = function (){this.className = "com2";}
		for (var intDay = 0;intDay < 7;intDay++){			
			var td = document.createElement("td");
			if ((intDay == startDay) && (0 == daily)) 
				daily = 1; 
				
			//if(tempDate == daily) 	todayStyle="today";
			
			if (intDay == 6 || intDay == 0) weekEndStyle = "week" ;
			
			if ((daily > 0) && (daily <= intDaysInMonth)) 
			{ 
			     if (large >= 0)
			     { if ((large != -1) && (daily >= today))
					{					 
					 td.innerHTML = "<a href=\"javascript:void(0);\" class=\""+ weekEndStyle + todayStyle +"\" onclick=\"getDate2('"+daily+"')\" title=\""+eval(tempMonth2)+"月"+daily+"日\">" + daily + "</a>";}
					 else
					 { td.innerHTML="<a href=\"javascript:void(0);\" class=\"old\" title=\""+eval(tempMonth2)+"月"+daily+"日\">" + daily + "</a>";}}
				
				 else
				  { td.innerHTML="<a href=\"javascript:void(0);\" class=\"old\" title=\""+eval(tempMonth2)+"月"+daily+"日\">" + daily + "</a>";}
				todayStyle = "";
				weekEndStyle = "";
				daily++; 
			}else{ 
				td.innerHTML = "&nbsp;"; 
				todayStyle = "";
				weekEndStyle = "";
			}			
			tr.appendChild(td);			
		}
		tbody2.appendChild(tr);
	} 
}

function getDate1(day){
	var year , month ,date;
	if(day == "0"){
		year = toDay.getFullYear();
		month = eval(toDay.getMonth()) < 10 ? "0"+eval(toDay.getMonth()) : eval(toDay.getMonth());
		date = toDay.getDate() < 10 ? "0"+toDay.getDate() : toDay.getDate();
	}else{
		year = tempYear;
		month = eval(tempMonth) < 10 ? "0"+eval(tempMonth) : eval(tempMonth);
		date = day < 10 ? "0"+day : day;		
	}
	objInput.value = year + "-" + month +"-"+ date;

	if(Date.UTC( year, month, date, 0,0,0) >= Date.UTC( tempYear2, tempMonth2, tempDate2,0,0,0)){
		var year2 , month2 ,date2;
		var myDate=new Date(year, month-1, date);
		myDate.setDate(myDate.getDate() + 1);
		year2 = myDate.getYear();		
		month2 = eval(myDate.getMonth()+1) < 10 ? "0"+eval(myDate.getMonth()+1) : eval(myDate.getMonth()+1);
		date2 = myDate.getDate() < 10 ? "0"+myDate.getDate() : myDate.getDate();
	
		objInput2.value = year2 + "-" + month2 +"-"+ date2;
		document.aspnetForm.ctl00_ContentPlaceHolder1_outdateSearch.value = year2 + "-" + month2 +"-"+ date2;
	}

	tempYear = year;
	tempMonth = month;
	tempDate = date;
	tempYear2 = year;
	tempMonth2 = month;
	compareYear = year;
	compareMonth = month;
	compareDate = date;
	close1();
}

function getDate2(day){
	var year , month ,date;
	if(day == "0"){
		year = toDay.getFullYear();
		month = eval(toDay.getMonth()) < 10 ? "0"+eval(toDay.getMonth()) : eval(toDay.getMonth());
		date = toDay.getDate() < 10 ? "0"+toDay.getDate() : toDay.getDate();
	}else{
		year = tempYear2;
		month = eval(Number(tempMonth2)) < 10 ? "0"+eval(Number(tempMonth2)) : eval(Number(tempMonth2));
		date = day < 10 ? "0"+day : day;		
	}
	objInput2.value = year + "-" + month + "-" + date;
	tempYear2 = year;
	tempMonth2 = month;
	tempDate2 = date;
	close2();
}

function sohwDate(){
	sohwId.innerHTML = tempYear + "年" + eval(Number(tempMonth)) +"月";
}

function sohwDate2(){
	sohwId2.innerHTML = tempYear2 + "年" + eval(Number(tempMonth2)) +"月";
}

function preYear(){
	isShow = false;
	if(tempYear > 999 && tempYear < 10000){
		tempYear--;
	}else{
		alert("Year Over (1000~9999)!");
	}
	sohwDate();
	writeDate();
}

function nextYear(){
	isShow = false;
	if(tempYear > 999 && tempYear < 10000){
		tempYear++;
	}else{
		alert("Year Over (1000~9999)!");
	}
	sohwDate();
	writeDate();
}

function preMonth(){
	isShow = false;
	if(tempMonth == 1){tempYear--;tempMonth = 12;}else{tempMonth--}
	sohwDate();
	writeDate();
}

function nextMonth(){
	isShow = false;
	if(tempMonth == 12){tempYear++;tempMonth = 1}else{tempMonth++}
	sohwDate();
	writeDate();
}

function preMonth2(){
	isShow = false;
	if(tempMonth2 == 1){tempYear2--;tempMonth2 = 12;}else{tempMonth2--}
	sohwDate2();
	writeDate2();
}

function nextMonth2(){
	isShow = false;
	if(tempMonth2 == 12){tempYear2++;tempMonth2 = 1}else{tempMonth2++}
	sohwDate2();
	writeDate2();
}

function clear(){
	var nodes = tbody.childNodes;
	var nodesNum = nodes.length; 
	for(var i=nodesNum-1;i>=0;i--) { 
		tbody.removeChild(nodes[i]); 
	}
}

function clear2(){
	var nodes = tbody2.childNodes;
	var nodesNum = nodes.length; 
	for(var i=nodesNum-1;i>=0;i--) { 
		tbody2.removeChild(nodes[i]); 
	}
}

function shut(){
	close1();
	close2();
}

function close1(){
	//tempYear = toDay.getFullYear();
	//tempMonth = toDay.getMonth();
	objouter.style.display = "none"
	objouter.style.top = 0;
	objouter.style.left = 0;
}

function close2(){
	objouter2.style.display = "none"
	objouter2.style.top = 0;
	objouter2.style.left = 0;
}


function vent(event){
	if(document.all){
		if(isShow){
			if (window.event.srcElement != objouter && window.event.srcElement != objInput) close1();
			if (window.event.srcElement != objouter2 && window.event.srcElement != objInput2) close2();
			isShow = true;
			return;
		}
		isShow = true;		
	}else{
		if(isShow){
			if(event.target != objouter && event.target != objInput) close1();
			if(event.target != objouter2 && event.target != objInput2) close2();		
			isShow = true;
			return;
		}
		isShow = true;
	}
}
document.onclick = vent;