

window.alert = function(message, callback, options)
{
	window.addEvent('domready', function() {
		DialogBox.alert(message, '确定', callback, $extend({title: '系统提示信息', autoclose: true}, options || {}));
	});
};

window.confirm = function(message, after)
{
	window.addEvent('domready', function() {
		DialogBox.confirm(message, '确定', '取消', after, {title: '系统提示信息'});
	});
};

//询问模式：删除按钮用
window.ask = function(message, btnAsk, after, x, y)
{
	window.addEvent('domready', function() {
		DialogBox.ask(message, btnAsk, '取消', after, {x: x, y: y});
	});
};

//下载页面信息并显示
window.ajaxDialog = function(title, url, behavior, options)
{
	window.addEvent('domready', function() {
		//防止缓存
		if(!url.contains('?')){
			url += '?';
		}
		else {
			url += '&';
		}
		url += 'rand=' + Math.random() + '&timestamp=' + new Date().getTime();

		DialogBox.load( url, $extend({title: title, fixed: false, behavior : behavior || $empty}, options || {}));
	});
};

//显示普通文本框
window.showDialog = function(title, content, options)
{
	window.addEvent('domready', function() {
		DialogBox.show(content, $extend({title: title}, options || {}));
	});
};

//显示右下角的提示信息
window.showNotice = function(title, content)
{
	window.addEvent('domready', function() {
		if(!title || title.trim() == '') {
			title = '提示';
		}
		new noticebox(title, content).show();
	});
};

