{if $ajax eq 0}
{include file="$admingentemplates/admin_top.tpl"}
<div class="back">
    <a href="{$site_url}{$site_root}/admin/admin_settings.php">{$lang.button.back}</a>
</div>
<div class="cr"></div>
<div class="show_help" style="display: none;">&nbsp;</div>
<div class="red_header">{if $sec eq 2}{$lang.country.first_list}{elseif $sec eq 3}{$lang.country.install}{else}{$lang.country.list}{/if}</div>
<div class="cr">&nbsp;</div>
<div class="cr">&nbsp;</div>
<div class="help_text" style="display:block;">
    <div class="close_help">
        <img src="{$site_root}{$template_root}/images/close_help.png" alt="">
    </div>
    <div class="help_body">
        <span class="help_title">{$lang.help}:</span>{if $sec eq 2}{$lang.admin_help.list_first_countries}{elseif $sec eq 3}{$lang.admin_help.install_countries}{else}{$lang.admin_help.ref_countries}{/if}
    </div>
</div>

<ul class="tabs">
    <li>
        <a {if $active eq "maps"} class="current" {else} href="admin_settings.php?section=22" {/if}>{$lang.settings.map_tab}</a>
    </li>
    <li>
        <a {if $active eq "distance"} class="current" {else} href="admin_distance.php" {/if}>{$lang.settings.distanse_tab}</a>
    </li>
    <li>
        <a {if $active eq "country"} class="current" {else} href="admin_countries.php" {/if}>{$lang.settings.counries_tab}</a>
    </li>
    </ul>
    <div class="cr"></div>
<div class="user_operation_line" {if $sec eq 2}style="height: 3px;"{/if}>
    {if $sec eq 1}
        <div class="uol_item">
            <div class="uol_in_item" onclick="javascript: if(confirm('{$form.confirm|escape:javascript}')){literal}{{/literal}Delete();{literal}}{/literal}">{$lang.button.delete}</div>
        </div>
    {elseif $sec eq 3}
        <div class="uol_item">
            <div class="uol_in_item" onclick="javascript:Select('all');">{$lang.admin_reference.select_all}</div>
        </div>
        <div class="uol_item">
            <div class="uol_in_item" onclick="javascript:Select('allun');">{$lang.admin_reference.unselect_all}</div>
        </div>
        <div class="uol_item">
            <div class="uol_in_item" onclick="javascript:Select('usa');">{$lang.admin_reference.select_usa}</div>
        </div>
        <div class="uol_item">
            <div class="uol_in_item" onclick="javascript:Select('canada');">{$lang.admin_reference.select_canada}</div>
        </div>
        <div class="uol_item">
            <div class="uol_in_item" onclick="javascript:Select('australia');">{$lang.admin_reference.select_australia}</div>
        </div>
    {/if}
</div>
<div class="cr"></div>
<div id="tooltip" onclick="hideToolTip()" style="text-align:center; background-color: #FFFFFF; border: 1px dotted;text-align: {$default_left}; position:absolute; display:none; padding:5px;"></div>
{literal}
<script type="text/javascript">
$(function() {
$('#tool_tip_4 *').tooltip();
});
</script>
{/literal}

{if $sec eq 1}
    <div style="margin-bottom: 10px; margin-top: 0px; width: 100%; float: left;">
    <table  cellSpacing="3" cellPadding="0" >
	   <form method="post" action="{$form.action}"  enctype="multipart/form-data" name=add_form>
	   {$form.hiddens}
       <tr>
			<td align="right" class="main_header_text">
			&nbsp;{$lang.country.name}:&nbsp;
			</td>
			<td class="main_content_text" align="left"><input type="text" name="name" value="{$name}" size=30></td>
			<td class="main_content_text" align="left">
                <a class="admin_button_major" onclick="javascript: document.add_form.submit();">{$lang.button.save}<span>&nbsp;</span></a>
            </td>
		</tr>
		</form>
	</table>
    </div>
{/if}

