<?
if(!defined('IN_BANDTOPIA')) {
	exit ;
}

class AccountTypeObj {

	


	/*************************************************************/
	/* List Users
	 /*************************************************************/
	public function AccountTypeList() {
		//////////////////////////////////////////////////////////
		//  Todo:
		// 		Add dynamic sorting
		//		Add dynamic filtering
		//////////////////////////////////////////////////////////
		$query = <<<EOD
			SELECT
				*
			FROM
				AccountTypes
			ORDER BY AccountTypeName
EOD;
		$result = mysql_query($query) or die(mysql_error() . "<br>$query");
		while($row = mysql_fetch_assoc($result)) {
			$tempObj = new AccountTypeObj;
			$tempObj -> AccountTypeId = $row['AccountTypeId'];
			$tempObj -> AccountTypeName = $row['AccountTypeName'];

			$accountTypeArray[] = $tempObj;
		}
		return $accountTypeArray;
	}

	/*************************************************************/
	/* Get Account
	 /*************************************************************/
	public function GetAccount($AccountId) {
		$query = <<<EOD
			SELECT
				a.*
			FROM
				Accounts a
				INNER JOIN AccountTypes at
					ON at.AccountTypeId = a.AccountTypeId
				INNER JOIN Statuses s
					ON s.StatusId = a.StatusId
			WHERE
				a.AccountId = {$AccountId}
EOD;
		$result = mysql_query($query) or die(mysql_error() . "<br>$query");
		$row = mysql_fetch_assoc($result);
		$this -> AccountId = $row['AccountId'];
		$this -> AccountName = $row['AccountName'];
		
	}

	/*************************************************************/
	/* Verify Password
	 /*************************************************************/
	public function VerifyPassword($userPass) {
		$crypt = new cryptRoutineObj();
		$query = <<<EOD
			SELECT
				Password
			FROM 
				Users 
			WHERE
				UserName = '{$this->UserName}'
EOD;
		$result = mysql_query($query) or die(mysql_error() . "<br>$query");
		$rows = mysql_fetch_assoc($result);
		if(mysql_num_rows($result) > 0 && $userPass == $crypt->DecryptPass($rows['Password'])) {
			return true;
		} else {
			return false;
		}
	}

	/*************************************************************/
	/* Update User
	 /*************************************************************/
	public function updateUser() {
		if($this -> int_UserId != '') {
			$query = <<<EOD
				UPDATE
					users
				SET
					UserName = '{$this->int_UserName}',
					UserFullName = '{$this->int_UserFullName}',
					user_type_id = {$this->int_user_type_id},
					user_info_id = {$this->int_user_info_id}
				WHERE
					UserId = {$this->int_UserId}
EOD;
			$result = mysql_query($query);
			$userMsg = "User information has successfully updated.";
		} else {
			$crypt = new cryptRoutineObj();
			$password = $crypt->EncryptPass($this->int_Password);
			$query = <<<EOD
				INSERT INTO
					Users
					(UserName, 
					Password,
					UserActive,
					FirstName,
					LastName,
					MI,
					UserTypeId)
				VALUES
					('{$this->int_UserName}',
					'$password',
					1,
					'{$this->int_FirstName}',
					'{$this->int_LastName}',
					'',
					{$this->int_UserTypeId})
EOD;
			$result = mysql_query($query);
			$this -> int_UserId = mysql_insert_id();
			$userMsg = "User has been added successfully.";
		}
		if(mysql_error() != '') {
			$userMsg = "An error has occurred while attempting your request.  Please try again.  If you continue to experience problems, please contact the site administrator<br>";
			$userMsg .= mysql_error() . "<br>$query";
		}
		return $userMsg;
	}

	/*************************************************************/
	/* Change Password
	 /*************************************************************/
	public function updatePass() {
		$query = <<<EOD
			UPDATE
				users
			SET
				Password = '{$this->int_Password}'
			WHERE
				UserId = {$this->int_UserId}
EOD;
		$userMsg = "Password has successfully updated.";
		$result = mysql_query($query);
		if(mysql_error() != '') {
			$userMsg = "An error has occurred while attempting your request.  Please try again.  If you continue to experience problems, please contact the site administrator<br>";
			$userMsg .= mysql_error() . "<br>$query";
		}
		return $userMsg;
	}

	/*************************************************************/
	/* Delete(Deactivate) User
	 /*************************************************************/
	public function delUser() {
		$query = <<<EOD
			UPDATE
				users
			SET
				user_active = 0
			WHERE
				UserId = {$this->int_UserId}
EOD;
		$userMsg = "User has successfully been removed.  If this was done in error, please contact your website administrator.";
		$result = mysql_query($query);
		if(mysql_error() != '') {
			$userMsg = "An error has occurred while attempting your request.  Please try again.  If you continue to experience problems, please contact the site administrator<br>";
			$userMsg .= mysql_error() . "<br>$query";
		}
		return $userMsg;
	}
}
?>