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

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

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

    echo $form->dropDownListControlGroup($model, 'type', User::getTypeOption(),
	    array(
		'empty' => Yii::t('default', 'Select Type'),
		'ajax' => array(
		    'type' => 'POST',
		    'data' => 'js:{}',
		    'url' => CController::createUrl('user/create'),
		    'beforeSend' => 'js:function(){
                     if($("#User_type").val() == "LDAP")
                        xhr.abort();
                    }',
		    'success' => 'js:function(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 id="username_div">
	<?php
	echo $form->textFieldControlGroup($model, 'username', array(
	    'ajax' => array(
		'type' => 'POST',
		'data' => 'js:{username:$(this).val(),ajax:true}',
		'url' => CController::createUrl('user/create'),
		'beforeSend' => 'js:function(){
                     if($("#User_type").val() == "LDAP")
                         $("#loading-div").addClass("loading");
                     else
                         xhr.abort();
                    }',
		'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);
		    $("#User_email").attr("disabled","disabled");

                    // 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->textFieldControlGroup($model, 'email', array('size' => 60, 'maxlength' => 255));

	echo $form->passwordFieldControlGroup($model, 'password');
    ?>

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


	if ($model->isNewRecord) {

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

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

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

	echo $form->radioButtonListControlGroup($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">
	    $(document).ready(function(){
		var ldap = '<?php echo ($model->type == 'LDAP'); ?>';		
		if(ldap == '1')
		    $("#User_email").attr("disabled","disabled");
    })
</script>
