/*   
 *
 *   FileName:  OperateFile.js
 *   Author: JimmyXiong
 *   E-mail: w3cgod@gmail.com
 *   Version:  1.0_Beta_b
 *
 *   OperateFile类为页面提供基本页面操作功能，并自动生成添加记录，支持记录改名.
 *   添加的节点目前支持 input, textarea, tr, file.
 *   读写与保存功能接口已经做好.
 *   CSS部分有待完善.
 *
 *   使用说明：
 *   在每个需要添加节点的母位置添加一个INPUT标签，格式如下：
 *       <input type="checkbox" onChange="OperateFile.setArea(this, '__ID__')" />
 *       __ID__ 为母节点的id.
 *
 *   创建一个ID为"workList"的节点(为节点添加记录的容器).
 *   创建一行为按钮以添加节点，所执行的函数格式如下：
 *      OperateFile.addNode(sNodeMeta, sInt, trId, h_name)
 *      创建一个input输入框
 *      OperateFile.addNode("input");
 *      创建一个tr节点包含有10个td节点；
 *      OperateFile.addNode("tr", "10");
 *      创建一个有标题的tr节点办法;
 *      OperateFile.addNode("thr","5","5","标题");
 *      创建一个有标题的table方法：
 *      OperateFile.addTable(parentNodeID,selfId)
 *          HTML部分为
 *          <table><tbody id="xxx"></tbody></table>
 *
*/

function goOld(id)
{ 
    document.getElementById(id).onclick();
} 
function go(id)
{
    OperateFile.sNode = document.getElementById(id).rel
}

function setArea(self, oElement)
{
    OperateFile.setArea(self, oElement);
}

function addNode(sNodeMeta, sInt, trId)
{
    OperateFile.addNode(sNodeMeta, sInt,trId); 
}
function addTable(srcId, selfId)
{
    OperateFile.addTable(srcId, selfId)
} 
function DeleteNode(tableId, rowId)
{
    OperateFile.delRowNode(tableId, rowId); 
}
function delTable(srcId, selfName, selfId)
{
    OperateFile.delNode(srcId, selfName, selfId)
}
    
