﻿/**
*	GeneralControl
*/
var GeneralControl = AbstractControl.extend(
	{
		/**
		*   init
		*
		*   Constructs the core
		*
		*	@param	c - model instance
		*/
		init: function(c)
		{
			this._super( "GeneralControl", c );
			this.delay = null;
		},
		
		/**
		*   authAdminUser
		*
		*   Authenticates an administrator
		*
		*	@param	id - user id
		*/
		authAdminUser: function(id)
		{
			//alert( "authenticating admin user : " + id );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "authenticate_admin_user",
				userId: id
			}, function(json) {
				//alert( "received : " + json.contents );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onAdminUserAuthFailed', json.contents );
				} else {
				// if data has been returned
				
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onAdminUserAuthSuccess', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   logInAsUser
		*
		*   Logs in as an existing user
		*
		*	@param	u - user id
		*/
		logInAsUser: function(u)
		{
			//alert( "logging in user : " + u );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "log_in_as_user",
				userId: u
			}, function(json) {
				//alert( "received : " + json.contents );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onLoginAsUserFailed', json.contents );
				} else {
				// if data has been returned
				
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onLoginAsUserSuccess', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   logInUser
		*
		*   Logs in a new user
		*
		*	@param	e - user email
		*	@param	p - user password
		*/
		logInUser: function(e, p)
		{
			//alert( "logging in user : " + e + " : " + p );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "log_in_user",
				email: e,
				password: p
			}, function(json) {
				//alert( "received : " + json.contents );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onUserLogInFailed', json.contents );
				} else {
				// if data has been returned
				
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onUserLogInSuccess', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   logOutUser
		*
		*   Logs out the currently logged in user
		*/
		logOutUser: function()
		{
			//alert( "logging out user" );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "log_out_user"
			}, function(json) {
				//alert( "received : " + json.contents );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onUserLogOutFailed', json.contents );
				} else {
				// if data has been returned
				
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onUserLogOutSuccess', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   signUpNewUser
		*
		*   Signs up a new user
		*
		*	@param	t - user type
		*	@param	e - email
		*	@param	p1 - password 1
		*	@param	p2 - password 2
		*	@param	pc - post code
		*	@param	ph - phone number
		*/
		signUpNewUser: function(t, e, p1, p2, pc, ph)
		{
			//alert( "signing user : " + t + " : " + e + " : " + p1 + " : " + p2 + " : " + pc + " : " + ph );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "sign_up_new_user",
				userType: t,
				email: e,
				password1: p1,
				password2: p2,
				postcode: pc,
				phone: ph
			}, function(json) {
				//alert( "received : " + json.contents.error );
				
				if ( json.contents.error != null ) {
				// if there has been an error
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onNewUserSignUpFailed', json.contents );
				} else {
				// if data has been returned
				
					var userProfile = obj.core.getSerializedProfileData( json.contents.profile );
					
					//alert( json.contents.profile + " po ?? " );
					//alert( userProfile.getProfileId() + " pid ?? " );
				
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onNewUserSignUpSuccess', userProfile );
				}
				return false;
			}, "json");
		},
		
		/**
		*   deleteUser
		*
		*   Deletes a users record from the database ( Well actually - it just sets all the statuses to non-live )
		*
		*	@param	t - user type
		*	@param	id - profile id
		*/
		deleteUser: function(t, id)
		{
			//alert( "deleting user : " + id + " of type " + t );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "delete_user",
				userType: t,
				profileId: id
			}, function(json) {
				//alert( "received : " + json );
				
				if ( json.contents.error != null ) {
				// if there has been an error
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onUserDeleteFailed', json.contents );
				} else {
				// if data has been returned
				
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onUserDeleteSuccess', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   editUserProperty
		*
		*   Edits a specified user property
		*
		*	@param	u - user id
		*	@param	p - property to edit
		*	@param	v - value to edit with
		*/
		editUserProperty: function(u, p, v)
		{
			//alert( "editing user property : " + u + " : " + p + " : " + v );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "edit_user_property",
				userId: u,
				editProperty: p,
				editValue: v
			}, function(json) {
				//alert( "received : " + json );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						// unable to add the fan
						obj.getCore().broadcast( 'onEditUserPropertyFailed', { property: p, value: v } );
					} else {
					// if data has been returned
						// broadcast event
						// added the fan
						obj.getCore().broadcast( 'onEditUserPropertySuccess', { property: p, value: v } );
					}
					return false;
				}
			}, "json");
		},
		
		/**
		*   addUserAccolade
		*
		*   Adds an accolade to a specified user
		*
		*	@param	u - user id
		*	@param	a - accolade
		*/
		addUserAccolade: function(u, a)
		{
			//alert( "adding user accolade : " + u + " : " + a );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "add_user_accolade",
				userId: u,
				accolade: a
			}, function(json) {
				//alert( "received : " + json );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						// unable to add the fan
						obj.getCore().broadcast( 'onAddUserAccoladeFailed', json.contents );
					} else {
					// if data has been returned
						// broadcast event
						// added the fan
						obj.getCore().broadcast( 'onAddUserAccoladeSuccess', json.contents );
					}
					return false;
				}
			}, "json");
		},
		
		/**
		*   removeUserAccolade
		*
		*   Removes an accolade from a specified user
		*
		*	@param	u - user id
		*	@param	a - accolade
		*/
		removeUserAccolade: function(u, a)
		{
			//alert( "removing user accolade : " + u + " : " + a );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "remove_user_accolade",
				userId: u,
				accolade: a
			}, function(json) {
				//alert( "received : " + json );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						// unable to add the fan
						obj.getCore().broadcast( 'onRemoveUserAccoladeFailed', json.contents );
					} else {
					// if data has been returned
						// broadcast event
						// added the fan
						obj.getCore().broadcast( 'onRemoveUserAccoladeSuccess', json.contents );
					}
					return false;
				}
			}, "json");
		},
		
		/**
		*   getNewPassword
		*
		*   Prompts ajax to send user a new password
		*
		*	@param	e - user email
		*/
		getNewPassword: function(e)
		{
			//alert( "getting password for : " + e );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "get_new_password",
				userEmail: e
			}, function(json) {
				//alert( "received : " + json );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						// unable to add the fan
						obj.getCore().broadcast( 'onGetNewPasswordFailed', json.contents );
					} else {
					// if data has been returned
						// broadcast event
						// added the fan
						obj.getCore().broadcast( 'onGetNewPasswordSuccess', json.contents );
					}
					return false;
				}
			}, "json");
		},
		
		/**
		*   getUserSignUps
		*
		*   Returns a list of sign ups based upon a desired format
		*
		*	@param	t - user type [ 0 if all ]
		*	@param	f - format
		*/
		getUserSignUps: function(t, f)
		{
			//alert( "getting sign ups : " + t + " : " + f );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "get_user_sign_ups",
				userType: t,
				format: f
			}, function(json) {
				//alert( "received : " + json );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						// unable to add the fan
						obj.getCore().broadcast( 'onGetUserSignUpsFailed', json.contents );
					} else {
					// if data has been returned
						// broadcast event
						// added the fan
						obj.getCore().broadcast( 'onGetUserSignUpsSuccess', json.contents );
					}
					return false;
				}
			}, "json");
		},
		
		/**
		*   getGirls
		*
		*   Gets a collection of girls
		*
		*	@param	g - girls id's array
		*	@param	s - status
		*	@Param	f - filter
		*/
		getGirls: function(g, s, f)
		{
			//alert( "getting girls : " + g + " : " + s + " : " + f );
			
			// TODO : Split array to string
			// TODO !!
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax.php", {
				cmd: "get_girls",
				girlIds: g,
				status: s,
				filter: f
			}, function(json) {
				//alert( "received : " + json.contents );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onGirlsReceived', json.contents );
				} else {
				// if data has been returned
				
					//alert( "girls returned : " + json.contents.girls.length );
				
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onGirlsReceived', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   getLeagueTableUsers
		*
		*   Gets a collection of users for the league table
		*
		*	@param	t - user type
		*/
		getLeagueTableUsers: function(t)
		{
			//alert( "getting league table users " + t );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax.php", {
				cmd: "get_league_table_users",
				userType: t
			}, function(json) {
				//alert( "received : " + json.contents + " type = " + t );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onLeagueTableUsersReceived', json.contents );
				} else {
				// if data has been returned
				
					//alert( json.contents.users.length );
					
					var users = new Array();
					for ( i=0; i<json.contents.users.length; i++ )
					{
						var userProfile = obj.core.getSerializedProfileData( json.contents.users[i].profile );
						users.push( userProfile );
					}
					
					var ltData = new LeagueTableData();
						ltData.setUsers( users );
				
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onLeagueTableUsersReceived', ltData );
				}/**/
				return false;
			}, "json");
		},
		
		/**
		*   getProfile
		*
		*   Gets a specified users profile details
		*
		*	@param	t - uset type id
		*	@param	id - user id
		*/
		getProfile: function(t, id)
		{
			//alert( "getting profile details from id : " + id + " of user type : " + t );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax.php", {
				cmd: "get_prof",
				userType: t,
				profileId: id
			}, function(json) {
				//alert( "received : " + json );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onUserProfileReceived', json.contents );
				} else {
				// if data has been returned
				
					var userProfile = obj.core.getSerializedProfileData( json.contents.profile );
					
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onUserProfileReceived', userProfile );
				}
				return false;
			}, "json");
		},
		
		/**
		*   editProfile
		*
		*   Edits a profile with passed data
		*
		*	@param	t - user type id
		*	@param	e - edits array
		*/
		editProfile: function(t, e)
		{
			//alert( "editing profile : " + e + " of user type : " + t );
			
			var cmdObj = new Object();
				cmdObj.cmd = "edit_prof";
				cmdObj.userType = t;
			for ( var i in e )
			{
				cmdObj[i] = e[i];
			}
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", cmdObj, function(json) {
				//alert( "received : " + json.contents );
				
				if ( json.contents.error != null ) {
				// if there has been no xml returned
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onUserProfileEditFailed', json.contents );
				} else {
				// if data has been returned
				
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onUserProfileEditSuccess', obj.core.getSerializedProfileData( json.contents.profile ) );
				}
				return false;
			}, "json");
		},
		
		/**
		*   viewUsersProfile
		*
		*   Redirects page to vuew a specified users profile
		*
		*	@param	id - user id
		*/
		viewUsersProfile: function(id)
		{
			//alert( "viewing profile : " + id );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax.php", {
				cmd: "view_users_profile",
				userId: id
			}, function(json) {
				//alert( "received : " + json.contents + " : " + id );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// do nothing
				} else {
				// if data has been returned
					
					// re-direct user
					obj.changePage( json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   updateProfileViews
		*
		*   Updates the users profile views
		*
		*	@param	t - user type
		*	@param	uid - user id
		*	@param	id - profile id
		*/
		updateProfileViews: function(t, uid, id)
		{
			//alert( "updating profile views " + t + " : " + id );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "update_profile_views",
				userType: t,
				userId: uid,
				profileId: id
			}, function(json) {
				//alert( "received : " + json.contents );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// do nothing
				} else {
				// if data has been returned
					// do nothing
				}
				return false;
			}, "json");
		},
		
		/**
		*   contactUser
		*
		*   Initiates the contact of a user
		*
		*	@param	id - user id
		*/
		contactUser: function(id)
		{
			//alert( "contacting user : " + id );
			
			// post a message
			var msge = new MessageData();
			msge.setMessageType( "bad" );
			msge.setMessage( "Sorry! This service has not been added yet. Please try again later" );
			this.getCore().broadcast( 'onMessageShow', msge );
		},
		
		/**
		*   getUserFans
		*
		*   Returns a list of user fans from the database
		*
		*	@param	id - user id
		*	@param	o - order by
		*	@Param	m - max results
		*	@param	p - page number
		*/
		getUserFans: function(id, o, m, p)
		{
			//alert( this.getType() + " getting user fans : " + id + " : " + o + " : " + m + " : " + p );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax.php", {
				cmd: "get_user_fans",
				userId: id,
				orderBy: o,
				searchMax: m,
				searchPage: p
			}, function(json) {
				//alert( "received : " + json.contents + " : " + json.contents.fans );
				
				// check for all err types
				// @@ error - user already has this fan
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						// unable to add the fan
						obj.getCore().broadcast( 'onUserFansFailed', json.contents );
					} else {
					// if data has been returned
						// broadcast event
						// added the fan
						obj.getCore().broadcast( 'onUserFansReceived', json.contents );
					}
					return false;
				}
			}, "json");/**/
		},
		
		/**
		*   getUserFansPreview
		*
		*   Returns a preview list of user fans from the database
		*
		*	@param	id - user id
		*	@param	p - preview amt
		*/
		getUserFansPreview: function(id, p)
		{
			//alert( "getting user fans preview : " + id );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax.php", {
				cmd: "get_user_fans_preview",
				userId: id,
				previewAmt: p
			}, function(json) {
				//alert( "received : " + json.contents + " : " + json.contents.fans );
				
				// check for all err types
				// @@ error - user already has this fan
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						// unable to add the fan
						obj.getCore().broadcast( 'onUserFansPreviewFailed', json.contents );
					} else {
					// if data has been returned
						// broadcast event
						// added the fan
						obj.getCore().broadcast( 'onUserFansPreviewReceived', json.contents );
					}
					return false;
				}
			}, "json");
		},
		
		/**
		*   addUserFan / removeUserFan
		*
		*   Adds / Removes a users fan
		*
		*	@param	id - user id
		*	@param	f - fan id
		*/
		addUserFan: function(id, f)
		{
			//alert( "adding user fan : " + id + " : " + f );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax.php", {
				cmd: "add_user_fan",
				userId: id,
				fanId: f
			}, function(json) {
				//alert( "received : " + json.contents );
				
				// check for all err types
				// @@ error - user already has this fan
				if ( json.contents == "ErrorUserAlreadyHasFan" ) {
					var err = new ErrorData();
					err.setErrorType( json.contents );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						// unable to add the fan
						obj.getCore().broadcast( 'onUserFanAddFailed', json.contents );
					} else {
					// if data has been returned
						// broadcast event
						// added the fan
						obj.getCore().broadcast( 'onUserFanAddSuccess', json.contents );
					}
					return false;
				}
			}, "json");
		},
		
		removeUserFan: function(id, f)
		{
			//alert( "removing user fan : " + id + " : " + f );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax.php", {
				cmd: "remove_user_fan",
				userId: id,
				fanId: f
			}, function(json) {
				//alert( "received : " + json.contents );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// do nothing
				} else {
				// if data has been returned
				}
				return false;
			}, "json");
		},
		
		/**
		*   searchUsersByName
		*
		*   Searches users by passed name
		*
		*	@param	t - user type id
		*	@param	n - search name string
		*	@Param	m - max results
		*	@param	p - page number
		*/
		searchUsersByName: function(t, n, m, p)
		{
			//alert( "search by name of user type : " + t + " : " + n + " : " + m + " : " + p );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "search_users_by_name",
				userType: t,
				searchName: n,
				searchMax: m,
				searchPage: p
			}, function(json) {
				//alert( "received : " + json );
				
				if ( json.contents.error != null ) {
				// if there has been no xml returned
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onSearchByNameFailed', json.contents );
				} else {
				// if data has been returned
				
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onSearchByNameSuccess', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   searchUsersByFirstLetter
		*
		*   Searches users by passed first letter
		*
		*	@param	t - user type id
		*	@param	n - search letter string
		*	@Param	m - max results
		*	@param	p - page number
		*/
		searchUsersByFirstLetter: function(t, n, m, p)
		{
			//alert( "search by first letter of user type : " + t + " : " + n + " : " + m + " : " + p );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "search_users_by_first_letter",
				userType: t,
				searchLetter: n,
				searchMax: m,
				searchPage: p
			}, function(json) {
				//alert( "received : " + json );
				
				if ( json.contents.error != null ) {
				// if there has been no xml returned
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onSearchByFirstLetterFailed', json.contents );
				} else {
				// if data has been returned
				
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onSearchByFirstLetterSuccess', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   searchUsersByCategory
		*
		*   Searches users by passed category
		*
		*	@param	t - user type id
		*	@param	n - cat index
		*	@Param	m - max results
		*	@param	p - page number
		*/
		searchUsersByCategory: function(t, n, m, p)
		{
			//alert( "search by cat of user type : " + t + " : " + n + " : " + m + " : " + p );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "search_users_by_category",
				userType: t,
				searchCategory: n,
				searchMax: m,
				searchPage: p
			}, function(json) {
				//alert( "received : " + json );
				
				if ( json.contents.error != null ) {
				// if there has been no xml returned
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onSearchByCategoryFailed', json.contents );
				} else {
				// if data has been returned
				
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onSearchByCategorySuccess', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   getUserCategories
		*
		*   Gets a list of user categories from the database
		*
		*	@param	ut - user type
		*/
		getUserCategories: function(ut)
		{
			//alert( this.getType() + " getting categories" );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "get_user_categories",
				userType: ut
			}, function(json) {
				//alert( "received : " + json );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						// unable to add the fan
						obj.getCore().broadcast( 'onGetUserCategoriesFailed', json.contents );
					} else {
					// if data has been returned
						// broadcast event
						// added the fan
						obj.getCore().broadcast( 'onGetUserCategoriesSuccess', json.contents );
					}
					return false;
				}
			}, "json");
		},
		
		/**
		*   applyForMissUKFF
		*
		*   Applies for Miss UKFF casting
		*
		*	@param	u - user id
		*	@param	s - suject
		*	@param	m - message
		*/
		applyForMissUKFF: function(u, s, m)
		{
			//alert( "applying for miss UKFF : " + u + " : " + s + " : " + m );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "apply_for_missukff_foc",
				userId: u,
				subject: s,
				message: m
			}, function(json) {
				//alert( "received : " + json );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						obj.getCore().broadcast( 'onApplyForMissUKFFFailed', json.contents );
					} else {
					// if data has been returned
						// broadcast event
						obj.getCore().broadcast( 'onApplyForMissUKFFSuccess', json.contents );
					}
					return false;
				}
			}, "json");
		},
		
		/**
		*   changePage
		*
		*   Changes the page to a passed URL
		*
		*	@param	url - target url
		*/
		changePage: function(url)
		{
			window.location = url;
		},
		
		/**
		*   changePageOnDelay
		*
		*   Changes the page to a passed URL after a passed delay
		*
		*	@param	url - target url
		*	@param	d - delay ( in seconds )
		*/
		changePageOnDelay: function(url, d)
		{
			//alert( "delayed page change : " + url );
			
			if ( this.delay != null )
				clearTimeout( this.delay );
				
			this.delay = setTimeout( 'app.generalControl.changePage( "' + url + '" );', d*1000 );
		},
		
		/**
		*   showMessage / hideMessage
		*
		*   Shows a new message to the user
		*
		*	@param	m - message data object
		*/
		showMessage: function(m)
		{
			this.getCore().broadcast( 'onMessageShow', m );
		},
		
		hideMessage: function(m)
		{
			this.getCore().broadcast( 'onMessageHide', m );
		},
		
		/**
		*   showDialogueMessage / hideDialogueMessage
		*
		*   Shows a new dialogue message to the user
		*
		*	@param	m - message data object
		*/
		showDialogueMessage: function(m)
		{
			this.getCore().broadcast( 'onDialogueMessageShow', m );
		},
		
		hideDialogueMessage: function(m)
		{
			this.getCore().broadcast( 'onDialogueMessageHide', m );
		},
		
		/**
		*   openWizard / closeWizard
		*
		*   Broadcasts event - Opens / Closes the upload wizard
		*
		*	@param	id - wizard id
		*/
		openWizard: function(id)
		{
			this.getCore().broadcast( 'onWizardOpened', id );
		},
		
		closeWizard: function(id)
		{
			this.getCore().broadcast( 'onWizardClosed', id );
		},
		
		/**
		*   openDialogue / closeDialogue
		*
		*   Broadcasts event - Opens / Closes a passed dialogue
		*
		*	@param	id - dialogue id
		*	@param	s - solo boolean
		*	@param	c - content data
		*/
		openDialogue: function(id, s, c)
		{
			//alert( "opening dialogue " + id + " : " + s + " : " + c );
			
			var toOpen = new DialogueData();
				toOpen.setDialogId( id );
				toOpen.setSolo( s );
				toOpen.setContentData( c );
				
			this.getCore().broadcast( 'onDialogueOpened', toOpen );
		},
		
		closeDialogue: function(id)
		{
			//alert( "closing dialogue " + id );
			
			var toClose = new DialogueData();
				toClose.setDialogId( id );
				
			this.getCore().broadcast( 'onDialogueClosed', toClose );
		}
	}
);

