var g_year = 0; 
var g_month = 0;
var g_day = 0; 
var g_eC = null;
var g_eCV = ""; 
var dF = document.all.CalFrame;
var wF = window.frames.CalFrame;

function ShowCalendar(eD,eD1,eP)
{
	if(wF.g_fCalLoaded == null || wF.g_fCalLoaded == false) 
	{
		alert("不能显示日历，请刷新页面。");
		return;
	}
	dmin=new Date();
	dmax=new Date();
	dmin.setYear(dmin.getFullYear() - 5);
	dmax.setYear(dmax.getFullYear() + 5);
	wF.SetMinMax(dmin,dmax);
	if (dF.style.display == "block" && eD == g_eC)
		dF.style.display = "none";
	else
	{  
		if(GetInputDate(eD.value)) 
		{
			wF.SetInputDate(g_day,g_month,g_year);
			wF.SetDate(g_day,g_month,g_year);
		}
		else if(null != eD1 && GetInputDate(eD1.value))
		{
			wF.SetInputDate(g_day,g_month,g_year);
			wF.SetDate(g_day,g_month,g_year);
		}
		else
		{
			var dt = new Date();
			wF.SetInputDate(-1,-1,-1);
			wF.SetDate(dt.getDate(),dt.getMonth()+1,dt.getFullYear());
		}	
		dF.style.left = eP.offsetLeft;
		var eT = eP.offsetTop;
		var eH = eP.offsetHeight;
		var dH = dF.style.pixelHeight;
		var sT = document.body.scrollTop;
		if(eT-dH >= sT && eT+eH+dH > document.body.clientHeight+sT)
			dF.style.top = eT-dH;
		else
			dF.style.top = eT+eH;
		dF.style.display="block";
		g_eC = eD;
	}  
}

function SetDate(d,m,y)
{
	g_eC.focus();
	g_eC.value = y+"-"+m+"-"+d;
	g_eCV = g_eC.value;
}

function atoi(s)
{
	var t = 0;
	for(var i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if(!isdigit(c)) 
			return t;
		else 
			t = t*10 + (c-'0');
	}
	return t;
}

function isdigit(c)
{
	return(c >= '0' && c <= '9');
}

function GetInputDate(t)
{
	var l = t.length;
	if(0 == l) return false;
	var cSp = '\0';
	var sSp1 = "";
	var sSp2 = "";
	for(var i = 0; i < t.length; i++)
	{
		var c = t.charAt(i);
		if(c == ' ' || isdigit(c)) 
			continue;
		else if(cSp == '\0' && (c == '/' || c == '-' || c == '.'))
		{
			cSp = c;
			sSp1 = t.substring(i+1,l);
		}
		else if(c == cSp) 
			sSp2 = t.substring(i+1,l);
		else if(c != cSp) 
			return false;
	}
	if(0 == sSp1.length) return false;
	var m;	
	var d;	
	var y; 
	if(0 == sSp2.length) return false;
	y = atoi(t);
	m = atoi(sSp1);
	d = atoi(sSp2);
	if(y < 100) y = 2000+y;
	if(y < 2000 || y > 2050 || m < 1 || m > 12) return false;
	if(d < 1 || d > wF.GetMonthCount(m,y)) return false;
	g_month = m;
	g_day = d;
	g_year = y;
	return true;
}

