<?php
if (!defined('IN_CONTEXT')) die('access violation error!');
?>
<script type="text/javascript" language="javascript">
<!--
var popup_win = false;

function on_success(response) {
    var o_result = _eval_json(response);
    if (!o_result) {
        return on_failure(response);
    }
    
    var stat = document.getElementById("adminmifrm_stat");
    if (o_result.result == "ERROR") {
        document.forms["miform"].reset();
        
        stat.innerHTML = o_result.errmsg;
        return false;
    } else if (o_result.result == "OK") {
	    stat.innerHTML = "<?php _e('OK, redirecting...'); ?>";
	    window.location.href = o_result.forward;
    } else {
        return on_failure(response);
    }
}

function on_failure(response) {
    document.forms["miform"].reset();
    
    document.getElementById("adminmifrm_stat").innerHTML = "<?php _e('Request failed!'); ?>";
    return false;
}
function on_succ_subc(response) {
    var o_result = _eval_json(response);
    if (!o_result) {
        return on_failure(response);
    }
    if(o_result.is_id) {
        popup_win = show_iframe_win_n_modal('index.php?_m=mod_menu_item&_a=getid&_c=' + o_result.obj_name + '&_field=' + o_result.obj_field + '&id_c=' + o_result.id_c);
    }

    var stat = document.getElementById("adminmifrm_stat");
    var tmp_link = document.getElementById("mi_link_");
    var tmp_id = document.getElementById("tmp_id");
    if(o_result['flag'] == 'outer_url') {
            tmp_link.readOnly = false;
    } else {
            tmp_link.readOnly = true;
    }
    if (o_result.result == "OK") {
       stat.style.display = 'none';
       tmp_link.value = '';
       tmp_id.value = '';
    } else {
        return on_failure(response);
    }
}

function on_select(val) {
    $("#adminmifrm_stat").css({"display":"block"});
    $("#adminmifrm_stat").html("<?php _e('Loading submenu...'); ?>");
    _ajax_request('mod_menu_item', 'subcategory', {flag:val}, on_succ_subc, on_failure);
}

//-->
</script>
<div class="content_title">
	<h3><?php _e($content_title); ?></h3>
</div>
<div class="space"></div>
<div class="content_toolbar">
	<a href="<?php echo Html::uriquery('mod_menu_item', 'admin_list', array('menu_id' => $menu_id)); ?>" title=""><?php _e('Back'); ?></a>
</div>
<div class="space"></div>
<div class="status_bar">
	<span id="adminmifrm_stat" class="status" style="display:none;"></span>
</div>
<div class="space"></div>
<?php
$mi_form = new Form('index.php', 'miform', 'check_mi_info');
$mi_form->p_open('mod_menu_item', $next_action, '_ajax');
?>
<table id="miform_table" class="form_table" cellspacing="1">
    <tfoot>
        <tr>
            <td colspan="2">
            <?php
            echo Html::input('reset', 'reset', __('Reset'));
            echo Html::input('button', 'cancel', __('Cancel'), 'onclick="window.history.go(-1);"');
            echo Html::input('submit', 'submit', __('Save'));
            echo Html::input('hidden', 'mi[id]', $curr_mi->id);
            echo Html::input('hidden', 'mi[menu_id]', $menu_id);
            ?>
            </td>
        </tr>
    </tfoot>
    <tbody>
        <tr>
            <td class="label"><?php _e('Language'); ?></td>
            <td class="entry">
            <?php
            echo Toolkit::switchText($curr_mi->s_locale?$curr_mi->s_locale:$mod_locale, 
                Toolkit::toSelectArray($langs, 'locale', 'name'));
            echo Html::input('hidden', 'mi[s_locale]', 
           		$curr_mi->s_locale?$curr_mi->s_locale:$mod_locale);
            ?>
            </td>
        </tr>
        <tr>
            <td class="label"><?php _e('Name'); ?></td>
            <td class="entry">
            <?php
            echo Html::input('text', 'mi[name]', $curr_mi->name, 
                '', $mi_form, 'RequiredTextbox', 
                __('Please input menu item name!'));
            ?>
            </td>
        </tr>
        <tr>
            <td class="label"><?php _e('Link'); ?></td>
            <td class="entry">
            <?php
            echo Html::select('mi[mi_category]', 
                $menus, $curr_mi->mi_category, 'onchange="on_select(this.options[this.selectedIndex].value);"');
            ?>
            <?php
            echo Html::input('text', 'mi[link]', $curr_mi->link, 
                '', $mi_form);
            ?>
            <?php
            echo Html::input('hidden', 'tmp_id', '', 
                ' readonly', $mi_form);
            ?>
            </td>
        </tr>
        <tr>
            <td class="label"><?php _e('Parent'); ?></td>
            <td class="entry">
            <?php
            echo Html::select('mi[menu_item_id]', 
            	$select_mis, 
            	$curr_mi->menu_item_id);
            ?>
            </td>
        </tr>
        <tr>
            <td class="label"></td>
            <td class="entry">
            <?php
            echo Html::input('checkbox', 'mi[published]', '1', 
                Toolkit::switchText($curr_mi->published, 
                    array('0' => '', '1' => 'checked="checked"')));
            ?>
            &nbsp;<?php _e('Publish'); ?>
            </td>
        </tr>
        <tr>
            <td class="label"></td>
            <td class="entry">
            <?php
            echo Html::input('checkbox', 'ismemonly', '1', 
                Toolkit::switchText(strval(ACL::isMemOnly($curr_mi->for_roles)), 
                    array('0' => '', '1' => 'checked="checked"')));
            ?>
            &nbsp;<?php _e('Member only access'); ?>
            </td>
        </tr>
    </tbody>
</table>
<?php
$mi_form->close();
$running_msg = __('Saving menu item...');
$custom_js = <<<JS
$("#adminmifrm_stat").css({"display":"block"});
$("#adminmifrm_stat").html("$running_msg");
_ajax_submit(thisForm, on_success, on_failure);
return false;

JS;
$mi_form->addCustValidationJs($custom_js);
$mi_form->writeValidateJs();
?>
