angular.module('app', []).controller('AppCtrl', function($rootScope, $scope, $http, $timeout) {
	$scope.std = {
			std_pass : '',
			exam_id : '',
			std_name : '',
			std_sex : '',
			cert_type : '',
			cert_id : '',
			std_param3 : '',
			std_school : '',
			std_mobile1 : '',
			std_mobile2 : '',
			link_addr : '',
			from_school : ''
	};
	$scope.msg = function(msg, title, callback) {
		var msgTitle = '提示消息';
		if (title !== undefined) {
			var type = typeof (title);
			if (type == 'string' && title.length > 0)
				msgTitle = title;
			else if (type == 'function') {
				callback = title;
			}
		}

		if (callback == undefined)
			callback = function() {

			}
		var d = dialog({
			title : msgTitle,
			width : '20em',
			content : msg,
			cancel : false,
			ok : callback,
			cancelDisplay : false,
			okValue : '确 定'
		});
		d.showModal();
	}
	
	$scope.goLogin = function() {
		//window.location = '../reg/std_reg.jsp';
		window.location = '../login/login.jsp';
	}
	
	//上传照片页面
	$scope.goUploadPhoto = function(std) {
		var std_id = $scope.std.StdReg.std_id;
		window.location = '../reg/std_upload_img.jsp?std_id='+std_id;
	}
	
	//公告页面
	$scope.goNotice = function() {
		window.location = '../reg/notice.jsp';
	}
	
	$scope.isMobile = function(v) {
		// var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
		// 简单点
		var regex = /(^1[3|4|5|6|7|8]\d{9}$)|(^09\d{8}$)/;
		if (!regex.test(v)) {
			return false;
		}
		return true;
	}
	$scope.submitReg = function() {
		if (!$scope.std.std_pass || $.trim($scope.std.std_pass).length == 0) {
			$scope.msg('密码不能为空！');
			return;
		}
		if (!$scope.std.std_name || $.trim($scope.std.std_name).length == 0) {
			$scope.msg('姓名不能为空！');
			return;
		}
		if (!$scope.std.std_sex || $.trim($scope.std.std_sex).length == 0) {
			$scope.msg('性别不能为空！');
			return;
		}
		if (!$scope.std.cert_type || $.trim($scope.std.cert_type).length == 0) {
			$scope.msg('证件类型不能为空！');
			return;
		}
		if (!$scope.std.cert_id || $.trim($scope.std.cert_id).length == 0) {
			$scope.msg('证件号码不允许为空！');
			return;
		}
		if (!$scope.std.std_param3 || $.trim($scope.std.std_param3).length == 0) {
			$scope.msg('报考类别不能为空！');
			return;
		}
		if (!$scope.isMobile($scope.std.std_mobile1)) {
			$scope.msg('请输入正确的联系电话(11位手机号)！');
			return;
		}
		if (!$scope.isMobile($scope.std.std_mobile2)) {
			$scope.msg('请输入正确的备用电话(11位手机号)！');
			return;
		}
		if ($scope.std.std_mobile2 == $scope.std.std_mobile1) {
			$scope.msg('备用电话和联系电话必须不一样！');
			return;
		}
		if ($scope.std.link_addr == undefined || $.trim($scope.std.link_addr).length == 0) {
			$scope.msg('通信地址不允许为空！');
			return;
		}
		$scope.std.from_school = window.location.href.replace('http://', '').split('.')[0];
		$.ajax({
			url : '../../std/reg/register.htm',
			type : "POST",
			dataType : "json",
			data : $scope.std,
			success : function(data) {
				if (data.errorCode == 0) {
					$scope.msg('注册成功！');
					$scope.std = data.map;
					var href = window.location.href.replace('http://', '').split('.')[0];	
					//$scope.goUploadPhoto($scope.std);  //上传照片
					//判断如果是交通职院则看公告，否则不看
					if(href ==8190){
						$scope.goNotice();  //公告
					}
					
				} else {
					$scope.msg(data.errorMsg);
				}
	
			},
			error : function(err) {
				$scope.msg(err);
				return;
			},
			complete : function(data) {
				$.LoadingOverlay("hide");
			}
		});
	}
	
	
	$scope.getSchoolId = function() {
		var href = window.location.href.replace('http://', '').split('.')[0];
		// href = 7157;
		if (href == "127")
			href = 1000;
		$scope.std.from_school = href;
		/*$.ajax({
			url : '../../enrol/std/school.htm',
			type : "POST",
			dataType : "json",
			data : {
				school_id : href
			},
			success : function(data) {
				$timeout(function() {
					$scope.school = data.entity;
				}, 100);
			}
		});*/
	}
	$scope.getSchoolId();	
	
	
});

