<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
  require_once "/homepages/19/d122429285/htdocs/agna/NewSite2010/cmsAdmin/lib/viewer_functions.php"; 
  
  list($accountsRecords, $accountsMetaData) = getRecords(array(
    'tableName'   => 'accounts',
    'where'       => whereRecordNumberInUrl(1),
    'limit'       => '1',
  ));
  $accountsRecord = @$accountsRecords[0]; // get first record

  // show error message if no matching record is found
  if (!$accountsRecord) {
    header("HTTP/1.0 404 Not Found");
    print "Record not found!";
    exit;
  }

  ?>
<?php if (!$CURRENT_USER) { websiteLogin_redirectToLogin(); } ?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">

<head>
  <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
  <meta name="description" content="AGNA" />
  <meta name="keywords" content="Alberta,Gerontological,Nurses,Association,Calgary" />
  <meta name="author" content="AGNA" >
  <link rel="stylesheet" type="text/css" media="screen,projection" href="./css/style_screen.css" />
  <link rel="stylesheet" type="text/css" media="print" href="./css/style_print.css" />
  
  <title>Alberta Gerontological Nurses Association - Edit Profile</title>
  <style type="text/css">
<!--
.style3 {font-size: 12px}
-->
  </style>
  
</head>

<body>
  <div class="page-container-1">

  	<!-- HEADER -->
		
    <!-- Navigation Level 1 -->
    
    <!-- Sitename -->
		<!-- Site slogan -->
        <!-- Header banner -->
<div><img class="img-header" src="./img/header.jpg" alt=""/></div>	
 <?php include("include_top_left_menus.php"); ?>   
    				
						
  	<!-- CONTENT -->
		<div class="content1">

  	  <?php
  // prepopulate form with current user values
   foreach ($CURRENT_USER as $name => $value) {
    if (array_key_exists($name, $_REQUEST)) { continue; }
    $_REQUEST[$name] = $value;
  }

  // process form
  if (@$_REQUEST['save']) {

    // error checking
    $errorsAndAlerts = "";
    if (!@$_REQUEST['first_name'])              { $errorsAndAlerts .= "You must enter your First name!<br/>\n"; }
	if (!@$_REQUEST['last_name'])               { $errorsAndAlerts .= "You must enter your Last name!<br/>\n"; }
	if (!@$_REQUEST['address_line_1'])          { $errorsAndAlerts .= "You must enter the First Line of your Address!<br/>\n"; }
	if (!@$_REQUEST['city'])                    { $errorsAndAlerts .= "You must enter your City!<br/>\n"; }
	if (!@$_REQUEST['postal_code'])             { $errorsAndAlerts .= "You must enter your Postal/ZIP Code!<br/>\n"; }
	if (!@$_REQUEST['nursing_licensure'])       { $errorsAndAlerts .= "You must enter your Nursing Licensure!<br/>\n"; }
    if (!@$_REQUEST['chapter'])                 { $errorsAndAlerts .= "You must choose your Chapter!<br/>\n"; }
	if (!@$_REQUEST['email'])                   { $errorsAndAlerts .= "You must enter your Email!<br/>\n"; }
	
    else if(!isValidEmail(@$_REQUEST['email'])) { $errorsAndAlerts .= "Please enter a valid email (example: user@example.com)<br/>\n"; }
    if (!@$_REQUEST['username'])                { $errorsAndAlerts .= "You must choose a username!<br/>\n"; }

    // new password checking
    if (@$_REQUEST['oldPassword'] || $_REQUEST['newPassword1'] || $_REQUEST['newPassword2']) {
      if (!@$_REQUEST['oldPassword'])                                 { $errorsAndAlerts .= "Please enter a value for: Current Password<br/>\n"; }
      elseif (@$_REQUEST['oldPassword'] != $CURRENT_USER['password']) { $errorsAndAlerts .= "Current password isn't correct!<br/>\n"; }
      elseif (!@$_REQUEST['newPassword1'])                            { $errorsAndAlerts .= "Please enter a value for: New Password<br/>\n"; }
      elseif (!@$_REQUEST['newPassword2'])                            { $errorsAndAlerts .= "Please enter a value for: Confirm New Password<br/>\n"; }
      elseif ($_REQUEST['newPassword1'] != $_REQUEST['newPassword2']) { $errorsAndAlerts .= "New passwords don't match!<br/>\n"; }
    }

    // check for duplicate usernames and emails
    if (@$_REQUEST['username'] != $CURRENT_USER['username']) {
      $count = mysql_select_count_from('accounts', "`username` = '".mysql_escape(@$_REQUEST['username'])."'");
      if ($count > 0 && @$_REQUEST['username']) { $errorsAndAlerts .= "That username is already in use, please choose another!<br/>\n"; }
    }
    if (@$_REQUEST['email'] != $CURRENT_USER['email']) {
      $count = mysql_select_count_from('accounts', "'".mysql_escape($_REQUEST['email'])."' IN (email, username)");
      if ($count > 0) { $errorsAndAlerts .= "That email is already in use, please choose another!<br/>\n"; }
    }

    // update user
    if (!$errorsAndAlerts) {
      mysqlStrictMode(false);   // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)
      if (@$_REQUEST['newPassword2']) { $CURRENT_USER['password'] = $_REQUEST['newPassword2']; } // update password
      $query = "UPDATE `{$TABLE_PREFIX}accounts` SET
                      first_name       = '".mysql_escape( $_REQUEST['first_name'] )."',
                      last_name        = '".mysql_escape( $_REQUEST['last_name'] )."',
					  phone_w          = '".mysql_escape( $_REQUEST['phone_w'] )."',
					  phone_h          = '".mysql_escape( $_REQUEST['phone_h'] )."',
					  address_line_1   = '".mysql_escape( $_REQUEST['address_line_1'] )."',
					  address_line_2   = '".mysql_escape( $_REQUEST['address_line_2'] )."',
					  address_line_3   = '".mysql_escape( $_REQUEST['address_line_3'] )."',
					  city             = '".mysql_escape( $_REQUEST['city'] )."',
					  postal_code      = '".mysql_escape( $_REQUEST['postal_code'] )."',
					  province_state   = '".mysql_escape( $_REQUEST['province_state'] )."',
					  employer_organization    = '".mysql_escape( $_REQUEST['employer_organization'] )."',
					  nursing_licensure    = '".mysql_escape( $_REQUEST['nursing_licensure'] )."',
					  chapter          = '".mysql_escape( $_REQUEST['chapter'] )."',
					  email            = '".mysql_escape( $_REQUEST['email'] )."',
                      username         = '".mysql_escape( $_REQUEST['username'] )."',
                      password         = '".mysql_escape( $CURRENT_USER['password'] )."',

                      updatedByUserNum = '".mysql_escape( $CURRENT_USER['num'] )."',
                      updatedDate      = NOW()
                 WHERE num = '".mysql_escape( $CURRENT_USER['num'] )."'";
      mysql_query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
      $userNum = mysql_insert_id();

      // on success
      unset($_REQUEST['oldPassword'], $_REQUEST['newPassword1'], $_REQUEST['newPassword2']); // clear password fields
      $errorsAndAlerts = "Thanks, we've updated your profile!";
    }
  }

  // delete account
  if (@$_POST['deleteAccount']) {
    if ($CURRENT_USER['isAdmin']) { die("Error: Deleting admin accounts is not permitted!"); }

    // delete uploads
    $GLOBALS['tableName'] = 'accounts';
    eraseRecordsUploads( $CURRENT_USER['num'] );

    // delete account
    $query = mysql_escapef("DELETE FROM `{$TABLE_PREFIX}accounts` WHERE num = ?", $CURRENT_USER['num']);
    mysql_query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysql_error()) . "\n");

    // redirect to login
    websiteLogin_redirectToLogin();
  }


