
// Profile Service
var ProfileService = 
{
	proxy : RegServiceUtils.proxyURI + "ProfileService",
	devURL : RegServiceUtils.serviceURI + "ProfileService",
	
	save: function(profile, callback, errorCallback) 
	{
		var soapBody = RegServiceUtils.createRequestBody("profile_save_request");
				
		soapBody.appendChild(ProfileService.profileToSoap(profile));				

		bam.soap.SOAPClient.Proxy = ProfileService.proxy; 
		bam.soap.SOAPClient.SOAPServer = ProfileService.devURL;
		
		var soapRequest = new bam.soap.SOAPRequest("http://services.bamnetworks.com/registration/profile/save", soapBody);
		bam.soap.SOAPClient.SendRequest(soapRequest, function(data) 
		{
			if (data && data.Body && data.Body[0].profile_save_response)
			{
				var responseBodyElement = data.Body[0].profile_save_response[0];
			
				if (!RegServiceUtils.isErrorThrown(responseBodyElement))
				{
					if (callback)
					{
						callback();
					}
				}
				else
				{
					errorCallback("Profile.save", RegServiceUtils.getStatus(responseBodyElement));
				}
			}
			else
			{
				errorCallback("Profile.save", {code:-100000, message:RegServiceUtils.getServiceErrorMessage(data)});
			}

		});
	}, 
	
	find: function(callback, errorCallback)
	{
		var soapBody = RegServiceUtils.createRequestBody("profile_find_request");
		bam.soap.SOAPClient.Proxy = ProfileService.proxy; 
		bam.soap.SOAPClient.SOAPServer = ProfileService.devURL;
		
		var soapRequest = new bam.soap.SOAPRequest("http://services.bamnetworks.com/registration/profile/find", soapBody);
		bam.soap.SOAPClient.SendRequest(soapRequest, function(data) 
		{
			if (data && data.Body && data.Body[0].profile_find_response)
			{
				var responseBodyElement = data.Body[0].profile_find_response[0];
			
				if (!RegServiceUtils.isErrorThrown(responseBodyElement))
				{
					callback(ProfileService.soapToProfile(responseBodyElement.profile[0]));
				}
				else
				{
					errorCallback("Profile.find", RegServiceUtils.getStatus(responseBodyElement));
				}
			}
			else
			{
				errorCallback("Profile.find", {code:-100000, message:RegServiceUtils.getServiceErrorMessage(data)});
			}

		});
	}, 
	
	clear: function(callback, errorCallback)
	{
		var soapBody = RegServiceUtils.createRequestBody("profile_clear_request");
		bam.soap.SOAPClient.Proxy = ProfileService.proxy; 
		bam.soap.SOAPClient.SOAPServer = ProfileService.devURL;
		
		var soapRequest = new bam.soap.SOAPRequest("http://services.bamnetworks.com/registration/profile/clear", soapBody);
		bam.soap.SOAPClient.SendRequest(soapRequest, function(data) 
		{
			if (data && data.Body && data.Body[0].profile_clear_response)
			{
				var responseBodyElement = data.Body[0].profile_clear_response[0];
			
				if (!RegServiceUtils.isErrorThrown(responseBodyElement))
				{
					if (callback)
					{
						callback();
					}
				}
				else
				{
					errorCallback("Profile.clear", RegServiceUtils.getStatus(responseBodyElement));
				}
			}
			else
			{
				errorCallback("Profile.clear", {code:-100000, message:RegServiceUtils.getServiceErrorMessage(data)});
			}
		});
	},
	
	profileToSoap: function(profile)
	{
		// add profile properties
		var profileElement = new bam.soap.SOAPObject("profile");
		
		for (var propName in profile) 
		{
			var profilePropertyElement = new bam.soap.SOAPObject("profileProperty");
				profilePropertyElement.appendChild(new bam.soap.SOAPObject("name")).val(propName);
			
			var profilePropValue = profile[propName];
			if (profilePropValue)
			{
				if (profilePropValue.constructor == Array)
				{
					for (var i = 0; i < profilePropValue.length; i++)
					{
						if (profilePropValue[i] && profilePropValue[i].length > 0)
						{
							profilePropertyElement.appendChild(new bam.soap.SOAPObject("value")).val(profilePropValue[i]);
						}
					}
				}
				else
				{
					profilePropertyElement.appendChild(new bam.soap.SOAPObject("value")).val(profilePropValue);
				}
			}
			
			profileElement.appendChild(profilePropertyElement);
		}
		
		return profileElement;
		
	},
	
	soapToProfile: function(profileElement)
	{
		var profile = null;
		
		if(profileElement)
		{
			profile = {};
			
			if (profileElement.profileProperty)
			{
				var profilePropertyElements = profileElement.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);    
                        }
                   }

				}
			}
		
		}
		
		return profile;
	}
	
}

