﻿function GetTeacherRecommendBySelect(cid, targetControl) {
    cid = "#" + cid;
    var selectedValue = $(cid).find("option:selected").text();
    $.ajax({
        type: "get",
        dataType: "html",
        data: "args=" + selectedValue + "&flag=teacherRecommend",
        url: "/CMSFile/controls/AjaxHandle.aspx",
        error: function() {
            //alert('Error loading JSON document');
        },
        success: function(d) {
            $("#" + targetControl).html(d);
        }
    });
}

//咨询问答右侧,点击部门分类
function ConsultConsultTypeClick(did) {
    $("#hf_consultTypeId").val(did);
    $.ajax({
        type: "get",
        dataType: "html",
        data: "args=" + did + "&flag=consultTypeClick",
        url: "/CMSFile/controls/AjaxHandle.aspx",
        error: function() {
            //alert('Error loading JSON document');
        },
        success: function(d) {
            $("#divQuestionList").html(d);
        }
    });
}
//首页
function consultFirstPage() {
    var pindex = getPageIndex();
    var psize = getPageSize();
    var pageCount = getPageCount();
    if (pindex == 1) {
        return;
    }
    ConsultDepartmentPageClick(1);
}
//上页
function consultPrevPage() {
    var pindex = getPageIndex();
    var psize = getPageSize();
    var pageCount = getPageCount();
    if (pindex == 1) {
        return;
    }
    ConsultDepartmentPageClick(pindex - 1);
}
//下页
function consultNextPage() {
    var pindex = getPageIndex();
    var psize = getPageSize();
    var pageCount = getPageCount();
    if (pindex >= pageCount) {
        return;
    }
    ConsultDepartmentPageClick(pindex + 1);
}
//尾页
function consultLastPage() {
    var pindex = getPageIndex();
    var psize = getPageSize();
    var pageCount = getPageCount();
    if (pindex >= pageCount) {
        return;
    }
    ConsultDepartmentPageClick(pageCount);
}
function ConsultDepartmentPageClick(pindex) {
    var did=$("#hf_departmentId").val();
    $.ajax({
        type: "get",
        dataType: "html",
        data: "args=" + did + "&pageIndex=" + pindex + "&flag=consultTypeClick",
        url: "/CMSFile/controls/AjaxHandle.aspx",
        error: function() {
            //alert('Error loading JSON document');
        },
        success: function(d) {
            $("#divQuestionList").html(d);
            $("#spanPageIndex").html(pindex);
        }
    });    
}
//设置当前页码
function setPageIndex(args) {
    return parseInt($("#spanPageIndex").html(args));
}
//获得当前页码
function getPageIndex() {
    return parseInt($("#spanPageIndex").html());
}
//获得每页数据行数
function getPageSize() {
    return parseInt($("#spanPageSize").html());
}
//获得总页数
function getPageCount() {
    return parseInt($("#spanAllPage").html());
}
//咨询问答搜索
function consultSearch() {   
    var keywords = $.trim($("#txtConsultKeywords").val());
    if (keywords == "" || keywords == "根据标题模糊搜索") {
        alert("请填写搜索关键字!");
        return;
    }
    $.ajax({
        type: "get",
        dataType: "html",
        data: "args=" + keywords + "&flag=consultSearch",
        url: "/CMSFile/controls/AjaxHandle.aspx",
        error: function() {
            //alert('Error loading JSON document');
        },
        success: function(d) {
            $("#divQuestionList").html(d);
            $("#txtConsultKeywords").val(keywords);
            $("#divPageBar").html("");
        }
    });
}


//提交投票
function submitVote() {
    var selectedIds="";
    var options = $("input[name='voteOption']");
    options.each(
        function() {
            if ($(this).attr("checked") == true) {
                var optionId = $(this).prev().val();
                if (selectedIds == "") {
                    selectedIds = optionId;
                }
                else {
                    selectedIds += "," + optionId;
                }
            }
        });
    if (selectedIds == "") {
        return;
    }
    $.ajax({
        type: "get",
        dataType: "html",
        data: "args=" + selectedIds + "&flag=consultSubmit",
        url: "/CMSFile/controls/AjaxHandle.aspx",
        error: function() {
            //alert('Error loading JSON document');
        },
        success: function(d) {
            if (d == "reBallot") {
                alert("您已经投过票了!");
            }
            else if (d == "cancel") {
                return;
            }
            else {
                $("#divBody").html(d);
                alert("投票成功!");
            }
        }
    });
}

