﻿/********************
外部应用接口，主要为传入查询，接口方法为JavaScript的searchGrid(argument)函数。
argument为传入where的sql语句
************************/

//参数
var ListArg = "";
var r = "";

/*初始化赋值*/
$("img[name='titleimg']").attr("src", $("input[name='rootPath']").val() + "control/lslist/images/gtk-go-forward-ltr.gif");

//checkbox点击事件
var itemKey = "";
var itemName = "";
function checkAll(checked, controlId) {
    var input = $("table[id='GridviewDiv" + controlId + "']").find("input");
    for (var i = 0; i < input.length; i++) {
        if (input[i].type == "checkbox" && input[i].id != "gridChkAll") {
            input[i].checked = checked;
            if (checked == true) {
                itemKey += input[i].value + ",";
                itemName += input[i].dataName + ",";
            }
            else {
                itemKey = itemKey.replace(input[i].value + ",", "");
                itemName = itemName.replace(input[i].dataName + ",", "");
            }
        }
    }
}

function checkItem(checked, obj) {
    if (checked == true) {
        itemKey += obj.value + ",";
        itemName += obj.dataName + ",";
    }
    else {
        itemKey = itemKey.replace(obj.value + ",", "");
        itemName = itemName.replace(obj.dataName + ",", "");
    }
}

//弹出窗口，公共函数
function openMDialog(src, width, height, showScroll) {
    window.showModalDialog(src, '', 'location:No;status:No;help:No;dialogWidth:' + width + ';dialogHeight:' + height + ';scroll:' + showScroll + ';');
}

function openWin(src, option) {
    if (option == null)
        option = "height=100, width=400, toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no";
    window.open(src, '_blank', option);  //写成一行
}

//grid操作事件
//详细信息表现模式，0、内嵌iframe；1、模式对话框；2、window.open;3、自跳转
function showData(trData, keyValue) {
    var url = $("#hidSrc").val() + "?" + $("#hidParameter").val() + "=" + encodeURI(keyValue);
    if ($("#hidShowMode").val() == 0) {
        $("#" + $("#hidIframeid").val()).attr("src", url);
    }
    else if ($("#hidShowMode").val() == 1) {
        openMDialog(url, $("#hidMDWidth").val(), $("#hidMDHeight").val(), $("#hidMDShowScroll").val());
    }
    else if ($("#hidShowMode").val() == 2) {
        openWin(url, $("#hidOWOption").val());
    }
    else if ($("#hidShowMode").val() == 3) {
        window.location = url;
    }
}

//详细信息表现模式，0、内嵌iframe；1、模式对话框；2、window.open;3、自跳转
function editData(trData, keyValue) {
    var url = $("#hidEditSrc").val() + "?" + $("#hidEditParameter").val() + "=" + encodeURI(keyValue);
    if ($("#hidEditMode").val() == 0) {
        $("#" + $("#hidEditIframeid").val()).attr("src", url);
    }
    else if ($("#hidEditMode").val() == 1) {
        openMDialog(url, $("#hidEditMDWidth").val(), $("#hidEditMDHeight").val(), $("#hidEditMDShowScroll").val());
    }
    else if ($("#hidEditMode").val() == 2) {
        openWin(url, $("#hidEditOWOption").val());
    }
    else if ($("#hidEditMode").val() == 3) {
        window.location = url;
    }
}

//详细信息表现模式，0、内嵌iframe；1、模式对话框；2、window.open;3、自跳转
function showOther(showName, keyValue) {
    var url = $("#hidSrc" + showName).val() + "?" + $("#hidParameter" + showName).val() + "=" + encodeURI(keyValue);
    if ($("#hidShowMode" + showName).val() == 0) {
        $("#" + $("#hidIframeid" + showName).val()).attr("src", url);
    }
    else if ($("#hidShowMode" + showName).val() == 1) {
        openMDialog(url, $("#hidMDWidth" + showName).val(), $("#hidMDHeight" + showName).val(), $("#hidMDShowScroll" + showName).val());
    }
    else if ($("#hidShowMode" + showName).val() == 2) {
        openWin(url, $("#hidOWOption" + showName).val());
    }
    else if ($("#hidShowMode" + showName).val() == 3) {
        window.location = url;
    }
}

//删除方法
function delData(controlId) {
    var alterStr = "请确定是否要删除如下数据：" + itemName.substring(0, itemName.length - 1);
    if (window.confirm(alterStr)) {
        if ($("#hiddenSorting").val() == "false")
            ListArg = "CallbackDelGrid$" + itemKey.substring(0, itemKey.length - 1);
        else
            ListArg = "CallbackDelGrid$" + itemKey.substring(0, itemKey.length - 1) + "$" + $("#hiddenSorting").val();
        UpdateData(controlId);
    }
}

//删除行记录，独立列
function delRow(controlId, alterText, rowKey) {
    var alterStr = alterText;
    if (window.confirm(alterStr)) {
        if ($("#hiddenSorting").val() == "false")
            ListArg = "CallbackDelRow$" + rowKey;
        else
            ListArg = "CallbackDelRow$" + rowKey + "$" + $("#hiddenSorting").val();
        UpdateData(controlId);
    }
}

//删除行记录，模板列
function delITCRow(controlId, alterText, rowKey) {
    var alterStr = alterText;
    if (window.confirm(alterStr)) {
        if ($("#hiddenSorting").val() == "false")
            ListArg = "CallbackITCDelRow$" + rowKey;
        else
            ListArg = "CallbackITCDelRow$" + rowKey + "$" + $("#hiddenSorting").val();
        UpdateData(controlId);
    }
}

