<?php
ob_start();
error_reporting(0);
session_start();
/* Check if users can register. */
$registration = get_option( 'users_can_register' );

/* If user registered, input info. */
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'adduser' ) {
    //$user_pass = wp_generate_password();
	//$user_pass  = $_POST['user_pass'];
   	//$user_pass_confirm = $_POST['user_pass_confirm'];
    $userdata = array(
        //'user_pass' => $user_pass,
        'user_login' => esc_attr( $_POST['user_name'] ),
		'civ' => esc_attr( $_POST['civ'] ),
        'first_name' => esc_attr( $_POST['first_name'] ),
        'last_name' => esc_attr( $_POST['last_name'] ),
		'url' => esc_attr( $_POST['url'] ),
		'nickname' => esc_attr( $_POST['nickname'] ),
		'rue' => esc_attr( $_POST['rue'] ),
		'ville' => esc_attr( $_POST['ville'] ),
		'code' => esc_attr( $_POST['code'] ),
		'pays' => esc_attr( $_POST['pays'] ),
		'description' => esc_attr( $_POST['description'] ),
		'fax' => esc_attr( $_POST['fax'] ),
		'phone' => esc_attr( $_POST['phone'] ),
		'gsm' => esc_attr( $_POST['gsm'] ),
		'skype' => esc_attr( $_POST['skype'] ),
		'agree' => esc_attr( $_POST['agree'] ),
        'user_email' => esc_attr( $_POST['email'] ),
		'tva' => esc_attr( $_POST['tva'] ),
		//'news_email' => esc_attr( $_POST['email_opt_in'] ),
		
    );

    

    if ( !$userdata['user_login'] )
        $error = __('Un nom d&acute;utilisateur est requis pour l&acute;inscription.', 'frontendprofile');
    elseif ( username_exists($userdata['user_login']) )
        $error = __('D&eacute;sol&eacute;, ce nom d&acute;utilisateur existe d&eacute;j&agrave; !', 'frontendprofile');
	
	 /**	
	//passwords must match
   	elseif ( ($user_pass != '' && $user_pass_confirm != '') && $user_pass != $user_pass_confirm )
      $error = __('Les mots de passe ne correspondent pas');
   //password is required
   	elseif (  isset($user_pass) && $user_pass == '' )
       $error = __('Le mot de passe est requis');
   //confirm password is required
   
   	elseif (  isset($user_pass_confirm) && $user_pass_confirm == '' )
      $error = __('Le champ mot de passe de confirmation est requis');
		**/
	elseif (!$userdata['civ'])
        $error = __('Merci d&acute;indiquer votre Civilit&eacute; !', 'frontendprofile');
		
	elseif (!$userdata['last_name'])
        $error = __('Merci d&acute;indiquer votre Nom !', 'frontendprofile');	
		
	elseif (!$userdata['first_name'])
        $error = __('Merci d&acute;indiquer votre Pr&eacute;nom !', 'frontendprofile');
		
	elseif (!$userdata['nickname'])
        $error = __('Merci d&acute;indiquer votre Pseudonyme !', 'frontendprofile');
		
	elseif (!$userdata['rue'])
        $error = __('Merci d&acute;indiquer votre rue !', 'frontendprofile');
	
	elseif (!$userdata['ville'])
        $error = __('Merci d&acute;indiquer votre ville !', 'frontendprofile');
	
	elseif (!$userdata['pays'])
        $error = __('Merci d&acute;indiquer votre pays !', 'frontendprofile');
		
	elseif (!$userdata['code'])
        $error = __('Merci d&acute;indiquer votre code postal !', 'frontendprofile');		

    elseif ( !is_email($userdata['user_email']) )
        $error = __('Vous devez entrer une adresse email valide.', 'frontendprofile');
		
    elseif ( email_exists($userdata['user_email']) )
        $error = __('D&eacute;sol&eacute;, cette adresse email est déjà utilis&eacute;e!', 'frontendprofile');
		
	elseif (!$userdata['phone'])
        $error = __('Merci d&acute;indiquer votre t&eacute;l&eacute;phone !', 'frontendprofile');
		
		
	elseif (!$userdata['agree'])
      $error = __('Vous devez accept&eacute; les conditions g&eacute;n&eacute;rales d&acute;utilisations. !', 'frontendprofile');
	
	elseif(isset($_POST['agree']) &&
   $_POST['agree'] == 'oui')
   
   {
		$error = __( "<strong>ERREUR</strong> : Merci d'entrez le code de securite correctement " );
	}

		
	// captcha
	elseif (empty($_SESSION['6_letters_code'] ) ||
	  strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
	{
	//Note: the captcha code is compared case insensitively.
	//if you want case sensitive match, update the check above to
	// strcmp()
		$error = __( "<strong>ERREUR</strong> : Merci d'entrez le code de securite correctement " );
	}
	// captcha
	
	

else{
        $new_user = wp_insert_user( $userdata );

        wp_new_user_notification($new_user, $user_pass);

    }

}
?>

  <!-- REGISTER FORM STARTS HERE -->

    <?php if ( is_user_logged_in() && !current_user_can( 'create_users' ) ) : ?>

        <p class="log-in-out alert">
        <?php printf( __('Vous &ecirc;tes connect&eacute; en tant <strong><a href="%1$s" title="%2$s">%2$s</a></strong>.  Vous n&acute;a pas besoin d&acute;un autre compte.', 'frontendprofile'), get_author_posts_url( $curauth->ID ), $user_identity ); ?> <a href="<?php echo wp_logout_url( get_permalink() ); ?>" title="<?php _e('se d&eacute;connect&eacute; de ce compte', 'frontendprofile'); ?>"><?php _e('D&eacute;connexion &raquo;', 'frontendprofile'); ?></a>
        </p><!-- .log-in-out .alert -->

    <?php elseif ( @$new_user ) : ?>
	<?php $redirect_url = get_option('url_free_code'); ?>
        <div class="alert">
        <script>location.href='<?php echo $redirect_url;?>'</script>
        </div><!-- .alert -->

    <?php else : ?>

        <?php if ( $error ) : ?>
            <p class="error">
                <?php echo $error; ?>
            </p><!-- .error -->
        <?php endif; ?>


        <?php if ( $registration || current_user_can( 'create_users' ) ) : ?>
        
        
       
        
<form method="post" id="registerform" name="registerform" action="http://<?php echo $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>">
<div id="register-contrainer">
<h2 class="title-register">Renseignements g&eacute;n&eacute;raux</h2>
            

            <p class="form-username">
                <label for="user_name"><?php _e('Identifiant (*)', 'frontendprofile'); ?></label>
                <input class="input" name="user_name" type="text" id="user_name" value="<?php if ( $error ) echo wp_specialchars( $_POST['user_name'], 1 ); ?>" />
            </p><!-- .form-username -->
            <!--
            <p>
			<label for="user_pass"><?php // _e('Password') ?></label>
			<input type="password" name="user_pass" id="user_pass" class="input" value="" tabindex="3" />
			</p>

            <p>
            <label for="user_pass_confirm"><?php // _e('Confirmer le mot de passe') ?></label>
            <input type="password" name="user_pass_confirm" id="user_pass_confirm" class="input" value="" tabindex="4" />
            </p> -->
            <!-- .password -->
            <p class="url">
                <label for="url"><?php _e('Site Web (<em>Facultatif</em>)', 'frontendprofile'); ?></label>
                <input class="input" name="url" type="text" id="url" value="<?php if ( $error ) echo wp_specialchars( $_POST['url'], 1 ); ?>" />
            </p><!-- .pseudonyme -->
          
			<p class="skype">
                <label for="skype"><?php _e('Skype (<em>Facultatif</em>)', 'frontendprofile'); ?></label>
                <input class="input" name="skype" type="text" id="skype" value="<?php if ( $error ) echo wp_specialchars( $_POST['skype'], 1 ); ?>" />
            </p><!-- .skype --><br />

<h2 class="title-register">Coordonn&eacute;es administratives</h2>           
            <p class="civ">
					<label for="civ"><?php _e('Votre Civilit&eacute;', 'frontendprofile'); ?></label>
					<?php
						
							$years= array(
										 "Mr" => 'Monsieur',
										 "Mme" => 'Madame',
										 "Melle" => 'Mademoiselle', 
										);
						
						echo '<select class="input_select" name="civ">';
							echo '<option value="">' . __("Votre Civilit&eacute;", 'frontendprofile' ) . '</option>';
							foreach($years as $year){
								if ($error && $year==$_POST['civ']) $selected = 'selected="slelected"';
								else $selected = '';
								echo '<option value="' . $year . '" ' . $selected . '>' . $year . '</option>';
							}
						echo '</select>';
					?>
                    </p>
            
            <p class="last_name">
                <label for="last_name"><?php _e('Nom (*)', 'frontendprofile'); ?></label>
                <input class="input" name="last_name" type="text" id="last_name" value="<?php if ( $error ) echo wp_specialchars( $_POST['last_name'], 1 ); ?>" />
            </p><!-- .last_name -->


            <p class="first_name">
                <label for="first_name"><?php _e('Pr&eacute;nom (*)', 'frontendprofile'); ?></label>
                <input class="input" name="first_name" type="text" id="first_name" value="<?php if ( $error ) echo wp_specialchars( $_POST['first_name'], 1 ); ?>" />
            </p><!-- .first_name -->

            <p class="nickname">
                <label for="nickname"><?php _e('Pseudonyme (<em>*</em>)', 'frontendprofile'); ?></label>
                <input class="input" name="nickname" type="text" id="nickname" value="<?php if ( $error ) echo wp_specialchars( $_POST['nickname'], 1 ); ?>" />
            </p><!-- .pseudonyme -->
            
            
            
            <p class="rue">
                <label for="rue"><?php _e('N&deg; et nom de la rue (<em>*</em>)', 'frontendprofile'); ?></label>
                <input class="input" name="rue" type="text" id="rue" value="<?php if ( $error ) echo wp_specialchars( $_POST['rue'], 1 ); ?>" />
            </p><!-- .rue -->
            
            <p class="ville">
                <label for="ville"><?php _e('Votre Ville (<em>*</em>)', 'frontendprofile'); ?></label>
                <input class="input" name="ville" type="text" id="ville" value="<?php if ( $error ) echo wp_specialchars( $_POST['ville'], 1 ); ?>" />
            </p><!-- .ville -->
            
            <p class="pays">
                <label for="pays"><?php _e('Votre Pays (<em>*</em>)', 'frontendprofile'); ?></label>
                <input class="input" name="pays" type="text" id="pays" value="<?php if ( $error ) echo wp_specialchars( $_POST['pays'], 1 ); ?>" />
            </p><!-- .pays -->
            
           <p class="code">
                <label for="code"><?php _e('Votre code postal (<em>*</em>)', 'frontendprofile'); ?></label>
                <input class="input" name="code" type="text" id="code" value="<?php if ( $error ) echo wp_specialchars( $_POST['code'], 1 ); ?>" />
            </p><!-- .codepostal -->
            
            <p class="form-email">
                <label for="email"><?php _e('E-mail (*)', 'frontendprofile'); ?></label>
                <input class="input" name="email" type="text" id="email" value="<?php if ( $error ) echo wp_specialchars( $_POST['email'], 1 ); ?>" />
            </p><!-- .form-email -->
            
           
            
            <p class="phone">
                <label for="phone"><?php _e('N&deg; de t&eacute;l&eacute;phone  (<em>*</em>)', 'frontendprofile'); ?></label>
                <input class="input" name="phone" type="text" id="phone" value="<?php if ( $error ) echo wp_specialchars( $_POST['phone'], 1 ); ?>" />
            </p><!-- .phone -->
            
            <p class="fax">
                <label for="fax"><?php _e('N&deg; de fax (<em>Facultatif</em>)', 'frontendprofile'); ?></label>
                <input class="input" name="fax" type="text" id="fax" value="<?php if ( $error ) echo wp_specialchars( $_POST['fax'], 1 ); ?>" />
            </p><!-- .fax -->
            
            <p class="gsm">
                <label for="gsm"><?php _e('N&deg; de portable (<em>Facultatif</em>)', 'frontendprofile'); ?></label>
                <input class="input" name="gsm" type="text" id="gsm" value="<?php if ( $error ) echo wp_specialchars( $_POST['gsm'], 1 ); ?>" />
            </p><!-- .fax -->
            
            <p class="tva">
                <label for="tva"><?php _e('N&deg; entreprise ou TVA Intra (<em>Facultatif</em>)', 'frontendprofile'); ?></label>
                <input class="input" name="tva" type="text" id="tva" value="<?php if ( $error ) echo wp_specialchars( $_POST['tva'], 1 ); ?>" />
            </p><!-- .fax -->
            </br>
            <p class="form-description">
					<label for="description"><?php _e('Biographie (<em>Facultatif</em>)', 'frontendprofile'); ?></label>
					<textarea class="text-input" name="description" id="description" rows="5" cols="30"><?php if ( $error ) echo wp_specialchars( $_POST['description'], 1 ); ?></textarea>
				</p><!-- .form-description -->
            
            <div id="agree" class="form-agree">
            		<?php echo get_option('desc_cg_code'); ?></br>
					<a class="various" href="<?php echo get_option('lien_id_code'); ?>" data-fancybox-type="iframe"><?php echo get_option('title_code'); ?></a>
                    <?php
						$agree = get_the_author_meta( 'agree', $current_user->id );
					?>
					<ul>
						<li><input value="oui" name="agree[]" <?php if (is_array($_POST['agree'])) { if ($error && in_array("oui",$_POST['agree'])) { ?>checked="checked"<?php } }?> type="checkbox" />J&acute;ai lu,pris connaissance et accept&eacute; les conditions <a href="<?php echo get_option('url_cgv_code'); ?>" target="_blank">g&eacute;n&eacute;rales d&acute;utilisations</a>.</li>
					
					</ul>
				</div><!-- .form-agree -->
            </div>
			<!-- captcha -->
            
            <div id="captcha">
            <p>
			<label class="label-capchat" for="6_letters_code">Entrer le code<?php _e( ' (*)'); ?>:</label>
            <img class="captcha_re" src="<?php bloginfo('template_directory'); ?>/membre/captcha/securitecode.php?rand=<?php echo rand(); ?>" id='captchaimg' >
			<input class="input_captcha_re" id="6_letters_code" name="6_letters_code" type="text" value="<?php echo $_POST['6_letters_code']; ?>"></p>
            
            <div class="captcha_rafreh"><a href='javascript: refreshCaptcha();'><strong><img src="<?php bloginfo('template_directory'); ?>/membre/captcha/images/refresh.png" /></strong></a></div>
			<script language="JavaScript">
			var frmvalidator  = new Validator("registerform");
			frmvalidator.EnableOnPageErrorDisplaySingleBox();
			frmvalidator.EnableMsgsTogether(); 
			</script>
			<script language='JavaScript' type='text/javascript'>
			function refreshCaptcha()
			{
				var img = document.images['captchaimg'];
				img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
			}
			</script>
           </div> <!-- captcha -->
           
            	</br>
            <p class="form-submit">
            <label class="label-capchat">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
                <input name="adduser" type="submit" id="wp-submit" onclick="copie()" value="<?php if ( current_user_can( 'create_users' ) ) _e('S&acute;inscrire', 'frontendprofile'); else _e('S&acute;inscrire', 'frontendprofile'); ?>" />
                <?php wp_nonce_field( 'add-user' ) ?>
                <input name="action" type="hidden" id="action" value="adduser" />

            </p><!-- .form-submit -->

        </form><!-- #adduser -->

        <?php endif; ?>

    <?php endif; ?>