<?php
 /* @var $this UserController */
 /* @var $model User */
 /* @var $form CActiveForm */
?>

<div id="loading-div" >&nbsp;
    <?php
     $form = $this->beginWidget('bootstrap.widgets.FTbActiveForm', array(
                 'id' => 'user-form',
                 'enableAjaxValidation' => false,
             ));

     $this->widget('bootstrap.widgets.TbAlert');

     echo $form->radioButtonListInlineRow($model, 'is_LDAP', array('1' => Yii::t('default', 'Yes'), '0' => Yii::t('default', 'No')), array(
         'onchange' => 'manage_notify_username_visibility()',
     ));

     // the following expression will check the initial display of the username field and when user is ldap the user can insert username but when he is not LDAP cannot have username
     $username_display_type = $model->is_LDAP == "yes" ? "none" : "block";
    ?> 
     <div id="username_div" style="display:<?php echo $username_display_type; ?>">
        <?php
         echo $form->textFieldRow($model, 'username', array(
             'ajax' => array(
                 'type' => 'POST',
                 'data' => 'js:{username:$(this).val(),ajax:true}',
                 'url' => CController::createUrl('user/create'),
                 'beforeSend' => 'js:function(){
                                           $("#loading-div").addClass("loading");
                           }',
                 'success' => 'js:function(data) {
                $("#loading-div").removeClass("loading");

                //Check the returned Data if Contain Span Tag (the username not found in LDAP System)
                if(data.indexOf("</span>") != -1)
                {
                   //Add the inline error
                   //Add Class "error" to username label (red color)
                   $("label[for=User_username]").addClass("error");

                   //Add Class "error" to username textbox (red color)
                   $("#User_username").addClass("error");

                   //Remove any tag span have class "validateLDAP" wich contain message error before add new one
                   $(".validateLDAP").remove();

                   //Add new span (message error )
                   $("#User_username").after(data);
                   
                   //Reset the email textbox value
                   $("#User_email").val("");
                   
                }else
                {
                    //Set the email textbox value
                    $("#User_email").val(data);

                    // remove the inline error
                    //Remove the error class from the label
                    $("label[for=User_username]").removeClass("error");
                    
                    //Remove the error class from the text field
                    $("#User_username").removeClass("error");
                    
                    //Remove the error message below the text field
                    $("#User_username").next(".error").html("");
                }
            }',
             )
                 )
         );
        ?>
     </div>
    <?php
         echo $form->textFieldRow($model, 'email', array('size' => 60, 'maxlength' => 255));

         echo $form->passwordFieldRow($model, 'password');
    ?>
         &nbsp;<span id="passstrength"></span><?php
         echo $form->hiddenField($model, 'password_strength');
         echo $form->passwordFieldRow($model, 'password_repeat');

         echo $form->dropDownListRow($model, 'location_id', CHtml::listData(Location::getAll("Active"), 'id', 'code'), array('empty' => Yii::t('default', 'Select Location')));

         if ($model->isNewRecord)
         {
         }
         else
         {
             //Get Assigned Role and set it to model as default value
             $model->role = $model->getAssignedRolesNames($model->id);
         }

         echo $form->dropDownListRow($model, 'role', User::getUserRoleOptions(), array('empty' => Yii::t('default', 'Select Role')));

         if ($model->isNewRecord)
         {
             $model->status = 'Active';
         }

         echo $form->radioButtonListInlineRow($model, 'status', array('Active' => Yii::t('default', 'Active'), 'Inactive' => Yii::t('default', 'Inactive')));
    ?>

         <div class="actions">
        <?php echo CHtml::submitButton(Yii::t('default', 'Save'), array('class' => 'btn primary')); ?>
         &ensp;
        <?php echo CHtml::link(Yii::t('default', 'Cancel'), array('index'), array('class' => 'btn primary')); ?>
     </div>
</div> <!--loading-->

<?php $this->endWidget(); ?>


         <script language="javascript">
             $('#User_password').keyup(function(e) {
                 var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
                 var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
                 var enoughRegex = new RegExp("(?=.{6,}).*", "g");
                 strength = '';
                 if (false == enoughRegex.test($(this).val())) {
                     $('#passstrength').html('<u><span style="color:red"><?php echo Yii::t('default', 'Password Too Short'); ?></span>');
                     strength = 'too short';
                 } else if (strongRegex.test($(this).val())) {
                     $('#passstrength').html('<u><span style="color:blue"><?php echo Yii::t('default', 'Password Strong'); ?></span>');
                     strength = 'strong';
                 } else if (mediumRegex.test($(this).val())) {
                     $('#passstrength').html('<u><span style="color:green"><?php echo Yii::t('default', 'Password Good'); ?></span>');
                     strength = 'good';
                 } else {
                     $('#passstrength').html('<u><span style="color:orange"><?php echo Yii::t('default', 'Password Weak'); ?></span>');
            strength = 'weak';
        }

        $("#User_password_strength").val(strength);
        return true;
    });
</script>
<script language="javascript" type="text/javascript">
    // this function will toggle the username input field when set radio button to yes ldap or not 
    function manage_notify_username_visibility(){
        if($("#User_is_LDAP_1").attr('checked')){
            $('#username_div').toggle().val('');
            $('#User_username').val('');
        }
        else{
            $('#username_div').toggle();
        }
    }
</script>
