function checkfrm(frm)
{
    if (frm.imgFile.value.length == 0)
	{
		alert("请选择要上传的文件!");
		frm.imgFile.focus();
		return false;
	}
	return true;
};
function addTag(id,txt)
{
	var obj=document.getElementById(id);	
	if (obj.selectionStart !== undefined)
	{
		var startPos = obj.selectionStart;
		var endPos = obj.selectionEnd;
        obj.value = obj.value.substring(0, startPos) + txt + obj.value.substring(endPos, obj.value.length);
		obj.focus();
        obj.selectionStart = startPos + txt.length;
        obj.selectionEnd = startPos + txt.length;
	}
	else
	{
		  if(document.selection) 
		  { 		  
		  	 obj.focus();
             var sel = document.selection.createRange();
             sel.text =  txt;
		  }
		  else
		  {
		  	obj.value=[obj.value,txt].join("");
		  }
	}	
};
function showHtmlTag()
{
	
	var url = "?modelid=" + ModelID + "&act=getjson";
	$.ajax({type:"get",async:false,url:url,dataType:"json",success:function(json)
		{
			if(json.success=="1")
			{
				viewJson=json.viewJson;
				contentJson=json.contentJson;
				siteJson=json.siteJson;
			}
		}
	});
	var html='';
	html+='<div style="padding:4px 0px 4px 10px;border: 0px dashed #ccc;background-color:#f2f2f2;">全局标签：</div>';
	html+='<div style="margin:6px;">';
	$.each(publicJson, function()
	{
		html+='<a href="javascript:;" class="htmltag" onclick="addTag(\'content\',\'{'+ this["type"] +':'+ this["value"] +'(' + this["text"] + ')}\');">' + this["text"] + '</a>';
	});	
	html+='</div>';
	
	html+='<div style="padding:4px 0px 4px 10px;border: 0px dashed #ccc;background-color:#f2f2f2;">站点标签：</div>';
	html+='<div style="margin:6px;">';
	$.each(siteJson, function()
	{
		html+='<a href="javascript:;" class="htmltag" onclick="addTag(\'content\',\'{site:'+ this["value"] +'(' + this["text"] + ')}\');">' + this["text"] + '</a>';
	});	
	html+='</div>';
	
	html+='<div style="padding:4px 0px 4px 10px;border: 0px dashed #ccc;background-color:#f2f2f2;">动态数据：</div>';
	html+='<div style="margin:6px;">';	
	$.each(viewJson, function()
	{
		html+='<a href="javascript:;" class="htmltag" onclick="addTag(\'content\',\'{view:'+ this["value"] +'(' + this["text"] + ')}\');">' + this["text"] + '</a>';
	});		
	html+='</div>';
	html+='<div style="padding:4px 0px 4px 10px;border: 0px dashed #ccc;background-color:#f2f2f2;">静态数据：</div>';
	html+='<div style="margin:6px;">';
	$.each(contentJson, function()
	{
		html+='<a href="javascript:;" class="htmltag" onclick="addTag(\'content\',\'{content:'+ this["value"] +'(' + this["text"] + ')}\');">' + this["text"] + '</a>';
	});	
	html+='</div>';
	
	layer.open({
					type: 1 //此处以iframe举例
					,title: "插入数据"
					//,skin: 'layui-layer-molv' //加上边框 layui-layer-rim  layui-layer-lan layui-layer-molv layer-ext-moon
					,shadeClose: false //点击遮罩关闭层
					, area: ['80%', '80%']
					,shadeClose: true //点击遮罩关闭
					,shade: 0.2 //透明度
					,maxmin: false
					,resize: false
					,btnAlign: 'l' //lcr
					,moveType: 0 //拖拽模式，0或者1
					,anim: 0 //0-6的动画形式，-1不开启
					,content: html
					,btn: ['返回'] //只是为了演示
					,yes: function(index,layero)
					{ 
						layer.close(index); 
					}	
					,cancel: function(index, layero)
					{
						//layer.msg("X关闭!");
					}
					,success: function(layero, index)
					{
					}
				});
};
function addPath(txt)
{
	var obj=document.getElementById("content");	
	if (obj.selectionStart !== undefined)
	{
		var startPos = obj.selectionStart;
		var endPos = obj.selectionEnd;
        obj.value = obj.value.substring(0, startPos) + txt + obj.value.substring(endPos, obj.value.length);
		obj.focus();
        obj.selectionStart = startPos + txt.length;
        obj.selectionEnd = startPos + txt.length;
	}
	else
	{
		  if(document.selection) 
		  { 		  
		  	 obj.focus();
             var sel = document.selection.createRange();
             sel.text =  txt;
		  }
		  else
		  {
		  	obj.value=[obj.value,txt].join("");
		  }
	}	
	parent.layer.close( parent.FileSelectFormIndex);
    parent.FileSelectFormIndex = null;
};
function showPathSelect()
{
	var url = "FileSelect.aspx";
    parent.layer.open({
      type: 2,
      title: "选择库文件",
      maxmin: false,
      shadeClose: true, //点击遮罩关闭层
      area : ["900px;" , "700px"],	
	  // content: [url,'no'], 
	  content: url,
	  success: function(layero, index)
	  {
	      parent.FileSelectFormIndex = index;
  	  }
    });	
};
//文件编辑
function edit(name)
{
	var url = "?" + (typeof (ModelID) == "string" && ModelID.length > 0 ? "ModelID=" + ModelID + "&" : "") + "act=edit" + (path.length>0 ? "&path=" + path : "") + "&name=" + name;
	$.ajax({type:"get",async:false,url:url,dataType:"json",success:function(json)
		{
			if(json.success=="1")
			{
				var html='';
				html+='<textarea id="content" name="content" class="autoNumber" style="width:98%;height:540px;">' + json.value + '</textarea>';
				layer.open({
					type: 1 //此处以iframe举例
					,title: name
					//,skin: 'layui-layer-molv' //加上边框 layui-layer-rim  layui-layer-lan layui-layer-molv layer-ext-moon
					,shadeClose: false //点击遮罩关闭层
					,area: ['90%', '90%']
					,shade: 0.2 //透明度
					,maxmin: false
					,resize: false
					,btnAlign: 'l' //lcr
					,moveType: 0 //拖拽模式，0或者1
					,anim: 0 //0-6的动画形式，-1不开启
					,content: html
					,btn: ['保存修改','关闭'] //只是为了演示
					,yes: function(index,layero)
					{ 
						var value=$("#content").val().replace(/\r/ig,"").replace(/\n/ig,"0x\\r\\n");						
						url =  "?" + (typeof (ModelID) == "string" && ModelID.length > 0 ? "ModelID=" + ModelID + "&" : "") + "act=save" + (path.length>0 ? "&path=" + path : "") + "&name=" + name;						
						$.ajax({
							type: "post", 
							async: true,
							data:{value:value},
							url: url,
							dataType: "json",
							success: function (data) {
								layer.msg(data.message);
								//layer.close(index); 不关闭、继续修改
							},
							error: function (XMLHttpRequest, textStatus, errorThrown) {				
							}
						});
					}	
					,cancel: function(index, layero)
					{
						//layer.msg("X关闭!");
					}
					,success: function(layero, index)
					{
						//alert($('#layui-layer'+ index).height());
						var h=$('#layui-layer'+ index).height()-110;
						var w=$('#layui-layer'+ index).width()-6;
						$("#content").height(h).width(w);
						$("#content").setTextareaCount({width: "40px",bgColor: "#f2f2f2",color: "#000",display: "inline-block"});
					}
				});
			}
		}
	});		
};
//模板编辑
function editFile(name)
{
	var url = "?" + (typeof (ModelID) == "string" && ModelID.length > 0 ? "ModelID=" + ModelID + "&" : "") + "act=edit" + (path.length>0 ? "&path=" + path : "") + "&name=" + name;
	$.ajax({type:"get",async:false,url:url,dataType:"json",success:function(json)
		{
			if(json.success=="1")
			{
				var html='';
				if(name.toLowerCase().indexOf(".html")>-1)
				{
					html+='<div style="padding:8px;background-color:#f2f2f2;"><a href="javascript:;" onclick="showHtmlTag();" style="color:#0026ff;">插入数据</a>&nbsp;&nbsp;<a href="javascript:;" onclick="showPathSelect();" style="color:#0026ff;">插入库文件</a></div>';
				}
				html+='<textarea id="content" name="content" class="autoNumber" style="width:98%;height:540px;">' + json.value + '</textarea>';
				layer.open({
					type: 1 //此处以iframe举例
					,title: name
					//,skin: 'layui-layer-molv' //加上边框 layui-layer-rim  layui-layer-lan layui-layer-molv layer-ext-moon
					,shadeClose: false //点击遮罩关闭层
					, area: ['90%', '90%']
					,shade: 0.2 //透明度
					,maxmin: false
					,resize: false
					,btnAlign: 'l' //lcr
					,moveType: 0 //拖拽模式，0或者1
					,anim: 0 //0-6的动画形式，-1不开启
					,content: html
					,btn: ['保存修改','关闭'] //只是为了演示
					,yes: function(index,layero)
					{ 

						var value=$("#content").val().replace(/\r/ig,"").replace(/\n/ig,"0x\\r\\n");						
						url = "?" + (typeof (ModelID) == "string" && ModelID.length > 0 ? "ModelID=" + ModelID + "&" : "") + "act=save" + (path.length>0 ? "&path=" + path : "") + "&name=" + name;						
						$.ajax({
							type: "post", 
							async: true,
							data:{value:value},
							url: url,
							dataType: "json",
							success: function (data) {
								layer.msg(data.message);
								//layer.close(index); 不关闭、继续修改
							},
							error: function (XMLHttpRequest, textStatus, errorThrown) {				
							}
						});
					}	
					,cancel: function(index, layero)
					{
						//layer.msg("X关闭!");
					}
					,success: function(layero, index)
					{
						//alert($('#layui-layer'+ index).height());
						var h=$('#layui-layer'+ index).height()-140;
						var w=$('#layui-layer'+ index).width()-6;
						$("#content").height(h).width(w);
						$("#content").setTextareaCount({width: "40px",bgColor: "#f2f2f2",color: "#000",display: "inline-block"});
						
					}
				});
			}
		}
	});	
};﻿
//文件上传
function fileManage_upload(obj) {
    var url = "?" + (typeof (ModelID) == "string" && ModelID.length > 0 ? "ModelID=" + ModelID + "&" : "") + "act=upload" + (path.length > 0 ? "&path=" + path : "");
    if (obj.files) {
        var formData = new FormData();
        for (var i = 0; i < obj.files.length; i++) {
            formData.append('files', obj.files[i], obj.files[i].name);
        }
        $(obj).hide();
        $.ajax({
            type: "POST",
            async: true,
            data: formData,
            url: url,
            contentType: false,
            //dataType: "formData",
            cache: false,//上传文件无需缓存
            processData: false,//用于对data参数进行序列化处理 这里必须false
            dataType: "html",
            success: function (data) {
                // alert(JSON.stringify(data));
                //$(".filemanage_files .clear").before(data);
                $("#filemanage_files").children().last().before(data);
                obj.value = "";
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        });
        $(obj).show();



    }
    else {       
        $.ajaxFileUpload({
            type: "post",
            url: url, //用于文件上传的服务器端请求地址 
            secureuri: false, //一般设置为false 
            fileElementId: $(obj).attr("id"), //文件上传空间的id属性  <input type="file" id="file" name="file" /> 
            dataType: "html", //返回值类型 一般设置为json 		
            complete:function(){},
            success: function (data, status)  //服务器成功响应处理函数
            {
                //$(".filemanage_files .clear").before(data);
                $("#filemanage_files").children().last().before(data);
            },
            error: function (data, status, e)//服务器响应失败处理函数
            {
            }
        });
    }
};

function addFile()
{
	layer.prompt({ title: '输入文件名称', formType: 3, value: '' }, function (value, index) {
		var url ="?" + (typeof(ModelID)=="string" && ModelID.length > 0 ? "ModelID=" + ModelID + "&" : "" ) + "act=addfile" + (path.length>0 ? "&path=" + path : "") + "&name=" + value;
        layer.close(index);
        document.location.href = url;
    });
};
function addFolder()
{
	layer.prompt({ title: '输入文件夹名称', formType: 3, value: name }, function (value, index) {
		var url = "?" + (typeof(ModelID)=="string" && ModelID.length > 0 ? "ModelID=" + ModelID + "&" : "" ) + "act=addfolder" + (path.length>0 ? "&path=" + path : "") + "&name=" + value;
        layer.close(index);
        document.location.href = url;
    });
};
function ReName(name,type)
{
	layer.prompt({ title: '输入文件' + (type == 1 ? '夹' : '') + '名称', formType: 3, value: name }, function (value, index) {
    	var url = "?" + (typeof(ModelID)=="string" && ModelID.length > 0 ? "ModelID=" + ModelID + "&" : "" ) + "act=rename" + (path.length>0 ? "&path=" + path : "") + "&type=" + type + "&oldname=" + name + "&newname=" + value;
        layer.close(index);
        document.location.href = url;
    });
};