/**首页验证**/
var validate = {
	validatePhoneLogin : function(){
		
		var telNum = $("#telno").val();
		telNum = telNum.replace(/\s+/g,"");
		
		var telPassword = $("#password").val();
		telPassword = telPassword.replace(/\s+/g,"");
		
		var telCode = $("#telCode").val();
		telCode = telCode.replace(/\s+/g,"");

		var regPartton=/^1[0-9]{10}$/;

		var rodio=$('input[name="method"]:checked').val();
		$("#telno").blur(function(){
			if($("#telno").val() != "" )
			{
				$("#js_errorBox").empty()
			};
		});
		$("#password").blur(function(){
			if($("#password").val() != "" )
			{
				$("#js_errorBox").empty()
			};
		});
		if(telNum == "" )
		{
			$("#js_errorBox").empty();
			$("#js_errorBox").html("<div class='bg error'><p>请填写手机号码。</p></div>");
			$("#telno").focus();
			return false
		}

	    if(!regPartton.test(telNum)){
			$("#js_errorBox").empty();
			$("#js_errorBox").html("<div class='bg error'><p>请正确输入手机号码。</p></div>");
			$("#telno").focus();
			return false
		
		}
		
		if(rodio==1)
		{
			if(telPassword == "" )
			{
				$("#js_errorBox").empty();
				$("#js_errorBox").html("<div class='bg error'><p>请填写服务密码。</p></div>");
				$("#password").focus();
				return false
			}
			
		}
		else
		{
			
			if(telCode == "" )
			{
				$("#js_errorBox").empty();
				$("#js_errorBox").html("<div class='bg error'><p>请填写动态密码。</p></div>");
				$("#telCode").focus();
				return false
			}
		}
		//让ajax和js验证同步  
		$.ajaxSetup({ 
		async : false 
		});   
		var flag = true;
        $.ajax({
            type: 'post',
            url : 'http://user.house365.com/login',
            data: "action=loginyz"+"&telno="+$("#telno").val()+"&password="+$("#password").val()+"&loginkind="+$("#loginkind").val()+"&method="+$('input[name="method"]:checked').val()+"&telCode="+$("#telCode").val(),
            dataType:'html',
            success: function(msg){

                switch(msg){
					
                    case '2':
                       	$("#js_errorBox").empty()
						$("#js_errorBox").html("<div class='bg error'><p>用户名或密码不正确。</p></div>")
						$("#telCode").focus()
						flag = false;
					    break;
				    case '3':
                       	$("#js_errorBox").empty()
						$("#js_errorBox").html("<div class='bg error'><p>动态验证码不正确。</p></div>")
						$("#telCode").focus()
						flag = false;
					    break;
                    case '4':
                       	$("#js_errorBox").empty()
						$("#js_errorBox").html("<div class='bg error'><p>号码未注册，请立即注册。</p></div>")
						$("#telCode").focus()
						flag = false;
					    break;
                }
            }
        });
		if(!flag)
        return false;

		
	},
	validateUserNameLogin : function(){
		var userName = $("#userName").val();
		userName = userName.replace(/\s+/g,"");
		
		var password = $("#password2").val();
		password = password.replace(/\s+/g,"");
		$("#userName").blur(function(){
			if($("#userName").val() != "" )
			{
				$("#user_errorBox").empty()
			};
		});
		$("#password2").blur(function(){
			if($("#password2").val() != "" )
			{
				$("#user_errorBox").empty()
			};
		});
		if(userName == "" )
		{
			$("#user_errorBox").empty();
			$("#user_errorBox").html("<div class='bg error'><p>请填写用户名。</p></div>");
			$("#userName").focus();
			return false
		}

		if(password == "" )
		{
			$("#user_errorBox").empty();
			$("#user_errorBox").html("<div class='bg error'><p>请输入密码。</p></div>");
			$("#password").focus();
			return false
		}

		var flag = true;
	    //让ajax和js验证同步  
		$.ajaxSetup({ 
		async : false 
		});  
        $.ajax({
            type: 'post',
            url : 'http://www.house365.com/center/login',
            data: "ajaxyz=1&telno="+$("#userName").val()+"&password2="+$("#password2").val(),
            dataType:'html',
            success: function(msg){

                switch(msg){
					
                    case '5':
                       	$("#user_errorBox").empty()
						$("#user_errorBox").html("<div class='bg error'><p>用户名或密码不正确。</p></div>")
						$("#userName").focus()
						flag = false;
					    break;
                }
            }
        });
		if(!flag)
        return false;
	}
}


