<?php

$user = Employer::find_by_id($user_id);
$email = $user->email_address;
$validated = $user->is_active;
$username = $user->username;
$smarty->assign('current_email', $email);
$smarty->assign('is_validated', $validated);
if (isset($_POST['bt_email'])) {
    $error = array();
    $email_address = $_POST['txt_email_address'];
    $con_email = $_POST['txt_confirm_email_address'];
    if (!check_email($email_address)) {
        $error[] = format_lang('error', 'incorrect_format_email');
    }
    $email_found = Employer::check_email($email_address);
    if ($email_found) {
        $error[] = format_lang('error', 'email_already_existed');
    }
    if (sizeof($error) == 0) {
        $user = Employer::change_email_address($username, $email_address);
        if ($user) {
            $change_key = Employer::change_key($username);
            if ($change_key) {
                $mess = "<p>To confirm your profile addition, please click the link below. Or, \r\n\t\t\t\t\t\t\t\tif the link is not clickable, copy and paste it into address bar of your \r\n\t\t\t\t\t\t\t\tweb browser, to directly access it.</p><p>&nbsp;</p>";
                $mess .= "<p>#Link#/confirm_reg/{$reg_key}/</p><p>&nbsp;</p>";
                $mess .= "<p>If you still have the final step of the registration wizard open, \r\n\t\t\t\t\tyou can input your confirmation code on that screen.</P><p>&nbsp;</p>";
                $mess .= "<p>Your confirmation code is: {$reg_key}</p><p>&nbsp;</p>";
            }
            $message = "<div class='success'>" . format_lang('success', 'update_email') . "</div>";
        } else {
/** if user already logged in take them to index page */
if ($session->get_recuriter()) {
    redirect_to(BASE_URL . "employer/account/");
    die;
}
if (isset($_POST['bt_username']) || isset($_POST['bt_new_password'])) {
    /*** Email validation */
    $email = $_POST['txt_email'];
    if (empty($email)) {
        $error[] = format_lang('error', 'email');
    }
    if (!empty($email)) {
        if (!check_email($email)) {
            $error[] = format_lang('error', 'incorrect_format_email');
        } else {
            $email_found = Employer::check_email($email);
            if (!$email_found) {
                //$error[] = "Email address does not existed";
            }
        }
    }
    if (ENABLE_SPAM_FD && ENABLE_SPAM_FD == 'Y') {
        if (strtolower($_POST['spam_code']) != strtolower($_SESSION['spam_code']) || (!isset($_SESSION['spam_code']) || $_SESSION['spam_code'] == NULL)) {
            $error[] = format_lang('error', 'spam_wrong_word');
            //$errors[] = "The security code you entered does not match the image. Please try again.";
        }
    }
    if (sizeof($error) == 0) {
        /* get my username**/
        $employer = new Employer();
        $employer = Employer::find_by_email($email);