// comm.js - initiators and callbacks for asynchronous server communication

function doLogin(){
	
	winLogin.setStatus("authenticating...");
	winLogin.unsetError();

	args = new Object();
	args.object="global";
	args.method="authenticate";
	args.username=$F('username');
	args.password=$F('password');
	http('POST','index.cfm',doLogin_Response,args); 
	
	return false;
  }
  
function doLogin_Response(obj){ 
	if(obj.error) {
		winLogin.unsetStatus();
		winLogin.setError(obj.errortext);
	} else {
 		winLogin.close();
		global.user=obj.data;
		setMasthead();
		if(global.user.username) {
			chat=initChat();
			chat.receive();
		} 
	}
}

function doSignup() {
 	winSignUp.setStatus('registering...');
	winSignUp.unsetError();
	
	args = new Object();
	args.object="users";
	args.method="signup";
	args.username=$F('signup_username');
	args.password=$F('signup_password');
	args.password2=$F('signup_password2');
	http('POST','index.cfm',doSignup_Response,args); 
	
	return false;
}

function doSignup_Response(obj) {
	if(obj.error) {
		winSignUp.unsetStatus();
		winSignUp.setError(obj.errortext);
	} else {
 		winSignUp.close();
		global.user=obj.data;
		setMasthead();
		if(global.user.username) {
			chat=initChat();
			chat.receive();
		} 
	}
}

function doLogout() {

	//document.location.reload();
	chat.stop();
	http('POST','index.cfm',doLogout_Response,{object:"users",method:"logout"});
}

function doLogout_Response(obj) {
	if(!obj.error) {
		document.location.reload();
	}
}		

function msgSend(m) {
	args = new Object();
	args.object="message";
	args.method="send";
	args.message=m;
	http('POST','index.cfm',msgSend_Response,args);
}	

function msgSend_Response(obj)  {
}

function chatApplySettings() {
	winChatSettings.setStatus("saving...");		
	args = new Object();
	args.object="users";
	args.method="saveChatSettings";
	args.lineSpacing=chat.lineSpacing;
	args.displaySize=chat.displaySize;
	args.fontSize=chat.fontSize;
	http('POST','index.cfm',chatApplySettings_Response,args);
	
	return false;
}	

function chatApplySettings_Response() {
	winChatSettings.setStatus("settings stored");	
}

function loadGlobalVariables() {	 
	args = new Object();
	args.object="global";
	args.method="loadClientGlobals";
	http('POST','index.cfm',loadGlobalVariables_Response,args); 
}
	   
function loadGlobalVariables_Response(obj){ 
	global = obj.data;
}

function loadUserVariables() {	 
	args = new Object();
	args.object="users";
	args.method="loadClientUser";
	http('POST','index.cfm',loadUserVariables_Response,args); 
}
	   
function loadUserVariables_Response(obj){ 
	global.user = obj.data;
}

function who(event,position) {
	args = new Object();
	args.object="global";
	args.method="who";
	http('POST','index.cfm',who_Response,args);
	winWho.setStatus("Retrieving data...");
	if(!winWho.isOpen) {winWho.open(event,position);}
}

function who_Response(obj) {
	a=obj.data;
	d=new Date;
	t=d.getTime();
	var s='';
	s+=obj.count + ' users recently active (' + (obj.count-a.length) + ' anonymous)';
	s+='<table border="0" cellpadding="2" cellspacing="2" width="100%" style="margin-top:5px;">';
	s+='<tr><td><b>User</b></td><td><b>Last Active</b></td></tr>';
	for(i=0;i<a.length;i++) {
		o=a[i];
		elapsed = (o.lastactive / 1000);
		s+= '<tr><td><a href="javascript:void(0)" onClick="command.parse(\'/user ' + o.username + '\');">' + (o.username=='Calahn' && global.user.username!=='Calahn'?'Sugar Britches':o.username) + '</a></td><td>' +  (elapsed>60?Math.floor(elapsed/60) + ' min':elapsed + ' sec') + '</td></tr>';
	}
	s+='</table>';
	winWho.setStatus("Updated: " + formatMessageDate(new Date().getTime(),false));
	Element.update('dspWho',s);
	setTimeout("(winWho.isOpen?who():void(0))",30000);
}

function getArchivedMessages(o) {
	args=new Object();
	args.object="message";
	args.method="getArchivedMessages";
	args.start=o.start;
	args.top=o.top;
	
	if(o.clear)  {
		Element.update($('chatArchiveBody'),'&nbsp;');
	}
	pnlLoading.open();
	http('POST','index.cfm',getArchivedMessages_Response,args);
}

function getArchivedMessages_Response(obj) {
	var ca=$('chatArchiveBody');
	var cc=$('chatArchiveControl');
	var messages=obj.data.reverse();
	var lump = '';
	var prefixHTML;
	var getOlder;
	
	for(i=0;i < messages.length;i++) {
	
		message = messages[i];
		prefixHTML = '&lt;' + formatArchiveMessageDate(message.dateposted) + '&nbsp;<a href="javascript:void(0);" onClick="command.parse(\'/user ' + message.username + '\');">' + message.username + '</a>&gt;';
		
		lump += '<div class="chatArchiveRow">';
		lump += '<div class="chatArchivePrefix">' + prefixHTML + '</div>';
		lump += '<div class="chatArchiveMessage">' + chat.clean(message.message) + '</div>';
		lump += '</div>';
	}	
		
	ca.innerHTML = lump + ca.innerHTML;
	
	if(messages.length == 0) {
		getOlder = 'All ' + ca.childNodes.length-1 + ' archived messages displayed<br><br>';
	} else {
		getOlder = ca.childNodes.length-1 + ' archived messages displayed<br><a href="javascript:void(0);" onClick="getArchivedMessages({top:100,start:' + messages[0].messageid + '}	);">Go further back</a><br><br>';
	}	
	cc.innerHTML = getOlder;	
	
	pnlChatArchive.open();
}

