
var g_date_select_cou = 0;
var g_date_array = new Array();

function Init_Date(date)
{
	date = date.replace(".","-"); 
	if(date.length < 8) return date;
	var ptr = date.search("-");
	if(ptr == -1) return date;
	
	var year = date.substr(0,4);
	var month = date.substr(5);
	var day;
	 
	ptr = month.search("-");
	if(ptr == -1) return date;
	day = month.substr(ptr + 1);
	month = month.substr(0,ptr);
	
	var init_date = year;
	if(month.length == 1)
		init_date += "0";
	init_date += month;

	if(day.length == 1)
		init_date += "0";
	init_date += day;
	
	//alert(year);
	//alert(month);
	//alert(day);
	
	return init_date;
}

function Date_Select(ctrl_id)
{
	var ctrls = document.getElementsByName(ctrl_id);
	if(ctrls == null)
	{
		alert('cant get by name ' + ctrl_id);
		 return;
	};
	
	//var ctrl = document.getElementById(ctrl_id);
	var ctrl = ctrls[0];
	if(ctrl == null)
	{
		alert('cant get ' + ctrl_id);
		 return;
	};
	var div_date_grid = null;
	var iframe_date_grid = null;
	var body = document.body;
	var html_str;
	var date_select_index = -1;
	var id,sid;
	var year;
	var month;
	var day;

	var init_date = Init_Date(ctrl.value);
	
	if(Time_Is_Date_Valid(init_date) == 0)
	{
		var curr_date = new Date();
		year = curr_date.getFullYear();
		month = curr_date.getMonth() + 1;
		day = curr_date.getDate();
	}
	else
	{
		year = Get_Year(init_date);
		month = Get_Month(init_date);
		day = Get_Day(init_date);
	}

	for(id = 0; id < g_date_array.length; id++)
	{
		if(ctrl == g_date_array[id])
			date_select_index = id;
	}

	if(date_select_index >= 0)
	{
		div_date_grid = document.getElementById("date_grid_" + date_select_index);
		iframe_date_grid = document.getElementById("iframe_date_grid_" + date_select_index);
	}
	else
	{
		date_select_index = g_date_select_cou++;
		g_date_array[date_select_index] = ctrl;
	}

	if(div_date_grid != null && iframe_date_grid != null && div_date_grid.style.display == "block")
	{
		div_date_grid.style.display = "none";
		iframe_date_grid.style.display = "none";
		return;
	}

	if(body == null)
	{
		 alert('body is null');
		 return;
	};

	if(iframe_date_grid == null)
	{
		 		 
		iframe_date_grid = document.createElement("iframe");
  	iframe_date_grid.id = "iframe_date_grid_" + date_select_index;
  	iframe_date_grid.name = "iframe_date_grid_" + date_select_index;
    iframe_date_grid.style.position = "absolute";
    iframe_date_grid.style.display = "block";
    iframe_date_grid.frameBorder = 0;
    iframe_date_grid.style.backgroundcolor="white";
    //iframe_date_grid.src = "about:";
    
    //alert("a1");
    
  	body.appendChild(iframe_date_grid);
  	
  	//alert("a2");
	}

	if(div_date_grid == null)
	{
		div_date_grid = document.createElement("div");

		div_date_grid.style.background = "#e0ecf9";
		div_date_grid.style.width = "200px";
		div_date_grid.style.height = "160px";
		div_date_grid.setAttribute("id","date_grid_" + date_select_index);
		div_date_grid.style.position="absolute";

		html_str = "<table border='0' cellpadding='0' cellspacing='0' width='200px' height='20px'>" +
								"<tr align='center'>" +
									"<td width='28px'><input type='button' value='&nbsp;&lt;&lt;&nbsp;' onclick=\"Reduce_One_Year('" + date_select_index + "')\" style='cursor:hand;font-family: Arial; font-size: 13px;text-align:left; border:none;margin-left: 2;'></td>" +
									"<td width='28px'><input type='button' value='&nbsp;&lt;&nbsp;' onclick=\"Reduce_One_Month('" + date_select_index + "')\" style='cursor:hand;font-family: Arial; font-size: 13px;text-align:left; border:none;margin-left: 2;'></td>" +
									"<td width='88px' style=' font-family: Arial; font-size: 13px;'><span id='year_" + date_select_index + "'></span>\u5e74<span id='month_" + date_select_index + "'></span>\u6708<span style='display:none;' id='day_" + date_select_index + "'></span></td>" +
									"<td width='28px'><input type='button' value='&nbsp;&gt;&nbsp;' onclick=\"Add_One_Month('" + date_select_index + "')\" style='cursor:hand;font-family: Arial; font-size: 13px;text-align:left; border:none;margin-left: 2;'></td>" +
									"<td width='28px'><input type='button' value='&nbsp;&gt;&gt;&nbsp;' onclick=\"Add_One_Year('" + date_select_index + "')\" style='cursor:hand;font-family: Arial; font-size: 13px;text-align:left; border:none;margin-left: 2;'></td>" +
								"</tr>" +
							"</table>";
    //Äê\u5e74 ÔÂ\u6708
    //ÈÕ\u65e5 Ò»\u4e00 ¶þ\u4e8c Èý\u4e09 ËÄ\u56db Îå\u4e94 Áù\u516d
		html_str += "<table border='0' cellpadding='0' cellspacing='0' width='200px' height='140px' style='align:center;font-family:Arial;font-size:13px;'>" +
								"<tr align='center'>" +
									"<td width='30px' style='color:#FF0000;'>\u65e5</td>" +
					        "<td width='28px'>Ò»\u4e00</td>" +
					        "<td width='28px'>¶þ\u4e8c</td>" +
					        "<td width='28px'>Èý\u4e09</td>" +
					        "<td width='28px'>ËÄ\u56db</td>" +
					        "<td width='28px'>Îå\u4e94</td>" +
					        "<td width='30px' style='color:#FF0000;'>Áù\u516d</td>" +
								"</tr>";

		for(id = 0; id < 6; id++)
		{
			html_str += "<tr align='center'>";
			html_str += "<td name='td_day_" + date_select_index + "' id='td_day_" + date_select_index + "' style='color:#FF0000;' onclick=\"Set_Day_End('" + date_select_index + "',this.innerText)\">&nbsp;</td>";
			for(sid = 0; sid < 6; sid++)
				html_str += "<td name='td_day_" + date_select_index + "' id='td_day_" + date_select_index + "' onclick=\"Set_Day_End('" + date_select_index + "',this.innerText)\">&nbsp;</td>";
			html_str += "<td name'td_day_" + date_select_index + "' id='td_day_" + date_select_index + "' style='color:#FF0000;' onclick=\"Set_Day_End('" + date_select_index + "',this.innerText)\">&nbsp;</td>";
			html_str += "</tr>";
		}

		html_str += "</table>";

		//alert(html_str);
		div_date_grid.innerHTML = html_str;
    body.appendChild(div_date_grid);
	}

	Set_Calendar(date_select_index,year,month,day);
	var pos = Get_Position(ctrl);
	
	div_date_grid.style.left = pos[0] + "px";
	div_date_grid.style.top = pos[1] + ctrl.offsetHeight + "px";
	iframe_date_grid.style.left = div_date_grid.style.left;
  iframe_date_grid.style.top = div_date_grid.style.top;
  iframe_date_grid.style.height = div_date_grid.style.height;
  iframe_date_grid.style.width = div_date_grid.style.width;

	div_date_grid.style.display = "block";
	iframe_date_grid.style.display = "block";

}


