<?php
if (!defined('IN_CONTEXT')) die('access violation error!');
?>
<script type="text/javascript" language="javascript">
<!--
function on_del_success(response) {
    var o_result = _eval_json(response);
    if (!o_result) {
        return on_failure(response);
    }
    
    var stat = document.getElementById("adminlanglst_stat");
    if (o_result.result == "ERROR") {
        stat.innerHTML = o_result.errmsg;
        return false;
    } else if (o_result.result == "OK") {
	    stat.innerHTML = "<?php _e('OK, refreshing...'); ?>";
	    reloadPage();
    } else {
        return on_failure(response);
    }
}

function on_del_failure(response) {
    document.getElementById("adminlanglst_stat").innerHTML = "<?php _e('Request failed!'); ?>";
    reloadParent();
    return false;
}

function delete_lang(lang_id) {
	if (confirm("Delete the selected language?")) {
	    var stat = document.getElementById("adminlanglst_stat");
	    stat.style.display = "block";
	    stat.innerHTML = "<?php _e('Deleting selected language...'); ?>";
		_ajax_request("mod_lang", 
			"admin_delete", 
	        {
	            l_id:lang_id
	        }, 
			on_del_success, 
			on_del_failure);
	}
}

function on_tog_success(response) {
    var o_result = _eval_json(response);
    if (!o_result) {
        return on_failure(response);
    }
    
    var stat = document.getElementById("adminlanglst_stat");
    if (o_result.result == "ERROR") {
        stat.innerHTML = o_result.errmsg;
        return false;
    } else if (o_result.result == "OK") {
        alert("<?php _e('Install finished!'); ?>");
 	    stat.innerHTML = "<?php _e('OK, refreshing...'); ?>";
	    reloadParent();
    } else {
        return on_failure(response);
    }
}

function on_tog_failure(response) {
    on_del_failure(response);
}

function toggle_default(lang_id) {
    var stat = document.getElementById("adminlanglst_stat");
    stat.style.display = "block";
    stat.innerHTML = "<?php _e('Setting default language...'); ?>";
    _ajax_request("mod_lang", 
    	"admin_make_default", 
    	{
    	    l_id:lang_id
    	}, 
    	on_tog_success, 
    	on_tog_failure);
}
//-->
</script>
<div class="content_toolbar">
	<table cellspacing="0" class="wrap_table">
		<tbody>
			<tr>
				<td><div class="title"><?php _e('Language Files'); ?></div></td>
				<td><a href="<?php echo Html::uriquery('mod_lang', 'admin_add'); ?>" title=""><?php _e('Upload Language File'); ?></a></td>
			</tr>
		</tbody>
	</table>
</div>
<div class="space"></div>
<div class="status_bar">
	<span id="adminlanglst_stat" class="status" style="display:none;"></span>
</div>
<div class="space"></div>
<table cellspacing="0" class="list_table" id="admin_lang_list">
	<thead>
		<tr>
		    <th width="20">ID</th>
            <th><?php _e('Name'); ?></th>
            <th><?php _e('Locale'); ?></th>
            <th><?php _e('Is Default'); ?></th>
            <th><?php _e('Operation'); ?></th>
        </tr>
    </thead>
    <tbody>
    <?php
    if (sizeof($langs) > 0) {
        $row_idx = 0;
        foreach ($langs as $lang) {
    ?>
        <tr class="row_style_<?php echo $row_idx; ?>">
        	<td><?php echo $lang->id; ?></td>
        	<td><?php echo $lang->name; ?></td>
        	<td><?php echo $lang->locale; ?></td>
        	<td>
        	<?php
        	if ($lang->locale == SessionHolder::get('_LOCALE')) {
        	    _e('Yes');
        	} else {
        	    echo '<a href="#" onclick="toggle_default(\''.$lang->id.'\');return false;" title="'.__('Set as default language').'">'.__('No').'</a>';
        	}
        	?>
        	</td>
        	<td>
        		<span class="small">
        			<a href="#" onclick="delete_lang(<?php echo $lang->id; ?>);return false;" title="<?php _e('Delete'); ?>"><?php _e('Delete'); ?></a>
        		</span>
        	</td>
        </tr>
    <?php
            $row_idx = 1 - $row_idx;
        }
    } else {
    ?>
    	<tr class="row_style_0">
    		<td colspan="5"><?php _e('No Records!'); ?></td>
    	</tr>
    <?php
    }
    ?>
    </tbody>
</table>