/**
*	PhotosControl
*/
var PhotosControl = AbstractControl.extend(
	{
		/**
		*   init
		*
		*   Constructs the core
		*
		*	@param	c - model instance
		*/
		init: function(c)
		{
			this._super( "PhotosControl", c );
		},
		
		/**
		*   getProfilePhotos
		*
		*   Gets an array of profile photo objects for a passed profile id
		*
		*	@param	t - users type
		*	@param	id - profile id
		*/
		getProfilePhotos: function(t, id)
		{
			//alert( this.getType() + " getting profile photos from id : " + id + " of user type " + t );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_photos.php", {
				cmd: "get_prof_photos",
				userType: t,
				profileId: id
			}, function(json) {
				//alert( obj.getType() + " received : " + json.contents );
				
				if ( json.contents.error != null ) {
				// if there has been an error
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
				// if data has been returned
				
					var photos = new Array();
					for ( i=0; i<json.contents.photos.length; i++ )
					{
						var photoData = new PhotoData();
						if ( json.contents.photos[i].photo_id != null )		photoData.setPhotoId( json.contents.photos[i].photo_id );
						if ( json.contents.photos[i].file != null )			photoData.setFile( json.contents.photos[i].file );
						photos.push( photoData );
					}
					
					var photosData = new PhotosData();
					photosData.setPhotos( photos );
					
					//alert( photosData.photos.length + " photos received" );
					
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onProfilePhotosReceived', photosData );
				}
				return false;
			}, "json");
		},
		
		/**
		*   getPrivateProfilePhotos
		*
		*   Gets an array of private profile photo objects for a passed profile id
		*
		*	@param	t - users type
		*	@param	id - profile id
		*/
		getPrivateProfilePhotos: function(t, id)
		{
			//alert( "getting private profile photos from id : " + id + " of user type " + t );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_photos.php", {
				cmd: "get_private_prof_photos",
				userType: t,
				profileId: id
			}, function(json) {
				//alert( "received : " + json );
				
				if ( json.contents.error != null ) {
				// if there has been no xml returned
					// broadcast event
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
				} else {
				// if data has been returned
				
					var photos = new Array();
					for ( i=0; i<json.contents.photos.length; i++ )
					{
						var photoData = new PrivatePhotoData();
						if ( json.contents.photos[i].photo_id != null )		photoData.setPhotoId( json.contents.photos[i].photo_id );
						if ( json.contents.photos[i].file != null )			photoData.setFile( json.contents.photos[i].file );
						if ( json.contents.photos[i].label != null )		photoData.setLabel( json.contents.photos[i].label );
						photos.push( photoData );
					}
					
					var photosData = new PrivatePhotosData();
					photosData.setPhotos( photos );
					
					//alert( photosData.photos.length + " photos received" );
					
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onPrivateProfilePhotosReceived', photosData );
				}
				return false;
			}, "json");
		},
		
		/**
		*   setPhotoAsProfileAndThumb
		*
		*   Sets the passed photo as users profile & thumbnail image
		*
		*	@param	t - user type
		*	@param	u - user id
		*	@param	id - photo id
		*/
		setPhotoAsProfileAndThumb: function(t, u, id)
		{
			//alert( "setting as profile : " + t + " : " + u + " : " + id );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_photos.php", {
				cmd: "set_profile_thumbnail_photo",
				userType: t,
				userId: u,
				photoId: id
			}, function(json) {
				//alert( "received : " + json.contents );
				if ( json.contents == 0 ) {
					obj.getCore().broadcast( 'onSetProfileThumbPhotoFailed', json.contents );
				} else {
					obj.getCore().broadcast( 'onSetProfileThumbPhotoSuccess', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   setPhotoAsProfile
		*
		*   Sets the passed photo as users profile image
		*
		*	@param	t - user type
		*	@param	u - user id
		*	@param	id - photo id
		*/
		setPhotoAsProfile: function(t, u, id)
		{
			//alert( "setting as profile : " + t + " : " + u + " : " + id );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_photos.php", {
				cmd: "set_profile_photo",
				userType: t,
				userId: u,
				photoId: id
			}, function(json) {
				//alert( "received : " + json.contents );
				if ( json.contents == 0 ) {
					obj.getCore().broadcast( 'onSetProfilePhotoFailed', json.contents );
				} else {
					obj.getCore().broadcast( 'onSetProfilePhotoSuccess', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   setPhotoAsThumbnail
		*
		*   Sets the passed photo as users thumbnail image
		*
		*	@param	t - user type
		*	@param	u - user id
		*	@param	id - photo id
		*/
		setPhotoAsThumbnail: function(t, u, id)
		{
			//alert( "setting as thumbnail : " + t + " : " + u + " : " + id );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_photos.php", {
				cmd: "set_thumbnail_photo",
				userType: t,
				userId: u,
				photoId: id
			}, function(json) {
				//alert( "received : " + json.contents );
				if ( json.contents == 0 ) {
					obj.getCore().broadcast( 'onSetThumbPhotoFailed', json.contents );
				} else {
					obj.getCore().broadcast( 'onSetThumbPhotoSuccess', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   deletePhoto
		*
		*   Deletes a passed photo
		*
		*	@param	uid - user id
		*	@param	id - photo id
		*/
		deletePhoto: function(uid, id)
		{
			//alert( "deleting photo : " + id + " from user : " + uid );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_photos.php", {
				cmd: "delete_photo",
				userId: uid,
				photoId: id
			}, function(json) {
				//alert( "received : " + json.contents );
				if ( json.contents == 0 ) {
					obj.getCore().broadcast( 'onDeletePhotoFailed', json.contents );
				} else {
					obj.getCore().broadcast( 'onDeletePhotoSuccess', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   getPrivatePhoto
		*
		*   Returns a private photo data object
		*
		*	@param	uid - logged in users id
		*	@param	pid - photo id
		*/
		getPrivatePhoto: function(uid, pid)
		{
			//alert( "getting private photo : " + pid + " for user " + uid );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_photos.php", {
				cmd: "get_private_photo",
				userId: uid,
				photoId: pid
			}, function(json) {
				//alert( "received : " + json );
				
				if ( json.contents.error != null ) {
				// if there has been no xml returned
					// broadcast event
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
				} else {
				// if data has been returned
				
					var photoData = new PrivatePhotoData();
					if ( json.contents.photo.photo_id != null )		photoData.setPhotoId( json.contents.photo.photo_id );
					if ( json.contents.photo.file != null )			photoData.setFile( json.contents.photo.file );
					if ( json.contents.photo.label != null )		photoData.setLabel( json.contents.photo.label );
					
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onProfilePhotoReceived', photoData );
				}
				return false;
			}, "json");
		},
		
		/**
		*   requestPrivatePhotoView
		*
		*   Handles the logic to request a private photo view
		*
		*	@param	pid - photo id
		*/
		requestPrivatePhotoView: function(pid)
		{
			//alert( "requesting private photo view for : " + pid );
			
			var obj = this;
			
			// check there is a user logged in
			if ( !this.getCore().getUserProfile() ) {
				// broadcast event
				var err = new ErrorData();
				err.setErrorType( "ErrorPrivateViewNoLoggedUser" );
				obj.getCore().broadcast( 'onErrorDetected', err );
				return;
			}
			
			// serialize the users allocated private views
			var allowedToView = String( this.core.userProfile.getUserPrivatePhotoViews() ).split( "," );
			var found = false;
			for ( var i in allowedToView ) 
			{
				if ( allowedToView[i] == pid )
					found = true;
			}
			
			if ( found ) {
			// if the user can view the photo
				// broadcast event - user can view photo
				obj.getCore().broadcast( 'onPrivatePhotoViewRequestGranted', pid );
			} else {
			// the user is not able to see this photo
				// broadcast event - user cannot view photo
				obj.getCore().broadcast( 'onPrivatePhotoViewRequestDenied', pid );
				return;
			}
		},
		
		/**
		*   viewPrivatePhoto
		*
		*   Initiates the view of a private photo
		*
		*	@param	uid - logged in users id
		*	@param	pid - photo id
		*/
		viewPrivatePhoto: function(uid, pid)
		{
			//alert( "viewing private photo : " + pid + " for user " + uid );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_photos.php", {
				cmd: "view_private_photo",
				userId: uid,
				photoId: pid
			}, function(json) {
				//alert( "received : " + json );
				
				if ( json.contents.error != null ) {
				// if there has been no xml returned
					// broadcast event
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
				} else {
				// if data has been returned
					if ( json.contents == 0 ) {
						obj.getCore().broadcast( 'onPrivatePhotoViewFailed', pid );
					} else {
						obj.getCore().broadcast( 'onPrivatePhotoViewReceived', pid );
					}
				}
				return false;
			}, "json");/**/
		},
		
		/**
		*   requestPrivatePhotoLabelChange
		*
		*   Handles the logic to request a private photo label change
		*
		*	@param	pid - photo id
		*	@param	l - label
		*/
		requestPrivatePhotoLabelChange: function(pid, l)
		{
			//alert( "requesting private photo view for : " + pid );
			
			var obj = this;
			
			// check there is a user logged in
			if ( !this.getCore().getUserProfile() ) {
				// broadcast event
				var err = new ErrorData();
				err.setErrorType( "ErrorPrivateViewNoLoggedUser" );
				obj.getCore().broadcast( 'onErrorDetected', err );
				return;
			}
			
			// set the photo data object
			var photoData = new PrivatePhotoData();
				photoData.setPhotoId( pid );
				photoData.setLabel( l );
			
			// broadcast event
			this.getCore().broadcast( 'onPrivatePhotoLabelChangeRequestGranted', photoData );
		},
		
		/**
		*   editPhotoLabel
		*
		*   Handles the logic to edit a photo label
		*
		*	@param	pid - photo id
		*	@param	l - label
		*/
		editPhotoLabel: function(pid, l)
		{
			//alert( "editing label of photo : " + pid + " with label " + l );
			
			// set the photo data object
			var photoData = new PrivatePhotoData();
				photoData.setPhotoId( pid );
				photoData.setLabel( l );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_photos.php", {
				cmd: "edit_private_photo_label",
				photoId: pid,
				photoLabel: l
			}, function(json) {
				//alert( "received : " + json );
				
				if ( json.contents.error != null ) {
				// if there has been no xml returned
					// broadcast event
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
				} else {
				// if data has been returned
					if ( json.contents == 0 ) {
						obj.getCore().broadcast( 'onPrivatePhotoLabelEditFailed', photoData );
					} else {
						obj.getCore().broadcast( 'onPrivatePhotoLabelEditSuccess', photoData );
					}
				}
				return false;
			}, "json");
		}
	}
);

/**
*	ForumControl
*/
var ForumControl = AbstractControl.extend(
	{
		/**
		*   init
		*
		*   Constructs the core
		*
		*	@param	c - model instance
		*/
		init: function(c)
		{
			this._super( "ForumControl", c );
		},
		
		/**
		*   reportPost
		*
		*   Notifys the administrator - report sender
		*
		*	@param	id - author id
		*	@param	p - post body
		*/
		reportPost: function(id, p)
		{
			//alert( "reporting author : " + id + " : " + p );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax.php", {
				cmd: "report_forum_post_author",
				authorId: id,
				postBody: p
			}, function(json) {
				//alert( "received : " + json.contents + " : " + id );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// do nothing
				} else {
				// if data has been returned
					
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onForumPostReported', { authorId: id, postBody: p } );
					// post a message
					var msge = new MessageData();
					msge.setMessageType( "good" );
					msge.setMessage( "Thankyou. The sender has been reported" );
					obj.getCore().broadcast( 'onMessageShow', msge );
				}
				return false;
			}, "json");
		},
		
		/**
		*   sendReply
		*
		*   Sends a reply post
		*
		*	@param	f - forum id
		*	@param	p - post id
		*	@param	b - post body
		*	@param	a - post author
		*	@param	d - target forum user
		*/
		sendReply: function(f, p, b, a, d)
		{
			//alert( "sending post : " + f + " : " + p + " : " + b + " : " + a + " : " + d );
			
			// broadcast request event
			this.getCore().broadcast( 'onForumReplyPostRequest', { forumId: f, postId: p } );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax.php", {
				cmd: "send_forum_post",
				forumId: f,
				postId: p,
				postBody: b,
				postAuthor: a,
				targetUser: d
			}, function(json) {
				//alert( "received : " + json.contents + " : " + f + " : " + p );
				
				switch ( json.contents )
				{
					// @@ case - swear word has been detected
					case "ErrorSwearWordDetected":
					
						var err = new ErrorData();
						err.setErrorType( json.contents );
						obj.getCore().broadcast( 'onErrorDetected', err );
						return false;
					
						break;
						
					// @@ case - no quota remaining to send post
					case "ErrorNoForumPostsRemaining":
					
						var err = new ErrorData();
						err.setErrorType( json.contents );
						obj.getCore().broadcast( 'onErrorDetected', err );
						return false;
					
						break;
						
					// @@ case - everything is okay, post was sent
					default:
					
						if ( json.contents == 0 ) {
						// if there has been no xml returned
							// do nothing
						} else {
						// if data has been returned
							
							// broadcast event
							// the forum data has been received
							obj.getCore().broadcast( 'onForumReplyPosted', { forumId: f, postId: p } );
							// post a message
							var msge = new MessageData();
							msge.setMessageType( "good" );
							msge.setMessage( "Your reply has been sent" );
							obj.getCore().broadcast( 'onMessageShow', msge );
						}
						return false;
					
						break;
				}
			}, "json");
		},
		
		/**
		*   postMessage
		*
		*   Posts a message onto a forum 
		*
		*	@param	f - forum id
		*	@param	b - post body
		*/
		postMessage: function(f, b)
		{
			//alert( "posting message : " + f + " : " + b );
			
			// broadcast request event
			this.getCore().broadcast( 'onForumMessagePostRequest', { forumId: f } );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax.php", {
				cmd: "post_forum_message",
				forumId: f,
				postBody: b
			}, function(json) {
				//alert( "received : " + json.contents );
				
				switch ( json.contents )
				{
					// @@ case - swear word has been detected
					case "ErrorSwearWordDetected":
					
						var err = new ErrorData();
						err.setErrorType( json.contents );
						obj.getCore().broadcast( 'onErrorDetected', err );
						return false;
					
						break;
						
					// @@ case - no quota remaining to send post
					case "ErrorNoForumPostsRemaining":
					
						var err = new ErrorData();
						err.setErrorType( json.contents );
						obj.getCore().broadcast( 'onErrorDetected', err );
						return false;
					
						break;
						
					// @@ case - everything is okay, post was sent
					default:
					
						if ( json.contents == 0 ) {
						// if there has been no xml returned
							// do nothing
						} else {
						// if data has been returned
							
							// broadcast event
							// the forum data has been received
							obj.getCore().broadcast( 'onForumMessagePosted', { forumId: f } );
							
							// post a message
							var msge = new MessageData();
							msge.setMessageType( "good" );
							msge.setMessage( "Your message has been posted" );
							obj.getCore().broadcast( 'onMessageShow', msge );
						}
						return false;
					
						break;
				}
			}, "json");/**/
		},
		
		/**
		*   deletePost
		*
		*   Deletes a post
		*
		*	@param	f - forum id
		*	@param	p - post id
		*/
		deletePost: function(f, p)
		{
			//alert( "deleting post : " + f + " : " + p );
			
			// broadcast request event
			this.getCore().broadcast( 'onForumPostDeleteRequest', { forumId: f, postId: p } );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax.php", {
				cmd: "delete_forum_post",
				forumId: f,
				postId: p
			}, function(json) {
				//alert( "received : " + json + " : " + f + " : " + p );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// do nothing
				} else {
				// if data has been returned
					
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onForumPostDeleted', { forumId: f, postId: p } );
					
					// post a message
					var msge = new MessageData();
					msge.setMessageType( "good" );
					msge.setMessage( "Your post has been deleted" );
					obj.getCore().broadcast( 'onMessageShow', msge );
				}
				return false;
			}, "json");
		},
		
		/**
		*   getForumData
		*
		*   Gets all the forum data
		*
		*	@param	o - forum owner id
		*/
		getForumData: function(o)
		{
			//alert( "getting forum data : " + o );
			
			var posts = [];
			var obj = this;
			//alert( "posting..." );
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax.php", {
				cmd: "get_forum_data",
				ownerId: o
			}, function(json) {
				//alert( "received : " + json );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					var forumData = new ForumData();
					forumData.setForumActive( false );
					// broadcast event
					// this user has no forum
					obj.getCore().broadcast( 'onForumDataReceived', forumData );
				} else {
				// if data has been returned
				
					//alert( "posts : " + json.contents.forum.posts );
				
					for ( var i=0; i<json.contents.forum.posts.length; i++ )
					{
						//alert( "building data object " + json.contents.forum.posts[i] );
						
						// populate a forum post data object
						var pData = new ForumPostData();
						pData.setPostId( json.contents.forum.posts[i].post_id );
						pData.setPost( json.contents.forum.posts[i].post );
						pData.setPostDate( json.contents.forum.posts[i].date );
						pData.setPostAuthor( json.contents.forum.posts[i].author_id );
						pData.setPostAuthorName( json.contents.forum.posts[i].author_name );
						
						// get the post author profile
						var userProfile = new UserProfileData();
						if ( json.contents.forum.posts[i].author_profile.profile.profile_id != null ) 			userProfile.setProfileId( json.contents.forum.posts[i].author_profile.profile.profile_id );
						if ( json.contents.forum.posts[i].author_profile.profile.user_id != null ) 				userProfile.setUserId( json.contents.forum.posts[i].author_profile.profile.user_id );
						if ( json.contents.forum.posts[i].author_profile.profile.user_type != null ) 			userProfile.setUserType( json.contents.forum.posts[i].author_profile.profile.user_type );
						if ( json.contents.forum.posts[i].author_profile.profile.user_type_string != null ) 	userProfile.setUserTypeString( json.contents.forum.posts[i].author_profile.profile.user_type_string );
						if ( json.contents.forum.posts[i].author_profile.profile.name != null ) 				userProfile.setName( json.contents.forum.posts[i].author_profile.profile.name );
						if ( json.contents.forum.posts[i].author_profile.profile.age != null ) 					userProfile.setAge( json.contents.forum.posts[i].author_profile.profile.age );
						if ( json.contents.forum.posts[i].author_profile.profile.location != null ) 			userProfile.setLocation( json.contents.forum.posts[i].author_profile.profile.location );
						if ( json.contents.forum.posts[i].author_profile.profile.height != null ) 				userProfile.setHeight( json.contents.forum.posts[i].author_profile.profile.height );
						if ( json.contents.forum.posts[i].author_profile.profile.eye_colour != null ) 			userProfile.setEyeColour( json.contents.forum.posts[i].author_profile.profile.eye_colour );
						if ( json.contents.forum.posts[i].author_profile.profile.chest != null ) 				userProfile.setChest( json.contents.forum.posts[i].author_profile.profile.chest );
						if ( json.contents.forum.posts[i].author_profile.profile.dress_size != null ) 			userProfile.setDressSize( json.contents.forum.posts[i].author_profile.profile.dress_size );
						if ( json.contents.forum.posts[i].author_profile.profile.category != null ) 			userProfile.setCategory( json.contents.forum.posts[i].author_profile.profile.category );
						if ( json.contents.forum.posts[i].author_profile.profile.biography != null ) 			userProfile.setBiography( json.contents.forum.posts[i].author_profile.profile.biography );
						if ( json.contents.forum.posts[i].author_profile.profile.message != null ) 				userProfile.setMessage( json.contents.forum.posts[i].author_profile.profile.message );
						if ( json.contents.forum.posts[i].author_profile.profile.profile_photo_id != null ) 	userProfile.setProfilePhotoId( json.contents.forum.posts[i].author_profile.profile.profile_photo_id );
						if ( json.contents.forum.posts[i].author_profile.profile.profile_photo != null ) 		userProfile.setProfilePhoto( json.contents.forum.posts[i].author_profile.profile.profile_photo );
						if ( json.contents.forum.posts[i].author_profile.profile.thumb_photo_id != null ) 		userProfile.setThumbPhotoId( json.contents.forum.posts[i].author_profile.profile.thumb_photo_id );
						if ( json.contents.forum.posts[i].author_profile.profile.thumb_photo != null ) 			userProfile.setThumbPhoto( json.contents.forum.posts[i].author_profile.profile.thumb_photo );
						if ( json.contents.forum.posts[i].author_profile.profile.url != null ) 					userProfile.setURL( json.contents.forum.posts[i].author_profile.profile.url );
						if ( json.contents.forum.posts[i].author_profile.profile.directory != null ) 			userProfile.setDirectory( json.contents.forum.posts[i].author_profile.profile.directory );
						if ( json.contents.forum.posts[i].author_profile.profile.user_directory != null ) 		userProfile.setUserDirectory( json.contents.forum.posts[i].author_profile.profile.user_directory );
						if ( json.contents.forum.posts[i].author_profile.profile.views != null ) 				userProfile.setViews( json.contents.forum.posts[i].author_profile.profile.views );
						if ( json.contents.forum.posts[i].author_profile.profile.photos != null ) 				userProfile.setPhotos( json.contents.forum.posts[i].author_profile.profile.photos );
						if ( json.contents.forum.posts[i].author_profile.profile.accolades != null ) 			userProfile.setAccolades( json.contents.forum.posts[i].author_profile.profile.accolades );
						
						pData.setPostAuthorProfile( userProfile );
							
						// add to posts array
						posts.push( pData );
					}
					
					var forumData = new ForumData();
					forumData.setForumActive( true );
					forumData.setForumId( json.contents.forum.id );
					forumData.setForumType( json.contents.forum.type );
					forumData.setForumOwner( json.contents.forum.owner );
					forumData.setForumPosts( posts );
					
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onForumDataReceived', forumData );
				}
				return false;
			}, "json");
		}
	}
);