/*function indexForm(){
	
	var text = $("#userName").val();
	text = text.replace(/\s+/g,""); 
	
	var phone = $("#phone").val();
	phone = phone.replace(/\s+/g,""); 
	
	var password = $("#password").val();
	password = password.replace(/\s+/g,""); 
	
	var code = $("#code").val();
	code = code.replace(/\s+/g,""); 
	
	
	$("#userName").blur(function(){
		if($("#userName").val() != "" )
		{
			$("#js_errorBox").empty()
		}
	})
	
	$("#phone").blur(function(){
		if($("#phone").val() != "" )
		{
			$("#js_errorBox").empty()
		}
	})
	
	$("#password").blur(function(){
		if( $("#password").val() != "" )
		{
			$("#js_errorBox").empty()
		}
	})
	
	$("#code").blur(function(){
		if($("#code").val() != "" )
		{
			$("#js_errorBox").empty()
		}
	})
	
	if(text == "" )
	{
		$("#js_errorBox").empty()
		$("#js_errorBox").html("<div class='bg error'><p>请填写用户名!</p></div>")
		$("#userName").focus()
		return false
	}
	
	if(password == "" )
	{
		$("#js_errorBox").empty()
		$("#js_errorBox").html("<div class='bg error'><p>请填写密码!</p></div>")
		$("#password").focus()
		return false
	}
	
	if(phone == "" )
	{
		$("#js_errorBox").empty()
		$("#js_errorBox").html("<div class='bg error'><p>请填写手机号码!</p></div>")
		$("#phone").focus()
		return false
	}
	
	if(code == "" )
	{
		$("#js_errorBox").empty()
		$("#js_errorBox").html("<div class='bg error'><p>请填写验证码!</p></div>")
		$("#code").focus()
		return false
	}
	
}*/

/***找回密码**/
function findPowForm(){
	var userName2 = $("#userName2").val();
		userName2 = userName2.replace(/\s+/g,""); 
		
	var userEMAIL = $("#userEMAIL").val();
		userEMAIL = userEMAIL.replace(/\s+/g,""); 
	
	
	$("#userName2").blur(function(){
		if($("#userName2").val() != "" )
		{
			$("#innerFormErrorBox").empty()	
		}
	})
	
	$("#userEMAIL").blur(function(){
		if($("#userEMAIL").val() != "" )
		{
			$("#innerFormErrorBox").empty()	
		}
	})
	
	if(userName2 == "" )
	{
		$("#innerFormErrorBox").empty()
		$("#innerFormErrorBox").html("<div class='bg error'><p>请填写您的用户名。</p></div>")
		$("#userName2").focus()
		return false
	}	
		
	if(userEMAIL == "" || !isEmail(userEMAIL) )
	{
		$("#innerFormErrorBox").empty()
		$("#innerFormErrorBox").html("<div class='bg error'><p>请填写正确的邮箱地址。</p></div>")
		$("#userEMAIL").focus()
		return false
	}	
}


/**登录框 弹出**/
var openWin = function(obj){
	var winHeght = $(document).height() ;
	var winElemt = $('#'+ obj);
	var $coverDom = "<div id='GTipsCover' style='height:"+winHeght+"px;width:100%;background:#000;left:0;top:0;position:absolute;z-index:9999901;overflow:hidden;'><iframe src='about:blank' style='width:100%;height:"+winHeght+"px;filter:alpha(opacity=0);opacity:0;scrolling=no;z-index:870610'></iframe></div>"
	
	var win = {
		
		height : function(){
			return winElemt.outerHeight();
		},
		
		width : function(){
			return winElemt.outerWidth();
		},
		
		show : function(){
			winElemt.css({
				'position':'absolute',
				'z-index':'9999902',
				'left':'50%',
				'margin-left':'-' + win.width() / 2 + 'px',
				'top':$(window).height()/2 - (win.height()/2) + $(window).scrollTop() + 'px'
			}).show();
			$($coverDom).appendTo('body').css({opacity:0.4})
		},
		
		hide : function(){
			winElemt.hide();
			$('#GTipsCover').remove();
		}
	
	};
	win.show();
	$('.close-win').click(function(){
		win.hide();
	});
	$(window).resize(function(e) {
		$('#GTipsCover').css('height', $(document).height());	
	});
};

