/**
* Javascript class that uses Ajax methods to communicate with the server. The server will
* perform validation, sanitize the input and also get the user id so that all commands are 
* performed on the currently logged in user.
*
* @author Mike Pritchard (mike@adastrasystems.com)
* @since 14th June, 2010
*/
var RemoteService = {
	
	commandURL : '/code/php/RemoteServices.php',
	
	// ////////////////////////////////////////////////////////////////////////////
	
	/**
	* Add an activation code (you need to be a user level 7 to do this)
	*/
	addActivationCode : function(actName, actCode, actStartDate, actEndDate, actMaxAccounts, callback){	
		var paras = {cmd: 3, name: actName, code: actCode, start_date: actStartDate, end_date: actEndDate, max_accounts: actMaxAccounts};
		RemoteService.sendCommand(paras, callback);																
	},

	// ////////////////////////////////////////////////////////////////////////////

	login : function(userName, userPassword, callback){
		var paras = {cmd: 1, username: userName, password: userPassword };
		RemoteService.sendCommand(paras, callback);																
	},
				
	checkUsernameExists : function(userName, blogID, callback){
		var paras = {cmd: 19, username: userName, blog_id: blogID };
		RemoteService.sendCommand(paras, callback);																
	},
	
	checkActivationCode : function(activationCode, blogID, callback){
		var paras = {cmd: 20, code: activationCode, blog_id: blogID };
		RemoteService.sendCommand(paras, callback);																
	},
							
	// ////////////////////////////////////////////////////////////////////////////
	
	signupUser : function(userName, userCode, userPassword, userEthnicity, userGender, userAge, userCaptcha, callback){
		var paras = {cmd: 2, username: userName, code: userCode, password: userPassword, ethnicity: userEthnicity, gender: userGender, age: userAge, captcha: userCaptcha };
		RemoteService.sendCommand(paras, callback);																
	},
	
	// ////////////////////////////////////////////////////////////////////////////
	
	editUser : function(userEthnicity, userGender, userAge, callback){
		var paras = {cmd: 4, ethnicity: userEthnicity, gender: userGender, age: userAge };
		RemoteService.sendCommand(paras, callback);																
	},	

	// ////////////////////////////////////////////////////////////////////////////
	
	addUserInfo : function(userMaritalStatus, userIncomeGroupID, userEducationGroupID, years156, years157, years158, years159, callback){
		var paras = {cmd: 14, mst: userMaritalStatus, iid: userIncomeGroupID, eid: userEducationGroupID, y156: years156, y157: years157, y158: years158, y159: years159 };
		RemoteService.sendCommand(paras, callback);																
	},	

	// ////////////////////////////////////////////////////////////////////////////

	changePassword : function(oldPassword, newPassword, callback){
		var paras = {cmd: 5, oldpass: oldPassword, newpass: newPassword };
		RemoteService.sendCommand(paras, callback);																
	},	

	// ////////////////////////////////////////////////////////////////////////////
	//
	// Journal
	//
	// ////////////////////////////////////////////////////////////////////////////

	addJournalEntry : function(entryText, journalType, callback){
		var paras = {cmd: 15, text: entryText, type: journalType };
		RemoteService.sendCommand(paras, callback);																
	},

	getJournalEntries : function(journalType, callback){
		var paras = {cmd: 17, type: journalType };
		RemoteService.sendCommand(paras, callback);																
	},
	
	deleteJournalEntry : function(entryID, callback){
		var paras = {cmd: 16, id: entryID };
		RemoteService.sendCommand(paras, callback);																
	},
	
	// ////////////////////////////////////////////////////////////////////////////
	//
	// Dynamic Media
	//
	// ////////////////////////////////////////////////////////////////////////////
	
	/**
	* Add dynanic media
	*/ 
	addDynamicText : function(pagePostID, pageContent, gend, ethnicGroupID, traumaGroupID, callback){
		var paras = {cmd: 11, post_id: pagePostID, content:pageContent, gender:gend, ethnic_id:ethnicGroupID, trauma_id:traumaGroupID};
		RemoteService.sendCommand(paras, callback);																
	},
		
	// ////////////////////////////////////////////////////////////////////////////
	
	/**
	* Add dynanic media
	*/ 
	deleteDynamicText : function(textID, callback){
		var paras = {cmd: 12, dyn_media_id: textID};
		RemoteService.sendCommand(paras, callback);																
	},
	
	// ////////////////////////////////////////////////////////////////////////////
	
	/**
	* Add dynanic media
	*/ 
	addDynamicMedia : function(pagePostID, imageURL, flvURL, gend, ethnicGroupID, traumaGroupID, callback){
		var paras = {cmd: 8, post_id: pagePostID, image_url:imageURL, flv_url:flvURL, gender:gend, ethnic_id:ethnicGroupID, trauma_id:traumaGroupID};
		RemoteService.sendCommand(paras, callback);																
	},
		
	// ////////////////////////////////////////////////////////////////////////////
	
	/**
	* Add dynanic media
	*/ 
	deleteDynamicMedia : function(mediaID, callback){
		var paras = {cmd: 9, dyn_media_id: mediaID};
		RemoteService.sendCommand(paras, callback);																
	},
		
	// ////////////////////////////////////////////////////////////////////////////
			
	/**
	* Update user progress. Do this using a remote call as we can attach this to a specific user action (clicking the
	* next button). We can't reliably do it on the page load, as some caching strategies will pre-load the next page
	* and so we don't want that to count as real user progress
	*/ 
	updateUserModuleProgress : function(submoduleID, pageNo){
		var paras = {cmd: 10, submodule_id: submoduleID, page_no: pageNo};
		RemoteService.sendCommand(paras);																
	},
					
	// ////////////////////////////////////////////////////////////////////////////

	/**
	* Save generic answer
	*/ 
	saveAnswers : function(questionText, answerList, pagePostID, moduleID, callback){			
		var paras = {cmd: 6, question: questionText, answers: $.toJSON(answerList), post_id: pagePostID,  module_id: moduleID};
		RemoteService.sendCommand(paras, callback);																
	},
				
	// ////////////////////////////////////////////////////////////////////////////

	/**
	* Save answers to the self-test module
	*/
	saveSelfTestAnswers : function(answerList, callback){		
		var paras = {cmd: 13, answers: $.toJSON(answerList)};
		RemoteService.sendCommand(paras, callback);																
	},
				
	/**
	* Get answers to the self-test module
	*/
	getSelfTestAnswers : function(callback){		
		var paras = {cmd: 18};
		RemoteService.sendCommand(paras, callback);																
	},
							
	// ////////////////////////////////////////////////////////////////////////////

	/**
	* Save the answer
	*/ 
	/*
	saveAnswers : function(questionText, answerList, pagePostID, moduleTemplate, callback){

		var paras = {cmd: 6, question: questionText, answers: answerList, post_id: pagePostID, module:moduleTemplate};
																
		jQuery.ajax({
			url: RemoteService.commandURL,
			dataType: "json",
			data: paras,
			success: function(ret){callback(ret);}
		});	
	},
		*/
	// ////////////////////////////////////////////////////////////////////////////

	/**
	* Clear all the answers for the given question and page
	*/
	clearAnswers : function(questionText, pagePostID, callback){
		var paras = {cmd: 7, question: questionText, post_id: pagePostID};
		RemoteService.sendCommand(paras, callback);																
	},
	
	// ////////////////////////////////////////////////////////////////////////////

	/**
	* Actually send the ajax command
	* @param paras
	* @param callback - the function to be called upon sucess
	*/
	sendCommand : function(paras, callback){
		$.ajax({
			url: RemoteService.commandURL,
			dataType: "json",
			data: paras,
			success: function(ret){if(callback != undefined) {callback(ret);}}
		});	
	}
		
	
}