/**
*	CommentsControl
*/
var CommentsControl = AbstractControl.extend(
	{
		/**
		*   init
		*
		*   Constructs the control
		*
		*	@param	c - model instance
		*/
		init: function(c)
		{
			this._super( "CommentsControl", c );
		},
		
		/**
		*   reportComment
		*
		*   Notifys the administrator - report sender
		*
		*	@param	id - author id
		*	@param	p - post body
		*/
		reportComment: function(id, p)
		{
			//alert( "reporting author : " + id + " : " + p );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax.php", {
				cmd: "report_comment_author",
				authorId: id,
				commentBody: p
			}, function(json) {
				//alert( "received : " + json.contents + " : " + id );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// do nothing
				} else {
				// if data has been returned
					
					// broadcast event
					// the comment data has been received
					obj.getCore().broadcast( 'onCommentReported', { authorId: id, commentBody: p } );
					// post a message
					var msge = new MessageData();
					msge.setMessageType( "good" );
					msge.setMessage( "Thankyou. The sender has been reported" );
					obj.getCore().broadcast( 'onMessageShow', msge );
				}
				return false;
			}, "json");
		},
		
		/**
		*   postPhotoComment
		*
		*   Posts a comment onto a photo comments panel 
		*
		*	@param	c - comments panel id
		*	@param	p - photo id
		*	@param	b - comment body
		*/
		postPhotoComment: function(c, p, b)
		{
			//alert( "posting comment : " + c + " : " + p + " : " + b );
			
			// broadcast request event
			this.getCore().broadcast( 'onCommentPostRequest', { photoId: p } );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax.php", {
				cmd: "post_photo_comment",
				panelId: c,
				photoId: p,
				commentBody: b
			}, function(json) {
				//alert( "received : " + json.contents + " : " + p );
				
				switch ( json.contents )
				{
					// @@ case - swear word has been detected
					case "ErrorSwearWordDetected":
					
						var err = new ErrorData();
						err.setErrorType( json.contents );
						obj.getCore().broadcast( 'onErrorDetected', err );
						return false;
					
						break;
						
					// @@ case - no quota remaining to send post
					case "ErrorNoCommentsRemaining":
					
						var err = new ErrorData();
						err.setErrorType( json.contents );
						obj.getCore().broadcast( 'onErrorDetected', err );
						return false;
					
						break;
						
					// @@ case - everything is okay, post was sent
					default:
					
						if ( json.contents == 0 ) {
						// if there has been no xml returned
							// do nothing
						} else {
						// if data has been returned
							
							// broadcast event
							// the comment data has been received
							obj.getCore().broadcast( 'onCommentPosted', { photoId: p } );
							
							// post a message
							var msge = new MessageData();
							msge.setMessageType( "good" );
							msge.setMessage( "Your comment has been posted" );
							obj.getCore().broadcast( 'onMessageShow', msge );
						}
						return false;
					
						break;
				}
			}, "json");/**/
		},
		
		/**
		*   deletePhotoComment
		*
		*   Deletes a photo comment
		*
		*	@param	p - photo id
		*	@param	c - comment id
		*/
		deletePhotoComment: function(p, c)
		{
			//alert( "deleting comment : " + p + " : " + c );
			
			// broadcast request event
			this.getCore().broadcast( 'onCommentDeleteRequest', { photoId: p, commentId: c } );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax.php", {
				cmd: "delete_photo_comment",
				photoId: p,
				commentId: c
			}, function(json) {
				//alert( "received : " + json + " : " + p + " : " + c );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// do nothing
				} else {
				// if data has been returned
					
					// broadcast event
					// the comment data has been received
					obj.getCore().broadcast( 'onCommentDeleted', { photoId: p, commentId: c } );
					
					// post a message
					var msge = new MessageData();
					msge.setMessageType( "good" );
					msge.setMessage( "Your comment has been deleted" );
					obj.getCore().broadcast( 'onMessageShow', msge );
				}
				return false;
			}, "json");
		},
		
		/**
		*   getPhotoCommentsData
		*
		*   Gets all the photo comment panel data for a passed photo
		*
		*	@param	o - comments panel owner id
		*	@param	p - photo id
		*/
		getPhotoCommentsData: function(o, p)
		{
			//alert( "getting comments data : " + o + " : " + p );
			
			var comments = [];
			var obj = this;
			//alert( "posting..." );
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax.php", {
				cmd: "get_photo_comments_data",
				ownerId: o,
				photoId: p
			}, function(json) {
				//alert( "received : " + json.contents );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					var commentsData = new PhotoCommentsData();
					commentsData.setCommentsActive( false );
					// broadcast event
					// this user has no comments panel
					obj.getCore().broadcast( 'onCommentsDataReceived', commentsData );
				} else {
				// if data has been returned
				
					for ( var i=0; i<json.contents.commentspanel.comments.length; i++ )
					{
						//alert( "building data object " + json.contents.commentspanel.comments[i] );
						
						// populate a comment photo comment data object
						var pData = new PhotoCommentData();
						pData.setCommentId( json.contents.commentspanel.comments[i].comment_id );
						pData.setComment( json.contents.commentspanel.comments[i].comment );
						pData.setCommentDate( json.contents.commentspanel.comments[i].date );
						pData.setCommentAuthor( json.contents.commentspanel.comments[i].author_id );
						pData.setCommentAuthorName( json.contents.commentspanel.comments[i].author_name );
						
						// get the post author profile
						var userProfile = new UserProfileData();
						if ( json.contents.commentspanel.comments[i].author_profile.profile.profile_id != null ) 		userProfile.setProfileId( json.contents.commentspanel.comments[i].author_profile.profile.profile_id );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.user_id != null ) 			userProfile.setUserId( json.contents.commentspanel.comments[i].author_profile.profile.user_id );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.user_type != null ) 		userProfile.setUserType( json.contents.commentspanel.comments[i].author_profile.profile.user_type );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.user_type_string != null ) 	userProfile.setUserTypeString( json.contents.commentspanel.comments[i].author_profile.profile.user_type_string );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.name != null ) 				userProfile.setName( json.contents.commentspanel.comments[i].author_profile.profile.name );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.age != null ) 				userProfile.setAge( json.contents.commentspanel.comments[i].author_profile.profile.age );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.location != null ) 			userProfile.setLocation( json.contents.commentspanel.comments[i].author_profile.profile.location );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.height != null ) 			userProfile.setHeight( json.contents.commentspanel.comments[i].author_profile.profile.height );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.eye_colour != null ) 		userProfile.setEyeColour( json.contents.commentspanel.comments[i].author_profile.profile.eye_colour );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.chest != null ) 			userProfile.setChest( json.contents.commentspanel.comments[i].author_profile.profile.chest );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.dress_size != null ) 		userProfile.setDressSize( json.contents.commentspanel.comments[i].author_profile.profile.dress_size );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.category != null ) 			userProfile.setCategory( json.contents.commentspanel.comments[i].author_profile.profile.category );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.biography != null ) 		userProfile.setBiography( json.contents.commentspanel.comments[i].author_profile.profile.biography );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.message != null ) 			userProfile.setMessage( json.contents.commentspanel.comments[i].author_profile.profile.message );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.profile_photo_id != null ) 	userProfile.setProfilePhotoId( json.contents.commentspanel.comments[i].author_profile.profile.profile_photo_id );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.profile_photo != null ) 	userProfile.setProfilePhoto( json.contents.commentspanel.comments[i].author_profile.profile.profile_photo );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.thumb_photo_id != null ) 	userProfile.setThumbPhotoId( json.contents.commentspanel.comments[i].author_profile.profile.thumb_photo_id );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.thumb_photo != null ) 		userProfile.setThumbPhoto( json.contents.commentspanel.comments[i].author_profile.profile.thumb_photo );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.url != null ) 				userProfile.setURL( json.contents.commentspanel.comments[i].author_profile.profile.url );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.directory != null ) 		userProfile.setDirectory( json.contents.commentspanel.comments[i].author_profile.profile.directory );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.user_directory != null ) 	userProfile.setUserDirectory( json.contents.commentspanel.comments[i].author_profile.profile.user_directory );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.views != null ) 			userProfile.setViews( json.contents.commentspanel.comments[i].author_profile.profile.views );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.photos != null ) 			userProfile.setPhotos( json.contents.commentspanel.comments[i].author_profile.profile.photos );
						if ( json.contents.commentspanel.comments[i].author_profile.profile.accolades != null ) 		userProfile.setAccolades( json.contents.commentspanel.comments[i].author_profile.profile.accolades );
						
						pData.setCommentAuthorProfile( userProfile );
							
						// add to comments array
						comments.push( pData );
					}
					
					var commentsData = new PhotoCommentsData();
					commentsData.setCommentsActive( true );
					commentsData.setCommentsId( json.contents.commentspanel.id );
					commentsData.setCommentsType( json.contents.commentspanel.type );
					commentsData.setCommentsOwner( json.contents.commentspanel.owner );
					commentsData.setCommentsPhoto( json.contents.commentspanel.photo );
					commentsData.setCommentsPosts( comments );
					
					// broadcast event
					// the comments panel data has been received
					obj.getCore().broadcast( 'onCommentsDataReceived', commentsData );
				}
				return false;
			}, "json");
		}
	}
);

