$(document).ready(function() {
    GetTableList();
    var docWidth = $(document).width();
    var selTableWidth = docWidth - 300;
    $('#divSelectedTable').css('width', '100000');
    $('#divScroll').css('width', selTableWidth);

    var mMain = decodeURI($('#' + hfHTMLMain).attr('value'));
    $('#divSelectedTable').html(mMain);
    var mWhere = decodeURI($('#' + hfHTMLWhere).attr('value'));
    if (mWhere != '' && mWhere != null) { $('#divFilter').html(mWhere); }
    var mOrder = decodeURI($('#' + hfHTMLOrder).attr('value'));
    if (mOrder != '' && mOrder != null) { $('#divOrder').html(mOrder); }
    var mInner = decodeURI($('#' + hfHTMLInner).attr('value'));
    if (mInner != '' && mInner != null) { $('#divConnect').html(mInner); }

    $('.FieldIcon>input').each(function() {
        $(this).removeAttr('checked');
    });

    var strFieldsJson = $('#' + hfSelectFields).attr('value');
    if (strFieldsJson.length > 0) {
        var arrayObj = JSON.parse(strFieldsJson);
        for (var i = 0; i < arrayObj.length; i++) {
            $('input[FieldID="' + arrayObj[i].FieldID + '"]').each(function() {
                $(this).attr('checked', 'checked');
            });
        }
    }
    var strWhereJson = $('#' + hfWhere).attr('value');
    if (strWhereJson.length > 0) {
        var arrayObj = JSON.parse(strWhereJson);
        for (var i = 0; i < arrayObj.length; i++) {
            if (arrayObj[i].isEmpty) {
                $('#F' + arrayObj[i].RowID).attr('checked', 'checked');
            }
            if (arrayObj[i].isIgnore) {
                $('#G' + arrayObj[i].RowID).attr('checked', 'checked');
            }
        }
    }
    $(document).bind('click', function() { vent(); });
    $('#' + btnFlash).bind('click', function() { iconReflash(); });
    $('.TBSelectFilterText[id]').each(function() {
        $(this).bind('click', function() { tdClick(this); });
    });
    $('#' + btnRunPars).bind('click', function() { btnParClick(); });
    var parHTML = $('#' + lblParList).html();
    if (parHTML != null && parHTML != '') { SQLTab('TabSQLTest'); }
});
function GetTableList() {
    $('#ulTableList').html('');
    $.getJSON("/TableList.gdr?tmp=" + (new Date()).getTime(), { "resultType": "json" }, function(data, textStatus) {
        if (data.length > 0) {
            for (var i = 0; i < data.length; i++) {
                $('#ulTableList').append('<li class="Tableli"><span><input type="checkbox" id="c' + data[i].ID + '" /></span><span id="t' + data[i].ID + '" title="' + data[i].TableName + '">' + data[i].TableNote + '</span></li>');
            }
            $('.Tableli>span[id]').each(function() {
                $(this).bind('click', function() { txtClick(this); });
            });
            $('.Tableli>span>input').each(function() {
                $(this).bind('click', function() { chkClick(this); });
            });
            var strTableJson = $('#' + hfTableJson).attr('value');
            if (strTableJson.length > 0) {
                var arrayObj = JSON.parse(strTableJson);
                for (var i = 0; i < arrayObj.length; i++) {
                    var a = $('#c' + arrayObj[i].ID);
                    $('#c' + arrayObj[i].ID).attr('checked', 'checked');
                }
            }
        }
    });
}
function txtClick(obj) {
    var tID = obj.id.replace('t', '');
    $('#c' + tID).attr('checked', 'checked');
    InsertTable(tID);
    SaveTables();
}
function chkClick(obj) {
    var tID = obj.id.replace('c', '');
    var checked = $(obj).attr('checked');
    if (checked == 'checked') {
        InsertTable(tID);
    } else {
        DeleteTable(tID)
    }
    SaveTables();
}
function InsertTable(TableID) {
    var html = '';
    var dtName = $('#t' + TableID).attr('title');
    var dtNote = $('#t' + TableID).html();
    tCount = $('#t' + TableID, '#divSelectedTable').size() + 1;
    if (tCount > 1) { return; }
    var strImgField = '<img src="/App_Themes/ManagerTheme/images/PrimaryKey.gif" alt="主键" />';
    $.getJSON("/FieldsList.gdr?TableID=" + TableID + "&tmp=" + (new Date()).getTime(), { "resultType": "json" }, function(data, textStatus) {
        if (data.length > 0) {
            var TBCount = $('#divSelectedTable>div[class="TBFieldsList"]').size() + 1;
            if (TBCount > 1) {
                html += '<div class="TBConnect" id="i' + TableID + '"><img src="/App_Themes/ManagerTheme/images/TableConnect.gif" /></div>';
            }
            html += '<div class="TBFieldsList" id="t' + TableID + '"><table cellpadding="0" cellspacing="0" class="TBFieldsList_table"><tr><td class="TBTitle" colspan="4">' + dtNote + '&nbsp;&nbsp;' + dtName + '</td></tr></table>';
            html += '<div class="FieldsScroll"><table cellpadding="0" cellspacing="0" class="TBFieldsList_table" id="f' + TableID + '">';
            for (var i = 0; i < data.length; i++) {
                if (data[i].IsPrimaryKey == '0') { strImgField = ''; }
                html += '<tr class="menuout" onmouseover="this.className=\'menuin\'" onmouseout="this.className=\'menuout\'">\
                            <td class="FieldIcon">' + strImgField + '</td>\
                            <td class="FieldIcon"><input type="checkbox" id="chk' + data[i].ID + '" TableID="' + TableID + '" FieldID="' + data[i].ID + '" TableName="' + dtName + '" FieldName="' + data[i].FieldName + '" PrimaryKey="' + data[i].IsPrimaryKey + '" /></td>\
                            <td>' + data[i].FieldNote + '</td>\
                            <td class="FieldsName">' + data[i].FieldName + '</td></tr>';
            }
            html += '</table></div></div>';
            $('#divSelectedTable').append(html);
        } else {
            $.dialog.alert('<div style="width:300px;padding:0px 0px 0px 15px;">请求的数据表没有导入字段，请重新导入表数据！</div>');
        }
    });
}
function DeleteTable(TableID) {
    $('#t' + TableID + '[class="TBFieldsList"]').remove();
    $('#i' + TableID + '[class="TBConnect"]').remove();
}
/*======输出    字段添加删除======*/
function SaveTables() {
    var arrayObj = new Array();
    $('#ulTableList>li>span>input').each(function() {
        if ($(this).attr('checked') == 'checked') {
            var TableID = $(this).attr('id').replace('c', '');
            var TableName = $('#t' + TableID).attr('title');
            var TableNote = $('#t' + TableID).html();
            var newJson = { 'ID': TableID, 'Name': TableName, 'Note': TableNote };
            arrayObj.push(newJson);
            var strImgField = '<img src="/App_Themes/ManagerTheme/images/PrimaryKey.gif" alt="主键" />';
            $('#' + hfTableJson).attr('value', JSON.stringify(arrayObj));
        }
    });
}
function SaveFields() {
    var arrayObj = new Array();
    var TableID = '';
    var FieldID = '';
    var FieldName = '';
    var TableName = '';
    var PrimaryKey = '';
    $('.FieldIcon>input').each(function() {
        if ($(this).attr('checked') == 'checked') {
            TableID = $(this).attr('TableID');
            FieldID = $(this).attr('FieldID');
            FieldName = $(this).attr('FieldName');
            TableName = $(this).attr('TableName');
            PrimaryKey = $(this).attr('PrimaryKey');
            var newSelectJson = { "TableID": TableID, "FieldID": FieldID, 'FieldName': FieldName, 'TableName': TableName, 'PrimaryKey': PrimaryKey };
            arrayObj.push(newSelectJson);
        }
    });
    $('#' + hfSelectFields).attr('value', JSON.stringify(arrayObj));
}
function FieldsTab(tabID) {
    close();
    divID = tabID.replace('Tab', 'div');
    $('td[title="Fields"]').each(function() {
        if ($(this).attr('id') == tabID) {
            $(this).attr('class', 'TabSelect');
        } else {
            $(this).attr('class', 'TabNormal');
        }
    });
    $('#divScroll>div').each(function() {
        if ($(this).attr('id') == divID) {
            $(this).css('display', 'block');
        } else {
            $(this).css('display', 'none');
        }
    });
}
function SQLTab(tabID) {
    close();
    divID = tabID.replace('Tab', 'div');
    $('#SQLText>div').each(function() {
        if ($(this).attr('id') == divID) {
            $(this).css('display', 'block');
        } else {
            $(this).css('display', 'none');
        }
    });
    if (tabID != 'TabSQLTest') {
        $('#' + hftxtSQL).attr('value', tabID);
    } else {
        return;
    }
    $('td[title="SQL"]').each(function() {
        if ($(this).attr('id') == tabID) {
            $(this).attr('class', 'TabSQLSelect');
        } else {
            $(this).attr('class', 'TabSQLNormal');
        }
    });
}
/*   -------------------  下拉框  --------------------*/
function getAbsoluteLeft(ob) {
    var x = ob.offsetLeft;
    var y = ob.offsetTop;
    while (ob = ob.offsetParent) { y += ob.offsetTop; x += ob.offsetLeft; }
    return x + "px";
}
function getAbsoluteTop(ob) {
    var x = ob.offsetLeft;
    var y = ob.offsetTop;
    var h = ob.offsetHeight;
    while (ob = ob.offsetParent) { y += ob.offsetTop; x += ob.offsetLeft; }
    return (y + h) + "px";
}

