var ox,oy;
var caldar;
function atCalendarControl(lan){
var calendar=this;
caldar=this;
this.calendarPad=null;
this.calendarPad1=null;
this.prevMonth=null;
this.nextMonth=null;
this.goToday=null;
this.head=null;
this.body=null;
this.head1=null;
this.body1=null;
this.today=[];
this.currentDate=[];
this.sltDate;
this.target;
this.source;

/************** 加入日历底板及阴影 *********************/
this.addCalendarPad=function(){
document.write("<div id='divCalendarpad' name='gggggg' style='position:absolute;top:0;left:20;display:none;'>");
document.write("<div style='position:absolute;top:2;left:22;'></div>");
document.write("</div>");
calendar.calendarPad=document.all.divCalendarpad;
}
/************** 加入日历面板 *********************/
this.addCalendarBoard=function(){
var BOARD=this;
var divBoard=document.createElement("div");
calendar.calendarPad.insertAdjacentElement("beforeEnd",divBoard);
var tbBoard=document.createElement("table");
divBoard.insertAdjacentElement("beforeEnd",tbBoard);
tbBoard.style.cssText="border-style:solid;border-color:#aa0000;border-width:3px;"; 
tbBoard.cellPadding=0;
tbBoard.cellSpacing=0;
tbBoard.height=144;
tbBoard.width=190;

/************** 设置各功能按钮的功能 *********************/
/*********** Calendar About Button ***************/
trRow = tbBoard.insertRow(0);
trRow.bgColor="#cc0000";
trRow.height="26px";
calendar.prevMonth = calendar.insertTbCell(trRow,0,"←","center");
if(lan=="1")
calendar.prevMonth.title="← 上一月 ";
else
calendar.prevMonth.title="← last month ";
calendar.prevMonth.onmousedown=function(){
calendar.currentDate[1]--;
if(calendar.currentDate[1]==0){
calendar.currentDate[1]=12;
calendar.currentDate[0]--;
}
calendar.show(calendar.target,calendar.currentDate[0]+"-"+calendar.formatTime(calendar.currentDate[1])+"-"+calendar.formatTime(calendar.currentDate[2]),calendar.source);
}
/*********** Calendar Head ***************/
tbCell=trRow.insertCell(1);
tbCell.colSpan=6;
tbCell.bgColor="#cc0000"; 
tbCell.align="center"; 
tbCell.style.cssText = "font-weight:bold;font-size:12pt;color: #ffffff;font-family: 'Arial','宋体'"; 
calendar.head=tbCell;

trRow= tbBoard.insertRow(1);
trRow.height="20px"; 
if(lan=="1")
var cnDateName = new Array("日","一","二","三","四","五","六");
else
var cnDateName = new Array("Sun","Mon","Tues ","Wed","Thur","Fri","Sat");
for (var i = 0; i < 7; i++) {
tbCell=trRow.insertCell(i);
tbCell.innerText=cnDateName[i];

tbCell.align="center";
tbCell.width="29px";
if(lan=="1"){
if(i=="0" || i=="6")
tbCell.style.cssText="background-color:#666666;color:red;font-weight:bold;font-size:9pt;";
else
tbCell.style.cssText="background-color:#666666;color:#ffffff;font-weight:bold;font-size:9pt;";
}
else
tbCell.style.cssText="background-color:#666666;color:#ffffff;font-weight:bold;font-size:7pt;";
}

/*********** Calendar Body ***************/
trRow= tbBoard.insertRow(2);
tbCell=trRow.insertCell(0);
tbCell.colSpan=7;
tbCell.height=152; 
tbCell.vAlign="top"; 
tbCell.bgColor="#ffffff"; 

var tbBody=document.createElement("table");
tbCell.insertAdjacentElement("beforeEnd",tbBody);
tbBody.height=112; 
tbBody.width=200;
tbBody.cellPadding=3; 
tbBody.cellSpacing=3; 
calendar.body=tbBody;

}

/************** 加入日历底板及阴影1 *********************/
this.addCalendarPad1=function(){
document.write("<div id='divCalendarpad1' style='position:absolute;display:none;'>");
document.write("<div style=''></div>");
document.write("</div>");
calendar.calendarPad1=document.all.divCalendarpad1;
}
/************** 加入日历面板1 *********************/
this.addCalendarBoard1=function(){
var BOARD1=this;
var divBoard1=document.createElement("div");
calendar.calendarPad1.insertAdjacentElement("beforeEnd",divBoard1);
divBoard1.style.cssText="";

var tbBoard1=document.createElement("table");
divBoard1.insertAdjacentElement("beforeEnd",tbBoard1);
tbBoard1.style.cssText="border-style:solid;border-color:#aa0000;border-width:3px;"; 
tbBoard1.cellPadding=0; 
tbBoard1.cellSpacing=0; 
tbBoard1.height=164; 
tbBoard1.width=190;
/************** 设置各功能按钮的功能 *********************/
/*********** Calendar About Button ***************/

/*********** Calendar Head ***************/
trRow1 = tbBoard1.insertRow(0);
trRow1.bgColor="#cc0000";
trRow1.height="26px";  
tbCell1=trRow1.insertCell(0); 
tbCell1.colSpan=6; 
tbCell1.bgColor="#cc0000"; 
tbCell1.align="center"; 
tbCell1.style.cssText = "font-weight:bold;font-size:12pt;color: #ffffff;font-family: 'Arial','宋体'"; 
calendar.head1=tbCell1; 
/*********** Calendar NextMonth Button ***************/


calendar.nextMonth = calendar.insertTbCell(trRow1,1,"→","center");
if(lan=="1")
calendar.nextMonth.title="下一月 →";
else
calendar.nextMonth.title="next month →";
calendar.nextMonth.onmousedown=function(){
calendar.currentDate[1]++;
if(calendar.currentDate[1]==13){
calendar.currentDate[1]=1;
calendar.currentDate[0]++;
}
calendar.show(calendar.target,calendar.currentDate[0]+"-"+calendar.formatTime(calendar.currentDate[1])+"-"+calendar.formatTime(calendar.currentDate[2]),calendar.source);
}


trRow1 = tbBoard1.insertRow(1);
trRow1.height="20px"; 
if(lan=="1")
var cnDateName = new Array("日","一","二","三","四","五","六");
else
var cnDateName = new Array("Sun","Mon","Tues ","Wed","Thur","Fri","Sat");
for (var i = 0; i < 7; i++) {
tbCell1=trRow1.insertCell(i)
tbCell1.innerText=cnDateName[i];
tbCell1.align="center";
tbCell1.width="29px";
if(lan=="1"){
if(i=="0" || i=="6")
tbCell1.style.cssText="background-color:#666666;color:red;font-weight:bold;font-size:9pt;";
else
tbCell1.style.cssText="background-color:#666666;color:#ffffff;font-weight:bold;font-size:9pt;";
}
else
tbCell1.style.cssText="background-color:#666666;color:#ffffff;font-weight:bold;font-size:7pt;";
}

/*********** Calendar Body ***************/
trRow1 = tbBoard1.insertRow(2);
tbCell1=trRow1.insertCell(0); 
tbCell1.colSpan=7; 
tbCell1.height=152; 
tbCell1.vAlign="top"; 
tbCell1.bgColor="#ffffff"; 

var tbBody1=document.createElement("table");
tbCell1.insertAdjacentElement("beforeEnd",tbBody1); 
tbBody1.height=112; 
tbBody1.width=200;
tbBody1.cellPadding=3; 
tbBody1.cellSpacing=3; 
calendar.body1=tbBody1; 

/*********** Time Body ***************/

}

/************** 加入功能按钮公共样式 *********************/
this.insertTbCell=function(trRow,cellIndex,TXT,trAlign,tbColSpan){
var tbCell=trRow.insertCell(cellIndex);
var btnCell=document.createElement("button");
tbCell.insertAdjacentElement("beforeEnd",btnCell);
btnCell.value=TXT;
btnCell.width="40px";
btnCell.style.cssText="border:0;font-weight:bold;color: #ffffff;background-color:#cc0000;font-size:14pt;"; 
btnCell.onmouseover=function(){
btnCell.style.cssText="border:0;font-weight:bold;color: #ffffff;background-color:#cc0000;cursor:hand;font-size:14pt;"; 

}
btnCell.onmouseout=function(){
btnCell.style.cssText="border:0;font-weight:bold;color: #ffffff;background-color:#cc0000;font-size:14pt;"; 
}

btnCell.onclick=function(){
btnCell.blur();
}
return btnCell;
}

this.setDefaultDate=function(){
var dftDate=new Date();
calendar.today[0]=dftDate.getYear();
calendar.today[1]=dftDate.getMonth()+1;
calendar.today[2]=dftDate.getDate();
}

/****************** Show Calendar *********************/
this.show=function(targetObject,defaultDate,sourceObject){
if(document.getElementById("butSear")!=undefined)
document.getElementById("butSear").style.display="none";
if(targetObject==undefined) {
alert("未设置目标对象. \n方法: ATCALENDAR.show(obj 目标对象,boolean 是否返回时间,string 默认日期,obj 点击对象);\n\n目标对象:接受日期返回值的对象.\n默认日期:格式为\"yyyy-mm-dd\",缺省为当前日期.\n点击对象:点击这个对象弹出calendar,默认为目标对象.\n");
return false;
}
else calendar.target=targetObject;

if(sourceObject==undefined) calendar.source=calendar.target;
else calendar.source=sourceObject;


var firstDay;
var Cells=new Array();
if((defaultDate==undefined) || (defaultDate=="")){
var theDate=new Array();
if(lan=="1"){
calendar.head.innerText = calendar.today[0]+"\t年\t"+calendar.formatTime(calendar.today[1])+"\t月";}
else{
calendar.head.innerText = calendar.formatTime(calendar.today[1])+"/"+calendar.today[0];}
if(calendar.today[1]+1==13){
if(lan=="1"){
calendar.head1.innerText = calendar.today[0]+1+"\t年\t01\t月";}
else{
calendar.head1.innerText = "01/"+calendar.today[0]+1;}
}else{
if(lan=="1"){
calendar.head1.innerText = calendar.today[0]+"\t年\t"+calendar.formatTime(calendar.today[1]+1)+"\t月";}
else{
calendar.head1.innerText =calendar.formatTime(calendar.today[1]+1)+"/"+ calendar.today[0];}}

theDate[0]=calendar.today[0];
 theDate[1]=calendar.today[1];
 theDate[2]=calendar.today[2];

}
else{
var Datereg=/^\d{4}-\d{1,2}-\d{2}$/
if(!defaultDate.match(Datereg)) {
alert("默认日期(时间)的格式不正确！\t\n\n默认可接受格式为:\n1、yyyy-mm-dd \n2、yyyy-mm-dd hh:mm\n3、(空)");
calendar.setDefaultDate();
return;
}

if(defaultDate.match(Datereg))
var strDateTime=defaultDate.split("-");
var theDate=new Array();
theDate[0]=strDateTime[0];
theDate[1]=strDateTime[1];
theDate[2]=strDateTime[2];

if(lan=="1"){
calendar.head.innerText = theDate[0]+"\t年\t"+calendar.formatTime(theDate[1])+"\t月";}
else{
calendar.head.innerText =calendar.formatTime(theDate[1])+"/"+theDate[0];}
if(parseInt(theDate[1],10)+1==13){
if(lan=="1"){
calendar.head1.innerText = parseInt(theDate[0],10)+1+"\t年\t01\t月";}
else{
calendar.head1.innerText = "01/"+ (parseInt(theDate[0],10)+1);}
}else{
if(lan=="1"){
calendar.head1.innerText = theDate[0]+"\t年\t"+calendar.formatTime(parseInt(theDate[1],10)+1)+"\t月";}
else{
calendar.head1.innerText =calendar.formatTime(parseInt(theDate[1],10)+1)+"/"+theDate[0];}
} }
calendar.currentDate[0]=theDate[0];
calendar.currentDate[1]=theDate[1];
calendar.currentDate[2]=theDate[2];

theFirstDay=calendar.getFirstDay(theDate[0],theDate[1]);
theMonthLen=theFirstDay+calendar.getMonthLen(theDate[0],theDate[1]);
theFirstDay1=calendar.getFirstDay(theDate[0],parseInt(theDate[1],10)+1);
theMonthLen1=theFirstDay1+calendar.getMonthLen(theDate[0],parseInt(theDate[1],10)+1);

calendar.calendarPad.style.display="";
calendar.calendarPad1.style.display="";
var theRows = Math.ceil((theMonthLen)/7);
var theRows1 = Math.ceil((theMonthLen1)/7);
//清除旧的日历;
while (calendar.body.rows.length > 0) {
calendar.body.deleteRow(0)
}
while (calendar.body1.rows.length > 0) {
calendar.body1.deleteRow(0)
}
//建立新的日历;
var n=0;day=0;
for(i=0;i<theRows;i++){
theRow=calendar.body.insertRow(i);
for(j=0;j<7;j++){
n++;
if(n>theFirstDay && n<=theMonthLen){
day=n-theFirstDay;
calendar.insertBodyCell(theRow,j,day,theDate[1],0);
}

else{
var theCell=theRow.insertCell(j);
theCell.style.cssText="";
}
}
}
//建立新的日历1;
var n=0;day=0;

for(i=0;i<theRows1;i++){
theRow1=calendar.body1.insertRow(i);
for(j=0;j<7;j++){
n++;
if(n>theFirstDay1 && n<=theMonthLen1){
day=n-theFirstDay1;
calendar.insertBodyCell(theRow1,j,day,parseInt(theDate[1],10)+1,1);

}

else{
var theCell=theRow1.insertCell(j);
theCell.style.cssText="";
}
}
}
//****************调整日历位置**************//
var offsetPos=calendar.getAbsolutePos(calendar.source);//计算对象的位置;
if((document.body.offsetHeight-(offsetPos.y+calendar.source.offsetHeight-document.documentElement.scrollTop))<calendar.calendarPad.style.pixelHeight){
var calTop=offsetPos.y-calendar.calendarPad.style.pixelHeight;
}
else{
var calTop=offsetPos.y+calendar.source.offsetHeight;
}
if((document.body.offsetWidth-(offsetPos.x+calendar.source.offsetWidth-document.documentElement.scrollLeft))>calendar.calendarPad.style.pixelWidth){
var calLeft=offsetPos.x;
}
else{
var calLeft=calendar.source.offsetLeft+calendar.source.offsetWidth;
}
if(calLeft+350>document.body.offsetWidth){
calendar.calendarPad.style.pixelLeft=calLeft-(calLeft+350-document.body.offsetWidth)-120;}else{
calendar.calendarPad.style.pixelLeft=calLeft-120; }
calendar.calendarPad.style.pixelTop=calTop;
calendar.calendarPad1.style.pixelLeft=calendar.calendarPad.style.pixelLeft+205;
calendar.calendarPad1.style.pixelTop=calTop;
ox=calendar.calendarPad.style.pixelLeft;
oy=calendar.calendarPad.style.pixelTop;
}
/****************** 计算对象的位置 *************************/
this.getAbsolutePos = function(el) {
var r = { x: el.offsetLeft, y: el.offsetTop };
if (el.offsetParent) {
var tmp = calendar.getAbsolutePos(el.offsetParent);
r.x += tmp.x;
r.y += tmp.y;
}
return r;
};

//************* 插入日期单元格 **************/
this.insertBodyCell=function(theRow,j,day,month,hit,targetObject){
var theCell=theRow.insertCell(j);
var theBgColor="#FFFFFF"; //其他日子的颜色
if(hit==0){
var days=((parseInt(calendar.currentDate[0],10)-2000)*365+day+parseInt(calendar.currentDate[1],10)*30)*3600*24;}else{
var days=((parseInt(calendar.currentDate[0],10)-2000)*365+day+(parseInt(calendar.currentDate[1],10)+1)*30)*3600*24;
}
var nowDays=((parseInt(calendar.today[0],10)-2000)*365+parseInt(calendar.today[2],10)+parseInt(calendar.today[1],10)*30)*3600*24;

if(calendar.target.value!=""){
var textTime=calendar.target.value.split("-") ;
if(day==textTime[2]&&month==textTime[1]&&calendar.currentDate[0]==textTime[0]) var theBgColor="red";
}//鏂囦綋妗嗘棩瀛愮殑棰滆壊

theCell.bgColor=theBgColor;
theCell.innerText=day;
theCell.align="center";
theCell.width="27px";
if(day==calendar.today[2]&&month==calendar.today[1]&&calendar.currentDate[0]==calendar.today[0]){
theCell.style.cssText="font-size:9pt;color:#ffffff;font-weight:bold;background-color:red;font-family:'verdana'";
}else{
if(nowDays>days) {
theCell.style.cssText="font-size:9pt;color:#888888;font-family:'verdana'"; 
}else{
if(j=="0"||j=="6")
theCell.style.cssText="font-size:9pt;color:red;text-decoration:underline;font-weight:bold;font-family:'verdana'";
else
theCell.style.cssText="font-size:9pt;color:#0000FE;text-decoration:underline;font-weight:bold;font-family:'verdana'"; 
}
}
theCell.onmouseover=function(){
if(days<nowDays){
theCell.style.cssText="font-size:9pt;color:#888888;font-family:'verdana'";
}else{
theCell.bgColor="red";
theCell.style.cssText="font-size:9pt;color:#0000FE;cursor:hand;text-decoration:underline;font-weight:bold;font-family:'verdana'";} 
if(day==calendar.today[2]&&month==calendar.today[1]&&calendar.currentDate[0]==calendar.today[0]){
theCell.style.cssText="font-size:10pt;color:#000000;font-weight:bold;background-color:#ffc650;text-decoration:underline;cursor:hand;";
}
}
theCell.onmouseout=function(){
if(days<nowDays){
theCell.style.cssText="font-size:9pt;color:#888888;font-family:'verdana'";
}else{ 
 
theCell.bgColor=theBgColor; 
if(j=="0"||j=="6")
theCell.style.cssText="font-size:9pt;color:red;cursor:hand;text-decoration:underline;font-weight:bold;font-family:'verdana'";
else
theCell.style.cssText="font-size:9pt;color:#0000FE;cursor:hand;text-decoration:underline;font-weight:bold;font-family:'verdana'"; }
if(day==calendar.today[2]&&month==calendar.today[1]&&calendar.currentDate[0]==calendar.today[0]){
theCell.style.cssText="font-size:9pt;color:#ffffff;font-weight:bold;background-color:red;font-family:'verdana'";
}
}
theCell.onmousedown=function(){
if(days<nowDays) return;
theCell.bgColor="#FFFFCC"; 
theCell.style.cssText="font-size:9pt;color:#000000;font-family:'verdana'"; 
}
theCell.onclick=function(){
if(days<nowDays) return;
if(hit==0){
calendar.sltDate=calendar.currentDate[0]+"-"+calendar.formatTime(calendar.currentDate[1])+"-"+calendar.formatTime(day); }
else{
if(parseInt(calendar.currentDate[1],10)+1==13){
calendar.sltDate=parseInt(calendar.currentDate[0],10)+1+"-01"+"-"+calendar.formatTime(day);
}else{
calendar.sltDate=calendar.currentDate[0]+"-"+calendar.formatTime(parseInt(calendar.currentDate[1],10)+1)+"-"+calendar.formatTime(day);}
}
calendar.target.value=calendar.sltDate;
calendar.hide();
}
}
/************** 取得月份的第一天为星期几 *********************/
this.getFirstDay=function(theYear, theMonth){
var firstDate = new Date(theYear,theMonth-1,1);
return firstDate.getDay();
}
/************** 取得月份共有几天 *********************/

this.getMonthLen=function(theYear, theMonth) {
theMonth--;
var oneDay = 1000 * 60 * 60 * 24;
var thisMonth = new Date(theYear, theMonth, 1);
var nextMonth = new Date(theYear, theMonth + 1, 1);
var len = Math.ceil((nextMonth.getTime() - thisMonth.getTime())/oneDay);
return len;
}
/************** 隐藏日历 *********************/
this.hide=function(){
//calendar.clearEventKey();
calendar.calendarPad.style.display="none";
calendar.calendarPad1.style.display="none";
if(document.getElementById("dstcity")!=undefined)
document.getElementById("dstcity").style.display="block";
if(document.getElementById("butSear")!=undefined)
document.getElementById("butSear").style.display="block";
}

document.onclick=function (e){
	var e=e||event;
	var e=e.target||e.srcElement;
	if ("网"==e.innerText.substring(0,1)&&calendar.calendarPad.style.display!="none")
	calendar.hide();
}
/************** 从这里开始 *********************/
this.setup=function(defaultDate){

calendar.addCalendarPad();
calendar.addCalendarBoard();
calendar.addCalendarPad1();
calendar.addCalendarBoard1();
calendar.setDefaultDate();
}
/************** 格式化时间 *********************/
this.formatTime = function(str) {
str = ("00"+str);
return str.substr(str.length-2);
}


calendar.setup();
} 
document.onmousedown = function(e){
//alert(document.documentElement.scrollTop);
var evt = e ? e : window.event;
var mx=evt.clientX+document.documentElement.scrollLeft;
var my=evt.clientY+document.documentElement.scrollTop;
if(my<oy||my>oy+206||mx<ox||mx>ox+412)
caldar.hide();
}