/**
*	AdvertsControl
*/
var AdvertsControl = AbstractControl.extend(
	{
		/**
		*   init
		*
		*   Constructs the core
		*
		*	@param	c - model instance
		*/
		init: function(c)
		{
			this._super( "AdvertsControl", c );
			this.delay = null;
		},
		
		/**
		*   getAdPlacements
		*
		*   Gets a collection of all ad placements & their data
		*/
		getAdPlacements: function()
		{
			//alert( "getting ad placements" );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_adverts.php", {
				cmd: "get_ad_placements"
			}, function(json) {
				//alert( "received : " + json.contents );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// broadcast event
					// the ad data has been received
					obj.getCore().broadcast( 'onAdPlacementsReceived', json.contents );
				} else {
				// if data has been returned
					
					// broadcast event
					// the ad data has been received
					obj.getCore().broadcast( 'onAdPlacementsReceived', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   getAds
		*
		*   Gets a collection of all ads
		*
		*	@param	a - array of ad ids
		*	@param	p - array of placement ids
		*	@param	s - status
		*	@param	m - max
		*/
		getAds: function(a, p, s, m)
		{
			//alert( "getting all ads : " + a + " : " + p + " : " + s + " : " + m );
			
			// manage the arguments
			if ( a == null || a == undefined ) a = [0];
			if ( p == null || p == undefined ) p = [0];
			if ( s == null || s == undefined ) s = 700;
			if ( m == null || m == undefined ) m = 0;
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_adverts.php", {
				cmd: "get_ads",
				adIds: a,
				placementIds: p,
				status: s,
				maxAmt: m
			}, function(json) {
				//alert( "received : " + json.contents );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// broadcast event
					// the ad data has been received
					obj.getCore().broadcast( 'onAdsReceived', json.contents );
				} else {
				// if data has been returned
				
					// broadcast event
					// the ad data has been received
					obj.getCore().broadcast( 'onAdsReceived', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   getAssignedAds
		*
		*   Gets a collection of assigned ads based upon a passed placement id
		*
		*	@param	p - placement id
		*/
		getAssignedAds: function(p)
		{
			//alert( "getting assigned ads for placement : " + p );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_adverts.php", {
				cmd: "get_assigned_ads",
				placementId: p
			}, function(json) {
				//alert( "received : " + json.contents );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// broadcast event
					// the ad data has been received
					obj.getCore().broadcast( 'onAssignedAdsReceived', json.contents );
				} else {
				// if data has been returned
				
					// broadcast event
					// the ad data has been received
					obj.getCore().broadcast( 'onAssignedAdsReceived', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   getUnassignedAds
		*
		*   Gets a collection of un-assigned ads based upon a passed placement id
		*
		*	@param	p - placement id
		*/
		getUnassignedAds: function(p)
		{
			//alert( "getting un-assigned ads for placement : " + p );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_adverts.php", {
				cmd: "get_un-assigned_ads",
				placementId: p
			}, function(json) {
				//alert( "received : " + json.contents );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// broadcast event
					// the ad data has been received
					obj.getCore().broadcast( 'onUnAssignedAdsReceived', json.contents );
				} else {
				// if data has been returned
				
					// broadcast event
					// the ad data has been received
					obj.getCore().broadcast( 'onUnAssignedAdsReceived', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   suspendAd
		*
		*   Removes an ad from a placement
		*
		*	@param	id - ad id
		*	@param	p - placement id
		*/
		suspendAd: function(id, p)
		{
			//alert( "suspending ad : " + id + " : " + p );
			
			if ( !id )
				return;
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_adverts.php", {
				cmd: "suspend_ad",
				adId: id,
				placementId: p
			}, function(json) {
				//alert( "received : " + json.contents );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// broadcast event
					// the ad data has been received
					obj.getCore().broadcast( 'onAdSuspended', json.contents );
				} else {
				// if data has been returned
				
					// broadcast event
					// the ad data has been received
					obj.getCore().broadcast( 'onAdSuspended', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   assignAd
		*
		*   Assigns an ad to a placement
		*
		*	@param	id - ad id
		*	@param	p - placement id
		*/
		assignAd: function(id, p)
		{
			//alert( "assigning ad : " + id + " : " + p );
			
			if ( !id || !p )
				return;
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_adverts.php", {
				cmd: "assign_ad",
				adId: id,
				placementId: p
			}, function(json) {
				//alert( "received : " + json.contents );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// broadcast event
					// the ad data has been received
					obj.getCore().broadcast( 'onAdAssigned', json.contents );
				} else {
				// if data has been returned
				
					// broadcast event
					// the ad data has been received
					obj.getCore().broadcast( 'onAdAssigned', json.contents );
				}
				return false;
			}, "json");
		}
	}
);

/**
*	CreditControl
*/
var CreditControl = AbstractControl.extend(
	{
		/**
		*   init
		*
		*   Constructs the core
		*
		*	@param	c - model instance
		*/
		init: function(c)
		{
			this._super( "CreditControl", c );
			
			// define static props
			this.CHARGE_CODE_PHOTO_UPLOAD 			= "photo_upload";
			this.CHARGE_CODE_PRIVATE_GALLERY_VIEW 	= "private_gallery_view";
			this.CHARGE_CODE_POST_CASTING 			= "post_casting";
			this.CHARGE_CODE_APPLY_CASTING 			= "apply_casting";
			this.CHARGE_CODE_APPLY_MISSUKFF 		= "apply_missukff";
			this.CHARGE_CODE_FEATURE_PRIMARY 		= "feature_primary";
			this.CHARGE_CODE_FEATURE_STANDARD 		= "feature_standard";
		},
		
		/**
		*   managePaypalCreditTX
		*
		*   Manages the paypal credit transaction
		*
		*	@param	uid - user id
		*	@param	tx - paypal transaction code
		*/
		managePaypalCreditTX: function(uid, tx)
		{
			//alert( "managing credit purchse from paypal transaction : " + tx + " for user : " + uid );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_credits.php", {
				cmd: "manage_pp_credit_purchase",
				userId: uid,
				paypalTX: tx
			}, function(json) {
				//alert( "received : " + json.contents );
				
				// check for all err types
				// @@ error - user already has this fan
				if ( json.contents.error != null ) {
				// if an error has occured
					// broadcast event
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						// unable to add the fan
						obj.getCore().broadcast( 'onPaypalCreditTXManagementFailed', json.contents );
					} else {
					// if data has been returned
						// broadcast event
						// added the fan
						obj.getCore().broadcast( 'onPaypalCreditTXManagementSuccess', json.contents );
					}
					return false;
				}
			}, "json");
		},
		
		/**
		*   manageCreditPurchase
		*
		*   Manages a standard credit purchase
		*
		*	@param	uid - user id
		*	@param	c - amount of credits purchased
		*	@param	a - amount of add ons awarded
		*/
		manageCreditPurchase: function(uid, c, a)
		{
			//alert( "managing standard credit purchase of " + c + " credits, and " + a + " add-ons for user : " + uid );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_credits.php", {
				cmd: "manage_credit_purchase",
				userId: uid,
				credits: c,
				addOn: a
			}, function(json) {
				//alert( "received : " + json.contents );
				
				// check for all err types
				// @@ error - user already has this fan
				if ( json.contents.error != null ) {
				// if an error has occured
					// broadcast event
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						// unable to add the fan
						obj.getCore().broadcast( 'onManageCreditPurchaseFailed', json.contents );
					} else {
					// if data has been returned
						// broadcast event
						// added the fan
						obj.getCore().broadcast( 'onManageCreditPurchaseSuccess', json.contents );
					}
					return false;
				}
			}, "json");
		},
		
		/**
		*   getCreditTopUpProducts
		*
		*   Returns a list of credit top up products from the database
		*/
		getCreditTopUpProducts: function()
		{
			//alert( "getting credit top up products" );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_credits.php", {
				cmd: "get_credit_top_up_products"
			}, function(json) {
				//alert( "received : " + json );
				
				if ( json.contents.error != null ) {
				// if there has been no xml returned
					// broadcast event
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
				// if data has been returned
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						// unable to add the fan
						obj.getCore().broadcast( 'onGetCreditTopUpProductsFailed', json.contents );
					} else {
					// if data has been returned
						// broadcast event
						// added the fan
						obj.getCore().broadcast( 'onGetCreditTopUpProductsSuccess', json.contents );
					}
				}
				return false;
			}, "json");
		},
		
		/**
		*   addCredits
		*
		*   Adds an amt of credits to a specified user
		*
		*	@param	uid - user id
		*	@param	c - amt of credits
		*/
		addCredits: function(uid, c)
		{
			//alert( "adding : " + c + " credits for user " + uid );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_credits.php", {
				cmd: "add_credits",
				userId: uid,
				credits: c
			}, function(json) {
				//alert( "received : " + json );
				
				if ( json.contents.error != null ) {
				// if there has been no xml returned
					// broadcast event
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
				// if data has been returned
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						// unable to add the fan
						obj.getCore().broadcast( 'onAddCreditsFailed', json.contents );
					} else {
					// if data has been returned
						// broadcast event
						// added the fan
						obj.getCore().broadcast( 'onAddCreditsSuccess', json.contents );
					}
				}
				return false;
			}, "json");
		},
		
		/**
		*   chargeCredits
		*
		*   Removes an amt of credits from a specified user
		*
		*	@param	uid - user id
		*	@param	c - credit charge code
		*/
		chargeCredits: function(uid, c)
		{
			//alert( "removing : " + c + " credits for user " + uid );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_credits.php", {
				cmd: "charge_credits",
				userId: uid,
				chargeCode: c
			}, function(json) {
				//alert( "received : " + json );
				
				if ( json.contents.error != null ) {
				// if there has been no xml returned
					// broadcast event
					// broadcast event
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
				// if data has been returned
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						// unable to add the fan
						obj.getCore().broadcast( 'onChargeCreditsFailed', json.contents );
					} else {
					// if data has been returned
						// broadcast event
						// added the fan
						obj.getCore().broadcast( 'onChargeCreditsSuccess', json.contents );
					}
				}
				return false;
			}, "json");
		},
		
		/**
		*   getCreditCharge
		*
		*   Returns a credit charge based upon a passed charge code
		*
		*	@param	c - credit charge request data object
		*/
		getCreditCharge: function(c)
		{
			//alert( this.getType() + " getting credit charge for " + c.getCode() );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_credits.php", {
				cmd: "get_credit_charge",
				chargeCode: c.getCode()
			}, function(json) {
				//alert( "received : " + json );
				
				if ( json.contents.error != null ) {
				// if there has been no xml returned
					// broadcast event
					// broadcast event
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
				// if data has been returned
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						// unable to add the fan
						obj.getCore().broadcast( 'onCreditChargeRequestFailed', c );
					} else {
					// if data has been returned
						// append to the credit charge data
						c.setChargeId( json.contents.creditCharge.chargeId );
						c.setName( json.contents.creditCharge.name );
						c.setCode( json.contents.creditCharge.code );
						c.setCharge( json.contents.creditCharge.charge );
						// broadcast event
						// added the fan
						obj.getCore().broadcast( 'onCreditChargeRequestReceived', c );
					}
				}
				return false;
			}, "json");
		},
		
		/**
		*   acceptCreditCharge / declineCreditCharge
		*
		*   Broadcasts event - Accepts / Declines a credit charge
		*
		*	@param	d - credit charge data
		*/
		acceptCreditCharge: function(d)
		{
			this.getCore().broadcast( 'onCreditChargeAccepted', d );
		},
		
		declineCreditCharge: function(d)
		{
			this.getCore().broadcast( 'onCreditChargeDeclined', d );
		}
	}
);

