// JavaScript Document
function yuchantest()
{
if (health.year.value=="" || health.month.value=="" || health.day.value =="")
{
alert("请填写完整年月日!");
return false;
}
if (health.year.value>2050 || health.year.value<1990)
{
alert("请填写正确年份!");
health.year.focus();
return false;
}
if (health.month.value>12 || health.month.value<1)
{
alert("请填写正确月份!");
health.month.focus();
return false;
}
if (health.day.value>31 || health.day.value<1)
{
alert("请填写正确日子!");
health.day.focus();
return false;
}
if (!isDate(health.year.value ,health.month.value ,health.day.value))
{
alert("年月日组合有错,请重新填写!");
return false;
}
function isDate (year, month, day)
{
// month argument must be in the range 1 - 12
month = month - 1;  // javascript month range : 0- 11
var tempDate = new Date(year,month,day);
if ( (year2k(tempDate.getYear()) == year) && (month == tempDate.getMonth()) && (day == tempDate.getDate()) )
{    
return true;
}
else
{
return false;
}
}
function year2k(d) 
{ 
return (d < 1000) ? d + 1900 : d;
}        
if (health.mweek.value==3)
{
var stime = health.month.value + "/" + health.day.value + "/" + health.year.value;
var stime2 = Date.parse(stime) + 39*7*24*3600*1000;
var yuchan2 = new Date(stime2);
var yuchan4 = new String(yuchan2);
var yuchan3 = yuchan4.split(" ");
var yuchanmonth = "";
if (yuchan3[1]=="Jan") yuchanmonth="1";
if (yuchan3[1]=="Feb") yuchanmonth="2";
if (yuchan3[1]=="Mar") yuchanmonth="3";
if (yuchan3[1]=="Apr") yuchanmonth="4";
if (yuchan3[1]=="May") yuchanmonth="5";
if (yuchan3[1]=="Jun") yuchanmonth="6";
if (yuchan3[1]=="Jul") yuchanmonth="7";
if (yuchan3[1]=="Aug") yuchanmonth="8";
if (yuchan3[1]=="Sep") yuchanmonth="9";
if (yuchan3[1]=="Oct") yuchanmonth="10";
if (yuchan3[1]=="Nov") yuchanmonth="11";
if (yuchan3[1]=="Dec") yuchanmonth="12";
var yuchan = yuchan3[5] + "-" + yuchanmonth + "-" + yuchan3[2];
var nowt = new Date();
var nowtime = nowt.getTime();
var chatime = nowtime - Date.parse(stime);
var chaweek = Math.floor((chatime)/(1000*60*60*24*7));
if (chaweek<0) chaweek=0;
if (chaweek>41) alert("你的预产期已过!");		
document.health.yuchan.value = yuchan;
document.health.yuchan2.value = chaweek;
return false;
}
if (health.mweek.value==4)
{
var stime = health.month.value + "/" + health.day.value + "/" + health.year.value;
var stime2 = Date.parse(stime) + 40*7*24*3600*1000;
var yuchan2 = new Date(stime2);
var yuchan4 = new String(yuchan2);
var yuchan3 = yuchan4.split(" ");
var yuchanmonth = "";
if (yuchan3[1]=="Jan") yuchanmonth="1";
if (yuchan3[1]=="Feb") yuchanmonth="2";
if (yuchan3[1]=="Mar") yuchanmonth="3";
if (yuchan3[1]=="Apr") yuchanmonth="4";
if (yuchan3[1]=="May") yuchanmonth="5";
if (yuchan3[1]=="Jun") yuchanmonth="6";
if (yuchan3[1]=="Jul") yuchanmonth="7";
if (yuchan3[1]=="Aug") yuchanmonth="8";
if (yuchan3[1]=="Sep") yuchanmonth="9";
if (yuchan3[1]=="Oct") yuchanmonth="10";
if (yuchan3[1]=="Nov") yuchanmonth="11";
if (yuchan3[1]=="Dec") yuchanmonth="12";
var yuchan = yuchan3[5] + "-" + yuchanmonth + "-" + yuchan3[2];
var nowt = new Date();
var nowtime = nowt.getTime();
var chatime = nowtime - Date.parse(stime);

var chaweek = Math.floor((chatime)/(1000*60*60*24*7));
if (chaweek<0) chaweek=0;
if (chaweek>42) alert("你的预产期已过!");		
document.health.yuchan.value = yuchan;
document.health.yuchan2.value = chaweek;
return false;
}
if (health.mweek.value==5)
{
var stime = health.month.value + "/" + health.day.value + "/" + health.year.value;
var stime2 = Date.parse(stime) + 41*7*24*3600*1000;
var yuchan2 = new Date(stime2);
var yuchan4 = new String(yuchan2);
var yuchan3 = yuchan4.split(" ");
var yuchanmonth = "";
if (yuchan3[1]=="Jan") yuchanmonth="1";
if (yuchan3[1]=="Feb") yuchanmonth="2";
if (yuchan3[1]=="Mar") yuchanmonth="3";
if (yuchan3[1]=="Apr") yuchanmonth="4";
if (yuchan3[1]=="May") yuchanmonth="5";
if (yuchan3[1]=="Jun") yuchanmonth="6";
if (yuchan3[1]=="Jul") yuchanmonth="7";
if (yuchan3[1]=="Aug") yuchanmonth="8";
if (yuchan3[1]=="Sep") yuchanmonth="9";
if (yuchan3[1]=="Oct") yuchanmonth="10";
if (yuchan3[1]=="Nov") yuchanmonth="11";
if (yuchan3[1]=="Dec") yuchanmonth="12";
var yuchan = yuchan3[5] + "-" + yuchanmonth + "-" + yuchan3[2];
var nowt = new Date();
var nowtime = nowt.getTime();
var chatime = nowtime - Date.parse(stime);
var chaweek = Math.floor((chatime)/(1000*60*60*24*7));
if (chaweek<0) chaweek=0;
if (chaweek>43) alert("你的预产期已过!");
document.health.yuchan.value = yuchan;
document.health.yuchan2.value = chaweek;
return false;
}	
}
/*体重自测*/
var imagenumber = 3 ;
var randomnumber = Math.random() ;
var rand = Math.round( (imagenumber-1) * randomnumber) + 1 ;
urls = new Array;
images = new Array;
alts = new Array;
var url = urls[rand];
var image = images[rand];
var alt = alts[rand];
function lookme(form) {
var bmi;
  if(! checkform(form)) return false;
  bmi = Math.round(form.weight.value*10000/eval(form.height.value*form.height.value));
  if (bmi >40) {
     form.nowstat.value="啊,你还能买到衣服吗?\n你太、太...太胖了";
  }else if (bmi >30) {
     form.nowstat.value="哇！你好胖啊!必须开始减肥了,听我的没错";
  }else if (bmi >27) {
     form.nowstat.value="哎呀！你可是比较胖啊，赶快开始减肥计划吧！";
  }else if (bmi >22) {
     form.nowstat.value="小心喔!稍胖，少吃点可以吗?\n还要多多运动啊!:)";
  }else if (bmi >=21) {
     form.nowstat.value="我好羡慕你啊,你这可是魔鬼身材啊!! :))";
  }else if (bmi >=18) {
     form.nowstat.value="瘦了一点点，你应该多吃点东西啊!";
  }else if (bmi >=16) {
     form.nowstat.value="你一定是受到了虐待，快点大量吃东西吧!";
  }else {
     form.nowstat.value="哇塞!前胸贴后背,你怎么像个电线杆子\n一点肉都没有,快找大夫看看吧!!";
  }
  return true;
}
function checkform(form){
  if(form.weight.value == null || form.weight.value.length ==0 ||
     form.height.value == null || form.height.value.length ==0) {
    alert("你以为我真的是神仙吗?你什么都不告诉我,我怎么给你测啊!!!");
    return false;
  }
  if(form.weight.value <=0) {
    alert("你将创下体重最轻的吉尼斯世界记录,当心地心引力对你不起作用啊.");
    return false;
  }
  if(form.weight.value >500) {
    alert("你不用测了,你的体重已经把我的秤压坏了.");
    return false;
  }
  if(form.height.value <=0) {
    alert("你不至于这么矮吧,你怎么比蚂蚁还小呢?");
    return false;
  }
  if(form.height.value >=300) {
    alert("喔!!!!你好伟大啊!!!!\n替我向上帝问好");
    return false;
  }
  return true;
}
/*安全期自测*/
if (!document.layers&&!document.all)
event="test"
function showtip2(current,e,text,index){
	if (document.all&&document.readyState=="complete"){
		eval("var tooltip=document.all.tooltip" + index + ";")
		//tooltip.innerHTML='<marquee style="border:1px solid black">'+text+'</marquee>'
		tooltip.innerHTML='' + text + '</TABLE>'
		tooltip.style.pixelLeft=event.clientX+document.body.scrollLeft+10
		tooltip.style.pixelTop=event.clientY+document.body.scrollTop+10
		tooltip.style.visibility="visible"
	}
	else if (document.layers){
		eval("var tooltip=document.tooltip" + index + ";")
		eval("var nstip=document.tooltip" + index + ".document.nstip" + index + ";")
		nstip.document.write('<b>'+text+'</b>')
		nstip.document.close()
		nstip.left=0
		//currentscroll=setInterval("scrolltip(" + index + ")",100)
		tooltip.left=e.pageX+10
		tooltip.top=e.pageY+10
		tooltip.visibility="show"
	}
}

