function CreateSql() {
    var hostTableValue = DianCMS_$("DropHostTable").value;
    var where = DianCMS_$("txtWhere").value;
    var order = DianCMS_$("txtOrder").value;
    if (hostTableValue == "0") {
        alert("请选择一个主表")
        DianCMS_$("DropHostTable").focus();
        return false;
    }
    else {
        var guestTableValue = DianCMS_$("DropGuestTable").value;
        var topNum = DianCMS_$("txtTopNum").value;
        if (topNum != "0")
            topNum = " TOP " + topNum;
        else
            topNum = "";

        if (guestTableValue == "0") {
            DianCMS_$("txtSql").value = "SELECT" + topNum + " " + HostTableFieldList() + " FROM " + hostTableValue + "" + where + "" + order + "";
        }
        else {
            var join = DianCMS_$("Join").value;
            var joinHost = DianCMS_$("DropHostFieldList").value;
            var joinGuest = DianCMS_$("DropGuestFieldList").value;
            DianCMS_$("txtSql").value = "SELECT" + topNum + " " + HostTableFieldList() + "," + GuestTableFieldList() + " FROM " + hostTableValue + " " + join + " " + guestTableValue + " ON " + hostTableValue + "." + joinHost + "=" + guestTableValue + "." + joinGuest + "" + where + "" + order + "";
        }
    }
}

function HostTableFieldList() {
    var hostTableValue = DianCMS_$("DropHostTable").value;
    var _select = false;
    var _sql = "";

    for (var i = 0; i < DianCMS_$("ListHostTable").options.length; i++) {
        var select = DianCMS_$("ListHostTable").options[i];
        if (select.selected) {
            _select = true;
            _sql += hostTableValue + "." + DianCMS_$("ListHostTable").options[i].value + ",";
        }
    }

    if (!_select) {
        _sql = hostTableValue + ".*";
    }
    else {
        _sql = _sql.substring(0, _sql.length - 1);
    }

    return _sql;
}

function GuestTableFieldList() {
    var guestTableValue = DianCMS_$("DropGuestTable").value;
    var _select = false;
    var _sql = "";

    for (var i = 0; i < DianCMS_$("ListGuestTable").options.length; i++) {
        var select = DianCMS_$("ListGuestTable").options[i];
        if (select.selected) {
            _select = true;
            _sql += guestTableValue + "." + DianCMS_$("ListGuestTable").options[i].value + ",";
        }
    }

    if (!_select) {
        _sql = guestTableValue + ".*";
    }
    else {
        _sql = _sql.substring(0, _sql.length - 1);
    }

    return _sql;
}

function ExistsSql() {
    var sql = DianCMS_$("txtSql").value;
    var outId = DianCMS_$("DropType").value;
    D.BLL.B_VSLabel.ExistsSql(sql, outId, CallBackSql);
}

function CallBackSql(res) {
    if (res.value) {
        Alert("该SQL语句可以正常使用");
    }
    else {
        Alert("<font color=red>该SQL语句存在错误，请检测</font>");
    }
}

function SetWhere(App, File, Name, css, admin, outdata) {
    var hostTableValue = DianCMS_$("DropHostTable").value;
    var guestTableValue = DianCMS_$("DropGuestTable").value;

    if (hostTableValue == 0) {
        Alert("请选择一张主表");
    }
    else {
        WinOpenLhgDialog('' + Name + '', '' + App + '/' + admin + '/label/DataSource' + File + '.aspx?ControlId=txt' + File + '&hostTableValue=' + hostTableValue + '&guestTableValue=' + guestTableValue + '&Id=' + outdata + '', 700, 400, '' + css + '');
    }
}