var MonHead = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
//填充所有下拉框
function YYYYMMDDstart() {
	// 先给年下拉框赋内容
	var y = new Date().getFullYear();
	for ( var i = y; i >= 1940; i--){
		// 从1940年开始到现在
		//加入60后、70后、80后、90后
		if(i==1960||i==1970||i==1980||i==1990){
			document.getElementById('birthYear').options.add(new Option((i-1900)+"后*", i+"*"));
			document.getElementById('birthYear').options.add(new Option("" + i + "", i));
		}else{
			document.getElementById('birthYear').options.add(new Option("" + i + "", i));
		}
	}
	// 赋月份的下拉框
	for ( var j = 1; j < 13; j++){
		if(j<10){j="0"+j;}
		document.getElementById('birthMonth').options.add(new Option("" + j + "", j));
	}
	var n;
	var bday = document.getElementById("bday").value;
	var birthYearValue="";
	var birthMonthValue="";
	var birthDayValue="";
	if(bday.length>0){
		birthYearValue = bday.substr(0,4);
		birthMonthValue = bday.substr(4,2);
		birthDayValue = bday.substr(6,2);
	}
	
	if(birthMonthValue!=""){
		n=MonHead[birthMonthValue-1];
		if (birthMonthValue == "02"  && IsPinYear(birthYearValue)){n++;}
	}else{
		var YYYYvalue = document.getElementById('birthYear').options[document.getElementById('birthYear').selectedIndex].value;
		n = MonHead[new Date().getMonth()-1];
		if (new Date().getMonth() == 1 && IsPinYear(YYYYvalue)){n++;}
	}
	//alert(document.getElementById('birthDayValue').value);
	writeDay(n); // 赋日期下拉框
	//设置用户生日的年份
	var birthYearStr = "";
	if ( document.getElementById('hideAge').value == 1){//n零后
		birthYearStr = birthYearValue+"*";
	}else{
		birthYearStr = birthYearValue;
	}
	for ( var x = 1; x < document.getElementById('birthYear').options.length; x++){
		if(document.getElementById('birthYear').options[x].value==birthYearStr){
			document.getElementById('birthYear').options[x].selected="selected";
		}		
	}	

	//设置用户生日的月份
	for ( var x = 1; x < 13; x++){
		if(document.getElementById('birthMonth').options[x].value==birthMonthValue){
			document.getElementById('birthMonth').options[x].selected="selected";
		}		
	}	
	//设置用户生日的日子
	for ( var x = 1; x < (n+1); x++){
		if(document.getElementById('birthDay').options[x].value==birthDayValue){
			document.getElementById('birthDay').options[x].selected="selected";
		}
	}	
}

// 年发生变化时日期发生变化(主要是判断闰平年)
function YYYYDD(str) {
	//alert("now i m in YYYYDD functiong ,and str is :"+str);
	var selectIndex=document.getElementById('birthMonth').selectedIndex;
	//alert("selectIndex is:"+selectIndex);
	var MMvalue=document.getElementById('birthMonth').options[selectIndex].value;
	//alert("MMvalue is:"+MMvalue);
	//var MMvalue = document.getElementById('birthMonth').options[document.getElementById('birthMonth').selectedIndex].value;
	
	//marked by ligang 
	//if (MMvalue == "") {
	//	var e = document.getElementById('birthDay');
	//	optionsClear(e);
	//	var e = document.getElementById('birthMonth');
	//	optionsClear(e);
	//	return;
	//}
	
	//如果选择了X0后，就需要进行截取与传值
	if (str.indexOf("*") != -1){
		document.getElementById('hideAge').value = 1;
	}else{
		document.getElementById('hideAge').value = 0;
	}
	
	var n = MonHead[MMvalue - 1];
	if (MMvalue == 2 && IsPinYear(str)){n++;}
	writeDay(n);
}

// 月发生变化时日期联动
function MMDD(str) {
	var YYYYvalue = document.getElementById('birthYear').options[document.getElementById('birthYear').selectedIndex].value;
	
	//marked by ligang 
	//if (YYYYvalue == "") {
	//	var e = document.getElementById('birthDay');
	//	optionsClear(e);
	//	return;
	//}
	
	var n = MonHead[str - 1];
	if (str == 2 && IsPinYear(YYYYvalue)){n++;}
	writeDay(n);
}


// 据条件写日期的下拉框
function writeDay(n) {
	//alert("now i m in the writeDay function ,and n is:"+n);
	var e = document.getElementById('birthDay');
	optionsClear(e);
	for ( var i = 1; i < (n + 1); i++){
		if(i<10){i="0"+i;}
		e.options.add(new Option("" + i + "", i));
	}
}

//判断是否闰平年
function IsPinYear(year){
	return (0 == year % 4 && (year % 100 != 0 || year % 400 == 0));
}

function optionsClear(e) {
	e.options.length = 1;
}

//获取星座
function getXingZuoByMonthDay(month,day){
	var s="魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
    var arr=[20,19,21,21,21,22,23,23,23,23,22,22];
    var xinZuo=s.substr(month*2-(day<arr[month-1]?2:0),2)+"座";	
    return xinZuo;	
}
//设置生日和星座的值
function setBdayXingZuo(){
	if(document.getElementById('birthMonth').value!=""&&document.getElementById('birthDay').value!=""){
		document.getElementById('bday').value=document.getElementById('birthYear').value.substring(0,4)+""+document.getElementById('birthMonth').value+""+document.getElementById('birthDay').value;
		document.getElementById('xingZuo').innerHTML="("+getXingZuoByMonthDay(document.getElementById('birthMonth').value,document.getElementById('birthDay').value)+")";
	}
}