} else {
        if (isset($_GET['advertisementID'])) {
            $advertisementID = $_GET['advertisementID'];
            $memberDetails = MemberController::getMemberDetails($memberID, $dbConnection);
            //If member details or advertisement ID cannot be obtained then do not continue.
            if ($memberDetails != null && $advertisementID != null) {
                //Check that the user has the priviledge to edit the advertisement.
                $advertisementDetails = AdvertisementController::getAdvertisementForEdit($advertisementID, $memberID, $dbConnection);
                //If the advertisementDetails object is null then the user does not have the permissions to edit it.
                if ($advertisementDetails != null) {
                    $advertisementGenres = AdvertisementGenreController::getAdvertisementGenres($advertisementID, $dbConnection);
                    $advertisementPositions = AdvertisementPositiionController::getAdvertisementPositions($advertisementID, $dbConnection);
                    //Encrypt values to be validated on update.
                    $advertisementKey = EncryptionUtilities::encryptString($advertisementID . $memberID);
                    $memberIDEncrypted = EncryptionUtilities::encryptString($memberID);
                    //Generate a submission key to be used upon update.
                    $submissionKey = MemberSubmissionController::generateSubmissionKey($memberID, $dbConnection);
                    $processed = true;
                }
            }
        }
    }
} catch (Exception $ex) {
    $processed = false;
}
$dbConnection = null;
if (!$processed) {
    ErrorUtilities::errorRedirect(ErrorConstants::ADVERTISEMENT_VIEW_ERROR);
} else {
    require_once $config->getIncludeURL(Config::INCLUDES_PATH, "views%edit_advertisement.php");
}
$dbConnection = null;
$memberID = null;
$processed = false;
try {
    $dbConnection = DatabaseUtilities::getDatabaseConnection();
    //Get the current user's ID and details.
    $memberID = LoginController::getLoggedInMemberID($dbConnection);
    if ($memberID != null) {
        if (isset($_POST[AdvertisementApplicationConstants::SUBMISSION_KEY_INPUT])) {
            if (MemberSubmissionController::checkSubmissionKey($memberID, $_POST[AdvertisementApplicationConstants::SUBMISSION_KEY_INPUT])) {
                $inputMemberID = $_POST[AdvertisementApplicationConstants::MEMBER_ID_INPUT];
                $encryptedID = $_POST[AdvertisementApplicationConstants::ENCRYPTED_ID_INPUT];
                if (AdvertisementApplicationController::processAdvertisementApplicationSecurityCheck($memberID, $inputMemberID, $encryptedID)) {
                    if (AdvertisementApplicationController::createAdvertismentPositionApplication($memberID, $_POST[AdvertisementApplicationConstants::ADVERTISEMENT_POSITION_ID_INPUT], $_POST[AdvertisementApplicationConstants::MESSAGE_INPUT], $dbConnection)) {
                        $processed = true;
                    }
                }
                //Clear submission key so it cannot be used again, regardless of success.
                MemberSubmissionController::deactivateSubmissionKey($memberID, $_POST[AdvertisementApplicationConstants::SUBMISSION_KEY_INPUT], $processed, $dbConnection);
            }
        }
    }
} catch (Exception $ex) {
    $processed = false;
}
$dbConnection = null;
if (!$processed) {
    echo "0";
} else {
    echo "1";
}
require_once $config->getIncludeURL(Config::INCLUDES_PATH, "objects%advertisement.php");
require_once $config->getIncludeURL(Config::INCLUDES_PATH, "objects%advertisement_position.php");
require_once $config->getIncludeURL(Config::INCLUDES_PATH, "captcha%recaptchalib.php");
$advertisementID = null;
$dbConnection = null;
$memberID = null;
$processed = false;
try {
    $dbConnection = DatabaseUtilities::getDatabaseConnection();
    //Get the current user's ID and details.
    $memberID = LoginController::getLoggedInMemberID($dbConnection);
    if ($memberID != null) {
        if (isset($_POST[AdvertisementCreateConstants::SUBMISSION_KEY_INPUT])) {
            $submissionKey = $_POST[AdvertisementCreateConstants::SUBMISSION_KEY_INPUT];
            echo "Submission Key " . $submissionKey;
            if (MemberSubmissionController::checkSubmissionKey($memberID, $submissionKey, $dbConnection)) {
                $inputMemberID = $_POST[AdvertisementCreateConstants::MEMBER_ID_INPUT];
                $encryptedID = $_POST[AdvertisementCreateConstants::ENCRYPTED_ID_INPUT];
                if (AdvertisementController::processAdvertisementCreateSecurityCheck($memberID, $inputMemberID, $encryptedID)) {
                    $advertisementID = AdvertisementController::createAdvertisement($memberID, $_POST[AdvertisementCreateConstants::FOR_INPUT_ID], $_POST[AdvertisementCreateConstants::SUMMARY_INPUT_ID], $_POST[AdvertisementCreateConstants::DESCRIPTION_INPUT_ID], $_POST[AdvertisementCreateConstants::INSPIRATIONS_INPUT_ID], $_POST[AdvertisementCreateConstants::BAND_INPUT_ID], $_POST[LocationConstants::LOCATION_COUNTRY_INPUT_ID], $_POST[LocationConstants::LOCATION_STATE_INPUT_ID], $_POST[LocationConstants::LOCATION_CITY_INPUT_ID], $_POST[LocationConstants::LOCATION_REGION_INPUT_ID], $dbConnection);
                    if ($advertisementID > 0) {
                        //If advertisement is for a position in a band.
                        if ($_POST[AdvertisementCreateConstants::FOR_INPUT_ID] == 2) {
                            $bandID = $_POST[AdvertisementCreateConstants::BAND_INPUT_ID];
                            if (BandMemberController::isBandMemberAdmin($bandID, $memberID, $dbConnection)) {
                                BandAdvertisementController::createBandAdvertisement($bandID, $advertisementID, $dbConnection);
                            }
                        }
                        if (AdvertisementGenreController::createAdvertisementGenres($advertisementID, $memberID, $_POST[AdvertisementCreateConstants::GENRE_INPUT_ID], $dbConnection)) {
                            $positionCount = $_POST[AdvertisementCreateConstants::POSITION_COUNT_ID];
                            echo "Position Count : " . $positionCount;
                $changeKey = $_POST[AdvertisementEditConstants::CHANGED_INPUT_ID];
                AdvertisementController::updateAdvertisement($memberID, $changeKey, $advertisementID, $_POST[AdvertisementEditConstants::FOR_INPUT_ID], $_POST[AdvertisementEditConstants::BAND_INPUT_ID], $_POST[AdvertisementEditConstants::SUMMARY_INPUT_ID], $_POST[AdvertisementEditConstants::INSPIRATIONS_INPUT_ID], $_POST[AdvertisementEditConstants::DESCRIPTION_INPUT_ID], $_POST[LocationConstants::LOCATION_COUNTRY_INPUT_ID], $_POST[LocationConstants::LOCATION_STATE_INPUT_ID], $_POST[LocationConstants::LOCATION_CITY_INPUT_ID], $_POST[LocationConstants::LOCATION_REGION_INPUT_ID], $dbConnection);
                BandAdvertisementController::updateBandAdvertisement($_POST[AdvertisementEditConstants::BAND_INPUT_ID], $advertisementID, $memberID, $changeKey, $_POST[AdvertisementEditConstants::FOR_INPUT_ID], $dbConnection);
                AdvertisementGenreController::updateAdvertisementGenres($advertisementID, $memberID, $changeKey, $_POST[AdvertisementEditConstants::GENRE_INPUT_ID], $dbConnection);
                //Update advertisement positions.
                if ($_POST[AdvertisementEditConstants::POSITION_COUNT_ID] > 0) {
                    $i = 1;
                    $positionCount = $_POST[AdvertisementEditConstants::POSITION_COUNT_ID];
                    for ($i = 1; $i <= $_POST[AdvertisementEditConstants::POSITION_COUNT_ID]; $i++) {
                        $advertisementPositionID = $_POST[AdvertisementPositionConstants::ADVERTISEMENT_POSITION_ID . $i];
                        if ($advertisementPositionID == null) {
                            AdvertisementPositiionController::createAdvertisementPosition($advertisementID, $memberID, $i, $_POST[AdvertisementPositionConstants::POSITION_TYPE_INPUT_ID . $i], $_POST[AdvertisementPositionConstants::POSITION_DURATION_INPUT_ID . $i], $_POST[AdvertisementPositionConstants::POSITION_OTHER_INPUT_ID . $i], $_POST[AdvertisementPositionConstants::POSITION_EXP_INPUT_ID . $i], $_POST[AdvertisementPositionConstants::POSITION_AGE_INPUT_ID . $i], $_POST[AdvertisementPositionConstants::POSITION_GENDER_INPUT_ID . $i], $dbConnection);
                        } else {
                            AdvertisementPositiionController::updateAdvertisementPosition($advertisementPositionID, $_POST[AdvertisementPositionConstants::CHANGE_KEY_INPUT . $i], $advertisementID, $memberID, $i, $_POST[AdvertisementPositionConstants::POSITION_TYPE_INPUT_ID . $i], $_POST[AdvertisementPositionConstants::POSITION_DURATION_INPUT_ID . $i], $_POST[AdvertisementPositionConstants::POSITION_OTHER_INPUT_ID . $i], $_POST[AdvertisementPositionConstants::POSITION_EXP_INPUT_ID . $i], $_POST[AdvertisementPositionConstants::POSITION_AGE_INPUT_ID . $i], $_POST[AdvertisementPositionConstants::POSITION_GENDER_INPUT_ID . $i], $dbConnection);
                        }
                    }
                }
                $processed = true;
            }
            MemberSubmissionController::deactivateSubmissionKey($memberID, $submissionKey, $processed, $dbConnection);
        }
    }
} catch (Exception $ex) {
    $processed = false;
}
$dbConnection = null;
if (!$processed) {
    ErrorUtilities::errorRedirect(ErrorConstants::GENERAL_ERROR);
} else {
    header("Location: advertisement.php?advertisementID=" . $advertisementID);
}