<script type="text/javascript">    $(document).ready(function () {
        $('#form').validate({
            rules: {
                company: {required: true},
                sucursal: {required: true},
                username: {
                    minlength: 2,
                    required: true
                }
                <?php  if(!isset($row->id)):?>,
                password: {
                    required: true,
                    minlength: 8,
                    maxlength: 20
                }
                <?php endif;?>,
                password_confirm: {minlength: 8, maxlength: 20, equalTo: "#password"}
            }, focusCleanup: false, highlight: function (label) {
                $(label).closest('.control-group').removeClass('success').addClass('error');
            }, success: function (label) {
                label.text('OK!').addClass('valid').closest('.control-group').addClass('success');
            }, errorPlacement: function (error, element) {
                error.appendTo(element.parents('.controls'));
            }
        });
        <?php $this->load->view('partials/script_edit.php'); ?>

        $("#company").change(function () {
            $("#sucursales").load("<?php echo base_url('admin/usuarios/sucursales'); ?>/" + $("#company").val());
        });

    }); // end document.ready</script>
<form action="<?php echo $action ?>" id="form" method="POST" enctype="multipart/form-data" class="form-horizontal"
      novalidate="novalidate"><input type="hidden" id="id" name="id"
                                     value="<?php echo (isset($row->id)) ? $row->id : "0"; ?>"/>
    <div id="page-title" class="clearfix"><span
            style="font-size:24px;font-weight:bold;float:left;margin-top:5px;"><?php echo intval($id)==0?$dir1:$dir2; ?></span>
        <div class="item-content" style="float:right;">
            <button type="submit" class="btn btn-inverse">Guardar</button>
            <button class="btn cancel">Cancelar</button>
        </div>
    </div> <!-- /.page-title -->
    <div class="row">
        <div class="span12">
            <div id="validation"
                 class="widget highlight widget-form">
                <?php if (isset($message)) echo $message; ?>
                <div class="widget-content">
                    <fieldset>
                        <?php if (($id == 0 || $id != 1) && $onlyAdmin == 'admin'): ?>
                            <div class="control-group"><label class="control-label" for="company">Empresa</label>
                                <?php $company = (isset($row->company)) ? $row->company : FALSE; ?>
                                <div class="controls">
                                    <select <?php echo ($onlyAdmin != 'admin') ? 'readonly' : ''; ?> id="company" name="company">
                                        <?php if (!isset($row->company)) { ?>
                                            <option value="">Seleccione</option>
                                        <?php } ?>
                                        <?php if ($empresas) {
                                            foreach ($empresas as $empresa) {
                                                $val = ($company == $empresa->id) ? TRUE : ''; ?>
                                                <option value="<?php echo $empresa->id ?>" <?php if ($val == TRUE || (isset($_POST['company']) && $_POST['company'] == $empresa->id)) { echo "selected"; } ?>>
                                                    <?php echo $empresa->razon_social ?>
                                                </option>
                                            <?php } } ?>
                                    </select>
                                </div>
                            </div>

                            <div id="sucursales" class="control-group"><label class="control-label" for="sucursal">Sucursal</label>
                                <?php $sucursal = (isset($row->sucursal)) ? $row->sucursal : FALSE; ?>
                                <div class="controls">
                                    <select <?php echo ($onlyAdmin != 'admin') ? 'readonly' : ''; ?> id="sucursal" name="sucursal">
                                        <?php if (!isset($row->sucursal)) { ?>
                                            <option value="">Seleccione</option>
                                            <option value="Master">Cuenta Master</option>
                                        <?php } else { ?>
                                            <option value="Master" <?php if($sucursal == "Master") { echo "selected"; } ?> >Cuenta Master</option>
                                        <?php } ?>
                                        <?php if ($sucursales) {
                                            foreach ($sucursales as $sucursale) {
                                                $val = ($sucursal == $sucursale->id) ? TRUE : ''; ?>
                                                <option
                                                    value="<?php echo $sucursale->id ?>" <?php if ($val == TRUE || (isset($_POST['sucursal']) && $_POST['sucursal'] == $sucursale->id)) { echo "selected"; } ?> >
                                                    <?php echo $sucursale->nombre ?>
                                                </option>
                                            <?php } } ?>
                                    </select>
                                </div>
                            </div>

                            <div class="control-group">
                                <label class="control-label" for="username">Usuario</label>
                                <div class="controls">
                                    <input <?php echo ($onlyAdmin != 'admin') ? 'readonly' : ''; ?> type="text" class="input-large" name="username" id="username"
                                        value="<?php if (isset($row->username)) echo $row->username; ?>">
                                </div>
                            </div>

                        <?php else: ?>

                            <div id="sucursales" class="control-group"><label class="control-label" for="sucursal">Sucursal</label>
                                <?php $sucursal = (isset($row->sucursal)) ? $row->sucursal : FALSE; ?>
                                <div class="controls">
                                    <select <?php echo ($master != 'Master') ? 'readonly' : ''; ?> id="sucursal" name="sucursal">
                                        <?php if (!isset($row->sucursal)) { ?>
                                            <option value="">Seleccione</option>
                                        <?php } ?>
                                        <?php if ($sucursales) {
                                            foreach ($sucursales as $sucursale) {
                                                $val = ($sucursal == $sucursale->id) ? TRUE : ''; ?>
                                                <option
                                                    value="<?php echo $sucursale->id ?>" <?php if ($val == TRUE || (isset($_POST['sucursal']) && $_POST['sucursal'] == $sucursale->id)) { echo "selected"; } ?> >
                                                    <?php echo $sucursale->nombre ?>
                                                </option>
                                            <?php } } ?>
                                    </select>
                                </div>
                            </div>

                            <div class="control-group">
                                <label class="control-label" for="username">Usuario</label>
                                <div class="controls">
                                    <input <?php echo ($master != 'Master') ? 'readonly' : ''; ?> type="text" class="input-large" name="username" id="username"
                                        value="<?php if (isset($row->username)) echo $row->username; ?>">
                                </div>
                            </div>
                        <?php endif; ?>
                        <div class="control-group">
                            <label class="control-label" for="password"><?=intval($id)!=0?'Nueva ':''?>Contraseña</label>
                            <div class="controls">
                                <input type="password" class="input-large" name="password" id="password" value="">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="password_confirm">Confirme Contraseña</label>
                            <div class="controls">
                                <input type="password" class="input-large" name="password_confirm" id="password_confirm" value="">
                            </div>
                        </div>
                        <?php if (($onlyAdmin == 'admin' && $id != 1) || $master == 'Master') { ?>
                            <div class="control-group"><label class="control-label">Habilitado</label>
                                <div class="controls">
                                    <?php $habilitado = (isset($row->active)) ? $row->active : '1';
                                    $si = ($habilitado == 1) ? TRUE : FALSE;
                                    $no = ($habilitado == 0) ? TRUE : FALSE; ?>
                                    <label class="radio">
                                        <input type="radio" name="active" id="si"
                                               value="1" <?php echo set_radio('active', '1', $si); ?>> Si </label>
                                    <label
                                        class="radio">
                                        <input type="radio" name="active" id="no"
                                               value="0" <?php echo set_radio('active', '0', $no); ?>> No
                                    </label>
                                </div>
                            </div>
                        <?php } else { ?>
                            <input type="hidden" name="active" id="si" value="1">
                        <?php } ?>
                        <div class="form-actions">
                            <button type="submit" class="btn btn-inverse">Guardar</button>
                            <button class="btn cancel">Cancelar</button>
                        </div>
                    </fieldset>
                </div> <!-- /widget-content -->
            </div>
        </div>
    </div>
</form>