function hidetip2(index){
	if (document.all)
		eval("document.all.tooltip" + index + ".style.visibility='hidden';");
	else if (document.layers){
		//clearInterval(currentscroll)
		eval("document.tooltip" + index + ".visibility='hidden';")
	}
}

function scrolltip(index){
	eval("var nstip=document.tooltip" + index + ".document.nstip" + index + ";")
	if (nstip.left>=-nstip.document.width)
		nstip.left-=5
	else
		nstip.left=150
}

function montharr(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11)
{
	this[0] = m0;
	this[1] = m1;
	this[2] = m2;
	this[3] = m3;
	this[4] = m4;
	this[5] = m5;
	this[6] = m6;
	this[7] = m7;
	this[8] = m8;
	this[9] = m9;
	this[10] = m10;
	this[11] = m11;
}

var CalendarOuterHTML = '';
var Today = new Date();
var DaysPerMonth = 0;

//Get the number of day in some month
function GetDayPerMonth(year,month)
{
	var monthDays = new montharr(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
		monthDays[1] = 29;
	DaysPerMonth = monthDays[month];	
}

function GetCalendarOuterHTML(CalendarIndex,HaveLayer)
{
	CalendarOuterHTML = '';		
	var thisYear = frmInput.txtYear.value;
	var thisMonth = parseInt(frmInput.txtMonth.value) +  CalendarIndex - 1 ;

	//title of the calendar
	CalendarOuterHTML = CalendarOuterHTML + "<table border=0 cellspacing=0 cellpadding=3 id=Calendar" + CalendarIndex + ">";
	CalendarOuterHTML = CalendarOuterHTML + "<TR bgcolor=#FFCCCC><TD colspan=7 class=p2 align=center>";
	CalendarOuterHTML = CalendarOuterHTML + thisYear+' 年 ';
	CalendarOuterHTML = CalendarOuterHTML + thisMonth +' 月';
	CalendarOuterHTML = CalendarOuterHTML + "<TR bgcolor=#DADADA><TD width=18 class=p2>日<TD width=18 class=p2>一<TD width=18 class=p2>二<TD width=18 class=p2>三<TD width=18 class=p2>四<TD width=18 class=p2>五<TD width=18 class=p2>六";
	CalendarOuterHTML = CalendarOuterHTML + "<TR bgcolor=#FFFFFF>";

	//Get the day of the first Day
	var firstDay = new Date(Date.UTC(frmInput.txtYear.value,(parseInt(frmInput.txtMonth.value)-2+CalendarIndex),1));
	testMe = firstDay.getDate();
	if (testMe == 2)
		firstDay.setDate(0); 
	startDay = firstDay.getDay();
		
    //display empty cells  before the first day of the month                
    column = 0;
	for (i=0; i<startDay; i++)
	{
		CalendarOuterHTML = CalendarOuterHTML + "<TD width=18 class=p2>";
		column++;
	}
	
	//display the grids in the calendar
	var Lastday = new Date(Date.UTC(frmInput.txtYear.value,(parseInt(frmInput.txtMonth.value)-1),frmInput.txtDay.value))	
	GetDayPerMonth(thisYear,thisMonth-1)	
	
	for (i=1; i<=DaysPerMonth; i++)
	{
		CalendarOuterHTML = CalendarOuterHTML + "<TD width=18 class=p2>";
		var color = "blue";		//default color without layer displayed
		
		//Get layer HTML
		if (HaveLayer)
		{		
			var ThisDay = new Date(Date.UTC(thisYear,thisMonth-1,i))
		
			var msPerDay = 24 * 60 * 60 * 1000 ;
			var mensesCyc = parseInt(frmInput.txtMinMensesCyc.value);		//Min menses Cycle
			var msDiff = ThisDay.getTime() - Lastday.getTime();
			dayDiff = Math.floor(msDiff / msPerDay);						//get the days between thisday and lastday
			dayRemainder =	(dayDiff % mensesCyc + mensesCyc) % mensesCyc;
			//if (i<2)	{alert(ThisDay.toLocaleString()); alert(Lastday.toLocaleString()); alert(dayDiff);alert(dayRemainder);}
	
			var tooltips ="";		//content of layer
		
			if (dayRemainder>=0 && dayRemainder<=4)
			{	color = "#FF9900";
				tooltips = "这是月经期，要注意经期卫生，当然也要“节欲”，避免性事哦！"
			}
			if (dayRemainder>=5 && dayRemainder<=(mensesCyc-20))
			{	color = "#009933";
				tooltips = "这是安全期，性事一般不会受孕，您放心吧！";	
			}
			if (dayRemainder>=(mensesCyc-19) && dayRemainder<=(mensesCyc-10))
			{	color = "#FF3300";
				tooltips = "这是危险期，亦称排卵期，性事受孕可能性大，千万要注意哦！";
			}
			if (dayRemainder>=(mensesCyc-9) && dayRemainder<=(mensesCyc-1))	
			{	color = "#009933";
				tooltips = "这是安全期，性事一般不会受孕，您放心吧！";	
			}
		
			iLayerIndex = 40*CalendarIndex + i ;		//index of layer

			tooltips = "<TABLE border=0 cellPadding=2 cellSpacing=1 width=100% align=center bgcolor=#008080>" +
						"<TR><TD style=BACKGROUND-COLOR:white;COLOR:" + color + ";FONT-FAMILY:宋体;FONT-SIZE:9pt;TEXT-DECORATION:none> " +
						tooltips + "</TD></TR></TABLE>";
						
			CalendarOuterHTML = CalendarOuterHTML + "<div id=\"tooltip" + iLayerIndex + "\" style=\"position:absolute;visibility:hidden;clip:rect(0 150 150 0);width:150px;background-color:seashell\">";
			CalendarOuterHTML = CalendarOuterHTML + "<layer name=\"nstip" + iLayerIndex + "\" width=\"1000px\" bgColor=\"seashell\" height=\"500px\"></layer></div>";
			CalendarOuterHTML = CalendarOuterHTML + "<a href=\"#\" onMouseOver=\"showtip2(this,event,'" + tooltips + "'," + iLayerIndex + ")\" onMouseOut=\"hidetip2(" + iLayerIndex + ")\">";
		}
		
		CalendarOuterHTML = CalendarOuterHTML + "<FONT COLOR=\"" + color + "\">" + i + "</FONT>";
		
		column++;
		
		if (column == 7)
		{
			CalendarOuterHTML = CalendarOuterHTML + "<TR bgcolor=#FFFFFF>"; 
			column = 0;
		}
	}
	
	//display empty cells  after the final day of the month    
	var FinalDay = new Date(Date.UTC(frmInput.txtYear.value,(parseInt(frmInput.txtMonth.value)-2+CalendarIndex),DaysPerMonth));
	testMe = FinalDay.getDate();
	if (testMe == 2)
		FinalDay.setDate(0); 
	EndDay = FinalDay.getDay();
	for (i=EndDay; i<6; i++)
	{
		CalendarOuterHTML = CalendarOuterHTML + "<TD width=18 class=p2>";
	}
	
	CalendarOuterHTML = CalendarOuterHTML + "</TABLE>";
}

//to check input errors and display both calendars
function DisplayCalendar()
{
	//check whether the date is legal
	if (frmInput.txtYear.value<1900||isNaN(frmInput.txtYear.value))
	{
		alert("请输入合法年份！")
		frmInput.txtYear.focus();
		return false;
	}
	if (isNaN(frmInput.txtMonth.value) || frmInput.txtMonth.value<1 || frmInput.txtMonth.value>12)
	{
		alert("请输入合法月份！")
		frmInput.txtMonth.focus();
		return false;
	}
	GetDayPerMonth(frmInput.txtYear.value,frmInput.txtMonth.value-1)
	if (isNaN(frmInput.txtDay.value) || frmInput.txtDay.value<1 || frmInput.txtDay.value>DaysPerMonth)
	{
		alert("请输入合法日期！")
		frmInput.txtDay.focus();
		return false;
	}
	var Lastday = new Date(Date.UTC(frmInput.txtYear.value,(parseInt(frmInput.txtMonth.value)-1),frmInput.txtDay.value))	
	if ((Today.getTime() - Lastday.getTime())<0)
	{
		alert("请输入正确的上次月经时间(不能早于当前时间)！")
		frmInput.txtYear.focus();
		return false;
	}	
	//check input
	if(isNaN(frmInput.txtMinMensesCyc.value))
	{
		alert("请输入数字！")
		frmInput.txtMinMensesCyc.focus();
		return false;
	}
	if(parseInt(frmInput.txtMinMensesCyc.value)>40 || parseInt(frmInput.txtMinMensesCyc.value)<24 )
	{
		alert("您输入的最短月经周期与标准月经周期相差太大，程序无法测试，请仔细核对。\n\n如输入确无问题请咨询医生！")
		frmInput.txtMinMensesCyc.focus();
		return false;
	}
	
	if(isNaN(frmInput.txtMaxMensesCyc.value) || parseInt(frmInput.txtMaxMensesCyc.value)<parseInt(frmInput.txtMinMensesCyc.value))
	{
		alert("输入错误，请仔细核对您的输入周期！");
		frmInput.txtMaxMensesCyc.focus();
		return false;
	}
	//display calendars
	document.all.jqzc.style.display="";
	GetCalendarOuterHTML(1,1);
	document.all.Calendar1.outerHTML = CalendarOuterHTML;
	GetCalendarOuterHTML(2,1);
	document.all.Calendar2.outerHTML = CalendarOuterHTML;	
}

//Initialize
function InitialCalendar()
{
	//Initialize the date input boxes
	frmInput.txtYear.value = Today.getYear();
	frmInput.txtMonth.value = Today.getMonth()+1;
	frmInput.txtDay.value = Today.getDate();
	
	//Initialize the calendars
	GetCalendarOuterHTML(1,0);
	document.all.Calendar1.outerHTML = CalendarOuterHTML;
	GetCalendarOuterHTML(2,0);
	document.all.Calendar2.outerHTML = CalendarOuterHTML;
	
	//Set focus
	frmInput.btnCalculate.focus();
}