<div id="country_list" {if $sec eq 1} class="user_status_elem_active" {else}class="user_status_elem" onclick="document.location.href='admin_countries.php?section=1'"{/if} width="80px">
    {if $sec eq 1}{$lang.admin.country.list}{else}<a>{$lang.admin.country.list}</a>{/if}
</div>
<div id="country_first" {if $sec eq 2} class="user_status_elem_active" {else}class="user_status_elem" onclick="document.location.href='admin_countries.php?section=2'"{/if} width="80px">
    {if $sec eq 2}{$lang.admin.country.first_list}{else}<a>{$lang.admin.country.first_list}</a>{/if}
</div>
<div id="country_install" {if $sec eq 3} class="user_status_elem_active" {else}class="user_status_elem" onclick="document.location.href='admin_countries.php?section=3&sel=install'"{/if} width="80px">
    {if $sec eq 3}{$lang.install_spr.install_country}{else}<a>{$lang.install_spr.install_country}</a>{/if}
</div>
{/if}
{if $sec eq 2}
<div style="float: left; width: 100%;" >
        <form class="panel_subtabs" style="width: 100%;" method="post" action="{$form.action}"  enctype="multipart/form-data" name=list_countries>
		<input type="hidden" name="sel" value="list_first"/>
		<table border="0" style="padding: 10px;" width="100%">
		<tr>
			<td class="text_head">
				{$lang.country.list_first_appear}:
			</td>
			<td>
			</td>
			<td class="text_head">
				{$lang.country.list_first}:
			</td>
		</tr>
		<tr>
			<td height="30px" width="45%">
				<div id="list">
				<select id="list_country" name="list_country[]" style="width: 100%" size="10" multiple>
					{foreach item=item from=$countries key=i}<option value="{$item.id}" {if $item.sel}selected{/if}>{$item.name}</option>{/foreach}
				</select>
				</div>
			</td>
			<td width="5%" style="text-align:center;">
				<div>                  
                    <img id="rotate" src="{$site_root}{$template_root}/images/arrow sort right.png" alt="" height="13" style="cursor: pointer;" onclick="MoveCountry('first_list');" />&nbsp;&nbsp;
				</div>
				<div>                
                    <img src="{$site_root}{$template_root}/images/arrow sort left.png" alt="" height="13" style="cursor: pointer;" onclick="MoveCountry('list');" />&nbsp;&nbsp;
				</div>
			</td>
			<td width="45%">
				<div id="first_list">
				<select id="first_country" name="first_country[]" style="width: 100%" class="index_select" size="10" multiple>
					{section name=s loop=$country}
						<option value="{$country[s].id}">{$country[s].name}</option>
					{/section}
				</select>
				</div>
			</td>
			<td width="5%" style="text-align:center;">
                <img id="edit_button{$countries[spr].id}" src="{$site_root}{$template_root}/images/arrow_sort_up.png" alt="" height="13" style="cursor: pointer;" onclick="MoveFirstList('up');" />&nbsp;&nbsp;
                <img id="edit_button{$countries[spr].id}" src="{$site_root}{$template_root}/images/arrow_sort_down.png" alt="" height="13" style="cursor: pointer;" onclick="MoveFirstList('down');" />&nbsp;&nbsp;
			</td>
		</tr>
		<tr>
			<td>
				
			</td>
			<td colspan="3">
				<div id="error_msg"></div>
			</td>
		</tr>
		</table>
		</form>
</div>
<div style="float: left; margin-top: 10px;">
    <a class="admin_button_major" onclick="SaveList();">{$lang.button.save}<span>&nbsp;</span></a>