/**
*	AnnouncementsControl
*/
var AnnouncementsControl = AbstractControl.extend(
	{
		/**
		*   init
		*
		*   Constructs the core
		*
		*	@param	c - model instance
		*/
		init: function(c)
		{
			this._super( "AnnouncementsControl", c );
		},
		
		/**
		*   postAnnouncement
		*
		*   Posts an announcement 
		*
		*	@param	a - announcement
		*/
		postAnnouncement: function(a)
		{
			//alert( "posting announcement : " + a );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "post_announcement",
				announcementBody: a
			}, function(json) {
				//alert( "received : " + json.contents + " : " + json.contents.error + " : " + json.contents.data.timeout );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
						err.setErrorType( json.contents.error );
						err.setErrorData( json.contents.data );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no data returned
						// broadcast event
						// the forum data has been received
						obj.getCore().broadcast( 'onAnnouncementPostFailed', null );
					} else {
					// if data has been returned
						
						// broadcast event
						// the forum data has been received
						obj.getCore().broadcast( 'onAnnouncementPostSuccess', null );
					}
					return false;
				}
			}, "json");/**/
		},
		
		/**
		*   deleteAnnouncement
		*
		*   Deletes a post
		*
		*	@param	id - announcement id
		*/
		deleteAnnouncement: function(id)
		{
			//alert( "deleting post : " + f + " : " + p );
			
			// broadcast request event
			/*this.getCore().broadcast( 'onForumPostDeleteRequest', { forumId: f, postId: p } );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax.php", {
				cmd: "delete_forum_post",
				forumId: f,
				postId: p
			}, function(json) {
				//alert( "received : " + json + " : " + f + " : " + p );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// do nothing
				} else {
				// if data has been returned
					
					// broadcast event
					// the forum data has been received
					obj.getCore().broadcast( 'onForumPostDeleted', { forumId: f, postId: p } );
					
					// post a message
					var msge = new MessageData();
					msge.setMessageType( "good" );
					msge.setMessage( "Your post has been deleted" );
					obj.getCore().broadcast( 'onMessageShow', msge );
				}
				return false;
			}, "json");*/
		},
		
		/**
		*   getAnnouncements
		*
		*   Gets all the announcements
		*/
		getAnnouncements: function()
		{
			//alert( "getting announcements" );
			
			var announcements = [];
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_general.php", {
				cmd: "get_announcements"
			}, function(json) {
				//alert( "received : " + json );
				
				for ( var i=0; i<json.contents.announcements.length; i++ )
				{
					//alert( "building data object " + json.contents.announcements[i].author_profile.profile );
					
					// populate a forum post data object
					var aData = new AnnouncementData();
						aData.setAnnouncementId( json.contents.announcements[i].announcement_id );
						aData.setAnnouncement( json.contents.announcements[i].announcement );
						aData.setAnnouncementAuthor( json.contents.announcements[i].author_id );
						aData.setAnnouncementAuthorName( json.contents.announcements[i].author_name );
						aData.setAnnouncementAuthorProfile( obj.core.getSerializedProfileData( json.contents.announcements[i].author_profile.profile ) );
						
					// add to announcements array
					announcements.push( aData );
				}
				
				var ancmtsData = new AnnouncementsData();
					ancmtsData.setAnnouncements( announcements );
				
				// broadcast event
				// the forum data has been received
				obj.getCore().broadcast( 'onAnnouncementsDataReceived', ancmtsData );
					
				return false;
			}, "json");
		}
	}
);

