var registerEnabled='false';
var validCodeEnabled='false';
var layer;
$(function() {
	layui.use(['layer'], function () {
		layer = layui.layer;
	});
	var login = new Login();
	login.init();
});
/**
 * 功能：移除首尾空格
 */
String.prototype.trim = function() {
	return this.replace(/(^[ \t\n\r]+)|([ \t\n\r]+$)/g, '');
};

var Login = function() {
};

Login.prototype = {
	init : function() {
		if (this.hasInit) // 是否已初始化
			return false;
		this.hasInit = true;
		this.handeEvent();
	
		if (registerEnabled == 'true') {
			$(".register").removeClass("hidden");
		} else {
			$(".register").addClass("hidden");
		}
		if (validCodeEnabled == 'true') {
			$("#validCodeFormGroup").removeClass("hidden");
		}

		$("#username").focus();
		this.handleFixInputPlaceholderForIE();
	},
	handeEvent : function() {
		var me = this;
		$('body').keydown(function(event) {// 处理回车事件
			if (event.keyCode == 13)
				me.login();
		});
		
		$(document).on('click', '#reset', function(e) {// 重置
			$('#loginForm')[0].reset();
		});
		$(document).on('click', '#login', function(e) {// 登录
			me.login();
		});
	},
	handleFixInputPlaceholderForIE : function() {
		var isIE8 = !!navigator.userAgent.match(/MSIE 8.0/), isIE9 = !!navigator.userAgent
				.match(/MSIE 9.0/), isIE10 = !!navigator.userAgent
				.match(/MSIE 10.0/);
		// fix html5 placeholder attribute for ie7 & ie8
		if (isIE8 || isIE9) { // ie8 & ie9
			$(
					'input[placeholder]:not(.placeholder-no-fix), textarea[placeholder]:not(.placeholder-no-fix)')
					.each(
							function() {
								var input = $(this);

								if (input.val() === ''
										&& input.attr("placeholder") !== '') {
									input.addClass("placeholder").val(
											input.attr('placeholder'));
								}

								input.focus(function() {
									if (input.val() == input
											.attr('placeholder')) {
										input.val('');
									}
								});

								input.blur(function() {
									if (input.val() === ''
											|| input.val() == input
													.attr('placeholder')) {
										input.val(input.attr('placeholder'));
									}
								});
							});
		}
	},
	/**
	 * 登录
	 */
	login : function() {
		var username = $("#username").val(), password = $("#password").val();

		if (username == "") {
			layer.msg("用户名或者密码为空");
			$("#username").focus();
			return;
		}

		if (registerEnabled == 'true'
				&& !(/^((\(\d{2,3}\))|(\d{3}\-))?1\d{10}$/
						.test(username.trim()))) {
			layer.msg("请使用手机号码登录");
			$("#username").focus();
			throw new error("请使用手机号码登录");
		}

		if (password == "") {
			layer.msg("用户名或者密码为空");
			$("#password").focus();
			return;
		}
		var _this = this;
		var loading = layer.load(0, {
			shade : 0.3
		});
		
		var param = {
			loginName : username,
			password : password,
			validCode : $("#validCode").val(),
			'rememberme' : $('#rememberMe').is(':checked') ? 'on' : 'off'
		};
		
		$.post(ctx + "/home!doLogin.do",
				param, 
				function(data) {
					
					if (data.result == 1) {
						var __redirectUri=data.redirectUri;
						
						if(undefined == __redirectUri || null == __redirectUri || "" == __redirectUri||typeof(__redirectUri)=='undefined'){
							window.location.href = ctx;
						}else{
							console.log("redirect to " + __redirectUri);
							window.location.href = __redirectUri;
						}
						
					} else if (data.result == 2) {

						layer.close(loading);
						param.kickout = false;
						DialogUtil.confirm(data.message, function(rtn) {
							if (rtn){

								param.kickout = true;
								$.post(ctx + "/login.htm",
										param, 
										function(data) {
											param.kickout = false;
											layer.close(loading);
											if (data.result == 1) {
												window.location.href = ctx
														+ "/platform/console/main.htm";
											} else {
												layer.close(loading);
												if (data.validCode) {
													$("#validCodeFormGroup").removeClass("hidden");
												}
												layer.msg(data.message);
											}
								}, "json");

							}

						});
						
					}
					else {
						layer.close(loading);
						layer.msg(data.message);
					}
					
				}, "json");

		
	}

};