?>


  <h1>Edit Profile</h1>
  <p><strong>Note:</strong> Items marked with an asterisk (*) are required.</p>
      <!-- EDIT PROFILE FORM -->
      <?php if (@$errorsAndAlerts): ?>
    
  <div style="color: #C00; font-weight: bold; font-size: 14px; font-family: arial;"><br/>
      <?php echo $errorsAndAlerts; ?><br/><br/>
    </div>
  <?php endif ?>

  <form method="post" action="?">
  <input type="hidden" name="save" value="1" />

  <table border="0" cellspacing="0" cellpadding="2">
   <tr>
    <td class="txt-black"><span class="style10 style3">First Name*</span></td>
    <td><input type="text" name="first_name" value="<?php echo htmlspecialchars(@$_REQUEST['first_name']); ?>" size="30" /></td>
   </tr>
   <tr>
    <td class="txt-black"><span class="style10 style3">Last Name*</span></td>
    <td><input type="text" name="last_name" value="<?php echo htmlspecialchars(@$_REQUEST['last_name']); ?>" size="30" /></td>
   </tr>
   <tr>
    <td class="txt-black"><span class="style10 style3">Phone (H)</span></td>
    <td><input type="text" name="phone_h" value="<?php echo htmlspecialchars(@$_REQUEST['phone_h']); ?>" size="20" /></td>
   </tr>
   <tr>
    <td class="txt-black"><span class="style10 style3">Phone (W)</span></td>
    <td><input type="text" name="phone_w" value="<?php echo htmlspecialchars(@$_REQUEST['phone_w']); ?>" size="20" /></td>
   </tr>
   <tr>
    <td class="txt-black"><span class="style10 style3">Address line 1*</span></td>
    <td><input type="text" name="address_line_1" value="<?php echo htmlspecialchars(@$_REQUEST['address_line_1']); ?>" size="40" /></td>
   </tr>
   <tr>
    <td class="txt-black"><span class="style10 style3">Address line 2</span></td>
    <td><input type="text" name="address_line_2" value="<?php echo htmlspecialchars(@$_REQUEST['address_line_2']); ?>" size="40" /></td>
   </tr>
   <tr>
    <td class="txt-black"><span class="style10 style3">Address line 3</span></td>
    <td><input type="text" name="address_line_3" value="<?php echo htmlspecialchars(@$_REQUEST['address_line_3']); ?>" size="40" /></td>
   </tr>
   <tr>
    <td class="txt-black"><span class="style10 style3">City*</span></td>
    <td><input type="text" name="city" value="<?php echo htmlspecialchars(@$_REQUEST['city']); ?>" size="30" /></td>
   </tr>
   <tr>
    <td class="txt-black"><span class="style10 style3">Postal/ZIP code*</span></td>
    <td><input type="text" name="postal_code" value="<?php echo htmlspecialchars(@$_REQUEST['postal_code']); ?>" size="15" /></td>
   </tr>
   <tr>
    <td class="txt-black"><span class="style10 style3">Province/State*</span></td>
    <td><input type="text" name="province_state" value="<?php echo htmlspecialchars(@$_REQUEST['province_state']); ?>" size="20" /></td>
   </tr>
   <tr>
    <td class="txt-black"><span class="style10 style3">Employer or Organization</span></td>
    <td><input type="text" name="employer_organization" value="<?php echo htmlspecialchars(@$_REQUEST['employer_organization']); ?>" size="40" /></td>
   </tr>
   <tr>
   <td valign="top" class="txt-black"><span class="style10 style3">Nursing Licensure*</span></td>
   <td>
     <?php $fieldname = 'nursing_licensure'; ?>
     <?php $idCounter = 0; ?>
     <?php foreach (getListOptions('accounts', $fieldname) as $value => $label): ?>
       <?php $id = "$fieldname." . ++$idCounter; ?>
       <input type="radio" name="<?php echo $fieldname ?>" id="<?php echo $id ?>"
             value="<?php echo htmlspecialchars($value) ?>" <?php checkedIf(@$_REQUEST[$fieldname], $value) ?> />
       <label for="<?php echo $id ?>"><?php echo htmlspecialchars($value) ?></label><br/>

     <?php endforeach ?>
   </td>
  </tr>
 <tr>
   <td valign="top" class="txt-black"><span class="style10 style3">Membership Type</span></td>
   <td>