/**
*	NewsControl
*/
var NewsControl = AbstractControl.extend(
	{
		/**
		*   init
		*
		*   Constructs the core
		*
		*	@param	c - model instance
		*/
		init: function(c)
		{
			this._super( "NewsControl", c );
		},
		
		/**
		*   getTickerNews
		*
		*   Gets a list of ticker news stories from the database
		*/
		getTickerNews: function()
		{
			//alert( this.type + " getting ticker news" );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_news.php", {
				cmd: "get_ticker_news"
			}, function(json) {
				//alert( obj.type + " received : " + json.contents + " : " + json.contents.error );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no data returned
						// broadcast event
						// the forum data has been received
						obj.getCore().broadcast( 'onGetTickerNewsFailed', json.contents );
					} else {
					// if data has been returned
						
						// broadcast event
						// the forum data has been received
						obj.getCore().broadcast( 'onGetTickerNewsSuccess', json.contents );
					}
					return false;
				}
			}, "json");/**/
		},
		
		/**
		*   addTickerNews
		*
		*   Adds a new ticker news story to the database
		*
		*	@param	t - ticker news title
		*	@param	u - ticker news url
		*/
		addTickerNews: function(t, u)
		{
			//alert( this.type + " adding ticker news : " + t + " : " + u );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_news.php", {
				cmd: "add_ticker_news",
				title: t,
				url: u
			}, function(json) {
				//alert( obj.type + " received : " + json.contents + " : " + json.contents.error );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no data returned
						// broadcast event
						// the forum data has been received
						obj.getCore().broadcast( 'onAddTickerNewsFailed', json.contents );
					} else {
					// if data has been returned
						
						// broadcast event
						// the forum data has been received
						obj.getCore().broadcast( 'onAddTickerNewsSuccess', json.contents );
					}
					return false;
				}
			}, "json");/**/
		},
		
		/**
		*   deleteTickerNews
		*
		*   Deeletes a ticker news story from the database
		*
		*	@param	id - ticker news id
		*/
		deleteTickerNews: function(id)
		{
			//alert( this.type + " deleting ticker news : " + id );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_news.php", {
				cmd: "delete_ticker_news",
				newsId: id
			}, function(json) {
				//alert( obj.type + " received : " + json.contents + " : " + json.contents.error );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no data returned
						// broadcast event
						// the forum data has been received
						obj.getCore().broadcast( 'onDeleteTickerNewsFailed', json.contents );
					} else {
					// if data has been returned
						
						// broadcast event
						// the forum data has been received
						obj.getCore().broadcast( 'onDeleteTickerNewsSuccess', json.contents );
					}
					return false;
				}
			}, "json");/**/
		}
	}
);