</div>
{elseif $sec eq 3}
	<div id="countries" class="panel_subtabs" style="margin-bottom:15px; float: none;">
	<form action="{$form.action}" method="POST" name="install_countries" id="install_countries" enctype="multipart/form-data" style="margin: 10px;">
	{$form.hiddens}
	<div>
		<font style="font-weight:bold;">{$lang.admin.country.install_type}</font><br>
		<input id="id_base_type_latin_radio" type="radio" name="base_type" value="latin" checked="checked" onclick="javascript: chooseCountriesType('latin');" /> <label for="id_base_type_latin_radio">{$lang.admin.country.install_latin}</label><br />
		<input id="id_base_type_rus_radio" type="radio" name="base_type" value="rus" onclick="javascript: chooseCountriesType('rus');"/> <label for="id_base_type_rus_radio">{$lang.admin.country.install_rus}</label><br /><br />
	</div>
	<div align="left" style="padding-top:5px;"><input type="checkbox" id="not_reinstall" value="0">{$lang.install_spr.not_reinstall}</div>
	
	<div id="latin" style=" margin-top: 10px; margin-bottom: 10px;">
    	<table class="table_main" cellpadding="5" cellspacing="0" border="0" width="100%">
    		<tr bgcolor="#ffffff">
    		{section start=1 name=i loop=$country}
    				<td class="main_content_text" align="left"><input type="checkbox" id="sel_country[{$smarty.section.i.index}]" name="sel_country[{$smarty.section.i.index}]" value="{$country[i].name}"></td>
                    <td class="main_content_text" align="left" width="195px;">{$country[i].name}</td>				
    				<td class="main_content_text" align="left" {if $smarty.section.i.index eq $country|@sizeof-1}colspan="11"{/if}>&nbsp;</td>
    		{if $smarty.section.i.index is div by 4 && $smarty.section.i.index ne 0 && $smarty.section.i.index ne $country|@sizeof-1}
    			</tr>
    			<tr bgcolor="#ffffff">
    		{/if}
    		{/section}
    		</tr>
    	</table>
	</div>
	<div id="rus" style="display:none;">
    	<table class="table_main" cellpadding="5" cellspacing="1" border="0" width="100%">
    		<tr bgcolor="#ffffff">
    			<td class="main_content_text" align="center" width="50%"><a href="#" onclick="javascript:Select('all');">{$lang.admin_reference.select_all}</a></td>
    			<td class="main_content_text" align="center" width="50%"><a href="#" onclick="javascript:Select('allun');">{$lang.admin_reference.unselect_all}</a></td>
    		</tr>
    	</table>
    	<table class="table_main" cellpadding="5" cellspacing="1" border="0" width="100%">
    		<tr bgcolor="#ffffff">
    		{section start=1 name=i loop=$country_rus}
    				<td class="main_content_text" align="left" width="195px;">{$country_rus[i].name}</td>
    				<td class="main_content_text" align="left"><input type="checkbox" id="sel_country_rus[{$smarty.section.i.index}]" name="sel_country_rus[{$smarty.section.i.index}]" value="{$country_rus[i].name}"></td>
    				<td class="main_content_text" align="left" {if $smarty.section.i.index eq $country_rus|@sizeof-1}colspan="11"{/if}>&nbsp;</td>
    		{if $smarty.section.i.index is div by 4 && $smarty.section.i.index ne 0 && $smarty.section.i.index ne $country_rus|@sizeof-1}
    			</tr>
    			<tr bgcolor="#ffffff">
    		{/if}
    		{/section}
    		</tr>
    	</table>
	</div>
	</form>
	</div>
    <div id="install_bottom" class="main_bottom">
        <a class="admin_button_major" onclick="javascript:Install();">{$lang.button.install}<span>&nbsp;</span></a>
    </div>
	<div id="install_yes" align="left" style="display:none;"></div>
	<div id="install" class="panel_subtabs" style="display:none;height:220px;overflow-y:auto;">
	</div>
{else}
    <div id="list_country_table" style="float: left; width: 100%;">
				<table border=0 cellspacing=0 cellpadding=0 width="100%">
					        <tr class="table_header">
								<td align="center" width="5%"><input type="checkbox" name="select_all" onclick="PutAll()" /></td>
								<td align="left" width="80%">{$lang.country.country}</td>
								<td align="right" width="15%">{$lang.users.actions}</td>
							</tr>
							{if $countries}
							{section name=spr loop=$countries}
							<tr {if $smarty.section.spr.index mod 2 eq 1}class="even"{else}class="odd"{/if}>
								<td align="center"><input type="checkbox" name="delete" id="delete" value="{$countries[spr].id}" /></td>
								<td align="left" style="padding-left: 10px;">									
									<div style="float: left;"><a id="link{$countries[spr].id}" href="{$countries[spr].editlink}">{$countries[spr].name}</a></div>
									<div style="float: left;"><input id="text_input{$countries[spr].id}" type="text" value="{$countries[spr].name}" style="display:none"/></div>
                                    <div id="inform_div{$countries[spr].id}" class="error_msg" style="display:none"></div>
								</td>
								<td align="center">
                                    <div class="block_action fr" style="display: none;">                                        
                                        <img id="save_button{$countries[spr].id}" src="{$site_root}{$template_root}/images/success.png" onmouseout="javascript:hideToolTip();" onmouseover="javascript: showToolTip(event,'{$lang.button.save}');return false" alt="" height="13" style="cursor: pointer; display:none; margin-left: 10px;" onclick="javascript: saveAction({$countries[spr].id});" />
                                        <img id="edit_button{$countries[spr].id}" onmouseout="javascript:hideToolTip();" onmouseover="javascript: showToolTip(event,'{$lang.button.edit}');return false" src="{$site_root}{$template_root}/images/edit.png" alt="" height="13" style="cursor: pointer; margin-left: 10px;" onclick="javascript: editAction({$countries[spr].id});" />
                                        <img src="{$site_root}{$template_root}/images/photo_delete.png" onmouseout="javascript:hideToolTip();" onmouseover="javascript: showToolTip(event,'{$lang.button.delete}');return false" alt="" height="13" style="cursor: pointer; margin-left: 10px;" onclick="javascript: if(confirm('{$form.confirm|escape:javascript}')){literal}{{/literal}location.href='{$countries[spr].deletelink}'{literal}}{/literal}" />
                                    </div>

                                </td>
							</tr>
							{/section}
							{else}
							<tr height="40">
								<td class="main_error_text" align="left" colspan="4" bgcolor="#FFFFFF">{$lang.country.empty}</td>
							</tr>
							{/if}
                            <tr class="table_header">
		                        <td colspan="11">
		                            &nbsp;&nbsp;
		                            {if $links}
		                            {foreach item=item from=$links.pages}
		                                &nbsp;<a onclick="PageList('users_table_div', '{$item.link}');" class="page_link" {if $item.selected} style=" font-weight: bold; text-decoration: none;" {/if}>{$item.name}</a>&nbsp;
		                            {/foreach}
		                            {/if}
		                            <div class="page_navigation">
		                            {if $links.previous}
		                                <a onclick="PageList('users_table_div', '{$links.previous}');">< {$lang.content.previous} {$per_page}</a>
		                            {/if}
		                            {if $links.next}
		                                <a onclick="PageList('users_table_div', '{$links.next}');">{$lang.content.next} {$per_page} ></a>
		                            {/if}
		                            </div>
		                        </td>
		                    </tr>
					</table>
        </div>
{/if}
<script type="text/javascript">
{literal}
		function showToolTip(e,text){
			if(document.all)e = event;
			var obj = document.getElementById('tooltip');
			obj.innerHTML = text;
			obj.style.display = 'block';
			var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
			if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0;
			var leftPos = e.clientX - $('#tooltip').width()-10;
			if(leftPos<0)leftPos = 0;
			obj.style.left = leftPos + 'px';
			obj.style.top = e.clientY - obj.offsetHeight -10 + st + 'px';
		}
		function hideToolTip(){
			document.getElementById('tooltip').style.display = 'none';
		}
{/literal}		
</script>

