﻿// JavaScript Document
function Gid(id){
	return document.getElementById(id);
}
function Calendar(obj,fn){
	var BegYear = 1990;
	var EndYear = 2049;
	this.init = function(b){
		this.begin = (!b)?0:b;
	}
	this.SetDate = function(y,m,d){
		this.today = new Date();
		this.y = (!y)?this.today.getFullYear():y;
		this.m = (!m)?this.today.getMonth()+1:m;
		this.d = (!d)?this.today.getDate():d;
	}
	this.ChkDate = function(){
		var bYear = this.y / 4;
		switch(this.m){
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				maxDay = 31;
				break;
			case 2:
				if(bYear==0)
					maxDay = 28;
				else
					maxDay = 29;
				break;
			default:
				maxDay = 30;
				break;
		}
		this.maxDay = maxDay;
	}
		
	this.LoadCale = function () {
		s = '';
		s += '<div id="Calendar"><table class="tableborder" cellspacing="0" cellpadding="0" width="100%" style="text-align: center">';
		s += '<tr align="center" class="cal_header"><td class="cal_header"><a href="javascript:;" title="上一月" onclick="'+obj+'.PrevMonth();"><<</a></td><td colspan="5" style="text-align: center" class="header"><a href="javascript:;" onclick="return false;'+obj+'.ShowSele(event,\'calendar_year\');'+obj+'.HideSele(event,\'calendar_month\');return false;" title="" id="year">'+this.y+'</a>  -  <a id="month" title="" href="javascript:;" onclick="return false;'+obj+'.ShowSele(event,\'calendar_month\');'+obj+'.HideSele(event,\'calendar_year\');return false;"></a></td><td class="header"><a href="javascript:;" title="下一月" onclick="'+obj+'.NextMonth();">>></a></td></tr>';
		s += '<tr class="category"><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>';
		for(var i = 0; i < 6; i++) {
			s += '<tr class="altbg2">';
			for(var j = 1; j <= 7; j++)
				s += "<td><a id='d" + (i * 7 + j) + "' "+this.chkfn()+"  href='javascript:;' style='cursor:pointer;'>0</a></td>";
			s += "</tr>";
		}
		s += '</table></div>';
//		s += '<div id="calendar_year"><div class="col">';
//		for(var k = BegYear; k <= EndYear; k++) {
//			s += k != BegYear && k % 10 == 0 ? '</div><div class="col">' : '';
//			s += '<a href="javascript:;" onclick="'+obj+'.ChgYear('+k+');'+obj+'.HideSele(event,\'calendar_year\');"><span' + (this.y == k ? ' class="today"' : '') + ' id="calendar_year_' + k + '">' + k + '</span></a><br />';
//		}
//		s += '</div><div class="col" style="cursor:pointer;" onclick="this.parentNode.style.display=\'none\';">关闭</div></div>';
//		s += '<div id="calendar_month">';
//		for(var k = 1; k <= 12; k++) {
//			s += '<a href="javascript:;" onclick="'+obj+'.ChgMonth('+k+');'+obj+'.HideSele(event,\'calendar_month\');"><span' + (this.m == k ? ' class="today"' : '') + ' id="calendar_month_' + k + '">' + k + ( k < 10 ? ' ' : '') + ' 月</span></a><br />';
//		}
//		s += '<div style="cursor:pointer;" onClick="this.parentNode.style.display=\'none\';">关闭</div></div>';
		document.write(s);
	}
	this.chkfn = function(){
		if (fn){
			return "onclick='"+fn+"(this);'";
		}
		else
			return "";
	}
	this.ChgDate = function(y,m,d){
		this.SetDate(y,m,d);
		this.ChkDate();
		var n_Date = new Date(y,m-1,1);
		var n_Date_2 = new Date();
		var n_y_2 = n_Date_2.getFullYear();
		var n_m_2 = n_Date_2.getMonth()+1;
		var n_d_2 = n_Date_2.getDate();
		var n_d = n_Date.getDay();
		
		Gid('year').innerHTML = y;
		Gid('month').innerHTML = m;
		for (var j=1; j<=42; j++){
			Gid('d'+j).innerHTML = " ";
			Gid('d'+j).className = "";
		}
		for (var i=1; i<=this.maxDay; i++ ){
			Gid('d'+(n_d+i)).innerHTML = i;
			if (n_d_2+n_d == i && n_y_2 == y && n_m_2 == m){
				Gid('d'+(n_d_2+n_d)).className = "month";
			}
		}
	}
	this.SetColor = function(n){
		this.SetDate();
		var y = this.y;
		var m = this.m;
		var d = this.d;
		var n_Date = new Date(y,m-1,1);
		var n_d = n_Date.getDay();
		var s_d = this.today.getDay();			// 这周过了几天
		var begin = this.begin%7-1;
		for (var j=1; j<=42; j++){
			Gid('d'+j).className = "";
		}
		this.ChgDate(y,m,d);
		if (n == 0){
			Gid('d'+(n_d+d)).className = "month2";
		}
		if (n == 1){
			
			for (var i=1;i<=4;i++){
				Gid('d'+(i+d+n_d-s_d+3)).className = "month2";
			}
		}
		if (n == 2){
			for (var i=1;i<=7;i++){
				Gid('d'+(i+d+n_d-s_d)).className = "month2";
			}
		}
        if (n == 3){
			for (var i=1;i<=7;i++){
				Gid('d'+(i+d+n_d-s_d+7)).className = "month2";
			}
		}
		if (n == 4){
			for (var i=d;i<=this.maxDay;i++){
				Gid('d'+(i+n_d)).className = "month2";
			}
		}
	}
	this.ChgYear = function(y){
		this.y = y;
		this.ChgDate(this.y,this.m,this.d);
	}
	this.ChgMonth = function(m){
		this.m = m;
		this.ChgDate(this.y,this.m,this.d);
	}
	this.PrevMonth = function(){
		this.m = this.m-1;
		if (this.m == 0){
			this.y = this.y-1;
			this.m = 12;
		}
		this.ChgDate(this.y,this.m,this.d);
	}
	this.NextMonth = function(){
		this.m = this.m+1;
		if (this.m == 13){
			this.y = this.y+1;
			this.m = 1;
		}
		this.ChgDate(this.y,this.m,this.d);
	}
	
	this.GetDate = function(){
		return this.y +"-"+ this.m +"-"+ this.d;
	}
	this.ShowCale = function(){
		//this.init();
		this.LoadCale();
		this.ChgDate(this.y,this.m,this.d);
	}

	this.ShowSele = function(e,obj){
		m = this.getPos(e);
		Gid(obj).style.display = "block";
		Gid(obj).style.position = "absolute";
		Gid(obj).style.left = (m.x) + "px";
		Gid(obj).style.top = 10 + "px";
		
	}
	this.HideSele = function (e,obj){
		Gid(obj).style.display = "none";
	}
	
	this.getPos = function(event) {
		var x = event.pageX || (event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft)) || 0;
		var y = event.pageY || (event.clientY + (document.documentElement.scrollTop || document.body.scrollTop)) || 0;
		return {x:x, y:y};
	}
}