var OperateFile =
{
    //待操作节点ID
    sNode: "",

    //插入节点计数
    sAddNum:1,

    //读文件
    //  filename:   需要读取文件的绝对路径
    readFile: function(filename)
    {
        var oFso, sFile, sResult;
        oFso = new ActiveXObject("Scripting.FileSystemObject");
        sFile = oFso.OpenTextFile(filename,1);
        sResult = "";

        while (!sFile.AtEndOfStream)
        {
            s += sFile.ReadLine() + "\n";
        }

        sFile.Close();
        return sResult;
    },

    //写文件
    //  filename:   要写的文件的绝对路径
    //  sSource:    写入文件的数据
    writeFile: function(filename, sSource)
    {
        var oFso, sFile;
        oFso = new ActiveXObject("Scripting.FileSystemObject");
        sFile = oFso.OpenTextFile(filename,8,true);
        sFile.WriteLine(sSource);
        sFile.Close();
    },

    //设置待操作节点ID
    //  oElement:   待操作节点ID
    setArea: function(self, oElement)
    {
        this.sNode = (self.checked) ? oElement : "" ;
    },

    //插入节点
    //  sNodeMeta:  插入节点的TagName  目前支持 input, textarea, tr
    //  sInt:   例如 tr 里面包含x个同级td标签则 sInt = x    
    addNode: function(sNodeMeta, sInt, trId)
    {
        if(this.sNode !=="")
        {
            var oElement = this.sNode;
        }
        else
        {
            alert("请选择插入位置");
            return;
        }
        if(!sInt && sInt != "thr")
        {
            var idNum = this.sAddNum;
            var oNodeMeta = document.createElement("div"); 
            var oNodeSpan = document.createElement("span");
            var oNodeInput = document.createElement("input");
            var oNodeArea = document.createElement("textarea");
            var sList = document.createElement("div");

            switch (sNodeMeta)
            {
                case "input":
                    oNodeMeta.id = "addId" + this.sAddNum;
                    oNodeSpan.className = "outer_div";
                    oNodeSpan.rel = "input";
                    oNodeInput.value = "自定义内容" + this.sAddNum;
                    oNodeInput.id = "add" + this.sAddNum;
                    oNodeInput.className = "no_border";
                    oNodeInput.onkeyup = function(){window.checkLength(this);};
                    oNodeInput.onpaste = function(){window.checkLength(this);};
                    oNodeInput.onfocus = function(){window.GetFocus(this);};
                    oNodeInput.size = 4;
                    oNodeSpan.appendChild(oNodeInput);
                    oNodeMeta.appendChild(oNodeSpan);
                    break;
                case "file":
                    oNodeMeta.id = "addId" + this.sAddNum;
                    oNodeSpan.className = "outer_div";
                    oNodeSpan.rel = "file";
                    oNodeInput.value = "自定义内容" + this.sAddNum;
                    oNodeInput.id = "add" + this.sAddNum;
                    oNodeInput.className = "file";
                    oNodeInput.size = 15;
                    oNodeInput.type = "file";
                    oNodeSpan.appendChild(oNodeInput);
                    oNodeMeta.appendChild(oNodeSpan);
                    break;
                case "textarea":
                    oNodeMeta.id = "addId" + this.sAddNum;
                    oNodeSpan.className = "outer_div";
                    oNodeSpan.rel = "input";
                    oNodeArea.id = "add" + this.sAddNum;
                    oNodeArea.className = "autoTextarea";
                    oNodeArea.onkeyup = function(){window.checkLength(this, event);};
                    oNodeArea.onpaste = function(){window.checkLength(this, event);};
                    oNodeSpan.appendChild(oNodeArea);
                    oNodeMeta.appendChild(oNodeSpan);
                    break;
                default:
                    alert("插入节点不符合规范");
                    break;
            }
            addList(idNum);
            document.getElementById(oElement).appendChild(oNodeMeta);
            document.getElementById("workList").appendChild(sList);

            this.sAddNum += 1;
            this.Install();
        }
        else
        {
            if(sNodeMeta == "tr")
            {
                var oTr = document.createElement("tr");
                //oTr.id = "addId" + this.sAddNum;
                oTr.id = "addId" + trId;
                for(var i=sInt; i>0; i--)
                {
                    var oTd = document.createElement("td");
                    var oTdInput = document.createElement("input");
                    //oTdInput.id = "add" + this.sAddNum + "_" + i;
                    oTdInput.id = "add" + trId + "_" + i;
                    oTdInput.onfocus = function(){window.GetFocus(this);};
                    oTdInput.onpaste = function(){window.checkLength(this);};
                    oTdInput.onkeyup = function(){window.checkLength(this);};
                    oTdInput.size = "2";
                    oTdInput.className = "table_input";
                    oTd.appendChild(oTdInput);
                    oTr.appendChild(oTd);
                }
                document.getElementById(oElement).appendChild(oTr);
                document.getElementById("workList").appendChild(sList);
                //this.sAddNum += 1;
            }
            else if(sNodeMeta == "th")
            {
                var oThr = document.createElement("tr")
                var oThead = document.createElement("thead")
                var oThead_th = document.createElement("th")
                oThead_th.id = "add" + trId;
                oThead_th.appendChild(document.createTextNode(sInt));
                oThead_th.colSpan = "5"; 
                oThr.appendChild(oThead_th)
                oThead.appendChild(oThr);
                document.getElementById(oElement).parentNode.appendChild(oThead)
            }
            else if(sNodeMeta == "thr")
            {
                var oTr = document.createElement("tr")
                oTr.id = "addId" + trId;
                for(var i=sInt; i>0; i--)
                {
                    var oTd = document.createElement("td");
                    var oTdInput = document.createElement("span");
                    //oTdInput.id = "add" + this.sAddNum + "_" + i;
                    oTdInput.id = "add" + trId + "_" + i;
                    oTdInput.className = "thr"
                    oTd.appendChild(oTdInput);
                    oTr.appendChild(oTd);
                }
                document.getElementById(oElement).appendChild(oTr);
                if(oElement == "TBTable10")
                {
                    document.getElementById("add" + trId +"_3").style.fontFamily = "Times New Roman";
                    document.getElementById("add" + trId +"_3").parentNode.style.width = "60px";
                    document.getElementById("add" + trId +"_2").parentNode.style.width = "150px";
                    document.getElementById("add" + trId +"_2").parentNode.style.height = "100%";
                }
                else if(oElement == "MethodLow214" || oElement == "MethodLow22")
                {
                    document.getElementById("add" + trId +"_2").parentNode.style.width = "170px";
                }
                else if(oElement == "MethodComposite2241" || oElement == "MethodComposite2242" || 
                oElement == "MethodComposite2244")
                {
                    document.getElementById("add" + trId +"_2").parentNode.style.width = "170px";
                }
                else if(oElement == "MethodComposite2243")
                {
                    document.getElementById("add"+trId+"_2").parentNode.style.width = "150px";
                    document.getElementById("add"+trId+"_1").parentNode.style.borderRight = "0";
                }
                var oStr = document.getElementById(oElement).getElementsByTagName("tr");
                var oAstd = document.getElementById(oElement).getElementsByTagName("td");
                var oStd = oStr[oStr.length - 1].getElementsByTagName("td");
                if(oStr.length == 1 && oStr[0].offsetHeight <20)
                {
                    for(var i=0; i<oAstd.length; i++)
                    {
                        oAstd[i].style.height = "20px";
                        oAstd[i].style.borderBottom = "0";
                    }
                }
                else
                {
                    for(var i=0; i<oAstd.length; i++)
                    {
                        oAstd[i].style.height = "";
                        oAstd[i].style.borderBottom = "1px solid #999";
                    }
                    if(oElement == "MethodComposite2241" || oElement == "MethodComposite2242" || 
                    oElement == "MethodComposite2244" || oElement == "MethodComposite2243" ||
                    oElement == "MethodLow22" || oElement == "MethodLow214" ||
                    oElement == "TBTable10")
                    {
                        for(var i=0; i<oStd.length; i++)
                        {
                            oStd[i].style.borderBottom = "0";
                        }
                    }
                    else
                    {
                        for(var i=0; i<oStd.length; i++)
                        {
                            oStd[i].style.borderBottom = "1px solid #999";
                        }
                    }
                }                    

            }
        }
        
        function addList(idNum)
        {
            var sListTitle = document.createElement("span");
            var sListInput = document.createElement("input");
            var sListClose = document.createElement("a");
            sList.id = "addList" + idNum;
            sList.className = "addList";
            sListTitle.className = "title";
            sListTitle.value = "title";
            sListTitle.innerHTML = "自定义内容" + idNum;
            sListInput.type = "text";
            sListInput.title = "自定义内容" + idNum;
            sListInput.className = "changName";
            sListClose.onclick = function(){OperateFile.delNode(oElement, idNum);};
            sListClose.innerHTML = "Close";
            sList.appendChild(sListTitle);
            sList.appendChild(sListInput);
            sList.appendChild(sListClose);
        }
    },

    //删除已插入节点
    //  此函数一般为自动调用
	//  oElement:   需要删除的节点所在的母节点ID
	//  sAddName：	添加节点时生成的ID的名字
    //  sAddNum:    添加节点时生成的ID的数字
    delNode: function(oElement, sAddName , sAddNum)
    {
        var arg1, arg2 ,target1 ,target2;
        arg1 = sAddName + sAddNum.toString();//JimmyXiong 2009-8-17
        arg2 = "addList" + sAddNum.toString();
        target1 = document.getElementById(arg1);
        document.getElementById(oElement).removeChild(target1);
		try{
			target2 = document.getElementById(arg2);
			document.getElementById("workList").removeChild(target2);
		}catch(err){}
    },
    
    delRowNode: function(tableId,rowId)
    {
        target1 = document.getElementById(rowId);
        document.getElementById(tableId).removeChild(target1);
    },

    //添加一个table节点
    //srcId 为要插入table的母节点id
    //selfId 为新插入table的div id(数字值)
    addTable: function(srcId, selfId)
    {
        var oDiv = document.createElement("div");
        var srcElement = document.getElementById(srcId);
        oDiv.id = selfId;
        oDiv.style.marginBottom = "15px"
        srcElement.appendChild(oDiv);
        document.getElementById(selfId).innerHTML = '<table border="2" cellspacing="0" width="750"><tbody id="tbody'+ selfId 
        + '"><tr valign="top"><td width="45"><font face="宋体" size="2">序号</font></td>'
        + '<td width="100"><font face="宋体" size="2">文件名</font></td>'
        + '<td width="100"><font face="宋体" size="2">文件类型</font></td>'
        + '<td width="80"><font face="宋体" size="2">文件大小</font></td>'
        + '<td width="230"><font face="宋体" size="2">备注</font></td>';
        var checkBox = "<span style=\"display:none;\"><input rel=\"tbody" + selfId + "\" type=\"checkbox\" checked=\"true\" id=\"set"+ selfId +"\" /></span>"
        var innerContent = document.getElementById("addCheck").innerHTML;
        document.getElementById("addCheck").innerHTML = innerContent + checkBox;
    },

    //事件监听
    //  oElement:   需要添加事件监听对象的ClassName
    //  type:   需要添加的事件监听类型
    //  func:   监听时间后的回调函数
    listenDef: function(oElement, type, func)
    {
        var iMark = 0;
        if(oElement === "addList" || iMark !== 0)
        {
            var oWorkList = document.getElementById("workList");

            //兼容IE-------------
            oWorkList.getElementsByClassName = function(eleClassName)
            {
                var getEleClass = [];
                var myclass = new RegExp("\\b"+eleClassName+"\\b");
                var elem = this.getElementsByTagName("*");
                for(var h=0;h<elem.length;h++)
                {
                    var classes = elem[h].className;
                    if (myclass.test(classes)){getEleClass.push(elem[h]);};
                }
                return getEleClass;
             }
          //---------------------

            var oElement = oWorkList.getElementsByClassName("addList");
            iMark = 1;
        }
        else
        {
            var oElement = document.getElementsByClassName(oElement);
        }

        var iL = oElement.length - 1;
        for(var i=iL; i>=0; i--)
        {
            if(oElement[i].addEventListener)
            {
                oElement[i].addEventListener(type, func, false);
            }
            else if(oElement[i].attachEvent)
            {
                oElement[i].attachEvent("on" + type, func);
            }
            else
            {
                oElement[i]["on" + type] = func;
            }
        }
    },

    //改变节点标题
    //  此函数由事件监听函数执行
    //  oEvt: 事件监听函数传过来的源节点参数
    changeText: function(oEvt)
    {
        var oElement = oEvt.srcElement || oEvt.target;
        var oDiv = oElement.parentNode;

        //兼容IE-------------
            oElement.getElementsByClassName = function(eleClassName)
            {
                var getEleClass = [];
                var myclass = new RegExp("\\b"+eleClassName+"\\b");
                var elem = this.getElementsByTagName("*");
                for(var h=0;h<elem.length;h++)
                {
                    var classes = elem[h].className;
                    if (myclass.test(classes)){getEleClass.push(elem[h]);};
                }
                return getEleClass;
            }
        //--------------------

        var oElementText = oDiv.getElementsByTagName("span")[0].innerHTML;
        var oElementInput = oDiv.getElementsByTagName("input")[0].value;

        if(oElementInput === "")
        {
            oDiv.getElementsByTagName("input")[0].value = oElementText; 
        }
        else
        {
            oDiv.getElementsByTagName("span")[0].innerHTML = oElementInput;
        }
    },

    //读取图片
    //  sUrl:   图片的路径和名字
    //  oElement:   显示图片的目标节点ID
    loadPic: function(sUrl, oElement)
    {
        document.getElementById(oElement).innerHTML = "<img src=" + sUrl + " />"
    },

    //内部调用语句
    //  一些类内部函数之间的调用执行
    Install: function()
    {
        OperateFile.listenDef("addList", "dblclick", OperateFile.changeText);
        OperateFile.listenDef("changName", "blur", OperateFile.changeText);
    }
}

//IE中getElementsByClassName实现
document.getElementsByClassName = function(eleClassName)
{
    var getEleClass = [];
    var myclass = new RegExp("\\b"+eleClassName+"\\b");
    var elem = this.getElementsByTagName("*");
    for(var h=0;h<elem.length;h++)
    {
        var classes = elem[h].className;
        if (myclass.test(classes)){getEleClass.push(elem[h]);};
    }
    return getEleClass;
}
