//底部二手房分站显示
$(function(){
	$("#show_fen_sell").bind("mouseover",function(){
		$("#fen_sell_tc").show();
	}).bind("mouseout",function(){
		$("#fen_sell_tc").hide();
	});
	$("#fen_sell_tc").bind("mouseover",function(){
		$(this).show();
	}).bind("mouseout",function(){
		$(this).hide();
	});
	$("#show_fen_rent").bind("mouseover",function(){
		$("#fen_rent_tc").show();
	}).bind("mouseout",function(){
		$("#fen_rent_tc").hide();
	});
	$("#fen_rent_tc").bind("mouseover",function(){
		$(this).show();
	}).bind("mouseout",function(){
		$(this).hide();
	});
	
	
	
	
	
	
	
	
	$(".js_intextInput").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();
		else
			$(this).prev(".label").show();	
	});
	
	
	
	
	
	
/*
	
	$("#name").blur(function(){
				if(this.value=='')
				{
					this.value='请输入登录名';this.className='inputText'
				}
				else if(this.value !='请输入登录名')
				{
					this.className='inputText inputTextActive'
				}
	}).focus(function(){
				$("#errorInner").html(this.value);
				$("#errorBlock").hide();
				if(this.value=='请输入登录名')
				{
					this.value='';
					this.className='inputText inputTextActive'
				};
	}).change(function(){
			if(this.value !='请输入登录名')
				{
					this.className='inputText inputTextActive'
				}
	});
	
	
	$("#password").blur(function(){
			if(this.value=='')
			{
				this.className='inputText inputTextBg'
			}
	}).focus(function(){
			$("#errorInner").html("");
			$("#errorBlock").hide();
			this.className='inputText inputTextActive'
	}).change(function(){
				this.className='inputText inputTextActive'
	});
	
	
	$("#code").blur(function(){
			if(this.value=='')
			{
				this.value='请输入验证码';this.className='inputText inputText02'
			}
	}).focus(function(){
			$("#errorInner").html("");
			$("#errorBlock").hide();
			if(this.value=='请输入验证码')
			{
				this.value='';this.className='inputText inputText02 inputTextActive'
			}
	});
	$("#code").trigger("focus");
*/


$("#code,#password").focus(function(){
	if($(this).hasClass('inputError'))
	{
		$(this).select();
	}
})



	
$("#login_submit").click(function(){
	var name = $("#name").val();
	var pwd = $("#password").val();
	var code = $("#code").val();
	if(name == "")
	{
		$("#errorInner").html("请输入登录名");
		$("#errorBlock").show();
		$("#name").addClass("inputError");
	}
	else if(pwd == "")
	{
		$("#errorInner").html("请输入密码");
		$("#errorBlock").show();
		$("#password").addClass("inputError");
	}
	else if(code == "")
	{
		$("#errorInner").html("请输入验证码");
		$("#errorBlock").show();
		$("#code").addClass("inputError");
	}
	else
	{
		$.post("/login/",
			{action:'check_login', username:name, password:pwd, yzm:code},
			function(data){
				switch(data)
				{
					case 'check_ok':	window.location.href = '/login/enter/';
										break;
					case 'u_p_error':	$("#new_login_btn_none").hide();
										$("#errorInner").html("您输入的密码错误，请重试。");
										$("#errorBlock").show();
										$("#password").addClass("inputError");
										change_validecode('/valid/');
										$("#code").val('');
										break;
					case 'exp_error':	$("#new_login_btn_none").hide();
										$("#errorInner").html("您的账号已到期。");
										$("#errorBlock").show();
										$("#name").addClass("inputError");
										break;
					case 'vad_error':	$("#new_login_btn_none").hide();
										$("#errorInner").html("您的账号已禁用，请联系客服解决。");
										$("#errorBlock").show();
										$("#name").addClass("inputError");
										break;
					case 'yzm_error':	$("#new_login_btn_none").hide();
										$("#errorInner").html("验证码错误，请重试。");
										$("#errorBlock").show();
										$("#code").addClass("inputError");
										change_validecode('/valid/');
										break;
					default:			$("#new_login_btn_none").hide();
										$("#errorInner").html("登陆提交处理异常，请稍后重试。");
										$("#errorBlock").show();
										break;
				}
			}
		);
	}
	
})

$("#name,#password,#code,#verify_code,#telno").focus(function(){
		$("#errorInner").html("");
		$("#errorBlock").hide();
		$(this).removeClass("inputError");
})


