﻿var AjaxRequest = {}; //AJAX
var AjaxTip = {}; //AJAX提示框
var Dialog = {}; //对话框
var StringHelper = {}; //字符串剔除
var DomNode = {}; //节点操作

//Ajax GET方法
AjaxRequest.Get = function(url, panelIDs) {
    url = url || (document.forms[0] && document.forms[0].action) || StringHelper.Clear(location.href, '#');
    var IDs = Array.prototype.slice.apply(arguments).slice(1 || 0);
    var callback = IDs[IDs.length - 1];
    if (typeof callback == 'function') {
        IDs = IDs.slice(0, IDs.length - 1);
    }
    else {
        callback = null;
    }

    var panelStrings = IDs.length > 0 ? IDs.join(',') : '__DefaultAjaxPanleID'; //panelIDs为空不更新
    $.ajax
    (
        {
            type: 'get',
            dataType: 'json',
            cache: false,
            url: url,
            data: '__AjaxPanelIDs=' + panelStrings,
            success:
            function(content) {
                //显示提示信息
                //AjaxTip.Show(content);
                if (content['Status'] == 'Success') {
                    //如果需要更新ajaxpanel就更新
                    for (var i = 0; i < IDs.length; i++) {
                        var o = document.getElementById(IDs[i]);
                        if (o) {
                            document.getElementById(IDs[i]).innerHTML = $.trim(content['Panels'][IDs[i]]);
                        }
                    }
                    //url && document.forms[0] && (document.forms[0].action = url);
                }

                if (callback != null)//如果有回调函数
                {
                    callback(content);
                }
            },
            complete:
            function() {
                // AjaxTip.Show('完成');
                //AjaxTip.Hide(100);
            },
            beforeSend:
            function() {
                //AjaxTip.Show('正在请求，请稍后...');
            },
            error:
            function(XMLHttpRequest, textStatus, errorThrown) {
                //AjaxTip.Show('请求错误，请重试');
            },
            timeout:
            function() {
                // AjaxTip.Show('请求超时，请重试');
            }


        }
    );
};

//--AJAX POST 方法
AjaxRequest.Post = function(buttonName, panelIDs) {
    var f = document.forms[0];
    for (var i = 0; i < document.forms.length; i++) {
        if (document.forms[i].elements[buttonName]) {
            f = document.forms[i];
            break;
        }
    }

    var url = (f && f.action) || StringHelper.Clear(location.href, '#');
    var IDs = Array.prototype.slice.apply(arguments).slice(1 || 0);
    var callback = IDs[IDs.length - 1];
    if (typeof callback == 'function') {
        IDs = IDs.slice(0, IDs.length - 1);
    }
    else {
        callback = null;
    }

    var panelStrings = IDs.length > 0 ? IDs.join(',') : '__DefaultAjaxPanleID';
    var data = [];
    data.push({ name: buttonName, value: buttonName });
    data.push({ name: '__AjaxPanelIDs', value: panelStrings });

    var options =
    {
        type: 'post',
        dataType: 'json',
        url: url,
        data: data,
        cache: false,
        success:
        function(content) {
            if (content['Status'] == 'Success') {
                //如果需要更新ajaxpanel就更新
                for (var i = 0; i < IDs.length; i++) {
                    document.getElementById(IDs[i]).innerHTML = $.trim(content['Panels'][IDs[i]]);
                }
            }
           //Dialog.Tip(content['Tip'], content['Status']);

            if (callback != null)//如果有回调函数
            {
                callback(content);
                return;
            }


        },
        complete:
        function() {
            $('input[name=' + buttonName + ']').attr('disabled', ''); //按钮设置为可用
        },
        beforeSend:
        function() {
            $('input[name=' + buttonName + ']').attr('disabled', 'disabled'); //按钮设置为不可用
        },
        error:
        function() {
            AjaxTip.Show('请求错误，请重试');
        },
        timeout:
        function() {
            AjaxTip.Show('请求超时，请重试');
        },
        resetForm: false
    };

    $('#' + f.id).ajaxSubmit(options);
    return false;
};

//字符串剔除方法
StringHelper.Clear = function (str, charString) {
    str = str.substring(0, str.indexOf(charString)) || str;
    return str;
};


