var Pur5 = (function(){
	var _userId,
		_userPwd,
		_token,
		_code,
		_serverIp,
		_serverPath,
		
		_debug = 1,

		_Ax = new ActiveXObject("pur5Util.pur5UtilAx.1"),
		_mac = (function(){
			try{
				return _Ax.Mac.replace(/-/g, ':').toUpperCase();
			}catch(e){
				return '';
			}
		})();
		
	return {
		startTime: new Date().getTime(),
		login: function(userId, userPwd, serverIp, callback){
			var self = this;
			
			if(serverIp) _serverIp = serverIp;
			
			this.getCode(function(){
				$.ajax({
					url: self.makeFullPath('login_do.asp'),
					cache: false,
					type: 'post',
					data: {
						act: 'login',
						name: encodeURIComponent(userId),
						pwd: encodeURIComponent(md5(_code + md5(userPwd))),
						mac: md5(_code + _mac),
						macB: md5(_mac)
					},
					success: function(result){
						var	data = result.split(':');
						
						if(result.indexOf(':') > -1 && data[0] == userId && data[1].length === 32){
							_userId = data[0];
							_token = data[1];
							callback & callback();
						}else{
							Alert('登录失败：' + data[4]);
						}
					},
					error: function(xhr){
						Alert('登录错误：' + xhr.responseText);
					}
				});
			});
		},
		getCode: function(callback){
			var self = this;
			
			$.ajax({
				url: self.makeFullPath('getCode.asp'),
				cache: false,
				success: function(result){
					_code = result;
					callback && callback();
				},
				error: function(xhr){
					Alert('getCode错误：' + gb2utf8(xhr.responseBody));
				}
			});
		},
		get: function(arg, callback){
			$.ajax({
				url: this.makeFullPath('getAjax.asp'),
				method: 'get',
				dataType: 'json',
				data: $.extend({}, arg, {
					token: (_token || pur5.Cfg.token)
				}),
				success: function(result, textStatus, jqXhr){
					if(result){
						callback(result.msg);
					}else{
						callback(false);
					}
				},
				error: function(jqXhr, textStatus){
					try{
						if(jqXhr.responseText.indexOf('请重新登陆！') > -1){
							alert('软件登录超时，请重新登陆！');
							return false;
						}
					}catch(e){}
					callback(false, ajaxError('读取数据'+(arg.action?arg.action:''), jqXhr, textStatus));
				}
			});
		},
		makeFullPath: function(page){
			
			return pur5.Cfg.GBHOSTSOFT + page;
			
			return str_replace(
				'http://{1}/pages/{2}',
				_serverIp,
				page
			);
		},
		getToken: function(){
			return _token || pur5.Cfg.token;
		},
		getUserId: function(){
			return _userId || pur5.Cfg.pur5Uid;
		}
	}
})();