<?php
global $current_user,$wpdb;
if($_POST)
{
	$user_id = $current_user->data->ID;
	$user_email = $_POST['user_email'];
	$userName = $_POST['user_fname'];
	$pwd = $_POST['pwd'];
	$cpwd = $_POST['cpwd'];
	if($user_email)
	{
		$check_users=$wpdb->get_var("select ID from $wpdb->users where user_email like \"$user_email\" where ID!=\"$user_id\"");
		if($check_users)
		{
			wp_redirect(get_option( 'siteurl' ).'/?ptype=profile&emsg=wemail');exit;	
		}
		
	}else
	{
			wp_redirect(get_option( 'siteurl' ).'/?ptype=profile&emsg=empty_email');exit;
	}
	if($pwd!=$cpwd)
	{
		wp_redirect(get_option( 'siteurl' ).'/?ptype=profile&emsg=pw_nomatch');exit;
	}
	if($userName)
	{
		if($pwd)
		{
			$pwd = md5($pwd);
			$subsql = " , user_pass=\"$pwd\"";	
		}
		$user_nicename = get_user_nice_name($userName); //generate nice name
		$updateUsersql = "update $wpdb->users set user_email=\"$user_email\", user_nicename=\"$user_nicename\", display_name=\"$userName\" $subsql  where ID=\"$user_id\"";
		$wpdb->query($updateUsersql);
		wp_redirect(get_option( 'siteurl' ).'/?ptype=profile&msg=success');exit;
	}
}
?>
<?php get_header(); ?>

<div id="wrapper" class="clearfix">
     <div id="content" class="clearfix" >
    	
        <?php if ( get_option( 'ptthemes_breadcrumbs' )) {  ?>
        <div class="breadcrumb clearfix">
            <div class="breadcrumb_in"><?php yoast_breadcrumb('',' &raquo; '.EDIT_PROFILE_TITLE);  ?></div>
        </div>
    <?php } ?>
      <h1><?php echo EDIT_PROFILE_TITLE; ?> </h1>   
       
        
        		 
<div id="sign_up">


<?php
if ( $_REQUEST['msg']=='success')
{
	echo "<p class=\"message\"> ".EDIT_PROFILE_SUCCESS_MSG." </p>";
}
if ( $_REQUEST['emsg']=='empty_email')
{
	echo "<p class=\"error_msg_fix\"> ".EMPTY_EMAIL_MSG." </p>";
}elseif ( $_REQUEST['emsg']=='wemail')
{
	echo "<p class=\"error_msg_fix\"> ".ALREADY_EXIST_MSG." </p>";
}elseif ( $_REQUEST['emsg']=='pw_nomatch')
{
	echo "<p class=\"error_msg_fix\"> ".PW_NO_MATCH_MSG." </p>";
}
?> 
<div class="registration_form_box">
<form name="profileform" id="profileform" action="<?php echo get_option( 'siteurl' ).'/?ptype=profile'; ?>" method="post">
<?php
if($_POST)
{
	$user_email = $_POST['user_email'];	
	$user_fname = $_POST['user_fname'];	
}else
{
	$user_email = $current_user->data->user_email;	
	$user_fname = $current_user->data->display_name;
}
?>
<div class="form_row clearfix">
  <label><?php echo EMAIL_TEXT; ?><span class="indicates">*</span></label>
  <input type="text" name="user_email" id="user_email" class="textfield" value="<?php echo esc_attr(stripslashes($user_email)); ?>" size="25" />
    <div id="reg_passmail">
	  <?php echo REGISTRATION_MESSAGE; ?>
	</div>
    <span id="user_emailInfo"></span>
</div>
<div class="row_spacer_registration clearfix" >
<div class="form_row clearfix">
  <label>
  <?php echo FIRST_NAME_TEXT; ?>
  <span class="indicates">*</span></label>
  <input type="text" name="user_fname" id="user_fname" class="textfield" value="<?php echo esc_attr(stripslashes($user_fname)); ?>" size="25"  />
   <span id="user_fnameInfo"></span>
</div>
<div class="form_row clearfix">
  <label>
  <?php echo PASSWORD_TEXT; ?>
  </label>
  <input type="password" name="pwd" id="pwd" class="textfield" value="<?php echo esc_attr(stripslashes($pwd)); ?>" size="25"  />
  <span id="pwdInfo"></span>
</div>
<div class="form_row clearfix">
  <label>
  <?php echo CONFIRM_PASSWORD_TEXT ?>
  </label>
  <input type="password" name="cpwd" id="cpwd" class="textfield" value="<?php echo esc_attr(stripslashes($cpwd)); ?>" size="25"  />
  <span id="cpwdInfo"></span>
</div>
</div> 
<input type="submit" name="registernow" value="<?php echo EDIT_BUTTON;?>" class="b_registernow" />
</form>
</div>
</div>
</div> <!-- content #end -->

    <?php get_sidebar(); ?>

</div> <!-- wrapper #end -->
<div id="bottom"></div>  
<?php get_footer(); ?>