window.onload=function(){
	$obj('name').focus();
}
//在线咨询--添加
function message(){
	if($v('name').trim()==''){
		alert('姓名不能为空！');
		$obj('name').value='';
		$obj('name').focus();
		return false;
	}
	//验证email
	var e = document.getElementById("email").value;
	if(e != "") {
		if(!/(\S)+[@]{1}(\S)+[.]{1}(\w)+/.test(e))
		{
			alert("e-mail格式不正确！");
			var email = document.getElementById ( "email" );
			email.focus ();
			return false;
		}
	}

	if($v('question').trim()==''){
		alert('请填写您的问题！');
		$obj('question').value='';
		$obj('question').focus();
		return false;
	}

	if($v('verifycode').trim()==''){
		alert('请填写验证码！');
		$obj('verifycode').value='';
		$obj('verifycode').focus();
		return false;
	}
}

//预约挂号
function booking(){
	//姓名
	if($v('name').trim()==''){
		alert('姓名不能为空！');
		$obj('name').value='';
		$obj('name').focus();
		return false;
	}
	//证件号码
	if($v('proofcode').trim()==''){
		alert('证件号码不能为空！');
		$obj('proofcode').value='';
		$obj('proofcode').focus();
		return false;
	}else{
		if(!isidcard($v('proofcode'))){
			alert('身份证格式不正确！');
			$obj('proofcode').focus();
			return false;
		}
	}

	if($v('mobile').trim()==''){
		//区号
		if($v('phone1').trim()==''){
			alert('区号不能为空！');
			$obj('phone1').value='';
			$obj('phone1').focus();
			return false;
		}
		//电话
		if($v('phone2').trim()==''){
			alert('电话不能为空！');
			$obj('phone2').value='';
			$obj('phone2').focus();
			return false;
		}else{
			if(!isDigital($v('phone2'))){
				alert('电话格式不正确！');
				$obj('phone2').focus();
				return false;
			}
		}
	}else{
		if(!isDigital($v('mobile'))){
			alert('手机格式不正确！');
			$obj('mobile').focus();
			return false;
		}
	}

	//手机
	if($v('phone1').trim()=='' || $v('phone2').trim()==''){
		if($v('mobile').trim()==''){
			alert('手机不能为空！');
			$obj('mobile').value='';
			$obj('mobile').focus();
			return false;
		}else{
			if(!isDigital($v('mobile'))){
				alert('手机格式不正确！');
				$obj('mobile').focus();
				return false;
			}
		}
	}else{
		if(!isDigital($v('phone2'))){
			alert('电话格式不正确！');
			$obj('phone2').focus();
			return false;
		}
	}



	//预约挂号时间
	if($v('bookingtime').trim()==''){
		alert('预约挂号时间不能为空！');
		return false;
	}else{
		//相差天数
		//		alert(DateDiff(getNowFormatDate(), '2010-05-30'));
		if($v('bookingtime')<=getNowFormatDate()){
			$obj('bookingtime').value='';
			alert('预约日期不能小于当天日期！');
			return false;
		}else{
			minus_days=DateDiff(getNowFormatDate(), $v('bookingtime'));
			//			alert(minus_days);
			//			alert($v('expert_booking_day'));
			if(minus_days<=$v('booking_start')){
				alert("预约挂号时间："+$v('booking_startdate')+" 至 "+$v('booking_enddate')+"");
				$obj('bookingtime').value='';
				return false;
			}
			//			alert(minus_days);
			//			alert((parseInt(minus_days)+parseInt($v('booking_start'))));
			//			alert((parseInt($v('booking_end'))-parseInt($v('booking_start'))));
			if((parseInt(minus_days)-parseInt($v('booking_start')))>(parseInt($v('booking_end'))-parseInt($v('booking_start'))))			{
				alert("预约挂号时间："+$v('booking_startdate')+" 至 "+$v('booking_enddate')+"");
				$obj('bookingtime').value='';
				return false;
			}

		}
	}

	//预约挂号科室
	if($v('dept').trim()==''){
		alert('预约挂号科室不能为空！');
		$obj('dept').focus();
		return false;
	}

	//预约专家不能为空
	if($v('expert').trim()=='0'){
		alert('预约专家不能为空！');
		$obj('expert').focus();
		return false;
	}


}


// 判断是否是数字
function isDigital(str)
{
	for(i=0;i<str.length;i++)
	{
		// 允许使用连字符
		if(str.charAt(i)>='0' && str.charAt(i)<='9')
		continue;
		else
		return false;
	}
	return true;
}

// 判断是否是Email
function isEmail(email)
{
	if(email.length==0)
	return false;
	index1 = email.indexOf('@');
	index2 = email.indexOf('.');
	if(index1 < 1              // @符号不存在，或者在第一个位置
	|| index2 < 1          // .符号不存在，或者在第一个位置
	|| index2-index1 <2    // .在@的左边或者相邻
	|| index2+1 == email.length) // .符号后面没有东西
	return false
	else
	return true;
}

//计算两个日期的相差天数
function DateDiff(startDate, endDate){
	var aDate, oDate1, oDate2, iDays ;
	aDate = startDate.split('-');
	oDate1 = new Date(aDate[1]+'-'+aDate[2]+'-'+aDate[0]) ;
	aDate = endDate.split('-');
	oDate2 = new Date(aDate[1]+'-'+ aDate[2] +'-'+aDate[0]);
	iDays = parseInt(Math.abs(oDate1 -oDate2)/1000/60/60/24); //把相差的毫秒数转换为天数
	return iDays ;
}

//获取当前格式化后的时间
function getNowFormatDate()
{
	var day = new Date();

	var Year = 0;
	var Month = 0;
	var Day = 0;
	var CurrentDate = "";
	//初始化时间
	//Year       = day.getYear();//有火狐下2008年显示108的bug
	Year       = day.getFullYear();//ie火狐下都可以
	Month      = day.getMonth()+1;
	Day        = day.getDate();

	CurrentDate += Year + "-";

	if (Month >= 10 )
	{
		CurrentDate += Month + "-";
	}
	else
	{
		CurrentDate += "0" + Month + "-";
	}
	if (Day >= 10 )
	{
		CurrentDate += Day ;
	}
	else
	{
		CurrentDate += "0" + Day ;
	}

	return CurrentDate;
}


//匹配身份证(18位)
function isidcard(str)
{
	var result=str.match(/^\d{15}(\d{2}[A-Za-z0-9])?$/);
	if(result==null){
		return false;
	}else{
		return true;
	}
}

String.prototype.trim = function()
{
	// 用正则表达式将前后空格，用空字符串替代。
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

function $v(id){
	return document.getElementById(id).value;
}
function $obj(id){
	return document.getElementById(id);
}