function Get_Month_Days(year,month)
{
	var days_collect = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

  var days = days_collect[month - 1];

  if(month == 2 && (year%4) == 0) days++;

  return days;
}

function Set_Calendar(code_id,year,month,day)
{
	//alert(code_id);
	//alert(year);
	//alert(month);
	//alert(day);
	
	var month_init = new Date(year,month - 1,1);
  var month_init_week = month_init.getDay();
  var month_days = Get_Month_Days(year,month);
  var id;

  if(day > month_days) day = month_days;

  var td_day = document.getElementsByName("td_day_" + code_id);
  
  for(id = 0; id < td_day.length; id++)
  {
  	td_day[id].innerText = "";
  	td_day[id].style.cursor = "default";

  	td_day[id].style.backgroundColor = "";
  }

	//alert(month_days);

	for(id = 0; id < month_days; id++)
  {
   	td_day[month_init_week + id].innerText = id + 1;
   	td_day[month_init_week + id].style.cursor = "hand";
   	if(id == day - 1)
   		td_day[month_init_week + id].style.backgroundColor = "blue";

  }

	var show_year = document.getElementById("year_" + code_id);
	if(show_year != null)
		show_year.innerText = year;

  var show_month = document.getElementById("month_" + code_id);
  if(show_month != null)
		show_month.innerText = month;

  var show_day = document.getElementById("day_" + code_id);
  if(show_day != null)
		show_day.innerText = day;

	Set_Edit_Day(code_id,day);
}


function Add_One_Year(code_id)
{
	var show_year = document.getElementById("year_" + code_id);
  var show_month = document.getElementById("month_" + code_id);
  var show_day = document.getElementById("day_" + code_id);

  Set_Calendar(code_id,parseInt(show_year.innerText) + 1,show_month.innerText,show_day.innerText);
}

function Reduce_One_Year(code_id)
{
	var show_year = document.getElementById("year_" + code_id);
  var show_month = document.getElementById("month_" + code_id);
  var show_day = document.getElementById("day_" + code_id);

  Set_Calendar(code_id,parseInt(show_year.innerText) - 1,show_month.innerText,show_day.innerText);
}