function changeState(obj, controlId) {
    var alterStr = "请确定" + obj.text + "如下数据：" + itemName.substring(0, itemName.length - 1);
    if (window.confirm(alterStr)) {
        if ($("#hiddenSorting").val() == "false")
            ListArg = "CallbackStateGrid$" + encodeURI(obj.text) + "$" + encodeURI(obj.value) + "$" + itemKey.substring(0, itemKey.length - 1);
        else
            ListArg = "CallbackStateGrid$" + encodeURI(obj.text) + "$" + encodeURI(obj.value) + "$" + itemKey.substring(0, itemKey.length - 1) + "$" + $("#hiddenSorting").val();
        UpdateData(controlId);
    }
}

//ajax处理

//辅助处理
function setPageInfo(RecordCount, PageCount, CurrentPageIndex, controlId) {
    $("#RecordCount" + controlId).attr("innerHTML", RecordCount);
    $("#PageCount" + controlId).attr("innerHTML", PageCount);
    $("#thispage" + controlId).attr("innerHTML", CurrentPageIndex);
    $("#currpage" + controlId).attr("value", CurrentPageIndex);
    $("#pageindex" + controlId).attr("value", CurrentPageIndex);
}

function UpdateData(controlId) {
    ListArg = "LSDataList" + "|" + ListArg + "$" + $("#currpage" + controlId).val();
    r = Math.round(Math.random() * 10000);
    postListData(controlId);
}

function ListResult(eventArgument, controlId) {
    if (eventArgument == "SUCCESS") {
        alert("数据更新已成功！");
    }
    else if (eventArgument == "FAILURE") {
        alert("发生错误，请再次尝试！");
    }
    else if (eventArgument == "Stop")
    { }
    else {
        try {
            var args = eventArgument.split('^');

            $("#GridviewDiv" + controlId).attr("outerHTML", args[0]);
            setPageInfo(args[1], args[2], args[3], controlId);

            itemKey = "";
            itemName = "";
        }
        catch (ex) {
            alert("Error Occurred.. Please try again..");
        }
    }
}

//gridview事件
var pageIndex;

function GridSorting(argument, controlId) {
    ListArg = argument + " " + $("#hiddenSortingValue").val();
    if ($("#hiddenSortingValue").val() == "ASC")
        $("#hiddenSortingValue").attr("value", "DESC");
    else
        $("#hiddenSortingValue").attr("value", "ASC");

    var args = ListArg.split('$');
    $("#hiddenSorting").attr("value", args[1]);

    UpdateData(controlId);
}

function SetPageIndex(indextype, controlId) {
    pageIndex = "none";
    if (indextype == 'go') {
        var goText = parseInt($("#pageindex" + controlId).val());
        var max = parseInt($("#PageCount" + controlId).attr("innerHTML")) + 1;
        if ($("#pageindex" + controlId).val() != "" && $("#pageindex" + controlId).val() != null
            && goText > 0 && goText < max)
            pageIndex = $("#pageindex" + controlId).val();
    }
    else if (indextype == 'btnFirst') {
        if ($("#currpage" + controlId).val() != "1")
            pageIndex = "1";
    }
    else if (indextype == 'btnPrev') {
        if ($("#currpage" + controlId).val() != "1")
            pageIndex = parseInt($("#currpage" + controlId).val()) - 1;
    }
    else if (indextype == 'btnNext') {
        if ($("#currpage" + controlId).val() != $("#PageCount" + controlId).attr("innerHTML"))
            pageIndex = parseInt($("#currpage" + controlId).val()) + 1;
    }
    else if (indextype == 'btnLast') {
        if ($("#currpage" + controlId).val() != $("#PageCount" + controlId).attr("innerHTML"))
            pageIndex = $("#PageCount" + controlId).attr("innerHTML");
    }

    if (pageIndex != "none") {
        if ($("#hiddenSorting").val() == "false")
            ListArg = "CallbackChangePageIndex$" + pageIndex;
        else
            ListArg = "CallbackChangePageIndex$" + pageIndex + "$" + $("#hiddenSorting").val();
        UpdateData(controlId);
    }
    else {
        event.returnValue = false;
    }
}

function searchGrid(argument, controlId) {
    pageIndex = "1";
    if ($("#hiddenSorting").val() == "false")
        ListArg = "CallbackSearchGrid$" + encodeURI(argument) + "$" + pageIndex;
    else
        ListArg = "CallbackSearchGrid$" + encodeURI(argument) + "$" + pageIndex + "$" + $("#hiddenSorting").val();
    UpdateData(controlId);
}


/*
var oldRow="";
function ChangeRowColor(rowObj)
{        
if(oldRow=="")
{
oldRow=rowObj;
oldRow.style.backgroundColor='#FFE6D0';
rowObj.style.backgroundColor='#FF80FF';
}
if(oldRow!=rowObj)
{
oldRow.style.backgroundColor='#FFE6D0';
rowObj.style.backgroundColor='#FF80FF';
oldRow=rowObj;
}
}
var checkedRows="";
function gvChecked(obj,rowinfo)
{
if(obj.checked==true)
            
checkedRows=checkedRows+"^"+rowinfo;
}
else
{
tmpRows=checkedRows.split("^"+rowinfo);
            
if(tmpRows[0].length!=0&&tmpRows[1].length!=0)
checkedRows=tmpRows[0]+tmpRows[1];
else if(tmpRows[0].length==0&&tmpRows[1].length!=0)
checkedRows=tmpRows[1];
else if(tmpRows[0].length!=0&&tmpRows[1].length==0)
checkedRows=tmpRows[0];
else
checkedRows="";
}
}

*/
