<?php
if ($_POST['mesg'])
{
   $max_line = 1000;
   $mesg = $_POST['mesg'];
   $bg_color = $_POST['bg_color'];
   $ft_color = $_POST['ft_color'];
   
   
   $tmpcnt = count(split("\n",$mesg));
   $numhtmlstr = "";
   
   for ($i = 1; $i < $tmpcnt + 1; $i++)
   {
      if ($i > $max_line )
      {
        echo "<script language=\"javascript\">alert(\"請輸入少於 $max_line 行的文字。\");location.replace(\"./mk-code.php\");</script>";
        return;
      }
   	  if (strlen($i) == 1) $i = "000".$i;
   	  if (strlen($i) == 2) $i = "00".$i;
   	  if (strlen($i) == 3) $i = "0".$i;
   	  if (strlen($i) == 4) $i = "".$i;
      $numhtmlstr .= "$i\n";
   }

   $mesghtml = htmlspecialchars($mesg);
   
   $mesghtml = ereg_replace("\\\\", '', $mesghtml);
   $mesghtml = ereg_replace("@b@", '<b>', $mesghtml);
   $mesghtml = ereg_replace("@eb@", '</b>', $mesghtml);
   $mesghtml = ereg_replace("<br>", '', $mesghtml);
   
   $mesghtml2 = $mesghtml;
   
   $sourceline = htmlspecialchars(ereg_replace("\\\\", '', htmlspecialchars($numhtmlstr)));
   $sourcehtml = htmlspecialchars($mesghtml2);
   $html_01 = "&lt;div style=&quot;overflow:auto; &quot;&gt;\n";
   if ($_POST['IsCSS'] == "1")
   {
   	  $html_01 .= "&lt;STYLE TYPE='text/css'&gt;.code pre{font-family: Courier, monospace;font-size: 14px;line-height:120%;}&lt;/STYLE&gt;\n";
   }
   
   $html_01 .= "&lt;table border=&quot;0&quot; cellspacing=&quot;1&quot; width=&quot;100%&quot;  cellpadding=&quot;3&quot; class=&quot;code&quot; bgcolor=&quot;#999999&quot;&gt;\n&lt;tr&gt;\n&lt;td width=&quot;1&quot; align=&quot;right&quot; nowrap&gt;&lt;pre&gt;\n";
   $html_01 .= $sourceline;
   $html_01 .= "&lt;/pre&gt;&lt;/td&gt;\n&lt;td align=&quot;left&quot; bgcolor=&quot;$bg_color&quot; valign=&quot;top&quot;&gt;&lt;pre&gt;\n&lt;font color=&quot;$ft_color&quot;&gt;";
   $html_01 .= $sourcehtml;
   $html_01 .= "&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;\n&lt;/tr&gt;\n&lt;/table&gt;&lt;/div&gt;\n";

}
?>
<html>
<head>
<title>HTML 轉換程式</title>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<STYLE TYPE='text/css'>
.code pre
{
   font-family: Courier, monospace;
   font-size: 10pt;
   line-height:120%;
}
	    
a:hover
{
   background: rgb(255, 255, 0) none repeat scroll 0% 50%;
   text-decoration: none;
   color: rgb(0, 0, 255); 
}

a:visited
{
   color: rgb(0, 0, 255);
}
</STYLE>
<script language="javascript">
//only for IE, too bad :(
function copy_code()
{
   var range=document.all["source_code"].createTextRange();
   range.execCommand("copy");
}

function showHTML()
{
   divSRC.style.display = "none";
   divHTML.style.display = "inline";
}

function showCode()
{
   divHTML.style.display = "none";
   divSRC.style.display = "inline";
}
</script>
</head>

<body <?php if ($_POST['mesg']) echo 'onLoad="showHTML();"';?>>
<table width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><form name="form1" method="post" action="">
      <p>
        <textarea name="mesg" cols="100" rows="10" id="mesg"><?php echo ereg_replace("\\\\", '', $mesghtml)?></textarea><p>
        <input type="checkbox" name="IsCSS" value="1">包含 CSS 樣式表
        選擇背景顏色
        <select name="bg_color" >
           <option value="#D7D7D7" style="background-color:#D7D7D7" selected>#D7D7D7</option>
           <option value="#BBF9CB" style="background-color:#BBF9CB">#BBF9CB</option>
           <option value="#FFFFCC" style="background-color:#FFFFCC">#FFFFCC</option>
           <option value="#DAD9C2" style="background-color:#DAD9C2">#DAD9C2</option>
           <option value="#CBD1FE" style="background-color:#CBD1FE">#CBD1FE</option>
           <option value="#000000" style="background-color:#000000">#000000</option>
           <option value="#FEC5D0" style="background-color:#FEC5D0">#FEC5D0</option>
        </select>
        選擇字型顏色
        <select name="ft_color" >
           <option value="#000000" style="background-color:#000000" selected>#000000</option>
           <option value="#330000" style="background-color:#330000">#330000</option>
           <option value="#00FF00" style="background-color:#00FF00">#00FF00</option>
           <option value="#0000CC" style="background-color:#0000CC">#0000CC</option>
           <option value="#FF0000" style="background-color:#FF0000">#FF0000</option>
           <option value="#FF6600" style="background-color:#FF6600">#FF6600</option>
           <option value="#9900CC" style="background-color:#9900CC">#9900CC</option>
        </select>
        
        <input type="submit" name="Submit" value="轉換"><p>
        <span onClick="showHTML();"><a href="#">顯示 HTML</a></span> | <span onClick="showCode();"><a href="#">顯示原始碼</a></span><p>
        <div id='source' style="display:none; "><textarea name="source_code" id="souce_code"><?php echo $html_01 ?></textarea></div>
      </p>

    </form>      </td>
  </tr>
  <tr>
    <td><div id="divHTML" style="display:none; ">原始碼：<div style="overflow:auto;">
     <table width="100%" border="1" cellpadding="2" cellspacing="0" class="code" bordercolor="#8EB4F9">
         <tr>
           <td width="0%" align="right" valign="top" nowrap bgcolor="#8EB4F9"><pre><?php echo $numhtmlstr;?></pre></td>
           <td width="100%" align="left" valign="top" bgcolor="<?php if (!empty($bg_color)) {echo $bg_color;} else {echo '#8EB4F9';} ?>"><pre><font color="<?php if (!empty($bg_color)) {echo $ft_color;} else {echo '#00FF00';} ?>"><?php echo $mesghtml2;?></font></pre></td>
          </tr>
       </table></div></div>
    </td>
  </tr>
  <tr>
    <td><p><div id="divSRC" style="display:none;">HTML 原始碼：<span onClick="copy_code();"><a href="#">複製到剪貼簿</a></span>
    <div style="overflow:auto;">    
      <table border="0" cellspacing="1" width="100%"  cellpadding="3" class="code" bgcolor="#999999">
        <tr>
          <td width="100%" bgcolor="#CCF1FD" nowrap><pre><?php echo $html_01 ?></pre></td>
        </tr>
      </table></div></div></td>
  </tr>
</table>
</body>
</html>