<p><strong>Changing your Membership type</strong></p>
<p>To change your Membership type, please contact us at <a href="Mailto:membership@agna.ca">membership@agna.ca</a></p>     
   </td>
  </tr>   
   <tr>
   <td valign="top" class="txt-black"><span class="style10 style3">Chapter*</span></td>
   <td>
     <?php $fieldname = 'chapter'; ?>
     <?php $idCounter = 0; ?>
     <?php foreach (getListOptions('accounts', $fieldname) as $value => $label): ?>
       <?php $id = "$fieldname." . ++$idCounter; ?>
       <input type="radio" name="<?php echo $fieldname ?>" id="<?php echo $id ?>"
             value="<?php echo htmlspecialchars($value) ?>" <?php checkedIf(@$_REQUEST[$fieldname], $value) ?> />
       <label for="<?php echo $id ?>"><?php echo htmlspecialchars($value) ?></label><br/>

     <?php endforeach ?>
   </td>
  </tr>
   <tr>
    <td class="txt-black"><span class="style10 style3">Email*</span></td>
    <td><input type="text" name="email" value="<?php echo htmlspecialchars(@$_REQUEST['email']); ?>" size="30" /></td>
   </tr>
   <tr>
    <td class="txt-black"><span class="style10 style3">Username*</span></td>
    <td><input type="text" name="username" value="<?php echo htmlspecialchars(@$_REQUEST['username']); ?>" size="30" /></td>
   </tr>

   <tr>
     <td colspan="2" class="txt-black"><span class="style10 style3">To change your password, enter your existing password, then the new password twice below: </span></td>
   </tr>

   <tr>
    <td class="txt-black"><span class="style10 style3">Current Password</span></td>
    <td><input type="password" name="oldPassword" value="<?php echo htmlspecialchars(@$_REQUEST['oldPassword']); ?>" size="40" /></td>
   </tr>
   <tr>
    <td class="txt-black"><span class="style10 style3">New Password</span></td>
    <td><input type="password" name="newPassword1" value="<?php echo htmlspecialchars(@$_REQUEST['newPassword1']); ?>" size="40" /></td>
   </tr>
   <tr>
    <td class="txt-black"><span class="style10 style3">New Password (again)</span></td>
    <td><input type="password" name="newPassword2" value="<?php echo htmlspecialchars(@$_REQUEST['newPassword2']); ?>" size="40" /></td>
   </tr>

   <tr>
    <td colspan="2" align="center" class="txt-black">
      <span class="style10 style3"><br/>
      <input class="button" type="submit" name="submit" value="Update profile &gt;&gt;" />    
      </span></td>
   </tr>
  </table>

  </form><br/>
  
<!-- /EDIT PROFILE FORM -->

</div>  
			
<?php include("include_sidebar_footerProfile.php"); ?>
  </div>
</body>
</html>