/***弹出登录框登录**/
function popWinLoginForm(){
	var userNAME = $("#userNAME").val();
		userNAME = userNAME.replace(/\s+/g,""); 
		
	var userPassword = $("#userPassword").val();
		userPassword = userPassword.replace(/\s+/g,""); 
	
	var userPopCod = $("#userPopCod").val();
		userPopCod = userPopCod.replace(/\s+/g,""); 
		
	$("#userNAME").blur(function(){
		if($("#userNAME").val() != "" )
		{
			$("#popWinLoginFormErrorBox").empty()	
		}
	})	
	$("#userPassword").blur(function(){
		if($("#userPassword").val() != "" )
		{
			$("#popWinLoginFormErrorBox").empty()	
		}
	})
	$("#userPopCod").blur(function(){
		if($("#userPopCod").val() != "" )
		{
			$("#popWinLoginFormErrorBox").empty()	
		}
	})
	if(userNAME == ""  )
	{
		$("#popWinLoginFormErrorBox").empty()
		$("#popWinLoginFormErrorBox").html("<div class='bg error'><p>请填写您的用户名。</p></div>")
		$("#userNAME").focus()
		return false
	}	
		
	
	
	if(userPassword == "" )
	{
		$("#popWinLoginFormErrorBox").empty()
		$("#popWinLoginFormErrorBox").html("<div class='bg error'><p>请填写您的密码。</p></div>")
		$("#userPassword").focus()
		return false
	}	
	
	
	
	if(userPopCod == "" )
	{
		$("#popWinLoginFormErrorBox").empty()
		$("#popWinLoginFormErrorBox").html("<div class='bg error'><p>请填写验证码。</p></div>")
		$("#userPopCod").focus()
		return false
	}	
}




/****注册验证***/

$("#jsinputChecked").click(function(){
	var check = $(this).attr("checked")?false:true;
	if(check)
	{
		$("#receive").show()
	}
	else
	{
		$("#receive").hide()
	}
})

function registerForm(){
	var registerName = $("#registerName").val();
		registerName = registerName.replace(/\s+/g,""); 
	var registerNameLen = getByteLen(registerName)	
		
	var registerPassword = $("#registerPassword").val();
		registerPassword = registerPassword.replace(/\s+/g,""); 
	var registerPasswordLen = getByteLen(registerPassword)	
		
	var registerEmail = $("#registerEmail").val();
		registerEmail = registerEmail.replace(/\s+/g,""); 
	
	
	if(registerName == "" || registerNameLen < 3)
	{	
		objErrorShow("registerName")
		$("#registerName").parents(".registerItem").find(".registerPrompt").empty().html("<span class='prompt prompt2'>用户名太短，至少3个字符(1个汉字算2个字符)，不能全为数字</span>")
	//	$("#registerName").parents(".registerItem").find(".registerPrompt").find(".prompt").addClass("prompt2")
		return false
	}	
	 if(!isNaN(registerName))
	{
		objErrorShow("registerName")
		$("#registerName").parents(".registerItem").find(".registerPrompt").empty().html("<span class='prompt prompt2'>用户名不能全为数字</span>")
		return false
	}
    if(registerNameLen > 16)
  	{
		$("#registerName").parents(".registerItem").find(".registerPrompt").empty().html("<span class='prompt prompt2'>用户名太长，最多16个字符(1个汉字算2个字符)，不能全为数字</span>")
		return false
	}
	
	 if(registerPassword == "" ||registerPasswordLen < 3 )
	{	
		objErrorShow("registerPassword")
		$("#registerPassword").parents(".registerItem").find(".registerPrompt").empty().html('<span class="prompt prompt2">密码太短，至少6个字符</span>')
		return false
	}	
	
	 if(registerEmail == "" || !isEmail(registerEmail)    )
	{	
		objErrorShow("registerEmail")
		$("#registerEmail").parents(".registerItem").find(".registerPrompt").empty().html('<span class="prompt prompt2">不正确的邮箱格式</span>')
		return false
	}	
	 if(registerCode == "" )
	{	
		objErrorShow("registerEmail")
		$("#registerCode").parents(".registerItem").find(".registerPrompt").empty().html('<span class="prompt prompt2">请输入验证码</span>')
		return false
	}	
	var check = $('#jsinputChecked').attr("checked") ? true : false;
	if(check)
	{
		return true;
	}
	else
	{
		return false;
	}	
	
} 

