// JScript 文件
var pathArray = new Array(); //定义存放要上传文件的数组

var fileType = "";  //存储文件类型
function CheckForm() {
    alert(document.getElementsByName("myFile").length);
    alert(document.getElementById("myFile").value);
}
function opFile() {
    document.uploderform.myFile.click();
}

//为数组添加remove方法
Array.prototype.remove = function () {
    var newArr = new Array();
    var a = arguments[0];
    for (i = 0; i < this.length; i++) {
        if (this[i] != a) {
            newArr.push(this[i]);
        }
    }
    return newArr;
}



//为数组添加判断是否有重复值方法
Array.prototype.hsRepeat = function (n) {
    if (n < this.length) {
        for (var i = n + 1; i < this.length; i++)
            if (this[n] == this[i])
                return true;
        if (i == this.length)
            return this.hsRepeat(++n);
    }
    else
        return false;
}

//添加上传文件
function AddFile(filePath) {
    if (!checkFileType(filePath)) {
        alert("请添加合法的图片格式(如:.jpg;.gif;.png;.bmp)");
        return false;
    }

    pathArray.push(filePath);
    if (pathArray.hsRepeat(0)) {
        pathArray.pop(filePath);
        //alert("抱歉,该文件已存在,请不要重复添加");
        return false;
    }
    var AddFiles = document.getElementById('AddFiles');
    AddFiles.innerHTML += "<li  id=\"filerow" + objID + "\"><span class=\"tle_withe_closs\"><a href=\"#\"><img src=\"../CSS/Admin/images/dell.jpg\" onclick='DeleteRow(\"" + objID + "\")' alt=\"删除\"  />" +
    "</a></span><span class=\"tle_withe\">" + filePath + "</span><span class=\"tle_witheouit\"></span></li>";


}

function DeleteRow(obj) {
    var Row = document.getElementById("filerow" + obj);
    Row.parentNode.removeChild(Row);   //删除
    document.getElementById("fileDiv").removeChild(document.getElementById("file" + obj));
}

//创建FIle对象
var objID = 1;
function createFile(obj) {
    objID++;
    if (AddFile(obj.value) == false) {
        return false;
    }
    var newTdType = document.createElement("TD");
    newTdType.innerHTML += "<img src='" + obj.value + "'/>";
    var parent = obj.parentElement;
    var fileHtml = '<input name="file' + objID + '" class="files" onChange="createFile(this)" type="file" id="file' + objID + '" size="1">';
    parent.insertAdjacentHTML("beforeEnd", fileHtml);
}

//检查文件格式
function checkFileType(filePath) {
    var fileTypes = filePath.substring(filePath.length - 3, filePath.length);
    switch (fileTypes.toLowerCase()) {
        case "gif": return true;
        case "jpg": return true;
        case "png": return true;
        case "bmp": return true;
        default: return false;
    }

}


