﻿String.prototype.format = function() {
    var s = this,
        i = arguments.length;

    while (i--) {
        s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
    }
    return s;
};
String.prototype.endsWith = function(suffix) {
    return (this.substr(this.length - suffix.length) === suffix);
};
String.prototype.startsWith = function(prefix) {
    return (this.substr(0, prefix.length) === prefix);
};


function confirmDelete(item, url) {
    var msg = "确实要删除 " + item + " ?";
    if (confirm(msg)) {
        window.location = url;
    }
}

function changeTopDep(sel) {
    window.location = "/admin/department/?top=" + sel.value;
}

function changeDep(sel, strFormat) {
//    if (controllerName.toString().toLowerCase() == "review")
//        window.location = "/admin/review/Index/" + sel.value;
//    else
//        window.location = "/admin/dep/" + sel.value + "/" + controllerName;
    if (!strFormat || !sel.value)
        return false;
    window.location = strFormat.format(sel.value);
}

function changeNav(sel) {
    var newUrl = $.query.set("navId", sel.value).toString();
    window.location = newUrl;
}



function filterFileType(file, exts) {
    if (!file || !exts) return false;
    var filePath = file.value;
    var ext = filePath.substr(filePath.indexOf('.') + 1).toLowerCase();
    var extArray = exts.split('|');
    if ($.inArray(ext, extArray) < 0) {
        alert("请选择合适的文件上传，支持：" + extArray.join(',') + "等格式");
        file.value = "";
        return false;
    }
}

function addpara(name,value) {
    var newUrl = $.query.set(name, value).toString();
    window.location = newUrl;
}


function changeTopNav(sel) {
    window.location = "/admin/home/HomeNav/?top=" + sel.value;
}

function changeTopMajor(sel) {
    window.location = "/admin/majors/?top=" + sel.value;
}


//sourcelist 源Listbox的ID
//targetlist 目标ListBox的ID
//isAll 是否全部选过去
//isLefttoRight 添加还是删除
function MoveClick(sourcelist, targetlist, isAll, isLefttoRight) {
    var lsource;
    var tsource;
    if (isLefttoRight) {
        lsource = document.getElementById(sourcelist);
        tsource = document.getElementById(targetlist);
    }
    else {
        lsource = document.getElementById(targetlist);
        tsource = document.getElementById(sourcelist);
    }

    var length = lsource.options.length; 
    if (isAll) {
        for (var i = 0; i < length; i++) {
            tsource.appendChild(lsource.options[0]);
        };
    }
    else {
        for (var i = 0; i < lsource.options.length; i++) {
            if (lsource.options[i].selected == true) {
                var oOption = document.createElement("OPTION");
                oOption.text = lsource.options[i].text;
                oOption.value = lsource.options[i].value;
                tsource.options.add(oOption);
                lsource.remove(i);
                i = i - 1;
            }
        }                           

    }
    return false;
}

//获取Listbox的每个item的value,串联起来赋值给Hiddenfield
function getListValue(ListBoxId, HiddenId) {
    var targetList = document.getElementById(ListBoxId);
    var listResult = document.getElementById(HiddenId);
    for (var i = 0; i < targetList.options.length; i++) {
        listResult.value = listResult.value + "," + targetList.options[i].value;
    }
    return true;
}


function ChangeDepartment(sel) {
    if (sel.value > 0) {
        window.location = "/department/index/" + sel.value;
    }
}

function ChangeUsefulSite(sel) {
    if (sel.value !="") {
        window.location = sel.value;
    }
}


function initEditor(textAreaId) {
    if (typeof KE == undefined || !KE) return;
    //KindEditor上传附件功能
    KE.plugin['accessory'] = {
        click: function(id) {
            KE.util.selection(id);
            var dialog = new KE.dialog({
                id: id,
                cmd: 'accessory',
                file: '/accessory/accessory.htm',
                width: 310,
                height: 90,
                title: KE.lang['accessory'],
                yesButton: KE.lang['yes'],
                noButton: KE.lang['no']
            });
            dialog.show();
        },
        check: function(id) {
            var dialogDoc = KE.util.getIframeDoc(KE.g[id].dialog);
            var type = KE.$('type', dialogDoc).value;
            var url = '';
            if (type == 1) {
                url = KE.$('localFile', dialogDoc).value;
            } else {
                url = KE.$('url', dialogDoc).value;
            }
            if (!url) return false;
            if (url.match(/\.(docx|doc|xlsx|xls|pptx|ppt|pdf|txt|rar|swf|zip)$/i) == null) {
                alert(KE.lang['invalidAccessory']);
                window.focus();
                KE.g[id].yesButton.focus();
                return false;
            }
            return true;
        },
        exec: function(id) {
            var self = this;
            KE.util.select(id);
            var dialogDoc = KE.util.getIframeDoc(KE.g[id].dialog);
            var type = KE.$('type', dialogDoc).value;
            var title = KE.$('imgTitle', dialogDoc).value;
            if (!this.check(id)) return false;
            if (type == 1) {
                //KE.$('editorId', dialogDoc).value = id;
                var uploadIframe = KE.$('uploadIframe', dialogDoc);
                KE.util.showLoadingPage(id);
                var onloadFunc = function() {
                    KE.event.remove(uploadIframe, 'load', onloadFunc);
                    KE.util.hideLoadingPage(id);
                    var uploadDoc = KE.util.getIframeDoc(uploadIframe);
                    var data = '';
                    try {
                        data = KE.util.parseJson(uploadDoc.body.innerHTML);
                    } catch (e) {
                        alert(KE.lang.invalidJson);
                    }
                    if (typeof data === 'object' && 'error' in data) {
                        if (data.error === 0) {
                            var url = data.url;
                            var ext = url.substring(url.lastIndexOf(".") + 1);
                            self.insert(id, url, title, ext);
                            //self.insert(id, data.url, title, width, height, 0, align);
                        } else {
                            alert(data.message);
                            return false;
                        }
                    }
                };
                KE.event.add(uploadIframe, 'load', onloadFunc);
                dialogDoc.uploadForm.submit();
                return;
            } else {
                var url = KE.$('url', dialogDoc).value;
                var ext = url.substring(url.lastIndexOf(".") + 1);
                this.insert(id, url, title, ext);
            }
        },
        insert: function(id, url, title, ext) {
            var img
            switch (ext) {
                case "doc":
                case "docx":
                    img = "/editor/attached/ke-doc.gif";
                    break;
                case "xls":
                case "xlsx":
                    img = "/editor/attached/ke-xls.gif";
                    break;
                case "ppt":
                case "pptx":
                    img = "/editor/attached/ke-ppt.gif";
                    break;
                case "rar":
                    img = "/editor/attached/ke-rar.gif";
                    break;
                case "zip":
                    img = "/editor/attached/ke-zip.gif";
                    break;
                case "txt":
                    img = "/editor/attached/ke-txt.gif";
                    break;
                case "pdf":
                    img = "/editor/attached/ke-pdf.gif";
                    break;
                default:
                    img = "/editor/attached/ke-accessory.gif";
                    break;
            }
            var html = '<img src="' + img + '" >&nbsp;';
            html += '<a href="' + url + '" >';
            if (title) html += title
            else html += "文件";
            html += '</a>';
            KE.util.insertHtml(id, html);
            KE.layout.hide(id);
            KE.util.focus(id);
        }
    };

    KE.show({
        id: textAreaId,
        imageUploadJson: '/editor/asp.net/upload_json.ashx',
        fileManagerJson: '/editor/asp.net/file_manager_json.ashx',
        allowFileManager: true
    });
}
