<%@ Page Language="C#" AutoEventWireup="true" Inherits="PowerEasy.Module.General.WebSite.Admin.Template.TemplateUI"
    Title="模板添加/修改" ValidateRequest="false" MasterPageFile="~/Admin/MasterPage.master"
    CodeBehind="Template.aspx.cs" %>

<asp:Content ID="ContentNavigation" ContentPlaceHolderID="CphNavigation" runat="server">
    <pe:ExtendedSiteMapPath ID="SmpNavigator" SiteMapProvider="AdminMapProvider" runat="server" />
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="CphContent" runat="Server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
    <table style="width: 100%; margin: 0 auto;" cellpadding="2" cellspacing="1" class="border">
        <tr>
            <td colspan="2" class="tdbgleft">
                <pe:AlternateLiteral ID="AlternateLiteral1" Text="填写文件名：" AlternateText="编辑模板：" runat="Server" /><asp:Label
                    ID="LblFileName" runat="server"></asp:Label><asp:TextBox ID="TxtFileName" runat="server"></asp:TextBox><pe:RequiredFieldValidator
                        ID="ValrTxtFileName" ControlToValidate="TxtFileName" Display="Dynamic" runat="server"
                        ErrorMessage="请填写文件名"></pe:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="ValeTxtFileName" ControlToValidate="TxtFileName"
                    Display="Dynamic" runat="server" ErrorMessage='文件扩展名应为html或htm，不能包含\/:*?"<>|.和空格等字符！'></asp:RegularExpressionValidator><asp:Label
                        ID="savefilename" Style="color: Red" runat="server"></asp:Label>
            </td>
        </tr>
        <tr class="tdbg">
            <td align="left" style="height: 430px" valign="top" colspan="2">
                <table style="width: 100%">
                    <tr>
                        <td style="text-align: left; vertical-align: top; width: 200px;" id="ft">
                            <pec:CodeSnippetPanel ID="CodeSnippetPanel1" runat="server" />
                        </td>
                        <td onclick="switchSysBar('sp','ft','left')" style="width: 12px; cursor: pointer">
                            <img id="sp" src="<% =StyleSheetPath %>images/butc.gif" alt="关闭" style="border: 0px;
                                width: 12px;" />
                        </td>
                        <td style="vertical-align: top;">
                            <div>
                                <input type="text" class="inputtext" value="" id="cbox" size="50" />
                                <input type="button" onclick="findText()" value="查找下一个并选择" />
                            </div>
                            <asp:TextBox ID="TxtTemplate" runat="server" Height="421px" Width="99%" TextMode="MultiLine"
                                Rows="7" Wrap="False"></asp:TextBox>
                            <div style="text-align: center; vertical-align: top;">
                                <img alt="增加高度" src="../../Admin/Images/sizeplus.gif" onclick="sizeChange(50)" />
                                <img alt="减少高度" src="../../Admin/Images/sizeminus.gif" onclick="sizeChange(-50)" />
                            </div>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    <div id="wysiwyg" class="HelpTips" title="可视化编辑URL参数设置">
        <div id="wysiwygTitle" class="HelpTitle" style="padding-left:0">
            <a href="javascript:" onclick="javascript:$('#ly').click();" title="关闭" class="tips_close">
            </a><span style="padding-left:10px">可视化编辑</span>
        </div>
        <div id="wysiwygInfo" class="HelpInfo" style="height:100px;height: 100px !important;">
           <table width="448">
           <tr>
           <td>
             URL参数设置
           </td>
           </tr>
           <tr>
              <td>
                  <input id="urlpama" class="inputtext" style="width:400px;" value="page=1<%=m_QueryString %>" />
                  <br />
              </td>
           </tr>
           <tr>
               <td align="center">
                  <input id="Button1" type="button" class="inputbutton" value="编辑" onclick="Gowysiwyg();"/>
                  <input id="Button2" type="button" class="inputbutton" value="取消" onclick="javascript:$('#ly').click();"/>
               </td>
           </tr>
           </table>
        </div>
    </div>
    <div id="ly" style="position: absolute; top: 0px; filter: alpha(opacity=60); opacity: 0.5;
        background-color: #777; z-index: 1000; left: 0px; display: none;">
       <iframe id="ifly" name="ifly" src="" frameborder="0" width="100%" height="100%" scrolling="no"
            marginheight="0" marginwidth="0" style="z-index: 999; filter: chroma(color=#ffffff);"></iframe>
    </div>
    
    <asp:HiddenField ID="HdnKeyword" Value="0" runat="server" />
    <pe:FloatDivDialog ID="FdvShowWindow" runat="server" EnableViewState="false" Width="800"
        Height="430" Enabled="true">
    </pe:FloatDivDialog>
    <br />
    <center>
    <asp:Button ID="BtnUse" runat="server" Text="应用" OnClick="BtnUse_Click" />&nbsp;
        <asp:Button ID="BtnSubmit" runat="server" Text="保存" OnClick="BtnSubmit_Click" />&nbsp;
        <input name="Cancel" type="button" class="inputbutton" id="Cancel" value="取消" onclick="Redirect('<%= ViewState["UrlReferrer"].ToString() %>')" />&nbsp;
        <input name="Loadpage" type="button" class="inputbutton" id="loadotherpage" value="导入"
            onclick="loadhtml()" />&nbsp;
        <asp:Button ID="showlabellevel" runat="server" Text="查看标签层次（仅适用于IE）" OnClientClick="slevel()"
            Visible="false" />
        <input type="button" class="inputbutton" value="可视化编辑" onclick="wysiwyg()" />
    </center>
    <script type="text/javascript" id="searchjs">
        function lock(id) {
            var tt = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
            var hh = jQuery(window).height();
            $("#" + id).css("left", (document.body.clientWidth - 500) / 2 + document.body.scrollLeft)
        .css("top", (tt + ((hh - 300) / 2)) + "px")
        .css("display", "block");
            $("#ly").css("display", "block")
        .css("top", tt + "px")
        .css("width", document.body.clientWidth)
        .css("height", document.body.clientHeight)
        .unbind().click(function () { $("#" + id).hide(); $(this).hide(); });
            $(window.frames["ifly"].document).click(function () { if (document.getElementById('ly')) { jQuery('#ly').click(); } });
        }

        //移动窗口部份
        var posX, posY, fdiv;
        document.getElementById("wysiwygTitle").onmousedown = moveFloatingDiv;
        function moveFloatingDiv(e) {
            if (!e) e = window.event;  //如果是IE
            fdiv = this.parentNode;
            posX = e.clientX - parseInt(fdiv.style.left);
            posY = e.clientY - parseInt(fdiv.style.top);
            document.onmousemove = mousemove;
        }

        document.onmouseup = function () {
            document.onmousemove = null;
        }

        function mousemove(ev) {
            if (ev == null) ev = window.event; //如果是IE
            fdiv.style.left = (ev.clientX - posX) + "px";
            fdiv.style.top = (ev.clientY - posY) + "px";
        }

        function wysiwyg() {
            lock("wysiwyg");
        }
        function Gowysiwyg() {
            var fname = $get("<% =LblFileName.ClientID %>");
            var urlpama = $("#urlpama").val();
            urlpama = urlpama.replace(/test\.aspx\?/, '');
            if (fname.innerHTML != "") {
                window.open("Template_Edit_Main.aspx?Suffix=<%=suffix %>&Dir=" + escape(fname.innerHTML) + "&" + urlpama);
            }
        }

        //边侧栏状态记忆，切换实现
        var fpath = '<% =StyleSheetPath %>';
        function setCookie(name, value, expires, path, domain, secure) {
            var today = new Date();
            today.setTime(today.getTime());
            if (expires) {
                expires = expires * 1000 * 60 * 60 * 24;
            }
            var expires_date = new Date(today.getTime() + (expires)); document.cookie = name + '=' + escape(value) + ((expires) ? ';expires=' + expires_date.toGMTString() : '') + ((path) ? ';path=' + path : '') + ((domain) ? ';domain=' + domain : '') + ((secure) ? ';secure' : '');
        }
        var testvalue = "";
        function getCookie(name) {
            if (document.cookie.length > 0) {
                cookieStart = document.cookie.indexOf(name + "=")
                if (cookieStart != -1) {
                    cookieStart = cookieStart + name.length + 1
                    cookieEnd = document.cookie.indexOf(";", cookieStart)
                    if (cookieEnd == -1) cookieEnd = document.cookie.length
                    return unescape(document.cookie.substring(cookieStart, cookieEnd))
                }
            }
            return ""
        }

        //弹出窗口函数
        function ShowWindow(url, title) {
            jQuery('.title').html(title);
            jQuery('#<%=FdvShowWindow.ClientID %>' + '__HdnLoadUrl').attr('value', url);
            jQuery('#<%=FdvShowWindow.ClientID %>' + '__LitID').click();
        }
        function Hashtable() {
            this._hash = new Object();
            this.add = function (key, value) {
                if (typeof (key) != "undefined") {
                    if (this.contains(key) == false) {
                        this._hash[key] = typeof (value) == "undefined" ? null : value;
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            }
            this.remove = function (key) { delete this._hash[key]; }
            this.count = function () { var i = 0; for (var k in this._hash) { i++; } return i; }
            this.items = function (key) { return this._hash[key]; }
            this.contains = function (key) { return typeof (this._hash[key]) != "undefined"; }
            this.clear = function () { for (var k in this._hash) { delete this._hash[k]; } }
            this.tostring = function () { var output = ""; for (var k in this._hash) { if (output) output += "&"; output += k + ":" + this._hash[k]; } return output; }
        }

        var _hashtable = new Hashtable();
        function ToHashtable(string) {
            var _hashdate = string.split("&");
            for (var _kv in _hashdate) {
                kv = _hashdate[_kv].split(":");
                _hashtable.add(kv[0], kv[1]);
            }
        }

        function appendSideBarState(target, status) {
            var sideBarCookie = getCookie("PE_SideBarCookie");
            if (sideBarCookie) {
                ToHashtable(sideBarCookie);
            }
            _hashtable.remove(target, status);
            _hashtable.add(target, status);
            setCookie("PE_SideBarCookie", _hashtable.tostring(), 300, "/", "", false);
        }

        function initSideBarState(objname, target, dir) {
            var obj = $get(objname);
            var sideBarCooke = getCookie("PE_SideBarCookie");
            if (sideBarCooke) {
                _hashtable.clear;
                ToHashtable(sideBarCooke);
                stateValue = _hashtable.items("InsideLabelTemplateStatic" + dir);
                if (stateValue == "none") {
                    obj.alt = "打开";
                    if (dir == "right")
                        obj.src = fpath + "Images/butd.gif";
                    else
                        obj.src = fpath + "Images/butc.gif";
                    $get(target).style.display = "none";
                }
                else {
                    obj.alt = "关闭";
                    if (dir == "right")
                        obj.src = fpath + "Images/butc.gif";
                    else
                        obj.src = fpath + "Images/butd.gif";
//                    $get(target).style.display = "block";在FF下有样式的变形
                }
            }
        }

        function switchSysBar(objname, target, dir) {
            var obj = $get(objname);
            if (obj.alt == "关闭") {
                obj.alt = "打开";
                if (dir == "right")
                    obj.src = fpath + "Images/butd.gif";
                else
                    obj.src = fpath + "Images/butc.gif";
                $get(target).style.display = "none";
                appendSideBarState("InsideLabelTemplateStatic" + dir, "none");
            }
            else {
                obj.alt = "关闭";
                if (dir == "right")
                    obj.src = fpath + "Images/butc.gif";
                else
                    obj.src = fpath + "Images/butd.gif";
                $get(target).style.display = "";
                appendSideBarState("InsideLabelTemplateStatic" + dir, "block");
            }
        }

        initSideBarState("sp", "ft", "left");

        function sizeChange(size) {
            var obj = $get("<% = TxtTemplate.ClientID %>");
            var height = parseInt(obj.offsetHeight);
            if (height + size >= 100) {
                obj.style.height = height + size + 'px';
            }
        }
        function slevel() {
            var fname = $get("<% =LblFileName.ClientID %>");
            if (fname.innerHTML != "") {
                parent.jQuery("#newFrameTab").click();
                var d = jQuery(parent.document);
                d.find("#main_right").attr("src", "<%=BasePath%><%= PowerEasy.Foundation.SiteConfig.SiteOption.ManageDir %>/Template/LabelLevelAnalay.aspx?Suffix=<%=suffix %>&name=" + escape(fname.innerHTML));
            }
        }
        function loadhtml() {
            var ret = prompt("请输入导入地址", "http://");
            if (ret != null) {
                if (ret.replace(/^\s+|\s+$/g, "") == "") {
                    alert("不能输入空值");
                }
                else if (ret == "http://") {
                    alert("请输入完整的网址");
                }
                else {
                    var alertspan;
                    alertspan = document.createElement('div');
                    alertspan.style.position = "absolute";
                    alertspan.className = "alertspandiv";

                    var e = $get("<% =TxtTemplate.ClientID %>");
                    alertspan.style.width = 100;
                    alertspan.style.height = 30;
                    var t = e.offsetTop;
                    var l = e.offsetLeft;
                    while (e = e.offsetParent) {
                        t += e.offsetTop;
                        l += e.offsetLeft;
                    }
                    alertspan.style.left = l;
                    alertspan.style.top = t;
                    alertspan.innerHTML = "读取中...";
                    document.body.appendChild(alertspan);
                    jQuery.post('<%= BasePath %>' + 'ajax.aspx?HandlerName=AdminAjaxHandler', "<?xml version='1.0' encoding='utf-8'?><root><type>GetUrlPageSource</type><url>" + ret + "</url></root>", function (s) {
                        document.body.removeChild(alertspan);
                        var status = $("status", s).text();
                        if (status == "yes") {
                            $get("<% =TxtTemplate.ClientID %>").value = $("PageSource", s).text();
                        }
                    });
                }
            }
        }

        if (document.all) {
            var textRange = document.getElementById('<% = TxtTemplate.ClientID %>').createTextRange();
            var isSelect = false;
            var inputWord = "";
        }

        var isff = document.getElementById && !document.all;
        if (!isff) {
            var textRange = document.getElementById("<% = TxtTemplate.ClientID %>").createTextRange();
            var isSelect = false;
            var inputWord = "";
        }

        function findText() {
            if (!isff) {
                if (inputWord != document.getElementById('cbox').value) {
                    isSelect = false;
                }
                inputWord = document.getElementById('cbox').value;
                if (inputWord == "") {
                    alert("请输入要搜索的内容");
                    return;
                }
                var selectedWord = 0;
                if (document.selection) {
                    selectedWord = document.selection.createRange().text.length;
                }
                textRange.moveEnd("character", document.getElementById('<% = TxtTemplate.ClientID %>').value.length);
                textRange.moveStart("character", selectedWord);
                if (textRange.findText(inputWord)) {
                    textRange.select();
                    isSelect = true;
                }
                if (textRange.text != inputWord) {
                    if (isSelect) {
                        alert("已经搜索到内容末端！搜索将从头开始");
                        textRange = document.getElementById('<% = TxtTemplate.ClientID %>').createTextRange()

                    }
                    else {
                        alert("找不到" + inputWord);
                    }
                }
                else { return; }

            }
            else {
                alert("抱歉，搜索功能不能适应您的浏览器！");
            }
        }    
    </script>
</asp:Content>