//站内搜索
function siteSearch() {
    var keyword = $("#siteSearchKeyword").val();
    if (keyword == "请输入关键字" || keyword=="" || keyword.length==0) {
        alert("请输入关键字!");
        return;
    }
    location.href = "Default.aspx?searchkey=" + keyword;
}

//网站地图
function gotoSiteMap() {
    location.href = "Default.aspx?msg=sitemap";
}

//加入收藏夹
function addFavorite() {
    var sURL = location.href;
    var sTitle=document.title;
    try {
        window.external.addFavorite(sURL, sTitle);
    }
    catch (e) {
        try {
            window.sidebar.addPanel(sTitle, sURL, "");
        }
        catch (e) {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}
//设为首页
function setHome(obj, vrl) {
    try {
        obj.style.behavior = 'url(#default#homepage)'; obj.setHomePage(vrl);
    }
    catch (e) {
        if (window.netscape) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e) {
                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
            }            
        }
    }
}

/********************************************************
*  除去咨询列表页外其他列表页分页
********************************************************/

//首页
function GotoFirstPage(containerId) {
    var pindex = getPageIndex();
    var psize = getPageSize();
    var pageCount = getPageCount();
    if (pindex == 1) {
        return;
    }
    GotoPage(1, containerId);
}
//上页
function GotoPrevPage(containerId) {
    var pindex = getPageIndex();
    var psize = getPageSize();
    var pageCount = getPageCount();
    if (pindex == 1) {
        return;
    }
    GotoPage(pindex - 1, containerId);
}
//下页
function GotoNextPage(containerId) {
    var pindex = getPageIndex();
    var psize = getPageSize();
    var pageCount = getPageCount();
    if (pindex >= pageCount) {
        return;
    }

    GotoPage(pindex + 1, containerId);
}
//尾页
function GotoLastPage(containerId) {
    var pindex = getPageIndex();
    var psize = getPageSize();
    var pageCount = getPageCount();
    if (pindex >= pageCount) {
        return;
    }
    GotoPage(pageCount, containerId);
}

function GotoPage(pindex, containerId) {
    var classId = $("#hd_classId").val();
    $.ajax({
        type: "get",
        dataType: "html",
        data: "args=" + classId + "&pageIndex=" + pindex + "&flag=pageing",
        url: "/CMSFile/controls/AjaxHandle.aspx",
        error: function() {
            //alert('Error loading JSON document');
        },
        success: function(d) {
            $("#" + containerId).html(d);
            $("#spanPageIndex").html(pindex);
        }
    });
}

//下载文件
function downloadFile(classId,contentId)
{
    $('#frameForDownload').remove();
	var iframe = document.createElement("iframe");
	iframe.id = "frameForDownload";
	iframe.src = "CMSFile/controls/download.aspx?cid=" + classId + "&id="+contentId;
	iframe.style.display = "none";
	//创建完成之后，添加到body中
	document.body.appendChild(iframe);
}

function login() {
    var loginName = $("#txtLoginname").val();
    var password = $("#txtLoginPass").val();
    $.ajax({
        type: "get",
        dataType: "html",
        data: "args=" + loginName + "," + password + "&flag=login",
        url: "/CMSFile/controls/AjaxHandle.aspx",
        error: function() {
            //alert('Error loading JSON document');
        },
        success: function(d) {
            if (d == "success") {
                $("#downArrows").html("<a href='/Admin/Default.aspx' style='color:#929393;text-decoration:none;'>后台管理</a>&nbsp;<a onclick='logout();' style='cursor: pointer;'>退出</a>");
                $("li#downArrows").css('background', function() { return '#EEE';});
                $("li#downArrows").css('color', function() { return '#298FDE';});
            }
        }
    });
}

function logout() {
    $.ajax({
        type: "get",
        dataType: "html",
        data: "&flag=logout",
        url: "/CMSFile/controls/AjaxHandle.aspx",
        error: function() {
            //alert('Error loading JSON document');
        },
        success: function(d) {
            if (d == "success") {
                location.href = "/Default.aspx";
            }
        }
    });
}