$("#registerName").focus(function(){
	$("#registerName").parents(".registerItem").find(".registerPrompt").empty().html('<span class="prompt">用户名为3-16个字符(汉字8个以内--1个汉字算2个字符)，不能全为数字</span>')
	objReset("registerName")
})
.blur(function(){
	var registerName = $("#registerName").val();
		registerName = registerName.replace(/\s+/g,""); 
		var registerNameLen = getByteLen(registerName)
		
	if(registerNameLen < 3)
	{
		objErrorShow("registerName")
		$("#registerName").parents(".registerItem").find(".registerPrompt").empty().html("<span class='prompt prompt2'>用户名太短，至少3个字符(1个汉字算2个字符)，不能全为数字</span>")
	}
	else if(!isNaN(registerName))
	{
		objErrorShow("registerName")
		$("#registerName").parents(".registerItem").find(".registerPrompt").empty().html("<span class='prompt prompt2'>用户名不能全为数字</span>")
	}
	else if(registerNameLen >=3 && registerNameLen<=16 && isNaN(registerName))
	{
		objRightShow("registerName")
	}
	else if(registerNameLen > 16)
	{
		objErrorShow("registerName")
		$("#registerName").parents(".registerItem").find(".registerPrompt").empty().html("<span class='prompt prompt2'>用户名太长，最多16个字符(1个汉字算2个字符)，不能全为数字</span>")
	}
	
})





$("#registerPassword").focus(function(){
		objReset("registerPassword")
	$("#registerPassword").parents(".registerItem").find(".registerPrompt").empty().html('<span class="prompt">密码为6-16位字符，请尽量不要使用过于简单的密码</span>')
	
})


.blur(function(){
	var registerPassword = $("#registerPassword").val();
		registerPassword = registerPassword.replace(/\s+/g,""); 
	
	if(registerPassword.length >=6 && registerPassword.length<=16)
	{
		objRightShow("registerPassword")
	}
	else if(registerPassword.length < 6)
	{
		objErrorShow("registerPassword")
		$("#registerPassword").parents(".registerItem").find(".registerPrompt").empty().html('<span class="prompt prompt2">密码太短，至少6个字符</span>')
	}
	
})

$("#registerEmail").focus(function(){
$("#registerEmail").parents(".registerItem").find(".registerPrompt").empty().html('<span class="prompt">请填写真实的邮箱，以便找回密码时使用</span>')
	objReset("registerEmail")
})
.blur(function(){
	var registerEmail = $("#registerEmail").val();
		registerEmail = registerEmail.replace(/\s+/g,""); 
	if(registerEmail == "" || !isEmail(registerEmail)    )
	{	
		objErrorShow("registerEmail")
		$("#registerEmail").parents(".registerItem").find(".registerPrompt").empty().html('<span class="prompt prompt2">不正确的邮箱格式</span>')
	}
	else if(isEmail(registerEmail))
	{
		objRightShow("registerEmail")
	}	
})


function getByteLen(val) {//一个汉字算两个字符
	var len = 0;
	for (var i = 0; i < val.length; i++) {
		 var a = val.charAt(i);
		 if (a.match(/[^\x00-\xff]/ig) != null) 
		{
			len += 2;
		}
		else
		{
			len += 1;
		}
	}
	return len;
}

function isEmail(val){//判断邮箱地址是否有效
 var reEmail= /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
 return(reEmail.test(val));
}

function objErrorShow(obj){
		$("#"+obj).parents(".registerItem").addClass("registerErrorItem")
		.end().siblings(".error").show().siblings(".correct").hide()
		return false
}

function objRightShow(obj){
	$("#"+obj).parents(".registerItem").removeClass("registerErrorItem")
	.end().siblings(".error").hide().siblings(".correct").show()
}

function objReset(obj){
	$("#"+obj).parents(".registerItem").removeClass("registerErrorItem")
	.end().siblings(".error").hide().siblings(".correct").hide()	
}

