$(function(){
	
	    loginStatus();//读取用户头像
	    valUser();
	    var setta = GetQueryString("seta");
	
		if(setta==2){
			$(".account-set a").eq(1).addClass("bgcolor").siblings().removeClass("bgcolor");
			$(".contentbox .content-set").eq(1).show().siblings().hide();
		}
		
		var userid = $.cookie("userid");
		//个人信息头像获取
   		$("#headimg").attr("src", "/images/head/" + userid + "_l.jpg");
		$("#headimg").load(function() {
		}).error(function() {
				$("#headimg").attr("src",  "/images/default-photo.jpg");
		})
		//获取用户用户名
		$.ajax({	
			"url":"/ajax/professor/info/"+userid,
			"type":"get",
			"success":function($data){
				if($data.data)
				{	
					$('#username').text($data.data.name);
				}
			}
		})	
		
     	$("#hsearch").on("click",function (){
	   		var searchContent = $("#hsearchContent").val();
	   		location.href = "search.html?searchContent="+searchContent;
		});
		
});
		/*专家信息左导航背景点击切换*/
		$(".leftsidebar").click(function(){
			var index = $(this).index()-1;
			$(this).addClass("bgcolor").siblings().removeClass("bgcolor");	
			$(".content-set").eq(index).show().siblings().hide();
		});
		/*编辑标签变色html里隐藏了*/
		$("#container .edit").hover(function(){
			$(this).addClass("edit-position-left")	
			},function(){
			$(this).removeClass("edit-position-left")			
		});
		//邮箱绑定，手机绑定，点击关闭隐藏这两个部分
		$(".times").click(function(){
			$(".cover,.coverset,.cover2").hide();	
		});
		//邮箱绑定，手机绑定，点击返回，隐藏这两个部分
		$(".back-btn").click(function(){
			$(".cover").hide();		
			$(".cover2").hide();		
		});
		//这个标签已经隐藏了，点击手机账号绑定
		$(".replace2").click(function(){
			$(".cover2").show();		
		});
		//这个标签已经隐藏了，点击邮箱账号绑定
		$(".replace1").click(function(){
			$(".cover").show();		
		});
		//点击邮箱账号绑定
		$(".replace5").click(function(){
			$(".cover").show();		
		})
		//点击手机账号绑定
		$(".replace6").click(function(){
			$(".cover2").show();		
		});
		//判断是否绑定了邮箱
		var emailCookie = $.cookie("userEmail");
		var phoneCookie = $.cookie("userMobilePhone");
		if(emailCookie != "" && emailCookie != null && emailCookie != "null" ){
		    //alert(emailCookieshow);
		    emailhome(emailCookie);
		    $("#emailShow").text(emailCookieshow);
			$(".replace1").hide();	
			$(".replace3").show();		
		}else{
			$(".replace1").show();	
			$(".replace3").hide();		
		}
		//判断是否绑定了手机号
		if(phoneCookie != "" && phoneCookie != null && phoneCookie != "null" ){
			$("#phoneShow").text(phoneCookie.substring(0, 3) + "****" + phoneCookie.substring(7, 11));
			$(".replace2").hide();	
			$(".replace4").show();		
		}else{
			$(".replace2").show();	
			$(".replace4").hide();		
		}
		
		function emailhome(emailset){
			//var emailCookieshow;
			var  emailq = emailset.replace(/@.*/, "");
		    var  emailh = emailset.replace(emailq, "");
		    if(emailq.length>=6){
		    	emailCookieshow = emailq.substring(emailq.length-4, 0) + "****" + emailh;
		    }
		    if(emailq.length==5){
		    	emailCookieshow = emailq.substring(emailq.length-3, 0) + "***" + emailh;
		    }
		    if(emailq.length==4){
		    	emailCookieshow = emailq.substring(emailq.length-2, 0) + "**" + emailh;
		    }
		    if(emailq.length==3){
		    	emailCookieshow = emailq.substring(emailq.length-1, 0) + "*" + emailh;
		    }
		    if(emailq.length<3){
		    	emailCookieshow = emailq+emailh;
		    }
		    return emailCookieshow;
		}


	var oldPass = false;
	var isPass = false;
	var isPass2 = false;
	function valOld(){
		var oldPassword = $("#oldPassword").val();
		if(oldPassword.length==0){
			$(".msg1").text("请输入密码");
			oldPass = false;
		}else if(oldPassword.length < 6){
			$(".msg1").text("密码不少于6位，请输入正确的密码");
		}else{
			$(".msg1").text("");
			oldPass = true;
		}
	}

	function valNew(){
		var newPassword = $("#newPassword").val();
		var oldPassword = $("#oldPassword").val();
		if(newPassword.length==0){
			 $(".msg2").text("密码不能为空");
		}else if(newPassword.length<6){
			$(".msg2").text("密码长度过短,至少六位");
		}else if(newPassword2 == newPassword){
			$(".msg2").text("新旧密码不能一致");
		} else{
			isPass = true;
			$(".msg2").text("");
		}
	}
	function valNew2(){
		var newPassword = $("#newPassword").val();
		var newPassword2 = $("#newPassword2").val();
		if(newPassword2.length==0){
			 $(".msg3").text("密码不能为空");
		}else if(newPassword2 != newPassword){
			$(".msg3").text("两次输入密码不一致");
		}else{
			isPass2 = true;
			$(".msg3").text("");
		}
	}

	function restPassword(){
		valOld();
		valNew();
		valNew2();
		if(oldPass == true && isPass == true && isPass2 == true){
			$.ajax("/ajax/cp",{
					type:"POST",
					 async: false,
					success:function(data){  

						if(data.success){
							if(data.data == true){
								$.MsgBox.Alert("消息提醒","设置成功,请重新登录");
								/*location.href="login.html";*/
							}else{

								$.MsgBox.Alert("消息提醒","设置失败,请检查密码是否正确!");
							}
						}else{
							$.MsgBox.Alert("消息提醒","系统异常!");
							// alert("系统异常!");
						}
					},
					error:function(){$.MsgBox.Alert('message','fail')},
					data:{"id":$.cookie("userid"),"npw":$("#newPassword2").val(),"onw":$("#oldPassword").val()},
				 	dataType: 'json'
				});
		}else{
			/*alert("请校验密码");*/
		}
	}
	
	
	
	//重新绑定邮箱
	var emailPass = false;
	var emailOk = false;
	//页面需要验证展示的位置，先用alert替代
	function valEmail(){
		var email = $("#email").val();
		//var gunf=/^\w+@\w+\.((cn)|(com)|(com\.cn))$/;
		var gunf= /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/; 
        if(gunf.test(email.trim())){
           userRegisterOk();
           emailPass = true;
           emailSuccess();
        }else{
        	/*alert("请输入正确是邮箱地址");*/
        	$(".msg11 span").text("请输入正确的邮箱地址。");
        	emailPass = false;
        }
	}
	//获取邮箱，填写地址
	function emailSuccess()
	{
		var mailVal =$('#email').val();
		$(".sett").each(function() {  
	        var url = mailVal.split('@')[1];
	        for (var j in hash){  
	            $(this).attr("href", hash[url]);  
	        }  
	     });
	}
	
	//判断邮箱是否绑定
	function userRegisterOk(){
		var email = $("#email").val();
		console.log(email);
		$.ajax("/ajax/isReg?key="+email,{
			type:"GET",
			async: false,
			success:function($data){ 
				console.log($data);
			    if($data.data==true){
			    	 $(".msg11").text("");
			    	 emailOk = true;
			    }else{
			    	 $(".msg11").text("该邮箱已绑定账号，请使用其他邮箱地址。");
			    	 emailOk = false;
			    }
			}, 
			error:function(){
				$.MsgBox.Alert('消息','服务器请求失败')},
			}
		);
	}	
	
	
	 var mailCode = GetQueryString('sc');
	//获取邮箱验证码传值注册
	if(mailCode!=''){
		$(".account-set a").eq(1).addClass("bgcolor").siblings().removeClass("bgcolor");
		$(".contentbox .content-set").eq(1).show().siblings().hide();
	    $.ajax("/ajax/bindMail/"+mailCode,{
				type:"get",
				async: true,
				success:function(data){ 
					 $.MsgBox.Alert('消息提醒','邮箱绑定成功')
					 userpe();
				},
				error:function(){
				   $.MsgBox.Alert('消息提醒','邮箱绑定失败')
				},
				data:{},			
			 	dataType: 'json'}
		); 	 
	 }
	
	
	//查询用户手机号和邮箱
	function userpe(){
		$.ajax("/ajax/qaUser",{
				type:"get",
				async: true,
				success:function(data){ 
					console.log(data)
					 $.MsgBox.Alert('消息提醒','邮箱绑定成功')
					 var emaild = data.data.email;
					 emailhome(emaild);
					 $.cookie('userEmail',emaild,{path:"/"});
					 $("#emailShow").text(emailCookieshow);
				},
				error:function(){
				   $.MsgBox.Alert('消息提醒','邮箱绑定失败')
				},
				data:{"id":$.cookie("userid")},			
			 	dataType: 'json'}
		); 	 
	}
	
	
	//点击邮箱激活
	function bindEmail(){
		valEmail();
		var email = $("#email").val();
		if(emailPass == true && emailOk == true){
			$.ajax("/ajax/reqBindMail",{
					type:"GET",
					async: false,
					success:function(data){ 
						console.log(data);
						if(data.success){
							console.log(data);
							if(data.data == true){
								//$.cookie('userEmail',email); 
								$(".cover").hide();
								$("#vovernext").show();
								$(".emalvel").text(email);
							}else{
								$.MsgBox.Alert("消息提醒","邮箱发布失败!");
							}
						}else{
							$.MsgBox.Alert("消息提醒","服务器链接超时!");
						}
					},
					error:function(){$.MsgBox.Alert('message','fail')},
					data:{"userid":$.cookie("userid"),"mail":$("#email").val()},
				 	dataType: 'json'
				});
		}else{
			/*alert("请输入正确是邮箱地址");*/
		}
	}
	
	$("#bindgo").on("click",function(){
		var mailVal =$('.sett').attr("href");  
		location.href=mailVal;
	})
	
	//判断手机是否绑定
	function phoneRegisterOk(){
		var phoneVal = $("#phone").val();
		$.ajax("/ajax/isReg?key="+phoneVal,{
			type:"GET",
			async: false,
			success:function($data){ 
				console.log($data);
			    if($data.data==true){
			    	 $(".msg12").text("");
			    	 phonePass = true;
			    }else{
			    	 $(".msg12").text("该手机已绑定账号，请使用其他手机号码。");
			    }
			}, 
			error:function(){
				$.MsgBox.Alert('消息','服务器请求失败')},
			}
		);
	}	
	
	//验证手机号是否合法
	var phonePass = false;
	function valPhone(){
		var phoneVal = $("#phone").val();
    	var hunPhone=/^1[3|4|5|7|8]\d{9}$/;
		if(hunPhone.test(phoneVal.trim())){
			phoneRegisterOk();
		}else{
			phonePass = false;
			/*alert("手机格式正确,请输入正确的手机号码");*/
			$(".msg12").text("请输入正确的手机号码");
		}
	}
	//验证手机验证码
	var codePass = false;
	function valCode(){
		var phoneCode = $("#phoneCode").val();
		if(phoneCode.length==0){
			/*alert("请输入验证码");*/
	        $(".msg13").text("请输入验证码");
	    }else{
		    if(!isNaN(phoneCode)){
				codePass = true;
			}else{
				codePass=false;
				 $(".msg13").text("请填写数字");
			  /*alert("请填写数字");*/
			}
	    }

	}
	//发送手机验证码
	var phoneState;
	function sendPhoneCode(){
		valPhone();
		if(phonePass == true){
			$.ajax("/ajax/vcWithBind",{
					type:"GET",
					 async: false,
					success:function(data){ 
						console.log(data);
						if(data.success){
							if(data.data == null){
								$(".msg13").text("验证码发送失败,请确认手机号码正确!");
								/*alert("验证码发送失败,请确认手机号码正确!");*/
							}else{
								phoneState = data.data;
								$(".msg13").text("发送成功,请检查验证码");
							}
						}else{
							$.MsgBox.Alert("消息提醒","系统异常!");
							// alert("系统异常!");
						}
					},
					error:function(){$.MsgBox.Alert('message','fail')},
					data:{"userid":$.cookie("userid"),"mobilePhone":$("#phone").val()},
				 	dataType: 'json'
				});
		}else{
			/*alert("请输入正确是手机号码");*/
		}
	}
	//绑定手机号
	function bindPhone(){
	 	valCode();
		if(codePass == true){
			/*alert($.cookie("userid"));
			alert($("#phone").val());
			alert(phoneState);
			alert($("#phoneCode").val());*/
			var phoneVal = $("#phone").val();
			$.ajax("/ajax/bindMobilePhone",{
					type:"POST",
					 async: false,
					success:function(data){ 
						if(data.success){
							/*alert(2);*/
							if(data.data == true){
								$.cookie('userMobilePhone',phoneVal,{path:"/"}); 
								$(".cover2").hide();
								$.MsgBox.Alert("消息提醒","手机绑定成功!");	
								setTimeout(function(){
									location.href="account-set.html?seta=2";
								},1500);
							}else{
								$.MsgBox.Alert("消息提醒","短信验证不正确，请检查后重新输入。");
							}
						}else{
							$.MsgBox.Alert("消息提醒","系统异常!");
							// alert("系统异常!");
						}
					},
					error:function(){$.MsgBox.Alert('message','fail')},
					data:{"userid":$.cookie("userid"),"mobilePhone":$("#phone").val(),"validateCode":$("#phoneCode").val(),"state": phoneState},
				 	dataType: 'json'
				});
		}else{
			/*alert("请输入验证码是否正确");*/
		}
	}

	
	



	
	
	
	
	
	