function close() {
    $('#dClass').css('display', 'none');
}
function vent(event) {
    var idName = document.activeElement.id;
    var objInput = $('#' + idName)[0];
    var objouter = $("#dClass")[0];
    var isShow = $('#dClass').is(":visible");
    if (document.all) {
        if (isShow) {
            if (window.event.srcElement != objouter && window.event.srcElement != objInput) close();

            return;
        }
    } else {
        if (isShow) {
            if (event.target != objouter && event.target != objInput) close();
        }
    }
}
function tdClick(obj) {
    $(obj).html('&nbsp;');
    $(obj).removeAttr('value');
    $('#ulClass').html('');
    $('#dClass').css('top', getAbsoluteTop(obj));
    $('#dClass').css('left', getAbsoluteLeft(obj));
    $('#dClass').css('width', obj.offsetWidth);
    $('#dClass').css('display', 'block');
    var html = '';
    switch ($(obj).attr('title')) {
        case 'CnnField':
            var strFieldsJson = $('#' + hfTableJson).attr('value');
            if (strFieldsJson.length > 0) {
                $.getJSON("/GetTablsFields.gdr?JsonTables=" + encodeURI(strFieldsJson) + "&tmp=" + (new Date()).getTime(), { "resultType": "json" }, function(data, textStatus) {
                    if (data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            var FieldNote = data[i].FieldNote;
                            var TableNote = data[i].TableNote;
                            if (FieldNote == '') { FieldNote = data[i].FieldName; }
                            if (TableNote == '') { TableNote = data[i].TableName; }
                            html += '<li id="' + data[i].TableName + '.' + data[i].FieldName + '">' + TableNote + '>>' + FieldNote + '</li>';
                        }
                    }
                    ShowdplList(html, obj);
                });
            }
            break;
        //---------------------------------------------------------------Where----------------------------------------------------------------
        case 'Field'://字段
            var strFieldsJson = $('#' + hfTableJson).attr('value');
            if (strFieldsJson.length > 0) {
                $.getJSON("/GetTablsFields.gdr?JsonTables=" + encodeURI(strFieldsJson) + "&tmp=" + (new Date()).getTime(), { "resultType": "json" }, function(data, textStatus) {
                    if (data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            var FieldNote = data[i].FieldNote;
                            var TableNote = data[i].TableNote;
                            if (FieldNote == '') { FieldNote = data[i].FieldName; }
                            if (TableNote == '') { TableNote = data[i].TableName; }
                            html += '<li id="' + data[i].TableName + '.' + data[i].FieldName + '">' + TableNote + '>>' + FieldNote + '</li>';
                        }
                    }
                    ShowdplList(html, obj);
                });
            }
            break;
        case 'Operator'://运算符
            ShowdplList('<li id="等于">=</li><li id="大于">></li><li id="小于"><</li><li id="大于等于">>=</li><li id="小于等于"><=</li><li id="不等于"><></li><li id="LIKE">Like</li>', obj);
            break;
        case 'ValueMethod'://取值方式
            ShowdplList('<li id="Request">传参</li><li id="Cookie">Cookies</li><li id="Value">固定值</li>', obj);
            break;
        case 'NameValue'://名称/值
            ShowdplList('<li style="padding:0px;"><input id="iValue" style="border: 0px;border-bottom: 1px solid #C0C0C0;width:96%;" /></li>', obj);
            break;
        case 'Decrypt'://值解密
            ShowdplList('<li>编码</li><li>MD5</li>', obj);
            break;
        case 'Connect':
            ShowdplList('<li>AND</li><li>OR</li>', obj);
            break;
        //-----------------------------------------------------------------------OrderBY------------------------------------------------------------------------
        case 'FieldSource': //字段来源
            var souTable = $(obj).attr('id').replace('AA', 'BB');
            $('#' + souTable).html('&nbsp;');
            $('#' + souTable).removeAttr('value');
            var strJson = $('#' + hfTableJson).attr('value');
            html += '<li id="Request">传参</li><li id="Cookie">Cookies</li>';
            if (strJson.length > 0) {
                arrayObj = JSON.parse(strJson);
                for (var i = 0; i < arrayObj.length; i++) {
                    var TableNote = arrayObj[i].Note;
                    if (TableNote == '') { TableNote = arrayObj[i].Note; }
                    html += '<li id="t' + arrayObj[i].ID + '">' + TableNote + '</li>';
                }
                ShowdplList(html,obj);
            }
            break;
        case 'FieldName': //字段名称
            var TDTable = $(obj).attr('id').replace('BB', 'AA');
            var selValue = $('#' + TDTable).attr('value');
            switch (selValue) {
                case 'Request':
                    ShowdplList('<li style="padding:0px;"><input id="iFieldName" type="input" style="border: 0px;border-bottom: 1px solid #C0C0C0;width:96%;" /></li>', obj);
                    break;
                case 'Cookie':
                    ShowdplList('<li style="padding:0px;"><input id="iFieldName" type="input" style="border: 0px;border-bottom: 1px solid #C0C0C0;width:96%;" /></li>', obj);
                    break;
                default:
                    var strValue = $('#' + TDTable).attr('value');
                    if (strValue != null && strValue != '') {
                        var TableID = strValue.replace('t', '');
                        $.getJSON("/FieldsList.gdr?TableID=" + TableID + "&tmp=" + (new Date()).getTime(), { "resultType": "json" }, function(data, textStatus) {
                            if (data.length > 0) {
                                for (var i = 0; i < data.length; i++) {
                                    var FieldNote = data[i].FieldNote;
                                    if (FieldNote == '') { FieldNote = data[i].FieldName; }
                                    html += '<li id="' + data[i].FieldName + '">' + FieldNote + '</li>';
                                }
                            }
                            ShowdplList(html, obj);
                        });
                    }
                    break;
            }
            break;
        case 'OrderSource'://排序来源
            ShowdplList('<li id="Request">传参</li><li id="Cookie">Cookies</li><li id="Value">固定值</li>', obj);
            break;
        case 'OrderName': //排序名称
            var TDTable = $(obj).attr('id').replace('DD', 'CC');
            var selValue = $('#' + TDTable).attr('value');
            if (selValue == 'Value') {
                ShowdplList('<li id="">升序</li><li id="DESC">降序</li>', obj);
            } else {
                ShowdplList('<li style="padding:0px;"><input id="iOrderName" type="input" style="border: 0px;border-bottom: 1px solid #C0C0C0;width:96%;" /></li>', obj);
            }
            break;
        default:
            break;
    }

}

