﻿$(document).ready(function() {
			bindEvent();
		});

function refreshVertifyCode() {
	var ticks = new Date().getTime();
	$("#imgVertify").attr("src", "servlet/vertify?vertifyKey=" + ticks);
	$("#vertifyKey").val(ticks);
}

function bindEvent() {
	
	$("#reallyConts_0").keypress(function(){
		if(event.keyCode==13){
		      $("#corp_login").trigger("click");
		}

	});

	$("#vertifyCode").focus(function(){
		if($("#imgVertify").css("display")=="none"){
			refreshVertifyCode();
			$("#imgVertify").show();
		}
	});

	// refreshVertifyCode();

	$("#imgVertify").click(refreshVertifyCode);

	$("#corp_login").click(function() {

				var account = $("#account").val();
				var password = $("#password").val();
				var vertifyCode = $("#vertifyCode").val();
				var vertifyKey = $("#vertifyKey").val();
				if (account == "") {
					alert("请输入邮箱/叮当号/会员名!");
					$("#account").focus();
					return;
				}

				if (password == "") {
					alert("请输入密码!");
					$("#password").focus();
					return;
				}

				if (vertifyCode == "") {
					alert("请输入校验码!");
					$("#vertifyCode").focus();
					return;
				}

				doLogin({
							"account" : account,
							"password" : password,
							"vertifyCode" : vertifyCode,
							"vertifyKey" : vertifyKey
						});

			});
}

function doLogin(params) {
	$("#tips").show();
	$("#tips").html("正在登录...");
	$.ajax({
				type : "POST",
				success : onSuccess_validate,
				error : onError_validate,
				timeout : 30000,
				data : params,
				url : "servlet/login",
				dataType : "json"
			});
}

function onError_validate(XMLHttpRequest, textStatus, errorThrown) {
	var errorStr = ''
	if (textStatus == "timeout") {
		errorStr = "网络超时!";
	} else if (textStatus == "parsererror") {
		errorStr = "服务器出错!请联系管理员!";
	} else {
		errorStr = "处理失败!";
	}
	$("#tips").html(errorStr);
}

function onSuccess_validate(result) {
	if (!result.success) {
		$("#tips").css("color", "red");
		switch (result.error) {
			case 0 :
				refreshVertifyCode();
				$("#tips").html("校验码已失效!");
				$("#vertifyCode").val("");
				$("#vertifyCode").focus();
				break;
			case 1 :
				refreshVertifyCode();
				$("#tips").html("校验码错误!");
				$("#vertifyCode").val("");
				$("#vertifyCode").focus();
				break;
			case 2 :
				$("#tips").html("请输入邮箱/叮当号/会员名!");
				$("#account").val("");
				$("#account").focus();
				break;
			case 3 :
				$("#tips").html("请输入密码!");
				$("#password").val("");
				$("#password").focus();
				break;
			case 4 :
				$("#tips").html("邮箱/叮当号/会员名错误!");
				refreshVertifyCode();
				$("#password").val("");
				$("#vertifyCode").val("");
				$("#account").val("");
				$("#account").focus();
				break;
			case 5 :
				$("#tips").html("密码错误!");
				$("#password").val("");
				$("#password").focus();
				break;
			case 6 :
			case 7 :
				$("#tips").html("接口调用出错,请联系管理员!");
				$("#password").val("");
				$("#vertifyCode").val("");
				$("#account").val("");
				break;
		}
	} else {
		$("#tips").css("color", "green");
		$("#tips").html("登录成功!");
		window.location.href= result.result.url;
	}
}