/**邮件联想****/
String.prototype.replaceAll = function(f, d, e) {
	if (!RegExp.prototype.isPrototypeOf(f)) {
		return this.replace(new RegExp(f, (e ? "gi": "g")), d)
	} else {
		return this.replace(f, d)
	}
};


function htmlEncode(b) {
	strData = b.replaceAll("&", "&amp;");
	strData = strData.replaceAll("<", "&lt;");
	strData = strData.replaceAll(">", "&gt;");
	strData = strData.replaceAll("&apos;", "&apos;");
	strData = strData.replaceAll('"', "&quot;");
	return strData
}
function contResult() {
	var c = $("#emailct");
	var d = $(".jsemaiAssociate");
	d.keyup(function(a) {
		var e = a.keyCode;
		//alert(e)
		var b = c.find("li.hover");
	
		if (e == 40) {
			if (b.length == 1) {
				if (b.next().length == 1) {
					b.removeClass("hover").next().addClass("hover")
				}
			} else {
				c.find("li").first().addClass("hover")
			}
		} else {
			if (e == 38) {
				if (b.length == 1 && b.prev().length == 1) {
					b.removeClass("hover").prev().addClass("hover")
				}
			} else {
				if (e == 13) {
					if (b.length == 1) {
						a.preventDefault();
						var h = b.html();
						h = h.substring(5, h.length - 6);
						d.val(h);
						$(this).val(h);
						c.hide();
						$(".jsemaiAssociate").focus()
						$(".jsemaiAssociate").blur()
					} else {
						$(this).change()
					}
				} else {
					$(this).change()
				}
			}
		}
	}).change(function(b) {
		var i = $(this).val();
		if (i.match("@") != null || i.length < 1) {
			c.hide();
			return
		}
		var k = $(this).attr("last");
		if (i != 0 && i != k) {
			var e = ["sina.com", "qq.com", "163.com", "126.com", "sohu.com", "hotmail.com", "yahoo.com.cn", "gmail.com", "139.com", "foxmail.com", "189.cn"];
			var l = "";
			for (var a = 0; a < e.length; a++) {
				l += "<li><pre>" + htmlEncode(i) + "@" + e[a] + "</pre></li>"
			}
			$(this).attr("last", i);
			c.html(l).show(function() {
				$("#emailct li").mouseenter(function(f) {
					$(this).addClass("hover");
					$("#emailct li").not(this).removeClass("hover")
				}).click(function(f) {
					var h = $(this).html();
					h = h.substring(5, h.length - 6);
					d.val(h);
					c.hide();
					$(".jsemaiAssociate").focus()
					$(".jsemaiAssociate").blur()
					
				})
			})
		}
	})
}


$(function(){
	contResult()
	/**上下居中**/
	var boxMiddleHeight =  $("#js_boxMiddle").outerHeight(true)	
	var divTopHeight = $(".top").outerHeight(true)	
	var winHeight2 = $(window).height()
	if(boxMiddleHeight + divTopHeight < winHeight2 )
	{
		$("#js_boxMiddle").css({
			"margin-top":(winHeight2-divTopHeight -boxMiddleHeight)*0.5 -divTopHeight
		})
	}
	 $(window).resize(function(){
		 winHeight2 = $(window).height()
		if(boxMiddleHeight + divTopHeight < winHeight2 )
		{
			$("#js_boxMiddle").css({
				"margin-top":(winHeight2-divTopHeight -boxMiddleHeight)*0.5 -divTopHeight
			})
		}
		
			if(boxMiddleHeight + divTopHeight > winHeight2 )
			{
				$("#js_boxMiddle").css({
					"margin-top":0
				})
			}
	 })
	
	/***输入框****/
		$(".inputText").each(function(index, element) {
			$(this).focus(function(){
				$(this).prev(".label").hide()
			}).blur(function(){
				if($(this).val() == "")
				{
					$(this).prev(".label").show()
				}
			})
			if($(this).val() != "")
			{
				$(this).prev(".label").hide()
			}	
		});
	
	
	/**其他分站**/
	$("#js_fen").hover(function(){
		$(".fenzhantc").show()
	},function(){
		$(".fenzhantc").hide()
	})
	$(".fenzhantc").hover(function() {
		$(this).show();
	},function(){
		$(this).hide();
	});	

})