<?php
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'country-form',
        'enableAjaxValidation' => false,
        'enableClientValidation' => false,
    ));
?>
    <div class="tab-content">
        <div class="tab-pane active">
            <ul class="clearfix form-ul material">
                
                 <li class="col-sm-6">
                    <?php echo $form->textField($model, 'first_name', array('data-label' => 'First Name','autocomplete'=>'off','onpaste'=>"return false",'onkeypress'=>'checkCharacters(this.id,"fname");')); ?>
                    <?php echo $form->hiddenField($model, 'id'); ?>
                </li>
                <li class="col-sm-6">
                    <?php echo $form->textField($model, 'last_name', array('data-label' => 'Last Name','autocomplete'=>'off','onkeypress'=>'checkCharacters(this.id,"lname");')); ?>
                </li>
                <!-- <li class="col-sm-6">
                    <?php //echo $form->dropdownlist($model, 'level', array(1 => 'Restrict Access', 0 => 'Access for all modules'), array('data-label' => 'Access type', 'class' => 'tooltipster')); ?>
                </li> -->
                <li class="col-sm-6">
                    <?php echo $form->textField($model, 'username', array('data-label' => 'Username','autocomplete'=>'off','onkeypress'=>'checkCharacters(this.id,"uname");')); ?>
                </li>
                <li class="col-sm-6">
                    <?php $model->password = '' ;?> 
                    <?php echo $form->passwordField($model, 'password', array('data-label' => 'Password','autocomplete'=>'off','onkeyup'=>'CheckPasswordStrength(this.value)')); ?>
                    <span id="password_strength"></span>
                </li>
                <li class="col-sm-6">
                    <?php echo $form->textField($model, 'email', array('data-label' => 'Email','autocomplete'=>'off')); ?>
                </li>
                <li class="col-sm-3">
                    <?php echo $form->dropdownlist($model, 'user_group_id', MasUserGroup::getUserGroup(), array('data-label' => 'User Group', 'class' => 'tooltipster')); ?>
                </li>
                <li class="col-sm-3">
                    <?php echo $form->dropdownlist($model, 'isActive', array('Y' => 'Active', 'N' => 'Block'), array('data-label' => 'Status', 'class' => 'tooltipster')); ?>
                </li>
               
            </ul>
        </div>
     </div>

<div class="modal-footer">
       <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <?php
            $url = Yii::app()->createUrl('users/SaveUsers');
            echo CHtml::ajaxSubmitButton('Save', $url, array(
                'dataType' => 'json',
                'type' => 'post',
                'success' => 'function(data) {
                        $("#loader").hide();
                        if(data.status=="success"){
                            t8.success("User added!");
                            $(".x.close").click();
                            reloadTable("#tbl_user_details");
                        } else {
                            $.each(data, function(key, val) {
                                t8.error(""+val+"");
                        });
                    }
                }',

                'beforeSend' => 'function(){
                    var pwd =$("#User_password").val();
                    
                    if(pwd!="" && (pwd.length < 8 || pwd.length > 16))
                    {
                        t8.error("Password should be between 8 and 16 characters");
                         return false;
                    }
                    if( $("#User_id").val() > 0 &&   $("#User_password").val().trim() != "" ){
                        if( confirm("Are you sure to change the password? If not, make the password field blank!") ){
                            $(".errorMessage").hide(); 
                            $("#loader").show();
                        }else
                            return false;
                    }else{
                        $("#loader").show();
                    }
                }'
        ), array('id' => 'mybtn' . rand(), 'class' => 'btn btn-theme'));
?>
    </div>
<?php $this->endWidget();?>

<script>

function checkCharacters(filed_id,frm){
    var flag=0;
     $("#"+filed_id).keypress(function (e) {
        var key = e.keyCode || e.which;       
        //Regular Expression
        if(frm=='uname'){
            var reg_exp = /^[A-Za-z0-9]+$/;
        }
        else{
            var reg_exp = /^[A-Za-z0-9 ]+$/;
        }
        //Validate Text Field value against the Regex.
        var is_valid = reg_exp.test(String.fromCharCode(key));
        if (!is_valid) {
          flag++;
        }
        return is_valid;
      });
      
      if(flag > 0){
          t8.error("No special characters Please!");
      }
      
}

function CheckPasswordStrength(password) {
        var password_strength = document.getElementById("password_strength");
 
        //TextBox left blank.
        if (password.length == 0) {
            password_strength.innerHTML = "";
            return;
        }
 
        //Regular Expressions.
        var regex = new Array();
        regex.push("[A-Z]"); //Uppercase Alphabet.
        regex.push("[a-z]"); //Lowercase Alphabet.
        regex.push("[0-9]"); //Digit.
        regex.push("[$@$!%*#?&]"); //Special Character.
 
        var passed = 0;
 
        //Validate for each Regular Expression.
        for (var i = 0; i < regex.length; i++) {
            if (new RegExp(regex[i]).test(password)) {
                passed++;
            }
        }
 
        //Validate for length of Password.
        if (passed > 2 && password.length > 8) {
            passed++;
        }
 
        //Display status.
        var color = "";
        var strength = "";
        switch (passed) {
            case 0:
            case 1:
                strength = "Weak";
                color = "red";
                break;
            case 2:
                strength = "Good";
                color = "darkorange";
                break;
            case 3:
            case 4:
                strength = "Strong";
                color = "green";
                break;
            case 5:
                strength = "Very Strong";
                color = "darkgreen";
                break;
        }
        password_strength.innerHTML = strength;
        password_strength.style.color = color;
    }


</script>