function ShowdplList(strHTML, obj) {
    $('#ulClass').append(strHTML);
    $('#ulClass>li').each(function() {
        $(this).bind('click', function() {
            switch ($(obj).attr('title')) {
                case 'CnnField':
                    $(obj).attr('value', $(this).attr('id'));
                    $(obj).html($(this).html());
                    break;
                //============================================================Where============================================            
                case 'NameValue': // 名称/值
                    var iValue = $('#iValue').attr('value');
                    if (iValue == '' || iValue == null) {
                        iValue = '&nbsp;';
                    }
                    $(obj).html(iValue);
                    break;
                case 'Field': // 字段下拉框点击
                    $(obj).attr('value', $(this).attr('id'));
                    $(obj).html($(this).html());
                    break;
                case 'ValueMethod': // 取值方式
                    $(obj).attr('value', $(this).attr('id'));
                    $(obj).html($(this).html());
                    break;
                case 'Operator': // 运算符
                    $(obj).attr('value', $(this).attr('id'));
                    $(obj).html($(this).html());
                    break;
                case 'Decrypt': // 值解密
                    $(obj).html($(this).html());
                    break;
                //================================================================OrderBY============================================           
                case 'FieldSource': //  字段来源
                    $(obj).attr('value', $(this).attr('id'));
                    $(obj).html($(this).html());
                    break;
                case 'FieldName': // 字段名称
                    var TDTable = $(obj).attr('id').replace('BB', 'AA');
                    var selValue = $('#' + TDTable).attr('value');
                    if (selValue == 'Request' || selValue == 'Cookie') {
                        var iValue = $('#iFieldName').attr('value');
                        if (iValue == '' || iValue == null) {
                            iValue = '&nbsp;';
                        }
                        $(obj).attr('value', iValue);
                        $(obj).html(iValue);
                    } else {
                        $(obj).attr('value', $(this).attr('id'));
                        $(obj).html($(this).html());
                    }
                    break;
                case 'OrderSource': // 排序来源
                    $(obj).attr('value', $(this).attr('id'));
                    $(obj).html($(this).html());
                    break;
                case 'OrderName': // 排序名称
                    var TDTable = $(obj).attr('id').replace('DD', 'CC');
                    var selValue = $('#' + TDTable).attr('value');
                    if (selValue == 'Request' || selValue == 'Cookie') {
                        var iValue = $('#iOrderName').attr('value');
                        if (iValue == '' || iValue == null) {
                            iValue = '&nbsp;';
                        }
                        $(obj).attr('value', iValue);
                        $(obj).html(iValue);
                    } else {
                        $(obj).attr('value', $(this).attr('id'));
                        $(obj).html($(this).html());
                    }
                    break;
                default:
                    $(obj).html($(this).html());
                    break;
            }
        });
        $(this).bind('mouseover', function() {
            $(this).attr('class', 'cClass_lihour');
        });
        $(this).bind('mouseout', function() {
            $(this).removeClass();
        });
    });
}
/*   -------------------  下拉框结束  --------------------*/
function SaveWhere() {
    var WhereJson = new Array();
    for (var i = 1; i < 8; i++) {
        var valueField = $('#A' + i).attr('value');
        var valueMethod = $('#C' + i).attr('value');
        var valueOperator = $('#B' + i).attr('value');
        var valueNameValue = $('#D' + i).html().replace('&nbsp;','');
        var valueDecrypt = $('#E' + i).html().replace('&nbsp;', '');
        var valueConnect = $('#H' + i).html().replace('&nbsp;', '');
        var WhereEmpty = $('#F' + i).attr('checked');
        var WhereIgnore = $('#G' + i).attr('checked');
        if (valueField != null && valueField != '' && valueOperator != '' && valueMethod != null && valueMethod != '' && valueNameValue != '') {
            var valueEmpty = false;
            var valueIgnore = false;
            if (WhereEmpty == 'checked') { valueEmpty = true; }
            if (WhereIgnore == 'checked') { valueIgnore = true; }
            var newWhere = { 'Field': valueField, 'Method': valueMethod, 'iOperator': valueOperator, 'NameValue': valueNameValue, 'Decrypt': valueDecrypt, 'isEmpty': valueEmpty, 'isIgnore': valueIgnore, 'RowID': i ,'Connect':valueConnect};
            WhereJson.push(newWhere);
        }
    }
    $('#' + hfWhere).attr('value', JSON.stringify(WhereJson));
}
function SaveOrder() {
    var OrderJson = new Array();
    for (var i = 1; i < 8; i++) {
        var valueFieldSource = $('#AA' + i).attr('value');
        var valueFieldName = $('#BB' + i).attr('value');
        var valueOrderSource = $('#CC' + i).attr('value');
        var valueOrderName = $('#DD' + i).attr('value');
        if (valueOrderName == null) {valueOrderName = ''; }
        if (valueFieldSource != 'Request' && valueFieldSource != 'Cookie' && valueFieldSource!=null) {
            var arrayObj;
            var strJson = $('#' + hfTableJson).attr('value');
            if (strJson.length > 0) {
                arrayObj = JSON.parse(strJson);
                for (var j = 0; j < arrayObj.length; j++) {
                    if (arrayObj[j].ID == valueFieldSource.replace('t', '')) {
                        valueFieldSource = arrayObj[j].Name;
                    }
                }
            }
        }
        if (valueFieldSource != null && valueFieldSource != '' && valueFieldName != '' && valueFieldName != null && valueOrderSource != '' && valueOrderSource != null ) {
            var newOrder = { 'FieldSource': valueFieldSource, 'FieldName': valueFieldName, 'OrderSource': valueOrderSource, 'OrderName': valueOrderName };
            OrderJson.push(newOrder);
        }
    }
    $('#' + hfOrderBY).attr('value', JSON.stringify(OrderJson));
}
function SaveInner() {
    var InnerJson = new Array();
    for (var i = 1; i < 8; i++) {
        var FieldA = $('#AAA' + i).attr('value');
        var FieldB = $('#BBB' + i).attr('value');
        if (FieldA != null && FieldA != '' && FieldB != '' && FieldB != null) {
            var newInner = { 'FieldA': FieldA, 'FieldB': FieldB };
            InnerJson.push(newInner);
        }
    }
    $('#' + hfInner).attr('value', JSON.stringify(InnerJson));
}
(function($) {
    $.fn.insertAtCaret = function(tagName) {
        return this.each(function() {
            if (document.selection) {
                //IE support
                this.focus();
                sel = document.selection.createRange();
                sel.text = tagName;
                this.focus();
            } else if (this.selectionStart || this.selectionStart == '0') {
                //MOZILLA/NETSCAPE support
                startPos = this.selectionStart;
                endPos = this.selectionEnd;
                scrollTop = this.scrollTop;
                this.value = this.value.substring(0, startPos) + tagName + this.value.substring(endPos, this.value.length);
                this.focus();
                this.selectionStart = startPos + tagName.length;
                this.selectionEnd = startPos + tagName.length;
                this.scrollTop = scrollTop;
            } else {
                this.value += tagName;
                this.focus();
            }
        });
    };
})(jQuery);