<script type="text/javascript">
{literal}
function editAction(id){
	link_obj = getById('link'+id);
	text_input_obj = getById('text_input'+id);
	edit_button_ogj = getById('edit_button'+id);
	save_button_ogj = getById('save_button'+id);

	link_obj.style.display = 'none';
	text_input_obj.style.display = '';
	edit_button_ogj.style.display = 'none';
	save_button_ogj.style.display = '';

	text_input_obj.focus();
}

function saveAction(id){
	inform_div_obj = getById('inform_div'+id);
	link_obj = getById('link'+id);
	text_input_obj = getById('text_input'+id);
	edit_button_ogj = getById('edit_button'+id);
	save_button_ogj = getById('save_button'+id);
	value = text_input_obj.value;

	file_name = '{/literal}{$form.action}{literal}';
	str = 'sel=rename_country&id='+id+'&value='+value;
	destination_odj = inform_div_obj;
	tmp_text = 'Saving...';
	anisochronous = false;
	ShowAjaxError(tmp_text, '', 'info');

	ajaxRequest(file_name, str, destination_odj, tmp_text, anisochronous);

	CloseAjaxError(1);
	inform_div_obj.style.display = '';

	saved_obj = getById('saved'+id);
	saved = saved_obj.value;
	if (saved == '1'){
		link_obj.style.display = '';
		link_obj.innerHTML = value;
		text_input_obj.style.display = 'none';
		edit_button_ogj.style.display = '';
		save_button_ogj.style.display = 'none';
		edit_button_ogj.focus();
	}
}