/**
*	CastingsControl
*/
var CastingsControl = AbstractControl.extend(
	{
		/**
		*   init
		*
		*   Constructs the core
		*
		*	@param	c - model instance
		*/
		init: function(c)
		{
			this._super( "CastingsControl", c );
		},
		
		/**
		*   getSerializedCastingData
		*
		*   Returns the serialized casting data object from the json casting object
		*
		*	@param	d - data
		*/
		getSerializedCastingData: function(d)
		{
			//alert( "serializing : " + d );
			
			if ( !d )
				return;
			
			var castingData = new CastingData();
			
			if ( d.castingId != null ) 			castingData.setCastingId( d.castingId );
			if ( d.title != null ) 				castingData.setTitle( d.title );
			if ( d.image != null ) 				castingData.setImage( d.image );
			if ( d.imageId != null ) 			castingData.setImageId( d.imageId );
			if ( d.shortDescription != null ) 	castingData.setShortDescription( d.shortDescription );
			if ( d.longDescription != null ) 	castingData.setLongDescription( d.longDescription );
			if ( d.postcode != null ) 			castingData.setPostcode( d.postcode );
			if ( d.latitude != null ) 			castingData.setLatitude( d.latitude );
			if ( d.longitude != null ) 			castingData.setLongitude( d.longitude );
			if ( d.location != null ) 			castingData.setLocation( d.location );
			if ( d.startDate != null ) 			castingData.setStartDate( d.startDate );
			if ( d.endDate != null ) 			castingData.setEndDate( d.endDate );
			if ( d.category != null ) 			castingData.setCategories( String(d.category).split(",") );
			if ( d.author.profile != null ) 	castingData.setAuthor( this.core.getSerializedProfileData( d.author.profile ) );
			
			return castingData;
		},
		
		/**
		*   reportCasting
		*
		*   Notifys the administrator - reports author
		*
		*	@param	id - author id
		*	@param	t - title
		*	@param	d - description
		*/
		reportCasting: function(id, t, d)
		{
			//alert( "reporting author : " + id + " : " + t + " : " + d );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_castings.php", {
				cmd: "report_casting_author",
				authorId: id,
				title: t,
				description: d
			}, function(json) {
				//alert( "received : " + json.contents + " : " + id );
				
				if ( json.contents == 0 ) {
				// if there has been no xml returned
					// broadcast event
					obj.getCore().broadcast( 'onReportCastingFailed', json.contents );
				} else {
				// if data has been returned
					// broadcast event
					obj.getCore().broadcast( 'onReportCastingSuccess', json.contents );
				}
				return false;
			}, "json");
		},
		
		/**
		*   getCastings
		*
		*   Returns a list of castings from the database
		*
		*	@param	c - casting id's array
		*	@Param	m - max results
		*	@param	p - page number
		*/
		getCastings: function(c, m, p)
		{
			//alert( "getting castings : " + c.join( "," ) + " : " + m + " : " + p );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_castings.php", {
				cmd: "get_castings",
				castingIds: c.join( "," ),
				resultsMax: m,
				resultsPage: p
			}, function(json) {
				//alert( "received : " + json );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						obj.getCore().broadcast( 'onGetCastingsFailed', json.contents );
					} else {
					// if data has been returned
					
						// serialize the data
						// loop through the castings and serialize all objects accordingly
						var castings = new Array();
						for ( var i=0; i<json.contents.castings.length; i++ )
						{
							castings.push( obj.getSerializedCastingData(json.contents.castings[i].casting) );
						}
						
						var successData = new Object();
							successData.castings = castings;
							successData.totalPages = json.contents.totalPages;
					
						// broadcast event
						obj.getCore().broadcast( 'onGetCastingsSuccess', successData );
					}
					return false;
				}
			}, "json");
		},
		
		/**
		*   searchCastings
		*
		*   Searchs the castings & returns a list of from the database based upon passed search criteria
		*
		*	@param	pc - post code
		*	@param	d - distance
		*	@param	c - category
		*	@Param	m - max results
		*	@param	p - page number
		*/
		searchCastings: function(pc, d, c, m, p)
		{
			//alert( "getting castings : " + pc + " : " + d + " : " + c + " : " + m + " : " + p );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_castings.php", {
				cmd: "search_castings",
				postcode: pc,
				distance: d,
				category: c,
				searchMax: m,
				searchPage: p
			}, function(json) {
				//alert( "received : " + json + " : " + json.contents.error );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						obj.getCore().broadcast( 'onSearchCastingsFailed', json.contents );
					} else {
					// if data has been returned
					
						// serialize the data
						// loop through the castings and serialize all objects accordingly
						var castings = new Array();
						for ( var i=0; i<json.contents.castings.length; i++ )
						{
							castings.push( obj.getSerializedCastingData(json.contents.castings[i].casting) );
						}
						
						var successData = new Object();
							successData.castings = castings;
							successData.totalPages = json.contents.totalPages;
					
						// broadcast event
						obj.getCore().broadcast( 'onSearchCastingsSuccess', successData );
					}
					return false;
				}
			}, "json");
		},
		
		/**
		*   getUsersCastings
		*
		*   Returns a list of castings assosciated with a specific user from the database
		*
		*	@param	u - user id
		*	@Param	m - max results
		*	@param	p - page number
		*/
		getUsersCastings: function(u, m, p)
		{
			//alert( "getting users castings : " + u + " : " + m + " : " + p );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_castings.php", {
				cmd: "get_castings",
				userId: u,
				resultsMax: m,
				resultsPage: p
			}, function(json) {
				//alert( "received : " + json.contents.error );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						obj.getCore().broadcast( 'onGetUsersCastingsFailed', json.contents );
					} else {
					// if data has been returned
					
						// serialize the data
						// loop through the castings and serialize all objects accordingly
						var castings = new Array();
						for ( var i=0; i<json.contents.castings.length; i++ )
						{
							castings.push( obj.getSerializedCastingData(json.contents.castings[i].casting) );
						}
						
						var successData = new Object();
							successData.castings = castings;
							successData.totalPages = json.contents.totalPages;
					
						// broadcast event
						obj.getCore().broadcast( 'onGetUsersCastingsSuccess', successData );
					}
					return false;
				}
			}, "json");
		},
		
		/**
		*   addCasting
		*
		*   Adds a casting to the database
		*
		*	@param	u - user id
		*	@param	d - casting data object
		*/
		addCasting: function(u, d)
		{
			/*alert( "adding casting : "
				+ " casting id " + d.getCastingId() + "\n"
				+ " title " + d.getTitle() + "\n"
				+ " image " + d.getImageId() + "\n"
				+ " desc short " + d.getShortDescription() + "\n"
				+ " desc long " + d.getLongDescription() + "\n"
				+ " postcode " + d.getPostcode() + "\n"
				+ " start date " + d.getStartDate() + "\n"
				+ " end date " + d.getEndDate() + "\n"
				+ " category " + d.getCategories().join( "," ) + "\n"
			);*/
			
			// manage any data before sending
			// @@ categories
			var categoryStr = d.getCategories().join( "," );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_castings.php", {
				cmd: "add_casting",
				userId: u,
				image: d.getImageId(),
				title: d.getTitle(),
				descShort: d.getShortDescription(),
				descLong: d.getLongDescription(),
				postcode: d.getPostcode(),
				startDate: d.getStartDate(),
				endDate: d.getEndDate(),
				category: categoryStr
			}, function(json) {
				//alert( "received : " + json.contents.error );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						obj.getCore().broadcast( 'onAddCastingFailed', json.contents );
					} else {
					// if data has been returned
						// broadcast event
						obj.getCore().broadcast( 'onAddCastingSuccess', json.contents );
					}
					return false;
				}
			}, "json");
		},
		
		/**
		*   editCasting
		*
		*   Edits a casting record in the database
		*
		*	@param	d - casting data object
		*/
		editCasting: function(d)
		{
			/*alert( "editing casting : "
				+ " casting id " + d.getCastingId() + "\n"
				+ " title " + d.getTitle() + "\n"
				+ " image " + d.getImageId() + "\n"
				+ " desc short " + d.getShortDescription() + "\n"
				+ " desc long " + d.getLongDescription() + "\n"
				+ " postcode " + d.getPostcode() + "\n"
				+ " start date " + d.getStartDate() + "\n"
				+ " end date " + d.getEndDate() + "\n"
				+ " category " + d.getCategories().join( "," ) + "\n"
			);*/
			
			// manage any data before sending
			// @@ categories
			var categoryStr = d.getCategories().join( "," );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_castings.php", {
				cmd: "edit_casting",
				castingId: d.getCastingId(),
				image: d.getImageId(),
				title: d.getTitle(),
				descShort: d.getShortDescription(),
				descLong: d.getLongDescription(),
				postcode: d.getPostcode(),
				startDate: d.getStartDate(),
				endDate: d.getEndDate(),
				category: categoryStr
			}, function(json) {
				//alert( "received : " + json.contents.error );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						obj.getCore().broadcast( 'onEditCastingFailed', json.contents );
					} else {
					// if data has been returned
						// broadcast event
						obj.getCore().broadcast( 'onEditCastingSuccess', json.contents );
					}
					return false;
				}
			}, "json");
		},
		
		/**
		*   applyForCasting
		*
		*   Applies for a csting
		*
		*	@param	u - user id
		*	@param	c - casting id
		*	@param	s - suject
		*	@param	m - message
		*/
		applyForCasting: function(u, c, s, m)
		{
			//alert( "applying for casting : " + u + " : " + c + " : " + s + " : " + m );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_castings.php", {
				cmd: "apply_for_casting",
				userId: u,
				castingId: c,
				subject: s,
				message: m
			}, function(json) {
				//alert( "received : " + json );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						obj.getCore().broadcast( 'onApplyForCastingFailed', json.contents );
					} else {
					// if data has been returned
						// broadcast event
						obj.getCore().broadcast( 'onApplyForCastingSuccess', json.contents );
					}
					return false;
				}
			}, "json");
		}
	}
);