function iconSaveClick() {
    SaveToHidden();
}
function iconReflash() {
    SaveToHidden();
}
function SaveToHidden() {
    SaveTables();
    SaveFields();
    SaveWhere();
    SaveOrder();
    SaveInner();
    $('#' + hfHTMLMain).attr('value', encodeURI($('#divSelectedTable').html()));
    $('#' + hfHTMLWhere).attr('value',encodeURI( $('#divFilter').html()));
    $('#' + hfHTMLOrder).attr('value', encodeURI($('#divOrder').html()));
    $('#' + hfHTMLInner).attr('value', encodeURI($('#divConnect').html()));
}
function InsertRequest() {
    if ($('#divSQLMain').is(':hidden')) {
//        $('#' + txtSQLCount).insertAtCaret('\r\n    <Value Type="Request" Name="参数名称" Decrypt="值解密(编码/MD5)" IsEmpty="可为空(True/False)" IsIgnore="可忽略(True/False)" />\r\n');
        $('#' + txtSQLCount).insertAtCaret('\r\n    <Value Type="Variable" ValueType="Request" Name="参数名称" Decrypt="值解密(编码/MD5)" />\r\n');
    } else {
    $('#' + txtSQLMain).insertAtCaret('\r\n    <Value Type="Variable" ValueType="Request" Name="参数名称" Decrypt="值解密(编码/MD5)" />\r\n');
    }
}
function InsertCookie() {
    if ($('#divSQLMain').is(':hidden')) {
        $('#' + txtSQLCount).insertAtCaret('\r\n    <Value Type="Variable" ValueType="Cookie" Name="参数名称" Decrypt="值解密(编码/MD5)" />\r\n');
    } else {
    $('#' + txtSQLMain).insertAtCaret('\r\n    <Value Type="Variable" ValueType="Cookie" Name="参数名称" Decrypt="值解密(编码/MD5)" />\r\n');
    }
}
function InsertPage() {
    if ($('#divSQLMain').is(':hidden')) {
        $('#' + txtSQLCount).insertAtCaret('@Page');
    } else {
        $('#' + txtSQLMain).insertAtCaret('@Page');
    }
}
function InsertPageSize() {
    if ($('#divSQLMain').is(':hidden')) {
        $('#' + txtSQLCount).insertAtCaret('@PageSize');
    } else {
        $('#' + txtSQLMain).insertAtCaret('@PageSize');
    }
}
function btnParClick() {
    var jsPar = new Array();
    var parCount = $('#ParTable').attr('Count');
    if (parCount != null && parCount != '') {
        for (var i = 1; i <= parCount; i++) {
            var s = $('#p' + i).attr('Sourcess');
            var v = $('#v' + i).attr('value');
            var t = $('#p' + i).attr('Tag');
            var newPar = { 'Sourcess': s, 'Value': v, 'Tag': t };
            jsPar.push(newPar);
        }
        $('#' + hfParList).attr('value', JSON.stringify(jsPar));
    }
}
function showErrorSQL() {
    $.dialog({
        lock: true,
        title: 'SQL语句',
        content: '<textarea name="textarea" style=" width:700px; height:340px;">' + $('#errorSQL').html() + '</textarea>',
        width: '700px',
        height: '340px',
        max: false,
        min: false,
        zIndex: '9999'
    });
}
function TabPannel(intStep) {
    for (var i = 1; i < 3; i++) {
        if (i == intStep) {
            $('#BuildStep' + i).css('display', 'none');
        } else {
            $('#BuildStep' + i).css('display', 'block');
        }
    }   
}