window.onload = function() {
	$('body').show();
	ifLoginRegister(function(flag) {
		
	},'register.html');
}

$(document).ready(function() {
	var toURL = getPar('toURL');
	$('#txt_phone').on('focus', function() {

		$('.input_1').removeClass('border_line_nofocus');
		$('.input_1').addClass('border_line_focus');

		$('.input_2').removeClass('border_line_focus');
		$('.input_2').addClass('border_line_nofocus');
	})

	$('#txt_msg').on('focus', function() {

		$('.input_1').removeClass('border_line_focus');
		$('.input_1').addClass('border_line_nofocus');

		$('.input_2').removeClass('border_line_nofocus');
		$('.input_2').addClass('border_line_focus');

	})
	var timer = null;
	$('.btn_send_msg').on('click', function() {
		var time = 60;
		var _phone = $('#txt_phone').val();
		if(_phone.trim().length != 11) {
			$('#txt_phone').focus();
			return;
		}
		$('#txt_msg').focus();
		var _data = {
			phone: _phone
		}
		ZZLog(base_api + 'user/login/send-message', JSON.stringify(_data));
		$.ajax({
			type: "post",
			url: base_api + 'user/login/send-message',
			data: _data,
			dataType: "json",
			success: function(res_data) {
				ZZLog(res_data);
				if(res_data['flag'] == false) {
					if(codeNotLogin == res_data['code']) {
						var URL = '/web/fx/register.html';
						URL = encodeURI(URL);
						location.href = res_data['url'] + URL;
					} else if(codeNotRegister == res_data['code']) {
						location.href = 'register.html';
					} else {
						alert(res_data['msg']);
					}
				} else {
					//..
				}
			},
			error: function() {
				time = 0;
				$('.btn_send_msg').text('发送验证码');
				clearInterval(timer);
				timer = null;
				alert(infoApiError);
			}
		});

		if(timer) {
			return;
		}
		$(this).text(time + 's后重试')
		timer = setInterval(function() {
			time--;
			$('.btn_send_msg').text(time + 's后重试');
			if(time <= 0) {
				$('.btn_send_msg').text('发送验证码');
				clearInterval(timer);
				timer = null;
			}
		}, 1000);

	})

	$('.btn_submit').on('click', function() {
		var _phone = $('#txt_phone').val();
		var _msg = $('#txt_msg').val();
		if(_phone.trim().length != 11) {
			$('#txt_phone').focus();
			return;
		}
		if(_msg.trim().length <= 0) {
			$('#txt_msg').focus();
			return;
		}
		var _data = {
			phone: _phone,
			code: _msg
		}
		ZZLog(base_api + 'user/login/check-code', JSON.stringify(_data));
		$.ajax({
			type: "post",
			url: base_api + 'user/login/check-code',
			data: _data,
			dataType: 'json',
			success: function(res_data) {
				ZZLog(res_data);
				if(false == res_data['flag']) {
					if(codeNotLogin == res_data['code']) {
						location.href = res_data['url'];
//					} else if(codeNotRegister == res_data['code']) {
//						location.href = 'register.html';
					} else {
						alert(res_data['msg']);
					}
				} else {
					location.href = toURL?toURL:"reserve_prod.html";
				}
			},
			error: function() {
				alert(infoApiError);
			}
		});
	})
})