/**
*	ProductsControl
*/
var ProductsControl = AbstractControl.extend(
	{
		/**
		*   init
		*
		*   Constructs the core
		*
		*	@param	c - model instance
		*/
		init: function(c)
		{
			this._super( "ProductsControl", c );
			
			// define all static properties
			this.PRODUCT_TYPE_CREDIT_TOP_UP = "credit_topup";
		}
	}
);

/**
*	PurchasesControl
*/
var PurchasesControl = AbstractControl.extend(
	{
		/**
		*   init
		*
		*   Constructs the core
		*
		*	@param	c - model instance
		*/
		init: function(c)
		{
			this._super( "PurchasesControl", c );
			this.delay = null;
		},
		
		/**
		*   makePurchase
		*
		*   Attempts to make a purchase by passing the purchase data to the ajax
		*
		*	@param	u - user id
		*	@param	d - payment data object
		*/
		makePurchase: function(u, d)
		{
			/*alert( "making payment : " 
				+ " customer name " + d.getCustomerName() + "\n"
				+ " card number " + d.getCardNumber() + "\n"
				+ " card start " + d.getCardStart() + "\n"
				+ " card end " + d.getCardEnd() + "\n"
				+ " card issue " + d.getCardIssue() + "\n"
				+ " card cvv " + d.getCardCVV() + "\n"
				+ " customer address " + d.getCustomerAddress() + "\n"
				+ " customer post code " + d.getCustomerPostCode() + "\n"
				+ " customer country " + d.getCustomerCountry() + "\n"
				+ " customer IP " + d.getCustomerIP() + "\n"
				+ " customer email " + d.getCustomerEmail() + "\n"
				+ " customer tel " + d.getCustomerTel() + "\n"
				+ " trans ref " + d.getTransactionRef() + "\n"
				+ " trans desc " + d.getTransactionDescription() + "\n"
				+ " trans amt " + d.getTransactionAmount() + "\n"
				+ " trans currency " + d.getTransactionCurrency() + "\n"
				+ " trans test mode " + d.getTransactionTestMode() + "\n"
				+ " trans type " + d.getTransactionType() + "\n"
				+ " trans class " + d.getTransactionClass() + "\n"
				+ " trans origin id " + d.getTransactionOriginId() + "\n"
				+ " product data " + d.productData + "\n"
				+ " product id " + d.productData.getProductId() + "\n"
			);*/
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_purchases.php", {
				cmd: "make_purchase",
				userId: u,
				custName: d.getCustomerName(),
				cardNum: d.getCardNumber(),
				cardStart: d.getCardStart(),
				cardExpiry: d.getCardEnd(),
				cardIssue: d.getCardIssue(),
				cardCvv: d.getCardCVV(),
				custAddress: d.getCustomerAddress(),
				custPostcode: d.getCustomerPostCode(),
				custCountry: d.getCustomerCountry(),
				custIp: d.getCustomerIP(),
				custEmail: d.getCustomerEmail(),
				custTel: d.getCustomerTel(),
				tranRef: d.getTransactionRef(),
				tranDesc: d.getTransactionDescription(),
				tranAmount: d.getTransactionAmount(),
				tranCurrency: d.getTransactionCurrency(),
				tranTestmode: d.getTransactionTestMode(),
				tranType: d.getTransactionType(),
				tranClass: d.getTransactionClass(),
				tranOrigId: d.getTransactionOriginId(),
				productId: d.productData.getProductId()
			}, function(json) {
				//alert( "received : " + json.contents );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						obj.getCore().broadcast( 'onMakePurchaseFailed', d.productData );
					} else {
					// if data has been returned
						// broadcast event
						obj.getCore().broadcast( 'onMakePurchaseSuccess', d.productData );
					}
					return false;
				}
			}, "json");
		}
	}
);

/**
*	RequestsControl
*/
var RequestsControl = AbstractControl.extend(
	{
		/**
		*   init
		*
		*   Constructs the core
		*
		*	@param	c - model instance
		*/
		init: function(c)
		{
			this._super( "RequestsControl", c );
		},
		
		/**
		*   getRequest
		*
		*   Returns a request object from the database
		*
		*	@param	r - request id
		*/
		getRequest: function(r)
		{
			//alert( this.getType() + " getting request " + r );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_requests.php", {
				cmd: "get_request",
				requestId: r
			}, function(json) {
				//alert( "received : " + json.contents );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						obj.getCore().broadcast( 'onGetRequestFailed', json.contents );
					} else {
					// if data has been returned
						// serialize the request data object
						var requestData = new RequestData();
							requestData.setRequestId( json.contents.request.requestId );
							requestData.setRequestType( json.contents.request.requestType );
							requestData.setRequestAuthor( obj.core.getSerializedProfileData(json.contents.request.author.profile) );
							
						// broadcast event
						obj.getCore().broadcast( 'onGetRequestSuccess', requestData );
					}
					return false;
				}
			}, "json");
		},
		
		/**
		*   editRequestProperty
		*
		*   Edits a specified request property
		*
		*	@param	r - request id
		*	@param	p - property to edit
		*	@param	v - value to edit with
		*/
		editRequestProperty: function(r, p, v)
		{
			//alert( "editing request property : " + r + " : " + p + " : " + v );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_requests.php", {
				cmd: "edit_request_property",
				requestId: r,
				editProperty: p,
				editValue: v
			}, function(json) {
				//alert( "received : " + json );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						// unable to add the fan
						obj.getCore().broadcast( 'onEditRequestPropertyFailed', { property: p, value: v } );
					} else {
					// if data has been returned
						// broadcast event
						// added the fan
						obj.getCore().broadcast( 'onEditRequestPropertySuccess', { property: p, value: v } );
					}
					return false;
				}
			}, "json");
		},
		
		/**
		*   approveRequest
		*
		*   Approves a request
		*
		*	@param	r - request id
		*/
		approveRequest: function(r)
		{
			//alert( "approving request : " + r );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_requests.php", {
				cmd: "approve_request",
				requestId: r
			}, function(json) {
				//alert( "received : " + json );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						// unable to add the fan
						obj.getCore().broadcast( 'onApproveRequestFailed', r );
					} else {
					// if data has been returned
						// broadcast event
						// added the fan
						obj.getCore().broadcast( 'onApproveRequestSuccess', r );
					}
					return false;
				}
			}, "json");
		},
		
		/**
		*   declineRequest
		*
		*   Declines a request
		*
		*	@param	r - request id
		*/
		declineRequest: function(r)
		{
			//alert( "declining request : " + r );
			
			var obj = this;
			$.post( "" + this.getCore().getPrefix() + "ajax/ajax_requests.php", {
				cmd: "decline_request",
				requestId: r
			}, function(json) {
				//alert( "received : " + json );
				
				// check for all err types
				if ( json.contents.error != null ) {
					var err = new ErrorData();
					err.setErrorType( json.contents.error );
					obj.getCore().broadcast( 'onErrorDetected', err );
					return false;
				} else {
					if ( json.contents == 0 ) {
					// if there has been no xml returned
						// broadcast event
						// unable to add the fan
						obj.getCore().broadcast( 'onDeclineRequestFailed', r );
					} else {
					// if data has been returned
						// broadcast event
						// added the fan
						obj.getCore().broadcast( 'onDeclineRequestSuccess', r );
					}
					return false;
				}
			}, "json");
		}
	}
);