//初始化Div
function initMenu(){
	document.getElementById('selDiv').style.display='none';
	var ul = document.getElementById('selUL');
	var liList = ul.getElementsByTagName('li');
	var lilength =  liList.length;
	for (var i = 0; i < lilength; i++) {
	ul.removeChild(liList[0]);
	}
	$("input[name='selArea']").attr("checked",false);
	$("#confirmDiv").remove();
}
//第一次选择
function showMenu(){
	initMenu();
	document.getElementById('areaDiv').style.display='block';
	document.getElementById("hospDiv").style.display="none";
	document.getElementById('deptDiv').style.display='none';
	document.getElementById('doctorDiv').style.display='none';
	document.getElementById("mainMenu").style.display="block";
	
}
//第二次选择
function showReMenu(){
	//$("input[name='selArea' value='1']").attr("checked",true);
	document.getElementById('areaDiv').style.display='block';
	document.getElementById("hospDiv").style.display="none";
	document.getElementById('deptDiv').style.display='none';
	document.getElementById('doctorDiv').style.display='none';
	document.getElementById("mainMenu").style.display="block";
	//showOtherHosp();
}
//关闭Div
function closeMenu(){
	document.getElementById('mainMenu').style.display='none';
	$("input[name='selArea']").attr("checked",false);
	var cid=$("#cityId").attr("value");
	var cname=$("#cityName").attr("value");
	var hid=$("#hospID").attr("value");
	var hname=$("#hospName").attr("value");
	var did=$("#deptID").attr("value");
	var dname=$("#deptName").attr("value");
	$("#selCid").val(cid);
	$("#selCname").val(cname);
	$("#selHid").val(hid);
	$("#selHname").val(hname);
	$("#selDid").val(did);
	$("#selDname").val(dname);
	var selCity=$("#selCity");
	if(selCity.length>0)
		selCity.html("您选择的地区是："+cname);
	var selHosp=$("#selHosp");
	if(selHosp.length>0)
		selHosp.html("您选择的医院："+hname+"<span><a href=\"javascript:void(0);\" onclick=\"showOtherHosp()\">选择其他医院</a></span>");
	var selDept=$("#selDept");
	if(selDept.length>0)
		selDept.html("您选择的科室是："+dname+"<span><a href=\"javascript:void(0);\" onclick=\"showOtherDept()\">选择其他科室</a></span>");
	
}
//获取医院列表
function getHospList(city){
	$.ajax({
	    type: "post",
	    url : "ajax/reg_hospital.jspx",
	    dataType:'json',
	    data: 'cityID='+city,
	    success: function(json){
		    var hospList=json.jsonRoot.hospital;
			for(var i=0;i<hospList.length;i++){
				$("#hospDiv").append("<span><a href=\"javascript:void(0)\" onclick=\"showDept('"+hospList[i].hospID+"','"+hospList[i].hospName+"')\">"+hospList[i].hospName+"</a></span>");
			}
			$("input[name=selArea]").removeAttr('disabled');
	    },
	    error: function(){
	    	$("input[name=selArea]").removeAttr('disabled');
		     alert('加载医院列表出错');
		}
	});
}
//获取科室列表
function getDeptListByHosp(hosp){
	$.ajax({
	    type: "post",
	    url : "ajax/reg_dept.jspx",
	    dataType:'json',
	    data: 'hospID='+hosp,
	    success: function(json){
			//var list=json.jsonRoot.dept;
	    	var mainDept=json.jsonRoot.mainList;
	    	var subDept=json.jsonRoot.subList;
			for(var i=0;i<mainDept.length;i++){
				var dl = $('<dl></dl>');
				if(mainDept[i].subFlag){
					dl.append("<dt>"+mainDept[i].deptName+"</dt>");
					for(var j=0;j<subDept.length;j++){
						if(subDept[j].parentID==mainDept[i].deptID)
						dl.append("<dd><a href=\"javascript:void(0)\" onclick=\"showDoctor('"+hosp+"','"+subDept[j].deptID+"','"+subDept[j].deptName+"')\">"+subDept[j].deptName+"</a><font style=\"FONT-SIZE: 12px; COLOR: #FF0002; LINE-HEIGHT: 24px \">("+subDept[j].count+")</font></dd>");
					}
				}else{
					dl.append("<dt><a href=\"javascript:void(0)\" onclick=\"showDoctor('"+hosp+"','"+mainDept[i].deptID+"','"+mainDept[i].deptName+"')\">"+mainDept[i].deptName+"</a><font style=\"FONT-SIZE: 12px; COLOR: #FF0002; LINE-HEIGHT: 24px \">("+mainDept[i].count+")</font></dt>");
				}
				$("#deptDiv").append(dl);
				$("#deptDiv").append("<div style=\"height:0;overflow:hidden;\"></div>");
				$("#deptDiv").append("<div class=\"clear\"></div>");
				//$("#hospDiv").append("<span><a href=\"javascript:void(0)\" onclick=\"showDept('"+json[i].hospId+"','"+json[i].hospName+"')\">"+json[i].hospName+"</a>(87595)</span>");
			}
	    },
	    error: function(){
		     alert('加载科室列表出错');
		}
	});
}
//获取医生列表
function getDoctorList(hosp,dept){
	$.ajax({
	    type: "post",
	    url : "ajax/reg_doctor.jspx",
	    dataType:'json',
	    data: 'hospID='+hosp+'&deptID='+dept,
	    success: function(json){
			var doctor=json.jsonRoot.doctor;
    		var reginfo=json.jsonRoot.reginfo;
    		if(doctor.length>0){
				for(var i=0;i<doctor.length;i++){
					str = "<tr><td class=\"color_black\">"+doctor[i].doctName+"</td><td class=\"color_black\">"+doctor[i].title+"</td>";
					count=0;
					content="";
					
					for(var j=0;j<reginfo.length;j++){
						if(reginfo[j].doctID==doctor[i].doctID && reginfo[j].flag==1){
							content=content+"<td><a href=\"javascript:void(0);\" class=\"under\" onclick=\"showInfo('"+doctor[i].doctID+"','"+doctor[i].doctName+"','"+reginfo[j].date+"','"+reginfo[j].time+"','"+reginfo[j].timeFlagStr+"','"+doctor[i].title+"','"+doctor[i].stopRegTimeM+"','"+doctor[i].stopRegTimeA+"','"+doctor[i].stopRegTimeE+"')\">"+reginfo[j].date+"（周"+reginfo[j].weekStr+""+reginfo[j].timeFlagStr+"）</a></td>";
							count++;
							if(count==2)
								break;
						}
					}
					if(content==""&&count==0)
						content="<td colspan=\"2\">无号源</td>"; 
					else if(count==1)
						content=content+"<td>&nbsp;</td>"; 	
					str=str+content+"<td><a href=\"reg_month.jspx?hospital="+hosp+"&dept="+dept+"&doctor="+doctor[i].doctID+"\">查看更多时间</a></td></tr>";
					$("#doctorBody").append(str);
				}
    		}else{
    			str = "<tr><td class=\"color_black\" colspan=\"5\">暂时没有号源</td></tr>";
    			$("#doctorBody").append(str);
    		}
	    },
	    error: function(){
		     alert('加载医生出错');
		}
	});
}
function showArea(){
	$("input[name='selArea']").attr("checked",false);
	document.getElementById('areaDiv').style.display='block';
	document.getElementById("hospDiv").style.display='none';
	document.getElementById('deptDiv').style.display='none';
	document.getElementById('doctorDiv').style.display='none';
}
//显示医院列表
function showHosp(cid,cname){
	$("#selCid").val(cid);
	$("#selCname").val(cname);
	$("input[name=selArea]").attr("disabled","disabled");
	document.getElementById('selDiv').style.display='block';
	document.getElementById('hospDiv').innerHTML="";
	$("#hospDiv").append("<h1>"+cname+"医院列表(点击您要预约的医院名称)：</h1>");
	
	var selCity=$("#selCity");
	if(selCity.length>0){
		selCity.html("您选择的地区是："+cname+"<span><a href=\"javascript:void(0);\" onclick=\"showOtherArea()\">选择其他地区</a></span>");
	}else{
		$("#selUL").append("<li id=\"selCity\">您选择的地区是："+cname+"<span><a href=\"javascript:void(0);\" onclick=\"showOtherArea()\">选择其他地区</a></span></li>");
	}
	getHospList(cid);
	document.getElementById('areaDiv').style.display='none';
	document.getElementById("hospDiv").style.display='block';
	document.getElementById('deptDiv').style.display='none';
	document.getElementById('doctorDiv').style.display='none';
}
//显示科室列表
function showDept(hid,hname){
	$("#selHid").val(hid);
	$("#selHname").val(hname);
	document.getElementById('deptDiv').innerHTML="";
	$("#deptDiv").append("<h1>"+hname+"科室列表(点击您要预约的科室名称，后面括号中的数字为<font style=\"FONT-SIZE: 12px; COLOR: #FF0002; LINE-HEIGHT: 24px \">当前可预约的号源数量</font>，如果为0，即当前暂时没有号源可供预约挂号，请选择其他医院或其他科室)：</h1>");
	var selHosp=$("#selHosp");
	if(selHosp.length>0){
		selHosp.html("您选择的医院："+hname+"<span><a href=\"javascript:void(0);\" onclick=\"showOtherHosp()\">选择其他医院</a></span>");
	}else{
		$("#selUL").append("<li id=\"selHosp\">您选择的医院："+hname+"<span><a href=\"javascript:void(0);\" onclick=\"showOtherHosp()\">选择其他医院</a></span></li>");
	}
	getDeptListByHosp(hid);
	document.getElementById('areaDiv').style.display='none';
	document.getElementById('hospDiv').style.display='none';
	document.getElementById("deptDiv").style.display='block';
	document.getElementById('doctorDiv').style.display='none';
}
//显示医生列表
function showDoctor(hid,did,dname){
	$("#selDid").val(did);
	$("#selDname").val(dname);
	$("#doctorTable tr:gt(0)").each(function() {
        $(this).remove();
    });
	document.getElementById('selDeptTitle').innerHTML="<h1>"+dname+"医生列表(点击“一周内可预约时间”中的时间名称，确定您要挂号的时间，选择完成后，点击地区名称上面一行的“确定”按钮；如需要选择某个医生的其他时间，请点击该医生一行最右边的“查看更多时间”)：</h1>";
	//$("#doctorDiv").append("<h1>"+dname+"医生列表：</h1>");
	var selDept=$("#selDept");
	if(selDept.length>0){
		selDept.html("您选择的科室是："+dname+"<span><a href=\"javascript:void(0);\" onclick=\"showOtherDept()\">选择其他科室</a></span>");
	}else{
		$("#selUL").append("<li id=\"selDept\">您选择的科室是："+dname+"<span><a href=\"javascript:void(0);\" onclick=\"showOtherDept()\">选择其他科室</a></span></li>");
	}
	getDoctorList(hid,did);
	document.getElementById('areaDiv').style.display='none';
	document.getElementById('hospDiv').style.display='none';
	document.getElementById('deptDiv').style.display='none';
	document.getElementById("doctorDiv").style.display='block';
}
//重新选择地区
function showOtherArea(){
	showArea();
}
//重新选择医院
function showOtherHosp(){
	id=$("#selCid").attr("value");
	name=$("#selCname").attr("value");
	showHosp(id,name);
}
//重新选择科室
function showOtherDept(){
	id=$("#selHid").attr("value");
	name=$("#selHname").attr("value");
	showDept(id,name);
}
function showOtherDoctor(){
	id=$("#selHid").attr("value");
	dname=$("#selDname").attr("value");;
	did=$("#selDid").attr("value");
	
	showDoctor(id,did,dname);
}
//选择预约
function showInfo(doctId,doctName,da,tp,tpStr,title,mTime,aTime,eTime){
	var area=$("#selCname").attr("value");
	var areaId=$("#selCid").attr("value");
	var hosp=$("#selHname").attr("value");
	var hospId=$("#selHid").attr("value");
	var dept=$("#selDname").attr("value");
	var deptId=$("#selDid").attr("value");

	$("#cityId").val(areaId);
	$("#cityName").val(area);
	$("#hospID").val(hospId);
	$("#hospName").val(hosp);
	$("#deptID").val(deptId);
	$("#deptName").val(dept);
	$("#doctID").val(doctId);
	$("#doctName").val(doctName);
	$("#regDate").val(da);
	$("#regTimeFlag").val(tp);
	//$("#title").val(title);
	$("#mTime").val(mTime);
	$("#aTime").val(aTime);
	$("#eTime").val(eTime);
	
	date=da+" "+tpStr;
	setSelReg(date);
	//$("#sarea").html(area);
	//$("#shosp").html(hosp);
	//$("#sdept").html(dept);
	//$("#sdoct").html(doctName);
	//$("#sdate").html(date);
	
	//document.getElementById('mainMenu').style.display='none';
	//document.getElementById('qId').style.display='none';
	//document.getElementById('qInfoId').style.display='none';
	//document.getElementById('menuHref').innerHTML="点击这里重新选择";
	//document.getElementById("selectInfo").style.display='block';
	//setType();
}
function setSelReg(selDate){
	document.getElementById('doctorDiv').style.display='none';
	var doctName=$("#doctName").attr("value");
	var selDoctor=$("#selDoctor");
	if(selDoctor.length>0){
		selDoctor.html("医生姓名："+doctName+"<span><a href=\"javascript:void(0);\" onclick=\"showOtherDoctor()\">选择其他医生</a></span>");
	}else{
		$("#selUL").append("<li id=\"selDoctor\">医生姓名："+doctName+"<span><a href=\"javascript:void(0);\" onclick=\"showOtherDoctor()\">选择其他医生</a></span></li>");
	}
	var selTime=$("#selTime");
	if(selTime.length>0){
		selTime.html("就医时间："+selDate);
	}else{
		$("#selUL").append("<li id=\"selTime\">就医时间："+selDate+"</li>");
	}
	var confirmDiv=$("#confirmDiv");
	if(confirmDiv.length>0)
		confirmDiv.html("<input type=\"submit\" value=\"确  定\" onclick=\"javascript:submitReg();\" style=\"width:80px;height:30px;font-size:14px;font-weight:bold;\"/>");
	else
		$("#selDiv").append("<div id=\"confirmDiv\"><input type=\"submit\" value=\"确  定\" onclick=\"javascript:submitReg();\" style=\"width:80px;height:30px;font-size:14px;font-weight:bold;\"/></div>");
	//$("#confirmID").focus();
}
function submitReg(){
	var flag=$("#regTimeFlag").attr("value");
	if(flag==1)
		document.getElementById("endTime").value=document.getElementById("mTime").value;
	else if(flag==2)
		document.getElementById("endTime").value=document.getElementById("aTime").value;
	else if(flag==3)
		document.getElementById("endTime").value=document.getElementById("eTime").value;
	
	document.regForm.submit();
}
function setType(){
	if(document.getElementById('hospId').value=="302"){
		 document.getElementById('payType').disabled=false;
		 document.getElementById('payTypeTr').style.display='block';
	}else{
		 document.getElementById('payType').disabled=true;
		 document.getElementById('payTypeTr').style.display='none';
	}
}
