
// CommunityService
var CommunityService = 
{
	proxy : RegServiceUtils.proxyURI + "CommunityService",
	devURL : RegServiceUtils.serviceURI + "CommunityService",
	
	findProfile: function(targetIdentityPointId, callback, errorCallback)
	{
		var soapBody = RegServiceUtils.createRequestBody("community_findProfile_request");
		
		soapBody.appendChild(new bam.soap.SOAPObject("targetIdentityPointId")).val(targetIdentityPointId);
		
		bam.soap.SOAPClient.Proxy = CommunityService.proxy; 
		bam.soap.SOAPClient.SOAPServer = CommunityService.devURL;
		
		var soapRequest = new bam.soap.SOAPRequest("http://services.bamnetworks.com/registration/community/findProfile", soapBody);
		bam.soap.SOAPClient.SendRequest(soapRequest, function(data) 
		{
			if (data && data.Body && data.Body[0].community_findProfile_response)
			{
				var responseBodyElement = data.Body[0].community_findProfile_response[0];
				
				if (!RegServiceUtils.isErrorThrown(responseBodyElement) && responseBodyElement.communityProfile)
				{
					callback(CommunityService.soapToCommunityProfile(responseBodyElement.communityProfile[0]));
				}
				else
				{
					errorCallback("Community.findProfile", RegServiceUtils.getStatus(responseBodyElement));
				}
			}
			else
			{
				errorCallback("Community.findProfile", {code:-100000, message:RegServiceUtils.getServiceErrorMessage(data)});
			}
		});
	},
	
	findPrivacySettings: function(callback, errorCallback)
	{
		var soapBody = RegServiceUtils.createRequestBody("community_findPrivacySettings_request");
		
		bam.soap.SOAPClient.Proxy = CommunityService.proxy; 
		bam.soap.SOAPClient.SOAPServer = CommunityService.devURL;
		
		var soapRequest = new bam.soap.SOAPRequest("http://services.bamnetworks.com/registration/community/findPrivacySettings", soapBody);
		bam.soap.SOAPClient.SendRequest(soapRequest, function(data) 
		{
			if (data && data.Body && data.Body[0].community_findPrivacySettings_response)
			{
				var responseBodyElement = data.Body[0].community_findPrivacySettings_response[0];
				
				if (!RegServiceUtils.isErrorThrown(responseBodyElement))
				{
					var privacySettingList = new Array();

					if (responseBodyElement.privacySetting)
					{
						for (var i = 0; i < responseBodyElement.privacySetting.length; i++)
						{
							var privacySetting = new Object();
							var privacySettingElement = responseBodyElement.privacySetting[i];
							privacySetting.name = (privacySettingElement.name) ? privacySettingElement.name[0].Text : "";
							privacySetting.level = (privacySettingElement.level) ? privacySettingElement.level[0].Text : "";
							privacySetting.defaultLevel = (privacySettingElement.defaultLevel) ? privacySettingElement.defaultLevel[0].Text : "";
							privacySettingList.push(privacySetting);
						}
					}
					
					callback(privacySettingList);
				}
				else
				{
					errorCallback("Community.findPrivacySettings", RegServiceUtils.getStatus(responseBodyElement));
				}
			}
			else
			{
				errorCallback("Community.findPrivacySettings", {code:-100000, message:RegServiceUtils.getServiceErrorMessage(data)});
			}

		});
	},
	
	savePrivacySettings: function(privacySettings, callback, errorCallback)
	{
		var soapBody = RegServiceUtils.createRequestBody("community_savePrivacySettings_request");
		
		for (var i = 0; i < privacySettings.length; i++)
		{
			var privacySetting = privacySettings[i];
		
			var privacySettingElement = soapBody.appendChild(new bam.soap.SOAPObject("privacySetting"));
		
			if(privacySetting.name) { privacySettingElement.appendChild(new bam.soap.SOAPObject("name")).val(privacySetting.name); }
			if(privacySetting.level) { privacySettingElement.appendChild(new bam.soap.SOAPObject("level")).val(privacySetting.level); }
			if(privacySetting.defaultLevel) { privacySettingElement.appendChild(new bam.soap.SOAPObject("defaultLevel")).val(privacySetting.defaultLevel); }
		}
		
		bam.soap.SOAPClient.Proxy = CommunityService.proxy; 
		bam.soap.SOAPClient.SOAPServer = CommunityService.devURL;
		
		var soapRequest = new bam.soap.SOAPRequest("http://services.bamnetworks.com/registration/community/savePrivacySettings", soapBody);
		bam.soap.SOAPClient.SendRequest(soapRequest, function(data) 
		{
			if (data && data.Body && data.Body[0].community_savePrivacySettings_response)
			{
				var responseBodyElement = data.Body[0].community_savePrivacySettings_response[0];
			
				if (!RegServiceUtils.isErrorThrown(responseBodyElement))
				{
					callback();
				}
				else
				{
					errorCallback("Community.savePrivacySettings", RegServiceUtils.getStatus(responseBodyElement));
				}
			}
			else
			{
				errorCallback("Community.savePrivacySettings", {code:-100000, message:RegServiceUtils.getServiceErrorMessage(data)});
			}
		});
	},
	
	saveStatus: function(type, value, callback, errorCallback)
	{
		var soapBody = RegServiceUtils.createRequestBody("community_saveStatus_request");
		
		soapBody.appendChild(new bam.soap.SOAPObject("type")).val(type);
		soapBody.appendChild(new bam.soap.SOAPObject("value")).val(value);
		
		bam.soap.SOAPClient.Proxy = CommunityService.proxy; 
		bam.soap.SOAPClient.SOAPServer = CommunityService.devURL;
		
		var soapRequest = new bam.soap.SOAPRequest("http://services.bamnetworks.com/registration/community/saveStatus", soapBody);
		bam.soap.SOAPClient.SendRequest(soapRequest, function(data) 
		{
			if (data && data.Body && data.Body[0].community_saveStatus_response)
			{
				var responseBodyElement = data.Body[0].community_saveStatus_response[0];
			
				if (!RegServiceUtils.isErrorThrown(responseBodyElement) && responseBodyElement.id)
				{
					callback(responseBodyElement.id[0].Text);
				}
				else
				{
					errorCallback("Community.saveStatus", RegServiceUtils.getStatus(responseBodyElement));
				}
			}
			else
			{
				errorCallback("Community.saveStatus", {code:-100000, message:RegServiceUtils.getServiceErrorMessage(data)});
			}
		});
	},
	
	deleteStatus: function(statusId, callback, errorCallback)
	{
		var soapBody = RegServiceUtils.createRequestBody("community_deleteStatus_request");
		
		soapBody.appendChild(new bam.soap.SOAPObject("id")).val(statusId);
		
		bam.soap.SOAPClient.Proxy = CommunityService.proxy; 
		bam.soap.SOAPClient.SOAPServer = CommunityService.devURL;
		
		var soapRequest = new bam.soap.SOAPRequest("http://services.bamnetworks.com/registration/community/deleteStatus", soapBody);
		bam.soap.SOAPClient.SendRequest(soapRequest, function(data) 
		{
			if (data && data.Body && data.Body[0].community_deleteStatus_response)
			{
				var responseBodyElement = data.Body[0].community_deleteStatus_response[0];
			
				if (!RegServiceUtils.isErrorThrown(responseBodyElement))
				{
					callback();
				}
				else
				{
					errorCallback("Community.deleteStatus", RegServiceUtils.getStatus(responseBodyElement));
				}
			}
			else
			{
				errorCallback("Community.deleteStatus", {code:-100000, message:RegServiceUtils.getServiceErrorMessage(data)});
			}
		});
	},
	
	soapToCommunityProfile: function(communityProfileElement)
	{
		var communityProfile = null;
		
		if(!!communityProfileElement)
		{
			communityProfile = {};
			
			if(!!communityProfileElement.relationshipType) {communityProfile.relationshipType = communityProfileElement.relationshipType[0].Text};
			if(!!communityProfileElement.friendsToConfirmCount) {communityProfile.friendsToConfirmCount = communityProfileElement.friendsToConfirmCount[0].Text};
			
			if(communityProfileElement.userStatus.length > 0)
			{
				communityProfile.status = [];
				if(!!communityProfileElement.userStatus[0].communityStatus) {
					
					var s = !!communityProfileElement.userStatus[0].communityStatus.length && communityProfileElement.userStatus[0].communityStatus.length-1;
					var cs = null;
					if(s >=0) {
						do {
							cs = communityProfileElement.userStatus[0].communityStatus[s];
							communityProfile.status.push((!!cs.value)?cs.value[0].Text:"");
						} while(s--);
						communityProfile.status.reverse();
					}
				}
			}

			if(communityProfileElement.friends.length)
			{
				communityProfile.friends = [];
				var f = !!communityProfileElement.friends[0].communityMember && communityProfileElement.friends[0].communityMember.length - 1;
				var cf = null;
				if((f==0 || !!f) && f>=0) {
					do {
						cf = !!communityProfileElement.friends[0].communityMember && communityProfileElement.friends[0].communityMember[f];
						communityProfile.friends.push(CommunityService.soapToCommunityMember(cf));					
					} while(f--);
				}
				communityProfile.friends.reverse();
			}
						
			if(!!communityProfileElement.profile)
			{
				communityProfile.profile = CommunityService.soapToProfile(communityProfileElement.profile);
			}
		}
		
		return communityProfile;
	},

	soapToProfile: function(profileElement)
	{
		var profile = null;
		
		if(profileElement)
		{
			profile = new Object();
			
			if (profileElement[0].profileProperty)
			{
				var profilePropertyElements = profileElement[0].profileProperty;
			
				for (var i = 0; i < profilePropertyElements.length; i++)
				{
					var profilePropName = profilePropertyElements[i].name[0].Text;
					if (profilePropertyElements[i].value)
					{
						var profileProp = new Object();
                        profile[profilePropName] = profileProp;
                        
                        // set the name
                        profileProp.name = profilePropName;
                        
                        // set the created and modified dates
                        profileProp.createdDate = profilePropertyElements[i].createdDate;
                        profileProp.modifiedDate = profilePropertyElements[i].modifiedDate;
                        
                        // set the value
                        profileProp.value = new Array();
                        for (var j = 0; j < profilePropertyElements[i].value.length; j++)
                        {
                            profileProp.value.push(profilePropertyElements[i].value[j].Text);    
                        }
					}
					else
					{
						profile[profilePropName] = null;
					}
				}
			}
		
		}
		
		return profile;
	}, 
	
	soapToCommunityMember: function(communityMemberElement)
	{
		var communityMember = null;
		
		if(communityMemberElement)
		{
			communityMember = new Object();
			
			if(communityMemberElement.identityPointId) {communityMember.identityPointId = communityMemberElement.identityPointId[0].Text};
			if(communityMemberElement.nickname) {communityMember.nickname = communityMemberElement.nickname[0].Text};
			if(communityMemberElement.avatar) {communityMember.avatar = communityMemberElement.avatar[0].Text};
			if(communityMemberElement.favoriteTeam) {communityMember.favoriteTeam = communityMemberElement.favoriteTeam[0].Text};
			if(communityMemberElement.status) {communityMember.status = communityMemberElement.status[0].Text};
			if(communityMemberElement.relationshipType) {communityMember.relationshipType = communityMemberElement.relationshipType[0].Text};
		}
		
		return communityMember;
	}
	
}