/*copy fuxunchu js*/
function GoSuperViewInfo(type, action, temp) {
    
    if (action == 0) {
        AjaxRequest.Get("?type=" + type + "&temp=" + temp + "", 'ap_superview', function () { SetBackColor1(type, action); });
    }
    else {
        AjaxRequest.Get("?type=" + type + "&temp=" + temp + "", 'ap_superview', function () { SetBackColor1(type, action); });
    } 
    
} 

function SetBackColor1(type, action)
 {
    var tab = document.getElementById("cont_table");
     
    if (action == 0)
    {
        for (var i = 0; i < tab.rows.length; i++)
        {
            if (type<=5 ) {
                tab.rows[i].cells[type - 1].className = 'shu'; 
            }
            else if (type <= 8) {
                tab.rows[i].cells[type-2].className = 'shu';
            }
            else if (type <= 11) {
                tab.rows[i].cells[type - 3].className = 'shu';
            }
            else if (type <= 14) {
                tab.rows[i].cells[type - 4].className = 'shu';
            }
             
        }
    } 
}

function GoSuperViewFull(type, action, temp) {
    if (action == 0) {
        AjaxRequest.Get("?type=" + type + "&temp=" + temp + "", 'ap_superview', function () { SetBackColor(type, action); });
    }
    else {
        AjaxRequest.Get("?type=" + type + "&temp=" + temp + "", 'ap_superview', function () { SetBackColor(type, action); });
    }
}


function GoSuperView(type, action) {
    if (action == 0) {
        AjaxRequest.Get('?type=' + type, 'ap_superview', function() { SetBackColor(type, action); }); 
    }
    else {
        AjaxRequest.Get('?type=' + type, 'ap_superview', function() { SetBackColor(type, action); });
    } 
}

function SetBackColor(type, action) { 
    var tab = document.getElementById("cont_table");
    if (action == 0) {
        for (var i = 2; i < tab.rows.length; i++) {
            if (type > 3) {
                tab.rows[i].cells[type - 3].className = 'shu';
            }
            else {
                tab.rows[i].cells[type].className = 'shu';
            }
        }
    }
    else {
        for (var i = 2; i < tab.rows.length; i++) {
            tab.rows[i].cells[type].className = 'shu';
        }
    }
}
 
function searchSuperView() {
    var code = $id('stockcode').value;
    AjaxRequest.Get('?code=' + code, 'ap_superview');
}
function blockdate(type, action) {
    AjaxRequest.Get('?type=' + type, 'ap_superview', function() { setblockcolor(type, action); });
}
function setblockcolor(type, action) 
{
    var tab = $id("cont_table");
    for (var i = 1; i < tab.rows.length; i++) {
        if (type > 0) 
        {
            tab.rows[i].cells[type-1].className = 'shu';
        }
        else {
            tab.rows[i].cells[type].className = 'shu';
        }
    }
}
function js_help(htmlurl) {
    window.open(htmlurl, 'newwin', "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=616,height=450"); 
}


/*ccfx排序 */
function GoCCFX(type, action) { 
    AjaxRequest.Get('?type=' + type, 'ap_ccfx', function () { SetBackColorCcfx(type, action); });
}

function SetBackColorCcfx(type, action) { 
    var tab = document.getElementById("cont_table");
    if (action == 0) {
        for (var i = 1; i < tab.rows.length; i++) {
            if (type < 3) { 
                tab.rows[i].cells[type+2].className = 'shu';
            }
            else if (type > 5 && type < 11){
                tab.rows[i].cells[type - 1].className = 'shu';
            } 
        }
    }
    else { 
        for (var i = 1; i < tab.rows.length; i++) {
            tab.rows[i].cells[type+2].className = 'shu';
        }
    }
}


/*ccfxHistory排序 */
function GoCCFXHistroy(type, action) {
    AjaxRequest.Get('?type=' + type, 'ap_CcfxHistory', function () { SetBackColorCcfxHistory(type, action); });
}

function SetBackColorCcfxHistory(type, action) {
    var tab = document.getElementById("cont_table");
    if (action == 0) {
        for (var i = 1; i < tab.rows.length; i++) {
            if (type < 3) {
                tab.rows[i].cells[type + 3].className = 'shu';
            }
            else if (type > 5 && type < 11) {
                tab.rows[i].cells[type].className = 'shu';
            }
        }
    }
    else {
        for (var i = 1; i < tab.rows.length; i++) {
            tab.rows[i].cells[type + 3].className = 'shu';
        }
    }
}