CS.implement({
	showLoginDialog : function(afterLogin) {
		ajaxDialog('登录', '/web/system/login.aspx', afterLogin, {
			fixed: true,
			afterShow : function(){
				try{
					$('loginPage').getElement('input[name=txtEmail]').focus();
				}catch(e){}
			}
		});
	},
	
	showFlagDialog : function(url) {
		ajaxDialog('举报', url, null, {
			fixed: true,
			afterShow : function(){
				try{
					$('flagPost').getElementById('txtFlagContent').focus();
				}catch(e){}
			}
		});
	},
	

	showEditFriendDialog : function(url) {
		ajaxDialog('设置朋友类别', url, null, {
			fixed: true,
			afterShow : function(){
				try{
					$('edit_friend').getElementById('btnSubmit').focus();
				}catch(e){}
			}
		});
	},

	//弹出式登录对话框用:弹出式对话框和页面上的登录框共用,dialog非空则弹出式
	login : function(dialog, containter) {
		//验证
		var message = (dialog == null ? '登录邮箱必须填写' : '');
		if(!checkNotNull(containter.getElement('input[name=txtEmail]'), message))
		{
			if(dialog != null)
			{
				containter.getElement('div[name=loginErrorMsg]').set('html','登录邮箱必须填写').warning();
				setFocus(containter.getElement('input[name=txtEmail]'));
			}

			return false;
		}
		
		message = (dialog == null ? '登录邮箱无效!' : '');
		if(!checkEmail(containter.getElement('input[name=txtEmail]'), message))
		{
			if(dialog != null)
			{
				containter.getElement('div[name=loginErrorMsg]').set('html','登录邮箱无效!').warning();
				setFocus(containter.getElement('input[name=txtEmail]'));
			}

			return false;
		}
		
		message = (dialog == null ? '密码必须填写' : '');
		if(!checkNotNull(containter.getElement('input[name=txtPassword]'), message))
		{
			if(dialog != null)
			{
				containter.getElement('div[name=loginErrorMsg]').set('html','密码必须填写').warning();
				setFocus(containter.getElement('input[name=txtPassword]'));
			}

			return false;
		}
		
		//ajax loading 开始
		AjaxLoading.show(containter);
		//设置登录按钮不可用
		var btnlogin = containter.getElement('input[name=btnLogin]');
		btnlogin.disabled = true;

		ChinSoftProject.Ajax.AjaxServer.Login(
			containter.getElement('input[name=txtEmail]').value,
			containter.getElement('input[name=txtPassword]').value,
			containter.getElement('input[name=cbRememberMe]').checked, function(data){
			
			var resp = data.value;

			if(resp.Code == resp.SUCCESS)
			{
				if(dialog != null) {
					dialog.hide();
					dialog.options.behavior();
				}
				else
				{
					chinsoft.refreshPage();
				}
			}
			else
			{
				//设置登录按钮可用
				btnlogin.disabled = false;
				
				//显示错误信息
				if(dialog != null)
				{
					containter.getElement('div[name=loginErrorMsg]').set('html', resp.Message).warning();
				}
				else
				{
					alert(resp.Message);
				}
			}

			//ajax loading 结束
			AjaxLoading.hide(containter);
		});

		return true;
	},

	deleteUploadFile : function(event, delFileIDs, fileID) {
		var event = new Event(event);
		
		ask('您确定要删除当前的信息吗?', '确定', function() { 
				$(delFileIDs).set('value', $(delFileIDs).value + ',' + fileID);
				$$('.fileupload tr[id=' + fileID + ']').dispose();
			}, 
			event.page.x,
			event.page.y
		);
	},

	searchTopic : function()
	{
		var txtTopics = $('searchbar').getElement('input[id$=txtTopics]');
		if(!checkNotNull(txtTopics, '关键字必须填写'))
		{
			return false;
		}

		top.location.href = '/search/1.aspx?k='+ encodeURIComponent(txtTopics.value.replace('<', '').replace('>', ''));
	},
	
	//电ta
	poke : function()
	{
		ChinSoftProject.Ajax.AjaxServer.Poke($('memberid').value, function(data){
			var resp = data.value;
			
			if(resp.Code == resp.SUCCESS)
			{
				alert('成功放电，如果对方对你感兴趣，可能会回电电你奥');

				//登录后的处理：默认刷新页面
				chinsoft.fireEvent('onAfterLogin');
			}
			else if(resp.Code == '0009')//未登录
			{
				chinsoft.showLoginDialog(chinsoft.poke);
			}
			else //其他错误
			{
				alert(resp.Message);
			}
		});

		return false;
	},
	
	//加为好友
	addFriend : function()
	{
		ChinSoftProject.Ajax.AjaxServer.AddFriend($('memberid').value, function(data){
			var resp = data.value;

			if(resp.Code == resp.SUCCESS)
			{
				alert('您已成功加对方为好友，请等待对方的审核');

				//登录后的处理：默认刷新页面
				chinsoft.fireEvent('onAfterLogin');
			}
			else if(resp.Code == '0009')
			{
				chinsoft.showLoginDialog(chinsoft.addFriend);
			}
			else
			{
				alert(resp.Message);
			}
		});

		return false;
	},

	//加入圈子
	joinGroups : function(groupsID)
	{
		ChinSoftProject.Ajax.AjaxServer.JoinGroups(groupsID, function(data){
			var resp = data.value;

			if(resp.Code == resp.SUCCESS)
			{
				//刷新当前页面
				chinsoft.refreshPage();
			}
			else if(resp.Code == '0009')//未登录
			{
				chinsoft.showLoginDialog(chinsoft.joinGroups.pass([groupsID]));
			}
			else //其他错误
			{
				alert(resp.Message);
			}
		});

		return false;
	},

	leaveGroups : function(groupsID)
	{
		ChinSoftProject.Ajax.AjaxServer.LeaveGroups(groupsID, function(data){
			var resp = data.value;
			
			if(resp.Code == resp.SUCCESS)
			{
				//刷新当前页面
				chinsoft.refreshPage();
			}
			else if(resp.Code == '0009')//未登录
			{
				chinsoft.showLoginDialog(chinsoft.leaveGroups.pass([groupsID]));
			}
			else //其他错误
			{
				alert(resp.Message);
			}
		});

		return false;
	}
});

chinsoft.init(false);
    