function MoveCountry(list) {
	if (list == 'first_list'){
		$('#list_country option:selected').appendTo('#first_country'); 
	} else if (list == 'list') {
		$('#first_country option:selected').appendTo('#list_country');
	}
}

function MoveFirstList(position) {
	var len = $('#first_country option').size();
	var list_text = new Array();
	var list_val = new Array();
	i=0;
	t=0;
	$('#first_country').find('option').each(function () {
		list_text[t] = $(this).text();
		list_val[t] = $(this).val();
		if($(this).is(":selected")) {
			i++;
			sel=t;
		}
		t++;
	});
	$("#error_msg").text('');
	if(i>1) {
		$("#error_msg").text('{/literal}{$lang.admin.country.err_count_select}{literal}');
		return;
	}
	if (position == 'up' && sel >0) {
		$('#first_country option:eq('+(sel-1)+')').val(list_val[sel]);
		$('#first_country option:eq('+(sel-1)+')').text(list_text[sel]);
		$('#first_country option:eq('+sel+')').val(list_val[sel-1]);
		$('#first_country option:eq('+sel+')').text(list_text[sel-1]);
		
	} else if (position == 'down' && sel < len-1) {
		$('#first_country option:eq('+(sel+1)+')').val(list_val[sel]);
		$('#first_country option:eq('+(sel+1)+')').text(list_text[sel]);
		$('#first_country option:eq('+sel+')').val(list_val[sel+1]);
		$('#first_country option:eq('+sel+')').text(list_text[sel+1]);
	}
}

function SaveList() {
	$('#first_country').find('option').each(function(){
		$(this).attr('selected', 'selected');
	});
	document.list_countries.submit();
}

function getById(name){
	return document.getElementById(name);
}

