

window.alert = function(message, callback, options)
{
	window.addEvent('domready', function() {
		DialogBox.alert(message, 'OK', callback, $extend({title: 'System information', autoclose: true}, options || {}));
	});
};

window.confirm = function(message, after)
{
	window.addEvent('domready', function() {
		DialogBox.confirm(message, 'OK', 'Cancel', after, {title: 'System information'});
	});
};

//询问模式：删除按钮用
window.ask = function(message, btnAsk, after, x, y)
{
	window.addEvent('domready', function() {
		DialogBox.ask(message, btnAsk, 'Cancel', 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 = 'Notice';
		}
		new noticebox(title, content).show();
	});
};

CS.implement({
	showLoginDialog : function(afterLogin) {
		ajaxDialog('Sign In', '/web/system/login.aspx', afterLogin, {
			fixed: true,
			afterShow : function(){
				try{
					$('loginPage').getElement('input[name=txtEmail]').focus();
				}catch(e){}
			}
		});
	},
	
	showFlagDialog : function(url) {
		ajaxDialog('Flag It', url, null, {
			fixed: true,
			afterShow : function(){
				try{
					$('flagPost').getElementById('txtFlagContent').focus();
				}catch(e){}
			}
		});
	},
	
	showEditAlbumDialog : function(url) {
		ajaxDialog('Edit Album', url, null, {
			fixed: true,
			afterShow : function(){
				try{
					$('edit_album').getElementById('txtAlbumName').focus();
					ShowAccessAuthority();
				}catch(e){}
			}
		});
	},

	showEditFriendDialog : function(url) {
		ajaxDialog('Set Category', url, null, {
			fixed: true,
			afterShow : function(){
				try{
					$('edit_friend').getElementById('btnSubmit').focus();
				}catch(e){}
			}
		});
	},

	//弹出式登录对话框用:弹出式对话框和页面上的登录框共用,dialog非空则弹出式
	login : function(dialog, containter) {
		//验证
		var message = (dialog == null ? 'Email required' : '');
		if(!checkNotNull(containter.getElement('input[name=txtEmail]'), message))
		{
			if(dialog != null)
			{
				containter.getElement('div[name=loginErrorMsg]').set('html','Email required').warning();
				setFocus(containter.getElement('input[name=txtEmail]'));
			}

			return false;
		}
		
		message = (dialog == null ? 'Email is invalid!' : '');
		if(!checkEmail(containter.getElement('input[name=txtEmail]'), message))
		{
			if(dialog != null)
			{
				containter.getElement('div[name=loginErrorMsg]').set('html','Email is invalid!').warning();
				setFocus(containter.getElement('input[name=txtEmail]'));
			}

			return false;
		}
		
		message = (dialog == null ? 'Password required' : '');
		if(!checkNotNull(containter.getElement('input[name=txtPassword]'), message))
		{
			if(dialog != null)
			{
				containter.getElement('div[name=loginErrorMsg]').set('html','Password required').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('Are you sure you want to delete current item ?', 'OK', function() { 
				$(delFileIDs).set('value', $(delFileIDs).value + ',' + fileID);
				$$('.fileupload tr[id=' + fileID + ']').dispose();
			}, 
			event.page.x,
			event.page.y
		);
	},

	checkTopics : function()
	{
		var txtTopics = $('searchbar').getElement('input[id$=txtTopics]');
		if(!checkNotNull(txtTopics, 'Forum name required'))
		{
			return false;
		}

		return true;
	},

	searchTopic : function()
	{
		var txtTopics = $('searchbar').getElement('input[id$=txtTopics]');
		if(!checkNotNull(txtTopics, 'keyword required'))
		{
			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('Poke successfully！');

				//登录后的处理：默认刷新页面
				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('Add friend successfully,Please wait for the confirmation');

				//登录后的处理：默认刷新页面
				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);
    