<?php

include 'head.php';
global $currentUser;
$cell_phone_carrier = CellPhoneCarrier::loadById($currentUser->getCellPhoneCarrierId());
?>

<div class="page-header">
	<h1>My Account</h1>
</div>

<form id="my-account-form" class="form" onsubmit="BackEnd.myAccount(); return false;" enctype="multipart/form-data">

	<div id="errors-container">
		<div class="alert alert-warning">
			<strong>Required Field Missing!</strong>
		</div>
	</div>

	<div id="success-container">
		<div class="alert alert-success">
			<strong>Success!</strong> Your profile was updated successfully!
		</div>
	</div>

	<h3>Profile Picture</h3>
	<?php 
if (!empty($currentUser->getProfilePicture())) {
    $profile_src = '/~group4/images/uploads/' . $currentUser->getProfilePicture();
} else {
    $profile_src = '/~group4/images/default.png';
    $cell = str_replace('-', '', $cell);
    //replace dashes from phone number
    $cell = str_replace('(', '', $cell);
    //replace open paren from phone number
    $cell = str_replace(')', '', $cell);
    //replace close paren from phone number
    $cell = str_replace(' ', '', $cell);
    //replace spaces from phone number
}
$cell_phone_carrier_id = $_POST['cell_carrier'];
$cell_phone_carrier = NULL;
if (empty($_POST['cell_carrier'])) {
    $response['error'][] = 'Cell Phone Carrier is required';
} else {
    $cell_phone_carrier_id = $_POST['cell_carrier'];
    $cell_phone_carrier = CellPhoneCarrier::loadById($cell_phone_carrier_id);
}
if (empty($_POST['password'])) {
    $response['error'][] = 'Password is required';
}
if (empty($_POST['password_confirm'])) {
    $response['error'][] = 'Confirm Password is required';
}
if ($_POST['password'] != $_POST['password_confirm']) {
    $response['error'][] = 'Passwords do not match';
}
if (empty($response['error'])) {
    $user = new User(array('first_name' => $_POST['first_name'], 'last_name' => $_POST['last_name'], 'email_address' => $_POST['email_address'], 'phone_number' => $cell, 'cell_phone_carrier_id' => $_POST['cell_carrier'], 'password' => $_POST['password'], 'password_again' => $_POST['password_confirm'], 'type_id' => 1));
    if ($user->add()) {
        $response['success'] = true;
        // email and text a welcome message