function Install(){
	if (!$('#not_reinstall').is(':checked')){
		if(!confirm('{/literal}{$lang.admin.country.rewrite_alert|escape:javascript}{literal}')){
			return;
		}
	}

	var err = 1; 
	var total = $("#ins_type").val()=='latin'?{/literal}{$country|@sizeof}{literal}:{/literal}{$country_rus|@sizeof}{literal};
	country = $("#ins_type").val()=='latin'?'sel_country':'sel_country_rus'; 
	for(var i = 1; i < total; i++)
	{
		if(document.getElementById(country+'['+i+']').checked == true)
		{
			err = 0;
		}
	}
	if (err == 0) 
	{
	$("#countries").hide();
    $("#install_bottom").hide();
	$("#install").show();
	$("#install_yes").show();
	var re = 1;
	var strhtml = '';
	//if (document.getElementById('not_reinstall').checked == true) re = 0;
	if ($('#not_reinstall').is(':checked')) re = 0;
	ins_type = $("#ins_type").val();
	file_name = '{/literal}{$form.action}{literal}'; 
	total = $("#ins_type").val()=='latin'?{/literal}{$country|@sizeof}{literal}:{/literal}{$country_rus|@sizeof}{literal}; 
	for (var i=1; i<total; i++){
		if (document.getElementById(country+'['+i+']').checked == true){
			ajaxLoadingImage(1, 'install_yes');
			sel_country = $('#'+country+'['+i+']').val();
			$.post(file_name,{async: false ,sel:'start_install',ins_type:ins_type,re:re,ind:i,sel_country:sel_country},function(data){
							strhtml = strhtml + '<div style="margin: 10px;">'+data+'</div>';
							$("#install").html(strhtml);
							ajaxLoadingImage(0, 'install_yes');
						}
					);
			re = 0;
			//setTimeout(";",2000);
		}
	}
	}
}

function Delete(){
    var mas=[]
    var i=0
    $('input[name=delete]:checked').each(function(){
        mas[i] = $(this).val()   
        i++    
    })
    var par = "ajax=1&id="+mas+"&sel=del";
        $.post("{/literal}{$form.action}{literal}", par, function(data){
            $("#list_country_table").html(data);
        })  
}

function PutAll() {
         var o=document.getElementsByName('select_all')	
         var i
         if (o[0].checked){                    
            o=document.getElementsByName('delete')
            for (i=0; i<o.length; i++){                
                o[i].checked = true
            }            
         }
         else{
            o=document.getElementsByName('delete')
            for (i=0; i<o.length; i++){                
                o[i].checked = false
            }            
         }             
}

    function PageList(id_div, par) {
        ajaxLoadingImage(1, 'list_country_table');
        $.post("{/literal}{$form.action}{literal}", par, function(data){
            ajaxLoadingImage(0, 'list_country_table');
            $('#list_country_table').html(data);
        });
    }

function chooseCountriesType(type){
	switch(type){
		case 'latin':
			Select('allun');
			$("#rus").hide();
			$("#latin").show();
			$("#ins_type").val('latin');
		break;
		case 'rus':
			Select('allun');
			$("#latin").hide();
			$("#rus").show();
			$("#ins_type").val('rus');
		break;
		default :
			$("#ins_type").val('latin');
	}
}

function Select(par)
{
	total = $("#ins_type").val()=='latin'?{/literal}{$country|@sizeof}{literal}:{/literal}{$country_rus|@sizeof}{literal}; 
	switch(par)
	{
		case "all":
			for(var i = 1; i < total; i++) {
				if ($("#ins_type").val()=='latin')
			document.getElementById('sel_country['+i+']').checked = true;
				else 
					document.getElementById('sel_country_rus['+i+']').checked = true;
			}
		break
		case "allun":
			for(var i = 1; i < total; i++) {
			if ($("#ins_type").val()=='latin')
			document.getElementById('sel_country['+i+']').checked = false;
				else 
					document.getElementById('sel_country_rus['+i+']').checked = false;
			}
		break
		case "usa":
			document.getElementById('sel_country[251]').checked = true;
		break
		case "canada":
			document.getElementById('sel_country[43]').checked = true;
		break
		case "australia":
			document.getElementById('sel_country[14]').checked = true;
		break
	}
}
{/literal}
</script>
{if $ajax eq 0}
{include file="$admingentemplates/admin_bottom.tpl"}
{/if}