function help(path) {
	var id = path.replace(/\./g,'');
	
	if(global.content[id]) {delete global.content[id];}
	
	args = new Object;
	args.object="global";
	args.method="getContent";
	args.path=path;

	winHelp.content.innerHTML = '&nbsp;';
	winHelp.setStatus('Loading...');
	if(!winHelp.isOpen) {winHelp.open('','center')};
	http('POST','index.cfm',help_Response,args);

}

function help_Response(obj) {
	if(!obj.error) {
		data = obj;
		Element.update(winHelp.content,obj.data[obj.data.id]);
		winHelp.unsetStatus();
	}
}


function profile(userid) {
	if(userid=="undefined") {userid=global.user.username;}
	pnlLoading.open();
	http('POST','index.cfm',profile_Response,{object:'global',method:'loadUserObject',userid:userid});
}

function profile_Response(obj) {
	pnlProfile.setUser(assembleUser(obj.data));
	pnlProfile.populate();
	pnlProfile.open();
}

function getContent(path,o) {
	
	var id = path.replace(/\./g,'');
	global.argProxy[id] = o;	
	
	args = new Object;
	args.object="global";
	args.method="getContent";
	args.path=path;

	http('POST','index.cfm',getContent_Response,args);
}

function getContent_Response(obj) {
	var e=$(global.argProxy[obj.data.id].element);
	Element.update(e,obj.data[obj.data.id]);
}

function editProfileMain() {
	$('userProfileMainEdit').className = 'profileEditStatus';	
	Element.update('userProfileMainEdit','updating your settings...');

	args = new Object();
	args.object="global";
	args.method="saveProfileMain";
	args.userid = pnlProfile.user.username;
	args.firstname = $F('fieldprofileMainfirstname');
	args.lastname = $F('fieldprofileMainlastname');
	args.location = $F('fieldprofileMainlocation');
	args.age = $F('fieldprofileMainage');
	args.soehandle = $F('fieldprofileMainsoehandle');
	args.sex = $F('fieldprofileMainsex');
	args.occupation = $F('fieldprofileMainoccupation');
	
	args.age=(args.age=='not specified'?'':args.age);
	args.sex=(args.sex=='not specified'?'':args.sex);	
	
	http('POST','index.cfm',editProfileMain_Response,args);
	
	return false;
}

function editProfileMain_Response(obj) {
	if(obj.error) {
		$('userProfileMainEdit').className = 'profileEditError';
		Element.update('userProfileMainEdit',obj.errortext);
		setTimeout('$("fieldprofileMainfirstname").focus()',50);
		return false;
	}
	
	pnlProfile.user=obj.data;
	if(pnlProfile.user.username == global.user.username)  {
		global.user = obj.data;
	}
	pnlProfile.populateUserMainAsDisplay();	
}

function editProfileContact() {
	$('userProfileContactEdit').className = 'profileEditStatus';	
	Element.update('userProfileContactEdit','updating your settings...');

	args = new Object();
	args.object="global";
	args.method="saveProfileContact";
	args.userid = pnlProfile.user.username;
	args.email = $F('fieldprofileContactemail');
	args.im_yahoo = $F('fieldprofileContactim_yahoo');
	args.im_msn = $F('fieldprofileContactim_msn');
	args.im_aim = $F('fieldprofileContactim_aim');
	args.im_google = $F('fieldprofileContactim_google');
	args.im_icq = $F('fieldprofileContactim_icq');
	
	http('POST','index.cfm',editProfileContact_Response,args);
	
	return false;
}

function editProfileContact_Response(obj) {
	if(obj.error) {
		$('userProfileContactEdit').className = 'profileEditError';
		Element.update('userProfileContactEdit',obj.errortext);
		setTimeout('$("fieldprofileContactemail").focus()',50);
		return false;
	}
	
	pnlProfile.user=obj.data;
	if(pnlProfile.user.username == global.user.username)  {
		global.user = obj.data;
	}
	pnlProfile.populateUserContactAsDisplay();	
}
	
	


function deleteToon(toonid)  {
	pnlProfile.setToonStatus('status','Deleting...');
	args = new Object();
	args.object="global";
	args.method="deleteToon";
	args.userid = pnlProfile.user.username;
	args.toonid=toonid;
	http('POST','index.cfm',deleteToon_Response,args);
}

function deleteToon_Response(obj) {
	if(obj.error) {	pnlProfile.setToonStatus('error',obj.errortext);}
	
	pnlProfile.user.toon = obj.data;
	pnlProfile.user = assembleUser(pnlProfile.user);
	if(pnlProfile.user.username == global.user.username) {
		global.user.toon = obj.data;
		global.user = assembleUser(global.user);
	}
	pnlProfile.populateToonsTab();
	pnlProfile.setToonStatus('status','Toon deleted');
}
	 