// 手机号码正则
	var rule = /^1[3456789]{1}[0-9]{9}$/;
	// 是否可点击发送验证码
	var is_code_click=true;
	// 发送验证码
	$(".get-code-text").click(function (e) {
		if (is_code_click === false) {
			return;
		}
		var telno = $.trim($("#telno").val())
		if (!rule.test(telno)) {
			$("#errorInner").html('请输入有效的手机号码');
			$("#errorBlock").show();
			return;
		}
		// 实例
		new TencentCaptcha(document.getElementById("SendCodeCaptcha"));
		$("#SendCodeCaptcha").trigger('click');
	})

	// 倒计时
	function resetTime(e) {
		is_code_click = false;
		var second = 60;
		var timer = null;
		timer = setInterval(function () {
			second -= 1;
			if (second > 0) {
				$(e).attr('disabled', true);
				if (!$(e).hasClass('disable-text-color')) {
					$(e).addClass('disable-text-color');
				}
				$(e).text("重新获取(" + second + ")");
			} else {
				// 发送按钮可点
				is_code_click = true;
				clearInterval(timer);
				$(e).attr('disabled', false).removeClass('disable-text-color');
				$(e).text("重新获取");
			}
		}, 1000);
	}

	window.sendCodeCallback = function (res) {
		if (res.ret === 0) {
			var city = $(".js_select_v").val();
			var telno = $("#telno").val();
			// 复制结果至剪切板
			var postData = {
				Ticket: res.ticket,
				Randstr: res.randstr,
				BizState: res.bizState,
				CaptchaAppId: res.CaptchaAppId,
				city: city,
				telno: telno,
			};
			// 发验证码
			$.ajax({
				url: '/login/get_code',
				type: 'post',
				data: postData,
				dataType: 'json',
				success: function (ret) {
					if(ret.result == 1){
						// 设置倒计时
						resetTime($(".get-code-text"));
						layer.msg(ret.msg)
					}else{
						$("#errorInner").html(ret.msg);
						$("#errorBlock").show();
					}
				}
			});
		}
	};


	$("#telnoSubmitForm").click(function (){
		$("#errorInner").html('');
		$("#errorBlock").hide();
		var telno = $.trim($("#telno").val());
		var code = $.trim($("#verify_code").val());
		var city = $(".js_select_v").val();
		if (!rule.test(telno)) {
			$("#errorInner").html('请输入有效的手机号码');
			$("#errorBlock").show();
			$("#telno").addClass("inputError");
			return;
		}
		if (code == '') {
			$("#verify_code").addClass("inputError");
			$("#errorInner").html('请输入验证码');
			$("#errorBlock").show();
			return;
		}
		var postData = {
			login_source: "pc",
			city: city,
			telno: telno,
			code: code
		}
		loginAction(postData);
	});

	function loginAction(data){
		$.ajax({
			url: '/login/code_login',
			type: 'post',
			data: data,
			dataType: 'json',
			success: function (ret) {
				var accountList = ret.account_list;
				if(ret.account_list.length == 0){
					switch (ret.msg) {
						case 'success':
							layer.msg('登录成功');
							window.location.href = '/login/enter/';
							break;
						case 'exp_error':
							$("#errorInner").html("您的账号已到期。");
							$("#errorBlock").show();
							break;
						case 'vad_error':
							$("#errorInner").html("您的账号已禁用，请联系客服解决。");
							$("#errorBlock").show();
							break;
						default:
							$("#errorInner").html(ret.msg);
							$("#errorBlock").show();
							break;
					}
					return false;
				}
				// 帐户列表
				var popHtml = accountHtml(accountList);
				layer.open({
					type: 1,
					title: '选择帐户', //不显示标题
					skin: 'layer-skin-theme',
					area:['450px'],
					closeBtn: false,
					btn:["确 定", '取 消'],
					btnAlign: 'c', //按钮居中
					content: popHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
					yes: function () {
						var accountId = $("input[name='accountUid']:checked").val();
						if(!accountId){
							layer.msg("请选择一个帐户登录")
							return false
						}
						data.uid = accountId;
						// 重新调用登录接口
						loginAction(data);
						layer.closeAll();
					},
					cancel: function () {
						// close btn
					}
				});
				// 改标题样式
				$(".choose-account-list").parent(".layui-layer-content").siblings(".layui-layer-title").css({
					"font-size": "16px",
					"text-align": "center",
					"font-weight": "700",
					"padding": "0",
					"background-color":"#f57d3a",
					"color":"#FFF"
				})
				// 下面增加线条
				$(".choose-account-list").parent(".layui-layer-content").css({
					"border-bottom":"1px solid #eee"
				})
				// 改按钮样式
				$(".choose-account-list").parent(".layui-layer-content").siblings(".layui-layer-btn").find(".layui-layer-btn0").css({
					"background-color":"#f57d3a",
					"color":"#FFF",
					"border-color":"#f57d3a"
				})
			}
		});
	}

	function accountHtml(list) {
		var html= '<div class="choose-account-list"><ul>';
		var lens = list.length;
		for (var i=0;i<lens;i++){
			var curr = list[i];
			html += '<li>\n' +
					'            <div class="list-left">\n' +
					'                <div class="head-img"><img src="'+curr.pic+'" width="70" ></div>\n' +
					'                <div class="truename-bk">\n' +
					'                    <p class="p1">'+curr.truename+'</p>\n' +
					'                    <p>'+curr.username+'</p>\n' +
					'                </div>\n' +
					'            </div>\n' +
					'            <div class="list-right">\n' +
					'                <label class="radio"><input name="accountUid" class="uid-radio" type="radio" value="'+curr.uid+'"><i></i></label>\n' +
					'            </div>\n' +
					'        </li>';
		}
		html += '</ul></div>';
		return html;
	}
}); 


