{include file="page_header.htm"}
{insert_scripts files="../js/utils.js"}

<div class="tab-div">
	<div id="tabbar-div">
		<p>
		{foreach from=$config item=list name=config}
			{if $smarty.foreach.config.index==0}
				<span class="tab-front" id="{$list.code}-tab">{$list.title}</span> 
			{else}
				<span class="tab-back" id="{$list.code}-tab">{$list.title}</span> 
			{/if}
		{/foreach}
		</p>
	</div>
  
	<div id="tabbody-div">
	<form enctype="multipart/form-data" name="theForm" action="config.php" method="post">
    {foreach from=$config item=group name="body_group"}
    <table width="90%" id="{$group.code}-table" {if $smarty.foreach.body_group.iteration neq 1}style="display:none"{/if}>
      {foreach from=$group.vars item=var}
      <tr>
        <td class="label" valign="top">
          {$var.title}:
        </td>
        <td>
          {if $var.type eq "text"}
          <input name="value[{$var.id}]" id="{$var.code}" type="text" value="{$var.value}" size="40" />

			{if $var.code == 'thumb_width'}&nbsp;&nbsp;<a href="make_img.php?act=make_product">重新生成产品缩略图</a>{/if}
			{if $var.code == 'info_thumb_width'}&nbsp;&nbsp;<a href="make_img.php?act=make_info">重新生成信息缩略图</a>{/if}

          {elseif $var.type eq "password"}
          <input name="value[{$var.id}]" id="{$var.code}" type="password" value="{$var.value}" size="40" />

          {elseif $var.type eq "textarea"}
          <textarea name="value[{$var.id}]" id="{$var.code}" cols="40" rows="5">{$var.value}</textarea>

          {elseif $var.type eq "select"}
          	{if $var.code eq "template"}
                <select name="value[{$var.id}]" id="{$var.code}" style="width:150px;">
                {foreach from=$template item=name}
                <option value="{$name}" {if $name == $var.value} selected {/if}>{$name}</option>
                {/foreach}		
                </select>
            {else}
            {foreach from=$var.option key=key item=opt}
		     <label for="value_{$var.id}_{$opt}">
             <input type="radio" name="value[{$var.id}]" {if $var.code == 'mail_type'} onclick="javascript:emailSelect({$opt})" {/if} id="value_{$var.id}_{$opt}" value="{$opt}"  {if $var.value eq $opt} checked="true"{/if} {if $var.code == 'url_rewrite' && $opt==1} onclick="return confirm('URL Rewrite 功能要求您的 Web Server 必须是 Apache，\n并且起用了 rewrite 模块。\n同时请您确认是否已经将htaccess.txt文件重命名为.htaccess。');" {/if} /> {$key}</label>
            {/foreach}
            {/if}
          {elseif $var.type eq "file"}
          <input name="{$var.code}" id="{$var.code}" type="file" size="40" />
          {/if}
        </td>
      </tr>
      {/foreach}
      {if $group.code eq "smtp"}
        <tr>
          <td class="label">邮件地址:</td>
          <td>
            <input type="text" name="test_mail_address" id="test_mail_address" size="30" />
            <input type="button" value="发送测试邮件" onclick="sendTestEmail();" />
          </td>
        </tr>
      {/if}
    </table>
    {/foreach}

	<table width="90%">
		<tr>
			<td class="label"></td>
			<td align="left">	
				<input type="hidden" name="act" value="update" />
				<input type="hidden" name="mail_type" id="mail_type" value="" />
				<input name="submit" type="submit" value=" 确定 " class="button" />
				<input name="reset" type="reset" value=" 重置 " class="button" />
			</td>
		</tr>
	</table>
	</form>
	</div>
</div>
<script type="text/javascript">
{literal}
function emailSelect(type) {
	if(type==0){
		document.getElementById("smtp_host").disabled="disabled";
		document.getElementById("smtp_port").disabled="disabled";
		document.getElementById("smtp_user").disabled="disabled";
		document.getElementById("smtp_pass").disabled="disabled";
		document.getElementById("smtp_mail").disabled="disabled";
	}
	if(type==1){
		document.getElementById("smtp_host").disabled="";
		document.getElementById("smtp_port").disabled="";
		document.getElementById("smtp_user").disabled="";
		document.getElementById("smtp_pass").disabled="";
		document.getElementById("smtp_mail").disabled="";
	}
	document.getElementById("mail_type").value=type;
}

function sendTestEmail(){
  var eles              = document.forms['theForm'].elements;
  var mail_type         = document.getElementById("mail_type").value;
  var smtp_host         = document.getElementById("smtp_host").value;
  var smtp_port         = document.getElementById("smtp_port").value;
  var smtp_user         = document.getElementById("smtp_user").value;
  var smtp_pass         = document.getElementById("smtp_pass").value;
  var reply_email       = document.getElementById("smtp_mail").value;
  var test_mail_address = document.getElementById("test_mail_address").value;

  var mail_charset = "UTF8";

  var msg = '';
  if (smtp_host.length == 0)
  {
    msg += smtp_host_empty + "\n";
  }
  if (smtp_port.length == 0)
  {
    msg += smtp_port_empty + "\n";
  }
  if (reply_email.length == 0)
  {
    msg += reply_email_empty + "\n";
  }
  if (test_mail_address.length == 0)
  {
    msg += test_email_empty + "\n";
  }
  if (reply_email.length > 0 && test_mail_address.length > 0)
  {
    if (reply_email == test_mail_address)
    {
      msg += email_address_same + "\n";
    }
  }
  if (msg.length > 0){
    alert(msg);
    return;
  }

	$.ajax({
			url:"config.php",
			type:"POST",
			dataType:"json",
			data:{"act":"send_test_email","email":test_mail_address,"smtp_host":smtp_host,"smtp_port":smtp_port,"smtp_user":smtp_user,"smtp_pass":smtp_pass,"reply_email":reply_email,"mail_charset":mail_charset,"mail_type":mail_type,"is_ajax":1},
			beforeSend:function(){
				$("#loading").fadeIn("fast");
			},
			complete:function(){
				$("#loading").stop();
				$("#loading").fadeOut("fast");
			},
			success:function(result){
				 alert(result.message);
			}
	});
}
{/literal}
</script>

{foreach from=$config item=group name="body_group"}
	{foreach from=$group.vars item=var}
		{if $var.type eq "select"}
			{foreach from=$var.option key=key item=opt}
				{if $var.code == 'mail_type'}
					{if $var.value eq $opt}
					<script type="text/javascript">
					emailSelect({$var.value});
					</script>
					{/if}
				{/if}
			{/foreach}
		{/if}
	{/foreach}
{/foreach}
{insert_scripts files="tab.js"}
{include file="page_footer.htm"}