function Add_One_Month(code_id)
{
	var show_year = document.getElementById("year_" + code_id);
  var show_month = document.getElementById("month_" + code_id);
  var show_day = document.getElementById("day_" + code_id);

  var new_month = parseInt(show_month.innerText) + 1;
  var new_year = parseInt(show_year.innerText);

  if(new_month > 12)
  {
  	new_month = 1;
  	new_year++;
  }

  Set_Calendar(code_id,new_year,new_month,show_day.innerText);
}

function Reduce_One_Month(code_id)
{
	var show_year = document.getElementById("year_" + code_id);
  var show_month = document.getElementById("month_" + code_id);
  var show_day = document.getElementById("day_" + code_id);

  var new_month = parseInt(show_month.innerText) - 1;
  var new_year = parseInt(show_year.innerText);

  if(new_month == 0)
  {
  	new_month = 12;
  	new_year--;
  }

  Set_Calendar(code_id,new_year,new_month,show_day.innerText);
}

function Set_Edit_Day(code_id,day)
{
	var edit = g_date_array[code_id];
	var show_year = document.getElementById("year_" + code_id);
  var show_month = document.getElementById("month_" + code_id);

	edit.value = show_year.innerText + "-";
	if(show_month.innerText.length == 1)
		edit.value += "0";
	edit.value += show_month.innerText + "-";

	if(day < 10)
		edit.value += "0";
	edit.value += day;
	//alert(edit.value);
}

function Set_Day_End(code_id,day)
{
	if(day == "") return;

	var div_date_grid = document.getElementById("date_grid_" + code_id);
	var iframe_date_grid = document.getElementById("iframe_date_grid_" + code_id);

	Set_Edit_Day(code_id,day);

	div_date_grid.style.display = "none";
	iframe_date_grid.style.display = "none";
}


function Get_Ctrl_Left(ctrl)
{
	var left = 0;
 	var parent = ctrl;

	while (parent != null && parent != document.body)
	{
		if(parent.tagName.toLowerCase() == "form")
		{
			parent = parent.parentElement;
			continue;
		}
		
 		left += parent.offsetLeft;

 		parseInt(parent.currentStyle.borderLeftWidth) > 0 ? left += parseInt(parent.currentStyle.borderLeftWidth):"";

 		parent = parent.parentElement;
 	}

	return left;
}

function Get_Position(ctrl, top_parent)
{
	var _top_parent=(top_parent)?top_parent:window.document.body;
	var x = ctrl.offsetLeft;
	var y = ctrl.offsetTop;
	var tmp_ctrl = ctrl.offsetParent;
	while ((tmp_ctrl!=_top_parent) && tmp_ctrl)
	{
		x += tmp_ctrl.offsetLeft + tmp_ctrl.clientLeft - tmp_ctrl.scrollLeft;
		y += tmp_ctrl.offsetTop + tmp_ctrl.clientTop - tmp_ctrl.scrollTop;
		tmp_ctrl = tmp_ctrl.offsetParent;
	}
	return ([x, y]);
}


function Get_Ctrl_Top(ctrl)
{
	var top = 0;
 	var parent = ctrl;

	while (parent != null && parent != document.body)
	{
		if(parent.tagName.toLowerCase() == "form")
		{
			parent = parent.parentElement;
			continue;
		}
		
 		top += parent.offsetTop;
		
		//alert(parent.tagName);
		//alert(parent.offsetTop);
 		parseInt(parent.currentStyle.borderTopWidth) > 0 ? top += parseInt(parent.currentStyle.borderTopWidth):"";

 		parent = parent.parentElement;
 	}
	//alert("top=" + top);
	return top;
}

function Time_Is_Date_Valid(str_date)
{
	if(str_date.length != 8) return 0;

	var year = str_date.substr(0,4);
	
	if(isNaN(parseInt(year)) || parseInt(year) < 1000) return 0;
	
	var month;
	if(str_date.substr(4,1) == "0")
		month = str_date.substr(5,1);
	else
		month = str_date.substr(4,2);

	if(isNaN(parseInt(month)) || parseInt(month) < 1 || parseInt(month) > 12) return 0;

	var month_days = Get_Month_Days(year,month);
	var day;
	if(str_date.substr(6,1) == "0")
		day = str_date.substr(7,1);
	else
		day = str_date.substr(6,2);

	if(isNaN(parseInt(day)) || parseInt(day) < 1 || parseInt(day) > month_days) return 0;

	return 1;
}

function Get_Year(str_date)
{
	return parseInt(str_date.substr(0,4));
}

function Get_Month(str_date)
{
	if(str_date.substr(4,1) == "0")
		return parseInt(str_date.substr(5,1));
	else
		return parseInt(str_date.substr(4,2));
}

function Get_Day(str_date)
{
	if(str_date.substr(6,1) == "0")
		return parseInt(str_date.substr(7,1));
	else
		return parseInt(str_date.substr(6,2));
}