function formSubmit(){
/*	
if($("#name").val() == "" || $("#name").val() == "请输入登录名")
	{
		$("#errorInner").html("请输入登录名");
		$("#errorBlock").show();
		$("#name").addClass("inputError");
		return false
	}
	else if($("#password").val() == "")
	{
		$("#errorInner").html("请输入密码");
		$("#errorBlock").show();
		$("#password").addClass("inputError");
		return false
	}
	else if($("#code").val() == "" || $("#code").val() == "请输入验证码")
	{
		$("#errorInner").html("请输入验证码");
		$("#errorBlock").show();
		$("#code").addClass("inputError");
		return false
	}
*/
}

function showError($errormsg)
{
    switch($errormsg)
    {
        case 'u_p_error':   $("#errorInner").html("账号或密码错误，请确认后重新提交！");
                            $("#errorBlock").show();
                            //$("#name").addClass("inputError");
                            break;
                            
        case 'vad_error':
        case 'exp_error':   $("#errorInner").html("该账号已到期，请联系网站工作人员！");
                            $("#errorBlock").show();
                            //$("#name").addClass("inputError");
                            break;
                            
        case 'yzm_error':   $("#errorInner").html("验证码错误！");
                            $("#errorBlock").show();
                            $("#code").addClass("inputError");
                            break;
                            
        default:            $("#errorInner").html("登陆失败，请稍后再试！");
                            $("#errorBlock").show();
                            //$("#name").addClass("inputError");
                            break;
    }
}


function addBookmark() {

		var ctrl = (navigator.userAgent.toLowerCase()).indexOf('mac') != -1 ? 'Command/Cmd': 'CTRL';
		if (document.all) {
			window.external.addFavorite("http://nj.zsb.house365.com", "南京租售宝_365二手房网");
		} else if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) {
			window.sidebar.addPanel("南京租售宝_365二手房网", "http://nj.zsb.house365.com", "");
		} else {
			alert('您可以尝试通过快捷键' + ctrl + ' + D 加入到收藏夹~');
		}


    return false
}

function setHomepage(B) {
    if (document.all) {
        document.body.style.behavior = "url(#default#homepage)";
        document.body.setHomePage(B)
    } else {
        if (window.sidebar) {
            if (window.netscape) {
                try {
                    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect")
                } catch(C) {
                    alert("该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true")
                }
            }
            var A = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
            A.setCharPref("browser.startup.homepage", B)
        } else {
            alert("您所使用的浏览器暂不支持此功能.")
        }
    }
    return false
}



function toDesktop(F, D, C) {
    try {
        var B = new ActiveXObject("WScript.Shell");
        var A = B.CreateShortcut(B.SpecialFolders("Desktop") + "\\" + F + ".url");
        A.TargetPath = D;
        A.Save()
    } catch(E) {
        location.href = C
    }
    return false
}

function change_validecode(normalsrc)
{ 
	var src = normalsrc + '?' + Math.random();
	$('.imgY').attr('src',src);
}